]>
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 | ||
19 | #include "ofproto/private.h" | |
20 | ||
21 | #include <errno.h> | |
22 | ||
23 | #include "autopath.h" | |
24 | #include "bond.h" | |
25 | #include "byte-order.h" | |
26 | #include "connmgr.h" | |
27 | #include "coverage.h" | |
28 | #include "cfm.h" | |
29 | #include "dpif.h" | |
30 | #include "dynamic-string.h" | |
31 | #include "fail-open.h" | |
32 | #include "hmapx.h" | |
33 | #include "lacp.h" | |
34 | #include "mac-learning.h" | |
35 | #include "multipath.h" | |
36 | #include "netdev.h" | |
37 | #include "netlink.h" | |
38 | #include "nx-match.h" | |
39 | #include "odp-util.h" | |
40 | #include "ofp-util.h" | |
41 | #include "ofpbuf.h" | |
42 | #include "ofp-print.h" | |
43 | #include "ofproto-sflow.h" | |
44 | #include "poll-loop.h" | |
45 | #include "timer.h" | |
6c1491fb | 46 | #include "unaligned.h" |
abe529af BP |
47 | #include "unixctl.h" |
48 | #include "vlan-bitmap.h" | |
49 | #include "vlog.h" | |
50 | ||
51 | VLOG_DEFINE_THIS_MODULE(ofproto_dpif); | |
52 | ||
53 | COVERAGE_DEFINE(ofproto_dpif_ctlr_action); | |
54 | COVERAGE_DEFINE(ofproto_dpif_expired); | |
55 | COVERAGE_DEFINE(ofproto_dpif_no_packet_in); | |
56 | COVERAGE_DEFINE(ofproto_dpif_xlate); | |
57 | COVERAGE_DEFINE(facet_changed_rule); | |
58 | COVERAGE_DEFINE(facet_invalidated); | |
59 | COVERAGE_DEFINE(facet_revalidate); | |
60 | COVERAGE_DEFINE(facet_unexpected); | |
61 | ||
62 | /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a | |
63 | * flow translation. */ | |
64 | #define MAX_RESUBMIT_RECURSION 16 | |
65 | ||
66 | struct ofport_dpif; | |
67 | struct ofproto_dpif; | |
68 | ||
69 | struct rule_dpif { | |
70 | struct rule up; | |
71 | ||
72 | long long int used; /* Time last used; time created if not used. */ | |
73 | ||
74 | /* These statistics: | |
75 | * | |
76 | * - Do include packets and bytes from facets that have been deleted or | |
77 | * whose own statistics have been folded into the rule. | |
78 | * | |
79 | * - Do include packets and bytes sent "by hand" that were accounted to | |
80 | * the rule without any facet being involved (this is a rare corner | |
81 | * case in rule_execute()). | |
82 | * | |
83 | * - Do not include packet or bytes that can be obtained from any facet's | |
84 | * packet_count or byte_count member or that can be obtained from the | |
85 | * datapath by, e.g., dpif_flow_get() for any facet. | |
86 | */ | |
87 | uint64_t packet_count; /* Number of packets received. */ | |
88 | uint64_t byte_count; /* Number of bytes received. */ | |
89 | ||
90 | struct list facets; /* List of "struct facet"s. */ | |
91 | }; | |
92 | ||
93 | static struct rule_dpif *rule_dpif_cast(const struct rule *rule) | |
94 | { | |
95 | return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL; | |
96 | } | |
97 | ||
98 | static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *ofproto, | |
99 | const struct flow *flow); | |
100 | ||
101 | #define MAX_MIRRORS 32 | |
102 | typedef uint32_t mirror_mask_t; | |
103 | #define MIRROR_MASK_C(X) UINT32_C(X) | |
104 | BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS); | |
105 | struct ofmirror { | |
106 | struct ofproto_dpif *ofproto; /* Owning ofproto. */ | |
107 | size_t idx; /* In ofproto's "mirrors" array. */ | |
108 | void *aux; /* Key supplied by ofproto's client. */ | |
109 | char *name; /* Identifier for log messages. */ | |
110 | ||
111 | /* Selection criteria. */ | |
112 | struct hmapx srcs; /* Contains "struct ofbundle *"s. */ | |
113 | struct hmapx dsts; /* Contains "struct ofbundle *"s. */ | |
114 | unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */ | |
115 | ||
116 | /* Output (mutually exclusive). */ | |
117 | struct ofbundle *out; /* Output port or NULL. */ | |
118 | int out_vlan; /* Output VLAN or -1. */ | |
119 | }; | |
120 | ||
121 | static void mirror_destroy(struct ofmirror *); | |
122 | ||
123 | /* A group of one or more OpenFlow ports. */ | |
124 | #define OFBUNDLE_FLOOD ((struct ofbundle *) 1) | |
125 | struct ofbundle { | |
126 | struct ofproto_dpif *ofproto; /* Owning ofproto. */ | |
127 | struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */ | |
128 | void *aux; /* Key supplied by ofproto's client. */ | |
129 | char *name; /* Identifier for log messages. */ | |
130 | ||
131 | /* Configuration. */ | |
132 | struct list ports; /* Contains "struct ofport"s. */ | |
133 | int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */ | |
134 | unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. | |
135 | * NULL if all VLANs are trunked. */ | |
136 | struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */ | |
137 | struct bond *bond; /* Nonnull iff more than one port. */ | |
138 | ||
139 | /* Status. */ | |
140 | bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */ | |
141 | ||
142 | /* Port mirroring info. */ | |
143 | mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */ | |
144 | mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */ | |
145 | mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */ | |
146 | }; | |
147 | ||
148 | static void bundle_remove(struct ofport *); | |
149 | static void bundle_destroy(struct ofbundle *); | |
150 | static void bundle_del_port(struct ofport_dpif *); | |
151 | static void bundle_run(struct ofbundle *); | |
152 | static void bundle_wait(struct ofbundle *); | |
153 | ||
154 | struct action_xlate_ctx { | |
155 | /* action_xlate_ctx_init() initializes these members. */ | |
156 | ||
157 | /* The ofproto. */ | |
158 | struct ofproto_dpif *ofproto; | |
159 | ||
160 | /* Flow to which the OpenFlow actions apply. xlate_actions() will modify | |
161 | * this flow when actions change header fields. */ | |
162 | struct flow flow; | |
163 | ||
164 | /* The packet corresponding to 'flow', or a null pointer if we are | |
165 | * revalidating without a packet to refer to. */ | |
166 | const struct ofpbuf *packet; | |
167 | ||
168 | /* If nonnull, called just before executing a resubmit action. | |
169 | * | |
170 | * This is normally null so the client has to set it manually after | |
171 | * calling action_xlate_ctx_init(). */ | |
172 | void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *); | |
173 | ||
abe529af BP |
174 | /* xlate_actions() initializes and uses these members. The client might want |
175 | * to look at them after it returns. */ | |
176 | ||
177 | struct ofpbuf *odp_actions; /* Datapath actions. */ | |
178 | tag_type tags; /* Tags associated with OFPP_NORMAL actions. */ | |
179 | bool may_set_up_flow; /* True ordinarily; false if the actions must | |
180 | * be reassessed for every packet. */ | |
181 | uint16_t nf_output_iface; /* Output interface index for NetFlow. */ | |
182 | ||
183 | /* xlate_actions() initializes and uses these members, but the client has no | |
184 | * reason to look at them. */ | |
185 | ||
186 | int recurse; /* Recursion level, via xlate_table_action. */ | |
187 | int last_pop_priority; /* Offset in 'odp_actions' just past most | |
188 | * recent ODP_ACTION_ATTR_SET_PRIORITY. */ | |
189 | }; | |
190 | ||
191 | static void action_xlate_ctx_init(struct action_xlate_ctx *, | |
192 | struct ofproto_dpif *, const struct flow *, | |
193 | const struct ofpbuf *); | |
194 | static struct ofpbuf *xlate_actions(struct action_xlate_ctx *, | |
195 | const union ofp_action *in, size_t n_in); | |
196 | ||
197 | /* An exact-match instantiation of an OpenFlow flow. */ | |
198 | struct facet { | |
199 | long long int used; /* Time last used; time created if not used. */ | |
200 | ||
201 | /* These statistics: | |
202 | * | |
203 | * - Do include packets and bytes sent "by hand", e.g. with | |
204 | * dpif_execute(). | |
205 | * | |
206 | * - Do include packets and bytes that were obtained from the datapath | |
207 | * when a flow was deleted (e.g. dpif_flow_del()) or when its | |
208 | * statistics were reset (e.g. dpif_flow_put() with | |
209 | * DPIF_FP_ZERO_STATS). | |
210 | * | |
211 | * - Do not include any packets or bytes that can currently be obtained | |
212 | * from the datapath by, e.g., dpif_flow_get(). | |
213 | */ | |
214 | uint64_t packet_count; /* Number of packets received. */ | |
215 | uint64_t byte_count; /* Number of bytes received. */ | |
216 | ||
217 | uint64_t dp_packet_count; /* Last known packet count in the datapath. */ | |
218 | uint64_t dp_byte_count; /* Last known byte count in the datapath. */ | |
219 | ||
220 | uint64_t rs_packet_count; /* Packets pushed to resubmit children. */ | |
221 | uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */ | |
222 | long long int rs_used; /* Used time pushed to resubmit children. */ | |
223 | ||
224 | /* Number of bytes passed to account_cb. This may include bytes that can | |
225 | * currently obtained from the datapath (thus, it can be greater than | |
226 | * byte_count). */ | |
227 | uint64_t accounted_bytes; | |
228 | ||
229 | struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */ | |
230 | struct list list_node; /* In owning rule's 'facets' list. */ | |
231 | struct rule_dpif *rule; /* Owning rule. */ | |
232 | struct flow flow; /* Exact-match flow. */ | |
233 | bool installed; /* Installed in datapath? */ | |
234 | bool may_install; /* True ordinarily; false if actions must | |
235 | * be reassessed for every packet. */ | |
236 | size_t actions_len; /* Number of bytes in actions[]. */ | |
237 | struct nlattr *actions; /* Datapath actions. */ | |
238 | tag_type tags; /* Tags. */ | |
239 | struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */ | |
240 | }; | |
241 | ||
242 | static struct facet *facet_create(struct rule_dpif *, const struct flow *, | |
243 | const struct ofpbuf *packet); | |
244 | static void facet_remove(struct ofproto_dpif *, struct facet *); | |
245 | static void facet_free(struct facet *); | |
246 | ||
247 | static struct facet *facet_find(struct ofproto_dpif *, const struct flow *); | |
248 | static struct facet *facet_lookup_valid(struct ofproto_dpif *, | |
249 | const struct flow *); | |
250 | static bool facet_revalidate(struct ofproto_dpif *, struct facet *); | |
251 | ||
252 | static void facet_execute(struct ofproto_dpif *, struct facet *, | |
253 | struct ofpbuf *packet); | |
254 | ||
255 | static int facet_put__(struct ofproto_dpif *, struct facet *, | |
256 | const struct nlattr *actions, size_t actions_len, | |
257 | struct dpif_flow_stats *); | |
258 | static void facet_install(struct ofproto_dpif *, struct facet *, | |
259 | bool zero_stats); | |
260 | static void facet_uninstall(struct ofproto_dpif *, struct facet *); | |
261 | static void facet_flush_stats(struct ofproto_dpif *, struct facet *); | |
262 | ||
263 | static void facet_make_actions(struct ofproto_dpif *, struct facet *, | |
264 | const struct ofpbuf *packet); | |
265 | static void facet_update_time(struct ofproto_dpif *, struct facet *, | |
266 | long long int used); | |
267 | static void facet_update_stats(struct ofproto_dpif *, struct facet *, | |
268 | const struct dpif_flow_stats *); | |
269 | static void facet_push_stats(struct facet *); | |
270 | static void facet_account(struct ofproto_dpif *, struct facet *, | |
271 | uint64_t extra_bytes); | |
272 | ||
273 | static bool facet_is_controller_flow(struct facet *); | |
274 | ||
275 | static void flow_push_stats(const struct rule_dpif *, | |
276 | struct flow *, uint64_t packets, uint64_t bytes, | |
277 | long long int used); | |
278 | ||
279 | struct ofport_dpif { | |
280 | struct ofport up; | |
281 | ||
282 | uint32_t odp_port; | |
283 | struct ofbundle *bundle; /* Bundle that contains this port, if any. */ | |
284 | struct list bundle_node; /* In struct ofbundle's "ports" list. */ | |
285 | struct cfm *cfm; /* Connectivity Fault Management, if any. */ | |
286 | tag_type tag; /* Tag associated with this port. */ | |
00794817 | 287 | uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */ |
abe529af BP |
288 | }; |
289 | ||
290 | static struct ofport_dpif * | |
291 | ofport_dpif_cast(const struct ofport *ofport) | |
292 | { | |
293 | assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class); | |
294 | return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL; | |
295 | } | |
296 | ||
297 | static void port_run(struct ofport_dpif *); | |
298 | static void port_wait(struct ofport_dpif *); | |
299 | static int set_cfm(struct ofport *, const struct cfm *, | |
300 | const uint16_t *remote_mps, size_t n_remote_mps); | |
301 | ||
302 | struct ofproto_dpif { | |
303 | struct ofproto up; | |
304 | struct dpif *dpif; | |
305 | int max_ports; | |
306 | ||
6c1491fb BP |
307 | /* Statistics. */ |
308 | uint64_t n_matches; | |
309 | ||
abe529af BP |
310 | /* Bridging. */ |
311 | struct netflow *netflow; | |
312 | struct ofproto_sflow *sflow; | |
313 | struct hmap bundles; /* Contains "struct ofbundle"s. */ | |
314 | struct mac_learning *ml; | |
315 | struct ofmirror *mirrors[MAX_MIRRORS]; | |
316 | bool has_bonded_bundles; | |
317 | ||
318 | /* Expiration. */ | |
319 | struct timer next_expiration; | |
320 | ||
321 | /* Facets. */ | |
322 | struct hmap facets; | |
323 | bool need_revalidate; | |
324 | struct tag_set revalidate_set; | |
325 | }; | |
326 | ||
327 | static void ofproto_dpif_unixctl_init(void); | |
328 | ||
329 | static struct ofproto_dpif * | |
330 | ofproto_dpif_cast(const struct ofproto *ofproto) | |
331 | { | |
332 | assert(ofproto->ofproto_class == &ofproto_dpif_class); | |
333 | return CONTAINER_OF(ofproto, struct ofproto_dpif, up); | |
334 | } | |
335 | ||
336 | static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *, | |
337 | uint16_t ofp_port); | |
338 | static struct ofport_dpif *get_odp_port(struct ofproto_dpif *, | |
339 | uint32_t odp_port); | |
340 | ||
341 | /* Packet processing. */ | |
342 | static void update_learning_table(struct ofproto_dpif *, | |
343 | const struct flow *, int vlan, | |
344 | struct ofbundle *); | |
345 | static bool is_admissible(struct ofproto_dpif *, const struct flow *, | |
346 | bool have_packet, tag_type *, int *vlanp, | |
347 | struct ofbundle **in_bundlep); | |
348 | static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *); | |
349 | ||
350 | /* Flow expiration. */ | |
351 | static int expire(struct ofproto_dpif *); | |
352 | ||
353 | /* Utilities. */ | |
354 | static int send_packet(struct ofproto_dpif *, | |
355 | uint32_t odp_port, uint16_t vlan_tci, | |
356 | const struct ofpbuf *packet); | |
357 | ||
358 | /* Global variables. */ | |
359 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
360 | \f | |
361 | /* Factory functions. */ | |
362 | ||
363 | static void | |
364 | enumerate_types(struct sset *types) | |
365 | { | |
366 | dp_enumerate_types(types); | |
367 | } | |
368 | ||
369 | static int | |
370 | enumerate_names(const char *type, struct sset *names) | |
371 | { | |
372 | return dp_enumerate_names(type, names); | |
373 | } | |
374 | ||
375 | static int | |
376 | del(const char *type, const char *name) | |
377 | { | |
378 | struct dpif *dpif; | |
379 | int error; | |
380 | ||
381 | error = dpif_open(name, type, &dpif); | |
382 | if (!error) { | |
383 | error = dpif_delete(dpif); | |
384 | dpif_close(dpif); | |
385 | } | |
386 | return error; | |
387 | } | |
388 | \f | |
389 | /* Basic life-cycle. */ | |
390 | ||
391 | static struct ofproto * | |
392 | alloc(void) | |
393 | { | |
394 | struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto); | |
395 | return &ofproto->up; | |
396 | } | |
397 | ||
398 | static void | |
399 | dealloc(struct ofproto *ofproto_) | |
400 | { | |
401 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
402 | free(ofproto); | |
403 | } | |
404 | ||
405 | static int | |
406 | construct(struct ofproto *ofproto_) | |
407 | { | |
408 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
409 | const char *name = ofproto->up.name; | |
410 | int error; | |
411 | int i; | |
412 | ||
413 | error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif); | |
414 | if (error) { | |
415 | VLOG_ERR("failed to open datapath %s: %s", name, strerror(error)); | |
416 | return error; | |
417 | } | |
418 | ||
419 | ofproto->max_ports = dpif_get_max_ports(ofproto->dpif); | |
6c1491fb | 420 | ofproto->n_matches = 0; |
abe529af BP |
421 | |
422 | error = dpif_recv_set_mask(ofproto->dpif, | |
423 | ((1u << DPIF_UC_MISS) | | |
424 | (1u << DPIF_UC_ACTION) | | |
425 | (1u << DPIF_UC_SAMPLE))); | |
426 | if (error) { | |
427 | VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error)); | |
428 | dpif_close(ofproto->dpif); | |
429 | return error; | |
430 | } | |
431 | dpif_flow_flush(ofproto->dpif); | |
432 | dpif_recv_purge(ofproto->dpif); | |
433 | ||
434 | ofproto->netflow = NULL; | |
435 | ofproto->sflow = NULL; | |
436 | hmap_init(&ofproto->bundles); | |
437 | ofproto->ml = mac_learning_create(); | |
438 | for (i = 0; i < MAX_MIRRORS; i++) { | |
439 | ofproto->mirrors[i] = NULL; | |
440 | } | |
441 | ofproto->has_bonded_bundles = false; | |
442 | ||
443 | timer_set_duration(&ofproto->next_expiration, 1000); | |
444 | ||
445 | hmap_init(&ofproto->facets); | |
446 | ofproto->need_revalidate = false; | |
447 | tag_set_init(&ofproto->revalidate_set); | |
448 | ||
6c1491fb BP |
449 | ofproto->up.tables = xmalloc(sizeof *ofproto->up.tables); |
450 | classifier_init(&ofproto->up.tables[0]); | |
451 | ofproto->up.n_tables = 1; | |
452 | ||
abe529af BP |
453 | ofproto_dpif_unixctl_init(); |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
458 | static void | |
459 | destruct(struct ofproto *ofproto_) | |
460 | { | |
461 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
462 | int i; | |
463 | ||
464 | for (i = 0; i < MAX_MIRRORS; i++) { | |
465 | mirror_destroy(ofproto->mirrors[i]); | |
466 | } | |
467 | ||
468 | netflow_destroy(ofproto->netflow); | |
469 | ofproto_sflow_destroy(ofproto->sflow); | |
470 | hmap_destroy(&ofproto->bundles); | |
471 | mac_learning_destroy(ofproto->ml); | |
472 | ||
473 | hmap_destroy(&ofproto->facets); | |
474 | ||
475 | dpif_close(ofproto->dpif); | |
476 | } | |
477 | ||
478 | static int | |
479 | run(struct ofproto *ofproto_) | |
480 | { | |
481 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
482 | struct ofport_dpif *ofport; | |
483 | struct ofbundle *bundle; | |
484 | int i; | |
485 | ||
486 | dpif_run(ofproto->dpif); | |
487 | ||
488 | for (i = 0; i < 50; i++) { | |
489 | struct dpif_upcall packet; | |
490 | int error; | |
491 | ||
492 | error = dpif_recv(ofproto->dpif, &packet); | |
493 | if (error) { | |
494 | if (error == ENODEV) { | |
495 | /* Datapath destroyed. */ | |
496 | return error; | |
497 | } | |
498 | break; | |
499 | } | |
500 | ||
501 | handle_upcall(ofproto, &packet); | |
502 | } | |
503 | ||
504 | if (timer_expired(&ofproto->next_expiration)) { | |
505 | int delay = expire(ofproto); | |
506 | timer_set_duration(&ofproto->next_expiration, delay); | |
507 | } | |
508 | ||
509 | if (ofproto->netflow) { | |
510 | netflow_run(ofproto->netflow); | |
511 | } | |
512 | if (ofproto->sflow) { | |
513 | ofproto_sflow_run(ofproto->sflow); | |
514 | } | |
515 | ||
516 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
517 | port_run(ofport); | |
518 | } | |
519 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
520 | bundle_run(bundle); | |
521 | } | |
522 | ||
523 | /* Now revalidate if there's anything to do. */ | |
524 | if (ofproto->need_revalidate | |
525 | || !tag_set_is_empty(&ofproto->revalidate_set)) { | |
526 | struct tag_set revalidate_set = ofproto->revalidate_set; | |
527 | bool revalidate_all = ofproto->need_revalidate; | |
528 | struct facet *facet, *next; | |
529 | ||
530 | /* Clear the revalidation flags. */ | |
531 | tag_set_init(&ofproto->revalidate_set); | |
532 | ofproto->need_revalidate = false; | |
533 | ||
534 | HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) { | |
535 | if (revalidate_all | |
536 | || tag_set_intersects(&revalidate_set, facet->tags)) { | |
537 | facet_revalidate(ofproto, facet); | |
538 | } | |
539 | } | |
540 | } | |
541 | ||
542 | return 0; | |
543 | } | |
544 | ||
545 | static void | |
546 | wait(struct ofproto *ofproto_) | |
547 | { | |
548 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
549 | struct ofport_dpif *ofport; | |
550 | struct ofbundle *bundle; | |
551 | ||
552 | dpif_wait(ofproto->dpif); | |
553 | dpif_recv_wait(ofproto->dpif); | |
554 | if (ofproto->sflow) { | |
555 | ofproto_sflow_wait(ofproto->sflow); | |
556 | } | |
557 | if (!tag_set_is_empty(&ofproto->revalidate_set)) { | |
558 | poll_immediate_wake(); | |
559 | } | |
560 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
561 | port_wait(ofport); | |
562 | } | |
563 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
564 | bundle_wait(bundle); | |
565 | } | |
566 | if (ofproto->need_revalidate) { | |
567 | /* Shouldn't happen, but if it does just go around again. */ | |
568 | VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()"); | |
569 | poll_immediate_wake(); | |
570 | } else { | |
571 | timer_wait(&ofproto->next_expiration); | |
572 | } | |
573 | } | |
574 | ||
575 | static void | |
576 | flush(struct ofproto *ofproto_) | |
577 | { | |
578 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
579 | struct facet *facet, *next_facet; | |
580 | ||
581 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
582 | /* Mark the facet as not installed so that facet_remove() doesn't | |
583 | * bother trying to uninstall it. There is no point in uninstalling it | |
584 | * individually since we are about to blow away all the facets with | |
585 | * dpif_flow_flush(). */ | |
586 | facet->installed = false; | |
587 | facet->dp_packet_count = 0; | |
588 | facet->dp_byte_count = 0; | |
589 | facet_remove(ofproto, facet); | |
590 | } | |
591 | dpif_flow_flush(ofproto->dpif); | |
592 | } | |
593 | ||
6c1491fb BP |
594 | static void |
595 | get_features(struct ofproto *ofproto_ OVS_UNUSED, | |
596 | bool *arp_match_ip, uint32_t *actions) | |
597 | { | |
598 | *arp_match_ip = true; | |
599 | *actions = ((1u << OFPAT_OUTPUT) | | |
600 | (1u << OFPAT_SET_VLAN_VID) | | |
601 | (1u << OFPAT_SET_VLAN_PCP) | | |
602 | (1u << OFPAT_STRIP_VLAN) | | |
603 | (1u << OFPAT_SET_DL_SRC) | | |
604 | (1u << OFPAT_SET_DL_DST) | | |
605 | (1u << OFPAT_SET_NW_SRC) | | |
606 | (1u << OFPAT_SET_NW_DST) | | |
607 | (1u << OFPAT_SET_NW_TOS) | | |
608 | (1u << OFPAT_SET_TP_SRC) | | |
609 | (1u << OFPAT_SET_TP_DST) | | |
610 | (1u << OFPAT_ENQUEUE)); | |
611 | } | |
612 | ||
613 | static void | |
614 | get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots) | |
615 | { | |
616 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
617 | struct odp_stats s; | |
618 | ||
619 | strcpy(ots->name, "classifier"); | |
620 | ||
621 | dpif_get_dp_stats(ofproto->dpif, &s); | |
622 | put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed)); | |
623 | put_32aligned_be64(&ots->matched_count, | |
624 | htonll(s.n_hit + ofproto->n_matches)); | |
625 | } | |
626 | ||
abe529af BP |
627 | static int |
628 | set_netflow(struct ofproto *ofproto_, | |
629 | const struct netflow_options *netflow_options) | |
630 | { | |
631 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
632 | ||
633 | if (netflow_options) { | |
634 | if (!ofproto->netflow) { | |
635 | ofproto->netflow = netflow_create(); | |
636 | } | |
637 | return netflow_set_options(ofproto->netflow, netflow_options); | |
638 | } else { | |
639 | netflow_destroy(ofproto->netflow); | |
640 | ofproto->netflow = NULL; | |
641 | return 0; | |
642 | } | |
643 | } | |
644 | ||
645 | static struct ofport * | |
646 | port_alloc(void) | |
647 | { | |
648 | struct ofport_dpif *port = xmalloc(sizeof *port); | |
649 | return &port->up; | |
650 | } | |
651 | ||
652 | static void | |
653 | port_dealloc(struct ofport *port_) | |
654 | { | |
655 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
656 | free(port); | |
657 | } | |
658 | ||
659 | static int | |
660 | port_construct(struct ofport *port_) | |
661 | { | |
662 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
663 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
664 | ||
665 | port->odp_port = ofp_port_to_odp_port(port->up.ofp_port); | |
666 | port->bundle = NULL; | |
667 | port->cfm = NULL; | |
668 | port->tag = tag_create_random(); | |
669 | ||
670 | if (ofproto->sflow) { | |
671 | ofproto_sflow_add_port(ofproto->sflow, port->odp_port, | |
672 | netdev_get_name(port->up.netdev)); | |
673 | } | |
674 | ||
675 | return 0; | |
676 | } | |
677 | ||
678 | static void | |
679 | port_destruct(struct ofport *port_) | |
680 | { | |
681 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
682 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
683 | ||
684 | bundle_remove(port_); | |
685 | set_cfm(port_, NULL, NULL, 0); | |
686 | if (ofproto->sflow) { | |
687 | ofproto_sflow_del_port(ofproto->sflow, port->odp_port); | |
688 | } | |
689 | } | |
690 | ||
691 | static void | |
692 | port_modified(struct ofport *port_) | |
693 | { | |
694 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
695 | ||
696 | if (port->bundle && port->bundle->bond) { | |
697 | bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev); | |
698 | } | |
699 | } | |
700 | ||
701 | static void | |
702 | port_reconfigured(struct ofport *port_, ovs_be32 old_config) | |
703 | { | |
704 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
705 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
706 | ovs_be32 changed = old_config ^ port->up.opp.config; | |
707 | ||
708 | if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP | | |
709 | OFPPC_NO_FWD | OFPPC_NO_FLOOD)) { | |
710 | ofproto->need_revalidate = true; | |
711 | } | |
712 | } | |
713 | ||
714 | static int | |
715 | set_sflow(struct ofproto *ofproto_, | |
716 | const struct ofproto_sflow_options *sflow_options) | |
717 | { | |
718 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
719 | struct ofproto_sflow *os = ofproto->sflow; | |
720 | if (sflow_options) { | |
721 | if (!os) { | |
722 | struct ofport_dpif *ofport; | |
723 | ||
724 | os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif); | |
725 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
726 | ofproto_sflow_add_port(os, ofport->odp_port, | |
727 | netdev_get_name(ofport->up.netdev)); | |
728 | } | |
729 | } | |
730 | ofproto_sflow_set_options(os, sflow_options); | |
731 | } else { | |
732 | ofproto_sflow_destroy(os); | |
733 | ofproto->sflow = NULL; | |
734 | } | |
735 | return 0; | |
736 | } | |
737 | ||
738 | static int | |
739 | set_cfm(struct ofport *ofport_, const struct cfm *cfm, | |
740 | const uint16_t *remote_mps, size_t n_remote_mps) | |
741 | { | |
742 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
743 | int error; | |
744 | ||
745 | if (!cfm) { | |
746 | error = 0; | |
747 | } else { | |
748 | if (!ofport->cfm) { | |
749 | ofport->cfm = cfm_create(); | |
750 | } | |
751 | ||
752 | ofport->cfm->mpid = cfm->mpid; | |
753 | ofport->cfm->interval = cfm->interval; | |
754 | memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN); | |
755 | ||
756 | cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps); | |
757 | ||
758 | if (cfm_configure(ofport->cfm)) { | |
759 | return 0; | |
760 | } | |
761 | ||
762 | error = EINVAL; | |
763 | } | |
764 | cfm_destroy(ofport->cfm); | |
765 | ofport->cfm = NULL; | |
766 | return error; | |
767 | } | |
768 | ||
769 | static int | |
770 | get_cfm(struct ofport *ofport_, const struct cfm **cfmp) | |
771 | { | |
772 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
773 | *cfmp = ofport->cfm; | |
774 | return 0; | |
775 | } | |
776 | \f | |
777 | /* Bundles. */ | |
778 | ||
779 | /* Expires all MAC learning entries associated with 'port' and forces ofproto | |
780 | * to revalidate every flow. */ | |
781 | static void | |
782 | bundle_flush_macs(struct ofbundle *bundle) | |
783 | { | |
784 | struct ofproto_dpif *ofproto = bundle->ofproto; | |
785 | struct mac_learning *ml = ofproto->ml; | |
786 | struct mac_entry *mac, *next_mac; | |
787 | ||
788 | ofproto->need_revalidate = true; | |
789 | LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) { | |
790 | if (mac->port.p == bundle) { | |
791 | mac_learning_expire(ml, mac); | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | static struct ofbundle * | |
797 | bundle_lookup(const struct ofproto_dpif *ofproto, void *aux) | |
798 | { | |
799 | struct ofbundle *bundle; | |
800 | ||
801 | HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0), | |
802 | &ofproto->bundles) { | |
803 | if (bundle->aux == aux) { | |
804 | return bundle; | |
805 | } | |
806 | } | |
807 | return NULL; | |
808 | } | |
809 | ||
810 | /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the | |
811 | * ones that are found to 'bundles'. */ | |
812 | static void | |
813 | bundle_lookup_multiple(struct ofproto_dpif *ofproto, | |
814 | void **auxes, size_t n_auxes, | |
815 | struct hmapx *bundles) | |
816 | { | |
817 | size_t i; | |
818 | ||
819 | hmapx_init(bundles); | |
820 | for (i = 0; i < n_auxes; i++) { | |
821 | struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]); | |
822 | if (bundle) { | |
823 | hmapx_add(bundles, bundle); | |
824 | } | |
825 | } | |
826 | } | |
827 | ||
828 | static void | |
829 | bundle_del_port(struct ofport_dpif *port) | |
830 | { | |
831 | struct ofbundle *bundle = port->bundle; | |
832 | ||
833 | list_remove(&port->bundle_node); | |
834 | port->bundle = NULL; | |
835 | ||
836 | if (bundle->lacp) { | |
837 | lacp_slave_unregister(bundle->lacp, port); | |
838 | } | |
839 | if (bundle->bond) { | |
840 | bond_slave_unregister(bundle->bond, port); | |
841 | } | |
842 | ||
843 | bundle->floodable = true; | |
844 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
845 | if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) { | |
846 | bundle->floodable = false; | |
847 | } | |
848 | } | |
849 | } | |
850 | ||
851 | static bool | |
852 | bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port, | |
00794817 BP |
853 | struct lacp_slave_settings *lacp, |
854 | uint32_t bond_stable_id) | |
abe529af BP |
855 | { |
856 | struct ofport_dpif *port; | |
857 | ||
858 | port = get_ofp_port(bundle->ofproto, ofp_port); | |
859 | if (!port) { | |
860 | return false; | |
861 | } | |
862 | ||
863 | if (port->bundle != bundle) { | |
864 | if (port->bundle) { | |
865 | bundle_del_port(port); | |
866 | } | |
867 | ||
868 | port->bundle = bundle; | |
869 | list_push_back(&bundle->ports, &port->bundle_node); | |
870 | if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) { | |
871 | bundle->floodable = false; | |
872 | } | |
873 | } | |
874 | if (lacp) { | |
875 | lacp_slave_register(bundle->lacp, port, lacp); | |
876 | } | |
877 | ||
00794817 BP |
878 | port->bond_stable_id = bond_stable_id; |
879 | ||
abe529af BP |
880 | return true; |
881 | } | |
882 | ||
883 | static void | |
884 | bundle_destroy(struct ofbundle *bundle) | |
885 | { | |
886 | struct ofproto_dpif *ofproto; | |
887 | struct ofport_dpif *port, *next_port; | |
888 | int i; | |
889 | ||
890 | if (!bundle) { | |
891 | return; | |
892 | } | |
893 | ||
894 | ofproto = bundle->ofproto; | |
895 | for (i = 0; i < MAX_MIRRORS; i++) { | |
896 | struct ofmirror *m = ofproto->mirrors[i]; | |
897 | if (m) { | |
898 | if (m->out == bundle) { | |
899 | mirror_destroy(m); | |
900 | } else if (hmapx_find_and_delete(&m->srcs, bundle) | |
901 | || hmapx_find_and_delete(&m->dsts, bundle)) { | |
902 | ofproto->need_revalidate = true; | |
903 | } | |
904 | } | |
905 | } | |
906 | ||
907 | LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) { | |
908 | bundle_del_port(port); | |
909 | } | |
910 | ||
911 | bundle_flush_macs(bundle); | |
912 | hmap_remove(&ofproto->bundles, &bundle->hmap_node); | |
913 | free(bundle->name); | |
914 | free(bundle->trunks); | |
915 | lacp_destroy(bundle->lacp); | |
916 | bond_destroy(bundle->bond); | |
917 | free(bundle); | |
918 | } | |
919 | ||
920 | static int | |
921 | bundle_set(struct ofproto *ofproto_, void *aux, | |
922 | const struct ofproto_bundle_settings *s) | |
923 | { | |
924 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
925 | bool need_flush = false; | |
926 | const unsigned long *trunks; | |
927 | struct ofport_dpif *port; | |
928 | struct ofbundle *bundle; | |
929 | size_t i; | |
930 | bool ok; | |
931 | ||
932 | if (!s) { | |
933 | bundle_destroy(bundle_lookup(ofproto, aux)); | |
934 | return 0; | |
935 | } | |
936 | ||
937 | assert(s->n_slaves == 1 || s->bond != NULL); | |
938 | assert((s->lacp != NULL) == (s->lacp_slaves != NULL)); | |
939 | ||
940 | bundle = bundle_lookup(ofproto, aux); | |
941 | if (!bundle) { | |
942 | bundle = xmalloc(sizeof *bundle); | |
943 | ||
944 | bundle->ofproto = ofproto; | |
945 | hmap_insert(&ofproto->bundles, &bundle->hmap_node, | |
946 | hash_pointer(aux, 0)); | |
947 | bundle->aux = aux; | |
948 | bundle->name = NULL; | |
949 | ||
950 | list_init(&bundle->ports); | |
951 | bundle->vlan = -1; | |
952 | bundle->trunks = NULL; | |
953 | bundle->lacp = NULL; | |
954 | bundle->bond = NULL; | |
955 | ||
956 | bundle->floodable = true; | |
957 | ||
958 | bundle->src_mirrors = 0; | |
959 | bundle->dst_mirrors = 0; | |
960 | bundle->mirror_out = 0; | |
961 | } | |
962 | ||
963 | if (!bundle->name || strcmp(s->name, bundle->name)) { | |
964 | free(bundle->name); | |
965 | bundle->name = xstrdup(s->name); | |
966 | } | |
967 | ||
968 | /* LACP. */ | |
969 | if (s->lacp) { | |
970 | if (!bundle->lacp) { | |
971 | bundle->lacp = lacp_create(); | |
972 | } | |
973 | lacp_configure(bundle->lacp, s->lacp); | |
974 | } else { | |
975 | lacp_destroy(bundle->lacp); | |
976 | bundle->lacp = NULL; | |
977 | } | |
978 | ||
979 | /* Update set of ports. */ | |
980 | ok = true; | |
981 | for (i = 0; i < s->n_slaves; i++) { | |
982 | if (!bundle_add_port(bundle, s->slaves[i], | |
00794817 BP |
983 | s->lacp ? &s->lacp_slaves[i] : NULL, |
984 | s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) { | |
abe529af BP |
985 | ok = false; |
986 | } | |
987 | } | |
988 | if (!ok || list_size(&bundle->ports) != s->n_slaves) { | |
989 | struct ofport_dpif *next_port; | |
990 | ||
991 | LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) { | |
992 | for (i = 0; i < s->n_slaves; i++) { | |
993 | if (s->slaves[i] == odp_port_to_ofp_port(port->odp_port)) { | |
994 | goto found; | |
995 | } | |
996 | } | |
997 | ||
998 | bundle_del_port(port); | |
999 | found: ; | |
1000 | } | |
1001 | } | |
1002 | assert(list_size(&bundle->ports) <= s->n_slaves); | |
1003 | ||
1004 | if (list_is_empty(&bundle->ports)) { | |
1005 | bundle_destroy(bundle); | |
1006 | return EINVAL; | |
1007 | } | |
1008 | ||
1009 | /* Set VLAN tag. */ | |
1010 | if (s->vlan != bundle->vlan) { | |
1011 | bundle->vlan = s->vlan; | |
1012 | need_flush = true; | |
1013 | } | |
1014 | ||
1015 | /* Get trunked VLANs. */ | |
1016 | trunks = s->vlan == -1 ? NULL : s->trunks; | |
1017 | if (!vlan_bitmap_equal(trunks, bundle->trunks)) { | |
1018 | free(bundle->trunks); | |
1019 | bundle->trunks = vlan_bitmap_clone(trunks); | |
1020 | need_flush = true; | |
1021 | } | |
1022 | ||
1023 | /* Bonding. */ | |
1024 | if (!list_is_short(&bundle->ports)) { | |
1025 | bundle->ofproto->has_bonded_bundles = true; | |
1026 | if (bundle->bond) { | |
1027 | if (bond_reconfigure(bundle->bond, s->bond)) { | |
1028 | ofproto->need_revalidate = true; | |
1029 | } | |
1030 | } else { | |
1031 | bundle->bond = bond_create(s->bond); | |
1032 | } | |
1033 | ||
1034 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
00794817 | 1035 | bond_slave_register(bundle->bond, port, port->bond_stable_id, |
abe529af BP |
1036 | port->up.netdev); |
1037 | } | |
1038 | } else { | |
1039 | bond_destroy(bundle->bond); | |
1040 | bundle->bond = NULL; | |
1041 | } | |
1042 | ||
1043 | /* If we changed something that would affect MAC learning, un-learn | |
1044 | * everything on this port and force flow revalidation. */ | |
1045 | if (need_flush) { | |
1046 | bundle_flush_macs(bundle); | |
1047 | } | |
1048 | ||
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | static void | |
1053 | bundle_remove(struct ofport *port_) | |
1054 | { | |
1055 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
1056 | struct ofbundle *bundle = port->bundle; | |
1057 | ||
1058 | if (bundle) { | |
1059 | bundle_del_port(port); | |
1060 | if (list_is_empty(&bundle->ports)) { | |
1061 | bundle_destroy(bundle); | |
1062 | } else if (list_is_short(&bundle->ports)) { | |
1063 | bond_destroy(bundle->bond); | |
1064 | bundle->bond = NULL; | |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | static void | |
1070 | send_pdu_cb(void *port_, const struct lacp_pdu *pdu) | |
1071 | { | |
1072 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10); | |
1073 | struct ofport_dpif *port = port_; | |
1074 | uint8_t ea[ETH_ADDR_LEN]; | |
1075 | int error; | |
1076 | ||
1077 | error = netdev_get_etheraddr(port->up.netdev, ea); | |
1078 | if (!error) { | |
1079 | struct lacp_pdu *packet_pdu; | |
1080 | struct ofpbuf packet; | |
1081 | ||
1082 | ofpbuf_init(&packet, 0); | |
1083 | packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP, | |
1084 | sizeof *packet_pdu); | |
1085 | *packet_pdu = *pdu; | |
1086 | error = netdev_send(port->up.netdev, &packet); | |
1087 | if (error) { | |
1088 | VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed " | |
1089 | "(%s)", port->bundle->name, | |
1090 | netdev_get_name(port->up.netdev), strerror(error)); | |
1091 | } | |
1092 | ofpbuf_uninit(&packet); | |
1093 | } else { | |
1094 | VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface " | |
1095 | "%s (%s)", port->bundle->name, | |
1096 | netdev_get_name(port->up.netdev), strerror(error)); | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | static void | |
1101 | bundle_send_learning_packets(struct ofbundle *bundle) | |
1102 | { | |
1103 | struct ofproto_dpif *ofproto = bundle->ofproto; | |
1104 | int error, n_packets, n_errors; | |
1105 | struct mac_entry *e; | |
1106 | ||
1107 | error = n_packets = n_errors = 0; | |
1108 | LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) { | |
1109 | if (e->port.p != bundle) { | |
1110 | int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan); | |
1111 | if (ret) { | |
1112 | error = ret; | |
1113 | n_errors++; | |
1114 | } | |
1115 | n_packets++; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | if (n_errors) { | |
1120 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1121 | VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning " | |
1122 | "packets, last error was: %s", | |
1123 | bundle->name, n_errors, n_packets, strerror(error)); | |
1124 | } else { | |
1125 | VLOG_DBG("bond %s: sent %d gratuitous learning packets", | |
1126 | bundle->name, n_packets); | |
1127 | } | |
1128 | } | |
1129 | ||
1130 | static void | |
1131 | bundle_run(struct ofbundle *bundle) | |
1132 | { | |
1133 | if (bundle->lacp) { | |
1134 | lacp_run(bundle->lacp, send_pdu_cb); | |
1135 | } | |
1136 | if (bundle->bond) { | |
1137 | struct ofport_dpif *port; | |
1138 | ||
1139 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
1140 | bool may_enable = lacp_slave_may_enable(bundle->lacp, port); | |
1141 | bond_slave_set_lacp_may_enable(bundle->bond, port, may_enable); | |
1142 | } | |
1143 | ||
1144 | bond_run(bundle->bond, &bundle->ofproto->revalidate_set, | |
1145 | lacp_negotiated(bundle->lacp)); | |
1146 | if (bond_should_send_learning_packets(bundle->bond)) { | |
1147 | bundle_send_learning_packets(bundle); | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | static void | |
1153 | bundle_wait(struct ofbundle *bundle) | |
1154 | { | |
1155 | if (bundle->lacp) { | |
1156 | lacp_wait(bundle->lacp); | |
1157 | } | |
1158 | if (bundle->bond) { | |
1159 | bond_wait(bundle->bond); | |
1160 | } | |
1161 | } | |
1162 | \f | |
1163 | /* Mirrors. */ | |
1164 | ||
1165 | static int | |
1166 | mirror_scan(struct ofproto_dpif *ofproto) | |
1167 | { | |
1168 | int idx; | |
1169 | ||
1170 | for (idx = 0; idx < MAX_MIRRORS; idx++) { | |
1171 | if (!ofproto->mirrors[idx]) { | |
1172 | return idx; | |
1173 | } | |
1174 | } | |
1175 | return -1; | |
1176 | } | |
1177 | ||
1178 | static struct ofmirror * | |
1179 | mirror_lookup(struct ofproto_dpif *ofproto, void *aux) | |
1180 | { | |
1181 | int i; | |
1182 | ||
1183 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1184 | struct ofmirror *mirror = ofproto->mirrors[i]; | |
1185 | if (mirror && mirror->aux == aux) { | |
1186 | return mirror; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | return NULL; | |
1191 | } | |
1192 | ||
1193 | static int | |
1194 | mirror_set(struct ofproto *ofproto_, void *aux, | |
1195 | const struct ofproto_mirror_settings *s) | |
1196 | { | |
1197 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1198 | mirror_mask_t mirror_bit; | |
1199 | struct ofbundle *bundle; | |
1200 | struct ofmirror *mirror; | |
1201 | struct ofbundle *out; | |
1202 | struct hmapx srcs; /* Contains "struct ofbundle *"s. */ | |
1203 | struct hmapx dsts; /* Contains "struct ofbundle *"s. */ | |
1204 | int out_vlan; | |
1205 | ||
1206 | mirror = mirror_lookup(ofproto, aux); | |
1207 | if (!s) { | |
1208 | mirror_destroy(mirror); | |
1209 | return 0; | |
1210 | } | |
1211 | if (!mirror) { | |
1212 | int idx; | |
1213 | ||
1214 | idx = mirror_scan(ofproto); | |
1215 | if (idx < 0) { | |
1216 | VLOG_WARN("bridge %s: maximum of %d port mirrors reached, " | |
1217 | "cannot create %s", | |
1218 | ofproto->up.name, MAX_MIRRORS, s->name); | |
1219 | return EFBIG; | |
1220 | } | |
1221 | ||
1222 | mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror); | |
1223 | mirror->ofproto = ofproto; | |
1224 | mirror->idx = idx; | |
1225 | mirror->out_vlan = -1; | |
1226 | mirror->name = NULL; | |
1227 | } | |
1228 | ||
1229 | if (!mirror->name || strcmp(s->name, mirror->name)) { | |
1230 | free(mirror->name); | |
1231 | mirror->name = xstrdup(s->name); | |
1232 | } | |
1233 | ||
1234 | /* Get the new configuration. */ | |
1235 | if (s->out_bundle) { | |
1236 | out = bundle_lookup(ofproto, s->out_bundle); | |
1237 | if (!out) { | |
1238 | mirror_destroy(mirror); | |
1239 | return EINVAL; | |
1240 | } | |
1241 | out_vlan = -1; | |
1242 | } else { | |
1243 | out = NULL; | |
1244 | out_vlan = s->out_vlan; | |
1245 | } | |
1246 | bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs); | |
1247 | bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts); | |
1248 | ||
1249 | /* If the configuration has not changed, do nothing. */ | |
1250 | if (hmapx_equals(&srcs, &mirror->srcs) | |
1251 | && hmapx_equals(&dsts, &mirror->dsts) | |
1252 | && vlan_bitmap_equal(mirror->vlans, s->src_vlans) | |
1253 | && mirror->out == out | |
1254 | && mirror->out_vlan == out_vlan) | |
1255 | { | |
1256 | hmapx_destroy(&srcs); | |
1257 | hmapx_destroy(&dsts); | |
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | hmapx_swap(&srcs, &mirror->srcs); | |
1262 | hmapx_destroy(&srcs); | |
1263 | ||
1264 | hmapx_swap(&dsts, &mirror->dsts); | |
1265 | hmapx_destroy(&dsts); | |
1266 | ||
1267 | free(mirror->vlans); | |
1268 | mirror->vlans = vlan_bitmap_clone(s->src_vlans); | |
1269 | ||
1270 | mirror->out = out; | |
1271 | mirror->out_vlan = out_vlan; | |
1272 | ||
1273 | /* Update bundles. */ | |
1274 | mirror_bit = MIRROR_MASK_C(1) << mirror->idx; | |
1275 | HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) { | |
1276 | if (hmapx_contains(&mirror->srcs, bundle)) { | |
1277 | bundle->src_mirrors |= mirror_bit; | |
1278 | } else { | |
1279 | bundle->src_mirrors &= ~mirror_bit; | |
1280 | } | |
1281 | ||
1282 | if (hmapx_contains(&mirror->dsts, bundle)) { | |
1283 | bundle->dst_mirrors |= mirror_bit; | |
1284 | } else { | |
1285 | bundle->dst_mirrors &= ~mirror_bit; | |
1286 | } | |
1287 | ||
1288 | if (mirror->out == bundle) { | |
1289 | bundle->mirror_out |= mirror_bit; | |
1290 | } else { | |
1291 | bundle->mirror_out &= ~mirror_bit; | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | ofproto->need_revalidate = true; | |
1296 | mac_learning_flush(ofproto->ml); | |
1297 | ||
1298 | return 0; | |
1299 | } | |
1300 | ||
1301 | static void | |
1302 | mirror_destroy(struct ofmirror *mirror) | |
1303 | { | |
1304 | struct ofproto_dpif *ofproto; | |
1305 | mirror_mask_t mirror_bit; | |
1306 | struct ofbundle *bundle; | |
1307 | ||
1308 | if (!mirror) { | |
1309 | return; | |
1310 | } | |
1311 | ||
1312 | ofproto = mirror->ofproto; | |
1313 | ofproto->need_revalidate = true; | |
1314 | mac_learning_flush(ofproto->ml); | |
1315 | ||
1316 | mirror_bit = MIRROR_MASK_C(1) << mirror->idx; | |
1317 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
1318 | bundle->src_mirrors &= ~mirror_bit; | |
1319 | bundle->dst_mirrors &= ~mirror_bit; | |
1320 | bundle->mirror_out &= ~mirror_bit; | |
1321 | } | |
1322 | ||
1323 | hmapx_destroy(&mirror->srcs); | |
1324 | hmapx_destroy(&mirror->dsts); | |
1325 | free(mirror->vlans); | |
1326 | ||
1327 | ofproto->mirrors[mirror->idx] = NULL; | |
1328 | free(mirror->name); | |
1329 | free(mirror); | |
1330 | } | |
1331 | ||
1332 | static int | |
1333 | set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans) | |
1334 | { | |
1335 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1336 | if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) { | |
1337 | ofproto->need_revalidate = true; | |
1338 | mac_learning_flush(ofproto->ml); | |
1339 | } | |
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | static bool | |
1344 | is_mirror_output_bundle(struct ofproto *ofproto_, void *aux) | |
1345 | { | |
1346 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1347 | struct ofbundle *bundle = bundle_lookup(ofproto, aux); | |
1348 | return bundle && bundle->mirror_out != 0; | |
1349 | } | |
1350 | \f | |
1351 | /* Ports. */ | |
1352 | ||
1353 | static struct ofport_dpif * | |
1354 | get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port) | |
1355 | { | |
7df6a8bd BP |
1356 | struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port); |
1357 | return ofport ? ofport_dpif_cast(ofport) : NULL; | |
abe529af BP |
1358 | } |
1359 | ||
1360 | static struct ofport_dpif * | |
1361 | get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port) | |
1362 | { | |
1363 | return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port)); | |
1364 | } | |
1365 | ||
1366 | static void | |
1367 | ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port, | |
1368 | struct dpif_port *dpif_port) | |
1369 | { | |
1370 | ofproto_port->name = dpif_port->name; | |
1371 | ofproto_port->type = dpif_port->type; | |
1372 | ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no); | |
1373 | } | |
1374 | ||
1375 | static void | |
1376 | port_run(struct ofport_dpif *ofport) | |
1377 | { | |
1378 | if (ofport->cfm) { | |
1379 | cfm_run(ofport->cfm); | |
1380 | ||
1381 | if (cfm_should_send_ccm(ofport->cfm)) { | |
1382 | struct ofpbuf packet; | |
1383 | struct ccm *ccm; | |
1384 | ||
1385 | ofpbuf_init(&packet, 0); | |
1386 | ccm = eth_compose(&packet, eth_addr_ccm, ofport->up.opp.hw_addr, | |
1387 | ETH_TYPE_CFM, sizeof *ccm); | |
1388 | cfm_compose_ccm(ofport->cfm, ccm); | |
1389 | send_packet(ofproto_dpif_cast(ofport->up.ofproto), | |
1390 | ofport->odp_port, 0, &packet); | |
1391 | ofpbuf_uninit(&packet); | |
1392 | } | |
1393 | } | |
1394 | } | |
1395 | ||
1396 | static void | |
1397 | port_wait(struct ofport_dpif *ofport) | |
1398 | { | |
1399 | if (ofport->cfm) { | |
1400 | cfm_wait(ofport->cfm); | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | static int | |
1405 | port_query_by_name(const struct ofproto *ofproto_, const char *devname, | |
1406 | struct ofproto_port *ofproto_port) | |
1407 | { | |
1408 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1409 | struct dpif_port dpif_port; | |
1410 | int error; | |
1411 | ||
1412 | error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port); | |
1413 | if (!error) { | |
1414 | ofproto_port_from_dpif_port(ofproto_port, &dpif_port); | |
1415 | } | |
1416 | return error; | |
1417 | } | |
1418 | ||
1419 | static int | |
1420 | port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp) | |
1421 | { | |
1422 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1423 | uint16_t odp_port; | |
1424 | int error; | |
1425 | ||
1426 | error = dpif_port_add(ofproto->dpif, netdev, &odp_port); | |
1427 | if (!error) { | |
1428 | *ofp_portp = odp_port_to_ofp_port(odp_port); | |
1429 | } | |
1430 | return error; | |
1431 | } | |
1432 | ||
1433 | static int | |
1434 | port_del(struct ofproto *ofproto_, uint16_t ofp_port) | |
1435 | { | |
1436 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1437 | int error; | |
1438 | ||
1439 | error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port)); | |
1440 | if (!error) { | |
1441 | struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port); | |
1442 | if (ofport) { | |
1443 | /* The caller is going to close ofport->up.netdev. If this is a | |
1444 | * bonded port, then the bond is using that netdev, so remove it | |
1445 | * from the bond. The client will need to reconfigure everything | |
1446 | * after deleting ports, so then the slave will get re-added. */ | |
1447 | bundle_remove(&ofport->up); | |
1448 | } | |
1449 | } | |
1450 | return error; | |
1451 | } | |
1452 | ||
1453 | struct port_dump_state { | |
1454 | struct dpif_port_dump dump; | |
1455 | bool done; | |
1456 | }; | |
1457 | ||
1458 | static int | |
1459 | port_dump_start(const struct ofproto *ofproto_, void **statep) | |
1460 | { | |
1461 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1462 | struct port_dump_state *state; | |
1463 | ||
1464 | *statep = state = xmalloc(sizeof *state); | |
1465 | dpif_port_dump_start(&state->dump, ofproto->dpif); | |
1466 | state->done = false; | |
1467 | return 0; | |
1468 | } | |
1469 | ||
1470 | static int | |
1471 | port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_, | |
1472 | struct ofproto_port *port) | |
1473 | { | |
1474 | struct port_dump_state *state = state_; | |
1475 | struct dpif_port dpif_port; | |
1476 | ||
1477 | if (dpif_port_dump_next(&state->dump, &dpif_port)) { | |
1478 | ofproto_port_from_dpif_port(port, &dpif_port); | |
1479 | return 0; | |
1480 | } else { | |
1481 | int error = dpif_port_dump_done(&state->dump); | |
1482 | state->done = true; | |
1483 | return error ? error : EOF; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | static int | |
1488 | port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_) | |
1489 | { | |
1490 | struct port_dump_state *state = state_; | |
1491 | ||
1492 | if (!state->done) { | |
1493 | dpif_port_dump_done(&state->dump); | |
1494 | } | |
1495 | free(state); | |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | static int | |
1500 | port_poll(const struct ofproto *ofproto_, char **devnamep) | |
1501 | { | |
1502 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1503 | return dpif_port_poll(ofproto->dpif, devnamep); | |
1504 | } | |
1505 | ||
1506 | static void | |
1507 | port_poll_wait(const struct ofproto *ofproto_) | |
1508 | { | |
1509 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1510 | dpif_port_poll_wait(ofproto->dpif); | |
1511 | } | |
1512 | ||
1513 | static int | |
1514 | port_is_lacp_current(const struct ofport *ofport_) | |
1515 | { | |
1516 | const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
1517 | return (ofport->bundle && ofport->bundle->lacp | |
1518 | ? lacp_slave_is_current(ofport->bundle->lacp, ofport) | |
1519 | : -1); | |
1520 | } | |
1521 | \f | |
1522 | /* Upcall handling. */ | |
1523 | ||
1524 | /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an | |
1525 | * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to | |
1526 | * their individual configurations. | |
1527 | * | |
1528 | * If 'clone' is true, the caller retains ownership of 'upcall->packet'. | |
1529 | * Otherwise, ownership is transferred to this function. */ | |
1530 | static void | |
1531 | send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall, | |
1532 | const struct flow *flow, bool clone) | |
1533 | { | |
1534 | struct ofputil_packet_in pin; | |
1535 | ||
1536 | pin.packet = upcall->packet; | |
1537 | pin.in_port = flow->in_port; | |
1538 | pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION; | |
1539 | pin.buffer_id = 0; /* not yet known */ | |
1540 | pin.send_len = upcall->userdata; | |
78bd1cd0 | 1541 | connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow, |
abe529af BP |
1542 | clone ? NULL : upcall->packet); |
1543 | } | |
1544 | ||
1545 | static bool | |
1546 | process_special(struct ofproto_dpif *ofproto, const struct flow *flow, | |
1547 | const struct ofpbuf *packet) | |
1548 | { | |
1549 | if (cfm_should_process_flow(flow)) { | |
1550 | struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port); | |
1551 | if (ofport && ofport->cfm) { | |
1552 | cfm_process_heartbeat(ofport->cfm, packet); | |
1553 | } | |
1554 | return true; | |
1555 | } else if (flow->dl_type == htons(ETH_TYPE_LACP)) { | |
1556 | struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port); | |
1557 | if (port && port->bundle && port->bundle->lacp) { | |
1558 | const struct lacp_pdu *pdu = parse_lacp_packet(packet); | |
1559 | if (pdu) { | |
1560 | lacp_process_pdu(port->bundle->lacp, port, pdu); | |
1561 | } | |
1562 | return true; | |
1563 | } | |
1564 | } | |
1565 | return false; | |
1566 | } | |
1567 | ||
1568 | static void | |
1569 | handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall) | |
1570 | { | |
1571 | struct facet *facet; | |
1572 | struct flow flow; | |
1573 | ||
1574 | /* Obtain in_port and tun_id, at least. */ | |
1575 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
1576 | ||
1577 | /* Set header pointers in 'flow'. */ | |
1578 | flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow); | |
1579 | ||
1580 | /* Handle 802.1ag and LACP. */ | |
1581 | if (process_special(ofproto, &flow, upcall->packet)) { | |
1582 | ofpbuf_delete(upcall->packet); | |
6c1491fb | 1583 | ofproto->n_matches++; |
abe529af BP |
1584 | return; |
1585 | } | |
1586 | ||
1587 | /* Check with in-band control to see if this packet should be sent | |
1588 | * to the local port regardless of the flow table. */ | |
1589 | if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) { | |
1590 | send_packet(ofproto, OFPP_LOCAL, 0, upcall->packet); | |
1591 | } | |
1592 | ||
1593 | facet = facet_lookup_valid(ofproto, &flow); | |
1594 | if (!facet) { | |
1595 | struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow); | |
1596 | if (!rule) { | |
1597 | /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */ | |
1598 | struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port); | |
1599 | if (port) { | |
1600 | if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) { | |
1601 | COVERAGE_INC(ofproto_dpif_no_packet_in); | |
1602 | /* XXX install 'drop' flow entry */ | |
1603 | ofpbuf_delete(upcall->packet); | |
1604 | return; | |
1605 | } | |
1606 | } else { | |
1607 | VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, | |
1608 | flow.in_port); | |
1609 | } | |
1610 | ||
1611 | send_packet_in(ofproto, upcall, &flow, false); | |
1612 | return; | |
1613 | } | |
1614 | ||
1615 | facet = facet_create(rule, &flow, upcall->packet); | |
1616 | } else if (!facet->may_install) { | |
1617 | /* The facet is not installable, that is, we need to process every | |
1618 | * packet, so process the current packet's actions into 'facet'. */ | |
1619 | facet_make_actions(ofproto, facet, upcall->packet); | |
1620 | } | |
1621 | ||
1622 | if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) { | |
1623 | /* | |
1624 | * Extra-special case for fail-open mode. | |
1625 | * | |
1626 | * We are in fail-open mode and the packet matched the fail-open rule, | |
1627 | * but we are connected to a controller too. We should send the packet | |
1628 | * up to the controller in the hope that it will try to set up a flow | |
1629 | * and thereby allow us to exit fail-open. | |
1630 | * | |
1631 | * See the top-level comment in fail-open.c for more information. | |
1632 | */ | |
1633 | send_packet_in(ofproto, upcall, &flow, true); | |
1634 | } | |
1635 | ||
1636 | facet_execute(ofproto, facet, upcall->packet); | |
1637 | facet_install(ofproto, facet, false); | |
6c1491fb | 1638 | ofproto->n_matches++; |
abe529af BP |
1639 | } |
1640 | ||
1641 | static void | |
1642 | handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall) | |
1643 | { | |
1644 | struct flow flow; | |
1645 | ||
1646 | switch (upcall->type) { | |
1647 | case DPIF_UC_ACTION: | |
1648 | COVERAGE_INC(ofproto_dpif_ctlr_action); | |
1649 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
1650 | send_packet_in(ofproto, upcall, &flow, false); | |
1651 | break; | |
1652 | ||
1653 | case DPIF_UC_SAMPLE: | |
1654 | if (ofproto->sflow) { | |
1655 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
1656 | ofproto_sflow_received(ofproto->sflow, upcall, &flow); | |
1657 | } | |
1658 | ofpbuf_delete(upcall->packet); | |
1659 | break; | |
1660 | ||
1661 | case DPIF_UC_MISS: | |
1662 | handle_miss_upcall(ofproto, upcall); | |
1663 | break; | |
1664 | ||
1665 | case DPIF_N_UC_TYPES: | |
1666 | default: | |
1667 | VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type); | |
1668 | break; | |
1669 | } | |
1670 | } | |
1671 | \f | |
1672 | /* Flow expiration. */ | |
1673 | ||
1674 | static int facet_max_idle(const struct ofproto_dpif *); | |
1675 | static void update_stats(struct ofproto_dpif *); | |
1676 | static void rule_expire(struct rule_dpif *); | |
1677 | static void expire_facets(struct ofproto_dpif *, int dp_max_idle); | |
1678 | ||
1679 | /* This function is called periodically by run(). Its job is to collect | |
1680 | * updates for the flows that have been installed into the datapath, most | |
1681 | * importantly when they last were used, and then use that information to | |
1682 | * expire flows that have not been used recently. | |
1683 | * | |
1684 | * Returns the number of milliseconds after which it should be called again. */ | |
1685 | static int | |
1686 | expire(struct ofproto_dpif *ofproto) | |
1687 | { | |
1688 | struct rule_dpif *rule, *next_rule; | |
1689 | struct cls_cursor cursor; | |
1690 | int dp_max_idle; | |
1691 | ||
1692 | /* Update stats for each flow in the datapath. */ | |
1693 | update_stats(ofproto); | |
1694 | ||
1695 | /* Expire facets that have been idle too long. */ | |
1696 | dp_max_idle = facet_max_idle(ofproto); | |
1697 | expire_facets(ofproto, dp_max_idle); | |
1698 | ||
1699 | /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */ | |
6c1491fb | 1700 | cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL); |
abe529af BP |
1701 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) { |
1702 | rule_expire(rule); | |
1703 | } | |
1704 | ||
1705 | /* All outstanding data in existing flows has been accounted, so it's a | |
1706 | * good time to do bond rebalancing. */ | |
1707 | if (ofproto->has_bonded_bundles) { | |
1708 | struct ofbundle *bundle; | |
1709 | ||
1710 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
1711 | if (bundle->bond) { | |
1712 | bond_rebalance(bundle->bond, &ofproto->revalidate_set); | |
1713 | } | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | return MIN(dp_max_idle, 1000); | |
1718 | } | |
1719 | ||
1720 | /* Update 'packet_count', 'byte_count', and 'used' members of installed facets. | |
1721 | * | |
1722 | * This function also pushes statistics updates to rules which each facet | |
1723 | * resubmits into. Generally these statistics will be accurate. However, if a | |
1724 | * facet changes the rule it resubmits into at some time in between | |
1725 | * update_stats() runs, it is possible that statistics accrued to the | |
1726 | * old rule will be incorrectly attributed to the new rule. This could be | |
1727 | * avoided by calling update_stats() whenever rules are created or | |
1728 | * deleted. However, the performance impact of making so many calls to the | |
1729 | * datapath do not justify the benefit of having perfectly accurate statistics. | |
1730 | */ | |
1731 | static void | |
1732 | update_stats(struct ofproto_dpif *p) | |
1733 | { | |
1734 | const struct dpif_flow_stats *stats; | |
1735 | struct dpif_flow_dump dump; | |
1736 | const struct nlattr *key; | |
1737 | size_t key_len; | |
1738 | ||
1739 | dpif_flow_dump_start(&dump, p->dpif); | |
1740 | while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) { | |
1741 | struct facet *facet; | |
1742 | struct flow flow; | |
1743 | ||
1744 | if (odp_flow_key_to_flow(key, key_len, &flow)) { | |
1745 | struct ds s; | |
1746 | ||
1747 | ds_init(&s); | |
1748 | odp_flow_key_format(key, key_len, &s); | |
1749 | VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s", | |
1750 | ds_cstr(&s)); | |
1751 | ds_destroy(&s); | |
1752 | ||
1753 | continue; | |
1754 | } | |
1755 | facet = facet_find(p, &flow); | |
1756 | ||
1757 | if (facet && facet->installed) { | |
1758 | ||
1759 | if (stats->n_packets >= facet->dp_packet_count) { | |
1760 | uint64_t extra = stats->n_packets - facet->dp_packet_count; | |
1761 | facet->packet_count += extra; | |
1762 | } else { | |
1763 | VLOG_WARN_RL(&rl, "unexpected packet count from the datapath"); | |
1764 | } | |
1765 | ||
1766 | if (stats->n_bytes >= facet->dp_byte_count) { | |
1767 | facet->byte_count += stats->n_bytes - facet->dp_byte_count; | |
1768 | } else { | |
1769 | VLOG_WARN_RL(&rl, "unexpected byte count from datapath"); | |
1770 | } | |
1771 | ||
1772 | facet->dp_packet_count = stats->n_packets; | |
1773 | facet->dp_byte_count = stats->n_bytes; | |
1774 | ||
1775 | facet_update_time(p, facet, stats->used); | |
1776 | facet_account(p, facet, stats->n_bytes); | |
1777 | facet_push_stats(facet); | |
1778 | } else { | |
1779 | /* There's a flow in the datapath that we know nothing about. | |
1780 | * Delete it. */ | |
1781 | COVERAGE_INC(facet_unexpected); | |
1782 | dpif_flow_del(p->dpif, key, key_len, NULL); | |
1783 | } | |
1784 | } | |
1785 | dpif_flow_dump_done(&dump); | |
1786 | } | |
1787 | ||
1788 | /* Calculates and returns the number of milliseconds of idle time after which | |
1789 | * facets should expire from the datapath and we should fold their statistics | |
1790 | * into their parent rules in userspace. */ | |
1791 | static int | |
1792 | facet_max_idle(const struct ofproto_dpif *ofproto) | |
1793 | { | |
1794 | /* | |
1795 | * Idle time histogram. | |
1796 | * | |
1797 | * Most of the time a switch has a relatively small number of facets. When | |
1798 | * this is the case we might as well keep statistics for all of them in | |
1799 | * userspace and to cache them in the kernel datapath for performance as | |
1800 | * well. | |
1801 | * | |
1802 | * As the number of facets increases, the memory required to maintain | |
1803 | * statistics about them in userspace and in the kernel becomes | |
1804 | * significant. However, with a large number of facets it is likely that | |
1805 | * only a few of them are "heavy hitters" that consume a large amount of | |
1806 | * bandwidth. At this point, only heavy hitters are worth caching in the | |
1807 | * kernel and maintaining in userspaces; other facets we can discard. | |
1808 | * | |
1809 | * The technique used to compute the idle time is to build a histogram with | |
1810 | * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet | |
1811 | * that is installed in the kernel gets dropped in the appropriate bucket. | |
1812 | * After the histogram has been built, we compute the cutoff so that only | |
1813 | * the most-recently-used 1% of facets (but at least 1000 flows) are kept | |
1814 | * cached. At least the most-recently-used bucket of facets is kept, so | |
1815 | * actually an arbitrary number of facets can be kept in any given | |
1816 | * expiration run (though the next run will delete most of those unless | |
1817 | * they receive additional data). | |
1818 | * | |
1819 | * This requires a second pass through the facets, in addition to the pass | |
1820 | * made by update_stats(), because the former function never looks | |
1821 | * at uninstallable facets. | |
1822 | */ | |
1823 | enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) }; | |
1824 | enum { N_BUCKETS = 5000 / BUCKET_WIDTH }; | |
1825 | int buckets[N_BUCKETS] = { 0 }; | |
1826 | struct facet *facet; | |
1827 | int total, bucket; | |
1828 | long long int now; | |
1829 | int i; | |
1830 | ||
1831 | total = hmap_count(&ofproto->facets); | |
1832 | if (total <= 1000) { | |
1833 | return N_BUCKETS * BUCKET_WIDTH; | |
1834 | } | |
1835 | ||
1836 | /* Build histogram. */ | |
1837 | now = time_msec(); | |
1838 | HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) { | |
1839 | long long int idle = now - facet->used; | |
1840 | int bucket = (idle <= 0 ? 0 | |
1841 | : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1 | |
1842 | : (unsigned int) idle / BUCKET_WIDTH); | |
1843 | buckets[bucket]++; | |
1844 | } | |
1845 | ||
1846 | /* Find the first bucket whose flows should be expired. */ | |
1847 | for (bucket = 0; bucket < N_BUCKETS; bucket++) { | |
1848 | if (buckets[bucket]) { | |
1849 | int subtotal = 0; | |
1850 | do { | |
1851 | subtotal += buckets[bucket++]; | |
1852 | } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100)); | |
1853 | break; | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | if (VLOG_IS_DBG_ENABLED()) { | |
1858 | struct ds s; | |
1859 | ||
1860 | ds_init(&s); | |
1861 | ds_put_cstr(&s, "keep"); | |
1862 | for (i = 0; i < N_BUCKETS; i++) { | |
1863 | if (i == bucket) { | |
1864 | ds_put_cstr(&s, ", drop"); | |
1865 | } | |
1866 | if (buckets[i]) { | |
1867 | ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]); | |
1868 | } | |
1869 | } | |
1870 | VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s)); | |
1871 | ds_destroy(&s); | |
1872 | } | |
1873 | ||
1874 | return bucket * BUCKET_WIDTH; | |
1875 | } | |
1876 | ||
1877 | static void | |
1878 | facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet) | |
1879 | { | |
1880 | if (ofproto->netflow && !facet_is_controller_flow(facet) && | |
1881 | netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) { | |
1882 | struct ofexpired expired; | |
1883 | ||
1884 | if (facet->installed) { | |
1885 | struct dpif_flow_stats stats; | |
1886 | ||
1887 | facet_put__(ofproto, facet, facet->actions, facet->actions_len, | |
1888 | &stats); | |
1889 | facet_update_stats(ofproto, facet, &stats); | |
1890 | } | |
1891 | ||
1892 | expired.flow = facet->flow; | |
1893 | expired.packet_count = facet->packet_count; | |
1894 | expired.byte_count = facet->byte_count; | |
1895 | expired.used = facet->used; | |
1896 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | static void | |
1901 | expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle) | |
1902 | { | |
1903 | long long int cutoff = time_msec() - dp_max_idle; | |
1904 | struct facet *facet, *next_facet; | |
1905 | ||
1906 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
1907 | facet_active_timeout(ofproto, facet); | |
1908 | if (facet->used < cutoff) { | |
1909 | facet_remove(ofproto, facet); | |
1910 | } | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules, | |
1915 | * then delete it entirely. */ | |
1916 | static void | |
1917 | rule_expire(struct rule_dpif *rule) | |
1918 | { | |
1919 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
1920 | struct facet *facet, *next_facet; | |
1921 | long long int now; | |
1922 | uint8_t reason; | |
1923 | ||
1924 | /* Has 'rule' expired? */ | |
1925 | now = time_msec(); | |
1926 | if (rule->up.hard_timeout | |
1927 | && now > rule->up.created + rule->up.hard_timeout * 1000) { | |
1928 | reason = OFPRR_HARD_TIMEOUT; | |
1929 | } else if (rule->up.idle_timeout && list_is_empty(&rule->facets) | |
1930 | && now > rule->used + rule->up.idle_timeout * 1000) { | |
1931 | reason = OFPRR_IDLE_TIMEOUT; | |
1932 | } else { | |
1933 | return; | |
1934 | } | |
1935 | ||
1936 | COVERAGE_INC(ofproto_dpif_expired); | |
1937 | ||
1938 | /* Update stats. (This is a no-op if the rule expired due to an idle | |
1939 | * timeout, because that only happens when the rule has no facets left.) */ | |
1940 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { | |
1941 | facet_remove(ofproto, facet); | |
1942 | } | |
1943 | ||
1944 | /* Get rid of the rule. */ | |
1945 | ofproto_rule_expire(&rule->up, reason); | |
1946 | } | |
1947 | \f | |
1948 | /* Facets. */ | |
1949 | ||
1950 | /* Creates and returns a new facet owned by 'rule', given a 'flow' and an | |
1951 | * example 'packet' within that flow. | |
1952 | * | |
1953 | * The caller must already have determined that no facet with an identical | |
1954 | * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in | |
1955 | * the ofproto's classifier table. */ | |
1956 | static struct facet * | |
1957 | facet_create(struct rule_dpif *rule, const struct flow *flow, | |
1958 | const struct ofpbuf *packet) | |
1959 | { | |
1960 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
1961 | struct facet *facet; | |
1962 | ||
1963 | facet = xzalloc(sizeof *facet); | |
1964 | facet->used = time_msec(); | |
1965 | hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0)); | |
1966 | list_push_back(&rule->facets, &facet->list_node); | |
1967 | facet->rule = rule; | |
1968 | facet->flow = *flow; | |
1969 | netflow_flow_init(&facet->nf_flow); | |
1970 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used); | |
1971 | ||
1972 | facet_make_actions(ofproto, facet, packet); | |
1973 | ||
1974 | return facet; | |
1975 | } | |
1976 | ||
1977 | static void | |
1978 | facet_free(struct facet *facet) | |
1979 | { | |
1980 | free(facet->actions); | |
1981 | free(facet); | |
1982 | } | |
1983 | ||
1984 | /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on | |
1985 | * 'packet', which arrived on 'in_port'. | |
1986 | * | |
1987 | * Takes ownership of 'packet'. */ | |
1988 | static bool | |
1989 | execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow, | |
1990 | const struct nlattr *odp_actions, size_t actions_len, | |
1991 | struct ofpbuf *packet) | |
1992 | { | |
1993 | if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t)) | |
1994 | && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) { | |
1995 | /* As an optimization, avoid a round-trip from userspace to kernel to | |
1996 | * userspace. This also avoids possibly filling up kernel packet | |
1997 | * buffers along the way. */ | |
1998 | struct dpif_upcall upcall; | |
1999 | ||
2000 | upcall.type = DPIF_UC_ACTION; | |
2001 | upcall.packet = packet; | |
2002 | upcall.key = NULL; | |
2003 | upcall.key_len = 0; | |
2004 | upcall.userdata = nl_attr_get_u64(odp_actions); | |
2005 | upcall.sample_pool = 0; | |
2006 | upcall.actions = NULL; | |
2007 | upcall.actions_len = 0; | |
2008 | ||
2009 | send_packet_in(ofproto, &upcall, flow, false); | |
2010 | ||
2011 | return true; | |
2012 | } else { | |
2013 | int error; | |
2014 | ||
2015 | error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet); | |
2016 | ofpbuf_delete(packet); | |
2017 | return !error; | |
2018 | } | |
2019 | } | |
2020 | ||
2021 | /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s | |
2022 | * statistics appropriately. 'packet' must have at least sizeof(struct | |
2023 | * ofp_packet_in) bytes of headroom. | |
2024 | * | |
2025 | * For correct results, 'packet' must actually be in 'facet''s flow; that is, | |
2026 | * applying flow_extract() to 'packet' would yield the same flow as | |
2027 | * 'facet->flow'. | |
2028 | * | |
2029 | * 'facet' must have accurately composed ODP actions; that is, it must not be | |
2030 | * in need of revalidation. | |
2031 | * | |
2032 | * Takes ownership of 'packet'. */ | |
2033 | static void | |
2034 | facet_execute(struct ofproto_dpif *ofproto, struct facet *facet, | |
2035 | struct ofpbuf *packet) | |
2036 | { | |
2037 | struct dpif_flow_stats stats; | |
2038 | ||
2039 | assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in)); | |
2040 | ||
2041 | flow_extract_stats(&facet->flow, packet, &stats); | |
2042 | stats.used = time_msec(); | |
2043 | if (execute_odp_actions(ofproto, &facet->flow, | |
2044 | facet->actions, facet->actions_len, packet)) { | |
2045 | facet_update_stats(ofproto, facet, &stats); | |
2046 | } | |
2047 | } | |
2048 | ||
2049 | /* Remove 'facet' from 'ofproto' and free up the associated memory: | |
2050 | * | |
2051 | * - If 'facet' was installed in the datapath, uninstalls it and updates its | |
2052 | * rule's statistics, via facet_uninstall(). | |
2053 | * | |
2054 | * - Removes 'facet' from its rule and from ofproto->facets. | |
2055 | */ | |
2056 | static void | |
2057 | facet_remove(struct ofproto_dpif *ofproto, struct facet *facet) | |
2058 | { | |
2059 | facet_uninstall(ofproto, facet); | |
2060 | facet_flush_stats(ofproto, facet); | |
2061 | hmap_remove(&ofproto->facets, &facet->hmap_node); | |
2062 | list_remove(&facet->list_node); | |
2063 | facet_free(facet); | |
2064 | } | |
2065 | ||
2066 | /* Composes the ODP actions for 'facet' based on its rule's actions. */ | |
2067 | static void | |
2068 | facet_make_actions(struct ofproto_dpif *p, struct facet *facet, | |
2069 | const struct ofpbuf *packet) | |
2070 | { | |
2071 | const struct rule_dpif *rule = facet->rule; | |
2072 | struct ofpbuf *odp_actions; | |
2073 | struct action_xlate_ctx ctx; | |
2074 | ||
2075 | action_xlate_ctx_init(&ctx, p, &facet->flow, packet); | |
2076 | odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions); | |
2077 | facet->tags = ctx.tags; | |
2078 | facet->may_install = ctx.may_set_up_flow; | |
2079 | facet->nf_flow.output_iface = ctx.nf_output_iface; | |
2080 | ||
2081 | if (facet->actions_len != odp_actions->size | |
2082 | || memcmp(facet->actions, odp_actions->data, odp_actions->size)) { | |
2083 | free(facet->actions); | |
2084 | facet->actions_len = odp_actions->size; | |
2085 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
2086 | } | |
2087 | ||
2088 | ofpbuf_delete(odp_actions); | |
2089 | } | |
2090 | ||
2091 | static int | |
2092 | facet_put__(struct ofproto_dpif *ofproto, struct facet *facet, | |
2093 | const struct nlattr *actions, size_t actions_len, | |
2094 | struct dpif_flow_stats *stats) | |
2095 | { | |
2096 | struct odputil_keybuf keybuf; | |
2097 | enum dpif_flow_put_flags flags; | |
2098 | struct ofpbuf key; | |
2099 | ||
2100 | flags = DPIF_FP_CREATE | DPIF_FP_MODIFY; | |
2101 | if (stats) { | |
2102 | flags |= DPIF_FP_ZERO_STATS; | |
2103 | facet->dp_packet_count = 0; | |
2104 | facet->dp_byte_count = 0; | |
2105 | } | |
2106 | ||
2107 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2108 | odp_flow_key_from_flow(&key, &facet->flow); | |
2109 | ||
2110 | return dpif_flow_put(ofproto->dpif, flags, key.data, key.size, | |
2111 | actions, actions_len, stats); | |
2112 | } | |
2113 | ||
2114 | /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If | |
2115 | * 'zero_stats' is true, clears any existing statistics from the datapath for | |
2116 | * 'facet'. */ | |
2117 | static void | |
2118 | facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats) | |
2119 | { | |
2120 | struct dpif_flow_stats stats; | |
2121 | ||
2122 | if (facet->may_install | |
2123 | && !facet_put__(p, facet, facet->actions, facet->actions_len, | |
2124 | zero_stats ? &stats : NULL)) { | |
2125 | facet->installed = true; | |
2126 | } | |
2127 | } | |
2128 | ||
2129 | static void | |
2130 | facet_account(struct ofproto_dpif *ofproto, | |
2131 | struct facet *facet, uint64_t extra_bytes) | |
2132 | { | |
2133 | uint64_t total_bytes, n_bytes; | |
2134 | struct ofbundle *in_bundle; | |
2135 | const struct nlattr *a; | |
2136 | tag_type dummy = 0; | |
2137 | unsigned int left; | |
2138 | int vlan; | |
2139 | ||
2140 | total_bytes = facet->byte_count + extra_bytes; | |
2141 | if (total_bytes <= facet->accounted_bytes) { | |
2142 | return; | |
2143 | } | |
2144 | n_bytes = total_bytes - facet->accounted_bytes; | |
2145 | facet->accounted_bytes = total_bytes; | |
2146 | ||
2147 | /* Test that 'tags' is nonzero to ensure that only flows that include an | |
2148 | * OFPP_NORMAL action are used for learning and bond slave rebalancing. | |
2149 | * This works because OFPP_NORMAL always sets a nonzero tag value. | |
2150 | * | |
2151 | * Feed information from the active flows back into the learning table to | |
2152 | * ensure that table is always in sync with what is actually flowing | |
2153 | * through the datapath. */ | |
2154 | if (!facet->tags | |
2155 | || !is_admissible(ofproto, &facet->flow, false, &dummy, | |
2156 | &vlan, &in_bundle)) { | |
2157 | return; | |
2158 | } | |
2159 | ||
2160 | update_learning_table(ofproto, &facet->flow, vlan, in_bundle); | |
2161 | ||
2162 | if (!ofproto->has_bonded_bundles) { | |
2163 | return; | |
2164 | } | |
2165 | NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) { | |
2166 | if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) { | |
2167 | struct ofport_dpif *port; | |
2168 | ||
2169 | port = get_odp_port(ofproto, nl_attr_get_u32(a)); | |
2170 | if (port && port->bundle && port->bundle->bond) { | |
2171 | bond_account(port->bundle->bond, &facet->flow, vlan, n_bytes); | |
2172 | } | |
2173 | } | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | /* If 'rule' is installed in the datapath, uninstalls it. */ | |
2178 | static void | |
2179 | facet_uninstall(struct ofproto_dpif *p, struct facet *facet) | |
2180 | { | |
2181 | if (facet->installed) { | |
2182 | struct odputil_keybuf keybuf; | |
2183 | struct dpif_flow_stats stats; | |
2184 | struct ofpbuf key; | |
2185 | ||
2186 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2187 | odp_flow_key_from_flow(&key, &facet->flow); | |
2188 | ||
2189 | if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) { | |
2190 | facet_update_stats(p, facet, &stats); | |
2191 | } | |
2192 | facet->installed = false; | |
2193 | facet->dp_packet_count = 0; | |
2194 | facet->dp_byte_count = 0; | |
2195 | } else { | |
2196 | assert(facet->dp_packet_count == 0); | |
2197 | assert(facet->dp_byte_count == 0); | |
2198 | } | |
2199 | } | |
2200 | ||
2201 | /* Returns true if the only action for 'facet' is to send to the controller. | |
2202 | * (We don't report NetFlow expiration messages for such facets because they | |
2203 | * are just part of the control logic for the network, not real traffic). */ | |
2204 | static bool | |
2205 | facet_is_controller_flow(struct facet *facet) | |
2206 | { | |
2207 | return (facet | |
2208 | && facet->rule->up.n_actions == 1 | |
2209 | && action_outputs_to_port(&facet->rule->up.actions[0], | |
2210 | htons(OFPP_CONTROLLER))); | |
2211 | } | |
2212 | ||
2213 | /* Folds all of 'facet''s statistics into its rule. Also updates the | |
2214 | * accounting ofhook and emits a NetFlow expiration if appropriate. All of | |
2215 | * 'facet''s statistics in the datapath should have been zeroed and folded into | |
2216 | * its packet and byte counts before this function is called. */ | |
2217 | static void | |
2218 | facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet) | |
2219 | { | |
2220 | assert(!facet->dp_byte_count); | |
2221 | assert(!facet->dp_packet_count); | |
2222 | ||
2223 | facet_push_stats(facet); | |
2224 | facet_account(ofproto, facet, 0); | |
2225 | ||
2226 | if (ofproto->netflow && !facet_is_controller_flow(facet)) { | |
2227 | struct ofexpired expired; | |
2228 | expired.flow = facet->flow; | |
2229 | expired.packet_count = facet->packet_count; | |
2230 | expired.byte_count = facet->byte_count; | |
2231 | expired.used = facet->used; | |
2232 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
2233 | } | |
2234 | ||
2235 | facet->rule->packet_count += facet->packet_count; | |
2236 | facet->rule->byte_count += facet->byte_count; | |
2237 | ||
2238 | /* Reset counters to prevent double counting if 'facet' ever gets | |
2239 | * reinstalled. */ | |
2240 | facet->packet_count = 0; | |
2241 | facet->byte_count = 0; | |
2242 | facet->rs_packet_count = 0; | |
2243 | facet->rs_byte_count = 0; | |
2244 | facet->accounted_bytes = 0; | |
2245 | ||
2246 | netflow_flow_clear(&facet->nf_flow); | |
2247 | } | |
2248 | ||
2249 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
2250 | * Returns it if found, otherwise a null pointer. | |
2251 | * | |
2252 | * The returned facet might need revalidation; use facet_lookup_valid() | |
2253 | * instead if that is important. */ | |
2254 | static struct facet * | |
2255 | facet_find(struct ofproto_dpif *ofproto, const struct flow *flow) | |
2256 | { | |
2257 | struct facet *facet; | |
2258 | ||
2259 | HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0), | |
2260 | &ofproto->facets) { | |
2261 | if (flow_equal(flow, &facet->flow)) { | |
2262 | return facet; | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | return NULL; | |
2267 | } | |
2268 | ||
2269 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
2270 | * Returns it if found, otherwise a null pointer. | |
2271 | * | |
2272 | * The returned facet is guaranteed to be valid. */ | |
2273 | static struct facet * | |
2274 | facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow) | |
2275 | { | |
2276 | struct facet *facet = facet_find(ofproto, flow); | |
2277 | ||
2278 | /* The facet we found might not be valid, since we could be in need of | |
2279 | * revalidation. If it is not valid, don't return it. */ | |
2280 | if (facet | |
2281 | && ofproto->need_revalidate | |
2282 | && !facet_revalidate(ofproto, facet)) { | |
2283 | COVERAGE_INC(facet_invalidated); | |
2284 | return NULL; | |
2285 | } | |
2286 | ||
2287 | return facet; | |
2288 | } | |
2289 | ||
2290 | /* Re-searches 'ofproto''s classifier for a rule matching 'facet': | |
2291 | * | |
2292 | * - If the rule found is different from 'facet''s current rule, moves | |
2293 | * 'facet' to the new rule and recompiles its actions. | |
2294 | * | |
2295 | * - If the rule found is the same as 'facet''s current rule, leaves 'facet' | |
2296 | * where it is and recompiles its actions anyway. | |
2297 | * | |
2298 | * - If there is none, destroys 'facet'. | |
2299 | * | |
2300 | * Returns true if 'facet' still exists, false if it has been destroyed. */ | |
2301 | static bool | |
2302 | facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet) | |
2303 | { | |
2304 | struct action_xlate_ctx ctx; | |
2305 | struct ofpbuf *odp_actions; | |
2306 | struct rule_dpif *new_rule; | |
2307 | bool actions_changed; | |
2308 | ||
2309 | COVERAGE_INC(facet_revalidate); | |
2310 | ||
2311 | /* Determine the new rule. */ | |
2312 | new_rule = rule_dpif_lookup(ofproto, &facet->flow); | |
2313 | if (!new_rule) { | |
2314 | /* No new rule, so delete the facet. */ | |
2315 | facet_remove(ofproto, facet); | |
2316 | return false; | |
2317 | } | |
2318 | ||
2319 | /* Calculate new ODP actions. | |
2320 | * | |
2321 | * We do not modify any 'facet' state yet, because we might need to, e.g., | |
2322 | * emit a NetFlow expiration and, if so, we need to have the old state | |
2323 | * around to properly compose it. */ | |
2324 | action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); | |
2325 | odp_actions = xlate_actions(&ctx, | |
2326 | new_rule->up.actions, new_rule->up.n_actions); | |
2327 | actions_changed = (facet->actions_len != odp_actions->size | |
2328 | || memcmp(facet->actions, odp_actions->data, | |
2329 | facet->actions_len)); | |
2330 | ||
2331 | /* If the ODP actions changed or the installability changed, then we need | |
2332 | * to talk to the datapath. */ | |
2333 | if (actions_changed || ctx.may_set_up_flow != facet->installed) { | |
2334 | if (ctx.may_set_up_flow) { | |
2335 | struct dpif_flow_stats stats; | |
2336 | ||
2337 | facet_put__(ofproto, facet, | |
2338 | odp_actions->data, odp_actions->size, &stats); | |
2339 | facet_update_stats(ofproto, facet, &stats); | |
2340 | } else { | |
2341 | facet_uninstall(ofproto, facet); | |
2342 | } | |
2343 | ||
2344 | /* The datapath flow is gone or has zeroed stats, so push stats out of | |
2345 | * 'facet' into 'rule'. */ | |
2346 | facet_flush_stats(ofproto, facet); | |
2347 | } | |
2348 | ||
2349 | /* Update 'facet' now that we've taken care of all the old state. */ | |
2350 | facet->tags = ctx.tags; | |
2351 | facet->nf_flow.output_iface = ctx.nf_output_iface; | |
2352 | facet->may_install = ctx.may_set_up_flow; | |
2353 | if (actions_changed) { | |
2354 | free(facet->actions); | |
2355 | facet->actions_len = odp_actions->size; | |
2356 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
2357 | } | |
2358 | if (facet->rule != new_rule) { | |
2359 | COVERAGE_INC(facet_changed_rule); | |
2360 | list_remove(&facet->list_node); | |
2361 | list_push_back(&new_rule->facets, &facet->list_node); | |
2362 | facet->rule = new_rule; | |
2363 | facet->used = new_rule->up.created; | |
2364 | facet->rs_used = facet->used; | |
2365 | } | |
2366 | ||
2367 | ofpbuf_delete(odp_actions); | |
2368 | ||
2369 | return true; | |
2370 | } | |
2371 | ||
2372 | /* Updates 'facet''s used time. Caller is responsible for calling | |
2373 | * facet_push_stats() to update the flows which 'facet' resubmits into. */ | |
2374 | static void | |
2375 | facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet, | |
2376 | long long int used) | |
2377 | { | |
2378 | if (used > facet->used) { | |
2379 | facet->used = used; | |
2380 | if (used > facet->rule->used) { | |
2381 | facet->rule->used = used; | |
2382 | } | |
2383 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used); | |
2384 | } | |
2385 | } | |
2386 | ||
2387 | /* Folds the statistics from 'stats' into the counters in 'facet'. | |
2388 | * | |
2389 | * Because of the meaning of a facet's counters, it only makes sense to do this | |
2390 | * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a | |
2391 | * packet that was sent by hand or if it represents statistics that have been | |
2392 | * cleared out of the datapath. */ | |
2393 | static void | |
2394 | facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet, | |
2395 | const struct dpif_flow_stats *stats) | |
2396 | { | |
2397 | if (stats->n_packets || stats->used > facet->used) { | |
2398 | facet_update_time(ofproto, facet, stats->used); | |
2399 | facet->packet_count += stats->n_packets; | |
2400 | facet->byte_count += stats->n_bytes; | |
2401 | facet_push_stats(facet); | |
2402 | netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags); | |
2403 | } | |
2404 | } | |
2405 | ||
2406 | static void | |
2407 | facet_push_stats(struct facet *facet) | |
2408 | { | |
2409 | uint64_t rs_packets, rs_bytes; | |
2410 | ||
2411 | assert(facet->packet_count >= facet->rs_packet_count); | |
2412 | assert(facet->byte_count >= facet->rs_byte_count); | |
2413 | assert(facet->used >= facet->rs_used); | |
2414 | ||
2415 | rs_packets = facet->packet_count - facet->rs_packet_count; | |
2416 | rs_bytes = facet->byte_count - facet->rs_byte_count; | |
2417 | ||
2418 | if (rs_packets || rs_bytes || facet->used > facet->rs_used) { | |
2419 | facet->rs_packet_count = facet->packet_count; | |
2420 | facet->rs_byte_count = facet->byte_count; | |
2421 | facet->rs_used = facet->used; | |
2422 | ||
2423 | flow_push_stats(facet->rule, &facet->flow, | |
2424 | rs_packets, rs_bytes, facet->used); | |
2425 | } | |
2426 | } | |
2427 | ||
2428 | struct ofproto_push { | |
2429 | struct action_xlate_ctx ctx; | |
2430 | uint64_t packets; | |
2431 | uint64_t bytes; | |
2432 | long long int used; | |
2433 | }; | |
2434 | ||
2435 | static void | |
2436 | push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule) | |
2437 | { | |
2438 | struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx); | |
2439 | ||
2440 | if (rule) { | |
2441 | rule->packet_count += push->packets; | |
2442 | rule->byte_count += push->bytes; | |
2443 | rule->used = MAX(push->used, rule->used); | |
2444 | } | |
2445 | } | |
2446 | ||
2447 | /* Pushes flow statistics to the rules which 'flow' resubmits into given | |
2448 | * 'rule''s actions. */ | |
2449 | static void | |
2450 | flow_push_stats(const struct rule_dpif *rule, | |
2451 | struct flow *flow, uint64_t packets, uint64_t bytes, | |
2452 | long long int used) | |
2453 | { | |
2454 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2455 | struct ofproto_push push; | |
2456 | ||
2457 | push.packets = packets; | |
2458 | push.bytes = bytes; | |
2459 | push.used = used; | |
2460 | ||
2461 | action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL); | |
2462 | push.ctx.resubmit_hook = push_resubmit; | |
2463 | ofpbuf_delete(xlate_actions(&push.ctx, | |
2464 | rule->up.actions, rule->up.n_actions)); | |
2465 | } | |
2466 | \f | |
2467 | /* Rules. */ | |
2468 | ||
2469 | static struct rule_dpif * | |
2470 | rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow) | |
2471 | { | |
154896e3 | 2472 | return rule_dpif_cast(rule_from_cls_rule( |
6c1491fb BP |
2473 | classifier_lookup(&ofproto->up.tables[0], |
2474 | flow))); | |
abe529af BP |
2475 | } |
2476 | ||
2477 | static struct rule * | |
2478 | rule_alloc(void) | |
2479 | { | |
2480 | struct rule_dpif *rule = xmalloc(sizeof *rule); | |
2481 | return &rule->up; | |
2482 | } | |
2483 | ||
2484 | static void | |
2485 | rule_dealloc(struct rule *rule_) | |
2486 | { | |
2487 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2488 | free(rule); | |
2489 | } | |
2490 | ||
2491 | static int | |
2492 | rule_construct(struct rule *rule_) | |
2493 | { | |
2494 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2495 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
08944c1d | 2496 | struct rule_dpif *old_rule; |
5bf0e941 BP |
2497 | int error; |
2498 | ||
2499 | error = validate_actions(rule->up.actions, rule->up.n_actions, | |
2500 | &rule->up.cr.flow, ofproto->max_ports); | |
2501 | if (error) { | |
2502 | return error; | |
2503 | } | |
abe529af | 2504 | |
08944c1d | 2505 | old_rule = rule_dpif_cast(rule_from_cls_rule(classifier_find_rule_exactly( |
6c1491fb | 2506 | &ofproto->up.tables[0], |
08944c1d BP |
2507 | &rule->up.cr))); |
2508 | if (old_rule) { | |
2509 | ofproto_rule_destroy(&old_rule->up); | |
2510 | } | |
2511 | ||
abe529af BP |
2512 | rule->used = rule->up.created; |
2513 | rule->packet_count = 0; | |
2514 | rule->byte_count = 0; | |
2515 | list_init(&rule->facets); | |
6c1491fb | 2516 | classifier_insert(&ofproto->up.tables[0], &rule->up.cr); |
abe529af | 2517 | |
abe529af BP |
2518 | ofproto->need_revalidate = true; |
2519 | ||
2520 | return 0; | |
2521 | } | |
2522 | ||
2523 | static void | |
2524 | rule_destruct(struct rule *rule_) | |
2525 | { | |
2526 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2527 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2528 | struct facet *facet, *next_facet; | |
2529 | ||
6c1491fb | 2530 | classifier_remove(&ofproto->up.tables[0], &rule->up.cr); |
abe529af BP |
2531 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { |
2532 | facet_revalidate(ofproto, facet); | |
2533 | } | |
abe529af | 2534 | ofproto->need_revalidate = true; |
abe529af BP |
2535 | } |
2536 | ||
2537 | static void | |
2538 | rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes) | |
2539 | { | |
2540 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2541 | struct facet *facet; | |
2542 | ||
2543 | /* Start from historical data for 'rule' itself that are no longer tracked | |
2544 | * in facets. This counts, for example, facets that have expired. */ | |
2545 | *packets = rule->packet_count; | |
2546 | *bytes = rule->byte_count; | |
2547 | ||
2548 | /* Add any statistics that are tracked by facets. This includes | |
2549 | * statistical data recently updated by ofproto_update_stats() as well as | |
2550 | * stats for packets that were executed "by hand" via dpif_execute(). */ | |
2551 | LIST_FOR_EACH (facet, list_node, &rule->facets) { | |
2552 | *packets += facet->packet_count; | |
2553 | *bytes += facet->byte_count; | |
2554 | } | |
2555 | } | |
2556 | ||
5bf0e941 | 2557 | static int |
abe529af BP |
2558 | rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet) |
2559 | { | |
2560 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2561 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2562 | struct action_xlate_ctx ctx; | |
2563 | struct ofpbuf *odp_actions; | |
2564 | struct facet *facet; | |
2565 | size_t size; | |
2566 | ||
2567 | /* First look for a related facet. If we find one, account it to that. */ | |
2568 | facet = facet_lookup_valid(ofproto, flow); | |
2569 | if (facet && facet->rule == rule) { | |
2570 | facet_execute(ofproto, facet, packet); | |
5bf0e941 | 2571 | return 0; |
abe529af BP |
2572 | } |
2573 | ||
2574 | /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then | |
2575 | * create a new facet for it and use that. */ | |
2576 | if (rule_dpif_lookup(ofproto, flow) == rule) { | |
2577 | facet = facet_create(rule, flow, packet); | |
2578 | facet_execute(ofproto, facet, packet); | |
2579 | facet_install(ofproto, facet, true); | |
5bf0e941 | 2580 | return 0; |
abe529af BP |
2581 | } |
2582 | ||
2583 | /* We can't account anything to a facet. If we were to try, then that | |
2584 | * facet would have a non-matching rule, busting our invariants. */ | |
2585 | action_xlate_ctx_init(&ctx, ofproto, flow, packet); | |
2586 | odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions); | |
2587 | size = packet->size; | |
2588 | if (execute_odp_actions(ofproto, flow, odp_actions->data, | |
2589 | odp_actions->size, packet)) { | |
2590 | rule->used = time_msec(); | |
2591 | rule->packet_count++; | |
2592 | rule->byte_count += size; | |
2593 | flow_push_stats(rule, flow, 1, size, rule->used); | |
2594 | } | |
2595 | ofpbuf_delete(odp_actions); | |
5bf0e941 BP |
2596 | |
2597 | return 0; | |
abe529af BP |
2598 | } |
2599 | ||
2600 | static int | |
2601 | rule_modify_actions(struct rule *rule_, | |
2602 | const union ofp_action *actions, size_t n_actions) | |
2603 | { | |
2604 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
2605 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2606 | int error; | |
2607 | ||
2608 | error = validate_actions(actions, n_actions, &rule->up.cr.flow, | |
2609 | ofproto->max_ports); | |
2610 | if (!error) { | |
2611 | ofproto->need_revalidate = true; | |
2612 | } | |
2613 | return error; | |
2614 | } | |
2615 | \f | |
2616 | /* Sends 'packet' out of port 'odp_port' within 'ofproto'. If 'vlan_tci' is | |
2617 | * zero the packet will not have any 802.1Q hader; if it is nonzero, then the | |
2618 | * packet will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'. | |
2619 | * | |
2620 | * Returns 0 if successful, otherwise a positive errno value. */ | |
2621 | static int | |
2622 | send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port, uint16_t vlan_tci, | |
2623 | const struct ofpbuf *packet) | |
2624 | { | |
2625 | struct ofpbuf odp_actions; | |
2626 | int error; | |
2627 | ||
2628 | ofpbuf_init(&odp_actions, 32); | |
2629 | if (vlan_tci != 0) { | |
2630 | nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI, | |
2631 | ntohs(vlan_tci & ~VLAN_CFI)); | |
2632 | } | |
2633 | nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port); | |
2634 | error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size, | |
2635 | packet); | |
2636 | ofpbuf_uninit(&odp_actions); | |
2637 | ||
2638 | if (error) { | |
2639 | VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)", | |
2640 | ofproto->up.name, odp_port, strerror(error)); | |
2641 | } | |
2642 | return error; | |
2643 | } | |
2644 | \f | |
2645 | /* OpenFlow to ODP action translation. */ | |
2646 | ||
2647 | static void do_xlate_actions(const union ofp_action *in, size_t n_in, | |
2648 | struct action_xlate_ctx *ctx); | |
2649 | static bool xlate_normal(struct action_xlate_ctx *); | |
2650 | ||
2651 | static void | |
2652 | add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port) | |
2653 | { | |
2654 | const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port); | |
2655 | uint16_t odp_port = ofp_port_to_odp_port(ofp_port); | |
2656 | ||
2657 | if (ofport) { | |
2658 | if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) { | |
2659 | /* Forwarding disabled on port. */ | |
2660 | return; | |
2661 | } | |
2662 | } else { | |
2663 | /* | |
2664 | * We don't have an ofport record for this port, but it doesn't hurt to | |
2665 | * allow forwarding to it anyhow. Maybe such a port will appear later | |
2666 | * and we're pre-populating the flow table. | |
2667 | */ | |
2668 | } | |
2669 | ||
2670 | nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port); | |
2671 | ctx->nf_output_iface = ofp_port; | |
2672 | } | |
2673 | ||
2674 | static void | |
2675 | xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port) | |
2676 | { | |
2677 | if (ctx->recurse < MAX_RESUBMIT_RECURSION) { | |
2678 | struct rule_dpif *rule; | |
2679 | uint16_t old_in_port; | |
2680 | ||
2681 | /* Look up a flow with 'in_port' as the input port. Then restore the | |
2682 | * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will | |
2683 | * have surprising behavior). */ | |
2684 | old_in_port = ctx->flow.in_port; | |
2685 | ctx->flow.in_port = in_port; | |
2686 | rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow); | |
2687 | ctx->flow.in_port = old_in_port; | |
2688 | ||
2689 | if (ctx->resubmit_hook) { | |
2690 | ctx->resubmit_hook(ctx, rule); | |
2691 | } | |
2692 | ||
2693 | if (rule) { | |
2694 | ctx->recurse++; | |
2695 | do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx); | |
2696 | ctx->recurse--; | |
2697 | } | |
2698 | } else { | |
2699 | static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1); | |
2700 | ||
2701 | VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times", | |
2702 | MAX_RESUBMIT_RECURSION); | |
2703 | } | |
2704 | } | |
2705 | ||
2706 | static void | |
2707 | flood_packets(struct ofproto_dpif *ofproto, | |
2708 | uint16_t ofp_in_port, ovs_be32 mask, | |
2709 | uint16_t *nf_output_iface, struct ofpbuf *odp_actions) | |
2710 | { | |
2711 | struct ofport_dpif *ofport; | |
2712 | ||
2713 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
2714 | uint16_t ofp_port = ofport->up.ofp_port; | |
2715 | if (ofp_port != ofp_in_port && !(ofport->up.opp.config & mask)) { | |
2716 | nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, | |
2717 | ofport->odp_port); | |
2718 | } | |
2719 | } | |
2720 | *nf_output_iface = NF_OUT_FLOOD; | |
2721 | } | |
2722 | ||
2723 | static void | |
2724 | xlate_output_action__(struct action_xlate_ctx *ctx, | |
2725 | uint16_t port, uint16_t max_len) | |
2726 | { | |
2727 | uint16_t prev_nf_output_iface = ctx->nf_output_iface; | |
2728 | ||
2729 | ctx->nf_output_iface = NF_OUT_DROP; | |
2730 | ||
2731 | switch (port) { | |
2732 | case OFPP_IN_PORT: | |
2733 | add_output_action(ctx, ctx->flow.in_port); | |
2734 | break; | |
2735 | case OFPP_TABLE: | |
2736 | xlate_table_action(ctx, ctx->flow.in_port); | |
2737 | break; | |
2738 | case OFPP_NORMAL: | |
2739 | xlate_normal(ctx); | |
2740 | break; | |
2741 | case OFPP_FLOOD: | |
2742 | flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD), | |
2743 | &ctx->nf_output_iface, ctx->odp_actions); | |
2744 | break; | |
2745 | case OFPP_ALL: | |
2746 | flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0), | |
2747 | &ctx->nf_output_iface, ctx->odp_actions); | |
2748 | break; | |
2749 | case OFPP_CONTROLLER: | |
2750 | nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len); | |
2751 | break; | |
2752 | case OFPP_LOCAL: | |
2753 | add_output_action(ctx, OFPP_LOCAL); | |
2754 | break; | |
2755 | default: | |
2756 | if (port != ctx->flow.in_port) { | |
2757 | add_output_action(ctx, port); | |
2758 | } | |
2759 | break; | |
2760 | } | |
2761 | ||
2762 | if (prev_nf_output_iface == NF_OUT_FLOOD) { | |
2763 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
2764 | } else if (ctx->nf_output_iface == NF_OUT_DROP) { | |
2765 | ctx->nf_output_iface = prev_nf_output_iface; | |
2766 | } else if (prev_nf_output_iface != NF_OUT_DROP && | |
2767 | ctx->nf_output_iface != NF_OUT_FLOOD) { | |
2768 | ctx->nf_output_iface = NF_OUT_MULTI; | |
2769 | } | |
2770 | } | |
2771 | ||
2772 | static void | |
2773 | xlate_output_action(struct action_xlate_ctx *ctx, | |
2774 | const struct ofp_action_output *oao) | |
2775 | { | |
2776 | xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len)); | |
2777 | } | |
2778 | ||
2779 | /* If the final ODP action in 'ctx' is "pop priority", drop it, as an | |
2780 | * optimization, because we're going to add another action that sets the | |
2781 | * priority immediately after, or because there are no actions following the | |
2782 | * pop. */ | |
2783 | static void | |
2784 | remove_pop_action(struct action_xlate_ctx *ctx) | |
2785 | { | |
2786 | if (ctx->odp_actions->size == ctx->last_pop_priority) { | |
2787 | ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN); | |
2788 | ctx->last_pop_priority = -1; | |
2789 | } | |
2790 | } | |
2791 | ||
2792 | static void | |
2793 | add_pop_action(struct action_xlate_ctx *ctx) | |
2794 | { | |
2795 | if (ctx->odp_actions->size != ctx->last_pop_priority) { | |
2796 | nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY); | |
2797 | ctx->last_pop_priority = ctx->odp_actions->size; | |
2798 | } | |
2799 | } | |
2800 | ||
2801 | static void | |
2802 | xlate_enqueue_action(struct action_xlate_ctx *ctx, | |
2803 | const struct ofp_action_enqueue *oae) | |
2804 | { | |
2805 | uint16_t ofp_port, odp_port; | |
2806 | uint32_t priority; | |
2807 | int error; | |
2808 | ||
2809 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id), | |
2810 | &priority); | |
2811 | if (error) { | |
2812 | /* Fall back to ordinary output action. */ | |
2813 | xlate_output_action__(ctx, ntohs(oae->port), 0); | |
2814 | return; | |
2815 | } | |
2816 | ||
2817 | /* Figure out ODP output port. */ | |
2818 | ofp_port = ntohs(oae->port); | |
2819 | if (ofp_port == OFPP_IN_PORT) { | |
2820 | ofp_port = ctx->flow.in_port; | |
2821 | } | |
2822 | odp_port = ofp_port_to_odp_port(ofp_port); | |
2823 | ||
2824 | /* Add ODP actions. */ | |
2825 | remove_pop_action(ctx); | |
2826 | nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority); | |
2827 | add_output_action(ctx, odp_port); | |
2828 | add_pop_action(ctx); | |
2829 | ||
2830 | /* Update NetFlow output port. */ | |
2831 | if (ctx->nf_output_iface == NF_OUT_DROP) { | |
2832 | ctx->nf_output_iface = odp_port; | |
2833 | } else if (ctx->nf_output_iface != NF_OUT_FLOOD) { | |
2834 | ctx->nf_output_iface = NF_OUT_MULTI; | |
2835 | } | |
2836 | } | |
2837 | ||
2838 | static void | |
2839 | xlate_set_queue_action(struct action_xlate_ctx *ctx, | |
2840 | const struct nx_action_set_queue *nasq) | |
2841 | { | |
2842 | uint32_t priority; | |
2843 | int error; | |
2844 | ||
2845 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id), | |
2846 | &priority); | |
2847 | if (error) { | |
2848 | /* Couldn't translate queue to a priority, so ignore. A warning | |
2849 | * has already been logged. */ | |
2850 | return; | |
2851 | } | |
2852 | ||
2853 | remove_pop_action(ctx); | |
2854 | nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority); | |
2855 | } | |
2856 | ||
2857 | static void | |
2858 | xlate_set_dl_tci(struct action_xlate_ctx *ctx) | |
2859 | { | |
2860 | ovs_be16 tci = ctx->flow.vlan_tci; | |
2861 | if (!(tci & htons(VLAN_CFI))) { | |
2862 | nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN); | |
2863 | } else { | |
2864 | nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI, | |
2865 | tci & ~htons(VLAN_CFI)); | |
2866 | } | |
2867 | } | |
2868 | ||
2869 | struct xlate_reg_state { | |
2870 | ovs_be16 vlan_tci; | |
2871 | ovs_be64 tun_id; | |
2872 | }; | |
2873 | ||
2874 | static void | |
2875 | save_reg_state(const struct action_xlate_ctx *ctx, | |
2876 | struct xlate_reg_state *state) | |
2877 | { | |
2878 | state->vlan_tci = ctx->flow.vlan_tci; | |
2879 | state->tun_id = ctx->flow.tun_id; | |
2880 | } | |
2881 | ||
2882 | static void | |
2883 | update_reg_state(struct action_xlate_ctx *ctx, | |
2884 | const struct xlate_reg_state *state) | |
2885 | { | |
2886 | if (ctx->flow.vlan_tci != state->vlan_tci) { | |
2887 | xlate_set_dl_tci(ctx); | |
2888 | } | |
2889 | if (ctx->flow.tun_id != state->tun_id) { | |
2890 | nl_msg_put_be64(ctx->odp_actions, | |
2891 | ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id); | |
2892 | } | |
2893 | } | |
2894 | ||
2895 | static void | |
2896 | xlate_autopath(struct action_xlate_ctx *ctx, | |
2897 | const struct nx_action_autopath *naa) | |
2898 | { | |
2899 | uint16_t ofp_port = ntohl(naa->id); | |
2900 | struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port); | |
2901 | ||
2902 | if (!port || !port->bundle) { | |
2903 | ofp_port = OFPP_NONE; | |
2904 | } else if (port->bundle->bond) { | |
2905 | /* Autopath does not support VLAN hashing. */ | |
2906 | struct ofport_dpif *slave = bond_choose_output_slave( | |
2907 | port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags); | |
2908 | if (slave) { | |
2909 | ofp_port = slave->up.ofp_port; | |
2910 | } | |
2911 | } | |
2912 | autopath_execute(naa, &ctx->flow, ofp_port); | |
2913 | } | |
2914 | ||
2915 | static void | |
2916 | xlate_nicira_action(struct action_xlate_ctx *ctx, | |
2917 | const struct nx_action_header *nah) | |
2918 | { | |
2919 | const struct nx_action_resubmit *nar; | |
2920 | const struct nx_action_set_tunnel *nast; | |
2921 | const struct nx_action_set_queue *nasq; | |
2922 | const struct nx_action_multipath *nam; | |
2923 | const struct nx_action_autopath *naa; | |
2924 | enum nx_action_subtype subtype = ntohs(nah->subtype); | |
2925 | struct xlate_reg_state state; | |
2926 | ovs_be64 tun_id; | |
2927 | ||
2928 | assert(nah->vendor == htonl(NX_VENDOR_ID)); | |
2929 | switch (subtype) { | |
2930 | case NXAST_RESUBMIT: | |
2931 | nar = (const struct nx_action_resubmit *) nah; | |
2932 | xlate_table_action(ctx, ntohs(nar->in_port)); | |
2933 | break; | |
2934 | ||
2935 | case NXAST_SET_TUNNEL: | |
2936 | nast = (const struct nx_action_set_tunnel *) nah; | |
2937 | tun_id = htonll(ntohl(nast->tun_id)); | |
2938 | nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id); | |
2939 | ctx->flow.tun_id = tun_id; | |
2940 | break; | |
2941 | ||
2942 | case NXAST_DROP_SPOOFED_ARP: | |
2943 | if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) { | |
2944 | nl_msg_put_flag(ctx->odp_actions, | |
2945 | ODP_ACTION_ATTR_DROP_SPOOFED_ARP); | |
2946 | } | |
2947 | break; | |
2948 | ||
2949 | case NXAST_SET_QUEUE: | |
2950 | nasq = (const struct nx_action_set_queue *) nah; | |
2951 | xlate_set_queue_action(ctx, nasq); | |
2952 | break; | |
2953 | ||
2954 | case NXAST_POP_QUEUE: | |
2955 | add_pop_action(ctx); | |
2956 | break; | |
2957 | ||
2958 | case NXAST_REG_MOVE: | |
2959 | save_reg_state(ctx, &state); | |
2960 | nxm_execute_reg_move((const struct nx_action_reg_move *) nah, | |
2961 | &ctx->flow); | |
2962 | update_reg_state(ctx, &state); | |
2963 | break; | |
2964 | ||
2965 | case NXAST_REG_LOAD: | |
2966 | save_reg_state(ctx, &state); | |
2967 | nxm_execute_reg_load((const struct nx_action_reg_load *) nah, | |
2968 | &ctx->flow); | |
2969 | update_reg_state(ctx, &state); | |
2970 | break; | |
2971 | ||
2972 | case NXAST_NOTE: | |
2973 | /* Nothing to do. */ | |
2974 | break; | |
2975 | ||
2976 | case NXAST_SET_TUNNEL64: | |
2977 | tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id; | |
2978 | nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id); | |
2979 | ctx->flow.tun_id = tun_id; | |
2980 | break; | |
2981 | ||
2982 | case NXAST_MULTIPATH: | |
2983 | nam = (const struct nx_action_multipath *) nah; | |
2984 | multipath_execute(nam, &ctx->flow); | |
2985 | break; | |
2986 | ||
2987 | case NXAST_AUTOPATH: | |
2988 | naa = (const struct nx_action_autopath *) nah; | |
2989 | xlate_autopath(ctx, naa); | |
2990 | break; | |
2991 | ||
2992 | /* If you add a new action here that modifies flow data, don't forget to | |
2993 | * update the flow key in ctx->flow at the same time. */ | |
2994 | ||
2995 | case NXAST_SNAT__OBSOLETE: | |
2996 | default: | |
2997 | VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype); | |
2998 | break; | |
2999 | } | |
3000 | } | |
3001 | ||
3002 | static void | |
3003 | do_xlate_actions(const union ofp_action *in, size_t n_in, | |
3004 | struct action_xlate_ctx *ctx) | |
3005 | { | |
3006 | const struct ofport_dpif *port; | |
3007 | struct actions_iterator iter; | |
3008 | const union ofp_action *ia; | |
3009 | ||
3010 | port = get_ofp_port(ctx->ofproto, ctx->flow.in_port); | |
3011 | if (port | |
3012 | && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) && | |
3013 | port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp) | |
3014 | ? htonl(OFPPC_NO_RECV_STP) | |
3015 | : htonl(OFPPC_NO_RECV))) { | |
3016 | /* Drop this flow. */ | |
3017 | return; | |
3018 | } | |
3019 | ||
3020 | for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) { | |
3021 | enum ofp_action_type type = ntohs(ia->type); | |
3022 | const struct ofp_action_dl_addr *oada; | |
3023 | ||
3024 | switch (type) { | |
3025 | case OFPAT_OUTPUT: | |
3026 | xlate_output_action(ctx, &ia->output); | |
3027 | break; | |
3028 | ||
3029 | case OFPAT_SET_VLAN_VID: | |
3030 | ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK); | |
3031 | ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI); | |
3032 | xlate_set_dl_tci(ctx); | |
3033 | break; | |
3034 | ||
3035 | case OFPAT_SET_VLAN_PCP: | |
3036 | ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); | |
3037 | ctx->flow.vlan_tci |= htons( | |
3038 | (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI); | |
3039 | xlate_set_dl_tci(ctx); | |
3040 | break; | |
3041 | ||
3042 | case OFPAT_STRIP_VLAN: | |
3043 | ctx->flow.vlan_tci = htons(0); | |
3044 | xlate_set_dl_tci(ctx); | |
3045 | break; | |
3046 | ||
3047 | case OFPAT_SET_DL_SRC: | |
3048 | oada = ((struct ofp_action_dl_addr *) ia); | |
3049 | nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC, | |
3050 | oada->dl_addr, ETH_ADDR_LEN); | |
3051 | memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN); | |
3052 | break; | |
3053 | ||
3054 | case OFPAT_SET_DL_DST: | |
3055 | oada = ((struct ofp_action_dl_addr *) ia); | |
3056 | nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST, | |
3057 | oada->dl_addr, ETH_ADDR_LEN); | |
3058 | memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN); | |
3059 | break; | |
3060 | ||
3061 | case OFPAT_SET_NW_SRC: | |
3062 | nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC, | |
3063 | ia->nw_addr.nw_addr); | |
3064 | ctx->flow.nw_src = ia->nw_addr.nw_addr; | |
3065 | break; | |
3066 | ||
3067 | case OFPAT_SET_NW_DST: | |
3068 | nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST, | |
3069 | ia->nw_addr.nw_addr); | |
3070 | ctx->flow.nw_dst = ia->nw_addr.nw_addr; | |
3071 | break; | |
3072 | ||
3073 | case OFPAT_SET_NW_TOS: | |
3074 | nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS, | |
3075 | ia->nw_tos.nw_tos); | |
3076 | ctx->flow.nw_tos = ia->nw_tos.nw_tos; | |
3077 | break; | |
3078 | ||
3079 | case OFPAT_SET_TP_SRC: | |
3080 | nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC, | |
3081 | ia->tp_port.tp_port); | |
3082 | ctx->flow.tp_src = ia->tp_port.tp_port; | |
3083 | break; | |
3084 | ||
3085 | case OFPAT_SET_TP_DST: | |
3086 | nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST, | |
3087 | ia->tp_port.tp_port); | |
3088 | ctx->flow.tp_dst = ia->tp_port.tp_port; | |
3089 | break; | |
3090 | ||
3091 | case OFPAT_VENDOR: | |
3092 | xlate_nicira_action(ctx, (const struct nx_action_header *) ia); | |
3093 | break; | |
3094 | ||
3095 | case OFPAT_ENQUEUE: | |
3096 | xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia); | |
3097 | break; | |
3098 | ||
3099 | default: | |
3100 | VLOG_DBG_RL(&rl, "unknown action type %d", (int) type); | |
3101 | break; | |
3102 | } | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | static void | |
3107 | action_xlate_ctx_init(struct action_xlate_ctx *ctx, | |
3108 | struct ofproto_dpif *ofproto, const struct flow *flow, | |
3109 | const struct ofpbuf *packet) | |
3110 | { | |
3111 | ctx->ofproto = ofproto; | |
3112 | ctx->flow = *flow; | |
3113 | ctx->packet = packet; | |
3114 | ctx->resubmit_hook = NULL; | |
abe529af BP |
3115 | } |
3116 | ||
3117 | static struct ofpbuf * | |
3118 | xlate_actions(struct action_xlate_ctx *ctx, | |
3119 | const union ofp_action *in, size_t n_in) | |
3120 | { | |
3121 | COVERAGE_INC(ofproto_dpif_xlate); | |
3122 | ||
3123 | ctx->odp_actions = ofpbuf_new(512); | |
3124 | ctx->tags = 0; | |
3125 | ctx->may_set_up_flow = true; | |
3126 | ctx->nf_output_iface = NF_OUT_DROP; | |
3127 | ctx->recurse = 0; | |
3128 | ctx->last_pop_priority = -1; | |
3129 | ||
fc08b7a2 | 3130 | if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) { |
abe529af BP |
3131 | ctx->may_set_up_flow = false; |
3132 | } else { | |
3133 | do_xlate_actions(in, n_in, ctx); | |
3134 | } | |
3135 | ||
3136 | remove_pop_action(ctx); | |
3137 | ||
3138 | /* Check with in-band control to see if we're allowed to set up this | |
3139 | * flow. */ | |
3140 | if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow, | |
3141 | ctx->odp_actions->data, | |
3142 | ctx->odp_actions->size)) { | |
3143 | ctx->may_set_up_flow = false; | |
3144 | } | |
3145 | ||
3146 | return ctx->odp_actions; | |
3147 | } | |
3148 | \f | |
3149 | /* OFPP_NORMAL implementation. */ | |
3150 | ||
3151 | struct dst { | |
3152 | struct ofport_dpif *port; | |
3153 | uint16_t vlan; | |
3154 | }; | |
3155 | ||
3156 | struct dst_set { | |
3157 | struct dst builtin[32]; | |
3158 | struct dst *dsts; | |
3159 | size_t n, allocated; | |
3160 | }; | |
3161 | ||
3162 | static void dst_set_init(struct dst_set *); | |
3163 | static void dst_set_add(struct dst_set *, const struct dst *); | |
3164 | static void dst_set_free(struct dst_set *); | |
3165 | ||
3166 | static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *); | |
3167 | ||
3168 | static bool | |
3169 | set_dst(struct action_xlate_ctx *ctx, struct dst *dst, | |
3170 | const struct ofbundle *in_bundle, const struct ofbundle *out_bundle) | |
3171 | { | |
3172 | dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE | |
3173 | : in_bundle->vlan >= 0 ? in_bundle->vlan | |
3174 | : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE | |
3175 | : vlan_tci_to_vid(ctx->flow.vlan_tci)); | |
3176 | ||
3177 | dst->port = (!out_bundle->bond | |
3178 | ? ofbundle_get_a_port(out_bundle) | |
3179 | : bond_choose_output_slave(out_bundle->bond, &ctx->flow, | |
3180 | dst->vlan, &ctx->tags)); | |
3181 | ||
3182 | return dst->port != NULL; | |
3183 | } | |
3184 | ||
3185 | static int | |
3186 | mirror_mask_ffs(mirror_mask_t mask) | |
3187 | { | |
3188 | BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask)); | |
3189 | return ffs(mask); | |
3190 | } | |
3191 | ||
3192 | static void | |
3193 | dst_set_init(struct dst_set *set) | |
3194 | { | |
3195 | set->dsts = set->builtin; | |
3196 | set->n = 0; | |
3197 | set->allocated = ARRAY_SIZE(set->builtin); | |
3198 | } | |
3199 | ||
3200 | static void | |
3201 | dst_set_add(struct dst_set *set, const struct dst *dst) | |
3202 | { | |
3203 | if (set->n >= set->allocated) { | |
3204 | size_t new_allocated; | |
3205 | struct dst *new_dsts; | |
3206 | ||
3207 | new_allocated = set->allocated * 2; | |
3208 | new_dsts = xmalloc(new_allocated * sizeof *new_dsts); | |
3209 | memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts); | |
3210 | ||
3211 | dst_set_free(set); | |
3212 | ||
3213 | set->dsts = new_dsts; | |
3214 | set->allocated = new_allocated; | |
3215 | } | |
3216 | set->dsts[set->n++] = *dst; | |
3217 | } | |
3218 | ||
3219 | static void | |
3220 | dst_set_free(struct dst_set *set) | |
3221 | { | |
3222 | if (set->dsts != set->builtin) { | |
3223 | free(set->dsts); | |
3224 | } | |
3225 | } | |
3226 | ||
3227 | static bool | |
3228 | dst_is_duplicate(const struct dst_set *set, const struct dst *test) | |
3229 | { | |
3230 | size_t i; | |
3231 | for (i = 0; i < set->n; i++) { | |
3232 | if (set->dsts[i].vlan == test->vlan | |
3233 | && set->dsts[i].port == test->port) { | |
3234 | return true; | |
3235 | } | |
3236 | } | |
3237 | return false; | |
3238 | } | |
3239 | ||
3240 | static bool | |
3241 | ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan) | |
3242 | { | |
3243 | return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan); | |
3244 | } | |
3245 | ||
3246 | static bool | |
3247 | ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan) | |
3248 | { | |
3249 | return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan); | |
3250 | } | |
3251 | ||
3252 | /* Returns an arbitrary interface within 'bundle'. */ | |
3253 | static struct ofport_dpif * | |
3254 | ofbundle_get_a_port(const struct ofbundle *bundle) | |
3255 | { | |
3256 | return CONTAINER_OF(list_front(&bundle->ports), | |
3257 | struct ofport_dpif, bundle_node); | |
3258 | } | |
3259 | ||
3260 | static void | |
3261 | compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan, | |
3262 | const struct ofbundle *in_bundle, | |
3263 | const struct ofbundle *out_bundle, struct dst_set *set) | |
3264 | { | |
3265 | struct dst dst; | |
3266 | ||
3267 | if (out_bundle == OFBUNDLE_FLOOD) { | |
3268 | struct ofbundle *bundle; | |
3269 | ||
3270 | HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) { | |
3271 | if (bundle != in_bundle | |
3272 | && ofbundle_includes_vlan(bundle, vlan) | |
3273 | && bundle->floodable | |
3274 | && !bundle->mirror_out | |
3275 | && set_dst(ctx, &dst, in_bundle, bundle)) { | |
3276 | dst_set_add(set, &dst); | |
3277 | } | |
3278 | } | |
3279 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
3280 | } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) { | |
3281 | dst_set_add(set, &dst); | |
3282 | ctx->nf_output_iface = dst.port->odp_port; | |
3283 | } | |
3284 | } | |
3285 | ||
3286 | static bool | |
3287 | vlan_is_mirrored(const struct ofmirror *m, int vlan) | |
3288 | { | |
3289 | return vlan_bitmap_contains(m->vlans, vlan); | |
3290 | } | |
3291 | ||
3292 | static void | |
3293 | compose_mirror_dsts(struct action_xlate_ctx *ctx, | |
3294 | uint16_t vlan, const struct ofbundle *in_bundle, | |
3295 | struct dst_set *set) | |
3296 | { | |
3297 | struct ofproto_dpif *ofproto = ctx->ofproto; | |
3298 | mirror_mask_t mirrors; | |
3299 | int flow_vlan; | |
3300 | size_t i; | |
3301 | ||
3302 | mirrors = in_bundle->src_mirrors; | |
3303 | for (i = 0; i < set->n; i++) { | |
3304 | mirrors |= set->dsts[i].port->bundle->dst_mirrors; | |
3305 | } | |
3306 | ||
3307 | if (!mirrors) { | |
3308 | return; | |
3309 | } | |
3310 | ||
3311 | flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci); | |
3312 | if (flow_vlan == 0) { | |
3313 | flow_vlan = OFP_VLAN_NONE; | |
3314 | } | |
3315 | ||
3316 | while (mirrors) { | |
3317 | struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1]; | |
3318 | if (vlan_is_mirrored(m, vlan)) { | |
3319 | struct dst dst; | |
3320 | ||
3321 | if (m->out) { | |
3322 | if (set_dst(ctx, &dst, in_bundle, m->out) | |
3323 | && !dst_is_duplicate(set, &dst)) { | |
3324 | dst_set_add(set, &dst); | |
3325 | } | |
3326 | } else { | |
3327 | struct ofbundle *bundle; | |
3328 | ||
3329 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
3330 | if (ofbundle_includes_vlan(bundle, m->out_vlan) | |
3331 | && set_dst(ctx, &dst, in_bundle, bundle)) | |
3332 | { | |
3333 | if (bundle->vlan < 0) { | |
3334 | dst.vlan = m->out_vlan; | |
3335 | } | |
3336 | if (dst_is_duplicate(set, &dst)) { | |
3337 | continue; | |
3338 | } | |
3339 | ||
3340 | /* Use the vlan tag on the original flow instead of | |
3341 | * the one passed in the vlan parameter. This ensures | |
3342 | * that we compare the vlan from before any implicit | |
3343 | * tagging tags place. This is necessary because | |
3344 | * dst->vlan is the final vlan, after removing implicit | |
3345 | * tags. */ | |
3346 | if (bundle == in_bundle && dst.vlan == flow_vlan) { | |
3347 | /* Don't send out input port on same VLAN. */ | |
3348 | continue; | |
3349 | } | |
3350 | dst_set_add(set, &dst); | |
3351 | } | |
3352 | } | |
3353 | } | |
3354 | } | |
3355 | mirrors &= mirrors - 1; | |
3356 | } | |
3357 | } | |
3358 | ||
3359 | static void | |
3360 | compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan, | |
3361 | const struct ofbundle *in_bundle, | |
3362 | const struct ofbundle *out_bundle) | |
3363 | { | |
3364 | uint16_t initial_vlan, cur_vlan; | |
3365 | const struct dst *dst; | |
3366 | struct dst_set set; | |
3367 | ||
3368 | dst_set_init(&set); | |
3369 | compose_dsts(ctx, vlan, in_bundle, out_bundle, &set); | |
3370 | compose_mirror_dsts(ctx, vlan, in_bundle, &set); | |
3371 | ||
3372 | /* Output all the packets we can without having to change the VLAN. */ | |
3373 | initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci); | |
3374 | if (initial_vlan == 0) { | |
3375 | initial_vlan = OFP_VLAN_NONE; | |
3376 | } | |
3377 | for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) { | |
3378 | if (dst->vlan != initial_vlan) { | |
3379 | continue; | |
3380 | } | |
3381 | nl_msg_put_u32(ctx->odp_actions, | |
3382 | ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port); | |
3383 | } | |
3384 | ||
3385 | /* Then output the rest. */ | |
3386 | cur_vlan = initial_vlan; | |
3387 | for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) { | |
3388 | if (dst->vlan == initial_vlan) { | |
3389 | continue; | |
3390 | } | |
3391 | if (dst->vlan != cur_vlan) { | |
3392 | if (dst->vlan == OFP_VLAN_NONE) { | |
3393 | nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN); | |
3394 | } else { | |
3395 | ovs_be16 tci; | |
3396 | tci = htons(dst->vlan & VLAN_VID_MASK); | |
3397 | tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK); | |
3398 | nl_msg_put_be16(ctx->odp_actions, | |
3399 | ODP_ACTION_ATTR_SET_DL_TCI, tci); | |
3400 | } | |
3401 | cur_vlan = dst->vlan; | |
3402 | } | |
3403 | nl_msg_put_u32(ctx->odp_actions, | |
3404 | ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port); | |
3405 | } | |
3406 | ||
3407 | dst_set_free(&set); | |
3408 | } | |
3409 | ||
3410 | /* Returns the effective vlan of a packet, taking into account both the | |
3411 | * 802.1Q header and implicitly tagged ports. A value of 0 indicates that | |
3412 | * the packet is untagged and -1 indicates it has an invalid header and | |
3413 | * should be dropped. */ | |
3414 | static int | |
3415 | flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow, | |
3416 | struct ofbundle *in_bundle, bool have_packet) | |
3417 | { | |
3418 | int vlan = vlan_tci_to_vid(flow->vlan_tci); | |
3419 | if (in_bundle->vlan >= 0) { | |
3420 | if (vlan) { | |
3421 | if (have_packet) { | |
3422 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3423 | VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged " | |
3424 | "packet received on port %s configured with " | |
3425 | "implicit VLAN %"PRIu16, | |
3426 | ofproto->up.name, vlan, | |
3427 | in_bundle->name, in_bundle->vlan); | |
3428 | } | |
3429 | return -1; | |
3430 | } | |
3431 | vlan = in_bundle->vlan; | |
3432 | } else { | |
3433 | if (!ofbundle_includes_vlan(in_bundle, vlan)) { | |
3434 | if (have_packet) { | |
3435 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3436 | VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged " | |
3437 | "packet received on port %s not configured for " | |
3438 | "trunking VLAN %d", | |
3439 | ofproto->up.name, vlan, in_bundle->name, vlan); | |
3440 | } | |
3441 | return -1; | |
3442 | } | |
3443 | } | |
3444 | ||
3445 | return vlan; | |
3446 | } | |
3447 | ||
3448 | /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after | |
3449 | * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to | |
3450 | * indicate this; newer upstream kernels use gratuitous ARP requests. */ | |
3451 | static bool | |
3452 | is_gratuitous_arp(const struct flow *flow) | |
3453 | { | |
3454 | return (flow->dl_type == htons(ETH_TYPE_ARP) | |
3455 | && eth_addr_is_broadcast(flow->dl_dst) | |
3456 | && (flow->nw_proto == ARP_OP_REPLY | |
3457 | || (flow->nw_proto == ARP_OP_REQUEST | |
3458 | && flow->nw_src == flow->nw_dst))); | |
3459 | } | |
3460 | ||
3461 | static void | |
3462 | update_learning_table(struct ofproto_dpif *ofproto, | |
3463 | const struct flow *flow, int vlan, | |
3464 | struct ofbundle *in_bundle) | |
3465 | { | |
3466 | struct mac_entry *mac; | |
3467 | ||
3468 | if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) { | |
3469 | return; | |
3470 | } | |
3471 | ||
3472 | mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan); | |
3473 | if (is_gratuitous_arp(flow)) { | |
3474 | /* We don't want to learn from gratuitous ARP packets that are | |
3475 | * reflected back over bond slaves so we lock the learning table. */ | |
3476 | if (!in_bundle->bond) { | |
3477 | mac_entry_set_grat_arp_lock(mac); | |
3478 | } else if (mac_entry_is_grat_arp_locked(mac)) { | |
3479 | return; | |
3480 | } | |
3481 | } | |
3482 | ||
3483 | if (mac_entry_is_new(mac) || mac->port.p != in_bundle) { | |
3484 | /* The log messages here could actually be useful in debugging, | |
3485 | * so keep the rate limit relatively high. */ | |
3486 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300); | |
3487 | VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is " | |
3488 | "on port %s in VLAN %d", | |
3489 | ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src), | |
3490 | in_bundle->name, vlan); | |
3491 | ||
3492 | mac->port.p = in_bundle; | |
3493 | tag_set_add(&ofproto->revalidate_set, | |
3494 | mac_learning_changed(ofproto->ml, mac)); | |
3495 | } | |
3496 | } | |
3497 | ||
3498 | /* Determines whether packets in 'flow' within 'br' should be forwarded or | |
3499 | * dropped. Returns true if they may be forwarded, false if they should be | |
3500 | * dropped. | |
3501 | * | |
3502 | * If 'have_packet' is true, it indicates that the caller is processing a | |
3503 | * received packet. If 'have_packet' is false, then the caller is just | |
3504 | * revalidating an existing flow because configuration has changed. Either | |
3505 | * way, 'have_packet' only affects logging (there is no point in logging errors | |
3506 | * during revalidation). | |
3507 | * | |
3508 | * Sets '*in_portp' to the input port. This will be a null pointer if | |
3509 | * flow->in_port does not designate a known input port (in which case | |
3510 | * is_admissible() returns false). | |
3511 | * | |
3512 | * When returning true, sets '*vlanp' to the effective VLAN of the input | |
3513 | * packet, as returned by flow_get_vlan(). | |
3514 | * | |
3515 | * May also add tags to '*tags', although the current implementation only does | |
3516 | * so in one special case. | |
3517 | */ | |
3518 | static bool | |
3519 | is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow, | |
3520 | bool have_packet, | |
3521 | tag_type *tags, int *vlanp, struct ofbundle **in_bundlep) | |
3522 | { | |
3523 | struct ofport_dpif *in_port; | |
3524 | struct ofbundle *in_bundle; | |
3525 | int vlan; | |
3526 | ||
3527 | /* Find the port and bundle for the received packet. */ | |
3528 | in_port = get_ofp_port(ofproto, flow->in_port); | |
23adee42 | 3529 | *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL; |
abe529af BP |
3530 | if (!in_port || !in_bundle) { |
3531 | /* No interface? Something fishy... */ | |
3532 | if (have_packet) { | |
3533 | /* Odd. A few possible reasons here: | |
3534 | * | |
3535 | * - We deleted a port but there are still a few packets queued up | |
3536 | * from it. | |
3537 | * | |
3538 | * - Someone externally added a port (e.g. "ovs-dpctl add-if") that | |
3539 | * we don't know about. | |
3540 | * | |
3541 | * - Packet arrived on the local port but the local port is not | |
3542 | * part of a bundle. | |
3543 | */ | |
3544 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3545 | ||
3546 | VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown " | |
3547 | "port %"PRIu16, | |
3548 | ofproto->up.name, flow->in_port); | |
3549 | } | |
3550 | return false; | |
3551 | } | |
3552 | *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet); | |
3553 | if (vlan < 0) { | |
3554 | return false; | |
3555 | } | |
3556 | ||
3557 | /* Drop frames for reserved multicast addresses. */ | |
3558 | if (eth_addr_is_reserved(flow->dl_dst)) { | |
3559 | return false; | |
3560 | } | |
3561 | ||
3562 | /* Drop frames on bundles reserved for mirroring. */ | |
3563 | if (in_bundle->mirror_out) { | |
3564 | if (have_packet) { | |
3565 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
3566 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port " | |
3567 | "%s, which is reserved exclusively for mirroring", | |
3568 | ofproto->up.name, in_bundle->name); | |
3569 | } | |
3570 | return false; | |
3571 | } | |
3572 | ||
3573 | if (in_bundle->bond) { | |
3574 | struct mac_entry *mac; | |
3575 | ||
3576 | switch (bond_check_admissibility(in_bundle->bond, in_port, | |
3577 | flow->dl_dst, tags)) { | |
3578 | case BV_ACCEPT: | |
3579 | break; | |
3580 | ||
3581 | case BV_DROP: | |
3582 | return false; | |
3583 | ||
3584 | case BV_DROP_IF_MOVED: | |
3585 | mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL); | |
3586 | if (mac && mac->port.p != in_bundle && | |
3587 | (!is_gratuitous_arp(flow) | |
3588 | || mac_entry_is_grat_arp_locked(mac))) { | |
3589 | return false; | |
3590 | } | |
3591 | break; | |
3592 | } | |
3593 | } | |
3594 | ||
3595 | return true; | |
3596 | } | |
3597 | ||
3598 | /* If the composed actions may be applied to any packet in the given 'flow', | |
3599 | * returns true. Otherwise, the actions should only be applied to 'packet', or | |
3600 | * not at all, if 'packet' was NULL. */ | |
3601 | static bool | |
3602 | xlate_normal(struct action_xlate_ctx *ctx) | |
3603 | { | |
3604 | struct ofbundle *in_bundle; | |
3605 | struct ofbundle *out_bundle; | |
3606 | struct mac_entry *mac; | |
3607 | int vlan; | |
3608 | ||
3609 | /* Check whether we should drop packets in this flow. */ | |
3610 | if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL, | |
3611 | &ctx->tags, &vlan, &in_bundle)) { | |
3612 | out_bundle = NULL; | |
3613 | goto done; | |
3614 | } | |
3615 | ||
3616 | /* Learn source MAC (but don't try to learn from revalidation). */ | |
3617 | if (ctx->packet) { | |
3618 | update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle); | |
3619 | } | |
3620 | ||
3621 | /* Determine output bundle. */ | |
3622 | mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan, | |
3623 | &ctx->tags); | |
3624 | if (mac) { | |
3625 | out_bundle = mac->port.p; | |
3626 | } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) { | |
3627 | /* If we are revalidating but don't have a learning entry then eject | |
3628 | * the flow. Installing a flow that floods packets opens up a window | |
3629 | * of time where we could learn from a packet reflected on a bond and | |
3630 | * blackhole packets before the learning table is updated to reflect | |
3631 | * the correct port. */ | |
3632 | return false; | |
3633 | } else { | |
3634 | out_bundle = OFBUNDLE_FLOOD; | |
3635 | } | |
3636 | ||
3637 | /* Don't send packets out their input bundles. */ | |
3638 | if (in_bundle == out_bundle) { | |
3639 | out_bundle = NULL; | |
3640 | } | |
3641 | ||
3642 | done: | |
3643 | if (in_bundle) { | |
3644 | compose_actions(ctx, vlan, in_bundle, out_bundle); | |
3645 | } | |
3646 | ||
3647 | return true; | |
3648 | } | |
3649 | \f | |
3650 | static bool | |
3651 | get_drop_frags(struct ofproto *ofproto_) | |
3652 | { | |
3653 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
3654 | bool drop_frags; | |
3655 | ||
3656 | dpif_get_drop_frags(ofproto->dpif, &drop_frags); | |
3657 | return drop_frags; | |
3658 | } | |
3659 | ||
3660 | static void | |
3661 | set_drop_frags(struct ofproto *ofproto_, bool drop_frags) | |
3662 | { | |
3663 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
3664 | ||
3665 | dpif_set_drop_frags(ofproto->dpif, drop_frags); | |
3666 | } | |
3667 | ||
3668 | static int | |
3669 | packet_out(struct ofproto *ofproto_, struct ofpbuf *packet, | |
3670 | const struct flow *flow, | |
3671 | const union ofp_action *ofp_actions, size_t n_ofp_actions) | |
3672 | { | |
3673 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
3674 | int error; | |
3675 | ||
3676 | error = validate_actions(ofp_actions, n_ofp_actions, flow, | |
3677 | ofproto->max_ports); | |
3678 | if (!error) { | |
3679 | struct action_xlate_ctx ctx; | |
3680 | struct ofpbuf *odp_actions; | |
3681 | ||
3682 | action_xlate_ctx_init(&ctx, ofproto, flow, packet); | |
3683 | odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions); | |
3684 | dpif_execute(ofproto->dpif, odp_actions->data, odp_actions->size, | |
3685 | packet); | |
3686 | ofpbuf_delete(odp_actions); | |
3687 | } | |
3688 | return error; | |
3689 | } | |
3690 | ||
3691 | static void | |
3692 | get_netflow_ids(const struct ofproto *ofproto_, | |
3693 | uint8_t *engine_type, uint8_t *engine_id) | |
3694 | { | |
3695 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
3696 | ||
3697 | dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id); | |
3698 | } | |
3699 | \f | |
3700 | static struct ofproto_dpif * | |
3701 | ofproto_dpif_lookup(const char *name) | |
3702 | { | |
3703 | struct ofproto *ofproto = ofproto_lookup(name); | |
3704 | return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class | |
3705 | ? ofproto_dpif_cast(ofproto) | |
3706 | : NULL); | |
3707 | } | |
3708 | ||
3709 | static void | |
3710 | ofproto_unixctl_fdb_show(struct unixctl_conn *conn, | |
3711 | const char *args, void *aux OVS_UNUSED) | |
3712 | { | |
3713 | struct ds ds = DS_EMPTY_INITIALIZER; | |
3714 | const struct ofproto_dpif *ofproto; | |
3715 | const struct mac_entry *e; | |
3716 | ||
3717 | ofproto = ofproto_dpif_lookup(args); | |
3718 | if (!ofproto) { | |
3719 | unixctl_command_reply(conn, 501, "no such bridge"); | |
3720 | return; | |
3721 | } | |
3722 | ||
3723 | ds_put_cstr(&ds, " port VLAN MAC Age\n"); | |
3724 | LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) { | |
3725 | struct ofbundle *bundle = e->port.p; | |
3726 | ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n", | |
3727 | ofbundle_get_a_port(bundle)->odp_port, | |
3728 | e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e)); | |
3729 | } | |
3730 | unixctl_command_reply(conn, 200, ds_cstr(&ds)); | |
3731 | ds_destroy(&ds); | |
3732 | } | |
3733 | ||
3734 | struct ofproto_trace { | |
3735 | struct action_xlate_ctx ctx; | |
3736 | struct flow flow; | |
3737 | struct ds *result; | |
3738 | }; | |
3739 | ||
3740 | static void | |
3741 | trace_format_rule(struct ds *result, int level, const struct rule *rule) | |
3742 | { | |
3743 | ds_put_char_multiple(result, '\t', level); | |
3744 | if (!rule) { | |
3745 | ds_put_cstr(result, "No match\n"); | |
3746 | return; | |
3747 | } | |
3748 | ||
3749 | ds_put_format(result, "Rule: cookie=%#"PRIx64" ", | |
3750 | ntohll(rule->flow_cookie)); | |
3751 | cls_rule_format(&rule->cr, result); | |
3752 | ds_put_char(result, '\n'); | |
3753 | ||
3754 | ds_put_char_multiple(result, '\t', level); | |
3755 | ds_put_cstr(result, "OpenFlow "); | |
3756 | ofp_print_actions(result, (const struct ofp_action_header *) rule->actions, | |
3757 | rule->n_actions * sizeof *rule->actions); | |
3758 | ds_put_char(result, '\n'); | |
3759 | } | |
3760 | ||
3761 | static void | |
3762 | trace_format_flow(struct ds *result, int level, const char *title, | |
3763 | struct ofproto_trace *trace) | |
3764 | { | |
3765 | ds_put_char_multiple(result, '\t', level); | |
3766 | ds_put_format(result, "%s: ", title); | |
3767 | if (flow_equal(&trace->ctx.flow, &trace->flow)) { | |
3768 | ds_put_cstr(result, "unchanged"); | |
3769 | } else { | |
3770 | flow_format(result, &trace->ctx.flow); | |
3771 | trace->flow = trace->ctx.flow; | |
3772 | } | |
3773 | ds_put_char(result, '\n'); | |
3774 | } | |
3775 | ||
3776 | static void | |
3777 | trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule) | |
3778 | { | |
3779 | struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx); | |
3780 | struct ds *result = trace->result; | |
3781 | ||
3782 | ds_put_char(result, '\n'); | |
3783 | trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace); | |
3784 | trace_format_rule(result, ctx->recurse + 1, &rule->up); | |
3785 | } | |
3786 | ||
3787 | static void | |
3788 | ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_, | |
3789 | void *aux OVS_UNUSED) | |
3790 | { | |
3791 | char *dpname, *in_port_s, *tun_id_s, *packet_s; | |
3792 | char *args = xstrdup(args_); | |
3793 | char *save_ptr = NULL; | |
3794 | struct ofproto_dpif *ofproto; | |
3795 | struct ofpbuf packet; | |
3796 | struct rule_dpif *rule; | |
3797 | struct ds result; | |
3798 | struct flow flow; | |
3799 | uint16_t in_port; | |
3800 | ovs_be64 tun_id; | |
3801 | char *s; | |
3802 | ||
3803 | ofpbuf_init(&packet, strlen(args) / 2); | |
3804 | ds_init(&result); | |
3805 | ||
3806 | dpname = strtok_r(args, " ", &save_ptr); | |
3807 | tun_id_s = strtok_r(NULL, " ", &save_ptr); | |
3808 | in_port_s = strtok_r(NULL, " ", &save_ptr); | |
3809 | packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */ | |
3810 | if (!dpname || !in_port_s || !packet_s) { | |
3811 | unixctl_command_reply(conn, 501, "Bad command syntax"); | |
3812 | goto exit; | |
3813 | } | |
3814 | ||
3815 | ofproto = ofproto_dpif_lookup(dpname); | |
3816 | if (!ofproto) { | |
3817 | unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list " | |
3818 | "for help)"); | |
3819 | goto exit; | |
3820 | } | |
3821 | ||
3822 | tun_id = htonll(strtoull(tun_id_s, NULL, 0)); | |
3823 | in_port = ofp_port_to_odp_port(atoi(in_port_s)); | |
3824 | ||
3825 | packet_s = ofpbuf_put_hex(&packet, packet_s, NULL); | |
3826 | packet_s += strspn(packet_s, " "); | |
3827 | if (*packet_s != '\0') { | |
3828 | unixctl_command_reply(conn, 501, "Trailing garbage in command"); | |
3829 | goto exit; | |
3830 | } | |
3831 | if (packet.size < ETH_HEADER_LEN) { | |
3832 | unixctl_command_reply(conn, 501, "Packet data too short for Ethernet"); | |
3833 | goto exit; | |
3834 | } | |
3835 | ||
3836 | ds_put_cstr(&result, "Packet: "); | |
3837 | s = ofp_packet_to_string(packet.data, packet.size, packet.size); | |
3838 | ds_put_cstr(&result, s); | |
3839 | free(s); | |
3840 | ||
3841 | flow_extract(&packet, tun_id, in_port, &flow); | |
3842 | ds_put_cstr(&result, "Flow: "); | |
3843 | flow_format(&result, &flow); | |
3844 | ds_put_char(&result, '\n'); | |
3845 | ||
3846 | rule = rule_dpif_lookup(ofproto, &flow); | |
3847 | trace_format_rule(&result, 0, &rule->up); | |
3848 | if (rule) { | |
3849 | struct ofproto_trace trace; | |
3850 | struct ofpbuf *odp_actions; | |
3851 | ||
3852 | trace.result = &result; | |
3853 | trace.flow = flow; | |
3854 | action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet); | |
3855 | trace.ctx.resubmit_hook = trace_resubmit; | |
3856 | odp_actions = xlate_actions(&trace.ctx, | |
3857 | rule->up.actions, rule->up.n_actions); | |
3858 | ||
3859 | ds_put_char(&result, '\n'); | |
3860 | trace_format_flow(&result, 0, "Final flow", &trace); | |
3861 | ds_put_cstr(&result, "Datapath actions: "); | |
3862 | format_odp_actions(&result, odp_actions->data, odp_actions->size); | |
3863 | ofpbuf_delete(odp_actions); | |
3864 | } | |
3865 | ||
3866 | unixctl_command_reply(conn, 200, ds_cstr(&result)); | |
3867 | ||
3868 | exit: | |
3869 | ds_destroy(&result); | |
3870 | ofpbuf_uninit(&packet); | |
3871 | free(args); | |
3872 | } | |
3873 | ||
3874 | static void | |
3875 | ofproto_dpif_unixctl_init(void) | |
3876 | { | |
3877 | static bool registered; | |
3878 | if (registered) { | |
3879 | return; | |
3880 | } | |
3881 | registered = true; | |
3882 | ||
3883 | unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL); | |
3884 | unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL); | |
3885 | } | |
3886 | \f | |
3887 | const struct ofproto_class ofproto_dpif_class = { | |
3888 | enumerate_types, | |
3889 | enumerate_names, | |
3890 | del, | |
3891 | alloc, | |
3892 | construct, | |
3893 | destruct, | |
3894 | dealloc, | |
3895 | run, | |
3896 | wait, | |
3897 | flush, | |
6c1491fb BP |
3898 | get_features, |
3899 | get_tables, | |
abe529af BP |
3900 | port_alloc, |
3901 | port_construct, | |
3902 | port_destruct, | |
3903 | port_dealloc, | |
3904 | port_modified, | |
3905 | port_reconfigured, | |
3906 | port_query_by_name, | |
3907 | port_add, | |
3908 | port_del, | |
3909 | port_dump_start, | |
3910 | port_dump_next, | |
3911 | port_dump_done, | |
3912 | port_poll, | |
3913 | port_poll_wait, | |
3914 | port_is_lacp_current, | |
3915 | rule_alloc, | |
3916 | rule_construct, | |
3917 | rule_destruct, | |
3918 | rule_dealloc, | |
abe529af BP |
3919 | rule_get_stats, |
3920 | rule_execute, | |
3921 | rule_modify_actions, | |
3922 | get_drop_frags, | |
3923 | set_drop_frags, | |
3924 | packet_out, | |
3925 | set_netflow, | |
3926 | get_netflow_ids, | |
3927 | set_sflow, | |
3928 | set_cfm, | |
3929 | get_cfm, | |
3930 | bundle_set, | |
3931 | bundle_remove, | |
3932 | mirror_set, | |
3933 | set_flood_vlans, | |
3934 | is_mirror_output_bundle, | |
3935 | }; |