]>
Commit | Line | Data |
---|---|---|
abe529af BP |
1 | /* |
2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
5bee6e26 | 19 | #include "ofproto/ofproto-provider.h" |
abe529af BP |
20 | |
21 | #include <errno.h> | |
22 | ||
23 | #include "autopath.h" | |
24 | #include "bond.h" | |
daff3353 | 25 | #include "bundle.h" |
abe529af BP |
26 | #include "byte-order.h" |
27 | #include "connmgr.h" | |
28 | #include "coverage.h" | |
29 | #include "cfm.h" | |
30 | #include "dpif.h" | |
31 | #include "dynamic-string.h" | |
32 | #include "fail-open.h" | |
33 | #include "hmapx.h" | |
34 | #include "lacp.h" | |
75a75043 | 35 | #include "learn.h" |
abe529af BP |
36 | #include "mac-learning.h" |
37 | #include "multipath.h" | |
38 | #include "netdev.h" | |
39 | #include "netlink.h" | |
40 | #include "nx-match.h" | |
41 | #include "odp-util.h" | |
42 | #include "ofp-util.h" | |
43 | #include "ofpbuf.h" | |
44 | #include "ofp-print.h" | |
bae473fe | 45 | #include "ofproto-dpif-sflow.h" |
abe529af BP |
46 | #include "poll-loop.h" |
47 | #include "timer.h" | |
6c1491fb | 48 | #include "unaligned.h" |
abe529af BP |
49 | #include "unixctl.h" |
50 | #include "vlan-bitmap.h" | |
51 | #include "vlog.h" | |
52 | ||
53 | VLOG_DEFINE_THIS_MODULE(ofproto_dpif); | |
54 | ||
55 | COVERAGE_DEFINE(ofproto_dpif_ctlr_action); | |
56 | COVERAGE_DEFINE(ofproto_dpif_expired); | |
57 | COVERAGE_DEFINE(ofproto_dpif_no_packet_in); | |
58 | COVERAGE_DEFINE(ofproto_dpif_xlate); | |
59 | COVERAGE_DEFINE(facet_changed_rule); | |
60 | COVERAGE_DEFINE(facet_invalidated); | |
61 | COVERAGE_DEFINE(facet_revalidate); | |
62 | COVERAGE_DEFINE(facet_unexpected); | |
63 | ||
29901626 | 64 | /* Maximum depth of flow table recursion (due to resubmit actions) in a |
abe529af BP |
65 | * flow translation. */ |
66 | #define MAX_RESUBMIT_RECURSION 16 | |
67 | ||
9cdaaebe BP |
68 | /* Number of implemented OpenFlow tables. */ |
69 | enum { N_TABLES = 255 }; | |
70 | BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255); | |
71 | ||
abe529af BP |
72 | struct ofport_dpif; |
73 | struct ofproto_dpif; | |
74 | ||
75 | struct rule_dpif { | |
76 | struct rule up; | |
77 | ||
78 | long long int used; /* Time last used; time created if not used. */ | |
79 | ||
80 | /* These statistics: | |
81 | * | |
82 | * - Do include packets and bytes from facets that have been deleted or | |
83 | * whose own statistics have been folded into the rule. | |
84 | * | |
85 | * - Do include packets and bytes sent "by hand" that were accounted to | |
86 | * the rule without any facet being involved (this is a rare corner | |
87 | * case in rule_execute()). | |
88 | * | |
89 | * - Do not include packet or bytes that can be obtained from any facet's | |
90 | * packet_count or byte_count member or that can be obtained from the | |
91 | * datapath by, e.g., dpif_flow_get() for any facet. | |
92 | */ | |
93 | uint64_t packet_count; /* Number of packets received. */ | |
94 | uint64_t byte_count; /* Number of bytes received. */ | |
95 | ||
54a9cbc9 BP |
96 | tag_type tag; /* Caches rule_calculate_tag() result. */ |
97 | ||
abe529af BP |
98 | struct list facets; /* List of "struct facet"s. */ |
99 | }; | |
100 | ||
101 | static struct rule_dpif *rule_dpif_cast(const struct rule *rule) | |
102 | { | |
103 | return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL; | |
104 | } | |
105 | ||
29901626 BP |
106 | static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *, |
107 | const struct flow *, uint8_t table); | |
abe529af BP |
108 | |
109 | #define MAX_MIRRORS 32 | |
110 | typedef uint32_t mirror_mask_t; | |
111 | #define MIRROR_MASK_C(X) UINT32_C(X) | |
112 | BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS); | |
113 | struct ofmirror { | |
114 | struct ofproto_dpif *ofproto; /* Owning ofproto. */ | |
115 | size_t idx; /* In ofproto's "mirrors" array. */ | |
116 | void *aux; /* Key supplied by ofproto's client. */ | |
117 | char *name; /* Identifier for log messages. */ | |
118 | ||
119 | /* Selection criteria. */ | |
120 | struct hmapx srcs; /* Contains "struct ofbundle *"s. */ | |
121 | struct hmapx dsts; /* Contains "struct ofbundle *"s. */ | |
122 | unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */ | |
123 | ||
124 | /* Output (mutually exclusive). */ | |
125 | struct ofbundle *out; /* Output port or NULL. */ | |
126 | int out_vlan; /* Output VLAN or -1. */ | |
127 | }; | |
128 | ||
129 | static void mirror_destroy(struct ofmirror *); | |
130 | ||
131 | /* A group of one or more OpenFlow ports. */ | |
132 | #define OFBUNDLE_FLOOD ((struct ofbundle *) 1) | |
133 | struct ofbundle { | |
134 | struct ofproto_dpif *ofproto; /* Owning ofproto. */ | |
135 | struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */ | |
136 | void *aux; /* Key supplied by ofproto's client. */ | |
137 | char *name; /* Identifier for log messages. */ | |
138 | ||
139 | /* Configuration. */ | |
140 | struct list ports; /* Contains "struct ofport"s. */ | |
ecac4ebf | 141 | enum port_vlan_mode vlan_mode; /* VLAN mode */ |
abe529af BP |
142 | int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */ |
143 | unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. | |
144 | * NULL if all VLANs are trunked. */ | |
145 | struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */ | |
146 | struct bond *bond; /* Nonnull iff more than one port. */ | |
147 | ||
148 | /* Status. */ | |
149 | bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */ | |
150 | ||
151 | /* Port mirroring info. */ | |
152 | mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */ | |
153 | mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */ | |
154 | mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */ | |
155 | }; | |
156 | ||
157 | static void bundle_remove(struct ofport *); | |
158 | static void bundle_destroy(struct ofbundle *); | |
159 | static void bundle_del_port(struct ofport_dpif *); | |
160 | static void bundle_run(struct ofbundle *); | |
161 | static void bundle_wait(struct ofbundle *); | |
162 | ||
163 | struct action_xlate_ctx { | |
164 | /* action_xlate_ctx_init() initializes these members. */ | |
165 | ||
166 | /* The ofproto. */ | |
167 | struct ofproto_dpif *ofproto; | |
168 | ||
169 | /* Flow to which the OpenFlow actions apply. xlate_actions() will modify | |
170 | * this flow when actions change header fields. */ | |
171 | struct flow flow; | |
172 | ||
173 | /* The packet corresponding to 'flow', or a null pointer if we are | |
174 | * revalidating without a packet to refer to. */ | |
175 | const struct ofpbuf *packet; | |
176 | ||
75a75043 BP |
177 | /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We |
178 | * want to execute them if we are actually processing a packet, or if we | |
179 | * are accounting for packets that the datapath has processed, but not if | |
180 | * we are just revalidating. */ | |
181 | bool may_learn; | |
182 | ||
abe529af BP |
183 | /* If nonnull, called just before executing a resubmit action. |
184 | * | |
185 | * This is normally null so the client has to set it manually after | |
186 | * calling action_xlate_ctx_init(). */ | |
187 | void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *); | |
188 | ||
abe529af BP |
189 | /* xlate_actions() initializes and uses these members. The client might want |
190 | * to look at them after it returns. */ | |
191 | ||
192 | struct ofpbuf *odp_actions; /* Datapath actions. */ | |
75a75043 | 193 | tag_type tags; /* Tags associated with actions. */ |
abe529af BP |
194 | bool may_set_up_flow; /* True ordinarily; false if the actions must |
195 | * be reassessed for every packet. */ | |
75a75043 BP |
196 | bool has_learn; /* Actions include NXAST_LEARN? */ |
197 | bool has_normal; /* Actions output to OFPP_NORMAL? */ | |
abe529af BP |
198 | uint16_t nf_output_iface; /* Output interface index for NetFlow. */ |
199 | ||
200 | /* xlate_actions() initializes and uses these members, but the client has no | |
201 | * reason to look at them. */ | |
202 | ||
203 | int recurse; /* Recursion level, via xlate_table_action. */ | |
afabef2b | 204 | uint32_t priority; /* Current flow priority. 0 if none. */ |
b3e9b2ed EJ |
205 | struct flow base_flow; /* Flow at the last commit. */ |
206 | uint32_t base_priority; /* Priority at the last commit. */ | |
29901626 | 207 | uint8_t table_id; /* OpenFlow table ID where flow was found. */ |
6ff686f2 PS |
208 | uint32_t sflow_n_outputs; /* Number of output ports. */ |
209 | uint16_t sflow_odp_port; /* Output port for composing sFlow action. */ | |
210 | uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */ | |
abe529af BP |
211 | }; |
212 | ||
213 | static void action_xlate_ctx_init(struct action_xlate_ctx *, | |
214 | struct ofproto_dpif *, const struct flow *, | |
215 | const struct ofpbuf *); | |
216 | static struct ofpbuf *xlate_actions(struct action_xlate_ctx *, | |
217 | const union ofp_action *in, size_t n_in); | |
218 | ||
219 | /* An exact-match instantiation of an OpenFlow flow. */ | |
220 | struct facet { | |
221 | long long int used; /* Time last used; time created if not used. */ | |
222 | ||
223 | /* These statistics: | |
224 | * | |
225 | * - Do include packets and bytes sent "by hand", e.g. with | |
226 | * dpif_execute(). | |
227 | * | |
228 | * - Do include packets and bytes that were obtained from the datapath | |
907a4c5e | 229 | * when its statistics were reset (e.g. dpif_flow_put() with |
abe529af | 230 | * DPIF_FP_ZERO_STATS). |
abe529af BP |
231 | */ |
232 | uint64_t packet_count; /* Number of packets received. */ | |
233 | uint64_t byte_count; /* Number of bytes received. */ | |
234 | ||
235 | uint64_t dp_packet_count; /* Last known packet count in the datapath. */ | |
236 | uint64_t dp_byte_count; /* Last known byte count in the datapath. */ | |
237 | ||
238 | uint64_t rs_packet_count; /* Packets pushed to resubmit children. */ | |
239 | uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */ | |
240 | long long int rs_used; /* Used time pushed to resubmit children. */ | |
241 | ||
907a4c5e | 242 | uint64_t accounted_bytes; /* Bytes processed by facet_account(). */ |
abe529af BP |
243 | |
244 | struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */ | |
245 | struct list list_node; /* In owning rule's 'facets' list. */ | |
246 | struct rule_dpif *rule; /* Owning rule. */ | |
247 | struct flow flow; /* Exact-match flow. */ | |
248 | bool installed; /* Installed in datapath? */ | |
249 | bool may_install; /* True ordinarily; false if actions must | |
250 | * be reassessed for every packet. */ | |
75a75043 BP |
251 | bool has_learn; /* Actions include NXAST_LEARN? */ |
252 | bool has_normal; /* Actions output to OFPP_NORMAL? */ | |
abe529af BP |
253 | size_t actions_len; /* Number of bytes in actions[]. */ |
254 | struct nlattr *actions; /* Datapath actions. */ | |
255 | tag_type tags; /* Tags. */ | |
256 | struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */ | |
257 | }; | |
258 | ||
259 | static struct facet *facet_create(struct rule_dpif *, const struct flow *, | |
260 | const struct ofpbuf *packet); | |
261 | static void facet_remove(struct ofproto_dpif *, struct facet *); | |
262 | static void facet_free(struct facet *); | |
263 | ||
264 | static struct facet *facet_find(struct ofproto_dpif *, const struct flow *); | |
265 | static struct facet *facet_lookup_valid(struct ofproto_dpif *, | |
266 | const struct flow *); | |
267 | static bool facet_revalidate(struct ofproto_dpif *, struct facet *); | |
268 | ||
269 | static void facet_execute(struct ofproto_dpif *, struct facet *, | |
270 | struct ofpbuf *packet); | |
271 | ||
272 | static int facet_put__(struct ofproto_dpif *, struct facet *, | |
273 | const struct nlattr *actions, size_t actions_len, | |
274 | struct dpif_flow_stats *); | |
275 | static void facet_install(struct ofproto_dpif *, struct facet *, | |
276 | bool zero_stats); | |
277 | static void facet_uninstall(struct ofproto_dpif *, struct facet *); | |
278 | static void facet_flush_stats(struct ofproto_dpif *, struct facet *); | |
279 | ||
280 | static void facet_make_actions(struct ofproto_dpif *, struct facet *, | |
281 | const struct ofpbuf *packet); | |
282 | static void facet_update_time(struct ofproto_dpif *, struct facet *, | |
283 | long long int used); | |
284 | static void facet_update_stats(struct ofproto_dpif *, struct facet *, | |
285 | const struct dpif_flow_stats *); | |
bbb5d219 | 286 | static void facet_reset_counters(struct facet *); |
3a88e544 | 287 | static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *); |
abe529af | 288 | static void facet_push_stats(struct facet *); |
55af77bb | 289 | static void facet_account(struct ofproto_dpif *, struct facet *); |
abe529af BP |
290 | |
291 | static bool facet_is_controller_flow(struct facet *); | |
292 | ||
293 | static void flow_push_stats(const struct rule_dpif *, | |
294 | struct flow *, uint64_t packets, uint64_t bytes, | |
295 | long long int used); | |
296 | ||
54a9cbc9 BP |
297 | static uint32_t rule_calculate_tag(const struct flow *, |
298 | const struct flow_wildcards *, | |
299 | uint32_t basis); | |
300 | static void rule_invalidate(const struct rule_dpif *); | |
301 | ||
abe529af BP |
302 | struct ofport_dpif { |
303 | struct ofport up; | |
304 | ||
305 | uint32_t odp_port; | |
306 | struct ofbundle *bundle; /* Bundle that contains this port, if any. */ | |
307 | struct list bundle_node; /* In struct ofbundle's "ports" list. */ | |
308 | struct cfm *cfm; /* Connectivity Fault Management, if any. */ | |
309 | tag_type tag; /* Tag associated with this port. */ | |
00794817 | 310 | uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */ |
015e08bc | 311 | bool may_enable; /* May be enabled in bonds. */ |
abe529af BP |
312 | }; |
313 | ||
314 | static struct ofport_dpif * | |
315 | ofport_dpif_cast(const struct ofport *ofport) | |
316 | { | |
317 | assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class); | |
318 | return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL; | |
319 | } | |
320 | ||
321 | static void port_run(struct ofport_dpif *); | |
322 | static void port_wait(struct ofport_dpif *); | |
a5610457 | 323 | static int set_cfm(struct ofport *, const struct cfm_settings *); |
abe529af | 324 | |
7ee20df1 BP |
325 | struct dpif_completion { |
326 | struct list list_node; | |
327 | struct ofoperation *op; | |
328 | }; | |
329 | ||
54a9cbc9 BP |
330 | /* Extra information about a classifier table. |
331 | * Currently used just for optimized flow revalidation. */ | |
332 | struct table_dpif { | |
333 | /* If either of these is nonnull, then this table has a form that allows | |
334 | * flows to be tagged to avoid revalidating most flows for the most common | |
335 | * kinds of flow table changes. */ | |
336 | struct cls_table *catchall_table; /* Table that wildcards all fields. */ | |
337 | struct cls_table *other_table; /* Table with any other wildcard set. */ | |
338 | uint32_t basis; /* Keeps each table's tags separate. */ | |
339 | }; | |
340 | ||
abe529af BP |
341 | struct ofproto_dpif { |
342 | struct ofproto up; | |
343 | struct dpif *dpif; | |
344 | int max_ports; | |
345 | ||
6c1491fb BP |
346 | /* Statistics. */ |
347 | uint64_t n_matches; | |
348 | ||
abe529af BP |
349 | /* Bridging. */ |
350 | struct netflow *netflow; | |
bae473fe | 351 | struct dpif_sflow *sflow; |
abe529af BP |
352 | struct hmap bundles; /* Contains "struct ofbundle"s. */ |
353 | struct mac_learning *ml; | |
354 | struct ofmirror *mirrors[MAX_MIRRORS]; | |
355 | bool has_bonded_bundles; | |
356 | ||
357 | /* Expiration. */ | |
358 | struct timer next_expiration; | |
359 | ||
360 | /* Facets. */ | |
361 | struct hmap facets; | |
54a9cbc9 BP |
362 | |
363 | /* Revalidation. */ | |
364 | struct table_dpif tables[N_TABLES]; | |
abe529af BP |
365 | bool need_revalidate; |
366 | struct tag_set revalidate_set; | |
7ee20df1 BP |
367 | |
368 | /* Support for debugging async flow mods. */ | |
369 | struct list completions; | |
daff3353 EJ |
370 | |
371 | bool has_bundle_action; /* True when the first bundle action appears. */ | |
abe529af BP |
372 | }; |
373 | ||
7ee20df1 BP |
374 | /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only |
375 | * for debugging the asynchronous flow_mod implementation.) */ | |
376 | static bool clogged; | |
377 | ||
abe529af BP |
378 | static void ofproto_dpif_unixctl_init(void); |
379 | ||
380 | static struct ofproto_dpif * | |
381 | ofproto_dpif_cast(const struct ofproto *ofproto) | |
382 | { | |
383 | assert(ofproto->ofproto_class == &ofproto_dpif_class); | |
384 | return CONTAINER_OF(ofproto, struct ofproto_dpif, up); | |
385 | } | |
386 | ||
387 | static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *, | |
388 | uint16_t ofp_port); | |
389 | static struct ofport_dpif *get_odp_port(struct ofproto_dpif *, | |
390 | uint32_t odp_port); | |
391 | ||
392 | /* Packet processing. */ | |
393 | static void update_learning_table(struct ofproto_dpif *, | |
394 | const struct flow *, int vlan, | |
395 | struct ofbundle *); | |
396 | static bool is_admissible(struct ofproto_dpif *, const struct flow *, | |
397 | bool have_packet, tag_type *, int *vlanp, | |
398 | struct ofbundle **in_bundlep); | |
399 | static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *); | |
400 | ||
401 | /* Flow expiration. */ | |
402 | static int expire(struct ofproto_dpif *); | |
403 | ||
404 | /* Utilities. */ | |
b2fda3ef | 405 | static int send_packet(struct ofproto_dpif *, uint32_t odp_port, |
abe529af | 406 | const struct ofpbuf *packet); |
6ff686f2 PS |
407 | static size_t |
408 | compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions, | |
409 | const struct flow *, uint32_t odp_port); | |
abe529af BP |
410 | /* Global variables. */ |
411 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
412 | \f | |
413 | /* Factory functions. */ | |
414 | ||
415 | static void | |
416 | enumerate_types(struct sset *types) | |
417 | { | |
418 | dp_enumerate_types(types); | |
419 | } | |
420 | ||
421 | static int | |
422 | enumerate_names(const char *type, struct sset *names) | |
423 | { | |
424 | return dp_enumerate_names(type, names); | |
425 | } | |
426 | ||
427 | static int | |
428 | del(const char *type, const char *name) | |
429 | { | |
430 | struct dpif *dpif; | |
431 | int error; | |
432 | ||
433 | error = dpif_open(name, type, &dpif); | |
434 | if (!error) { | |
435 | error = dpif_delete(dpif); | |
436 | dpif_close(dpif); | |
437 | } | |
438 | return error; | |
439 | } | |
440 | \f | |
441 | /* Basic life-cycle. */ | |
442 | ||
443 | static struct ofproto * | |
444 | alloc(void) | |
445 | { | |
446 | struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto); | |
447 | return &ofproto->up; | |
448 | } | |
449 | ||
450 | static void | |
451 | dealloc(struct ofproto *ofproto_) | |
452 | { | |
453 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
454 | free(ofproto); | |
455 | } | |
456 | ||
457 | static int | |
073e2a6f | 458 | construct(struct ofproto *ofproto_, int *n_tablesp) |
abe529af BP |
459 | { |
460 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
461 | const char *name = ofproto->up.name; | |
462 | int error; | |
463 | int i; | |
464 | ||
465 | error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif); | |
466 | if (error) { | |
467 | VLOG_ERR("failed to open datapath %s: %s", name, strerror(error)); | |
468 | return error; | |
469 | } | |
470 | ||
471 | ofproto->max_ports = dpif_get_max_ports(ofproto->dpif); | |
6c1491fb | 472 | ofproto->n_matches = 0; |
abe529af | 473 | |
be8194bb JG |
474 | dpif_flow_flush(ofproto->dpif); |
475 | dpif_recv_purge(ofproto->dpif); | |
476 | ||
abe529af BP |
477 | error = dpif_recv_set_mask(ofproto->dpif, |
478 | ((1u << DPIF_UC_MISS) | | |
6ff686f2 | 479 | (1u << DPIF_UC_ACTION))); |
abe529af BP |
480 | if (error) { |
481 | VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error)); | |
482 | dpif_close(ofproto->dpif); | |
483 | return error; | |
484 | } | |
abe529af BP |
485 | |
486 | ofproto->netflow = NULL; | |
487 | ofproto->sflow = NULL; | |
488 | hmap_init(&ofproto->bundles); | |
489 | ofproto->ml = mac_learning_create(); | |
490 | for (i = 0; i < MAX_MIRRORS; i++) { | |
491 | ofproto->mirrors[i] = NULL; | |
492 | } | |
493 | ofproto->has_bonded_bundles = false; | |
494 | ||
495 | timer_set_duration(&ofproto->next_expiration, 1000); | |
496 | ||
497 | hmap_init(&ofproto->facets); | |
54a9cbc9 BP |
498 | |
499 | for (i = 0; i < N_TABLES; i++) { | |
500 | struct table_dpif *table = &ofproto->tables[i]; | |
501 | ||
502 | table->catchall_table = NULL; | |
503 | table->other_table = NULL; | |
504 | table->basis = random_uint32(); | |
505 | } | |
abe529af BP |
506 | ofproto->need_revalidate = false; |
507 | tag_set_init(&ofproto->revalidate_set); | |
508 | ||
7ee20df1 BP |
509 | list_init(&ofproto->completions); |
510 | ||
abe529af BP |
511 | ofproto_dpif_unixctl_init(); |
512 | ||
daff3353 EJ |
513 | ofproto->has_bundle_action = false; |
514 | ||
9cdaaebe | 515 | *n_tablesp = N_TABLES; |
abe529af BP |
516 | return 0; |
517 | } | |
518 | ||
7ee20df1 BP |
519 | static void |
520 | complete_operations(struct ofproto_dpif *ofproto) | |
521 | { | |
522 | struct dpif_completion *c, *next; | |
523 | ||
524 | LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) { | |
525 | ofoperation_complete(c->op, 0); | |
526 | list_remove(&c->list_node); | |
527 | free(c); | |
528 | } | |
529 | } | |
530 | ||
abe529af BP |
531 | static void |
532 | destruct(struct ofproto *ofproto_) | |
533 | { | |
534 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
7ee20df1 | 535 | struct rule_dpif *rule, *next_rule; |
0697b5c3 | 536 | struct classifier *table; |
abe529af BP |
537 | int i; |
538 | ||
7ee20df1 BP |
539 | complete_operations(ofproto); |
540 | ||
0697b5c3 BP |
541 | OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) { |
542 | struct cls_cursor cursor; | |
543 | ||
544 | cls_cursor_init(&cursor, table, NULL); | |
545 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) { | |
546 | ofproto_rule_destroy(&rule->up); | |
547 | } | |
7ee20df1 BP |
548 | } |
549 | ||
abe529af BP |
550 | for (i = 0; i < MAX_MIRRORS; i++) { |
551 | mirror_destroy(ofproto->mirrors[i]); | |
552 | } | |
553 | ||
554 | netflow_destroy(ofproto->netflow); | |
bae473fe | 555 | dpif_sflow_destroy(ofproto->sflow); |
abe529af BP |
556 | hmap_destroy(&ofproto->bundles); |
557 | mac_learning_destroy(ofproto->ml); | |
558 | ||
559 | hmap_destroy(&ofproto->facets); | |
560 | ||
561 | dpif_close(ofproto->dpif); | |
562 | } | |
563 | ||
564 | static int | |
565 | run(struct ofproto *ofproto_) | |
566 | { | |
567 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
568 | struct ofport_dpif *ofport; | |
569 | struct ofbundle *bundle; | |
570 | int i; | |
571 | ||
7ee20df1 BP |
572 | if (!clogged) { |
573 | complete_operations(ofproto); | |
574 | } | |
abe529af BP |
575 | dpif_run(ofproto->dpif); |
576 | ||
577 | for (i = 0; i < 50; i++) { | |
578 | struct dpif_upcall packet; | |
579 | int error; | |
580 | ||
581 | error = dpif_recv(ofproto->dpif, &packet); | |
582 | if (error) { | |
583 | if (error == ENODEV) { | |
584 | /* Datapath destroyed. */ | |
585 | return error; | |
586 | } | |
587 | break; | |
588 | } | |
589 | ||
590 | handle_upcall(ofproto, &packet); | |
591 | } | |
592 | ||
593 | if (timer_expired(&ofproto->next_expiration)) { | |
594 | int delay = expire(ofproto); | |
595 | timer_set_duration(&ofproto->next_expiration, delay); | |
596 | } | |
597 | ||
598 | if (ofproto->netflow) { | |
599 | netflow_run(ofproto->netflow); | |
600 | } | |
601 | if (ofproto->sflow) { | |
bae473fe | 602 | dpif_sflow_run(ofproto->sflow); |
abe529af BP |
603 | } |
604 | ||
605 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
606 | port_run(ofport); | |
607 | } | |
608 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
609 | bundle_run(bundle); | |
610 | } | |
611 | ||
1c313b88 BP |
612 | mac_learning_run(ofproto->ml, &ofproto->revalidate_set); |
613 | ||
abe529af BP |
614 | /* Now revalidate if there's anything to do. */ |
615 | if (ofproto->need_revalidate | |
616 | || !tag_set_is_empty(&ofproto->revalidate_set)) { | |
617 | struct tag_set revalidate_set = ofproto->revalidate_set; | |
618 | bool revalidate_all = ofproto->need_revalidate; | |
619 | struct facet *facet, *next; | |
620 | ||
621 | /* Clear the revalidation flags. */ | |
622 | tag_set_init(&ofproto->revalidate_set); | |
623 | ofproto->need_revalidate = false; | |
624 | ||
625 | HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) { | |
626 | if (revalidate_all | |
627 | || tag_set_intersects(&revalidate_set, facet->tags)) { | |
628 | facet_revalidate(ofproto, facet); | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | return 0; | |
634 | } | |
635 | ||
636 | static void | |
637 | wait(struct ofproto *ofproto_) | |
638 | { | |
639 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
640 | struct ofport_dpif *ofport; | |
641 | struct ofbundle *bundle; | |
642 | ||
7ee20df1 BP |
643 | if (!clogged && !list_is_empty(&ofproto->completions)) { |
644 | poll_immediate_wake(); | |
645 | } | |
646 | ||
abe529af BP |
647 | dpif_wait(ofproto->dpif); |
648 | dpif_recv_wait(ofproto->dpif); | |
649 | if (ofproto->sflow) { | |
bae473fe | 650 | dpif_sflow_wait(ofproto->sflow); |
abe529af BP |
651 | } |
652 | if (!tag_set_is_empty(&ofproto->revalidate_set)) { | |
653 | poll_immediate_wake(); | |
654 | } | |
655 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
656 | port_wait(ofport); | |
657 | } | |
658 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
659 | bundle_wait(bundle); | |
660 | } | |
1c313b88 | 661 | mac_learning_wait(ofproto->ml); |
abe529af BP |
662 | if (ofproto->need_revalidate) { |
663 | /* Shouldn't happen, but if it does just go around again. */ | |
664 | VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()"); | |
665 | poll_immediate_wake(); | |
666 | } else { | |
667 | timer_wait(&ofproto->next_expiration); | |
668 | } | |
669 | } | |
670 | ||
671 | static void | |
672 | flush(struct ofproto *ofproto_) | |
673 | { | |
674 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
675 | struct facet *facet, *next_facet; | |
676 | ||
677 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
678 | /* Mark the facet as not installed so that facet_remove() doesn't | |
679 | * bother trying to uninstall it. There is no point in uninstalling it | |
680 | * individually since we are about to blow away all the facets with | |
681 | * dpif_flow_flush(). */ | |
682 | facet->installed = false; | |
683 | facet->dp_packet_count = 0; | |
684 | facet->dp_byte_count = 0; | |
685 | facet_remove(ofproto, facet); | |
686 | } | |
687 | dpif_flow_flush(ofproto->dpif); | |
688 | } | |
689 | ||
6c1491fb BP |
690 | static void |
691 | get_features(struct ofproto *ofproto_ OVS_UNUSED, | |
692 | bool *arp_match_ip, uint32_t *actions) | |
693 | { | |
694 | *arp_match_ip = true; | |
695 | *actions = ((1u << OFPAT_OUTPUT) | | |
696 | (1u << OFPAT_SET_VLAN_VID) | | |
697 | (1u << OFPAT_SET_VLAN_PCP) | | |
698 | (1u << OFPAT_STRIP_VLAN) | | |
699 | (1u << OFPAT_SET_DL_SRC) | | |
700 | (1u << OFPAT_SET_DL_DST) | | |
701 | (1u << OFPAT_SET_NW_SRC) | | |
702 | (1u << OFPAT_SET_NW_DST) | | |
703 | (1u << OFPAT_SET_NW_TOS) | | |
704 | (1u << OFPAT_SET_TP_SRC) | | |
705 | (1u << OFPAT_SET_TP_DST) | | |
706 | (1u << OFPAT_ENQUEUE)); | |
707 | } | |
708 | ||
709 | static void | |
710 | get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots) | |
711 | { | |
712 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
a8d9304d | 713 | struct dpif_dp_stats s; |
6c1491fb BP |
714 | |
715 | strcpy(ots->name, "classifier"); | |
716 | ||
717 | dpif_get_dp_stats(ofproto->dpif, &s); | |
718 | put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed)); | |
719 | put_32aligned_be64(&ots->matched_count, | |
720 | htonll(s.n_hit + ofproto->n_matches)); | |
721 | } | |
722 | ||
abe529af BP |
723 | static int |
724 | set_netflow(struct ofproto *ofproto_, | |
725 | const struct netflow_options *netflow_options) | |
726 | { | |
727 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
728 | ||
729 | if (netflow_options) { | |
730 | if (!ofproto->netflow) { | |
731 | ofproto->netflow = netflow_create(); | |
732 | } | |
733 | return netflow_set_options(ofproto->netflow, netflow_options); | |
734 | } else { | |
735 | netflow_destroy(ofproto->netflow); | |
736 | ofproto->netflow = NULL; | |
737 | return 0; | |
738 | } | |
739 | } | |
740 | ||
741 | static struct ofport * | |
742 | port_alloc(void) | |
743 | { | |
744 | struct ofport_dpif *port = xmalloc(sizeof *port); | |
745 | return &port->up; | |
746 | } | |
747 | ||
748 | static void | |
749 | port_dealloc(struct ofport *port_) | |
750 | { | |
751 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
752 | free(port); | |
753 | } | |
754 | ||
755 | static int | |
756 | port_construct(struct ofport *port_) | |
757 | { | |
758 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
759 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
760 | ||
f11c28c4 | 761 | ofproto->need_revalidate = true; |
abe529af BP |
762 | port->odp_port = ofp_port_to_odp_port(port->up.ofp_port); |
763 | port->bundle = NULL; | |
764 | port->cfm = NULL; | |
765 | port->tag = tag_create_random(); | |
d5ffa7f2 | 766 | port->may_enable = true; |
abe529af BP |
767 | |
768 | if (ofproto->sflow) { | |
bae473fe JP |
769 | dpif_sflow_add_port(ofproto->sflow, port->odp_port, |
770 | netdev_get_name(port->up.netdev)); | |
abe529af BP |
771 | } |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | static void | |
777 | port_destruct(struct ofport *port_) | |
778 | { | |
779 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
780 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
781 | ||
f11c28c4 | 782 | ofproto->need_revalidate = true; |
abe529af | 783 | bundle_remove(port_); |
a5610457 | 784 | set_cfm(port_, NULL); |
abe529af | 785 | if (ofproto->sflow) { |
bae473fe | 786 | dpif_sflow_del_port(ofproto->sflow, port->odp_port); |
abe529af BP |
787 | } |
788 | } | |
789 | ||
790 | static void | |
791 | port_modified(struct ofport *port_) | |
792 | { | |
793 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
794 | ||
795 | if (port->bundle && port->bundle->bond) { | |
796 | bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev); | |
797 | } | |
798 | } | |
799 | ||
800 | static void | |
801 | port_reconfigured(struct ofport *port_, ovs_be32 old_config) | |
802 | { | |
803 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
804 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
805 | ovs_be32 changed = old_config ^ port->up.opp.config; | |
806 | ||
807 | if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP | | |
808 | OFPPC_NO_FWD | OFPPC_NO_FLOOD)) { | |
809 | ofproto->need_revalidate = true; | |
810 | } | |
811 | } | |
812 | ||
813 | static int | |
814 | set_sflow(struct ofproto *ofproto_, | |
815 | const struct ofproto_sflow_options *sflow_options) | |
816 | { | |
817 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
bae473fe | 818 | struct dpif_sflow *ds = ofproto->sflow; |
6ff686f2 | 819 | |
abe529af | 820 | if (sflow_options) { |
bae473fe | 821 | if (!ds) { |
abe529af BP |
822 | struct ofport_dpif *ofport; |
823 | ||
bae473fe | 824 | ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif); |
abe529af | 825 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { |
bae473fe JP |
826 | dpif_sflow_add_port(ds, ofport->odp_port, |
827 | netdev_get_name(ofport->up.netdev)); | |
abe529af | 828 | } |
6ff686f2 | 829 | ofproto->need_revalidate = true; |
abe529af | 830 | } |
bae473fe | 831 | dpif_sflow_set_options(ds, sflow_options); |
abe529af | 832 | } else { |
6ff686f2 PS |
833 | if (ds) { |
834 | dpif_sflow_destroy(ds); | |
835 | ofproto->need_revalidate = true; | |
836 | ofproto->sflow = NULL; | |
837 | } | |
abe529af BP |
838 | } |
839 | return 0; | |
840 | } | |
841 | ||
842 | static int | |
a5610457 | 843 | set_cfm(struct ofport *ofport_, const struct cfm_settings *s) |
abe529af BP |
844 | { |
845 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
846 | int error; | |
847 | ||
a5610457 | 848 | if (!s) { |
abe529af BP |
849 | error = 0; |
850 | } else { | |
851 | if (!ofport->cfm) { | |
8c977421 EJ |
852 | struct ofproto_dpif *ofproto; |
853 | ||
854 | ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
855 | ofproto->need_revalidate = true; | |
6f629657 | 856 | ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev)); |
abe529af BP |
857 | } |
858 | ||
a5610457 | 859 | if (cfm_configure(ofport->cfm, s)) { |
abe529af BP |
860 | return 0; |
861 | } | |
862 | ||
863 | error = EINVAL; | |
864 | } | |
865 | cfm_destroy(ofport->cfm); | |
866 | ofport->cfm = NULL; | |
867 | return error; | |
868 | } | |
869 | ||
870 | static int | |
a5610457 | 871 | get_cfm_fault(const struct ofport *ofport_) |
abe529af BP |
872 | { |
873 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
a5610457 EJ |
874 | |
875 | return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1; | |
abe529af | 876 | } |
1de11730 EJ |
877 | |
878 | static int | |
879 | get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps, | |
880 | size_t *n_rmps) | |
881 | { | |
882 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
883 | ||
884 | if (ofport->cfm) { | |
885 | cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps); | |
886 | return 0; | |
887 | } else { | |
888 | return -1; | |
889 | } | |
890 | } | |
abe529af BP |
891 | \f |
892 | /* Bundles. */ | |
893 | ||
894 | /* Expires all MAC learning entries associated with 'port' and forces ofproto | |
895 | * to revalidate every flow. */ | |
896 | static void | |
897 | bundle_flush_macs(struct ofbundle *bundle) | |
898 | { | |
899 | struct ofproto_dpif *ofproto = bundle->ofproto; | |
900 | struct mac_learning *ml = ofproto->ml; | |
901 | struct mac_entry *mac, *next_mac; | |
902 | ||
903 | ofproto->need_revalidate = true; | |
904 | LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) { | |
905 | if (mac->port.p == bundle) { | |
906 | mac_learning_expire(ml, mac); | |
907 | } | |
908 | } | |
909 | } | |
910 | ||
911 | static struct ofbundle * | |
912 | bundle_lookup(const struct ofproto_dpif *ofproto, void *aux) | |
913 | { | |
914 | struct ofbundle *bundle; | |
915 | ||
916 | HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0), | |
917 | &ofproto->bundles) { | |
918 | if (bundle->aux == aux) { | |
919 | return bundle; | |
920 | } | |
921 | } | |
922 | return NULL; | |
923 | } | |
924 | ||
925 | /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the | |
926 | * ones that are found to 'bundles'. */ | |
927 | static void | |
928 | bundle_lookup_multiple(struct ofproto_dpif *ofproto, | |
929 | void **auxes, size_t n_auxes, | |
930 | struct hmapx *bundles) | |
931 | { | |
932 | size_t i; | |
933 | ||
934 | hmapx_init(bundles); | |
935 | for (i = 0; i < n_auxes; i++) { | |
936 | struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]); | |
937 | if (bundle) { | |
938 | hmapx_add(bundles, bundle); | |
939 | } | |
940 | } | |
941 | } | |
942 | ||
943 | static void | |
944 | bundle_del_port(struct ofport_dpif *port) | |
945 | { | |
946 | struct ofbundle *bundle = port->bundle; | |
947 | ||
6f77f4ae BP |
948 | bundle->ofproto->need_revalidate = true; |
949 | ||
abe529af BP |
950 | list_remove(&port->bundle_node); |
951 | port->bundle = NULL; | |
952 | ||
953 | if (bundle->lacp) { | |
954 | lacp_slave_unregister(bundle->lacp, port); | |
955 | } | |
956 | if (bundle->bond) { | |
957 | bond_slave_unregister(bundle->bond, port); | |
958 | } | |
959 | ||
960 | bundle->floodable = true; | |
961 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
962 | if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) { | |
963 | bundle->floodable = false; | |
964 | } | |
965 | } | |
966 | } | |
967 | ||
968 | static bool | |
969 | bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port, | |
00794817 BP |
970 | struct lacp_slave_settings *lacp, |
971 | uint32_t bond_stable_id) | |
abe529af BP |
972 | { |
973 | struct ofport_dpif *port; | |
974 | ||
975 | port = get_ofp_port(bundle->ofproto, ofp_port); | |
976 | if (!port) { | |
977 | return false; | |
978 | } | |
979 | ||
980 | if (port->bundle != bundle) { | |
6f77f4ae | 981 | bundle->ofproto->need_revalidate = true; |
abe529af BP |
982 | if (port->bundle) { |
983 | bundle_del_port(port); | |
984 | } | |
985 | ||
986 | port->bundle = bundle; | |
987 | list_push_back(&bundle->ports, &port->bundle_node); | |
988 | if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) { | |
989 | bundle->floodable = false; | |
990 | } | |
991 | } | |
992 | if (lacp) { | |
4a86aece | 993 | port->bundle->ofproto->need_revalidate = true; |
abe529af BP |
994 | lacp_slave_register(bundle->lacp, port, lacp); |
995 | } | |
996 | ||
00794817 BP |
997 | port->bond_stable_id = bond_stable_id; |
998 | ||
abe529af BP |
999 | return true; |
1000 | } | |
1001 | ||
1002 | static void | |
1003 | bundle_destroy(struct ofbundle *bundle) | |
1004 | { | |
1005 | struct ofproto_dpif *ofproto; | |
1006 | struct ofport_dpif *port, *next_port; | |
1007 | int i; | |
1008 | ||
1009 | if (!bundle) { | |
1010 | return; | |
1011 | } | |
1012 | ||
1013 | ofproto = bundle->ofproto; | |
1014 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1015 | struct ofmirror *m = ofproto->mirrors[i]; | |
1016 | if (m) { | |
1017 | if (m->out == bundle) { | |
1018 | mirror_destroy(m); | |
1019 | } else if (hmapx_find_and_delete(&m->srcs, bundle) | |
1020 | || hmapx_find_and_delete(&m->dsts, bundle)) { | |
1021 | ofproto->need_revalidate = true; | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) { | |
1027 | bundle_del_port(port); | |
1028 | } | |
1029 | ||
1030 | bundle_flush_macs(bundle); | |
1031 | hmap_remove(&ofproto->bundles, &bundle->hmap_node); | |
1032 | free(bundle->name); | |
1033 | free(bundle->trunks); | |
1034 | lacp_destroy(bundle->lacp); | |
1035 | bond_destroy(bundle->bond); | |
1036 | free(bundle); | |
1037 | } | |
1038 | ||
1039 | static int | |
1040 | bundle_set(struct ofproto *ofproto_, void *aux, | |
1041 | const struct ofproto_bundle_settings *s) | |
1042 | { | |
1043 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1044 | bool need_flush = false; | |
abe529af BP |
1045 | struct ofport_dpif *port; |
1046 | struct ofbundle *bundle; | |
ecac4ebf BP |
1047 | unsigned long *trunks; |
1048 | int vlan; | |
abe529af BP |
1049 | size_t i; |
1050 | bool ok; | |
1051 | ||
1052 | if (!s) { | |
1053 | bundle_destroy(bundle_lookup(ofproto, aux)); | |
1054 | return 0; | |
1055 | } | |
1056 | ||
1057 | assert(s->n_slaves == 1 || s->bond != NULL); | |
1058 | assert((s->lacp != NULL) == (s->lacp_slaves != NULL)); | |
1059 | ||
1060 | bundle = bundle_lookup(ofproto, aux); | |
1061 | if (!bundle) { | |
1062 | bundle = xmalloc(sizeof *bundle); | |
1063 | ||
1064 | bundle->ofproto = ofproto; | |
1065 | hmap_insert(&ofproto->bundles, &bundle->hmap_node, | |
1066 | hash_pointer(aux, 0)); | |
1067 | bundle->aux = aux; | |
1068 | bundle->name = NULL; | |
1069 | ||
1070 | list_init(&bundle->ports); | |
ecac4ebf | 1071 | bundle->vlan_mode = PORT_VLAN_TRUNK; |
abe529af BP |
1072 | bundle->vlan = -1; |
1073 | bundle->trunks = NULL; | |
1074 | bundle->lacp = NULL; | |
1075 | bundle->bond = NULL; | |
1076 | ||
1077 | bundle->floodable = true; | |
1078 | ||
1079 | bundle->src_mirrors = 0; | |
1080 | bundle->dst_mirrors = 0; | |
1081 | bundle->mirror_out = 0; | |
1082 | } | |
1083 | ||
1084 | if (!bundle->name || strcmp(s->name, bundle->name)) { | |
1085 | free(bundle->name); | |
1086 | bundle->name = xstrdup(s->name); | |
1087 | } | |
1088 | ||
1089 | /* LACP. */ | |
1090 | if (s->lacp) { | |
1091 | if (!bundle->lacp) { | |
8c977421 | 1092 | ofproto->need_revalidate = true; |
abe529af BP |
1093 | bundle->lacp = lacp_create(); |
1094 | } | |
1095 | lacp_configure(bundle->lacp, s->lacp); | |
1096 | } else { | |
1097 | lacp_destroy(bundle->lacp); | |
1098 | bundle->lacp = NULL; | |
1099 | } | |
1100 | ||
1101 | /* Update set of ports. */ | |
1102 | ok = true; | |
1103 | for (i = 0; i < s->n_slaves; i++) { | |
1104 | if (!bundle_add_port(bundle, s->slaves[i], | |
00794817 BP |
1105 | s->lacp ? &s->lacp_slaves[i] : NULL, |
1106 | s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) { | |
abe529af BP |
1107 | ok = false; |
1108 | } | |
1109 | } | |
1110 | if (!ok || list_size(&bundle->ports) != s->n_slaves) { | |
1111 | struct ofport_dpif *next_port; | |
1112 | ||
1113 | LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) { | |
1114 | for (i = 0; i < s->n_slaves; i++) { | |
56c769ab | 1115 | if (s->slaves[i] == port->up.ofp_port) { |
abe529af BP |
1116 | goto found; |
1117 | } | |
1118 | } | |
1119 | ||
1120 | bundle_del_port(port); | |
1121 | found: ; | |
1122 | } | |
1123 | } | |
1124 | assert(list_size(&bundle->ports) <= s->n_slaves); | |
1125 | ||
1126 | if (list_is_empty(&bundle->ports)) { | |
1127 | bundle_destroy(bundle); | |
1128 | return EINVAL; | |
1129 | } | |
1130 | ||
ecac4ebf BP |
1131 | /* Set VLAN tagging mode */ |
1132 | if (s->vlan_mode != bundle->vlan_mode) { | |
1133 | bundle->vlan_mode = s->vlan_mode; | |
1134 | need_flush = true; | |
1135 | } | |
1136 | ||
abe529af | 1137 | /* Set VLAN tag. */ |
ecac4ebf BP |
1138 | vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1 |
1139 | : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan | |
1140 | : 0); | |
1141 | if (vlan != bundle->vlan) { | |
1142 | bundle->vlan = vlan; | |
abe529af BP |
1143 | need_flush = true; |
1144 | } | |
1145 | ||
1146 | /* Get trunked VLANs. */ | |
ecac4ebf BP |
1147 | switch (s->vlan_mode) { |
1148 | case PORT_VLAN_ACCESS: | |
1149 | trunks = NULL; | |
1150 | break; | |
1151 | ||
1152 | case PORT_VLAN_TRUNK: | |
1153 | trunks = (unsigned long *) s->trunks; | |
1154 | break; | |
1155 | ||
1156 | case PORT_VLAN_NATIVE_UNTAGGED: | |
1157 | case PORT_VLAN_NATIVE_TAGGED: | |
1158 | if (vlan != 0 && (!s->trunks | |
1159 | || !bitmap_is_set(s->trunks, vlan) | |
1160 | || bitmap_is_set(s->trunks, 0))) { | |
1161 | /* Force trunking the native VLAN and prohibit trunking VLAN 0. */ | |
1162 | if (s->trunks) { | |
1163 | trunks = bitmap_clone(s->trunks, 4096); | |
1164 | } else { | |
1165 | trunks = bitmap_allocate1(4096); | |
1166 | } | |
1167 | bitmap_set1(trunks, vlan); | |
1168 | bitmap_set0(trunks, 0); | |
1169 | } else { | |
1170 | trunks = (unsigned long *) s->trunks; | |
1171 | } | |
1172 | break; | |
1173 | ||
1174 | default: | |
1175 | NOT_REACHED(); | |
1176 | } | |
abe529af BP |
1177 | if (!vlan_bitmap_equal(trunks, bundle->trunks)) { |
1178 | free(bundle->trunks); | |
ecac4ebf BP |
1179 | if (trunks == s->trunks) { |
1180 | bundle->trunks = vlan_bitmap_clone(trunks); | |
1181 | } else { | |
1182 | bundle->trunks = trunks; | |
1183 | trunks = NULL; | |
1184 | } | |
abe529af BP |
1185 | need_flush = true; |
1186 | } | |
ecac4ebf BP |
1187 | if (trunks != s->trunks) { |
1188 | free(trunks); | |
1189 | } | |
abe529af BP |
1190 | |
1191 | /* Bonding. */ | |
1192 | if (!list_is_short(&bundle->ports)) { | |
1193 | bundle->ofproto->has_bonded_bundles = true; | |
1194 | if (bundle->bond) { | |
1195 | if (bond_reconfigure(bundle->bond, s->bond)) { | |
1196 | ofproto->need_revalidate = true; | |
1197 | } | |
1198 | } else { | |
1199 | bundle->bond = bond_create(s->bond); | |
6f77f4ae | 1200 | ofproto->need_revalidate = true; |
abe529af BP |
1201 | } |
1202 | ||
1203 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
00794817 | 1204 | bond_slave_register(bundle->bond, port, port->bond_stable_id, |
abe529af BP |
1205 | port->up.netdev); |
1206 | } | |
1207 | } else { | |
1208 | bond_destroy(bundle->bond); | |
1209 | bundle->bond = NULL; | |
1210 | } | |
1211 | ||
1212 | /* If we changed something that would affect MAC learning, un-learn | |
1213 | * everything on this port and force flow revalidation. */ | |
1214 | if (need_flush) { | |
1215 | bundle_flush_macs(bundle); | |
1216 | } | |
1217 | ||
1218 | return 0; | |
1219 | } | |
1220 | ||
1221 | static void | |
1222 | bundle_remove(struct ofport *port_) | |
1223 | { | |
1224 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
1225 | struct ofbundle *bundle = port->bundle; | |
1226 | ||
1227 | if (bundle) { | |
1228 | bundle_del_port(port); | |
1229 | if (list_is_empty(&bundle->ports)) { | |
1230 | bundle_destroy(bundle); | |
1231 | } else if (list_is_short(&bundle->ports)) { | |
1232 | bond_destroy(bundle->bond); | |
1233 | bundle->bond = NULL; | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | static void | |
5f877369 | 1239 | send_pdu_cb(void *port_, const void *pdu, size_t pdu_size) |
abe529af BP |
1240 | { |
1241 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10); | |
1242 | struct ofport_dpif *port = port_; | |
1243 | uint8_t ea[ETH_ADDR_LEN]; | |
1244 | int error; | |
1245 | ||
1246 | error = netdev_get_etheraddr(port->up.netdev, ea); | |
1247 | if (!error) { | |
abe529af | 1248 | struct ofpbuf packet; |
5f877369 | 1249 | void *packet_pdu; |
abe529af BP |
1250 | |
1251 | ofpbuf_init(&packet, 0); | |
1252 | packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP, | |
5f877369 EJ |
1253 | pdu_size); |
1254 | memcpy(packet_pdu, pdu, pdu_size); | |
1255 | ||
abe529af BP |
1256 | error = netdev_send(port->up.netdev, &packet); |
1257 | if (error) { | |
1258 | VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed " | |
1259 | "(%s)", port->bundle->name, | |
1260 | netdev_get_name(port->up.netdev), strerror(error)); | |
1261 | } | |
1262 | ofpbuf_uninit(&packet); | |
1263 | } else { | |
1264 | VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface " | |
1265 | "%s (%s)", port->bundle->name, | |
1266 | netdev_get_name(port->up.netdev), strerror(error)); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | static void | |
1271 | bundle_send_learning_packets(struct ofbundle *bundle) | |
1272 | { | |
1273 | struct ofproto_dpif *ofproto = bundle->ofproto; | |
1274 | int error, n_packets, n_errors; | |
1275 | struct mac_entry *e; | |
1276 | ||
1277 | error = n_packets = n_errors = 0; | |
1278 | LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) { | |
1279 | if (e->port.p != bundle) { | |
1280 | int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan); | |
1281 | if (ret) { | |
1282 | error = ret; | |
1283 | n_errors++; | |
1284 | } | |
1285 | n_packets++; | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | if (n_errors) { | |
1290 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1291 | VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning " | |
1292 | "packets, last error was: %s", | |
1293 | bundle->name, n_errors, n_packets, strerror(error)); | |
1294 | } else { | |
1295 | VLOG_DBG("bond %s: sent %d gratuitous learning packets", | |
1296 | bundle->name, n_packets); | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | static void | |
1301 | bundle_run(struct ofbundle *bundle) | |
1302 | { | |
1303 | if (bundle->lacp) { | |
1304 | lacp_run(bundle->lacp, send_pdu_cb); | |
1305 | } | |
1306 | if (bundle->bond) { | |
1307 | struct ofport_dpif *port; | |
1308 | ||
1309 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
015e08bc | 1310 | bond_slave_set_may_enable(bundle->bond, port, port->may_enable); |
abe529af BP |
1311 | } |
1312 | ||
1313 | bond_run(bundle->bond, &bundle->ofproto->revalidate_set, | |
1314 | lacp_negotiated(bundle->lacp)); | |
1315 | if (bond_should_send_learning_packets(bundle->bond)) { | |
1316 | bundle_send_learning_packets(bundle); | |
1317 | } | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | static void | |
1322 | bundle_wait(struct ofbundle *bundle) | |
1323 | { | |
1324 | if (bundle->lacp) { | |
1325 | lacp_wait(bundle->lacp); | |
1326 | } | |
1327 | if (bundle->bond) { | |
1328 | bond_wait(bundle->bond); | |
1329 | } | |
1330 | } | |
1331 | \f | |
1332 | /* Mirrors. */ | |
1333 | ||
1334 | static int | |
1335 | mirror_scan(struct ofproto_dpif *ofproto) | |
1336 | { | |
1337 | int idx; | |
1338 | ||
1339 | for (idx = 0; idx < MAX_MIRRORS; idx++) { | |
1340 | if (!ofproto->mirrors[idx]) { | |
1341 | return idx; | |
1342 | } | |
1343 | } | |
1344 | return -1; | |
1345 | } | |
1346 | ||
1347 | static struct ofmirror * | |
1348 | mirror_lookup(struct ofproto_dpif *ofproto, void *aux) | |
1349 | { | |
1350 | int i; | |
1351 | ||
1352 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1353 | struct ofmirror *mirror = ofproto->mirrors[i]; | |
1354 | if (mirror && mirror->aux == aux) { | |
1355 | return mirror; | |
1356 | } | |
1357 | } | |
1358 | ||
1359 | return NULL; | |
1360 | } | |
1361 | ||
1362 | static int | |
1363 | mirror_set(struct ofproto *ofproto_, void *aux, | |
1364 | const struct ofproto_mirror_settings *s) | |
1365 | { | |
1366 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1367 | mirror_mask_t mirror_bit; | |
1368 | struct ofbundle *bundle; | |
1369 | struct ofmirror *mirror; | |
1370 | struct ofbundle *out; | |
1371 | struct hmapx srcs; /* Contains "struct ofbundle *"s. */ | |
1372 | struct hmapx dsts; /* Contains "struct ofbundle *"s. */ | |
1373 | int out_vlan; | |
1374 | ||
1375 | mirror = mirror_lookup(ofproto, aux); | |
1376 | if (!s) { | |
1377 | mirror_destroy(mirror); | |
1378 | return 0; | |
1379 | } | |
1380 | if (!mirror) { | |
1381 | int idx; | |
1382 | ||
1383 | idx = mirror_scan(ofproto); | |
1384 | if (idx < 0) { | |
1385 | VLOG_WARN("bridge %s: maximum of %d port mirrors reached, " | |
1386 | "cannot create %s", | |
1387 | ofproto->up.name, MAX_MIRRORS, s->name); | |
1388 | return EFBIG; | |
1389 | } | |
1390 | ||
1391 | mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror); | |
1392 | mirror->ofproto = ofproto; | |
1393 | mirror->idx = idx; | |
8b28d864 | 1394 | mirror->aux = aux; |
abe529af BP |
1395 | mirror->out_vlan = -1; |
1396 | mirror->name = NULL; | |
1397 | } | |
1398 | ||
1399 | if (!mirror->name || strcmp(s->name, mirror->name)) { | |
1400 | free(mirror->name); | |
1401 | mirror->name = xstrdup(s->name); | |
1402 | } | |
1403 | ||
1404 | /* Get the new configuration. */ | |
1405 | if (s->out_bundle) { | |
1406 | out = bundle_lookup(ofproto, s->out_bundle); | |
1407 | if (!out) { | |
1408 | mirror_destroy(mirror); | |
1409 | return EINVAL; | |
1410 | } | |
1411 | out_vlan = -1; | |
1412 | } else { | |
1413 | out = NULL; | |
1414 | out_vlan = s->out_vlan; | |
1415 | } | |
1416 | bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs); | |
1417 | bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts); | |
1418 | ||
1419 | /* If the configuration has not changed, do nothing. */ | |
1420 | if (hmapx_equals(&srcs, &mirror->srcs) | |
1421 | && hmapx_equals(&dsts, &mirror->dsts) | |
1422 | && vlan_bitmap_equal(mirror->vlans, s->src_vlans) | |
1423 | && mirror->out == out | |
1424 | && mirror->out_vlan == out_vlan) | |
1425 | { | |
1426 | hmapx_destroy(&srcs); | |
1427 | hmapx_destroy(&dsts); | |
1428 | return 0; | |
1429 | } | |
1430 | ||
1431 | hmapx_swap(&srcs, &mirror->srcs); | |
1432 | hmapx_destroy(&srcs); | |
1433 | ||
1434 | hmapx_swap(&dsts, &mirror->dsts); | |
1435 | hmapx_destroy(&dsts); | |
1436 | ||
1437 | free(mirror->vlans); | |
1438 | mirror->vlans = vlan_bitmap_clone(s->src_vlans); | |
1439 | ||
1440 | mirror->out = out; | |
1441 | mirror->out_vlan = out_vlan; | |
1442 | ||
1443 | /* Update bundles. */ | |
1444 | mirror_bit = MIRROR_MASK_C(1) << mirror->idx; | |
1445 | HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) { | |
1446 | if (hmapx_contains(&mirror->srcs, bundle)) { | |
1447 | bundle->src_mirrors |= mirror_bit; | |
1448 | } else { | |
1449 | bundle->src_mirrors &= ~mirror_bit; | |
1450 | } | |
1451 | ||
1452 | if (hmapx_contains(&mirror->dsts, bundle)) { | |
1453 | bundle->dst_mirrors |= mirror_bit; | |
1454 | } else { | |
1455 | bundle->dst_mirrors &= ~mirror_bit; | |
1456 | } | |
1457 | ||
1458 | if (mirror->out == bundle) { | |
1459 | bundle->mirror_out |= mirror_bit; | |
1460 | } else { | |
1461 | bundle->mirror_out &= ~mirror_bit; | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | ofproto->need_revalidate = true; | |
1466 | mac_learning_flush(ofproto->ml); | |
1467 | ||
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | static void | |
1472 | mirror_destroy(struct ofmirror *mirror) | |
1473 | { | |
1474 | struct ofproto_dpif *ofproto; | |
1475 | mirror_mask_t mirror_bit; | |
1476 | struct ofbundle *bundle; | |
1477 | ||
1478 | if (!mirror) { | |
1479 | return; | |
1480 | } | |
1481 | ||
1482 | ofproto = mirror->ofproto; | |
1483 | ofproto->need_revalidate = true; | |
1484 | mac_learning_flush(ofproto->ml); | |
1485 | ||
1486 | mirror_bit = MIRROR_MASK_C(1) << mirror->idx; | |
1487 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
1488 | bundle->src_mirrors &= ~mirror_bit; | |
1489 | bundle->dst_mirrors &= ~mirror_bit; | |
1490 | bundle->mirror_out &= ~mirror_bit; | |
1491 | } | |
1492 | ||
1493 | hmapx_destroy(&mirror->srcs); | |
1494 | hmapx_destroy(&mirror->dsts); | |
1495 | free(mirror->vlans); | |
1496 | ||
1497 | ofproto->mirrors[mirror->idx] = NULL; | |
1498 | free(mirror->name); | |
1499 | free(mirror); | |
1500 | } | |
1501 | ||
1502 | static int | |
1503 | set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans) | |
1504 | { | |
1505 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1506 | if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) { | |
1507 | ofproto->need_revalidate = true; | |
1508 | mac_learning_flush(ofproto->ml); | |
1509 | } | |
1510 | return 0; | |
1511 | } | |
1512 | ||
1513 | static bool | |
1514 | is_mirror_output_bundle(struct ofproto *ofproto_, void *aux) | |
1515 | { | |
1516 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1517 | struct ofbundle *bundle = bundle_lookup(ofproto, aux); | |
1518 | return bundle && bundle->mirror_out != 0; | |
1519 | } | |
8402c74b SS |
1520 | |
1521 | static void | |
b53055f4 | 1522 | forward_bpdu_changed(struct ofproto *ofproto_) |
8402c74b SS |
1523 | { |
1524 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1525 | /* Revalidate cached flows whenever forward_bpdu option changes. */ | |
1526 | ofproto->need_revalidate = true; | |
1527 | } | |
abe529af BP |
1528 | \f |
1529 | /* Ports. */ | |
1530 | ||
1531 | static struct ofport_dpif * | |
1532 | get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port) | |
1533 | { | |
7df6a8bd BP |
1534 | struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port); |
1535 | return ofport ? ofport_dpif_cast(ofport) : NULL; | |
abe529af BP |
1536 | } |
1537 | ||
1538 | static struct ofport_dpif * | |
1539 | get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port) | |
1540 | { | |
1541 | return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port)); | |
1542 | } | |
1543 | ||
1544 | static void | |
1545 | ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port, | |
1546 | struct dpif_port *dpif_port) | |
1547 | { | |
1548 | ofproto_port->name = dpif_port->name; | |
1549 | ofproto_port->type = dpif_port->type; | |
1550 | ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no); | |
1551 | } | |
1552 | ||
1553 | static void | |
1554 | port_run(struct ofport_dpif *ofport) | |
1555 | { | |
015e08bc EJ |
1556 | bool enable = netdev_get_carrier(ofport->up.netdev); |
1557 | ||
abe529af BP |
1558 | if (ofport->cfm) { |
1559 | cfm_run(ofport->cfm); | |
1560 | ||
1561 | if (cfm_should_send_ccm(ofport->cfm)) { | |
1562 | struct ofpbuf packet; | |
abe529af BP |
1563 | |
1564 | ofpbuf_init(&packet, 0); | |
c0a2e71d | 1565 | cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr); |
abe529af | 1566 | send_packet(ofproto_dpif_cast(ofport->up.ofproto), |
b2fda3ef | 1567 | ofport->odp_port, &packet); |
abe529af BP |
1568 | ofpbuf_uninit(&packet); |
1569 | } | |
015e08bc | 1570 | |
86dc6501 EJ |
1571 | enable = enable && !cfm_get_fault(ofport->cfm) |
1572 | && cfm_get_opup(ofport->cfm); | |
abe529af | 1573 | } |
015e08bc EJ |
1574 | |
1575 | if (ofport->bundle) { | |
1576 | enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport); | |
1577 | } | |
1578 | ||
daff3353 EJ |
1579 | if (ofport->may_enable != enable) { |
1580 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
1581 | ||
1582 | if (ofproto->has_bundle_action) { | |
1583 | ofproto->need_revalidate = true; | |
1584 | } | |
1585 | } | |
1586 | ||
015e08bc | 1587 | ofport->may_enable = enable; |
abe529af BP |
1588 | } |
1589 | ||
1590 | static void | |
1591 | port_wait(struct ofport_dpif *ofport) | |
1592 | { | |
1593 | if (ofport->cfm) { | |
1594 | cfm_wait(ofport->cfm); | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | static int | |
1599 | port_query_by_name(const struct ofproto *ofproto_, const char *devname, | |
1600 | struct ofproto_port *ofproto_port) | |
1601 | { | |
1602 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1603 | struct dpif_port dpif_port; | |
1604 | int error; | |
1605 | ||
1606 | error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port); | |
1607 | if (!error) { | |
1608 | ofproto_port_from_dpif_port(ofproto_port, &dpif_port); | |
1609 | } | |
1610 | return error; | |
1611 | } | |
1612 | ||
1613 | static int | |
1614 | port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp) | |
1615 | { | |
1616 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1617 | uint16_t odp_port; | |
1618 | int error; | |
1619 | ||
1620 | error = dpif_port_add(ofproto->dpif, netdev, &odp_port); | |
1621 | if (!error) { | |
1622 | *ofp_portp = odp_port_to_ofp_port(odp_port); | |
1623 | } | |
1624 | return error; | |
1625 | } | |
1626 | ||
1627 | static int | |
1628 | port_del(struct ofproto *ofproto_, uint16_t ofp_port) | |
1629 | { | |
1630 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1631 | int error; | |
1632 | ||
1633 | error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port)); | |
1634 | if (!error) { | |
1635 | struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port); | |
1636 | if (ofport) { | |
1637 | /* The caller is going to close ofport->up.netdev. If this is a | |
1638 | * bonded port, then the bond is using that netdev, so remove it | |
1639 | * from the bond. The client will need to reconfigure everything | |
1640 | * after deleting ports, so then the slave will get re-added. */ | |
1641 | bundle_remove(&ofport->up); | |
1642 | } | |
1643 | } | |
1644 | return error; | |
1645 | } | |
1646 | ||
1647 | struct port_dump_state { | |
1648 | struct dpif_port_dump dump; | |
1649 | bool done; | |
1650 | }; | |
1651 | ||
1652 | static int | |
1653 | port_dump_start(const struct ofproto *ofproto_, void **statep) | |
1654 | { | |
1655 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1656 | struct port_dump_state *state; | |
1657 | ||
1658 | *statep = state = xmalloc(sizeof *state); | |
1659 | dpif_port_dump_start(&state->dump, ofproto->dpif); | |
1660 | state->done = false; | |
1661 | return 0; | |
1662 | } | |
1663 | ||
1664 | static int | |
1665 | port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_, | |
1666 | struct ofproto_port *port) | |
1667 | { | |
1668 | struct port_dump_state *state = state_; | |
1669 | struct dpif_port dpif_port; | |
1670 | ||
1671 | if (dpif_port_dump_next(&state->dump, &dpif_port)) { | |
1672 | ofproto_port_from_dpif_port(port, &dpif_port); | |
1673 | return 0; | |
1674 | } else { | |
1675 | int error = dpif_port_dump_done(&state->dump); | |
1676 | state->done = true; | |
1677 | return error ? error : EOF; | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | static int | |
1682 | port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_) | |
1683 | { | |
1684 | struct port_dump_state *state = state_; | |
1685 | ||
1686 | if (!state->done) { | |
1687 | dpif_port_dump_done(&state->dump); | |
1688 | } | |
1689 | free(state); | |
1690 | return 0; | |
1691 | } | |
1692 | ||
1693 | static int | |
1694 | port_poll(const struct ofproto *ofproto_, char **devnamep) | |
1695 | { | |
1696 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1697 | return dpif_port_poll(ofproto->dpif, devnamep); | |
1698 | } | |
1699 | ||
1700 | static void | |
1701 | port_poll_wait(const struct ofproto *ofproto_) | |
1702 | { | |
1703 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1704 | dpif_port_poll_wait(ofproto->dpif); | |
1705 | } | |
1706 | ||
1707 | static int | |
1708 | port_is_lacp_current(const struct ofport *ofport_) | |
1709 | { | |
1710 | const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
1711 | return (ofport->bundle && ofport->bundle->lacp | |
1712 | ? lacp_slave_is_current(ofport->bundle->lacp, ofport) | |
1713 | : -1); | |
1714 | } | |
1715 | \f | |
1716 | /* Upcall handling. */ | |
1717 | ||
1718 | /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an | |
1719 | * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to | |
1720 | * their individual configurations. | |
1721 | * | |
1722 | * If 'clone' is true, the caller retains ownership of 'upcall->packet'. | |
1723 | * Otherwise, ownership is transferred to this function. */ | |
1724 | static void | |
1725 | send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall, | |
1726 | const struct flow *flow, bool clone) | |
1727 | { | |
1728 | struct ofputil_packet_in pin; | |
6ff686f2 | 1729 | struct user_action_cookie cookie; |
abe529af BP |
1730 | |
1731 | pin.packet = upcall->packet; | |
1732 | pin.in_port = flow->in_port; | |
1733 | pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION; | |
1734 | pin.buffer_id = 0; /* not yet known */ | |
6ff686f2 PS |
1735 | |
1736 | memcpy(&cookie, &upcall->userdata, sizeof(cookie)); | |
1737 | pin.send_len = cookie.data; | |
78bd1cd0 | 1738 | connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow, |
abe529af BP |
1739 | clone ? NULL : upcall->packet); |
1740 | } | |
1741 | ||
1742 | static bool | |
1743 | process_special(struct ofproto_dpif *ofproto, const struct flow *flow, | |
1744 | const struct ofpbuf *packet) | |
1745 | { | |
b6e001b6 EJ |
1746 | struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port); |
1747 | ||
1748 | if (!ofport) { | |
1749 | return false; | |
1750 | } | |
1751 | ||
ef9819b5 | 1752 | if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) { |
b6e001b6 | 1753 | if (packet) { |
abe529af BP |
1754 | cfm_process_heartbeat(ofport->cfm, packet); |
1755 | } | |
1756 | return true; | |
b6e001b6 EJ |
1757 | } else if (ofport->bundle && ofport->bundle->lacp |
1758 | && flow->dl_type == htons(ETH_TYPE_LACP)) { | |
1759 | if (packet) { | |
1760 | lacp_process_packet(ofport->bundle->lacp, ofport, packet); | |
abe529af | 1761 | } |
da37ebac | 1762 | return true; |
abe529af BP |
1763 | } |
1764 | return false; | |
1765 | } | |
1766 | ||
1767 | static void | |
1768 | handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall) | |
1769 | { | |
1770 | struct facet *facet; | |
1771 | struct flow flow; | |
1772 | ||
1773 | /* Obtain in_port and tun_id, at least. */ | |
1774 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
1775 | ||
1776 | /* Set header pointers in 'flow'. */ | |
1777 | flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow); | |
1778 | ||
1779 | /* Handle 802.1ag and LACP. */ | |
1780 | if (process_special(ofproto, &flow, upcall->packet)) { | |
1781 | ofpbuf_delete(upcall->packet); | |
6c1491fb | 1782 | ofproto->n_matches++; |
abe529af BP |
1783 | return; |
1784 | } | |
1785 | ||
1786 | /* Check with in-band control to see if this packet should be sent | |
1787 | * to the local port regardless of the flow table. */ | |
1788 | if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) { | |
df2c07f4 | 1789 | send_packet(ofproto, OVSP_LOCAL, upcall->packet); |
abe529af BP |
1790 | } |
1791 | ||
1792 | facet = facet_lookup_valid(ofproto, &flow); | |
1793 | if (!facet) { | |
29901626 | 1794 | struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow, 0); |
abe529af BP |
1795 | if (!rule) { |
1796 | /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */ | |
1797 | struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port); | |
1798 | if (port) { | |
1799 | if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) { | |
1800 | COVERAGE_INC(ofproto_dpif_no_packet_in); | |
1801 | /* XXX install 'drop' flow entry */ | |
1802 | ofpbuf_delete(upcall->packet); | |
1803 | return; | |
1804 | } | |
1805 | } else { | |
1806 | VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, | |
1807 | flow.in_port); | |
1808 | } | |
1809 | ||
1810 | send_packet_in(ofproto, upcall, &flow, false); | |
1811 | return; | |
1812 | } | |
1813 | ||
1814 | facet = facet_create(rule, &flow, upcall->packet); | |
1815 | } else if (!facet->may_install) { | |
1816 | /* The facet is not installable, that is, we need to process every | |
1817 | * packet, so process the current packet's actions into 'facet'. */ | |
1818 | facet_make_actions(ofproto, facet, upcall->packet); | |
1819 | } | |
1820 | ||
1821 | if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) { | |
1822 | /* | |
1823 | * Extra-special case for fail-open mode. | |
1824 | * | |
1825 | * We are in fail-open mode and the packet matched the fail-open rule, | |
1826 | * but we are connected to a controller too. We should send the packet | |
1827 | * up to the controller in the hope that it will try to set up a flow | |
1828 | * and thereby allow us to exit fail-open. | |
1829 | * | |
1830 | * See the top-level comment in fail-open.c for more information. | |
1831 | */ | |
1832 | send_packet_in(ofproto, upcall, &flow, true); | |
1833 | } | |
1834 | ||
1835 | facet_execute(ofproto, facet, upcall->packet); | |
1836 | facet_install(ofproto, facet, false); | |
6c1491fb | 1837 | ofproto->n_matches++; |
abe529af BP |
1838 | } |
1839 | ||
1840 | static void | |
6ff686f2 PS |
1841 | handle_userspace_upcall(struct ofproto_dpif *ofproto, |
1842 | struct dpif_upcall *upcall) | |
abe529af BP |
1843 | { |
1844 | struct flow flow; | |
6ff686f2 | 1845 | struct user_action_cookie cookie; |
abe529af | 1846 | |
6ff686f2 | 1847 | memcpy(&cookie, &upcall->userdata, sizeof(cookie)); |
abe529af | 1848 | |
6ff686f2 | 1849 | if (cookie.type == USER_ACTION_COOKIE_SFLOW) { |
abe529af BP |
1850 | if (ofproto->sflow) { |
1851 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
6ff686f2 | 1852 | dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie); |
abe529af BP |
1853 | } |
1854 | ofpbuf_delete(upcall->packet); | |
6ff686f2 PS |
1855 | |
1856 | } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) { | |
1857 | COVERAGE_INC(ofproto_dpif_ctlr_action); | |
1858 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
1859 | send_packet_in(ofproto, upcall, &flow, false); | |
1860 | } else { | |
1861 | VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata); | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | static void | |
1866 | handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall) | |
1867 | { | |
1868 | switch (upcall->type) { | |
1869 | case DPIF_UC_ACTION: | |
1870 | handle_userspace_upcall(ofproto, upcall); | |
abe529af BP |
1871 | break; |
1872 | ||
1873 | case DPIF_UC_MISS: | |
1874 | handle_miss_upcall(ofproto, upcall); | |
1875 | break; | |
1876 | ||
1877 | case DPIF_N_UC_TYPES: | |
1878 | default: | |
1879 | VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type); | |
1880 | break; | |
1881 | } | |
1882 | } | |
1883 | \f | |
1884 | /* Flow expiration. */ | |
1885 | ||
1886 | static int facet_max_idle(const struct ofproto_dpif *); | |
1887 | static void update_stats(struct ofproto_dpif *); | |
1888 | static void rule_expire(struct rule_dpif *); | |
1889 | static void expire_facets(struct ofproto_dpif *, int dp_max_idle); | |
1890 | ||
1891 | /* This function is called periodically by run(). Its job is to collect | |
1892 | * updates for the flows that have been installed into the datapath, most | |
1893 | * importantly when they last were used, and then use that information to | |
1894 | * expire flows that have not been used recently. | |
1895 | * | |
1896 | * Returns the number of milliseconds after which it should be called again. */ | |
1897 | static int | |
1898 | expire(struct ofproto_dpif *ofproto) | |
1899 | { | |
1900 | struct rule_dpif *rule, *next_rule; | |
0697b5c3 | 1901 | struct classifier *table; |
abe529af BP |
1902 | int dp_max_idle; |
1903 | ||
1904 | /* Update stats for each flow in the datapath. */ | |
1905 | update_stats(ofproto); | |
1906 | ||
1907 | /* Expire facets that have been idle too long. */ | |
1908 | dp_max_idle = facet_max_idle(ofproto); | |
1909 | expire_facets(ofproto, dp_max_idle); | |
1910 | ||
1911 | /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */ | |
0697b5c3 BP |
1912 | OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) { |
1913 | struct cls_cursor cursor; | |
1914 | ||
1915 | cls_cursor_init(&cursor, table, NULL); | |
1916 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) { | |
1917 | rule_expire(rule); | |
1918 | } | |
abe529af BP |
1919 | } |
1920 | ||
1921 | /* All outstanding data in existing flows has been accounted, so it's a | |
1922 | * good time to do bond rebalancing. */ | |
1923 | if (ofproto->has_bonded_bundles) { | |
1924 | struct ofbundle *bundle; | |
1925 | ||
1926 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
1927 | if (bundle->bond) { | |
1928 | bond_rebalance(bundle->bond, &ofproto->revalidate_set); | |
1929 | } | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | return MIN(dp_max_idle, 1000); | |
1934 | } | |
1935 | ||
1936 | /* Update 'packet_count', 'byte_count', and 'used' members of installed facets. | |
1937 | * | |
1938 | * This function also pushes statistics updates to rules which each facet | |
1939 | * resubmits into. Generally these statistics will be accurate. However, if a | |
1940 | * facet changes the rule it resubmits into at some time in between | |
1941 | * update_stats() runs, it is possible that statistics accrued to the | |
1942 | * old rule will be incorrectly attributed to the new rule. This could be | |
1943 | * avoided by calling update_stats() whenever rules are created or | |
1944 | * deleted. However, the performance impact of making so many calls to the | |
1945 | * datapath do not justify the benefit of having perfectly accurate statistics. | |
1946 | */ | |
1947 | static void | |
1948 | update_stats(struct ofproto_dpif *p) | |
1949 | { | |
1950 | const struct dpif_flow_stats *stats; | |
1951 | struct dpif_flow_dump dump; | |
1952 | const struct nlattr *key; | |
1953 | size_t key_len; | |
1954 | ||
1955 | dpif_flow_dump_start(&dump, p->dpif); | |
1956 | while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) { | |
1957 | struct facet *facet; | |
1958 | struct flow flow; | |
1959 | ||
1960 | if (odp_flow_key_to_flow(key, key_len, &flow)) { | |
1961 | struct ds s; | |
1962 | ||
1963 | ds_init(&s); | |
1964 | odp_flow_key_format(key, key_len, &s); | |
df2c07f4 | 1965 | VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s", |
abe529af BP |
1966 | ds_cstr(&s)); |
1967 | ds_destroy(&s); | |
1968 | ||
1969 | continue; | |
1970 | } | |
1971 | facet = facet_find(p, &flow); | |
1972 | ||
1973 | if (facet && facet->installed) { | |
1974 | ||
1975 | if (stats->n_packets >= facet->dp_packet_count) { | |
1976 | uint64_t extra = stats->n_packets - facet->dp_packet_count; | |
1977 | facet->packet_count += extra; | |
1978 | } else { | |
1979 | VLOG_WARN_RL(&rl, "unexpected packet count from the datapath"); | |
1980 | } | |
1981 | ||
1982 | if (stats->n_bytes >= facet->dp_byte_count) { | |
1983 | facet->byte_count += stats->n_bytes - facet->dp_byte_count; | |
1984 | } else { | |
1985 | VLOG_WARN_RL(&rl, "unexpected byte count from datapath"); | |
1986 | } | |
1987 | ||
1988 | facet->dp_packet_count = stats->n_packets; | |
1989 | facet->dp_byte_count = stats->n_bytes; | |
1990 | ||
1991 | facet_update_time(p, facet, stats->used); | |
55af77bb | 1992 | facet_account(p, facet); |
abe529af BP |
1993 | facet_push_stats(facet); |
1994 | } else { | |
1995 | /* There's a flow in the datapath that we know nothing about. | |
1996 | * Delete it. */ | |
1997 | COVERAGE_INC(facet_unexpected); | |
1998 | dpif_flow_del(p->dpif, key, key_len, NULL); | |
1999 | } | |
2000 | } | |
2001 | dpif_flow_dump_done(&dump); | |
2002 | } | |
2003 | ||
2004 | /* Calculates and returns the number of milliseconds of idle time after which | |
2005 | * facets should expire from the datapath and we should fold their statistics | |
2006 | * into their parent rules in userspace. */ | |
2007 | static int | |
2008 | facet_max_idle(const struct ofproto_dpif *ofproto) | |
2009 | { | |
2010 | /* | |
2011 | * Idle time histogram. | |
2012 | * | |
2013 | * Most of the time a switch has a relatively small number of facets. When | |
2014 | * this is the case we might as well keep statistics for all of them in | |
2015 | * userspace and to cache them in the kernel datapath for performance as | |
2016 | * well. | |
2017 | * | |
2018 | * As the number of facets increases, the memory required to maintain | |
2019 | * statistics about them in userspace and in the kernel becomes | |
2020 | * significant. However, with a large number of facets it is likely that | |
2021 | * only a few of them are "heavy hitters" that consume a large amount of | |
2022 | * bandwidth. At this point, only heavy hitters are worth caching in the | |
2023 | * kernel and maintaining in userspaces; other facets we can discard. | |
2024 | * | |
2025 | * The technique used to compute the idle time is to build a histogram with | |
2026 | * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet | |
2027 | * that is installed in the kernel gets dropped in the appropriate bucket. | |
2028 | * After the histogram has been built, we compute the cutoff so that only | |
084f5290 SH |
2029 | * the most-recently-used 1% of facets (but at least |
2030 | * ofproto->up.flow_eviction_threshold flows) are kept cached. At least | |
2031 | * the most-recently-used bucket of facets is kept, so actually an | |
2032 | * arbitrary number of facets can be kept in any given expiration run | |
2033 | * (though the next run will delete most of those unless they receive | |
2034 | * additional data). | |
abe529af BP |
2035 | * |
2036 | * This requires a second pass through the facets, in addition to the pass | |
2037 | * made by update_stats(), because the former function never looks | |
2038 | * at uninstallable facets. | |
2039 | */ | |
2040 | enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) }; | |
2041 | enum { N_BUCKETS = 5000 / BUCKET_WIDTH }; | |
2042 | int buckets[N_BUCKETS] = { 0 }; | |
f11c1ef4 | 2043 | int total, subtotal, bucket; |
abe529af | 2044 | struct facet *facet; |
abe529af BP |
2045 | long long int now; |
2046 | int i; | |
2047 | ||
2048 | total = hmap_count(&ofproto->facets); | |
084f5290 | 2049 | if (total <= ofproto->up.flow_eviction_threshold) { |
abe529af BP |
2050 | return N_BUCKETS * BUCKET_WIDTH; |
2051 | } | |
2052 | ||
2053 | /* Build histogram. */ | |
2054 | now = time_msec(); | |
2055 | HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) { | |
2056 | long long int idle = now - facet->used; | |
2057 | int bucket = (idle <= 0 ? 0 | |
2058 | : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1 | |
2059 | : (unsigned int) idle / BUCKET_WIDTH); | |
2060 | buckets[bucket]++; | |
2061 | } | |
2062 | ||
2063 | /* Find the first bucket whose flows should be expired. */ | |
f11c1ef4 SH |
2064 | subtotal = bucket = 0; |
2065 | do { | |
2066 | subtotal += buckets[bucket++]; | |
084f5290 SH |
2067 | } while (bucket < N_BUCKETS && |
2068 | subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100)); | |
abe529af BP |
2069 | |
2070 | if (VLOG_IS_DBG_ENABLED()) { | |
2071 | struct ds s; | |
2072 | ||
2073 | ds_init(&s); | |
2074 | ds_put_cstr(&s, "keep"); | |
2075 | for (i = 0; i < N_BUCKETS; i++) { | |
2076 | if (i == bucket) { | |
2077 | ds_put_cstr(&s, ", drop"); | |
2078 | } | |
2079 | if (buckets[i]) { | |
2080 | ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]); | |
2081 | } | |
2082 | } | |
2083 | VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s)); | |
2084 | ds_destroy(&s); | |
2085 | } | |
2086 | ||
2087 | return bucket * BUCKET_WIDTH; | |
2088 | } | |
2089 | ||
2090 | static void | |
2091 | facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet) | |
2092 | { | |
2093 | if (ofproto->netflow && !facet_is_controller_flow(facet) && | |
2094 | netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) { | |
2095 | struct ofexpired expired; | |
2096 | ||
2097 | if (facet->installed) { | |
2098 | struct dpif_flow_stats stats; | |
2099 | ||
2100 | facet_put__(ofproto, facet, facet->actions, facet->actions_len, | |
2101 | &stats); | |
2102 | facet_update_stats(ofproto, facet, &stats); | |
2103 | } | |
2104 | ||
2105 | expired.flow = facet->flow; | |
2106 | expired.packet_count = facet->packet_count; | |
2107 | expired.byte_count = facet->byte_count; | |
2108 | expired.used = facet->used; | |
2109 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
2110 | } | |
2111 | } | |
2112 | ||
2113 | static void | |
2114 | expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle) | |
2115 | { | |
2116 | long long int cutoff = time_msec() - dp_max_idle; | |
2117 | struct facet *facet, *next_facet; | |
2118 | ||
2119 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
2120 | facet_active_timeout(ofproto, facet); | |
2121 | if (facet->used < cutoff) { | |
2122 | facet_remove(ofproto, facet); | |
2123 | } | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules, | |
2128 | * then delete it entirely. */ | |
2129 | static void | |
2130 | rule_expire(struct rule_dpif *rule) | |
2131 | { | |
2132 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2133 | struct facet *facet, *next_facet; | |
2134 | long long int now; | |
2135 | uint8_t reason; | |
2136 | ||
2137 | /* Has 'rule' expired? */ | |
2138 | now = time_msec(); | |
2139 | if (rule->up.hard_timeout | |
308881af | 2140 | && now > rule->up.modified + rule->up.hard_timeout * 1000) { |
abe529af BP |
2141 | reason = OFPRR_HARD_TIMEOUT; |
2142 | } else if (rule->up.idle_timeout && list_is_empty(&rule->facets) | |
2143 | && now > rule->used + rule->up.idle_timeout * 1000) { | |
2144 | reason = OFPRR_IDLE_TIMEOUT; | |
2145 | } else { | |
2146 | return; | |
2147 | } | |
2148 | ||
2149 | COVERAGE_INC(ofproto_dpif_expired); | |
2150 | ||
2151 | /* Update stats. (This is a no-op if the rule expired due to an idle | |
2152 | * timeout, because that only happens when the rule has no facets left.) */ | |
2153 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { | |
2154 | facet_remove(ofproto, facet); | |
2155 | } | |
2156 | ||
2157 | /* Get rid of the rule. */ | |
2158 | ofproto_rule_expire(&rule->up, reason); | |
2159 | } | |
2160 | \f | |
2161 | /* Facets. */ | |
2162 | ||
2163 | /* Creates and returns a new facet owned by 'rule', given a 'flow' and an | |
2164 | * example 'packet' within that flow. | |
2165 | * | |
2166 | * The caller must already have determined that no facet with an identical | |
2167 | * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in | |
2168 | * the ofproto's classifier table. */ | |
2169 | static struct facet * | |
2170 | facet_create(struct rule_dpif *rule, const struct flow *flow, | |
2171 | const struct ofpbuf *packet) | |
2172 | { | |
2173 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2174 | struct facet *facet; | |
2175 | ||
2176 | facet = xzalloc(sizeof *facet); | |
2177 | facet->used = time_msec(); | |
2178 | hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0)); | |
2179 | list_push_back(&rule->facets, &facet->list_node); | |
2180 | facet->rule = rule; | |
2181 | facet->flow = *flow; | |
2182 | netflow_flow_init(&facet->nf_flow); | |
2183 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used); | |
2184 | ||
2185 | facet_make_actions(ofproto, facet, packet); | |
2186 | ||
2187 | return facet; | |
2188 | } | |
2189 | ||
2190 | static void | |
2191 | facet_free(struct facet *facet) | |
2192 | { | |
2193 | free(facet->actions); | |
2194 | free(facet); | |
2195 | } | |
2196 | ||
2197 | /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on | |
2198 | * 'packet', which arrived on 'in_port'. | |
2199 | * | |
2200 | * Takes ownership of 'packet'. */ | |
2201 | static bool | |
2202 | execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow, | |
2203 | const struct nlattr *odp_actions, size_t actions_len, | |
2204 | struct ofpbuf *packet) | |
2205 | { | |
6ff686f2 PS |
2206 | struct odputil_keybuf keybuf; |
2207 | struct ofpbuf key; | |
2208 | int error; | |
2209 | ||
b6bff4e9 BP |
2210 | if (actions_len == 0) { |
2211 | return true; | |
2212 | } else if (odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE | |
2213 | && NLA_ALIGN(odp_actions->nla_len) == actions_len) { | |
98403001 | 2214 | struct user_action_cookie cookie; |
abe529af | 2215 | struct dpif_upcall upcall; |
98403001 | 2216 | uint64_t cookie_u64; |
abe529af | 2217 | |
98403001 BP |
2218 | cookie_u64 = nl_attr_get_u64(nl_attr_find_nested( |
2219 | odp_actions, | |
2220 | OVS_USERSPACE_ATTR_USERDATA)); | |
2221 | memcpy(&cookie, &cookie_u64, sizeof cookie); | |
2222 | if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) { | |
6ff686f2 PS |
2223 | /* As an optimization, avoid a round-trip from userspace to kernel |
2224 | * to userspace. This also avoids possibly filling up kernel packet | |
2225 | * buffers along the way. | |
2226 | * This optimization does not work in case of sFlow is turned ON. | |
2227 | * Since first action would be sFlow SAMPLE action followed by | |
2228 | * Controller action. */ | |
2229 | ||
2230 | upcall.type = DPIF_UC_ACTION; | |
2231 | upcall.packet = packet; | |
2232 | upcall.key = NULL; | |
2233 | upcall.key_len = 0; | |
2234 | upcall.userdata = nl_attr_get_u64(odp_actions); | |
2235 | ||
2236 | send_packet_in(ofproto, &upcall, flow, false); | |
2237 | return true; | |
2238 | } | |
2239 | } | |
abe529af | 2240 | |
6ff686f2 PS |
2241 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); |
2242 | odp_flow_key_from_flow(&key, flow); | |
80e5eed9 | 2243 | |
6ff686f2 PS |
2244 | error = dpif_execute(ofproto->dpif, key.data, key.size, |
2245 | odp_actions, actions_len, packet); | |
80e5eed9 | 2246 | |
6ff686f2 PS |
2247 | ofpbuf_delete(packet); |
2248 | return !error; | |
abe529af BP |
2249 | } |
2250 | ||
2251 | /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s | |
2252 | * statistics appropriately. 'packet' must have at least sizeof(struct | |
2253 | * ofp_packet_in) bytes of headroom. | |
2254 | * | |
2255 | * For correct results, 'packet' must actually be in 'facet''s flow; that is, | |
2256 | * applying flow_extract() to 'packet' would yield the same flow as | |
2257 | * 'facet->flow'. | |
2258 | * | |
df2c07f4 JP |
2259 | * 'facet' must have accurately composed datapath actions; that is, it must |
2260 | * not be in need of revalidation. | |
abe529af BP |
2261 | * |
2262 | * Takes ownership of 'packet'. */ | |
2263 | static void | |
2264 | facet_execute(struct ofproto_dpif *ofproto, struct facet *facet, | |
2265 | struct ofpbuf *packet) | |
2266 | { | |
2267 | struct dpif_flow_stats stats; | |
2268 | ||
2269 | assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in)); | |
2270 | ||
572b7068 | 2271 | dpif_flow_stats_extract(&facet->flow, packet, &stats); |
abe529af BP |
2272 | stats.used = time_msec(); |
2273 | if (execute_odp_actions(ofproto, &facet->flow, | |
2274 | facet->actions, facet->actions_len, packet)) { | |
2275 | facet_update_stats(ofproto, facet, &stats); | |
2276 | } | |
2277 | } | |
2278 | ||
2279 | /* Remove 'facet' from 'ofproto' and free up the associated memory: | |
2280 | * | |
2281 | * - If 'facet' was installed in the datapath, uninstalls it and updates its | |
2282 | * rule's statistics, via facet_uninstall(). | |
2283 | * | |
2284 | * - Removes 'facet' from its rule and from ofproto->facets. | |
2285 | */ | |
2286 | static void | |
2287 | facet_remove(struct ofproto_dpif *ofproto, struct facet *facet) | |
2288 | { | |
2289 | facet_uninstall(ofproto, facet); | |
2290 | facet_flush_stats(ofproto, facet); | |
2291 | hmap_remove(&ofproto->facets, &facet->hmap_node); | |
2292 | list_remove(&facet->list_node); | |
2293 | facet_free(facet); | |
2294 | } | |
2295 | ||
df2c07f4 | 2296 | /* Composes the datapath actions for 'facet' based on its rule's actions. */ |
abe529af BP |
2297 | static void |
2298 | facet_make_actions(struct ofproto_dpif *p, struct facet *facet, | |
2299 | const struct ofpbuf *packet) | |
2300 | { | |
2301 | const struct rule_dpif *rule = facet->rule; | |
2302 | struct ofpbuf *odp_actions; | |
2303 | struct action_xlate_ctx ctx; | |
2304 | ||
2305 | action_xlate_ctx_init(&ctx, p, &facet->flow, packet); | |
2306 | odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions); | |
2307 | facet->tags = ctx.tags; | |
2308 | facet->may_install = ctx.may_set_up_flow; | |
75a75043 BP |
2309 | facet->has_learn = ctx.has_learn; |
2310 | facet->has_normal = ctx.has_normal; | |
abe529af BP |
2311 | facet->nf_flow.output_iface = ctx.nf_output_iface; |
2312 | ||
2313 | if (facet->actions_len != odp_actions->size | |
2314 | || memcmp(facet->actions, odp_actions->data, odp_actions->size)) { | |
2315 | free(facet->actions); | |
2316 | facet->actions_len = odp_actions->size; | |
2317 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
2318 | } | |
2319 | ||
2320 | ofpbuf_delete(odp_actions); | |
2321 | } | |
2322 | ||
3a88e544 BP |
2323 | /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len' |
2324 | * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters | |
2325 | * in the datapath will be zeroed and 'stats' will be updated with traffic new | |
2326 | * since 'facet' was last updated. | |
2327 | * | |
2328 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
abe529af BP |
2329 | static int |
2330 | facet_put__(struct ofproto_dpif *ofproto, struct facet *facet, | |
2331 | const struct nlattr *actions, size_t actions_len, | |
2332 | struct dpif_flow_stats *stats) | |
2333 | { | |
2334 | struct odputil_keybuf keybuf; | |
2335 | enum dpif_flow_put_flags flags; | |
2336 | struct ofpbuf key; | |
3a88e544 | 2337 | int ret; |
abe529af BP |
2338 | |
2339 | flags = DPIF_FP_CREATE | DPIF_FP_MODIFY; | |
2340 | if (stats) { | |
2341 | flags |= DPIF_FP_ZERO_STATS; | |
abe529af BP |
2342 | } |
2343 | ||
2344 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2345 | odp_flow_key_from_flow(&key, &facet->flow); | |
2346 | ||
3a88e544 BP |
2347 | ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size, |
2348 | actions, actions_len, stats); | |
2349 | ||
2350 | if (stats) { | |
2351 | facet_reset_dp_stats(facet, stats); | |
2352 | } | |
2353 | ||
2354 | return ret; | |
abe529af BP |
2355 | } |
2356 | ||
2357 | /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If | |
2358 | * 'zero_stats' is true, clears any existing statistics from the datapath for | |
2359 | * 'facet'. */ | |
2360 | static void | |
2361 | facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats) | |
2362 | { | |
2363 | struct dpif_flow_stats stats; | |
2364 | ||
2365 | if (facet->may_install | |
2366 | && !facet_put__(p, facet, facet->actions, facet->actions_len, | |
2367 | zero_stats ? &stats : NULL)) { | |
2368 | facet->installed = true; | |
2369 | } | |
2370 | } | |
2371 | ||
2372 | static void | |
55af77bb | 2373 | facet_account(struct ofproto_dpif *ofproto, struct facet *facet) |
abe529af | 2374 | { |
55af77bb | 2375 | uint64_t n_bytes; |
abe529af | 2376 | const struct nlattr *a; |
abe529af | 2377 | unsigned int left; |
d78be13b | 2378 | ovs_be16 vlan_tci; |
abe529af | 2379 | |
55af77bb | 2380 | if (facet->byte_count <= facet->accounted_bytes) { |
abe529af BP |
2381 | return; |
2382 | } | |
55af77bb EJ |
2383 | n_bytes = facet->byte_count - facet->accounted_bytes; |
2384 | facet->accounted_bytes = facet->byte_count; | |
abe529af | 2385 | |
75a75043 | 2386 | /* Feed information from the active flows back into the learning table to |
abe529af BP |
2387 | * ensure that table is always in sync with what is actually flowing |
2388 | * through the datapath. */ | |
75a75043 BP |
2389 | if (facet->has_learn || facet->has_normal) { |
2390 | struct action_xlate_ctx ctx; | |
abe529af | 2391 | |
75a75043 BP |
2392 | action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); |
2393 | ctx.may_learn = true; | |
2394 | ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions, | |
2395 | facet->rule->up.n_actions)); | |
2396 | } | |
abe529af | 2397 | |
75a75043 | 2398 | if (!facet->has_normal || !ofproto->has_bonded_bundles) { |
abe529af BP |
2399 | return; |
2400 | } | |
d78be13b BP |
2401 | |
2402 | /* This loop feeds byte counters to bond_account() for rebalancing to use | |
2403 | * as a basis. We also need to track the actual VLAN on which the packet | |
2404 | * is going to be sent to ensure that it matches the one passed to | |
2405 | * bond_choose_output_slave(). (Otherwise, we will account to the wrong | |
2406 | * hash bucket.) */ | |
2407 | vlan_tci = facet->flow.vlan_tci; | |
abe529af | 2408 | NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) { |
d78be13b | 2409 | struct ofport_dpif *port; |
abe529af | 2410 | |
d78be13b | 2411 | switch (nl_attr_type(a)) { |
df2c07f4 | 2412 | case OVS_ACTION_ATTR_OUTPUT: |
abe529af BP |
2413 | port = get_odp_port(ofproto, nl_attr_get_u32(a)); |
2414 | if (port && port->bundle && port->bundle->bond) { | |
d78be13b | 2415 | bond_account(port->bundle->bond, &facet->flow, |
dc155bff | 2416 | vlan_tci_to_vid(vlan_tci), n_bytes); |
abe529af | 2417 | } |
d78be13b BP |
2418 | break; |
2419 | ||
d9065a90 | 2420 | case OVS_ACTION_ATTR_POP_VLAN: |
d78be13b BP |
2421 | vlan_tci = htons(0); |
2422 | break; | |
2423 | ||
d9065a90 | 2424 | case OVS_ACTION_ATTR_PUSH_VLAN: |
d78be13b BP |
2425 | vlan_tci = nl_attr_get_be16(a); |
2426 | break; | |
abe529af BP |
2427 | } |
2428 | } | |
2429 | } | |
2430 | ||
2431 | /* If 'rule' is installed in the datapath, uninstalls it. */ | |
2432 | static void | |
2433 | facet_uninstall(struct ofproto_dpif *p, struct facet *facet) | |
2434 | { | |
2435 | if (facet->installed) { | |
2436 | struct odputil_keybuf keybuf; | |
2437 | struct dpif_flow_stats stats; | |
2438 | struct ofpbuf key; | |
3a88e544 | 2439 | int error; |
abe529af BP |
2440 | |
2441 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2442 | odp_flow_key_from_flow(&key, &facet->flow); | |
2443 | ||
3a88e544 BP |
2444 | error = dpif_flow_del(p->dpif, key.data, key.size, &stats); |
2445 | facet_reset_dp_stats(facet, &stats); | |
2446 | if (!error) { | |
abe529af BP |
2447 | facet_update_stats(p, facet, &stats); |
2448 | } | |
2449 | facet->installed = false; | |
abe529af BP |
2450 | } else { |
2451 | assert(facet->dp_packet_count == 0); | |
2452 | assert(facet->dp_byte_count == 0); | |
2453 | } | |
2454 | } | |
2455 | ||
2456 | /* Returns true if the only action for 'facet' is to send to the controller. | |
2457 | * (We don't report NetFlow expiration messages for such facets because they | |
2458 | * are just part of the control logic for the network, not real traffic). */ | |
2459 | static bool | |
2460 | facet_is_controller_flow(struct facet *facet) | |
2461 | { | |
2462 | return (facet | |
2463 | && facet->rule->up.n_actions == 1 | |
2464 | && action_outputs_to_port(&facet->rule->up.actions[0], | |
2465 | htons(OFPP_CONTROLLER))); | |
2466 | } | |
2467 | ||
3a88e544 BP |
2468 | /* Resets 'facet''s datapath statistics counters. This should be called when |
2469 | * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null, | |
2470 | * it should contain the statistics returned by dpif when 'facet' was reset in | |
2471 | * the datapath. 'stats' will be modified to only included statistics new | |
2472 | * since 'facet' was last updated. */ | |
2473 | static void | |
2474 | facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats) | |
2475 | { | |
2476 | if (stats && facet->dp_packet_count <= stats->n_packets | |
2477 | && facet->dp_byte_count <= stats->n_bytes) { | |
2478 | stats->n_packets -= facet->dp_packet_count; | |
2479 | stats->n_bytes -= facet->dp_byte_count; | |
2480 | } | |
2481 | ||
2482 | facet->dp_packet_count = 0; | |
2483 | facet->dp_byte_count = 0; | |
2484 | } | |
2485 | ||
abe529af BP |
2486 | /* Folds all of 'facet''s statistics into its rule. Also updates the |
2487 | * accounting ofhook and emits a NetFlow expiration if appropriate. All of | |
2488 | * 'facet''s statistics in the datapath should have been zeroed and folded into | |
2489 | * its packet and byte counts before this function is called. */ | |
2490 | static void | |
2491 | facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet) | |
2492 | { | |
2493 | assert(!facet->dp_byte_count); | |
2494 | assert(!facet->dp_packet_count); | |
2495 | ||
2496 | facet_push_stats(facet); | |
55af77bb | 2497 | facet_account(ofproto, facet); |
abe529af BP |
2498 | |
2499 | if (ofproto->netflow && !facet_is_controller_flow(facet)) { | |
2500 | struct ofexpired expired; | |
2501 | expired.flow = facet->flow; | |
2502 | expired.packet_count = facet->packet_count; | |
2503 | expired.byte_count = facet->byte_count; | |
2504 | expired.used = facet->used; | |
2505 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
2506 | } | |
2507 | ||
2508 | facet->rule->packet_count += facet->packet_count; | |
2509 | facet->rule->byte_count += facet->byte_count; | |
2510 | ||
2511 | /* Reset counters to prevent double counting if 'facet' ever gets | |
2512 | * reinstalled. */ | |
bbb5d219 | 2513 | facet_reset_counters(facet); |
abe529af BP |
2514 | |
2515 | netflow_flow_clear(&facet->nf_flow); | |
2516 | } | |
2517 | ||
2518 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
2519 | * Returns it if found, otherwise a null pointer. | |
2520 | * | |
2521 | * The returned facet might need revalidation; use facet_lookup_valid() | |
2522 | * instead if that is important. */ | |
2523 | static struct facet * | |
2524 | facet_find(struct ofproto_dpif *ofproto, const struct flow *flow) | |
2525 | { | |
2526 | struct facet *facet; | |
2527 | ||
2528 | HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0), | |
2529 | &ofproto->facets) { | |
2530 | if (flow_equal(flow, &facet->flow)) { | |
2531 | return facet; | |
2532 | } | |
2533 | } | |
2534 | ||
2535 | return NULL; | |
2536 | } | |
2537 | ||
2538 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
2539 | * Returns it if found, otherwise a null pointer. | |
2540 | * | |
2541 | * The returned facet is guaranteed to be valid. */ | |
2542 | static struct facet * | |
2543 | facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow) | |
2544 | { | |
2545 | struct facet *facet = facet_find(ofproto, flow); | |
2546 | ||
2547 | /* The facet we found might not be valid, since we could be in need of | |
2548 | * revalidation. If it is not valid, don't return it. */ | |
2549 | if (facet | |
0e4b3771 BP |
2550 | && (ofproto->need_revalidate |
2551 | || tag_set_intersects(&ofproto->revalidate_set, facet->tags)) | |
abe529af BP |
2552 | && !facet_revalidate(ofproto, facet)) { |
2553 | COVERAGE_INC(facet_invalidated); | |
2554 | return NULL; | |
2555 | } | |
2556 | ||
2557 | return facet; | |
2558 | } | |
2559 | ||
2560 | /* Re-searches 'ofproto''s classifier for a rule matching 'facet': | |
2561 | * | |
2562 | * - If the rule found is different from 'facet''s current rule, moves | |
2563 | * 'facet' to the new rule and recompiles its actions. | |
2564 | * | |
2565 | * - If the rule found is the same as 'facet''s current rule, leaves 'facet' | |
2566 | * where it is and recompiles its actions anyway. | |
2567 | * | |
2568 | * - If there is none, destroys 'facet'. | |
2569 | * | |
2570 | * Returns true if 'facet' still exists, false if it has been destroyed. */ | |
2571 | static bool | |
2572 | facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet) | |
2573 | { | |
2574 | struct action_xlate_ctx ctx; | |
2575 | struct ofpbuf *odp_actions; | |
2576 | struct rule_dpif *new_rule; | |
2577 | bool actions_changed; | |
2578 | ||
2579 | COVERAGE_INC(facet_revalidate); | |
2580 | ||
2581 | /* Determine the new rule. */ | |
29901626 | 2582 | new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0); |
abe529af BP |
2583 | if (!new_rule) { |
2584 | /* No new rule, so delete the facet. */ | |
2585 | facet_remove(ofproto, facet); | |
2586 | return false; | |
2587 | } | |
2588 | ||
df2c07f4 | 2589 | /* Calculate new datapath actions. |
abe529af BP |
2590 | * |
2591 | * We do not modify any 'facet' state yet, because we might need to, e.g., | |
2592 | * emit a NetFlow expiration and, if so, we need to have the old state | |
2593 | * around to properly compose it. */ | |
2594 | action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); | |
2595 | odp_actions = xlate_actions(&ctx, | |
2596 | new_rule->up.actions, new_rule->up.n_actions); | |
2597 | actions_changed = (facet->actions_len != odp_actions->size | |
2598 | || memcmp(facet->actions, odp_actions->data, | |
2599 | facet->actions_len)); | |
2600 | ||
df2c07f4 JP |
2601 | /* If the datapath actions changed or the installability changed, |
2602 | * then we need to talk to the datapath. */ | |
abe529af BP |
2603 | if (actions_changed || ctx.may_set_up_flow != facet->installed) { |
2604 | if (ctx.may_set_up_flow) { | |
2605 | struct dpif_flow_stats stats; | |
2606 | ||
2607 | facet_put__(ofproto, facet, | |
2608 | odp_actions->data, odp_actions->size, &stats); | |
2609 | facet_update_stats(ofproto, facet, &stats); | |
2610 | } else { | |
2611 | facet_uninstall(ofproto, facet); | |
2612 | } | |
2613 | ||
2614 | /* The datapath flow is gone or has zeroed stats, so push stats out of | |
2615 | * 'facet' into 'rule'. */ | |
2616 | facet_flush_stats(ofproto, facet); | |
2617 | } | |
2618 | ||
2619 | /* Update 'facet' now that we've taken care of all the old state. */ | |
2620 | facet->tags = ctx.tags; | |
2621 | facet->nf_flow.output_iface = ctx.nf_output_iface; | |
2622 | facet->may_install = ctx.may_set_up_flow; | |
75a75043 BP |
2623 | facet->has_learn = ctx.has_learn; |
2624 | facet->has_normal = ctx.has_normal; | |
abe529af BP |
2625 | if (actions_changed) { |
2626 | free(facet->actions); | |
2627 | facet->actions_len = odp_actions->size; | |
2628 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
2629 | } | |
2630 | if (facet->rule != new_rule) { | |
2631 | COVERAGE_INC(facet_changed_rule); | |
2632 | list_remove(&facet->list_node); | |
2633 | list_push_back(&new_rule->facets, &facet->list_node); | |
2634 | facet->rule = new_rule; | |
2635 | facet->used = new_rule->up.created; | |
2636 | facet->rs_used = facet->used; | |
2637 | } | |
2638 | ||
2639 | ofpbuf_delete(odp_actions); | |
2640 | ||
2641 | return true; | |
2642 | } | |
2643 | ||
2644 | /* Updates 'facet''s used time. Caller is responsible for calling | |
2645 | * facet_push_stats() to update the flows which 'facet' resubmits into. */ | |
2646 | static void | |
2647 | facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet, | |
2648 | long long int used) | |
2649 | { | |
2650 | if (used > facet->used) { | |
2651 | facet->used = used; | |
2652 | if (used > facet->rule->used) { | |
2653 | facet->rule->used = used; | |
2654 | } | |
2655 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used); | |
2656 | } | |
2657 | } | |
2658 | ||
2659 | /* Folds the statistics from 'stats' into the counters in 'facet'. | |
2660 | * | |
2661 | * Because of the meaning of a facet's counters, it only makes sense to do this | |
2662 | * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a | |
2663 | * packet that was sent by hand or if it represents statistics that have been | |
2664 | * cleared out of the datapath. */ | |
2665 | static void | |
2666 | facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet, | |
2667 | const struct dpif_flow_stats *stats) | |
2668 | { | |
2669 | if (stats->n_packets || stats->used > facet->used) { | |
2670 | facet_update_time(ofproto, facet, stats->used); | |
2671 | facet->packet_count += stats->n_packets; | |
2672 | facet->byte_count += stats->n_bytes; | |
2673 | facet_push_stats(facet); | |
2674 | netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags); | |
2675 | } | |
2676 | } | |
2677 | ||
bbb5d219 EJ |
2678 | static void |
2679 | facet_reset_counters(struct facet *facet) | |
2680 | { | |
2681 | facet->packet_count = 0; | |
2682 | facet->byte_count = 0; | |
2683 | facet->rs_packet_count = 0; | |
2684 | facet->rs_byte_count = 0; | |
2685 | facet->accounted_bytes = 0; | |
2686 | } | |
2687 | ||
abe529af BP |
2688 | static void |
2689 | facet_push_stats(struct facet *facet) | |
2690 | { | |
2691 | uint64_t rs_packets, rs_bytes; | |
2692 | ||
2693 | assert(facet->packet_count >= facet->rs_packet_count); | |
2694 | assert(facet->byte_count >= facet->rs_byte_count); | |
2695 | assert(facet->used >= facet->rs_used); | |
2696 | ||
2697 | rs_packets = facet->packet_count - facet->rs_packet_count; | |
2698 | rs_bytes = facet->byte_count - facet->rs_byte_count; | |
2699 | ||
2700 | if (rs_packets || rs_bytes || facet->used > facet->rs_used) { | |
2701 | facet->rs_packet_count = facet->packet_count; | |
2702 | facet->rs_byte_count = facet->byte_count; | |
2703 | facet->rs_used = facet->used; | |
2704 | ||
2705 | flow_push_stats(facet->rule, &facet->flow, | |
2706 | rs_packets, rs_bytes, facet->used); | |
2707 | } | |
2708 | } | |
2709 | ||
2710 | struct ofproto_push { | |
2711 | struct action_xlate_ctx ctx; | |
2712 | uint64_t packets; | |
2713 | uint64_t bytes; | |
2714 | long long int used; | |
2715 | }; | |
2716 | ||
2717 | static void | |
2718 | push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule) | |
2719 | { | |
2720 | struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx); | |
2721 | ||
2722 | if (rule) { | |
2723 | rule->packet_count += push->packets; | |
2724 | rule->byte_count += push->bytes; | |
2725 | rule->used = MAX(push->used, rule->used); | |
2726 | } | |
2727 | } | |
2728 | ||
2729 | /* Pushes flow statistics to the rules which 'flow' resubmits into given | |
2730 | * 'rule''s actions. */ | |
2731 | static void | |
2732 | flow_push_stats(const struct rule_dpif *rule, | |
2733 | struct flow *flow, uint64_t packets, uint64_t bytes, | |
2734 | long long int used) | |
2735 | { | |
2736 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2737 | struct ofproto_push push; | |
2738 | ||
2739 | push.packets = packets; | |
2740 | push.bytes = bytes; | |
2741 | push.used = used; | |
2742 | ||
2743 | action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL); | |
2744 | push.ctx.resubmit_hook = push_resubmit; | |
2745 | ofpbuf_delete(xlate_actions(&push.ctx, | |
2746 | rule->up.actions, rule->up.n_actions)); | |
2747 | } | |
2748 | \f | |
2749 | /* Rules. */ | |
2750 | ||
2751 | static struct rule_dpif * | |
29901626 BP |
2752 | rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow, |
2753 | uint8_t table_id) | |
abe529af | 2754 | { |
9cdaaebe BP |
2755 | if (table_id >= N_TABLES) { |
2756 | return NULL; | |
2757 | } | |
2758 | ||
154896e3 | 2759 | return rule_dpif_cast(rule_from_cls_rule( |
29901626 | 2760 | classifier_lookup(&ofproto->up.tables[table_id], |
6c1491fb | 2761 | flow))); |
abe529af BP |
2762 | } |
2763 | ||
7ee20df1 BP |
2764 | static void |
2765 | complete_operation(struct rule_dpif *rule) | |
2766 | { | |
2767 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2768 | ||
54a9cbc9 | 2769 | rule_invalidate(rule); |
7ee20df1 BP |
2770 | if (clogged) { |
2771 | struct dpif_completion *c = xmalloc(sizeof *c); | |
2772 | c->op = rule->up.pending; | |
2773 | list_push_back(&ofproto->completions, &c->list_node); | |
2774 | } else { | |
2775 | ofoperation_complete(rule->up.pending, 0); | |
2776 | } | |
2777 | } | |
2778 | ||
abe529af BP |
2779 | static struct rule * |
2780 | rule_alloc(void) | |
2781 | { | |
2782 | struct rule_dpif *rule = xmalloc(sizeof *rule); | |
2783 | return &rule->up; | |
2784 | } | |
2785 | ||
2786 | static void | |
2787 | rule_dealloc(struct rule *rule_) | |
2788 | { | |
2789 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2790 | free(rule); | |
2791 | } | |
2792 | ||
2793 | static int | |
2794 | rule_construct(struct rule *rule_) | |
2795 | { | |
2796 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2797 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
7ee20df1 | 2798 | struct rule_dpif *victim; |
54a9cbc9 | 2799 | uint8_t table_id; |
5bf0e941 BP |
2800 | int error; |
2801 | ||
2802 | error = validate_actions(rule->up.actions, rule->up.n_actions, | |
2803 | &rule->up.cr.flow, ofproto->max_ports); | |
2804 | if (error) { | |
2805 | return error; | |
2806 | } | |
abe529af BP |
2807 | |
2808 | rule->used = rule->up.created; | |
2809 | rule->packet_count = 0; | |
2810 | rule->byte_count = 0; | |
abe529af | 2811 | |
7ee20df1 BP |
2812 | victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending)); |
2813 | if (victim && !list_is_empty(&victim->facets)) { | |
2814 | struct facet *facet; | |
2815 | ||
2816 | rule->facets = victim->facets; | |
2817 | list_moved(&rule->facets); | |
2818 | LIST_FOR_EACH (facet, list_node, &rule->facets) { | |
bbb5d219 EJ |
2819 | /* XXX: We're only clearing our local counters here. It's possible |
2820 | * that quite a few packets are unaccounted for in the datapath | |
2821 | * statistics. These will be accounted to the new rule instead of | |
2822 | * cleared as required. This could be fixed by clearing out the | |
2823 | * datapath statistics for this facet, but currently it doesn't | |
2824 | * seem worth it. */ | |
2825 | facet_reset_counters(facet); | |
7ee20df1 BP |
2826 | facet->rule = rule; |
2827 | } | |
2828 | } else { | |
2829 | /* Must avoid list_moved() in this case. */ | |
2830 | list_init(&rule->facets); | |
2831 | } | |
abe529af | 2832 | |
54a9cbc9 BP |
2833 | table_id = rule->up.table_id; |
2834 | rule->tag = (victim ? victim->tag | |
2835 | : table_id == 0 ? 0 | |
2836 | : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc, | |
2837 | ofproto->tables[table_id].basis)); | |
2838 | ||
7ee20df1 | 2839 | complete_operation(rule); |
abe529af BP |
2840 | return 0; |
2841 | } | |
2842 | ||
2843 | static void | |
2844 | rule_destruct(struct rule *rule_) | |
2845 | { | |
2846 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2847 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2848 | struct facet *facet, *next_facet; | |
2849 | ||
abe529af BP |
2850 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { |
2851 | facet_revalidate(ofproto, facet); | |
2852 | } | |
7ee20df1 BP |
2853 | |
2854 | complete_operation(rule); | |
abe529af BP |
2855 | } |
2856 | ||
2857 | static void | |
2858 | rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes) | |
2859 | { | |
2860 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2861 | struct facet *facet; | |
2862 | ||
2863 | /* Start from historical data for 'rule' itself that are no longer tracked | |
2864 | * in facets. This counts, for example, facets that have expired. */ | |
2865 | *packets = rule->packet_count; | |
2866 | *bytes = rule->byte_count; | |
2867 | ||
2868 | /* Add any statistics that are tracked by facets. This includes | |
2869 | * statistical data recently updated by ofproto_update_stats() as well as | |
2870 | * stats for packets that were executed "by hand" via dpif_execute(). */ | |
2871 | LIST_FOR_EACH (facet, list_node, &rule->facets) { | |
2872 | *packets += facet->packet_count; | |
2873 | *bytes += facet->byte_count; | |
2874 | } | |
2875 | } | |
2876 | ||
5bf0e941 | 2877 | static int |
abe529af BP |
2878 | rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet) |
2879 | { | |
2880 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2881 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2882 | struct action_xlate_ctx ctx; | |
2883 | struct ofpbuf *odp_actions; | |
2884 | struct facet *facet; | |
2885 | size_t size; | |
2886 | ||
2887 | /* First look for a related facet. If we find one, account it to that. */ | |
2888 | facet = facet_lookup_valid(ofproto, flow); | |
2889 | if (facet && facet->rule == rule) { | |
eea109bb BP |
2890 | if (!facet->may_install) { |
2891 | facet_make_actions(ofproto, facet, packet); | |
2892 | } | |
abe529af | 2893 | facet_execute(ofproto, facet, packet); |
5bf0e941 | 2894 | return 0; |
abe529af BP |
2895 | } |
2896 | ||
2897 | /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then | |
2898 | * create a new facet for it and use that. */ | |
29901626 | 2899 | if (rule_dpif_lookup(ofproto, flow, 0) == rule) { |
abe529af BP |
2900 | facet = facet_create(rule, flow, packet); |
2901 | facet_execute(ofproto, facet, packet); | |
2902 | facet_install(ofproto, facet, true); | |
5bf0e941 | 2903 | return 0; |
abe529af BP |
2904 | } |
2905 | ||
2906 | /* We can't account anything to a facet. If we were to try, then that | |
2907 | * facet would have a non-matching rule, busting our invariants. */ | |
2908 | action_xlate_ctx_init(&ctx, ofproto, flow, packet); | |
2909 | odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions); | |
2910 | size = packet->size; | |
2911 | if (execute_odp_actions(ofproto, flow, odp_actions->data, | |
2912 | odp_actions->size, packet)) { | |
2913 | rule->used = time_msec(); | |
2914 | rule->packet_count++; | |
2915 | rule->byte_count += size; | |
2916 | flow_push_stats(rule, flow, 1, size, rule->used); | |
2917 | } | |
2918 | ofpbuf_delete(odp_actions); | |
5bf0e941 BP |
2919 | |
2920 | return 0; | |
abe529af BP |
2921 | } |
2922 | ||
7ee20df1 BP |
2923 | static void |
2924 | rule_modify_actions(struct rule *rule_) | |
abe529af BP |
2925 | { |
2926 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2927 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2928 | int error; | |
2929 | ||
7ee20df1 BP |
2930 | error = validate_actions(rule->up.actions, rule->up.n_actions, |
2931 | &rule->up.cr.flow, ofproto->max_ports); | |
2932 | if (error) { | |
2933 | ofoperation_complete(rule->up.pending, error); | |
2934 | return; | |
abe529af | 2935 | } |
7ee20df1 BP |
2936 | |
2937 | complete_operation(rule); | |
abe529af BP |
2938 | } |
2939 | \f | |
b47e2a82 | 2940 | /* Sends 'packet' out of port 'odp_port' within 'ofproto'. |
abe529af BP |
2941 | * Returns 0 if successful, otherwise a positive errno value. */ |
2942 | static int | |
b2fda3ef | 2943 | send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port, |
abe529af BP |
2944 | const struct ofpbuf *packet) |
2945 | { | |
80e5eed9 BP |
2946 | struct ofpbuf key, odp_actions; |
2947 | struct odputil_keybuf keybuf; | |
2948 | struct flow flow; | |
abe529af BP |
2949 | int error; |
2950 | ||
80e5eed9 BP |
2951 | flow_extract((struct ofpbuf *) packet, 0, 0, &flow); |
2952 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2953 | odp_flow_key_from_flow(&key, &flow); | |
2954 | ||
abe529af | 2955 | ofpbuf_init(&odp_actions, 32); |
6ff686f2 PS |
2956 | compose_sflow_action(ofproto, &odp_actions, &flow, odp_port); |
2957 | ||
df2c07f4 | 2958 | nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port); |
80e5eed9 BP |
2959 | error = dpif_execute(ofproto->dpif, |
2960 | key.data, key.size, | |
2961 | odp_actions.data, odp_actions.size, | |
abe529af BP |
2962 | packet); |
2963 | ofpbuf_uninit(&odp_actions); | |
2964 | ||
2965 | if (error) { | |
2966 | VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)", | |
2967 | ofproto->up.name, odp_port, strerror(error)); | |
2968 | } | |
2969 | return error; | |
2970 | } | |
2971 | \f | |
df2c07f4 | 2972 | /* OpenFlow to datapath action translation. */ |
abe529af BP |
2973 | |
2974 | static void do_xlate_actions(const union ofp_action *in, size_t n_in, | |
2975 | struct action_xlate_ctx *ctx); | |
4cd78906 | 2976 | static void xlate_normal(struct action_xlate_ctx *); |
abe529af | 2977 | |
98403001 BP |
2978 | static size_t |
2979 | put_userspace_action(const struct ofproto_dpif *ofproto, | |
2980 | struct ofpbuf *odp_actions, | |
2981 | const struct flow *flow, | |
2982 | const struct user_action_cookie *cookie) | |
2983 | { | |
2984 | size_t offset; | |
2985 | uint32_t pid; | |
2986 | ||
2987 | pid = dpif_port_get_pid(ofproto->dpif, | |
2988 | ofp_port_to_odp_port(flow->in_port)); | |
2989 | ||
2990 | offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE); | |
2991 | nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid); | |
2992 | nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA, | |
2993 | cookie, sizeof *cookie); | |
2994 | nl_msg_end_nested(odp_actions, offset); | |
2995 | ||
2996 | return odp_actions->size - NLA_ALIGN(sizeof *cookie); | |
2997 | } | |
2998 | ||
6ff686f2 PS |
2999 | /* Compose SAMPLE action for sFlow. */ |
3000 | static size_t | |
3001 | compose_sflow_action(const struct ofproto_dpif *ofproto, | |
3002 | struct ofpbuf *odp_actions, | |
3003 | const struct flow *flow, | |
3004 | uint32_t odp_port) | |
3005 | { | |
3006 | uint32_t port_ifindex; | |
3007 | uint32_t probability; | |
98403001 | 3008 | struct user_action_cookie cookie; |
6ff686f2 | 3009 | size_t sample_offset, actions_offset; |
98403001 | 3010 | int cookie_offset, n_output; |
6ff686f2 PS |
3011 | |
3012 | if (!ofproto->sflow || flow->in_port == OFPP_NONE) { | |
3013 | return 0; | |
3014 | } | |
3015 | ||
3016 | if (odp_port == OVSP_NONE) { | |
3017 | port_ifindex = 0; | |
3018 | n_output = 0; | |
3019 | } else { | |
3020 | port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port); | |
3021 | n_output = 1; | |
3022 | } | |
3023 | ||
3024 | sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE); | |
3025 | ||
3026 | /* Number of packets out of UINT_MAX to sample. */ | |
3027 | probability = dpif_sflow_get_probability(ofproto->sflow); | |
3028 | nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability); | |
3029 | ||
3030 | actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS); | |
3031 | ||
98403001 BP |
3032 | cookie.type = USER_ACTION_COOKIE_SFLOW; |
3033 | cookie.data = port_ifindex; | |
3034 | cookie.n_output = n_output; | |
3035 | cookie.vlan_tci = 0; | |
3036 | cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie); | |
6ff686f2 PS |
3037 | |
3038 | nl_msg_end_nested(odp_actions, actions_offset); | |
3039 | nl_msg_end_nested(odp_actions, sample_offset); | |
98403001 | 3040 | return cookie_offset; |
6ff686f2 PS |
3041 | } |
3042 | ||
3043 | /* SAMPLE action must be first action in any given list of actions. | |
3044 | * At this point we do not have all information required to build it. So try to | |
3045 | * build sample action as complete as possible. */ | |
3046 | static void | |
3047 | add_sflow_action(struct action_xlate_ctx *ctx) | |
3048 | { | |
3049 | ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto, | |
3050 | ctx->odp_actions, | |
3051 | &ctx->flow, OVSP_NONE); | |
3052 | ctx->sflow_odp_port = 0; | |
3053 | ctx->sflow_n_outputs = 0; | |
3054 | } | |
3055 | ||
3056 | /* Fix SAMPLE action according to data collected while composing ODP actions. | |
3057 | * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested | |
3058 | * USERSPACE action's user-cookie which is required for sflow. */ | |
3059 | static void | |
3060 | fix_sflow_action(struct action_xlate_ctx *ctx) | |
3061 | { | |
3062 | const struct flow *base = &ctx->base_flow; | |
3063 | struct user_action_cookie *cookie; | |
3064 | ||
3065 | if (!ctx->user_cookie_offset) { | |
3066 | return; | |
3067 | } | |
3068 | ||
3069 | cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset, | |
3070 | sizeof(*cookie)); | |
3071 | assert(cookie != NULL); | |
3072 | assert(cookie->type == USER_ACTION_COOKIE_SFLOW); | |
3073 | ||
3074 | if (ctx->sflow_n_outputs) { | |
3075 | cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow, | |
3076 | ctx->sflow_odp_port); | |
3077 | } | |
3078 | if (ctx->sflow_n_outputs >= 255) { | |
3079 | cookie->n_output = 255; | |
3080 | } else { | |
3081 | cookie->n_output = ctx->sflow_n_outputs; | |
3082 | } | |
3083 | cookie->vlan_tci = base->vlan_tci; | |
3084 | } | |
3085 | ||
823518f1 BP |
3086 | static void |
3087 | commit_vlan_tci(struct action_xlate_ctx *ctx, ovs_be16 vlan_tci) | |
3088 | { | |
3089 | struct flow *base = &ctx->base_flow; | |
3090 | struct ofpbuf *odp_actions = ctx->odp_actions; | |
3091 | ||
3092 | if (base->vlan_tci != vlan_tci) { | |
3093 | if (!(vlan_tci & htons(VLAN_CFI))) { | |
3094 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); | |
3095 | } else { | |
3096 | if (base->vlan_tci != htons(0)) { | |
3097 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); | |
3098 | } | |
3099 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
3100 | vlan_tci & ~htons(VLAN_CFI)); | |
3101 | } | |
3102 | base->vlan_tci = vlan_tci; | |
3103 | } | |
3104 | } | |
3105 | ||
b3e9b2ed EJ |
3106 | static void |
3107 | commit_odp_actions(struct action_xlate_ctx *ctx) | |
3108 | { | |
3109 | const struct flow *flow = &ctx->flow; | |
3110 | struct flow *base = &ctx->base_flow; | |
3111 | struct ofpbuf *odp_actions = ctx->odp_actions; | |
3112 | ||
3113 | if (base->tun_id != flow->tun_id) { | |
df2c07f4 | 3114 | nl_msg_put_be64(odp_actions, OVS_ACTION_ATTR_SET_TUNNEL, flow->tun_id); |
b3e9b2ed EJ |
3115 | base->tun_id = flow->tun_id; |
3116 | } | |
3117 | ||
3118 | if (base->nw_src != flow->nw_src) { | |
df2c07f4 | 3119 | nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_SRC, flow->nw_src); |
b3e9b2ed EJ |
3120 | base->nw_src = flow->nw_src; |
3121 | } | |
3122 | ||
3123 | if (base->nw_dst != flow->nw_dst) { | |
df2c07f4 | 3124 | nl_msg_put_be32(odp_actions, OVS_ACTION_ATTR_SET_NW_DST, flow->nw_dst); |
b3e9b2ed EJ |
3125 | base->nw_dst = flow->nw_dst; |
3126 | } | |
3127 | ||
150a9f15 | 3128 | if (base->nw_tos != flow->nw_tos) { |
df2c07f4 | 3129 | nl_msg_put_u8(odp_actions, OVS_ACTION_ATTR_SET_NW_TOS, flow->nw_tos); |
150a9f15 BP |
3130 | base->nw_tos = flow->nw_tos; |
3131 | } | |
3132 | ||
823518f1 | 3133 | commit_vlan_tci(ctx, flow->vlan_tci); |
b3e9b2ed EJ |
3134 | |
3135 | if (base->tp_src != flow->tp_src) { | |
df2c07f4 | 3136 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_SRC, flow->tp_src); |
b3e9b2ed EJ |
3137 | base->tp_src = flow->tp_src; |
3138 | } | |
3139 | ||
3140 | if (base->tp_dst != flow->tp_dst) { | |
df2c07f4 | 3141 | nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_SET_TP_DST, flow->tp_dst); |
b3e9b2ed EJ |
3142 | base->tp_dst = flow->tp_dst; |
3143 | } | |
3144 | ||
3145 | if (!eth_addr_equals(base->dl_src, flow->dl_src)) { | |
df2c07f4 | 3146 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_SRC, |
b3e9b2ed EJ |
3147 | flow->dl_src, ETH_ADDR_LEN); |
3148 | memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN); | |
3149 | } | |
3150 | ||
3151 | if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) { | |
df2c07f4 | 3152 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_SET_DL_DST, |
b3e9b2ed EJ |
3153 | flow->dl_dst, ETH_ADDR_LEN); |
3154 | memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN); | |
3155 | } | |
3156 | ||
3157 | if (ctx->base_priority != ctx->priority) { | |
3158 | if (ctx->priority) { | |
df2c07f4 | 3159 | nl_msg_put_u32(odp_actions, OVS_ACTION_ATTR_SET_PRIORITY, |
b3e9b2ed EJ |
3160 | ctx->priority); |
3161 | } else { | |
df2c07f4 | 3162 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_PRIORITY); |
b3e9b2ed EJ |
3163 | } |
3164 | ctx->base_priority = ctx->priority; | |
3165 | } | |
3166 | } | |
3167 | ||
6ff686f2 PS |
3168 | static void |
3169 | compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port) | |
3170 | { | |
3171 | nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port); | |
3172 | ctx->sflow_odp_port = odp_port; | |
3173 | ctx->sflow_n_outputs++; | |
3174 | } | |
3175 | ||
abe529af BP |
3176 | static void |
3177 | add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port) | |
3178 | { | |
3179 | const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port); | |
3180 | uint16_t odp_port = ofp_port_to_odp_port(ofp_port); | |
3181 | ||
3182 | if (ofport) { | |
3183 | if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) { | |
3184 | /* Forwarding disabled on port. */ | |
3185 | return; | |
3186 | } | |
3187 | } else { | |
3188 | /* | |
3189 | * We don't have an ofport record for this port, but it doesn't hurt to | |
3190 | * allow forwarding to it anyhow. Maybe such a port will appear later | |
3191 | * and we're pre-populating the flow table. | |
3192 | */ | |
3193 | } | |
3194 | ||
b3e9b2ed | 3195 | commit_odp_actions(ctx); |
6ff686f2 | 3196 | compose_output_action(ctx, odp_port); |
abe529af BP |
3197 | ctx->nf_output_iface = ofp_port; |
3198 | } | |
3199 | ||
3200 | static void | |
29901626 BP |
3201 | xlate_table_action(struct action_xlate_ctx *ctx, |
3202 | uint16_t in_port, uint8_t table_id) | |
abe529af BP |
3203 | { |
3204 | if (ctx->recurse < MAX_RESUBMIT_RECURSION) { | |
54a9cbc9 | 3205 | struct ofproto_dpif *ofproto = ctx->ofproto; |
abe529af BP |
3206 | struct rule_dpif *rule; |
3207 | uint16_t old_in_port; | |
29901626 BP |
3208 | uint8_t old_table_id; |
3209 | ||
3210 | old_table_id = ctx->table_id; | |
3211 | ctx->table_id = table_id; | |
abe529af | 3212 | |
54a9cbc9 | 3213 | /* Look up a flow with 'in_port' as the input port. */ |
abe529af BP |
3214 | old_in_port = ctx->flow.in_port; |
3215 | ctx->flow.in_port = in_port; | |
54a9cbc9 BP |
3216 | rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id); |
3217 | ||
3218 | /* Tag the flow. */ | |
3219 | if (table_id > 0 && table_id < N_TABLES) { | |
3220 | struct table_dpif *table = &ofproto->tables[table_id]; | |
3221 | if (table->other_table) { | |
3222 | ctx->tags |= (rule | |
3223 | ? rule->tag | |
3224 | : rule_calculate_tag(&ctx->flow, | |
3225 | &table->other_table->wc, | |
3226 | table->basis)); | |
3227 | } | |
3228 | } | |
3229 | ||
3230 | /* Restore the original input port. Otherwise OFPP_NORMAL and | |
3231 | * OFPP_IN_PORT will have surprising behavior. */ | |
abe529af BP |
3232 | ctx->flow.in_port = old_in_port; |
3233 | ||
3234 | if (ctx->resubmit_hook) { | |
3235 | ctx->resubmit_hook(ctx, rule); | |
3236 | } | |
3237 | ||
3238 | if (rule) { | |
3239 | ctx->recurse++; | |
3240 | do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx); | |
3241 | ctx->recurse--; | |
3242 | } | |
29901626 BP |
3243 | |
3244 | ctx->table_id = old_table_id; | |
abe529af BP |
3245 | } else { |
3246 | static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1); | |
3247 | ||
29901626 | 3248 | VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times", |
abe529af BP |
3249 | MAX_RESUBMIT_RECURSION); |
3250 | } | |
3251 | } | |
3252 | ||
29901626 BP |
3253 | static void |
3254 | xlate_resubmit_table(struct action_xlate_ctx *ctx, | |
3255 | const struct nx_action_resubmit *nar) | |
3256 | { | |
3257 | uint16_t in_port; | |
3258 | uint8_t table_id; | |
3259 | ||
3260 | in_port = (nar->in_port == htons(OFPP_IN_PORT) | |
3261 | ? ctx->flow.in_port | |
3262 | : ntohs(nar->in_port)); | |
3263 | table_id = nar->table == 255 ? ctx->table_id : nar->table; | |
3264 | ||
3265 | xlate_table_action(ctx, in_port, table_id); | |
3266 | } | |
3267 | ||
abe529af | 3268 | static void |
b3e9b2ed | 3269 | flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask) |
abe529af BP |
3270 | { |
3271 | struct ofport_dpif *ofport; | |
3272 | ||
b3e9b2ed EJ |
3273 | commit_odp_actions(ctx); |
3274 | HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) { | |
abe529af | 3275 | uint16_t ofp_port = ofport->up.ofp_port; |
b3e9b2ed | 3276 | if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) { |
6ff686f2 | 3277 | compose_output_action(ctx, ofport->odp_port); |
abe529af BP |
3278 | } |
3279 | } | |
b3e9b2ed EJ |
3280 | |
3281 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
abe529af BP |
3282 | } |
3283 | ||
6ff686f2 | 3284 | static void |
98403001 | 3285 | compose_controller_action(struct action_xlate_ctx *ctx, int len) |
6ff686f2 PS |
3286 | { |
3287 | struct user_action_cookie cookie; | |
3288 | ||
3289 | cookie.type = USER_ACTION_COOKIE_CONTROLLER; | |
3290 | cookie.data = len; | |
3291 | cookie.n_output = 0; | |
3292 | cookie.vlan_tci = 0; | |
98403001 | 3293 | put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie); |
6ff686f2 PS |
3294 | } |
3295 | ||
abe529af BP |
3296 | static void |
3297 | xlate_output_action__(struct action_xlate_ctx *ctx, | |
3298 | uint16_t port, uint16_t max_len) | |
3299 | { | |
3300 | uint16_t prev_nf_output_iface = ctx->nf_output_iface; | |
3301 | ||
3302 | ctx->nf_output_iface = NF_OUT_DROP; | |
3303 | ||
3304 | switch (port) { | |
3305 | case OFPP_IN_PORT: | |
3306 | add_output_action(ctx, ctx->flow.in_port); | |
3307 | break; | |
3308 | case OFPP_TABLE: | |
29901626 | 3309 | xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id); |
abe529af BP |
3310 | break; |
3311 | case OFPP_NORMAL: | |
3312 | xlate_normal(ctx); | |
3313 | break; | |
3314 | case OFPP_FLOOD: | |
b3e9b2ed | 3315 | flood_packets(ctx, htonl(OFPPC_NO_FLOOD)); |
abe529af BP |
3316 | break; |
3317 | case OFPP_ALL: | |
b3e9b2ed | 3318 | flood_packets(ctx, htonl(0)); |
abe529af BP |
3319 | break; |
3320 | case OFPP_CONTROLLER: | |
b3e9b2ed | 3321 | commit_odp_actions(ctx); |
98403001 | 3322 | compose_controller_action(ctx, max_len); |
abe529af BP |
3323 | break; |
3324 | case OFPP_LOCAL: | |
3325 | add_output_action(ctx, OFPP_LOCAL); | |
3326 | break; | |
e81d2933 EJ |
3327 | case OFPP_NONE: |
3328 | break; | |
abe529af BP |
3329 | default: |
3330 | if (port != ctx->flow.in_port) { | |
3331 | add_output_action(ctx, port); | |
3332 | } | |
3333 | break; | |
3334 | } | |
3335 | ||
3336 | if (prev_nf_output_iface == NF_OUT_FLOOD) { | |
3337 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
3338 | } else if (ctx->nf_output_iface == NF_OUT_DROP) { | |
3339 | ctx->nf_output_iface = prev_nf_output_iface; | |
3340 | } else if (prev_nf_output_iface != NF_OUT_DROP && | |
3341 | ctx->nf_output_iface != NF_OUT_FLOOD) { | |
3342 | ctx->nf_output_iface = NF_OUT_MULTI; | |
3343 | } | |
3344 | } | |
3345 | ||
f694937d EJ |
3346 | static void |
3347 | xlate_output_reg_action(struct action_xlate_ctx *ctx, | |
3348 | const struct nx_action_output_reg *naor) | |
3349 | { | |
3350 | uint64_t ofp_port; | |
3351 | ||
3352 | ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow); | |
3353 | ||
3354 | if (ofp_port <= UINT16_MAX) { | |
3355 | xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len)); | |
3356 | } | |
3357 | } | |
3358 | ||
abe529af BP |
3359 | static void |
3360 | xlate_output_action(struct action_xlate_ctx *ctx, | |
3361 | const struct ofp_action_output *oao) | |
3362 | { | |
3363 | xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len)); | |
3364 | } | |
3365 | ||
abe529af BP |
3366 | static void |
3367 | xlate_enqueue_action(struct action_xlate_ctx *ctx, | |
3368 | const struct ofp_action_enqueue *oae) | |
3369 | { | |
3370 | uint16_t ofp_port, odp_port; | |
b3e9b2ed | 3371 | uint32_t ctx_priority, priority; |
abe529af BP |
3372 | int error; |
3373 | ||
3374 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id), | |
3375 | &priority); | |
3376 | if (error) { | |
3377 | /* Fall back to ordinary output action. */ | |
3378 | xlate_output_action__(ctx, ntohs(oae->port), 0); | |
3379 | return; | |
3380 | } | |
3381 | ||
df2c07f4 | 3382 | /* Figure out datapath output port. */ |
abe529af BP |
3383 | ofp_port = ntohs(oae->port); |
3384 | if (ofp_port == OFPP_IN_PORT) { | |
3385 | ofp_port = ctx->flow.in_port; | |
3386 | } | |
3387 | odp_port = ofp_port_to_odp_port(ofp_port); | |
3388 | ||
df2c07f4 | 3389 | /* Add datapath actions. */ |
b3e9b2ed EJ |
3390 | ctx_priority = ctx->priority; |
3391 | ctx->priority = priority; | |
abe529af | 3392 | add_output_action(ctx, odp_port); |
b3e9b2ed | 3393 | ctx->priority = ctx_priority; |
abe529af BP |
3394 | |
3395 | /* Update NetFlow output port. */ | |
3396 | if (ctx->nf_output_iface == NF_OUT_DROP) { | |
3397 | ctx->nf_output_iface = odp_port; | |
3398 | } else if (ctx->nf_output_iface != NF_OUT_FLOOD) { | |
3399 | ctx->nf_output_iface = NF_OUT_MULTI; | |
3400 | } | |
3401 | } | |
3402 | ||
3403 | static void | |
3404 | xlate_set_queue_action(struct action_xlate_ctx *ctx, | |
3405 | const struct nx_action_set_queue *nasq) | |
3406 | { | |
3407 | uint32_t priority; | |
3408 | int error; | |
3409 | ||
3410 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id), | |
3411 | &priority); | |
3412 | if (error) { | |
3413 | /* Couldn't translate queue to a priority, so ignore. A warning | |
3414 | * has already been logged. */ | |
3415 | return; | |
3416 | } | |
3417 | ||
afabef2b | 3418 | ctx->priority = priority; |
abe529af BP |
3419 | } |
3420 | ||
3421 | struct xlate_reg_state { | |
3422 | ovs_be16 vlan_tci; | |
3423 | ovs_be64 tun_id; | |
3424 | }; | |
3425 | ||
abe529af BP |
3426 | static void |
3427 | xlate_autopath(struct action_xlate_ctx *ctx, | |
3428 | const struct nx_action_autopath *naa) | |
3429 | { | |
3430 | uint16_t ofp_port = ntohl(naa->id); | |
3431 | struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port); | |
3432 | ||
3433 | if (!port || !port->bundle) { | |
3434 | ofp_port = OFPP_NONE; | |
3435 | } else if (port->bundle->bond) { | |
3436 | /* Autopath does not support VLAN hashing. */ | |
3437 | struct ofport_dpif *slave = bond_choose_output_slave( | |
dc155bff | 3438 | port->bundle->bond, &ctx->flow, 0, &ctx->tags); |
abe529af BP |
3439 | if (slave) { |
3440 | ofp_port = slave->up.ofp_port; | |
3441 | } | |
3442 | } | |
3443 | autopath_execute(naa, &ctx->flow, ofp_port); | |
3444 | } | |
3445 | ||
daff3353 EJ |
3446 | static bool |
3447 | slave_enabled_cb(uint16_t ofp_port, void *ofproto_) | |
3448 | { | |
3449 | struct ofproto_dpif *ofproto = ofproto_; | |
3450 | struct ofport_dpif *port; | |
3451 | ||
3452 | switch (ofp_port) { | |
3453 | case OFPP_IN_PORT: | |
3454 | case OFPP_TABLE: | |
3455 | case OFPP_NORMAL: | |
3456 | case OFPP_FLOOD: | |
3457 | case OFPP_ALL: | |
3458 | case OFPP_LOCAL: | |
3459 | return true; | |
3460 | case OFPP_CONTROLLER: /* Not supported by the bundle action. */ | |
3461 | return false; | |
3462 | default: | |
3463 | port = get_ofp_port(ofproto, ofp_port); | |
3464 | return port ? port->may_enable : false; | |
3465 | } | |
3466 | } | |
3467 | ||
75a75043 BP |
3468 | static void |
3469 | xlate_learn_action(struct action_xlate_ctx *ctx, | |
3470 | const struct nx_action_learn *learn) | |
3471 | { | |
3472 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1); | |
3473 | struct ofputil_flow_mod fm; | |
3474 | int error; | |
3475 | ||
3476 | learn_execute(learn, &ctx->flow, &fm); | |
3477 | ||
3478 | error = ofproto_flow_mod(&ctx->ofproto->up, &fm); | |
3479 | if (error && !VLOG_DROP_WARN(&rl)) { | |
3480 | char *msg = ofputil_error_to_string(error); | |
3481 | VLOG_WARN("learning action failed to modify flow table (%s)", msg); | |
3482 | free(msg); | |
3483 | } | |
3484 | ||
3485 | free(fm.actions); | |
3486 | } | |
3487 | ||
abe529af BP |
3488 | static void |
3489 | do_xlate_actions(const union ofp_action *in, size_t n_in, | |
3490 | struct action_xlate_ctx *ctx) | |
3491 | { | |
3492 | const struct ofport_dpif *port; | |
abe529af | 3493 | const union ofp_action *ia; |
b4b8c781 | 3494 | size_t left; |
abe529af BP |
3495 | |
3496 | port = get_ofp_port(ctx->ofproto, ctx->flow.in_port); | |
3497 | if (port | |
3498 | && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) && | |
3499 | port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp) | |
3500 | ? htonl(OFPPC_NO_RECV_STP) | |
3501 | : htonl(OFPPC_NO_RECV))) { | |
3502 | /* Drop this flow. */ | |
3503 | return; | |
3504 | } | |
3505 | ||
b4b8c781 | 3506 | OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) { |
abe529af | 3507 | const struct ofp_action_dl_addr *oada; |
38f2e360 BP |
3508 | const struct nx_action_resubmit *nar; |
3509 | const struct nx_action_set_tunnel *nast; | |
3510 | const struct nx_action_set_queue *nasq; | |
3511 | const struct nx_action_multipath *nam; | |
3512 | const struct nx_action_autopath *naa; | |
daff3353 | 3513 | const struct nx_action_bundle *nab; |
f694937d | 3514 | const struct nx_action_output_reg *naor; |
38f2e360 BP |
3515 | enum ofputil_action_code code; |
3516 | ovs_be64 tun_id; | |
3517 | ||
3518 | code = ofputil_decode_action_unsafe(ia); | |
3519 | switch (code) { | |
3520 | case OFPUTIL_OFPAT_OUTPUT: | |
abe529af BP |
3521 | xlate_output_action(ctx, &ia->output); |
3522 | break; | |
3523 | ||
38f2e360 | 3524 | case OFPUTIL_OFPAT_SET_VLAN_VID: |
abe529af BP |
3525 | ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK); |
3526 | ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI); | |
abe529af BP |
3527 | break; |
3528 | ||
38f2e360 | 3529 | case OFPUTIL_OFPAT_SET_VLAN_PCP: |
abe529af BP |
3530 | ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); |
3531 | ctx->flow.vlan_tci |= htons( | |
3532 | (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI); | |
abe529af BP |
3533 | break; |
3534 | ||
38f2e360 | 3535 | case OFPUTIL_OFPAT_STRIP_VLAN: |
abe529af | 3536 | ctx->flow.vlan_tci = htons(0); |
abe529af BP |
3537 | break; |
3538 | ||
38f2e360 | 3539 | case OFPUTIL_OFPAT_SET_DL_SRC: |
abe529af | 3540 | oada = ((struct ofp_action_dl_addr *) ia); |
abe529af BP |
3541 | memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN); |
3542 | break; | |
3543 | ||
38f2e360 | 3544 | case OFPUTIL_OFPAT_SET_DL_DST: |
abe529af | 3545 | oada = ((struct ofp_action_dl_addr *) ia); |
abe529af BP |
3546 | memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN); |
3547 | break; | |
3548 | ||
38f2e360 | 3549 | case OFPUTIL_OFPAT_SET_NW_SRC: |
abe529af BP |
3550 | ctx->flow.nw_src = ia->nw_addr.nw_addr; |
3551 | break; | |
3552 | ||
38f2e360 | 3553 | case OFPUTIL_OFPAT_SET_NW_DST: |
abe529af BP |
3554 | ctx->flow.nw_dst = ia->nw_addr.nw_addr; |
3555 | break; | |
3556 | ||
38f2e360 | 3557 | case OFPUTIL_OFPAT_SET_NW_TOS: |
1fa96cf4 | 3558 | ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK; |
abe529af BP |
3559 | break; |
3560 | ||
38f2e360 | 3561 | case OFPUTIL_OFPAT_SET_TP_SRC: |
abe529af BP |
3562 | ctx->flow.tp_src = ia->tp_port.tp_port; |
3563 | break; | |
3564 | ||
38f2e360 | 3565 | case OFPUTIL_OFPAT_SET_TP_DST: |
abe529af BP |
3566 | ctx->flow.tp_dst = ia->tp_port.tp_port; |
3567 | break; | |
3568 | ||
38f2e360 BP |
3569 | case OFPUTIL_OFPAT_ENQUEUE: |
3570 | xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia); | |
3571 | break; | |
3572 | ||
3573 | case OFPUTIL_NXAST_RESUBMIT: | |
3574 | nar = (const struct nx_action_resubmit *) ia; | |
29901626 BP |
3575 | xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id); |
3576 | break; | |
3577 | ||
3578 | case OFPUTIL_NXAST_RESUBMIT_TABLE: | |
3579 | xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia); | |
abe529af BP |
3580 | break; |
3581 | ||
38f2e360 BP |
3582 | case OFPUTIL_NXAST_SET_TUNNEL: |
3583 | nast = (const struct nx_action_set_tunnel *) ia; | |
3584 | tun_id = htonll(ntohl(nast->tun_id)); | |
3585 | ctx->flow.tun_id = tun_id; | |
3586 | break; | |
3587 | ||
3588 | case OFPUTIL_NXAST_SET_QUEUE: | |
3589 | nasq = (const struct nx_action_set_queue *) ia; | |
3590 | xlate_set_queue_action(ctx, nasq); | |
3591 | break; | |
3592 | ||
3593 | case OFPUTIL_NXAST_POP_QUEUE: | |
3594 | ctx->priority = 0; | |
3595 | break; | |
3596 | ||
3597 | case OFPUTIL_NXAST_REG_MOVE: | |
3598 | nxm_execute_reg_move((const struct nx_action_reg_move *) ia, | |
3599 | &ctx->flow); | |
3600 | break; | |
3601 | ||
3602 | case OFPUTIL_NXAST_REG_LOAD: | |
3603 | nxm_execute_reg_load((const struct nx_action_reg_load *) ia, | |
3604 | &ctx->flow); | |
3605 | break; | |
3606 | ||
3607 | case OFPUTIL_NXAST_NOTE: | |
3608 | /* Nothing to do. */ | |
3609 | break; | |
3610 | ||
3611 | case OFPUTIL_NXAST_SET_TUNNEL64: | |
3612 | tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id; | |
3613 | ctx->flow.tun_id = tun_id; | |
3614 | break; | |
3615 | ||
3616 | case OFPUTIL_NXAST_MULTIPATH: | |
3617 | nam = (const struct nx_action_multipath *) ia; | |
3618 | multipath_execute(nam, &ctx->flow); | |
abe529af BP |
3619 | break; |
3620 | ||
38f2e360 BP |
3621 | case OFPUTIL_NXAST_AUTOPATH: |
3622 | naa = (const struct nx_action_autopath *) ia; | |
3623 | xlate_autopath(ctx, naa); | |
abe529af | 3624 | break; |
daff3353 EJ |
3625 | |
3626 | case OFPUTIL_NXAST_BUNDLE: | |
3627 | ctx->ofproto->has_bundle_action = true; | |
3628 | nab = (const struct nx_action_bundle *) ia; | |
3629 | xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow, | |
3630 | slave_enabled_cb, | |
3631 | ctx->ofproto), 0); | |
3632 | break; | |
a368bb53 EJ |
3633 | |
3634 | case OFPUTIL_NXAST_BUNDLE_LOAD: | |
3635 | ctx->ofproto->has_bundle_action = true; | |
3636 | nab = (const struct nx_action_bundle *) ia; | |
3637 | bundle_execute_load(nab, &ctx->flow, slave_enabled_cb, | |
3638 | ctx->ofproto); | |
3639 | break; | |
f694937d EJ |
3640 | |
3641 | case OFPUTIL_NXAST_OUTPUT_REG: | |
3642 | naor = (const struct nx_action_output_reg *) ia; | |
3643 | xlate_output_reg_action(ctx, naor); | |
3644 | break; | |
75a75043 BP |
3645 | |
3646 | case OFPUTIL_NXAST_LEARN: | |
3647 | ctx->has_learn = true; | |
3648 | if (ctx->may_learn) { | |
3649 | xlate_learn_action(ctx, (const struct nx_action_learn *) ia); | |
3650 | } | |
3651 | break; | |
abe529af BP |
3652 | } |
3653 | } | |
3654 | } | |
3655 | ||
3656 | static void | |
3657 | action_xlate_ctx_init(struct action_xlate_ctx *ctx, | |
3658 | struct ofproto_dpif *ofproto, const struct flow *flow, | |
3659 | const struct ofpbuf *packet) | |
3660 | { | |
3661 | ctx->ofproto = ofproto; | |
3662 | ctx->flow = *flow; | |
3663 | ctx->packet = packet; | |
75a75043 | 3664 | ctx->may_learn = packet != NULL; |
abe529af | 3665 | ctx->resubmit_hook = NULL; |
abe529af BP |
3666 | } |
3667 | ||
3668 | static struct ofpbuf * | |
3669 | xlate_actions(struct action_xlate_ctx *ctx, | |
3670 | const union ofp_action *in, size_t n_in) | |
3671 | { | |
3672 | COVERAGE_INC(ofproto_dpif_xlate); | |
3673 | ||
3674 | ctx->odp_actions = ofpbuf_new(512); | |
3675 | ctx->tags = 0; | |
3676 | ctx->may_set_up_flow = true; | |
75a75043 BP |
3677 | ctx->has_learn = false; |
3678 | ctx->has_normal = false; | |
abe529af BP |
3679 | ctx->nf_output_iface = NF_OUT_DROP; |
3680 | ctx->recurse = 0; | |
afabef2b | 3681 | ctx->priority = 0; |
b3e9b2ed EJ |
3682 | ctx->base_priority = 0; |
3683 | ctx->base_flow = ctx->flow; | |
bf1e0371 | 3684 | ctx->base_flow.tun_id = 0; |
29901626 | 3685 | ctx->table_id = 0; |
abe529af | 3686 | |
fc08b7a2 | 3687 | if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) { |
abe529af BP |
3688 | ctx->may_set_up_flow = false; |
3689 | } else { | |
6ff686f2 | 3690 | add_sflow_action(ctx); |
abe529af | 3691 | do_xlate_actions(in, n_in, ctx); |
6ff686f2 | 3692 | fix_sflow_action(ctx); |
abe529af BP |
3693 | } |
3694 | ||
abe529af BP |
3695 | /* Check with in-band control to see if we're allowed to set up this |
3696 | * flow. */ | |
3697 | if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow, | |
3698 | ctx->odp_actions->data, | |
3699 | ctx->odp_actions->size)) { | |
3700 | ctx->may_set_up_flow = false; | |
3701 | } | |
3702 | ||
3703 | return ctx->odp_actions; | |
3704 | } | |
3705 | \f | |
3706 | /* OFPP_NORMAL implementation. */ | |
3707 | ||
3708 | struct dst { | |
3709 | struct ofport_dpif *port; | |
dc155bff | 3710 | uint16_t vid; |
abe529af BP |
3711 | }; |
3712 | ||
3713 | struct dst_set { | |
3714 | struct dst builtin[32]; | |
3715 | struct dst *dsts; | |
3716 | size_t n, allocated; | |
3717 | }; | |
3718 | ||
3719 | static void dst_set_init(struct dst_set *); | |
3720 | static void dst_set_add(struct dst_set *, const struct dst *); | |
3721 | static void dst_set_free(struct dst_set *); | |
3722 | ||
3723 | static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *); | |
3724 | ||
ecac4ebf BP |
3725 | /* Given 'vid', the VID obtained from the 802.1Q header that was received as |
3726 | * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle', | |
3727 | * the bundle on which the packet was received, returns the VLAN to which the | |
3728 | * packet belongs. | |
3729 | * | |
3730 | * Both 'vid' and the return value are in the range 0...4095. */ | |
3731 | static uint16_t | |
3732 | input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid) | |
3733 | { | |
3734 | switch (in_bundle->vlan_mode) { | |
3735 | case PORT_VLAN_ACCESS: | |
3736 | return in_bundle->vlan; | |
3737 | break; | |
3738 | ||
3739 | case PORT_VLAN_TRUNK: | |
3740 | return vid; | |
3741 | ||
3742 | case PORT_VLAN_NATIVE_UNTAGGED: | |
3743 | case PORT_VLAN_NATIVE_TAGGED: | |
3744 | return vid ? vid : in_bundle->vlan; | |
3745 | ||
3746 | default: | |
3747 | NOT_REACHED(); | |
3748 | } | |
3749 | } | |
3750 | ||
3751 | /* Given 'vlan', the VLAN that a packet belongs to, and | |
3752 | * 'out_bundle', a bundle on which the packet is to be output, returns the VID | |
3753 | * that should be included in the 802.1Q header. (If the return value is 0, | |
3754 | * then the 802.1Q header should only be included in the packet if there is a | |
3755 | * nonzero PCP.) | |
3756 | * | |
3757 | * Both 'vlan' and the return value are in the range 0...4095. */ | |
3758 | static uint16_t | |
3759 | output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan) | |
3760 | { | |
3761 | switch (out_bundle->vlan_mode) { | |
3762 | case PORT_VLAN_ACCESS: | |
3763 | return 0; | |
3764 | ||
3765 | case PORT_VLAN_TRUNK: | |
3766 | case PORT_VLAN_NATIVE_TAGGED: | |
3767 | return vlan; | |
3768 | ||
3769 | case PORT_VLAN_NATIVE_UNTAGGED: | |
3770 | return vlan == out_bundle->vlan ? 0 : vlan; | |
3771 | ||
3772 | default: | |
3773 | NOT_REACHED(); | |
3774 | } | |
3775 | } | |
3776 | ||
abe529af BP |
3777 | static bool |
3778 | set_dst(struct action_xlate_ctx *ctx, struct dst *dst, | |
3779 | const struct ofbundle *in_bundle, const struct ofbundle *out_bundle) | |
3780 | { | |
ecac4ebf BP |
3781 | uint16_t vlan; |
3782 | ||
3783 | vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci)); | |
3784 | dst->vid = output_vlan_to_vid(out_bundle, vlan); | |
abe529af BP |
3785 | |
3786 | dst->port = (!out_bundle->bond | |
3787 | ? ofbundle_get_a_port(out_bundle) | |
3788 | : bond_choose_output_slave(out_bundle->bond, &ctx->flow, | |
dc155bff | 3789 | dst->vid, &ctx->tags)); |
abe529af BP |
3790 | return dst->port != NULL; |
3791 | } | |
3792 | ||
3793 | static int | |
3794 | mirror_mask_ffs(mirror_mask_t mask) | |
3795 | { | |
3796 | BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask)); | |
3797 | return ffs(mask); | |
3798 | } | |
3799 | ||
3800 | static void | |
3801 | dst_set_init(struct dst_set *set) | |
3802 | { | |
3803 | set->dsts = set->builtin; | |
3804 | set->n = 0; | |
3805 | set->allocated = ARRAY_SIZE(set->builtin); | |
3806 | } | |
3807 | ||
3808 | static void | |
3809 | dst_set_add(struct dst_set *set, const struct dst *dst) | |
3810 | { | |
3811 | if (set->n >= set->allocated) { | |
3812 | size_t new_allocated; | |
3813 | struct dst *new_dsts; | |
3814 | ||
3815 | new_allocated = set->allocated * 2; | |
3816 | new_dsts = xmalloc(new_allocated * sizeof *new_dsts); | |
3817 | memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts); | |
3818 | ||
3819 | dst_set_free(set); | |
3820 | ||
3821 | set->dsts = new_dsts; | |
3822 | set->allocated = new_allocated; | |
3823 | } | |
3824 | set->dsts[set->n++] = *dst; | |
3825 | } | |
3826 | ||
3827 | static void | |
3828 | dst_set_free(struct dst_set *set) | |
3829 | { | |
3830 | if (set->dsts != set->builtin) { | |
3831 | free(set->dsts); | |
3832 | } | |
3833 | } | |
3834 | ||
3835 | static bool | |
3836 | dst_is_duplicate(const struct dst_set *set, const struct dst *test) | |
3837 | { | |
3838 | size_t i; | |
3839 | for (i = 0; i < set->n; i++) { | |
dc155bff | 3840 | if (set->dsts[i].vid == test->vid |
abe529af BP |
3841 | && set->dsts[i].port == test->port) { |
3842 | return true; | |
3843 | } | |
3844 | } | |
3845 | return false; | |
3846 | } | |
3847 | ||
3848 | static bool | |
3849 | ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan) | |
3850 | { | |
ecac4ebf | 3851 | return (bundle->vlan_mode != PORT_VLAN_ACCESS |
fc3d7408 | 3852 | && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan))); |
abe529af BP |
3853 | } |
3854 | ||
3855 | static bool | |
3856 | ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan) | |
3857 | { | |
3858 | return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan); | |
3859 | } | |
3860 | ||
3861 | /* Returns an arbitrary interface within 'bundle'. */ | |
3862 | static struct ofport_dpif * | |
3863 | ofbundle_get_a_port(const struct ofbundle *bundle) | |
3864 | { | |
3865 | return CONTAINER_OF(list_front(&bundle->ports), | |
3866 | struct ofport_dpif, bundle_node); | |
3867 | } | |
3868 | ||
3869 | static void | |
3870 | compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan, | |
3871 | const struct ofbundle *in_bundle, | |
3872 | const struct ofbundle *out_bundle, struct dst_set *set) | |
3873 | { | |
3874 | struct dst dst; | |
3875 | ||
3876 | if (out_bundle == OFBUNDLE_FLOOD) { | |
3877 | struct ofbundle *bundle; | |
3878 | ||
3879 | HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) { | |
3880 | if (bundle != in_bundle | |
3881 | && ofbundle_includes_vlan(bundle, vlan) | |
3882 | && bundle->floodable | |
3883 | && !bundle->mirror_out | |
3884 | && set_dst(ctx, &dst, in_bundle, bundle)) { | |
3885 | dst_set_add(set, &dst); | |
3886 | } | |
3887 | } | |
3888 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
3889 | } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) { | |
3890 | dst_set_add(set, &dst); | |
3891 | ctx->nf_output_iface = dst.port->odp_port; | |
3892 | } | |
3893 | } | |
3894 | ||
3895 | static bool | |
3896 | vlan_is_mirrored(const struct ofmirror *m, int vlan) | |
3897 | { | |
fc3d7408 | 3898 | return !m->vlans || bitmap_is_set(m->vlans, vlan); |
abe529af BP |
3899 | } |
3900 | ||
07817dfe BP |
3901 | /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored |
3902 | * to a VLAN. In general most packets may be mirrored but we want to drop | |
3903 | * protocols that may confuse switches. */ | |
3904 | static bool | |
3905 | eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN]) | |
3906 | { | |
3907 | /* If you change this function's behavior, please update corresponding | |
3908 | * documentation in vswitch.xml at the same time. */ | |
3909 | if (dst[0] != 0x01) { | |
3910 | /* All the currently banned MACs happen to start with 01 currently, so | |
3911 | * this is a quick way to eliminate most of the good ones. */ | |
3912 | } else { | |
3913 | if (eth_addr_is_reserved(dst)) { | |
3914 | /* Drop STP, IEEE pause frames, and other reserved protocols | |
3915 | * (01-80-c2-00-00-0x). */ | |
3916 | return false; | |
3917 | } | |
3918 | ||
3919 | if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) { | |
3920 | /* Cisco OUI. */ | |
3921 | if ((dst[3] & 0xfe) == 0xcc && | |
3922 | (dst[4] & 0xfe) == 0xcc && | |
3923 | (dst[5] & 0xfe) == 0xcc) { | |
3924 | /* Drop the following protocols plus others following the same | |
3925 | pattern: | |
3926 | ||
3927 | CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc) | |
3928 | Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd) | |
3929 | STP Uplink Fast (01-00-0c-cd-cd-cd) */ | |
3930 | return false; | |
3931 | } | |
3932 | ||
3933 | if (!(dst[3] | dst[4] | dst[5])) { | |
3934 | /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */ | |
3935 | return false; | |
3936 | } | |
3937 | } | |
3938 | } | |
3939 | return true; | |
3940 | } | |
3941 | ||
abe529af BP |
3942 | static void |
3943 | compose_mirror_dsts(struct action_xlate_ctx *ctx, | |
3944 | uint16_t vlan, const struct ofbundle *in_bundle, | |
3945 | struct dst_set *set) | |
3946 | { | |
3947 | struct ofproto_dpif *ofproto = ctx->ofproto; | |
3948 | mirror_mask_t mirrors; | |
dc155bff | 3949 | uint16_t flow_vid; |
abe529af BP |
3950 | size_t i; |
3951 | ||
3952 | mirrors = in_bundle->src_mirrors; | |
3953 | for (i = 0; i < set->n; i++) { | |
3954 | mirrors |= set->dsts[i].port->bundle->dst_mirrors; | |
3955 | } | |
3956 | ||
3957 | if (!mirrors) { | |
3958 | return; | |
3959 | } | |
3960 | ||
dc155bff | 3961 | flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci); |
abe529af BP |
3962 | while (mirrors) { |
3963 | struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1]; | |
3964 | if (vlan_is_mirrored(m, vlan)) { | |
3965 | struct dst dst; | |
3966 | ||
3967 | if (m->out) { | |
3968 | if (set_dst(ctx, &dst, in_bundle, m->out) | |
3969 | && !dst_is_duplicate(set, &dst)) { | |
3970 | dst_set_add(set, &dst); | |
3971 | } | |
07817dfe | 3972 | } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) { |
abe529af BP |
3973 | struct ofbundle *bundle; |
3974 | ||
3975 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
3976 | if (ofbundle_includes_vlan(bundle, m->out_vlan) | |
3977 | && set_dst(ctx, &dst, in_bundle, bundle)) | |
3978 | { | |
ecac4ebf BP |
3979 | /* set_dst() got dst->vid from the input packet's VLAN, |
3980 | * not from m->out_vlan, so recompute it. */ | |
3981 | dst.vid = output_vlan_to_vid(bundle, m->out_vlan); | |
3982 | ||
abe529af BP |
3983 | if (dst_is_duplicate(set, &dst)) { |
3984 | continue; | |
3985 | } | |
3986 | ||
dc155bff | 3987 | if (bundle == in_bundle && dst.vid == flow_vid) { |
abe529af BP |
3988 | /* Don't send out input port on same VLAN. */ |
3989 | continue; | |
3990 | } | |
3991 | dst_set_add(set, &dst); | |
3992 | } | |
3993 | } | |
3994 | } | |
3995 | } | |
3996 | mirrors &= mirrors - 1; | |
3997 | } | |
3998 | } | |
3999 | ||
4000 | static void | |
4001 | compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan, | |
4002 | const struct ofbundle *in_bundle, | |
4003 | const struct ofbundle *out_bundle) | |
4004 | { | |
dc155bff | 4005 | uint16_t initial_vid, cur_vid; |
abe529af BP |
4006 | const struct dst *dst; |
4007 | struct dst_set set; | |
4008 | ||
4009 | dst_set_init(&set); | |
4010 | compose_dsts(ctx, vlan, in_bundle, out_bundle, &set); | |
4011 | compose_mirror_dsts(ctx, vlan, in_bundle, &set); | |
823518f1 BP |
4012 | if (!set.n) { |
4013 | dst_set_free(&set); | |
4014 | return; | |
4015 | } | |
abe529af BP |
4016 | |
4017 | /* Output all the packets we can without having to change the VLAN. */ | |
823518f1 | 4018 | commit_odp_actions(ctx); |
dc155bff | 4019 | initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci); |
abe529af | 4020 | for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) { |
dc155bff | 4021 | if (dst->vid != initial_vid) { |
abe529af BP |
4022 | continue; |
4023 | } | |
6ff686f2 | 4024 | compose_output_action(ctx, dst->port->odp_port); |
abe529af BP |
4025 | } |
4026 | ||
4027 | /* Then output the rest. */ | |
dc155bff | 4028 | cur_vid = initial_vid; |
abe529af | 4029 | for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) { |
dc155bff | 4030 | if (dst->vid == initial_vid) { |
abe529af BP |
4031 | continue; |
4032 | } | |
dc155bff | 4033 | if (dst->vid != cur_vid) { |
823518f1 | 4034 | ovs_be16 tci; |
d9065a90 | 4035 | |
dc155bff | 4036 | tci = htons(dst->vid); |
823518f1 BP |
4037 | tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK); |
4038 | if (tci) { | |
4039 | tci |= htons(VLAN_CFI); | |
abe529af | 4040 | } |
823518f1 BP |
4041 | commit_vlan_tci(ctx, tci); |
4042 | ||
dc155bff | 4043 | cur_vid = dst->vid; |
abe529af | 4044 | } |
6ff686f2 | 4045 | compose_output_action(ctx, dst->port->odp_port); |
abe529af BP |
4046 | } |
4047 | ||
4048 | dst_set_free(&set); | |
4049 | } | |
4050 | ||
4051 | /* Returns the effective vlan of a packet, taking into account both the | |
4052 | * 802.1Q header and implicitly tagged ports. A value of 0 indicates that | |
4053 | * the packet is untagged and -1 indicates it has an invalid header and | |
4054 | * should be dropped. */ | |
4055 | static int | |
4056 | flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow, | |
4057 | struct ofbundle *in_bundle, bool have_packet) | |
4058 | { | |
4059 | int vlan = vlan_tci_to_vid(flow->vlan_tci); | |
ecac4ebf BP |
4060 | if (vlan) { |
4061 | if (in_bundle->vlan_mode == PORT_VLAN_ACCESS) { | |
4062 | /* Drop tagged packet on access port */ | |
abe529af BP |
4063 | if (have_packet) { |
4064 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4065 | VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged " | |
4066 | "packet received on port %s configured with " | |
4067 | "implicit VLAN %"PRIu16, | |
4068 | ofproto->up.name, vlan, | |
4069 | in_bundle->name, in_bundle->vlan); | |
4070 | } | |
4071 | return -1; | |
ecac4ebf BP |
4072 | } else if (ofbundle_includes_vlan(in_bundle, vlan)) { |
4073 | return vlan; | |
4074 | } else { | |
4075 | /* Drop packets from a VLAN not member of the trunk */ | |
abe529af BP |
4076 | if (have_packet) { |
4077 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4078 | VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged " | |
4079 | "packet received on port %s not configured for " | |
4080 | "trunking VLAN %d", | |
4081 | ofproto->up.name, vlan, in_bundle->name, vlan); | |
4082 | } | |
4083 | return -1; | |
4084 | } | |
ecac4ebf BP |
4085 | } else { |
4086 | if (in_bundle->vlan_mode != PORT_VLAN_TRUNK) { | |
4087 | return in_bundle->vlan; | |
4088 | } else { | |
4089 | return ofbundle_includes_vlan(in_bundle, 0) ? 0 : -1; | |
4090 | } | |
abe529af | 4091 | } |
abe529af BP |
4092 | } |
4093 | ||
4094 | /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after | |
4095 | * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to | |
4096 | * indicate this; newer upstream kernels use gratuitous ARP requests. */ | |
4097 | static bool | |
4098 | is_gratuitous_arp(const struct flow *flow) | |
4099 | { | |
4100 | return (flow->dl_type == htons(ETH_TYPE_ARP) | |
4101 | && eth_addr_is_broadcast(flow->dl_dst) | |
4102 | && (flow->nw_proto == ARP_OP_REPLY | |
4103 | || (flow->nw_proto == ARP_OP_REQUEST | |
4104 | && flow->nw_src == flow->nw_dst))); | |
4105 | } | |
4106 | ||
4107 | static void | |
4108 | update_learning_table(struct ofproto_dpif *ofproto, | |
4109 | const struct flow *flow, int vlan, | |
4110 | struct ofbundle *in_bundle) | |
4111 | { | |
4112 | struct mac_entry *mac; | |
4113 | ||
4114 | if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) { | |
4115 | return; | |
4116 | } | |
4117 | ||
4118 | mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan); | |
4119 | if (is_gratuitous_arp(flow)) { | |
4120 | /* We don't want to learn from gratuitous ARP packets that are | |
4121 | * reflected back over bond slaves so we lock the learning table. */ | |
4122 | if (!in_bundle->bond) { | |
4123 | mac_entry_set_grat_arp_lock(mac); | |
4124 | } else if (mac_entry_is_grat_arp_locked(mac)) { | |
4125 | return; | |
4126 | } | |
4127 | } | |
4128 | ||
4129 | if (mac_entry_is_new(mac) || mac->port.p != in_bundle) { | |
4130 | /* The log messages here could actually be useful in debugging, | |
4131 | * so keep the rate limit relatively high. */ | |
4132 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300); | |
4133 | VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is " | |
4134 | "on port %s in VLAN %d", | |
4135 | ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src), | |
4136 | in_bundle->name, vlan); | |
4137 | ||
4138 | mac->port.p = in_bundle; | |
4139 | tag_set_add(&ofproto->revalidate_set, | |
4140 | mac_learning_changed(ofproto->ml, mac)); | |
4141 | } | |
4142 | } | |
4143 | ||
4144 | /* Determines whether packets in 'flow' within 'br' should be forwarded or | |
4145 | * dropped. Returns true if they may be forwarded, false if they should be | |
4146 | * dropped. | |
4147 | * | |
4148 | * If 'have_packet' is true, it indicates that the caller is processing a | |
4149 | * received packet. If 'have_packet' is false, then the caller is just | |
4150 | * revalidating an existing flow because configuration has changed. Either | |
4151 | * way, 'have_packet' only affects logging (there is no point in logging errors | |
4152 | * during revalidation). | |
4153 | * | |
4154 | * Sets '*in_portp' to the input port. This will be a null pointer if | |
4155 | * flow->in_port does not designate a known input port (in which case | |
4156 | * is_admissible() returns false). | |
4157 | * | |
4158 | * When returning true, sets '*vlanp' to the effective VLAN of the input | |
4159 | * packet, as returned by flow_get_vlan(). | |
4160 | * | |
4161 | * May also add tags to '*tags', although the current implementation only does | |
4162 | * so in one special case. | |
4163 | */ | |
4164 | static bool | |
4165 | is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow, | |
4166 | bool have_packet, | |
4167 | tag_type *tags, int *vlanp, struct ofbundle **in_bundlep) | |
4168 | { | |
4169 | struct ofport_dpif *in_port; | |
4170 | struct ofbundle *in_bundle; | |
4171 | int vlan; | |
4172 | ||
4173 | /* Find the port and bundle for the received packet. */ | |
4174 | in_port = get_ofp_port(ofproto, flow->in_port); | |
23adee42 | 4175 | *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL; |
abe529af BP |
4176 | if (!in_port || !in_bundle) { |
4177 | /* No interface? Something fishy... */ | |
4178 | if (have_packet) { | |
4179 | /* Odd. A few possible reasons here: | |
4180 | * | |
4181 | * - We deleted a port but there are still a few packets queued up | |
4182 | * from it. | |
4183 | * | |
4184 | * - Someone externally added a port (e.g. "ovs-dpctl add-if") that | |
4185 | * we don't know about. | |
4186 | * | |
4187 | * - Packet arrived on the local port but the local port is not | |
4188 | * part of a bundle. | |
4189 | */ | |
4190 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4191 | ||
4192 | VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown " | |
4193 | "port %"PRIu16, | |
4194 | ofproto->up.name, flow->in_port); | |
4195 | } | |
75a75043 | 4196 | *vlanp = -1; |
abe529af BP |
4197 | return false; |
4198 | } | |
4199 | *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet); | |
4200 | if (vlan < 0) { | |
4201 | return false; | |
4202 | } | |
4203 | ||
b53055f4 | 4204 | /* Drop frames for reserved multicast addresses |
8402c74b | 4205 | * only if forward_bpdu option is absent. */ |
b53055f4 | 4206 | if (eth_addr_is_reserved(flow->dl_dst) && |
8402c74b | 4207 | !ofproto->up.forward_bpdu) { |
abe529af BP |
4208 | return false; |
4209 | } | |
4210 | ||
4211 | /* Drop frames on bundles reserved for mirroring. */ | |
4212 | if (in_bundle->mirror_out) { | |
4213 | if (have_packet) { | |
4214 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4215 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port " | |
4216 | "%s, which is reserved exclusively for mirroring", | |
4217 | ofproto->up.name, in_bundle->name); | |
4218 | } | |
4219 | return false; | |
4220 | } | |
4221 | ||
4222 | if (in_bundle->bond) { | |
4223 | struct mac_entry *mac; | |
4224 | ||
4225 | switch (bond_check_admissibility(in_bundle->bond, in_port, | |
4226 | flow->dl_dst, tags)) { | |
4227 | case BV_ACCEPT: | |
4228 | break; | |
4229 | ||
4230 | case BV_DROP: | |
4231 | return false; | |
4232 | ||
4233 | case BV_DROP_IF_MOVED: | |
4234 | mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL); | |
4235 | if (mac && mac->port.p != in_bundle && | |
4236 | (!is_gratuitous_arp(flow) | |
4237 | || mac_entry_is_grat_arp_locked(mac))) { | |
4238 | return false; | |
4239 | } | |
4240 | break; | |
4241 | } | |
4242 | } | |
4243 | ||
4244 | return true; | |
4245 | } | |
4246 | ||
4cd78906 | 4247 | static void |
abe529af BP |
4248 | xlate_normal(struct action_xlate_ctx *ctx) |
4249 | { | |
4250 | struct ofbundle *in_bundle; | |
4251 | struct ofbundle *out_bundle; | |
4252 | struct mac_entry *mac; | |
4253 | int vlan; | |
4254 | ||
75a75043 BP |
4255 | ctx->has_normal = true; |
4256 | ||
abe529af BP |
4257 | /* Check whether we should drop packets in this flow. */ |
4258 | if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL, | |
4259 | &ctx->tags, &vlan, &in_bundle)) { | |
4260 | out_bundle = NULL; | |
4261 | goto done; | |
4262 | } | |
4263 | ||
75a75043 BP |
4264 | /* Learn source MAC. */ |
4265 | if (ctx->may_learn) { | |
abe529af BP |
4266 | update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle); |
4267 | } | |
4268 | ||
4269 | /* Determine output bundle. */ | |
4270 | mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan, | |
4271 | &ctx->tags); | |
4272 | if (mac) { | |
4273 | out_bundle = mac->port.p; | |
4274 | } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) { | |
4275 | /* If we are revalidating but don't have a learning entry then eject | |
4276 | * the flow. Installing a flow that floods packets opens up a window | |
4277 | * of time where we could learn from a packet reflected on a bond and | |
4278 | * blackhole packets before the learning table is updated to reflect | |
4279 | * the correct port. */ | |
4cd78906 BP |
4280 | ctx->may_set_up_flow = false; |
4281 | return; | |
abe529af BP |
4282 | } else { |
4283 | out_bundle = OFBUNDLE_FLOOD; | |
4284 | } | |
4285 | ||
4286 | /* Don't send packets out their input bundles. */ | |
4287 | if (in_bundle == out_bundle) { | |
4288 | out_bundle = NULL; | |
4289 | } | |
4290 | ||
4291 | done: | |
4292 | if (in_bundle) { | |
4293 | compose_actions(ctx, vlan, in_bundle, out_bundle); | |
4294 | } | |
abe529af BP |
4295 | } |
4296 | \f | |
54a9cbc9 BP |
4297 | /* Optimized flow revalidation. |
4298 | * | |
4299 | * It's a difficult problem, in general, to tell which facets need to have | |
4300 | * their actions recalculated whenever the OpenFlow flow table changes. We | |
4301 | * don't try to solve that general problem: for most kinds of OpenFlow flow | |
4302 | * table changes, we recalculate the actions for every facet. This is | |
4303 | * relatively expensive, but it's good enough if the OpenFlow flow table | |
4304 | * doesn't change very often. | |
4305 | * | |
4306 | * However, we can expect one particular kind of OpenFlow flow table change to | |
4307 | * happen frequently: changes caused by MAC learning. To avoid wasting a lot | |
4308 | * of CPU on revalidating every facet whenever MAC learning modifies the flow | |
4309 | * table, we add a special case that applies to flow tables in which every rule | |
4310 | * has the same form (that is, the same wildcards), except that the table is | |
4311 | * also allowed to have a single "catch-all" flow that matches all packets. We | |
4312 | * optimize this case by tagging all of the facets that resubmit into the table | |
4313 | * and invalidating the same tag whenever a flow changes in that table. The | |
4314 | * end result is that we revalidate just the facets that need it (and sometimes | |
4315 | * a few more, but not all of the facets or even all of the facets that | |
4316 | * resubmit to the table modified by MAC learning). */ | |
4317 | ||
4318 | /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted | |
4319 | * into an OpenFlow table with the given 'basis'. */ | |
4320 | static uint32_t | |
4321 | rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc, | |
4322 | uint32_t secret) | |
4323 | { | |
4324 | if (flow_wildcards_is_catchall(wc)) { | |
4325 | return 0; | |
4326 | } else { | |
4327 | struct flow tag_flow = *flow; | |
4328 | flow_zero_wildcards(&tag_flow, wc); | |
4329 | return tag_create_deterministic(flow_hash(&tag_flow, secret)); | |
4330 | } | |
4331 | } | |
4332 | ||
4333 | /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the | |
4334 | * taggability of that table. | |
4335 | * | |
4336 | * This function must be called after *each* change to a flow table. If you | |
4337 | * skip calling it on some changes then the pointer comparisons at the end can | |
4338 | * be invalid if you get unlucky. For example, if a flow removal causes a | |
4339 | * cls_table to be destroyed and then a flow insertion causes a cls_table with | |
4340 | * different wildcards to be created with the same address, then this function | |
4341 | * will incorrectly skip revalidation. */ | |
4342 | static void | |
4343 | table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id) | |
4344 | { | |
4345 | struct table_dpif *table = &ofproto->tables[table_id]; | |
4346 | const struct classifier *cls = &ofproto->up.tables[table_id]; | |
4347 | struct cls_table *catchall, *other; | |
4348 | struct cls_table *t; | |
4349 | ||
4350 | catchall = other = NULL; | |
4351 | ||
4352 | switch (hmap_count(&cls->tables)) { | |
4353 | case 0: | |
4354 | /* We could tag this OpenFlow table but it would make the logic a | |
4355 | * little harder and it's a corner case that doesn't seem worth it | |
4356 | * yet. */ | |
4357 | break; | |
4358 | ||
4359 | case 1: | |
4360 | case 2: | |
4361 | HMAP_FOR_EACH (t, hmap_node, &cls->tables) { | |
4362 | if (cls_table_is_catchall(t)) { | |
4363 | catchall = t; | |
4364 | } else if (!other) { | |
4365 | other = t; | |
4366 | } else { | |
4367 | /* Indicate that we can't tag this by setting both tables to | |
4368 | * NULL. (We know that 'catchall' is already NULL.) */ | |
4369 | other = NULL; | |
4370 | } | |
4371 | } | |
4372 | break; | |
4373 | ||
4374 | default: | |
4375 | /* Can't tag this table. */ | |
4376 | break; | |
4377 | } | |
4378 | ||
4379 | if (table->catchall_table != catchall || table->other_table != other) { | |
4380 | table->catchall_table = catchall; | |
4381 | table->other_table = other; | |
4382 | ofproto->need_revalidate = true; | |
4383 | } | |
4384 | } | |
4385 | ||
4386 | /* Given 'rule' that has changed in some way (either it is a rule being | |
4387 | * inserted, a rule being deleted, or a rule whose actions are being | |
4388 | * modified), marks facets for revalidation to ensure that packets will be | |
4389 | * forwarded correctly according to the new state of the flow table. | |
4390 | * | |
4391 | * This function must be called after *each* change to a flow table. See | |
4392 | * the comment on table_update_taggable() for more information. */ | |
4393 | static void | |
4394 | rule_invalidate(const struct rule_dpif *rule) | |
4395 | { | |
4396 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
4397 | ||
4398 | table_update_taggable(ofproto, rule->up.table_id); | |
4399 | ||
4400 | if (!ofproto->need_revalidate) { | |
4401 | struct table_dpif *table = &ofproto->tables[rule->up.table_id]; | |
4402 | ||
4403 | if (table->other_table && rule->tag) { | |
4404 | tag_set_add(&ofproto->revalidate_set, rule->tag); | |
4405 | } else { | |
4406 | ofproto->need_revalidate = true; | |
4407 | } | |
4408 | } | |
4409 | } | |
4410 | \f | |
abe529af BP |
4411 | static bool |
4412 | get_drop_frags(struct ofproto *ofproto_) | |
4413 | { | |
4414 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
4415 | bool drop_frags; | |
4416 | ||
4417 | dpif_get_drop_frags(ofproto->dpif, &drop_frags); | |
4418 | return drop_frags; | |
4419 | } | |
4420 | ||
4421 | static void | |
4422 | set_drop_frags(struct ofproto *ofproto_, bool drop_frags) | |
4423 | { | |
4424 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
4425 | ||
4426 | dpif_set_drop_frags(ofproto->dpif, drop_frags); | |
4427 | } | |
4428 | ||
4429 | static int | |
4430 | packet_out(struct ofproto *ofproto_, struct ofpbuf *packet, | |
4431 | const struct flow *flow, | |
4432 | const union ofp_action *ofp_actions, size_t n_ofp_actions) | |
4433 | { | |
4434 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
4435 | int error; | |
4436 | ||
4437 | error = validate_actions(ofp_actions, n_ofp_actions, flow, | |
4438 | ofproto->max_ports); | |
4439 | if (!error) { | |
80e5eed9 | 4440 | struct odputil_keybuf keybuf; |
abe529af BP |
4441 | struct action_xlate_ctx ctx; |
4442 | struct ofpbuf *odp_actions; | |
80e5eed9 BP |
4443 | struct ofpbuf key; |
4444 | ||
4445 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
4446 | odp_flow_key_from_flow(&key, flow); | |
abe529af BP |
4447 | |
4448 | action_xlate_ctx_init(&ctx, ofproto, flow, packet); | |
4449 | odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions); | |
80e5eed9 BP |
4450 | dpif_execute(ofproto->dpif, key.data, key.size, |
4451 | odp_actions->data, odp_actions->size, packet); | |
abe529af BP |
4452 | ofpbuf_delete(odp_actions); |
4453 | } | |
4454 | return error; | |
4455 | } | |
4456 | ||
4457 | static void | |
4458 | get_netflow_ids(const struct ofproto *ofproto_, | |
4459 | uint8_t *engine_type, uint8_t *engine_id) | |
4460 | { | |
4461 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
4462 | ||
4463 | dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id); | |
4464 | } | |
4465 | \f | |
4466 | static struct ofproto_dpif * | |
4467 | ofproto_dpif_lookup(const char *name) | |
4468 | { | |
4469 | struct ofproto *ofproto = ofproto_lookup(name); | |
4470 | return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class | |
4471 | ? ofproto_dpif_cast(ofproto) | |
4472 | : NULL); | |
4473 | } | |
4474 | ||
4475 | static void | |
4476 | ofproto_unixctl_fdb_show(struct unixctl_conn *conn, | |
4477 | const char *args, void *aux OVS_UNUSED) | |
4478 | { | |
4479 | struct ds ds = DS_EMPTY_INITIALIZER; | |
4480 | const struct ofproto_dpif *ofproto; | |
4481 | const struct mac_entry *e; | |
4482 | ||
4483 | ofproto = ofproto_dpif_lookup(args); | |
4484 | if (!ofproto) { | |
4485 | unixctl_command_reply(conn, 501, "no such bridge"); | |
4486 | return; | |
4487 | } | |
4488 | ||
4489 | ds_put_cstr(&ds, " port VLAN MAC Age\n"); | |
4490 | LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) { | |
4491 | struct ofbundle *bundle = e->port.p; | |
4492 | ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n", | |
4493 | ofbundle_get_a_port(bundle)->odp_port, | |
4494 | e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e)); | |
4495 | } | |
4496 | unixctl_command_reply(conn, 200, ds_cstr(&ds)); | |
4497 | ds_destroy(&ds); | |
4498 | } | |
4499 | ||
4500 | struct ofproto_trace { | |
4501 | struct action_xlate_ctx ctx; | |
4502 | struct flow flow; | |
4503 | struct ds *result; | |
4504 | }; | |
4505 | ||
4506 | static void | |
29901626 BP |
4507 | trace_format_rule(struct ds *result, uint8_t table_id, int level, |
4508 | const struct rule_dpif *rule) | |
abe529af BP |
4509 | { |
4510 | ds_put_char_multiple(result, '\t', level); | |
4511 | if (!rule) { | |
4512 | ds_put_cstr(result, "No match\n"); | |
4513 | return; | |
4514 | } | |
4515 | ||
29901626 BP |
4516 | ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ", |
4517 | table_id, ntohll(rule->up.flow_cookie)); | |
79feb7df | 4518 | cls_rule_format(&rule->up.cr, result); |
abe529af BP |
4519 | ds_put_char(result, '\n'); |
4520 | ||
4521 | ds_put_char_multiple(result, '\t', level); | |
4522 | ds_put_cstr(result, "OpenFlow "); | |
79feb7df | 4523 | ofp_print_actions(result, rule->up.actions, rule->up.n_actions); |
abe529af BP |
4524 | ds_put_char(result, '\n'); |
4525 | } | |
4526 | ||
4527 | static void | |
4528 | trace_format_flow(struct ds *result, int level, const char *title, | |
4529 | struct ofproto_trace *trace) | |
4530 | { | |
4531 | ds_put_char_multiple(result, '\t', level); | |
4532 | ds_put_format(result, "%s: ", title); | |
4533 | if (flow_equal(&trace->ctx.flow, &trace->flow)) { | |
4534 | ds_put_cstr(result, "unchanged"); | |
4535 | } else { | |
4536 | flow_format(result, &trace->ctx.flow); | |
4537 | trace->flow = trace->ctx.flow; | |
4538 | } | |
4539 | ds_put_char(result, '\n'); | |
4540 | } | |
4541 | ||
eb9e1c26 EJ |
4542 | static void |
4543 | trace_format_regs(struct ds *result, int level, const char *title, | |
4544 | struct ofproto_trace *trace) | |
4545 | { | |
4546 | size_t i; | |
4547 | ||
4548 | ds_put_char_multiple(result, '\t', level); | |
4549 | ds_put_format(result, "%s:", title); | |
4550 | for (i = 0; i < FLOW_N_REGS; i++) { | |
4551 | ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]); | |
4552 | } | |
4553 | ds_put_char(result, '\n'); | |
4554 | } | |
4555 | ||
abe529af BP |
4556 | static void |
4557 | trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule) | |
4558 | { | |
4559 | struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx); | |
4560 | struct ds *result = trace->result; | |
4561 | ||
4562 | ds_put_char(result, '\n'); | |
4563 | trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace); | |
eb9e1c26 | 4564 | trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace); |
29901626 | 4565 | trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule); |
abe529af BP |
4566 | } |
4567 | ||
4568 | static void | |
4569 | ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_, | |
4570 | void *aux OVS_UNUSED) | |
4571 | { | |
876b0e1c | 4572 | char *dpname, *arg1, *arg2, *arg3; |
abe529af BP |
4573 | char *args = xstrdup(args_); |
4574 | char *save_ptr = NULL; | |
4575 | struct ofproto_dpif *ofproto; | |
876b0e1c BP |
4576 | struct ofpbuf odp_key; |
4577 | struct ofpbuf *packet; | |
abe529af BP |
4578 | struct rule_dpif *rule; |
4579 | struct ds result; | |
4580 | struct flow flow; | |
abe529af BP |
4581 | char *s; |
4582 | ||
876b0e1c BP |
4583 | packet = NULL; |
4584 | ofpbuf_init(&odp_key, 0); | |
abe529af BP |
4585 | ds_init(&result); |
4586 | ||
4587 | dpname = strtok_r(args, " ", &save_ptr); | |
876b0e1c BP |
4588 | arg1 = strtok_r(NULL, " ", &save_ptr); |
4589 | arg2 = strtok_r(NULL, " ", &save_ptr); | |
4590 | arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */ | |
8b3b8dd1 BP |
4591 | if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) { |
4592 | /* ofproto/trace dpname flow [-generate] */ | |
876b0e1c BP |
4593 | int error; |
4594 | ||
df2c07f4 | 4595 | /* Convert string to datapath key. */ |
876b0e1c BP |
4596 | ofpbuf_init(&odp_key, 0); |
4597 | error = odp_flow_key_from_string(arg1, &odp_key); | |
4598 | if (error) { | |
4599 | unixctl_command_reply(conn, 501, "Bad flow syntax"); | |
4600 | goto exit; | |
4601 | } | |
4602 | ||
4603 | /* Convert odp_key to flow. */ | |
4604 | error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow); | |
4605 | if (error) { | |
4606 | unixctl_command_reply(conn, 501, "Invalid flow"); | |
4607 | goto exit; | |
4608 | } | |
8b3b8dd1 BP |
4609 | |
4610 | /* Generate a packet, if requested. */ | |
4611 | if (arg2) { | |
4612 | packet = ofpbuf_new(0); | |
4613 | flow_compose(packet, &flow); | |
4614 | } | |
876b0e1c BP |
4615 | } else if (dpname && arg1 && arg2 && arg3) { |
4616 | /* ofproto/trace dpname tun_id in_port packet */ | |
4617 | uint16_t in_port; | |
4618 | ovs_be64 tun_id; | |
4619 | ||
4620 | tun_id = htonll(strtoull(arg1, NULL, 0)); | |
4621 | in_port = ofp_port_to_odp_port(atoi(arg2)); | |
4622 | ||
4623 | packet = ofpbuf_new(strlen(args) / 2); | |
4624 | arg3 = ofpbuf_put_hex(packet, arg3, NULL); | |
4625 | arg3 += strspn(arg3, " "); | |
4626 | if (*arg3 != '\0') { | |
4627 | unixctl_command_reply(conn, 501, "Trailing garbage in command"); | |
4628 | goto exit; | |
4629 | } | |
4630 | if (packet->size < ETH_HEADER_LEN) { | |
4631 | unixctl_command_reply(conn, 501, | |
4632 | "Packet data too short for Ethernet"); | |
4633 | goto exit; | |
4634 | } | |
4635 | ||
4636 | ds_put_cstr(&result, "Packet: "); | |
4637 | s = ofp_packet_to_string(packet->data, packet->size, packet->size); | |
4638 | ds_put_cstr(&result, s); | |
4639 | free(s); | |
4640 | ||
4641 | flow_extract(packet, tun_id, in_port, &flow); | |
4642 | } else { | |
abe529af BP |
4643 | unixctl_command_reply(conn, 501, "Bad command syntax"); |
4644 | goto exit; | |
4645 | } | |
4646 | ||
4647 | ofproto = ofproto_dpif_lookup(dpname); | |
4648 | if (!ofproto) { | |
4649 | unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list " | |
4650 | "for help)"); | |
4651 | goto exit; | |
4652 | } | |
4653 | ||
abe529af BP |
4654 | ds_put_cstr(&result, "Flow: "); |
4655 | flow_format(&result, &flow); | |
4656 | ds_put_char(&result, '\n'); | |
4657 | ||
29901626 BP |
4658 | rule = rule_dpif_lookup(ofproto, &flow, 0); |
4659 | trace_format_rule(&result, 0, 0, rule); | |
abe529af BP |
4660 | if (rule) { |
4661 | struct ofproto_trace trace; | |
4662 | struct ofpbuf *odp_actions; | |
4663 | ||
4664 | trace.result = &result; | |
4665 | trace.flow = flow; | |
876b0e1c | 4666 | action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet); |
abe529af BP |
4667 | trace.ctx.resubmit_hook = trace_resubmit; |
4668 | odp_actions = xlate_actions(&trace.ctx, | |
4669 | rule->up.actions, rule->up.n_actions); | |
4670 | ||
4671 | ds_put_char(&result, '\n'); | |
4672 | trace_format_flow(&result, 0, "Final flow", &trace); | |
4673 | ds_put_cstr(&result, "Datapath actions: "); | |
4674 | format_odp_actions(&result, odp_actions->data, odp_actions->size); | |
4675 | ofpbuf_delete(odp_actions); | |
876b0e1c BP |
4676 | |
4677 | if (!trace.ctx.may_set_up_flow) { | |
4678 | if (packet) { | |
4679 | ds_put_cstr(&result, "\nThis flow is not cachable."); | |
4680 | } else { | |
4681 | ds_put_cstr(&result, "\nThe datapath actions are incomplete--" | |
4682 | "for complete actions, please supply a packet."); | |
4683 | } | |
4684 | } | |
abe529af BP |
4685 | } |
4686 | ||
4687 | unixctl_command_reply(conn, 200, ds_cstr(&result)); | |
4688 | ||
4689 | exit: | |
4690 | ds_destroy(&result); | |
876b0e1c BP |
4691 | ofpbuf_delete(packet); |
4692 | ofpbuf_uninit(&odp_key); | |
abe529af BP |
4693 | free(args); |
4694 | } | |
4695 | ||
7ee20df1 BP |
4696 | static void |
4697 | ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, | |
4698 | const char *args_ OVS_UNUSED, void *aux OVS_UNUSED) | |
4699 | { | |
4700 | clogged = true; | |
4701 | unixctl_command_reply(conn, 200, NULL); | |
4702 | } | |
4703 | ||
4704 | static void | |
4705 | ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, | |
4706 | const char *args_ OVS_UNUSED, void *aux OVS_UNUSED) | |
4707 | { | |
4708 | clogged = false; | |
4709 | unixctl_command_reply(conn, 200, NULL); | |
4710 | } | |
4711 | ||
abe529af BP |
4712 | static void |
4713 | ofproto_dpif_unixctl_init(void) | |
4714 | { | |
4715 | static bool registered; | |
4716 | if (registered) { | |
4717 | return; | |
4718 | } | |
4719 | registered = true; | |
4720 | ||
7ff2009a JP |
4721 | unixctl_command_register("ofproto/trace", |
4722 | "bridge {tun_id in_port packet | odp_flow [-generate]}", | |
4723 | ofproto_unixctl_trace, NULL); | |
4724 | unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show, | |
4725 | NULL); | |
4726 | unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL); | |
4727 | unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL); | |
abe529af BP |
4728 | } |
4729 | \f | |
4730 | const struct ofproto_class ofproto_dpif_class = { | |
4731 | enumerate_types, | |
4732 | enumerate_names, | |
4733 | del, | |
4734 | alloc, | |
4735 | construct, | |
4736 | destruct, | |
4737 | dealloc, | |
4738 | run, | |
4739 | wait, | |
4740 | flush, | |
6c1491fb BP |
4741 | get_features, |
4742 | get_tables, | |
abe529af BP |
4743 | port_alloc, |
4744 | port_construct, | |
4745 | port_destruct, | |
4746 | port_dealloc, | |
4747 | port_modified, | |
4748 | port_reconfigured, | |
4749 | port_query_by_name, | |
4750 | port_add, | |
4751 | port_del, | |
4752 | port_dump_start, | |
4753 | port_dump_next, | |
4754 | port_dump_done, | |
4755 | port_poll, | |
4756 | port_poll_wait, | |
4757 | port_is_lacp_current, | |
0ab6decf | 4758 | NULL, /* rule_choose_table */ |
abe529af BP |
4759 | rule_alloc, |
4760 | rule_construct, | |
4761 | rule_destruct, | |
4762 | rule_dealloc, | |
abe529af BP |
4763 | rule_get_stats, |
4764 | rule_execute, | |
4765 | rule_modify_actions, | |
4766 | get_drop_frags, | |
4767 | set_drop_frags, | |
4768 | packet_out, | |
4769 | set_netflow, | |
4770 | get_netflow_ids, | |
4771 | set_sflow, | |
4772 | set_cfm, | |
a5610457 | 4773 | get_cfm_fault, |
1de11730 | 4774 | get_cfm_remote_mpids, |
abe529af BP |
4775 | bundle_set, |
4776 | bundle_remove, | |
4777 | mirror_set, | |
4778 | set_flood_vlans, | |
4779 | is_mirror_output_bundle, | |
8402c74b | 4780 | forward_bpdu_changed, |
abe529af | 4781 | }; |