2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ofproto/ofproto-provider.h"
26 #include "byte-order.h"
31 #include "dynamic-string.h"
32 #include "fail-open.h"
36 #include "mac-learning.h"
37 #include "multipath.h"
44 #include "ofp-print.h"
45 #include "ofproto-dpif-sflow.h"
46 #include "poll-loop.h"
48 #include "unaligned.h"
50 #include "vlan-bitmap.h"
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
55 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
56 COVERAGE_DEFINE(ofproto_dpif_expired);
57 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
58 COVERAGE_DEFINE(ofproto_dpif_xlate);
59 COVERAGE_DEFINE(facet_changed_rule);
60 COVERAGE_DEFINE(facet_invalidated);
61 COVERAGE_DEFINE(facet_revalidate);
62 COVERAGE_DEFINE(facet_unexpected);
64 /* Maximum depth of flow table recursion (due to resubmit actions) in a
65 * flow translation. */
66 #define MAX_RESUBMIT_RECURSION 32
68 /* Number of implemented OpenFlow tables. */
69 enum { N_TABLES = 255 };
70 BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255);
78 long long int used; /* Time last used; time created if not used. */
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
91 * datapath by, e.g., dpif_flow_get() for any subfacet.
93 uint64_t packet_count; /* Number of packets received. */
94 uint64_t byte_count; /* Number of bytes received. */
96 tag_type tag; /* Caches rule_calculate_tag() result. */
98 struct list facets; /* List of "struct facet"s. */
101 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
103 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
106 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
107 const struct flow *, uint8_t table);
109 static void flow_push_stats(const struct rule_dpif *, const struct flow *,
110 uint64_t packets, uint64_t bytes,
113 static uint32_t rule_calculate_tag(const struct flow *,
114 const struct flow_wildcards *,
116 static void rule_invalidate(const struct rule_dpif *);
118 #define MAX_MIRRORS 32
119 typedef uint32_t mirror_mask_t;
120 #define MIRROR_MASK_C(X) UINT32_C(X)
121 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
123 struct ofproto_dpif *ofproto; /* Owning ofproto. */
124 size_t idx; /* In ofproto's "mirrors" array. */
125 void *aux; /* Key supplied by ofproto's client. */
126 char *name; /* Identifier for log messages. */
128 /* Selection criteria. */
129 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
130 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
131 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
133 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
134 struct ofbundle *out; /* Output port or NULL. */
135 int out_vlan; /* Output VLAN or -1. */
136 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
139 static void mirror_destroy(struct ofmirror *);
141 /* A group of one or more OpenFlow ports. */
142 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
144 struct ofproto_dpif *ofproto; /* Owning ofproto. */
145 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
146 void *aux; /* Key supplied by ofproto's client. */
147 char *name; /* Identifier for log messages. */
150 struct list ports; /* Contains "struct ofport"s. */
151 enum port_vlan_mode vlan_mode; /* VLAN mode */
152 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
153 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
154 * NULL if all VLANs are trunked. */
155 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
156 struct bond *bond; /* Nonnull iff more than one port. */
157 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
160 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
162 /* Port mirroring info. */
163 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
164 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
165 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
168 static void bundle_remove(struct ofport *);
169 static void bundle_update(struct ofbundle *);
170 static void bundle_destroy(struct ofbundle *);
171 static void bundle_del_port(struct ofport_dpif *);
172 static void bundle_run(struct ofbundle *);
173 static void bundle_wait(struct ofbundle *);
175 static void stp_run(struct ofproto_dpif *ofproto);
176 static void stp_wait(struct ofproto_dpif *ofproto);
178 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
180 struct action_xlate_ctx {
181 /* action_xlate_ctx_init() initializes these members. */
184 struct ofproto_dpif *ofproto;
186 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
187 * this flow when actions change header fields. */
190 /* The packet corresponding to 'flow', or a null pointer if we are
191 * revalidating without a packet to refer to. */
192 const struct ofpbuf *packet;
194 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
195 * want to execute them if we are actually processing a packet, or if we
196 * are accounting for packets that the datapath has processed, but not if
197 * we are just revalidating. */
200 /* If nonnull, called just before executing a resubmit action.
202 * This is normally null so the client has to set it manually after
203 * calling action_xlate_ctx_init(). */
204 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
206 /* xlate_actions() initializes and uses these members. The client might want
207 * to look at them after it returns. */
209 struct ofpbuf *odp_actions; /* Datapath actions. */
210 tag_type tags; /* Tags associated with actions. */
211 bool may_set_up_flow; /* True ordinarily; false if the actions must
212 * be reassessed for every packet. */
213 bool has_learn; /* Actions include NXAST_LEARN? */
214 bool has_normal; /* Actions output to OFPP_NORMAL? */
215 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
217 /* xlate_actions() initializes and uses these members, but the client has no
218 * reason to look at them. */
220 int recurse; /* Recursion level, via xlate_table_action. */
221 struct flow base_flow; /* Flow at the last commit. */
222 uint32_t original_priority; /* Priority when packet arrived. */
223 uint8_t table_id; /* OpenFlow table ID where flow was found. */
224 uint32_t sflow_n_outputs; /* Number of output ports. */
225 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
226 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
227 bool exit; /* No further actions should be processed. */
230 static void action_xlate_ctx_init(struct action_xlate_ctx *,
231 struct ofproto_dpif *, const struct flow *,
232 ovs_be16 initial_tci, const struct ofpbuf *);
233 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
234 const union ofp_action *in, size_t n_in);
236 /* An exact-match instantiation of an OpenFlow flow.
238 * A facet associates a "struct flow", which represents the Open vSwitch
239 * userspace idea of an exact-match flow, with one or more subfacets. Each
240 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
241 * the facet. When the kernel module (or other dpif implementation) and Open
242 * vSwitch userspace agree on the definition of a flow key, there is exactly
243 * one subfacet per facet. If the dpif implementation supports more-specific
244 * flow matching than userspace, however, a facet can have more than one
245 * subfacet, each of which corresponds to some distinction in flow that
246 * userspace simply doesn't understand.
248 * Flow expiration works in terms of subfacets, so a facet must have at least
249 * one subfacet or it will never expire, leaking memory. */
252 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
253 struct list list_node; /* In owning rule's 'facets' list. */
254 struct rule_dpif *rule; /* Owning rule. */
257 struct list subfacets;
258 long long int used; /* Time last used; time created if not used. */
265 * - Do include packets and bytes sent "by hand", e.g. with
268 * - Do include packets and bytes that were obtained from the datapath
269 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
270 * DPIF_FP_ZERO_STATS).
272 * - Do not include packets or bytes that can be obtained from the
273 * datapath for any existing subfacet.
275 uint64_t packet_count; /* Number of packets received. */
276 uint64_t byte_count; /* Number of bytes received. */
278 /* Resubmit statistics. */
279 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
280 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
281 long long int rs_used; /* Used time pushed to resubmit children. */
284 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
285 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
287 /* Properties of datapath actions.
289 * Every subfacet has its own actions because actions can differ slightly
290 * between splintered and non-splintered subfacets due to the VLAN tag
291 * being initially different (present vs. absent). All of them have these
292 * properties in common so we just store one copy of them here. */
293 bool may_install; /* Reassess actions for every packet? */
294 bool has_learn; /* Actions include NXAST_LEARN? */
295 bool has_normal; /* Actions output to OFPP_NORMAL? */
296 tag_type tags; /* Tags that would require revalidation. */
299 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
300 static void facet_remove(struct ofproto_dpif *, struct facet *);
301 static void facet_free(struct facet *);
303 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
304 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
305 const struct flow *);
306 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
308 static bool execute_controller_action(struct ofproto_dpif *,
310 const struct nlattr *odp_actions,
312 struct ofpbuf *packet);
314 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
316 static void facet_update_time(struct ofproto_dpif *, struct facet *,
318 static void facet_reset_counters(struct facet *);
319 static void facet_push_stats(struct facet *);
320 static void facet_account(struct ofproto_dpif *, struct facet *);
322 static bool facet_is_controller_flow(struct facet *);
324 /* A dpif flow and actions associated with a facet.
326 * See also the large comment on struct facet. */
329 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
330 struct list list_node; /* In struct facet's 'facets' list. */
331 struct facet *facet; /* Owning facet. */
335 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
336 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
337 * regenerate the ODP flow key from ->facet->flow. */
338 enum odp_key_fitness key_fitness;
342 long long int used; /* Time last used; time created if not used. */
344 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
345 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
349 * These should be essentially identical for every subfacet in a facet, but
350 * may differ in trivial ways due to VLAN splinters. */
351 size_t actions_len; /* Number of bytes in actions[]. */
352 struct nlattr *actions; /* Datapath actions. */
354 bool installed; /* Installed in datapath? */
356 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
357 * splinters can cause it to differ. This value should be removed when
358 * the VLAN splinters feature is no longer needed. */
359 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
362 static struct subfacet *subfacet_create(struct ofproto_dpif *, struct facet *,
363 enum odp_key_fitness,
364 const struct nlattr *key,
365 size_t key_len, ovs_be16 initial_tci);
366 static struct subfacet *subfacet_find(struct ofproto_dpif *,
367 const struct nlattr *key, size_t key_len,
368 const struct flow *);
369 static void subfacet_destroy(struct ofproto_dpif *, struct subfacet *);
370 static void subfacet_destroy__(struct ofproto_dpif *, struct subfacet *);
371 static void subfacet_reset_dp_stats(struct subfacet *,
372 struct dpif_flow_stats *);
373 static void subfacet_update_time(struct ofproto_dpif *, struct subfacet *,
375 static void subfacet_update_stats(struct ofproto_dpif *, struct subfacet *,
376 const struct dpif_flow_stats *);
377 static void subfacet_make_actions(struct ofproto_dpif *, struct subfacet *,
378 const struct ofpbuf *packet);
379 static int subfacet_install(struct ofproto_dpif *, struct subfacet *,
380 const struct nlattr *actions, size_t actions_len,
381 struct dpif_flow_stats *);
382 static void subfacet_uninstall(struct ofproto_dpif *, struct subfacet *);
388 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
389 struct list bundle_node; /* In struct ofbundle's "ports" list. */
390 struct cfm *cfm; /* Connectivity Fault Management, if any. */
391 tag_type tag; /* Tag associated with this port. */
392 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
393 bool may_enable; /* May be enabled in bonds. */
395 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
396 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
397 long long int stp_state_entered;
399 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
402 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
403 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
404 * traffic egressing the 'ofport' with that priority should be marked with. */
405 struct priority_to_dscp {
406 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
407 uint32_t priority; /* Priority of this queue (see struct flow). */
409 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
412 static struct ofport_dpif *
413 ofport_dpif_cast(const struct ofport *ofport)
415 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
416 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
419 static void port_run(struct ofport_dpif *);
420 static void port_wait(struct ofport_dpif *);
421 static int set_cfm(struct ofport *, const struct cfm_settings *);
422 static void ofport_clear_priorities(struct ofport_dpif *);
424 struct dpif_completion {
425 struct list list_node;
426 struct ofoperation *op;
429 /* Extra information about a classifier table.
430 * Currently used just for optimized flow revalidation. */
432 /* If either of these is nonnull, then this table has a form that allows
433 * flows to be tagged to avoid revalidating most flows for the most common
434 * kinds of flow table changes. */
435 struct cls_table *catchall_table; /* Table that wildcards all fields. */
436 struct cls_table *other_table; /* Table with any other wildcard set. */
437 uint32_t basis; /* Keeps each table's tags separate. */
440 struct ofproto_dpif {
449 struct netflow *netflow;
450 struct dpif_sflow *sflow;
451 struct hmap bundles; /* Contains "struct ofbundle"s. */
452 struct mac_learning *ml;
453 struct ofmirror *mirrors[MAX_MIRRORS];
454 bool has_bonded_bundles;
457 struct timer next_expiration;
461 struct hmap subfacets;
464 struct table_dpif tables[N_TABLES];
465 bool need_revalidate;
466 struct tag_set revalidate_set;
468 /* Support for debugging async flow mods. */
469 struct list completions;
471 bool has_bundle_action; /* True when the first bundle action appears. */
475 long long int stp_last_tick;
478 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
479 * for debugging the asynchronous flow_mod implementation.) */
482 static void ofproto_dpif_unixctl_init(void);
484 static struct ofproto_dpif *
485 ofproto_dpif_cast(const struct ofproto *ofproto)
487 assert(ofproto->ofproto_class == &ofproto_dpif_class);
488 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
491 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
493 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
496 /* Packet processing. */
497 static void update_learning_table(struct ofproto_dpif *,
498 const struct flow *, int vlan,
501 #define FLOW_MISS_MAX_BATCH 50
503 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
504 static void handle_miss_upcalls(struct ofproto_dpif *,
505 struct dpif_upcall *, size_t n);
507 /* Flow expiration. */
508 static int expire(struct ofproto_dpif *);
511 static void send_netflow_active_timeouts(struct ofproto_dpif *);
514 static int send_packet(const struct ofport_dpif *,
515 const struct ofpbuf *packet);
517 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
518 const struct flow *, uint32_t odp_port);
519 /* Global variables. */
520 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
522 /* Factory functions. */
525 enumerate_types(struct sset *types)
527 dp_enumerate_types(types);
531 enumerate_names(const char *type, struct sset *names)
533 return dp_enumerate_names(type, names);
537 del(const char *type, const char *name)
542 error = dpif_open(name, type, &dpif);
544 error = dpif_delete(dpif);
550 /* Basic life-cycle. */
552 static struct ofproto *
555 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
560 dealloc(struct ofproto *ofproto_)
562 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
567 construct(struct ofproto *ofproto_, int *n_tablesp)
569 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
570 const char *name = ofproto->up.name;
574 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
576 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
580 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
581 ofproto->n_matches = 0;
583 dpif_flow_flush(ofproto->dpif);
584 dpif_recv_purge(ofproto->dpif);
586 error = dpif_recv_set_mask(ofproto->dpif,
587 ((1u << DPIF_UC_MISS) |
588 (1u << DPIF_UC_ACTION)));
590 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
591 dpif_close(ofproto->dpif);
595 ofproto->netflow = NULL;
596 ofproto->sflow = NULL;
598 hmap_init(&ofproto->bundles);
599 ofproto->ml = mac_learning_create();
600 for (i = 0; i < MAX_MIRRORS; i++) {
601 ofproto->mirrors[i] = NULL;
603 ofproto->has_bonded_bundles = false;
605 timer_set_duration(&ofproto->next_expiration, 1000);
607 hmap_init(&ofproto->facets);
608 hmap_init(&ofproto->subfacets);
610 for (i = 0; i < N_TABLES; i++) {
611 struct table_dpif *table = &ofproto->tables[i];
613 table->catchall_table = NULL;
614 table->other_table = NULL;
615 table->basis = random_uint32();
617 ofproto->need_revalidate = false;
618 tag_set_init(&ofproto->revalidate_set);
620 list_init(&ofproto->completions);
622 ofproto_dpif_unixctl_init();
624 ofproto->has_bundle_action = false;
626 *n_tablesp = N_TABLES;
631 complete_operations(struct ofproto_dpif *ofproto)
633 struct dpif_completion *c, *next;
635 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
636 ofoperation_complete(c->op, 0);
637 list_remove(&c->list_node);
643 destruct(struct ofproto *ofproto_)
645 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
646 struct rule_dpif *rule, *next_rule;
647 struct classifier *table;
650 complete_operations(ofproto);
652 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
653 struct cls_cursor cursor;
655 cls_cursor_init(&cursor, table, NULL);
656 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
657 ofproto_rule_destroy(&rule->up);
661 for (i = 0; i < MAX_MIRRORS; i++) {
662 mirror_destroy(ofproto->mirrors[i]);
665 netflow_destroy(ofproto->netflow);
666 dpif_sflow_destroy(ofproto->sflow);
667 hmap_destroy(&ofproto->bundles);
668 mac_learning_destroy(ofproto->ml);
670 hmap_destroy(&ofproto->facets);
671 hmap_destroy(&ofproto->subfacets);
673 dpif_close(ofproto->dpif);
677 run(struct ofproto *ofproto_)
679 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
680 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
681 struct ofport_dpif *ofport;
682 struct ofbundle *bundle;
687 complete_operations(ofproto);
689 dpif_run(ofproto->dpif);
692 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
693 struct dpif_upcall *upcall = &misses[n_misses];
696 error = dpif_recv(ofproto->dpif, upcall);
698 if (error == ENODEV && n_misses == 0) {
704 if (upcall->type == DPIF_UC_MISS) {
705 /* Handle it later. */
708 handle_upcall(ofproto, upcall);
712 handle_miss_upcalls(ofproto, misses, n_misses);
714 if (timer_expired(&ofproto->next_expiration)) {
715 int delay = expire(ofproto);
716 timer_set_duration(&ofproto->next_expiration, delay);
719 if (ofproto->netflow) {
720 if (netflow_run(ofproto->netflow)) {
721 send_netflow_active_timeouts(ofproto);
724 if (ofproto->sflow) {
725 dpif_sflow_run(ofproto->sflow);
728 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
731 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
736 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
738 /* Now revalidate if there's anything to do. */
739 if (ofproto->need_revalidate
740 || !tag_set_is_empty(&ofproto->revalidate_set)) {
741 struct tag_set revalidate_set = ofproto->revalidate_set;
742 bool revalidate_all = ofproto->need_revalidate;
743 struct facet *facet, *next;
745 /* Clear the revalidation flags. */
746 tag_set_init(&ofproto->revalidate_set);
747 ofproto->need_revalidate = false;
749 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
751 || tag_set_intersects(&revalidate_set, facet->tags)) {
752 facet_revalidate(ofproto, facet);
761 wait(struct ofproto *ofproto_)
763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
764 struct ofport_dpif *ofport;
765 struct ofbundle *bundle;
767 if (!clogged && !list_is_empty(&ofproto->completions)) {
768 poll_immediate_wake();
771 dpif_wait(ofproto->dpif);
772 dpif_recv_wait(ofproto->dpif);
773 if (ofproto->sflow) {
774 dpif_sflow_wait(ofproto->sflow);
776 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
777 poll_immediate_wake();
779 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
782 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
785 if (ofproto->netflow) {
786 netflow_wait(ofproto->netflow);
788 mac_learning_wait(ofproto->ml);
790 if (ofproto->need_revalidate) {
791 /* Shouldn't happen, but if it does just go around again. */
792 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
793 poll_immediate_wake();
795 timer_wait(&ofproto->next_expiration);
800 flush(struct ofproto *ofproto_)
802 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
803 struct facet *facet, *next_facet;
805 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
806 /* Mark the facet as not installed so that facet_remove() doesn't
807 * bother trying to uninstall it. There is no point in uninstalling it
808 * individually since we are about to blow away all the facets with
809 * dpif_flow_flush(). */
810 struct subfacet *subfacet;
812 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
813 subfacet->installed = false;
814 subfacet->dp_packet_count = 0;
815 subfacet->dp_byte_count = 0;
817 facet_remove(ofproto, facet);
819 dpif_flow_flush(ofproto->dpif);
823 get_features(struct ofproto *ofproto_ OVS_UNUSED,
824 bool *arp_match_ip, uint32_t *actions)
826 *arp_match_ip = true;
827 *actions = ((1u << OFPAT_OUTPUT) |
828 (1u << OFPAT_SET_VLAN_VID) |
829 (1u << OFPAT_SET_VLAN_PCP) |
830 (1u << OFPAT_STRIP_VLAN) |
831 (1u << OFPAT_SET_DL_SRC) |
832 (1u << OFPAT_SET_DL_DST) |
833 (1u << OFPAT_SET_NW_SRC) |
834 (1u << OFPAT_SET_NW_DST) |
835 (1u << OFPAT_SET_NW_TOS) |
836 (1u << OFPAT_SET_TP_SRC) |
837 (1u << OFPAT_SET_TP_DST) |
838 (1u << OFPAT_ENQUEUE));
842 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
844 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
845 struct dpif_dp_stats s;
847 strcpy(ots->name, "classifier");
849 dpif_get_dp_stats(ofproto->dpif, &s);
850 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
851 put_32aligned_be64(&ots->matched_count,
852 htonll(s.n_hit + ofproto->n_matches));
855 static struct ofport *
858 struct ofport_dpif *port = xmalloc(sizeof *port);
863 port_dealloc(struct ofport *port_)
865 struct ofport_dpif *port = ofport_dpif_cast(port_);
870 port_construct(struct ofport *port_)
872 struct ofport_dpif *port = ofport_dpif_cast(port_);
873 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
875 ofproto->need_revalidate = true;
876 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
879 port->tag = tag_create_random();
880 port->may_enable = true;
881 port->stp_port = NULL;
882 port->stp_state = STP_DISABLED;
883 hmap_init(&port->priorities);
885 if (ofproto->sflow) {
886 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
887 netdev_get_name(port->up.netdev));
894 port_destruct(struct ofport *port_)
896 struct ofport_dpif *port = ofport_dpif_cast(port_);
897 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
899 ofproto->need_revalidate = true;
900 bundle_remove(port_);
901 set_cfm(port_, NULL);
902 if (ofproto->sflow) {
903 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
906 ofport_clear_priorities(port);
907 hmap_destroy(&port->priorities);
911 port_modified(struct ofport *port_)
913 struct ofport_dpif *port = ofport_dpif_cast(port_);
915 if (port->bundle && port->bundle->bond) {
916 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
921 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
923 struct ofport_dpif *port = ofport_dpif_cast(port_);
924 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
925 ovs_be32 changed = old_config ^ port->up.opp.config;
927 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
928 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
929 ofproto->need_revalidate = true;
931 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
932 bundle_update(port->bundle);
938 set_sflow(struct ofproto *ofproto_,
939 const struct ofproto_sflow_options *sflow_options)
941 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
942 struct dpif_sflow *ds = ofproto->sflow;
946 struct ofport_dpif *ofport;
948 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
949 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
950 dpif_sflow_add_port(ds, ofport->odp_port,
951 netdev_get_name(ofport->up.netdev));
953 ofproto->need_revalidate = true;
955 dpif_sflow_set_options(ds, sflow_options);
958 dpif_sflow_destroy(ds);
959 ofproto->need_revalidate = true;
960 ofproto->sflow = NULL;
967 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
969 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
976 struct ofproto_dpif *ofproto;
978 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
979 ofproto->need_revalidate = true;
980 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
983 if (cfm_configure(ofport->cfm, s)) {
989 cfm_destroy(ofport->cfm);
995 get_cfm_fault(const struct ofport *ofport_)
997 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
999 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
1003 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1006 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1009 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1016 /* Spanning Tree. */
1019 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1021 struct ofproto_dpif *ofproto = ofproto_;
1022 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1023 struct ofport_dpif *ofport;
1025 ofport = stp_port_get_aux(sp);
1027 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1028 ofproto->up.name, port_num);
1030 struct eth_header *eth = pkt->l2;
1032 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1033 if (eth_addr_is_zero(eth->eth_src)) {
1034 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1035 "with unknown MAC", ofproto->up.name, port_num);
1037 send_packet(ofport, pkt);
1043 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1045 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1047 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1049 /* Only revalidate flows if the configuration changed. */
1050 if (!s != !ofproto->stp) {
1051 ofproto->need_revalidate = true;
1055 if (!ofproto->stp) {
1056 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1057 send_bpdu_cb, ofproto);
1058 ofproto->stp_last_tick = time_msec();
1061 stp_set_bridge_id(ofproto->stp, s->system_id);
1062 stp_set_bridge_priority(ofproto->stp, s->priority);
1063 stp_set_hello_time(ofproto->stp, s->hello_time);
1064 stp_set_max_age(ofproto->stp, s->max_age);
1065 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1067 stp_destroy(ofproto->stp);
1068 ofproto->stp = NULL;
1075 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1081 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1082 s->designated_root = stp_get_designated_root(ofproto->stp);
1083 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1092 update_stp_port_state(struct ofport_dpif *ofport)
1094 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1095 enum stp_state state;
1097 /* Figure out new state. */
1098 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1102 if (ofport->stp_state != state) {
1106 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1107 netdev_get_name(ofport->up.netdev),
1108 stp_state_name(ofport->stp_state),
1109 stp_state_name(state));
1110 if (stp_learn_in_state(ofport->stp_state)
1111 != stp_learn_in_state(state)) {
1112 /* xxx Learning action flows should also be flushed. */
1113 mac_learning_flush(ofproto->ml);
1115 fwd_change = stp_forward_in_state(ofport->stp_state)
1116 != stp_forward_in_state(state);
1118 ofproto->need_revalidate = true;
1119 ofport->stp_state = state;
1120 ofport->stp_state_entered = time_msec();
1122 if (fwd_change && ofport->bundle) {
1123 bundle_update(ofport->bundle);
1126 /* Update the STP state bits in the OpenFlow port description. */
1127 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1128 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1129 : state == STP_LEARNING ? OFPPS_STP_LEARN
1130 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1131 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1133 ofproto_port_set_state(&ofport->up, of_state);
1137 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1138 * caller is responsible for assigning STP port numbers and ensuring
1139 * there are no duplicates. */
1141 set_stp_port(struct ofport *ofport_,
1142 const struct ofproto_port_stp_settings *s)
1144 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1145 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1146 struct stp_port *sp = ofport->stp_port;
1148 if (!s || !s->enable) {
1150 ofport->stp_port = NULL;
1151 stp_port_disable(sp);
1152 update_stp_port_state(ofport);
1155 } else if (sp && stp_port_no(sp) != s->port_num
1156 && ofport == stp_port_get_aux(sp)) {
1157 /* The port-id changed, so disable the old one if it's not
1158 * already in use by another port. */
1159 stp_port_disable(sp);
1162 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1163 stp_port_enable(sp);
1165 stp_port_set_aux(sp, ofport);
1166 stp_port_set_priority(sp, s->priority);
1167 stp_port_set_path_cost(sp, s->path_cost);
1169 update_stp_port_state(ofport);
1175 get_stp_port_status(struct ofport *ofport_,
1176 struct ofproto_port_stp_status *s)
1178 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1179 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1180 struct stp_port *sp = ofport->stp_port;
1182 if (!ofproto->stp || !sp) {
1188 s->port_id = stp_port_get_id(sp);
1189 s->state = stp_port_get_state(sp);
1190 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1191 s->role = stp_port_get_role(sp);
1192 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1198 stp_run(struct ofproto_dpif *ofproto)
1201 long long int now = time_msec();
1202 long long int elapsed = now - ofproto->stp_last_tick;
1203 struct stp_port *sp;
1206 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1207 ofproto->stp_last_tick = now;
1209 while (stp_get_changed_port(ofproto->stp, &sp)) {
1210 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1213 update_stp_port_state(ofport);
1220 stp_wait(struct ofproto_dpif *ofproto)
1223 poll_timer_wait(1000);
1227 /* Returns true if STP should process 'flow'. */
1229 stp_should_process_flow(const struct flow *flow)
1231 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1235 stp_process_packet(const struct ofport_dpif *ofport,
1236 const struct ofpbuf *packet)
1238 struct ofpbuf payload = *packet;
1239 struct eth_header *eth = payload.data;
1240 struct stp_port *sp = ofport->stp_port;
1242 /* Sink packets on ports that have STP disabled when the bridge has
1244 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1248 /* Trim off padding on payload. */
1249 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1250 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1253 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1254 stp_received_bpdu(sp, payload.data, payload.size);
1258 static struct priority_to_dscp *
1259 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1261 struct priority_to_dscp *pdscp;
1264 hash = hash_int(priority, 0);
1265 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1266 if (pdscp->priority == priority) {
1274 ofport_clear_priorities(struct ofport_dpif *ofport)
1276 struct priority_to_dscp *pdscp, *next;
1278 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1279 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1285 set_queues(struct ofport *ofport_,
1286 const struct ofproto_port_queue *qdscp_list,
1289 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1290 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1291 struct hmap new = HMAP_INITIALIZER(&new);
1294 for (i = 0; i < n_qdscp; i++) {
1295 struct priority_to_dscp *pdscp;
1299 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1300 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1305 pdscp = get_priority(ofport, priority);
1307 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1309 pdscp = xmalloc(sizeof *pdscp);
1310 pdscp->priority = priority;
1312 ofproto->need_revalidate = true;
1315 if (pdscp->dscp != dscp) {
1317 ofproto->need_revalidate = true;
1320 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1323 if (!hmap_is_empty(&ofport->priorities)) {
1324 ofport_clear_priorities(ofport);
1325 ofproto->need_revalidate = true;
1328 hmap_swap(&new, &ofport->priorities);
1336 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1337 * to revalidate every flow. */
1339 bundle_flush_macs(struct ofbundle *bundle)
1341 struct ofproto_dpif *ofproto = bundle->ofproto;
1342 struct mac_learning *ml = ofproto->ml;
1343 struct mac_entry *mac, *next_mac;
1345 ofproto->need_revalidate = true;
1346 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1347 if (mac->port.p == bundle) {
1348 mac_learning_expire(ml, mac);
1353 static struct ofbundle *
1354 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1356 struct ofbundle *bundle;
1358 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1359 &ofproto->bundles) {
1360 if (bundle->aux == aux) {
1367 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1368 * ones that are found to 'bundles'. */
1370 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1371 void **auxes, size_t n_auxes,
1372 struct hmapx *bundles)
1376 hmapx_init(bundles);
1377 for (i = 0; i < n_auxes; i++) {
1378 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1380 hmapx_add(bundles, bundle);
1386 bundle_update(struct ofbundle *bundle)
1388 struct ofport_dpif *port;
1390 bundle->floodable = true;
1391 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1392 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1393 bundle->floodable = false;
1400 bundle_del_port(struct ofport_dpif *port)
1402 struct ofbundle *bundle = port->bundle;
1404 bundle->ofproto->need_revalidate = true;
1406 list_remove(&port->bundle_node);
1407 port->bundle = NULL;
1410 lacp_slave_unregister(bundle->lacp, port);
1413 bond_slave_unregister(bundle->bond, port);
1416 bundle_update(bundle);
1420 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1421 struct lacp_slave_settings *lacp,
1422 uint32_t bond_stable_id)
1424 struct ofport_dpif *port;
1426 port = get_ofp_port(bundle->ofproto, ofp_port);
1431 if (port->bundle != bundle) {
1432 bundle->ofproto->need_revalidate = true;
1434 bundle_del_port(port);
1437 port->bundle = bundle;
1438 list_push_back(&bundle->ports, &port->bundle_node);
1439 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1440 bundle->floodable = false;
1444 port->bundle->ofproto->need_revalidate = true;
1445 lacp_slave_register(bundle->lacp, port, lacp);
1448 port->bond_stable_id = bond_stable_id;
1454 bundle_destroy(struct ofbundle *bundle)
1456 struct ofproto_dpif *ofproto;
1457 struct ofport_dpif *port, *next_port;
1464 ofproto = bundle->ofproto;
1465 for (i = 0; i < MAX_MIRRORS; i++) {
1466 struct ofmirror *m = ofproto->mirrors[i];
1468 if (m->out == bundle) {
1470 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1471 || hmapx_find_and_delete(&m->dsts, bundle)) {
1472 ofproto->need_revalidate = true;
1477 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1478 bundle_del_port(port);
1481 bundle_flush_macs(bundle);
1482 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1484 free(bundle->trunks);
1485 lacp_destroy(bundle->lacp);
1486 bond_destroy(bundle->bond);
1491 bundle_set(struct ofproto *ofproto_, void *aux,
1492 const struct ofproto_bundle_settings *s)
1494 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1495 bool need_flush = false;
1496 struct ofport_dpif *port;
1497 struct ofbundle *bundle;
1498 unsigned long *trunks;
1504 bundle_destroy(bundle_lookup(ofproto, aux));
1508 assert(s->n_slaves == 1 || s->bond != NULL);
1509 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1511 bundle = bundle_lookup(ofproto, aux);
1513 bundle = xmalloc(sizeof *bundle);
1515 bundle->ofproto = ofproto;
1516 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1517 hash_pointer(aux, 0));
1519 bundle->name = NULL;
1521 list_init(&bundle->ports);
1522 bundle->vlan_mode = PORT_VLAN_TRUNK;
1524 bundle->trunks = NULL;
1525 bundle->use_priority_tags = s->use_priority_tags;
1526 bundle->lacp = NULL;
1527 bundle->bond = NULL;
1529 bundle->floodable = true;
1531 bundle->src_mirrors = 0;
1532 bundle->dst_mirrors = 0;
1533 bundle->mirror_out = 0;
1536 if (!bundle->name || strcmp(s->name, bundle->name)) {
1538 bundle->name = xstrdup(s->name);
1543 if (!bundle->lacp) {
1544 ofproto->need_revalidate = true;
1545 bundle->lacp = lacp_create();
1547 lacp_configure(bundle->lacp, s->lacp);
1549 lacp_destroy(bundle->lacp);
1550 bundle->lacp = NULL;
1553 /* Update set of ports. */
1555 for (i = 0; i < s->n_slaves; i++) {
1556 if (!bundle_add_port(bundle, s->slaves[i],
1557 s->lacp ? &s->lacp_slaves[i] : NULL,
1558 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1562 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1563 struct ofport_dpif *next_port;
1565 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1566 for (i = 0; i < s->n_slaves; i++) {
1567 if (s->slaves[i] == port->up.ofp_port) {
1572 bundle_del_port(port);
1576 assert(list_size(&bundle->ports) <= s->n_slaves);
1578 if (list_is_empty(&bundle->ports)) {
1579 bundle_destroy(bundle);
1583 /* Set VLAN tagging mode */
1584 if (s->vlan_mode != bundle->vlan_mode
1585 || s->use_priority_tags != bundle->use_priority_tags) {
1586 bundle->vlan_mode = s->vlan_mode;
1587 bundle->use_priority_tags = s->use_priority_tags;
1592 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1593 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1595 if (vlan != bundle->vlan) {
1596 bundle->vlan = vlan;
1600 /* Get trunked VLANs. */
1601 switch (s->vlan_mode) {
1602 case PORT_VLAN_ACCESS:
1606 case PORT_VLAN_TRUNK:
1607 trunks = (unsigned long *) s->trunks;
1610 case PORT_VLAN_NATIVE_UNTAGGED:
1611 case PORT_VLAN_NATIVE_TAGGED:
1612 if (vlan != 0 && (!s->trunks
1613 || !bitmap_is_set(s->trunks, vlan)
1614 || bitmap_is_set(s->trunks, 0))) {
1615 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1617 trunks = bitmap_clone(s->trunks, 4096);
1619 trunks = bitmap_allocate1(4096);
1621 bitmap_set1(trunks, vlan);
1622 bitmap_set0(trunks, 0);
1624 trunks = (unsigned long *) s->trunks;
1631 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1632 free(bundle->trunks);
1633 if (trunks == s->trunks) {
1634 bundle->trunks = vlan_bitmap_clone(trunks);
1636 bundle->trunks = trunks;
1641 if (trunks != s->trunks) {
1646 if (!list_is_short(&bundle->ports)) {
1647 bundle->ofproto->has_bonded_bundles = true;
1649 if (bond_reconfigure(bundle->bond, s->bond)) {
1650 ofproto->need_revalidate = true;
1653 bundle->bond = bond_create(s->bond);
1654 ofproto->need_revalidate = true;
1657 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1658 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1662 bond_destroy(bundle->bond);
1663 bundle->bond = NULL;
1666 /* If we changed something that would affect MAC learning, un-learn
1667 * everything on this port and force flow revalidation. */
1669 bundle_flush_macs(bundle);
1676 bundle_remove(struct ofport *port_)
1678 struct ofport_dpif *port = ofport_dpif_cast(port_);
1679 struct ofbundle *bundle = port->bundle;
1682 bundle_del_port(port);
1683 if (list_is_empty(&bundle->ports)) {
1684 bundle_destroy(bundle);
1685 } else if (list_is_short(&bundle->ports)) {
1686 bond_destroy(bundle->bond);
1687 bundle->bond = NULL;
1693 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1696 struct ofport_dpif *port = port_;
1697 uint8_t ea[ETH_ADDR_LEN];
1700 error = netdev_get_etheraddr(port->up.netdev, ea);
1702 struct ofpbuf packet;
1705 ofpbuf_init(&packet, 0);
1706 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1708 memcpy(packet_pdu, pdu, pdu_size);
1710 send_packet(port, &packet);
1711 ofpbuf_uninit(&packet);
1713 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1714 "%s (%s)", port->bundle->name,
1715 netdev_get_name(port->up.netdev), strerror(error));
1720 bundle_send_learning_packets(struct ofbundle *bundle)
1722 struct ofproto_dpif *ofproto = bundle->ofproto;
1723 int error, n_packets, n_errors;
1724 struct mac_entry *e;
1726 error = n_packets = n_errors = 0;
1727 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1728 if (e->port.p != bundle) {
1729 struct ofpbuf *learning_packet;
1730 struct ofport_dpif *port;
1733 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1736 ret = send_packet(port, learning_packet);
1737 ofpbuf_delete(learning_packet);
1747 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1748 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1749 "packets, last error was: %s",
1750 bundle->name, n_errors, n_packets, strerror(error));
1752 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1753 bundle->name, n_packets);
1758 bundle_run(struct ofbundle *bundle)
1761 lacp_run(bundle->lacp, send_pdu_cb);
1764 struct ofport_dpif *port;
1766 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1767 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1770 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1771 lacp_negotiated(bundle->lacp));
1772 if (bond_should_send_learning_packets(bundle->bond)) {
1773 bundle_send_learning_packets(bundle);
1779 bundle_wait(struct ofbundle *bundle)
1782 lacp_wait(bundle->lacp);
1785 bond_wait(bundle->bond);
1792 mirror_scan(struct ofproto_dpif *ofproto)
1796 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1797 if (!ofproto->mirrors[idx]) {
1804 static struct ofmirror *
1805 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1809 for (i = 0; i < MAX_MIRRORS; i++) {
1810 struct ofmirror *mirror = ofproto->mirrors[i];
1811 if (mirror && mirror->aux == aux) {
1819 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1821 mirror_update_dups(struct ofproto_dpif *ofproto)
1825 for (i = 0; i < MAX_MIRRORS; i++) {
1826 struct ofmirror *m = ofproto->mirrors[i];
1829 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1833 for (i = 0; i < MAX_MIRRORS; i++) {
1834 struct ofmirror *m1 = ofproto->mirrors[i];
1841 for (j = i + 1; j < MAX_MIRRORS; j++) {
1842 struct ofmirror *m2 = ofproto->mirrors[j];
1844 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1845 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1846 m2->dup_mirrors |= m1->dup_mirrors;
1853 mirror_set(struct ofproto *ofproto_, void *aux,
1854 const struct ofproto_mirror_settings *s)
1856 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1857 mirror_mask_t mirror_bit;
1858 struct ofbundle *bundle;
1859 struct ofmirror *mirror;
1860 struct ofbundle *out;
1861 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1862 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1865 mirror = mirror_lookup(ofproto, aux);
1867 mirror_destroy(mirror);
1873 idx = mirror_scan(ofproto);
1875 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1877 ofproto->up.name, MAX_MIRRORS, s->name);
1881 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1882 mirror->ofproto = ofproto;
1885 mirror->out_vlan = -1;
1886 mirror->name = NULL;
1889 if (!mirror->name || strcmp(s->name, mirror->name)) {
1891 mirror->name = xstrdup(s->name);
1894 /* Get the new configuration. */
1895 if (s->out_bundle) {
1896 out = bundle_lookup(ofproto, s->out_bundle);
1898 mirror_destroy(mirror);
1904 out_vlan = s->out_vlan;
1906 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1907 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1909 /* If the configuration has not changed, do nothing. */
1910 if (hmapx_equals(&srcs, &mirror->srcs)
1911 && hmapx_equals(&dsts, &mirror->dsts)
1912 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1913 && mirror->out == out
1914 && mirror->out_vlan == out_vlan)
1916 hmapx_destroy(&srcs);
1917 hmapx_destroy(&dsts);
1921 hmapx_swap(&srcs, &mirror->srcs);
1922 hmapx_destroy(&srcs);
1924 hmapx_swap(&dsts, &mirror->dsts);
1925 hmapx_destroy(&dsts);
1927 free(mirror->vlans);
1928 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1931 mirror->out_vlan = out_vlan;
1933 /* Update bundles. */
1934 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1935 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1936 if (hmapx_contains(&mirror->srcs, bundle)) {
1937 bundle->src_mirrors |= mirror_bit;
1939 bundle->src_mirrors &= ~mirror_bit;
1942 if (hmapx_contains(&mirror->dsts, bundle)) {
1943 bundle->dst_mirrors |= mirror_bit;
1945 bundle->dst_mirrors &= ~mirror_bit;
1948 if (mirror->out == bundle) {
1949 bundle->mirror_out |= mirror_bit;
1951 bundle->mirror_out &= ~mirror_bit;
1955 ofproto->need_revalidate = true;
1956 mac_learning_flush(ofproto->ml);
1957 mirror_update_dups(ofproto);
1963 mirror_destroy(struct ofmirror *mirror)
1965 struct ofproto_dpif *ofproto;
1966 mirror_mask_t mirror_bit;
1967 struct ofbundle *bundle;
1973 ofproto = mirror->ofproto;
1974 ofproto->need_revalidate = true;
1975 mac_learning_flush(ofproto->ml);
1977 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1978 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1979 bundle->src_mirrors &= ~mirror_bit;
1980 bundle->dst_mirrors &= ~mirror_bit;
1981 bundle->mirror_out &= ~mirror_bit;
1984 hmapx_destroy(&mirror->srcs);
1985 hmapx_destroy(&mirror->dsts);
1986 free(mirror->vlans);
1988 ofproto->mirrors[mirror->idx] = NULL;
1992 mirror_update_dups(ofproto);
1996 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1998 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1999 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2000 ofproto->need_revalidate = true;
2001 mac_learning_flush(ofproto->ml);
2007 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2009 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2010 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2011 return bundle && bundle->mirror_out != 0;
2015 forward_bpdu_changed(struct ofproto *ofproto_)
2017 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2018 /* Revalidate cached flows whenever forward_bpdu option changes. */
2019 ofproto->need_revalidate = true;
2024 static struct ofport_dpif *
2025 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
2027 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2028 return ofport ? ofport_dpif_cast(ofport) : NULL;
2031 static struct ofport_dpif *
2032 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
2034 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2038 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2039 struct dpif_port *dpif_port)
2041 ofproto_port->name = dpif_port->name;
2042 ofproto_port->type = dpif_port->type;
2043 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2047 port_run(struct ofport_dpif *ofport)
2049 bool enable = netdev_get_carrier(ofport->up.netdev);
2052 cfm_run(ofport->cfm);
2054 if (cfm_should_send_ccm(ofport->cfm)) {
2055 struct ofpbuf packet;
2057 ofpbuf_init(&packet, 0);
2058 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
2059 send_packet(ofport, &packet);
2060 ofpbuf_uninit(&packet);
2063 enable = enable && !cfm_get_fault(ofport->cfm)
2064 && cfm_get_opup(ofport->cfm);
2067 if (ofport->bundle) {
2068 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2071 if (ofport->may_enable != enable) {
2072 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2074 if (ofproto->has_bundle_action) {
2075 ofproto->need_revalidate = true;
2079 ofport->may_enable = enable;
2083 port_wait(struct ofport_dpif *ofport)
2086 cfm_wait(ofport->cfm);
2091 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2092 struct ofproto_port *ofproto_port)
2094 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2095 struct dpif_port dpif_port;
2098 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2100 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2106 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2108 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2112 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2114 *ofp_portp = odp_port_to_ofp_port(odp_port);
2120 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2122 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2125 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2127 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2129 /* The caller is going to close ofport->up.netdev. If this is a
2130 * bonded port, then the bond is using that netdev, so remove it
2131 * from the bond. The client will need to reconfigure everything
2132 * after deleting ports, so then the slave will get re-added. */
2133 bundle_remove(&ofport->up);
2139 struct port_dump_state {
2140 struct dpif_port_dump dump;
2145 port_dump_start(const struct ofproto *ofproto_, void **statep)
2147 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2148 struct port_dump_state *state;
2150 *statep = state = xmalloc(sizeof *state);
2151 dpif_port_dump_start(&state->dump, ofproto->dpif);
2152 state->done = false;
2157 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2158 struct ofproto_port *port)
2160 struct port_dump_state *state = state_;
2161 struct dpif_port dpif_port;
2163 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2164 ofproto_port_from_dpif_port(port, &dpif_port);
2167 int error = dpif_port_dump_done(&state->dump);
2169 return error ? error : EOF;
2174 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2176 struct port_dump_state *state = state_;
2179 dpif_port_dump_done(&state->dump);
2186 port_poll(const struct ofproto *ofproto_, char **devnamep)
2188 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2189 return dpif_port_poll(ofproto->dpif, devnamep);
2193 port_poll_wait(const struct ofproto *ofproto_)
2195 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2196 dpif_port_poll_wait(ofproto->dpif);
2200 port_is_lacp_current(const struct ofport *ofport_)
2202 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2203 return (ofport->bundle && ofport->bundle->lacp
2204 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2208 /* Upcall handling. */
2210 /* Flow miss batching.
2212 * Some dpifs implement operations faster when you hand them off in a batch.
2213 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2214 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2215 * more packets, plus possibly installing the flow in the dpif.
2217 * So far we only batch the operations that affect flow setup time the most.
2218 * It's possible to batch more than that, but the benefit might be minimal. */
2220 struct hmap_node hmap_node;
2222 enum odp_key_fitness key_fitness;
2223 const struct nlattr *key;
2225 ovs_be16 initial_tci;
2226 struct list packets;
2229 struct flow_miss_op {
2230 union dpif_op dpif_op;
2231 struct subfacet *subfacet;
2234 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2235 * OpenFlow controller as necessary according to their individual
2238 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2239 * ownership is transferred to this function. */
2241 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2242 const struct flow *flow, bool clone)
2244 struct ofputil_packet_in pin;
2246 pin.packet = packet;
2247 pin.in_port = flow->in_port;
2248 pin.reason = OFPR_NO_MATCH;
2249 pin.buffer_id = 0; /* not yet known */
2250 pin.send_len = 0; /* not used for flow table misses */
2251 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2252 clone ? NULL : packet);
2255 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2256 * OpenFlow controller as necessary according to their individual
2259 * 'send_len' should be the number of bytes of 'packet' to send to the
2260 * controller, as specified in the action that caused the packet to be sent.
2262 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2263 * Otherwise, ownership is transferred to this function. */
2265 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2266 uint64_t userdata, const struct flow *flow, bool clone)
2268 struct ofputil_packet_in pin;
2269 struct user_action_cookie cookie;
2271 memcpy(&cookie, &userdata, sizeof(cookie));
2273 pin.packet = packet;
2274 pin.in_port = flow->in_port;
2275 pin.reason = OFPR_ACTION;
2276 pin.buffer_id = 0; /* not yet known */
2277 pin.send_len = cookie.data;
2278 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2279 clone ? NULL : packet);
2283 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2284 const struct ofpbuf *packet)
2286 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2292 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2294 cfm_process_heartbeat(ofport->cfm, packet);
2297 } else if (ofport->bundle && ofport->bundle->lacp
2298 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2300 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2303 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2305 stp_process_packet(ofport, packet);
2312 static struct flow_miss *
2313 flow_miss_create(struct hmap *todo, const struct flow *flow,
2314 enum odp_key_fitness key_fitness,
2315 const struct nlattr *key, size_t key_len,
2316 ovs_be16 initial_tci)
2318 uint32_t hash = flow_hash(flow, 0);
2319 struct flow_miss *miss;
2321 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2322 if (flow_equal(&miss->flow, flow)) {
2327 miss = xmalloc(sizeof *miss);
2328 hmap_insert(todo, &miss->hmap_node, hash);
2330 miss->key_fitness = key_fitness;
2332 miss->key_len = key_len;
2333 miss->initial_tci = initial_tci;
2334 list_init(&miss->packets);
2339 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2340 struct flow_miss_op *ops, size_t *n_ops)
2342 const struct flow *flow = &miss->flow;
2343 struct ofpbuf *packet, *next_packet;
2344 struct subfacet *subfacet;
2345 struct facet *facet;
2347 facet = facet_lookup_valid(ofproto, flow);
2349 struct rule_dpif *rule;
2351 rule = rule_dpif_lookup(ofproto, flow, 0);
2353 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2354 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2356 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2357 COVERAGE_INC(ofproto_dpif_no_packet_in);
2358 /* XXX install 'drop' flow entry */
2362 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2366 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2368 list_remove(&packet->list_node);
2369 send_packet_in_miss(ofproto, packet, flow, false);
2375 facet = facet_create(rule, flow);
2378 subfacet = subfacet_create(ofproto, facet,
2379 miss->key_fitness, miss->key, miss->key_len,
2382 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2383 list_remove(&packet->list_node);
2384 ofproto->n_matches++;
2386 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2388 * Extra-special case for fail-open mode.
2390 * We are in fail-open mode and the packet matched the fail-open
2391 * rule, but we are connected to a controller too. We should send
2392 * the packet up to the controller in the hope that it will try to
2393 * set up a flow and thereby allow us to exit fail-open.
2395 * See the top-level comment in fail-open.c for more information.
2397 send_packet_in_miss(ofproto, packet, flow, true);
2400 if (!facet->may_install || !subfacet->actions) {
2401 subfacet_make_actions(ofproto, subfacet, packet);
2403 if (!execute_controller_action(ofproto, &facet->flow,
2405 subfacet->actions_len, packet)) {
2406 struct flow_miss_op *op = &ops[(*n_ops)++];
2407 struct dpif_execute *execute = &op->dpif_op.execute;
2409 op->subfacet = subfacet;
2410 execute->type = DPIF_OP_EXECUTE;
2411 execute->key = miss->key;
2412 execute->key_len = miss->key_len;
2414 = (facet->may_install
2416 : xmemdup(subfacet->actions, subfacet->actions_len));
2417 execute->actions_len = subfacet->actions_len;
2418 execute->packet = packet;
2422 if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
2423 struct flow_miss_op *op = &ops[(*n_ops)++];
2424 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2426 op->subfacet = subfacet;
2427 put->type = DPIF_OP_FLOW_PUT;
2428 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2429 put->key = miss->key;
2430 put->key_len = miss->key_len;
2431 put->actions = subfacet->actions;
2432 put->actions_len = subfacet->actions_len;
2437 static enum odp_key_fitness
2438 ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto OVS_UNUSED,
2439 const struct nlattr *key, size_t key_len,
2440 struct flow *flow, ovs_be16 *initial_tci)
2442 enum odp_key_fitness fitness;
2444 fitness = odp_flow_key_to_flow(key, key_len, flow);
2445 if (fitness == ODP_FIT_ERROR) {
2448 *initial_tci = flow->vlan_tci;
2454 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2457 struct dpif_upcall *upcall;
2458 struct flow_miss *miss, *next_miss;
2459 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2460 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2469 /* Construct the to-do list.
2471 * This just amounts to extracting the flow from each packet and sticking
2472 * the packets that have the same flow in the same "flow_miss" structure so
2473 * that we can process them together. */
2475 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2476 enum odp_key_fitness fitness;
2477 struct flow_miss *miss;
2478 ovs_be16 initial_tci;
2481 /* Obtain metadata and check userspace/kernel agreement on flow match,
2482 * then set 'flow''s header pointers. */
2483 fitness = ofproto_dpif_extract_flow_key(ofproto,
2484 upcall->key, upcall->key_len,
2485 &flow, &initial_tci);
2486 if (fitness == ODP_FIT_ERROR) {
2489 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2490 flow.in_port, &flow);
2492 /* Handle 802.1ag, LACP, and STP specially. */
2493 if (process_special(ofproto, &flow, upcall->packet)) {
2494 ofpbuf_delete(upcall->packet);
2495 ofproto->n_matches++;
2499 /* Add other packets to a to-do list. */
2500 miss = flow_miss_create(&todo, &flow, fitness,
2501 upcall->key, upcall->key_len, initial_tci);
2502 list_push_back(&miss->packets, &upcall->packet->list_node);
2505 /* Process each element in the to-do list, constructing the set of
2506 * operations to batch. */
2508 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2509 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2510 ofpbuf_list_delete(&miss->packets);
2511 hmap_remove(&todo, &miss->hmap_node);
2514 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2515 hmap_destroy(&todo);
2517 /* Execute batch. */
2518 for (i = 0; i < n_ops; i++) {
2519 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2521 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2523 /* Free memory and update facets. */
2524 for (i = 0; i < n_ops; i++) {
2525 struct flow_miss_op *op = &flow_miss_ops[i];
2526 struct dpif_execute *execute;
2527 struct dpif_flow_put *put;
2529 switch (op->dpif_op.type) {
2530 case DPIF_OP_EXECUTE:
2531 execute = &op->dpif_op.execute;
2532 if (op->subfacet->actions != execute->actions) {
2533 free((struct nlattr *) execute->actions);
2535 ofpbuf_delete((struct ofpbuf *) execute->packet);
2538 case DPIF_OP_FLOW_PUT:
2539 put = &op->dpif_op.flow_put;
2541 op->subfacet->installed = true;
2549 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2550 struct dpif_upcall *upcall)
2552 struct user_action_cookie cookie;
2553 enum odp_key_fitness fitness;
2554 ovs_be16 initial_tci;
2557 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2559 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
2560 upcall->key_len, &flow,
2562 if (fitness == ODP_FIT_ERROR) {
2566 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2567 if (ofproto->sflow) {
2568 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
2571 ofpbuf_delete(upcall->packet);
2572 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2573 COVERAGE_INC(ofproto_dpif_ctlr_action);
2574 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2577 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2582 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2584 switch (upcall->type) {
2585 case DPIF_UC_ACTION:
2586 handle_userspace_upcall(ofproto, upcall);
2590 /* The caller handles these. */
2593 case DPIF_N_UC_TYPES:
2595 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2600 /* Flow expiration. */
2602 static int subfacet_max_idle(const struct ofproto_dpif *);
2603 static void update_stats(struct ofproto_dpif *);
2604 static void rule_expire(struct rule_dpif *);
2605 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
2607 /* This function is called periodically by run(). Its job is to collect
2608 * updates for the flows that have been installed into the datapath, most
2609 * importantly when they last were used, and then use that information to
2610 * expire flows that have not been used recently.
2612 * Returns the number of milliseconds after which it should be called again. */
2614 expire(struct ofproto_dpif *ofproto)
2616 struct rule_dpif *rule, *next_rule;
2617 struct classifier *table;
2620 /* Update stats for each flow in the datapath. */
2621 update_stats(ofproto);
2623 /* Expire subfacets that have been idle too long. */
2624 dp_max_idle = subfacet_max_idle(ofproto);
2625 expire_subfacets(ofproto, dp_max_idle);
2627 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2628 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2629 struct cls_cursor cursor;
2631 cls_cursor_init(&cursor, table, NULL);
2632 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2637 /* All outstanding data in existing flows has been accounted, so it's a
2638 * good time to do bond rebalancing. */
2639 if (ofproto->has_bonded_bundles) {
2640 struct ofbundle *bundle;
2642 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2644 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2649 return MIN(dp_max_idle, 1000);
2652 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2654 * This function also pushes statistics updates to rules which each facet
2655 * resubmits into. Generally these statistics will be accurate. However, if a
2656 * facet changes the rule it resubmits into at some time in between
2657 * update_stats() runs, it is possible that statistics accrued to the
2658 * old rule will be incorrectly attributed to the new rule. This could be
2659 * avoided by calling update_stats() whenever rules are created or
2660 * deleted. However, the performance impact of making so many calls to the
2661 * datapath do not justify the benefit of having perfectly accurate statistics.
2664 update_stats(struct ofproto_dpif *p)
2666 const struct dpif_flow_stats *stats;
2667 struct dpif_flow_dump dump;
2668 const struct nlattr *key;
2671 dpif_flow_dump_start(&dump, p->dpif);
2672 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2673 enum odp_key_fitness fitness;
2674 struct subfacet *subfacet;
2677 fitness = odp_flow_key_to_flow(key, key_len, &flow);
2678 if (fitness == ODP_FIT_ERROR) {
2682 subfacet = subfacet_find(p, key, key_len, &flow);
2683 if (subfacet && subfacet->installed) {
2684 struct facet *facet = subfacet->facet;
2686 if (stats->n_packets >= subfacet->dp_packet_count) {
2687 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
2688 facet->packet_count += extra;
2690 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2693 if (stats->n_bytes >= subfacet->dp_byte_count) {
2694 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
2696 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2699 subfacet->dp_packet_count = stats->n_packets;
2700 subfacet->dp_byte_count = stats->n_bytes;
2702 subfacet_update_time(p, subfacet, stats->used);
2703 facet_account(p, facet);
2704 facet_push_stats(facet);
2706 /* There's a flow in the datapath that we know nothing about, or a
2707 * flow that shouldn't be installed but was anyway. Delete it. */
2708 COVERAGE_INC(facet_unexpected);
2709 dpif_flow_del(p->dpif, key, key_len, NULL);
2712 dpif_flow_dump_done(&dump);
2715 /* Calculates and returns the number of milliseconds of idle time after which
2716 * subfacets should expire from the datapath. When a subfacet expires, we fold
2717 * its statistics into its facet, and when a facet's last subfacet expires, we
2718 * fold its statistic into its rule. */
2720 subfacet_max_idle(const struct ofproto_dpif *ofproto)
2723 * Idle time histogram.
2725 * Most of the time a switch has a relatively small number of subfacets.
2726 * When this is the case we might as well keep statistics for all of them
2727 * in userspace and to cache them in the kernel datapath for performance as
2730 * As the number of subfacets increases, the memory required to maintain
2731 * statistics about them in userspace and in the kernel becomes
2732 * significant. However, with a large number of subfacets it is likely
2733 * that only a few of them are "heavy hitters" that consume a large amount
2734 * of bandwidth. At this point, only heavy hitters are worth caching in
2735 * the kernel and maintaining in userspaces; other subfacets we can
2738 * The technique used to compute the idle time is to build a histogram with
2739 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
2740 * that is installed in the kernel gets dropped in the appropriate bucket.
2741 * After the histogram has been built, we compute the cutoff so that only
2742 * the most-recently-used 1% of subfacets (but at least
2743 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2744 * the most-recently-used bucket of subfacets is kept, so actually an
2745 * arbitrary number of subfacets can be kept in any given expiration run
2746 * (though the next run will delete most of those unless they receive
2749 * This requires a second pass through the subfacets, in addition to the
2750 * pass made by update_stats(), because the former function never looks at
2751 * uninstallable subfacets.
2753 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2754 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2755 int buckets[N_BUCKETS] = { 0 };
2756 int total, subtotal, bucket;
2757 struct subfacet *subfacet;
2761 total = hmap_count(&ofproto->subfacets);
2762 if (total <= ofproto->up.flow_eviction_threshold) {
2763 return N_BUCKETS * BUCKET_WIDTH;
2766 /* Build histogram. */
2768 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
2769 long long int idle = now - subfacet->used;
2770 int bucket = (idle <= 0 ? 0
2771 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2772 : (unsigned int) idle / BUCKET_WIDTH);
2776 /* Find the first bucket whose flows should be expired. */
2777 subtotal = bucket = 0;
2779 subtotal += buckets[bucket++];
2780 } while (bucket < N_BUCKETS &&
2781 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2783 if (VLOG_IS_DBG_ENABLED()) {
2787 ds_put_cstr(&s, "keep");
2788 for (i = 0; i < N_BUCKETS; i++) {
2790 ds_put_cstr(&s, ", drop");
2793 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2796 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2800 return bucket * BUCKET_WIDTH;
2804 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
2806 long long int cutoff = time_msec() - dp_max_idle;
2807 struct subfacet *subfacet, *next_subfacet;
2809 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
2810 &ofproto->subfacets) {
2811 if (subfacet->used < cutoff) {
2812 subfacet_destroy(ofproto, subfacet);
2817 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2818 * then delete it entirely. */
2820 rule_expire(struct rule_dpif *rule)
2822 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2823 struct facet *facet, *next_facet;
2827 /* Has 'rule' expired? */
2829 if (rule->up.hard_timeout
2830 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2831 reason = OFPRR_HARD_TIMEOUT;
2832 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2833 && now > rule->used + rule->up.idle_timeout * 1000) {
2834 reason = OFPRR_IDLE_TIMEOUT;
2839 COVERAGE_INC(ofproto_dpif_expired);
2841 /* Update stats. (This is a no-op if the rule expired due to an idle
2842 * timeout, because that only happens when the rule has no facets left.) */
2843 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2844 facet_remove(ofproto, facet);
2847 /* Get rid of the rule. */
2848 ofproto_rule_expire(&rule->up, reason);
2853 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2855 * The caller must already have determined that no facet with an identical
2856 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2857 * the ofproto's classifier table.
2859 * The facet will initially have no subfacets. The caller should create (at
2860 * least) one subfacet with subfacet_create(). */
2861 static struct facet *
2862 facet_create(struct rule_dpif *rule, const struct flow *flow)
2864 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2865 struct facet *facet;
2867 facet = xzalloc(sizeof *facet);
2868 facet->used = time_msec();
2869 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2870 list_push_back(&rule->facets, &facet->list_node);
2872 facet->flow = *flow;
2873 list_init(&facet->subfacets);
2874 netflow_flow_init(&facet->nf_flow);
2875 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2881 facet_free(struct facet *facet)
2887 execute_controller_action(struct ofproto_dpif *ofproto,
2888 const struct flow *flow,
2889 const struct nlattr *odp_actions, size_t actions_len,
2890 struct ofpbuf *packet)
2893 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2894 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2895 /* As an optimization, avoid a round-trip from userspace to kernel to
2896 * userspace. This also avoids possibly filling up kernel packet
2897 * buffers along the way.
2899 * This optimization will not accidentally catch sFlow
2900 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2901 * inside OVS_ACTION_ATTR_SAMPLE. */
2902 const struct nlattr *nla;
2904 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2905 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2913 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2914 * 'packet', which arrived on 'in_port'.
2916 * Takes ownership of 'packet'. */
2918 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2919 const struct nlattr *odp_actions, size_t actions_len,
2920 struct ofpbuf *packet)
2922 struct odputil_keybuf keybuf;
2926 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2931 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2932 odp_flow_key_from_flow(&key, flow);
2934 error = dpif_execute(ofproto->dpif, key.data, key.size,
2935 odp_actions, actions_len, packet);
2937 ofpbuf_delete(packet);
2941 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2943 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2944 * rule's statistics, via subfacet_uninstall().
2946 * - Removes 'facet' from its rule and from ofproto->facets.
2949 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2951 struct subfacet *subfacet, *next_subfacet;
2953 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
2954 &facet->subfacets) {
2955 subfacet_destroy__(ofproto, subfacet);
2958 facet_flush_stats(ofproto, facet);
2959 hmap_remove(&ofproto->facets, &facet->hmap_node);
2960 list_remove(&facet->list_node);
2965 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2968 struct subfacet *subfacet;
2969 const struct nlattr *a;
2973 if (facet->byte_count <= facet->accounted_bytes) {
2976 n_bytes = facet->byte_count - facet->accounted_bytes;
2977 facet->accounted_bytes = facet->byte_count;
2979 /* Feed information from the active flows back into the learning table to
2980 * ensure that table is always in sync with what is actually flowing
2981 * through the datapath. */
2982 if (facet->has_learn || facet->has_normal) {
2983 struct action_xlate_ctx ctx;
2985 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
2986 facet->flow.vlan_tci, NULL);
2987 ctx.may_learn = true;
2988 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2989 facet->rule->up.n_actions));
2992 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2996 /* This loop feeds byte counters to bond_account() for rebalancing to use
2997 * as a basis. We also need to track the actual VLAN on which the packet
2998 * is going to be sent to ensure that it matches the one passed to
2999 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3002 * We use the actions from an arbitrary subfacet because they should all
3003 * be equally valid for our purpose. */
3004 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3005 struct subfacet, list_node);
3006 vlan_tci = facet->flow.vlan_tci;
3007 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3008 subfacet->actions, subfacet->actions_len) {
3009 const struct ovs_action_push_vlan *vlan;
3010 struct ofport_dpif *port;
3012 switch (nl_attr_type(a)) {
3013 case OVS_ACTION_ATTR_OUTPUT:
3014 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3015 if (port && port->bundle && port->bundle->bond) {
3016 bond_account(port->bundle->bond, &facet->flow,
3017 vlan_tci_to_vid(vlan_tci), n_bytes);
3021 case OVS_ACTION_ATTR_POP_VLAN:
3022 vlan_tci = htons(0);
3025 case OVS_ACTION_ATTR_PUSH_VLAN:
3026 vlan = nl_attr_get(a);
3027 vlan_tci = vlan->vlan_tci;
3033 /* Returns true if the only action for 'facet' is to send to the controller.
3034 * (We don't report NetFlow expiration messages for such facets because they
3035 * are just part of the control logic for the network, not real traffic). */
3037 facet_is_controller_flow(struct facet *facet)
3040 && facet->rule->up.n_actions == 1
3041 && action_outputs_to_port(&facet->rule->up.actions[0],
3042 htons(OFPP_CONTROLLER)));
3045 /* Folds all of 'facet''s statistics into its rule. Also updates the
3046 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3047 * 'facet''s statistics in the datapath should have been zeroed and folded into
3048 * its packet and byte counts before this function is called. */
3050 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3052 struct subfacet *subfacet;
3054 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3055 assert(!subfacet->dp_byte_count);
3056 assert(!subfacet->dp_packet_count);
3059 facet_push_stats(facet);
3060 facet_account(ofproto, facet);
3062 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3063 struct ofexpired expired;
3064 expired.flow = facet->flow;
3065 expired.packet_count = facet->packet_count;
3066 expired.byte_count = facet->byte_count;
3067 expired.used = facet->used;
3068 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3071 facet->rule->packet_count += facet->packet_count;
3072 facet->rule->byte_count += facet->byte_count;
3074 /* Reset counters to prevent double counting if 'facet' ever gets
3076 facet_reset_counters(facet);
3078 netflow_flow_clear(&facet->nf_flow);
3081 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3082 * Returns it if found, otherwise a null pointer.
3084 * The returned facet might need revalidation; use facet_lookup_valid()
3085 * instead if that is important. */
3086 static struct facet *
3087 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3089 struct facet *facet;
3091 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3093 if (flow_equal(flow, &facet->flow)) {
3101 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3102 * Returns it if found, otherwise a null pointer.
3104 * The returned facet is guaranteed to be valid. */
3105 static struct facet *
3106 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3108 struct facet *facet = facet_find(ofproto, flow);
3110 /* The facet we found might not be valid, since we could be in need of
3111 * revalidation. If it is not valid, don't return it. */
3113 && (ofproto->need_revalidate
3114 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3115 && !facet_revalidate(ofproto, facet)) {
3116 COVERAGE_INC(facet_invalidated);
3123 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3125 * - If the rule found is different from 'facet''s current rule, moves
3126 * 'facet' to the new rule and recompiles its actions.
3128 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3129 * where it is and recompiles its actions anyway.
3131 * - If there is none, destroys 'facet'.
3133 * Returns true if 'facet' still exists, false if it has been destroyed. */
3135 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3138 struct nlattr *odp_actions;
3141 struct actions *new_actions;
3143 struct action_xlate_ctx ctx;
3144 struct rule_dpif *new_rule;
3145 struct subfacet *subfacet;
3146 bool actions_changed;
3149 COVERAGE_INC(facet_revalidate);
3151 /* Determine the new rule. */
3152 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3154 /* No new rule, so delete the facet. */
3155 facet_remove(ofproto, facet);
3159 /* Calculate new datapath actions.
3161 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3162 * emit a NetFlow expiration and, if so, we need to have the old state
3163 * around to properly compose it. */
3165 /* If the datapath actions changed or the installability changed,
3166 * then we need to talk to the datapath. */
3169 memset(&ctx, 0, sizeof ctx);
3170 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3171 struct ofpbuf *odp_actions;
3172 bool should_install;
3174 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3175 subfacet->initial_tci, NULL);
3176 odp_actions = xlate_actions(&ctx, new_rule->up.actions,
3177 new_rule->up.n_actions);
3178 actions_changed = (subfacet->actions_len != odp_actions->size
3179 || memcmp(subfacet->actions, odp_actions->data,
3180 subfacet->actions_len));
3182 should_install = (ctx.may_set_up_flow
3183 && subfacet->key_fitness != ODP_FIT_TOO_LITTLE);
3184 if (actions_changed || should_install != subfacet->installed) {
3185 if (should_install) {
3186 struct dpif_flow_stats stats;
3188 subfacet_install(ofproto, subfacet,
3189 odp_actions->data, odp_actions->size, &stats);
3190 subfacet_update_stats(ofproto, subfacet, &stats);
3192 subfacet_uninstall(ofproto, subfacet);
3196 new_actions = xcalloc(list_size(&facet->subfacets),
3197 sizeof *new_actions);
3199 new_actions[i].odp_actions = xmemdup(odp_actions->data,
3201 new_actions[i].actions_len = odp_actions->size;
3204 ofpbuf_delete(odp_actions);
3208 facet_flush_stats(ofproto, facet);
3211 /* Update 'facet' now that we've taken care of all the old state. */
3212 facet->tags = ctx.tags;
3213 facet->nf_flow.output_iface = ctx.nf_output_iface;
3214 facet->may_install = ctx.may_set_up_flow;
3215 facet->has_learn = ctx.has_learn;
3216 facet->has_normal = ctx.has_normal;
3219 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3220 if (new_actions[i].odp_actions) {
3221 free(subfacet->actions);
3222 subfacet->actions = new_actions[i].odp_actions;
3223 subfacet->actions_len = new_actions[i].actions_len;
3229 if (facet->rule != new_rule) {
3230 COVERAGE_INC(facet_changed_rule);
3231 list_remove(&facet->list_node);
3232 list_push_back(&new_rule->facets, &facet->list_node);
3233 facet->rule = new_rule;
3234 facet->used = new_rule->up.created;
3235 facet->rs_used = facet->used;
3241 /* Updates 'facet''s used time. Caller is responsible for calling
3242 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3244 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3247 if (used > facet->used) {
3249 if (used > facet->rule->used) {
3250 facet->rule->used = used;
3252 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3257 facet_reset_counters(struct facet *facet)
3259 facet->packet_count = 0;
3260 facet->byte_count = 0;
3261 facet->rs_packet_count = 0;
3262 facet->rs_byte_count = 0;
3263 facet->accounted_bytes = 0;
3267 facet_push_stats(struct facet *facet)
3269 uint64_t rs_packets, rs_bytes;
3271 assert(facet->packet_count >= facet->rs_packet_count);
3272 assert(facet->byte_count >= facet->rs_byte_count);
3273 assert(facet->used >= facet->rs_used);
3275 rs_packets = facet->packet_count - facet->rs_packet_count;
3276 rs_bytes = facet->byte_count - facet->rs_byte_count;
3278 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3279 facet->rs_packet_count = facet->packet_count;
3280 facet->rs_byte_count = facet->byte_count;
3281 facet->rs_used = facet->used;
3283 flow_push_stats(facet->rule, &facet->flow,
3284 rs_packets, rs_bytes, facet->used);
3288 struct ofproto_push {
3289 struct action_xlate_ctx ctx;
3296 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3298 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3301 rule->packet_count += push->packets;
3302 rule->byte_count += push->bytes;
3303 rule->used = MAX(push->used, rule->used);
3307 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3308 * 'rule''s actions. */
3310 flow_push_stats(const struct rule_dpif *rule,
3311 const struct flow *flow, uint64_t packets, uint64_t bytes,
3314 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3315 struct ofproto_push push;
3317 push.packets = packets;
3321 action_xlate_ctx_init(&push.ctx, ofproto, flow, flow->vlan_tci, NULL);
3322 push.ctx.resubmit_hook = push_resubmit;
3323 ofpbuf_delete(xlate_actions(&push.ctx,
3324 rule->up.actions, rule->up.n_actions));
3329 static struct subfacet *
3330 subfacet_find__(struct ofproto_dpif *ofproto,
3331 const struct nlattr *key, size_t key_len, uint32_t key_hash,
3332 const struct flow *flow)
3334 struct subfacet *subfacet;
3336 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
3337 &ofproto->subfacets) {
3339 ? (subfacet->key_len == key_len
3340 && !memcmp(key, subfacet->key, key_len))
3341 : flow_equal(flow, &subfacet->facet->flow)) {
3349 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
3350 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
3351 * there is one, otherwise creates and returns a new subfacet.
3353 * If the returned subfacet is new, then subfacet->actions will be NULL, in
3354 * which case the caller must populate the actions with
3355 * subfacet_make_actions(). */
3356 static struct subfacet *
3357 subfacet_create(struct ofproto_dpif *ofproto, struct facet *facet,
3358 enum odp_key_fitness key_fitness,
3359 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
3361 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3362 struct subfacet *subfacet;
3364 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
3366 if (subfacet->facet == facet) {
3370 /* This shouldn't happen. */
3371 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3372 subfacet_destroy(ofproto, subfacet);
3375 subfacet = xzalloc(sizeof *subfacet);
3376 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
3377 list_push_back(&facet->subfacets, &subfacet->list_node);
3378 subfacet->facet = facet;
3379 subfacet->used = time_msec();
3380 subfacet->key_fitness = key_fitness;
3381 if (key_fitness != ODP_FIT_PERFECT) {
3382 subfacet->key = xmemdup(key, key_len);
3383 subfacet->key_len = key_len;
3385 subfacet->installed = false;
3386 subfacet->initial_tci = initial_tci;
3391 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
3392 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
3393 static struct subfacet *
3394 subfacet_find(struct ofproto_dpif *ofproto,
3395 const struct nlattr *key, size_t key_len,
3396 const struct flow *flow)
3398 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3400 return subfacet_find__(ofproto, key, key_len, key_hash, flow);
3403 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
3404 * its facet within 'ofproto', and frees it. */
3406 subfacet_destroy__(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3408 subfacet_uninstall(ofproto, subfacet);
3409 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
3410 list_remove(&subfacet->list_node);
3411 free(subfacet->key);
3412 free(subfacet->actions);
3416 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
3417 * last remaining subfacet in its facet destroys the facet too. */
3419 subfacet_destroy(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3421 struct facet *facet = subfacet->facet;
3423 subfacet_destroy__(ofproto, subfacet);
3424 if (list_is_empty(&facet->subfacets)) {
3425 facet_remove(ofproto, facet);
3429 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
3430 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
3431 * for use as temporary storage. */
3433 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
3436 if (!subfacet->key) {
3437 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
3438 odp_flow_key_from_flow(key, &subfacet->facet->flow);
3440 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
3444 /* Composes the datapath actions for 'subfacet' based on its rule's actions. */
3446 subfacet_make_actions(struct ofproto_dpif *p, struct subfacet *subfacet,
3447 const struct ofpbuf *packet)
3449 struct facet *facet = subfacet->facet;
3450 const struct rule_dpif *rule = facet->rule;
3451 struct ofpbuf *odp_actions;
3452 struct action_xlate_ctx ctx;
3454 action_xlate_ctx_init(&ctx, p, &facet->flow, subfacet->initial_tci,
3456 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3457 facet->tags = ctx.tags;
3458 facet->may_install = ctx.may_set_up_flow;
3459 facet->has_learn = ctx.has_learn;
3460 facet->has_normal = ctx.has_normal;
3461 facet->nf_flow.output_iface = ctx.nf_output_iface;
3463 if (subfacet->actions_len != odp_actions->size
3464 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
3465 free(subfacet->actions);
3466 subfacet->actions_len = odp_actions->size;
3467 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
3470 ofpbuf_delete(odp_actions);
3473 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
3474 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
3475 * in the datapath will be zeroed and 'stats' will be updated with traffic new
3476 * since 'subfacet' was last updated.
3478 * Returns 0 if successful, otherwise a positive errno value. */
3480 subfacet_install(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3481 const struct nlattr *actions, size_t actions_len,
3482 struct dpif_flow_stats *stats)
3484 struct odputil_keybuf keybuf;
3485 enum dpif_flow_put_flags flags;
3489 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3491 flags |= DPIF_FP_ZERO_STATS;
3494 subfacet_get_key(subfacet, &keybuf, &key);
3495 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
3496 actions, actions_len, stats);
3499 subfacet_reset_dp_stats(subfacet, stats);
3505 /* If 'subfacet' is installed in the datapath, uninstalls it. */
3507 subfacet_uninstall(struct ofproto_dpif *p, struct subfacet *subfacet)
3509 if (subfacet->installed) {
3510 struct odputil_keybuf keybuf;
3511 struct dpif_flow_stats stats;
3515 subfacet_get_key(subfacet, &keybuf, &key);
3516 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3517 subfacet_reset_dp_stats(subfacet, &stats);
3519 subfacet_update_stats(p, subfacet, &stats);
3521 subfacet->installed = false;
3523 assert(subfacet->dp_packet_count == 0);
3524 assert(subfacet->dp_byte_count == 0);
3528 /* Resets 'subfacet''s datapath statistics counters. This should be called
3529 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
3530 * non-null, it should contain the statistics returned by dpif when 'subfacet'
3531 * was reset in the datapath. 'stats' will be modified to include only
3532 * statistics new since 'subfacet' was last updated. */
3534 subfacet_reset_dp_stats(struct subfacet *subfacet,
3535 struct dpif_flow_stats *stats)
3538 && subfacet->dp_packet_count <= stats->n_packets
3539 && subfacet->dp_byte_count <= stats->n_bytes) {
3540 stats->n_packets -= subfacet->dp_packet_count;
3541 stats->n_bytes -= subfacet->dp_byte_count;
3544 subfacet->dp_packet_count = 0;
3545 subfacet->dp_byte_count = 0;
3548 /* Updates 'subfacet''s used time. The caller is responsible for calling
3549 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
3551 subfacet_update_time(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3554 if (used > subfacet->used) {
3555 subfacet->used = used;
3556 facet_update_time(ofproto, subfacet->facet, used);
3560 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
3562 * Because of the meaning of a subfacet's counters, it only makes sense to do
3563 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
3564 * represents a packet that was sent by hand or if it represents statistics
3565 * that have been cleared out of the datapath. */
3567 subfacet_update_stats(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3568 const struct dpif_flow_stats *stats)
3570 if (stats->n_packets || stats->used > subfacet->used) {
3571 struct facet *facet = subfacet->facet;
3573 subfacet_update_time(ofproto, subfacet, stats->used);
3574 facet->packet_count += stats->n_packets;
3575 facet->byte_count += stats->n_bytes;
3576 facet_push_stats(facet);
3577 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3583 static struct rule_dpif *
3584 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3587 struct cls_rule *cls_rule;
3588 struct classifier *cls;
3590 if (table_id >= N_TABLES) {
3594 cls = &ofproto->up.tables[table_id];
3595 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3596 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3597 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3598 * are unavailable. */
3599 struct flow ofpc_normal_flow = *flow;
3600 ofpc_normal_flow.tp_src = htons(0);
3601 ofpc_normal_flow.tp_dst = htons(0);
3602 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3604 cls_rule = classifier_lookup(cls, flow);
3606 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3610 complete_operation(struct rule_dpif *rule)
3612 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3614 rule_invalidate(rule);
3616 struct dpif_completion *c = xmalloc(sizeof *c);
3617 c->op = rule->up.pending;
3618 list_push_back(&ofproto->completions, &c->list_node);
3620 ofoperation_complete(rule->up.pending, 0);
3624 static struct rule *
3627 struct rule_dpif *rule = xmalloc(sizeof *rule);
3632 rule_dealloc(struct rule *rule_)
3634 struct rule_dpif *rule = rule_dpif_cast(rule_);
3639 rule_construct(struct rule *rule_)
3641 struct rule_dpif *rule = rule_dpif_cast(rule_);
3642 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3643 struct rule_dpif *victim;
3647 error = validate_actions(rule->up.actions, rule->up.n_actions,
3648 &rule->up.cr.flow, ofproto->max_ports);
3653 rule->used = rule->up.created;
3654 rule->packet_count = 0;
3655 rule->byte_count = 0;
3657 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3658 if (victim && !list_is_empty(&victim->facets)) {
3659 struct facet *facet;
3661 rule->facets = victim->facets;
3662 list_moved(&rule->facets);
3663 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3664 /* XXX: We're only clearing our local counters here. It's possible
3665 * that quite a few packets are unaccounted for in the datapath
3666 * statistics. These will be accounted to the new rule instead of
3667 * cleared as required. This could be fixed by clearing out the
3668 * datapath statistics for this facet, but currently it doesn't
3670 facet_reset_counters(facet);
3674 /* Must avoid list_moved() in this case. */
3675 list_init(&rule->facets);
3678 table_id = rule->up.table_id;
3679 rule->tag = (victim ? victim->tag
3681 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3682 ofproto->tables[table_id].basis));
3684 complete_operation(rule);
3689 rule_destruct(struct rule *rule_)
3691 struct rule_dpif *rule = rule_dpif_cast(rule_);
3692 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3693 struct facet *facet, *next_facet;
3695 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3696 facet_revalidate(ofproto, facet);
3699 complete_operation(rule);
3703 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3705 struct rule_dpif *rule = rule_dpif_cast(rule_);
3706 struct facet *facet;
3708 /* Start from historical data for 'rule' itself that are no longer tracked
3709 * in facets. This counts, for example, facets that have expired. */
3710 *packets = rule->packet_count;
3711 *bytes = rule->byte_count;
3713 /* Add any statistics that are tracked by facets. This includes
3714 * statistical data recently updated by ofproto_update_stats() as well as
3715 * stats for packets that were executed "by hand" via dpif_execute(). */
3716 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3717 *packets += facet->packet_count;
3718 *bytes += facet->byte_count;
3723 rule_execute(struct rule *rule_, const struct flow *flow,
3724 struct ofpbuf *packet)
3726 struct rule_dpif *rule = rule_dpif_cast(rule_);
3727 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3728 struct action_xlate_ctx ctx;
3729 struct ofpbuf *odp_actions;
3732 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
3733 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3734 size = packet->size;
3735 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3736 odp_actions->size, packet)) {
3737 rule->used = time_msec();
3738 rule->packet_count++;
3739 rule->byte_count += size;
3740 flow_push_stats(rule, flow, 1, size, rule->used);
3742 ofpbuf_delete(odp_actions);
3748 rule_modify_actions(struct rule *rule_)
3750 struct rule_dpif *rule = rule_dpif_cast(rule_);
3751 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3754 error = validate_actions(rule->up.actions, rule->up.n_actions,
3755 &rule->up.cr.flow, ofproto->max_ports);
3757 ofoperation_complete(rule->up.pending, error);
3761 complete_operation(rule);
3764 /* Sends 'packet' out 'ofport'.
3765 * Returns 0 if successful, otherwise a positive errno value. */
3767 send_packet(const struct ofport_dpif *ofport, const struct ofpbuf *packet)
3769 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3770 uint16_t odp_port = ofport->odp_port;
3771 struct ofpbuf key, odp_actions;
3772 struct odputil_keybuf keybuf;
3776 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3777 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3778 odp_flow_key_from_flow(&key, &flow);
3780 ofpbuf_init(&odp_actions, 32);
3781 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3783 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3784 error = dpif_execute(ofproto->dpif,
3786 odp_actions.data, odp_actions.size,
3788 ofpbuf_uninit(&odp_actions);
3791 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3792 ofproto->up.name, odp_port, strerror(error));
3797 /* OpenFlow to datapath action translation. */
3799 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3800 struct action_xlate_ctx *ctx);
3801 static void xlate_normal(struct action_xlate_ctx *);
3804 put_userspace_action(const struct ofproto_dpif *ofproto,
3805 struct ofpbuf *odp_actions,
3806 const struct flow *flow,
3807 const struct user_action_cookie *cookie)
3811 pid = dpif_port_get_pid(ofproto->dpif,
3812 ofp_port_to_odp_port(flow->in_port));
3814 return odp_put_userspace_action(pid, cookie, odp_actions);
3817 /* Compose SAMPLE action for sFlow. */
3819 compose_sflow_action(const struct ofproto_dpif *ofproto,
3820 struct ofpbuf *odp_actions,
3821 const struct flow *flow,
3824 uint32_t port_ifindex;
3825 uint32_t probability;
3826 struct user_action_cookie cookie;
3827 size_t sample_offset, actions_offset;
3828 int cookie_offset, n_output;
3830 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3834 if (odp_port == OVSP_NONE) {
3838 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3842 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3844 /* Number of packets out of UINT_MAX to sample. */
3845 probability = dpif_sflow_get_probability(ofproto->sflow);
3846 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3848 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3850 cookie.type = USER_ACTION_COOKIE_SFLOW;
3851 cookie.data = port_ifindex;
3852 cookie.n_output = n_output;
3853 cookie.vlan_tci = 0;
3854 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3856 nl_msg_end_nested(odp_actions, actions_offset);
3857 nl_msg_end_nested(odp_actions, sample_offset);
3858 return cookie_offset;
3861 /* SAMPLE action must be first action in any given list of actions.
3862 * At this point we do not have all information required to build it. So try to
3863 * build sample action as complete as possible. */
3865 add_sflow_action(struct action_xlate_ctx *ctx)
3867 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3869 &ctx->flow, OVSP_NONE);
3870 ctx->sflow_odp_port = 0;
3871 ctx->sflow_n_outputs = 0;
3874 /* Fix SAMPLE action according to data collected while composing ODP actions.
3875 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3876 * USERSPACE action's user-cookie which is required for sflow. */
3878 fix_sflow_action(struct action_xlate_ctx *ctx)
3880 const struct flow *base = &ctx->base_flow;
3881 struct user_action_cookie *cookie;
3883 if (!ctx->user_cookie_offset) {
3887 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3889 assert(cookie != NULL);
3890 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3892 if (ctx->sflow_n_outputs) {
3893 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3894 ctx->sflow_odp_port);
3896 if (ctx->sflow_n_outputs >= 255) {
3897 cookie->n_output = 255;
3899 cookie->n_output = ctx->sflow_n_outputs;
3901 cookie->vlan_tci = base->vlan_tci;
3905 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3906 const void *key, size_t key_size)
3908 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3909 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3910 nl_msg_end_nested(odp_actions, offset);
3914 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3915 struct ofpbuf *odp_actions)
3917 if (base->tun_id == flow->tun_id) {
3920 base->tun_id = flow->tun_id;
3922 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
3923 &base->tun_id, sizeof(base->tun_id));
3927 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3928 struct ofpbuf *odp_actions)
3930 struct ovs_key_ethernet eth_key;
3932 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3933 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3937 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3938 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3940 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3941 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3943 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3944 ð_key, sizeof(eth_key));
3948 commit_vlan_action(const struct flow *flow, struct flow *base,
3949 struct ofpbuf *odp_actions)
3951 if (base->vlan_tci == flow->vlan_tci) {
3955 if (base->vlan_tci & htons(VLAN_CFI)) {
3956 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3959 if (flow->vlan_tci & htons(VLAN_CFI)) {
3960 struct ovs_action_push_vlan vlan;
3962 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3963 vlan.vlan_tci = flow->vlan_tci;
3964 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3965 &vlan, sizeof vlan);
3967 base->vlan_tci = flow->vlan_tci;
3971 commit_set_nw_action(const struct flow *flow, struct flow *base,
3972 struct ofpbuf *odp_actions)
3974 struct ovs_key_ipv4 ipv4_key;
3976 if (base->dl_type != htons(ETH_TYPE_IP) ||
3977 !base->nw_src || !base->nw_dst) {
3981 if (base->nw_src == flow->nw_src &&
3982 base->nw_dst == flow->nw_dst &&
3983 base->nw_tos == flow->nw_tos &&
3984 base->nw_ttl == flow->nw_ttl &&
3985 base->nw_frag == flow->nw_frag) {
3989 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3990 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3991 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3992 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3993 ipv4_key.ipv4_proto = base->nw_proto;
3994 ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE
3995 : base->nw_frag == FLOW_NW_FRAG_ANY
3996 ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER);
3998 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3999 &ipv4_key, sizeof(ipv4_key));
4003 commit_set_port_action(const struct flow *flow, struct flow *base,
4004 struct ofpbuf *odp_actions)
4006 if (!base->tp_src || !base->tp_dst) {
4010 if (base->tp_src == flow->tp_src &&
4011 base->tp_dst == flow->tp_dst) {
4015 if (flow->nw_proto == IPPROTO_TCP) {
4016 struct ovs_key_tcp port_key;
4018 port_key.tcp_src = base->tp_src = flow->tp_src;
4019 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
4021 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
4022 &port_key, sizeof(port_key));
4024 } else if (flow->nw_proto == IPPROTO_UDP) {
4025 struct ovs_key_udp port_key;
4027 port_key.udp_src = base->tp_src = flow->tp_src;
4028 port_key.udp_dst = base->tp_dst = flow->tp_dst;
4030 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
4031 &port_key, sizeof(port_key));
4036 commit_set_priority_action(const struct flow *flow, struct flow *base,
4037 struct ofpbuf *odp_actions)
4039 if (base->priority == flow->priority) {
4042 base->priority = flow->priority;
4044 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
4045 &base->priority, sizeof(base->priority));
4049 commit_odp_actions(struct action_xlate_ctx *ctx)
4051 const struct flow *flow = &ctx->flow;
4052 struct flow *base = &ctx->base_flow;
4053 struct ofpbuf *odp_actions = ctx->odp_actions;
4055 commit_set_tun_id_action(flow, base, odp_actions);
4056 commit_set_ether_addr_action(flow, base, odp_actions);
4057 commit_vlan_action(flow, base, odp_actions);
4058 commit_set_nw_action(flow, base, odp_actions);
4059 commit_set_port_action(flow, base, odp_actions);
4060 commit_set_priority_action(flow, base, odp_actions);
4064 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4067 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
4068 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
4069 uint8_t flow_nw_tos = ctx->flow.nw_tos;
4072 struct priority_to_dscp *pdscp;
4074 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
4075 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4079 pdscp = get_priority(ofport, ctx->flow.priority);
4081 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4082 ctx->flow.nw_tos |= pdscp->dscp;
4085 /* We may not have an ofport record for this port, but it doesn't hurt
4086 * to allow forwarding to it anyhow. Maybe such a port will appear
4087 * later and we're pre-populating the flow table. */
4090 commit_odp_actions(ctx);
4091 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
4092 ctx->sflow_odp_port = odp_port;
4093 ctx->sflow_n_outputs++;
4094 ctx->nf_output_iface = ofp_port;
4095 ctx->flow.nw_tos = flow_nw_tos;
4099 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
4101 compose_output_action__(ctx, ofp_port, true);
4105 xlate_table_action(struct action_xlate_ctx *ctx,
4106 uint16_t in_port, uint8_t table_id)
4108 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
4109 struct ofproto_dpif *ofproto = ctx->ofproto;
4110 struct rule_dpif *rule;
4111 uint16_t old_in_port;
4112 uint8_t old_table_id;
4114 old_table_id = ctx->table_id;
4115 ctx->table_id = table_id;
4117 /* Look up a flow with 'in_port' as the input port. */
4118 old_in_port = ctx->flow.in_port;
4119 ctx->flow.in_port = in_port;
4120 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
4123 if (table_id > 0 && table_id < N_TABLES) {
4124 struct table_dpif *table = &ofproto->tables[table_id];
4125 if (table->other_table) {
4128 : rule_calculate_tag(&ctx->flow,
4129 &table->other_table->wc,
4134 /* Restore the original input port. Otherwise OFPP_NORMAL and
4135 * OFPP_IN_PORT will have surprising behavior. */
4136 ctx->flow.in_port = old_in_port;
4138 if (ctx->resubmit_hook) {
4139 ctx->resubmit_hook(ctx, rule);
4144 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4148 ctx->table_id = old_table_id;
4150 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4152 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
4153 MAX_RESUBMIT_RECURSION);
4158 xlate_resubmit_table(struct action_xlate_ctx *ctx,
4159 const struct nx_action_resubmit *nar)
4164 in_port = (nar->in_port == htons(OFPP_IN_PORT)
4166 : ntohs(nar->in_port));
4167 table_id = nar->table == 255 ? ctx->table_id : nar->table;
4169 xlate_table_action(ctx, in_port, table_id);
4173 flood_packets(struct action_xlate_ctx *ctx, bool all)
4175 struct ofport_dpif *ofport;
4177 commit_odp_actions(ctx);
4178 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
4179 uint16_t ofp_port = ofport->up.ofp_port;
4181 if (ofp_port == ctx->flow.in_port) {
4186 compose_output_action__(ctx, ofp_port, false);
4187 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
4188 compose_output_action(ctx, ofp_port);
4192 ctx->nf_output_iface = NF_OUT_FLOOD;
4196 compose_controller_action(struct action_xlate_ctx *ctx, int len)
4198 struct user_action_cookie cookie;
4200 commit_odp_actions(ctx);
4201 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4203 cookie.n_output = 0;
4204 cookie.vlan_tci = 0;
4205 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
4209 xlate_output_action__(struct action_xlate_ctx *ctx,
4210 uint16_t port, uint16_t max_len)
4212 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4214 ctx->nf_output_iface = NF_OUT_DROP;
4218 compose_output_action(ctx, ctx->flow.in_port);
4221 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
4227 flood_packets(ctx, false);
4230 flood_packets(ctx, true);
4232 case OFPP_CONTROLLER:
4233 compose_controller_action(ctx, max_len);
4236 compose_output_action(ctx, OFPP_LOCAL);
4241 if (port != ctx->flow.in_port) {
4242 compose_output_action(ctx, port);
4247 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4248 ctx->nf_output_iface = NF_OUT_FLOOD;
4249 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4250 ctx->nf_output_iface = prev_nf_output_iface;
4251 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4252 ctx->nf_output_iface != NF_OUT_FLOOD) {
4253 ctx->nf_output_iface = NF_OUT_MULTI;
4258 xlate_output_reg_action(struct action_xlate_ctx *ctx,
4259 const struct nx_action_output_reg *naor)
4263 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4265 if (ofp_port <= UINT16_MAX) {
4266 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4271 xlate_output_action(struct action_xlate_ctx *ctx,
4272 const struct ofp_action_output *oao)
4274 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4278 xlate_enqueue_action(struct action_xlate_ctx *ctx,
4279 const struct ofp_action_enqueue *oae)
4282 uint32_t flow_priority, priority;
4285 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4288 /* Fall back to ordinary output action. */
4289 xlate_output_action__(ctx, ntohs(oae->port), 0);
4293 /* Figure out datapath output port. */
4294 ofp_port = ntohs(oae->port);
4295 if (ofp_port == OFPP_IN_PORT) {
4296 ofp_port = ctx->flow.in_port;
4297 } else if (ofp_port == ctx->flow.in_port) {
4301 /* Add datapath actions. */
4302 flow_priority = ctx->flow.priority;
4303 ctx->flow.priority = priority;
4304 compose_output_action(ctx, ofp_port);
4305 ctx->flow.priority = flow_priority;
4307 /* Update NetFlow output port. */
4308 if (ctx->nf_output_iface == NF_OUT_DROP) {
4309 ctx->nf_output_iface = ofp_port;
4310 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4311 ctx->nf_output_iface = NF_OUT_MULTI;
4316 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4317 const struct nx_action_set_queue *nasq)
4322 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4325 /* Couldn't translate queue to a priority, so ignore. A warning
4326 * has already been logged. */
4330 ctx->flow.priority = priority;
4333 struct xlate_reg_state {
4339 xlate_autopath(struct action_xlate_ctx *ctx,
4340 const struct nx_action_autopath *naa)
4342 uint16_t ofp_port = ntohl(naa->id);
4343 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4345 if (!port || !port->bundle) {
4346 ofp_port = OFPP_NONE;
4347 } else if (port->bundle->bond) {
4348 /* Autopath does not support VLAN hashing. */
4349 struct ofport_dpif *slave = bond_choose_output_slave(
4350 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4352 ofp_port = slave->up.ofp_port;
4355 autopath_execute(naa, &ctx->flow, ofp_port);
4359 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4361 struct ofproto_dpif *ofproto = ofproto_;
4362 struct ofport_dpif *port;
4372 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4375 port = get_ofp_port(ofproto, ofp_port);
4376 return port ? port->may_enable : false;
4381 xlate_learn_action(struct action_xlate_ctx *ctx,
4382 const struct nx_action_learn *learn)
4384 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4385 struct ofputil_flow_mod fm;
4388 learn_execute(learn, &ctx->flow, &fm);
4390 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4391 if (error && !VLOG_DROP_WARN(&rl)) {
4392 char *msg = ofputil_error_to_string(error);
4393 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4401 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4403 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4404 ? htonl(OFPPC_NO_RECV_STP)
4405 : htonl(OFPPC_NO_RECV))) {
4409 /* Only drop packets here if both forwarding and learning are
4410 * disabled. If just learning is enabled, we need to have
4411 * OFPP_NORMAL and the learning action have a look at the packet
4412 * before we can drop it. */
4413 if (!stp_forward_in_state(port->stp_state)
4414 && !stp_learn_in_state(port->stp_state)) {
4422 do_xlate_actions(const union ofp_action *in, size_t n_in,
4423 struct action_xlate_ctx *ctx)
4425 const struct ofport_dpif *port;
4426 const union ofp_action *ia;
4429 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4430 if (port && !may_receive(port, ctx)) {
4431 /* Drop this flow. */
4435 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4436 const struct ofp_action_dl_addr *oada;
4437 const struct nx_action_resubmit *nar;
4438 const struct nx_action_set_tunnel *nast;
4439 const struct nx_action_set_queue *nasq;
4440 const struct nx_action_multipath *nam;
4441 const struct nx_action_autopath *naa;
4442 const struct nx_action_bundle *nab;
4443 const struct nx_action_output_reg *naor;
4444 enum ofputil_action_code code;
4451 code = ofputil_decode_action_unsafe(ia);
4453 case OFPUTIL_OFPAT_OUTPUT:
4454 xlate_output_action(ctx, &ia->output);
4457 case OFPUTIL_OFPAT_SET_VLAN_VID:
4458 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4459 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4462 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4463 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4464 ctx->flow.vlan_tci |= htons(
4465 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4468 case OFPUTIL_OFPAT_STRIP_VLAN:
4469 ctx->flow.vlan_tci = htons(0);
4472 case OFPUTIL_OFPAT_SET_DL_SRC:
4473 oada = ((struct ofp_action_dl_addr *) ia);
4474 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4477 case OFPUTIL_OFPAT_SET_DL_DST:
4478 oada = ((struct ofp_action_dl_addr *) ia);
4479 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4482 case OFPUTIL_OFPAT_SET_NW_SRC:
4483 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4486 case OFPUTIL_OFPAT_SET_NW_DST:
4487 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4490 case OFPUTIL_OFPAT_SET_NW_TOS:
4491 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4492 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4495 case OFPUTIL_OFPAT_SET_TP_SRC:
4496 ctx->flow.tp_src = ia->tp_port.tp_port;
4499 case OFPUTIL_OFPAT_SET_TP_DST:
4500 ctx->flow.tp_dst = ia->tp_port.tp_port;
4503 case OFPUTIL_OFPAT_ENQUEUE:
4504 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4507 case OFPUTIL_NXAST_RESUBMIT:
4508 nar = (const struct nx_action_resubmit *) ia;
4509 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4512 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4513 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4516 case OFPUTIL_NXAST_SET_TUNNEL:
4517 nast = (const struct nx_action_set_tunnel *) ia;
4518 tun_id = htonll(ntohl(nast->tun_id));
4519 ctx->flow.tun_id = tun_id;
4522 case OFPUTIL_NXAST_SET_QUEUE:
4523 nasq = (const struct nx_action_set_queue *) ia;
4524 xlate_set_queue_action(ctx, nasq);
4527 case OFPUTIL_NXAST_POP_QUEUE:
4528 ctx->flow.priority = ctx->original_priority;
4531 case OFPUTIL_NXAST_REG_MOVE:
4532 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4536 case OFPUTIL_NXAST_REG_LOAD:
4537 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4541 case OFPUTIL_NXAST_NOTE:
4542 /* Nothing to do. */
4545 case OFPUTIL_NXAST_SET_TUNNEL64:
4546 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4547 ctx->flow.tun_id = tun_id;
4550 case OFPUTIL_NXAST_MULTIPATH:
4551 nam = (const struct nx_action_multipath *) ia;
4552 multipath_execute(nam, &ctx->flow);
4555 case OFPUTIL_NXAST_AUTOPATH:
4556 naa = (const struct nx_action_autopath *) ia;
4557 xlate_autopath(ctx, naa);
4560 case OFPUTIL_NXAST_BUNDLE:
4561 ctx->ofproto->has_bundle_action = true;
4562 nab = (const struct nx_action_bundle *) ia;
4563 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4568 case OFPUTIL_NXAST_BUNDLE_LOAD:
4569 ctx->ofproto->has_bundle_action = true;
4570 nab = (const struct nx_action_bundle *) ia;
4571 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4575 case OFPUTIL_NXAST_OUTPUT_REG:
4576 naor = (const struct nx_action_output_reg *) ia;
4577 xlate_output_reg_action(ctx, naor);
4580 case OFPUTIL_NXAST_LEARN:
4581 ctx->has_learn = true;
4582 if (ctx->may_learn) {
4583 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4587 case OFPUTIL_NXAST_EXIT:
4593 /* We've let OFPP_NORMAL and the learning action look at the packet,
4594 * so drop it now if forwarding is disabled. */
4595 if (port && !stp_forward_in_state(port->stp_state)) {
4596 ofpbuf_clear(ctx->odp_actions);
4597 add_sflow_action(ctx);
4602 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4603 struct ofproto_dpif *ofproto, const struct flow *flow,
4604 ovs_be16 initial_tci, const struct ofpbuf *packet)
4606 ctx->ofproto = ofproto;
4608 ctx->base_flow = ctx->flow;
4609 ctx->base_flow.tun_id = 0;
4610 ctx->base_flow.vlan_tci = initial_tci;
4611 ctx->packet = packet;
4612 ctx->may_learn = packet != NULL;
4613 ctx->resubmit_hook = NULL;
4616 static struct ofpbuf *
4617 xlate_actions(struct action_xlate_ctx *ctx,
4618 const union ofp_action *in, size_t n_in)
4620 COVERAGE_INC(ofproto_dpif_xlate);
4622 ctx->odp_actions = ofpbuf_new(512);
4623 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4625 ctx->may_set_up_flow = true;
4626 ctx->has_learn = false;
4627 ctx->has_normal = false;
4628 ctx->nf_output_iface = NF_OUT_DROP;
4630 ctx->original_priority = ctx->flow.priority;
4634 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4635 switch (ctx->ofproto->up.frag_handling) {
4636 case OFPC_FRAG_NORMAL:
4637 /* We must pretend that transport ports are unavailable. */
4638 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4639 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4642 case OFPC_FRAG_DROP:
4643 return ctx->odp_actions;
4645 case OFPC_FRAG_REASM:
4648 case OFPC_FRAG_NX_MATCH:
4649 /* Nothing to do. */
4654 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4655 ctx->may_set_up_flow = false;
4656 return ctx->odp_actions;
4658 add_sflow_action(ctx);
4659 do_xlate_actions(in, n_in, ctx);
4661 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4662 ctx->odp_actions->data,
4663 ctx->odp_actions->size)) {
4664 ctx->may_set_up_flow = false;
4666 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4668 compose_output_action(ctx, OFPP_LOCAL);
4671 fix_sflow_action(ctx);
4674 return ctx->odp_actions;
4677 /* OFPP_NORMAL implementation. */
4679 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4681 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4682 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4683 * the bundle on which the packet was received, returns the VLAN to which the
4686 * Both 'vid' and the return value are in the range 0...4095. */
4688 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4690 switch (in_bundle->vlan_mode) {
4691 case PORT_VLAN_ACCESS:
4692 return in_bundle->vlan;
4695 case PORT_VLAN_TRUNK:
4698 case PORT_VLAN_NATIVE_UNTAGGED:
4699 case PORT_VLAN_NATIVE_TAGGED:
4700 return vid ? vid : in_bundle->vlan;
4707 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4708 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4711 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4712 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4715 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4717 switch (in_bundle->vlan_mode) {
4718 case PORT_VLAN_ACCESS:
4721 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4722 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4723 "packet received on port %s configured as VLAN "
4724 "%"PRIu16" access port",
4725 in_bundle->ofproto->up.name, vid,
4726 in_bundle->name, in_bundle->vlan);
4732 case PORT_VLAN_NATIVE_UNTAGGED:
4733 case PORT_VLAN_NATIVE_TAGGED:
4735 /* Port must always carry its native VLAN. */
4739 case PORT_VLAN_TRUNK:
4740 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4742 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4743 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4744 "received on port %s not configured for trunking "
4746 in_bundle->ofproto->up.name, vid,
4747 in_bundle->name, vid);
4759 /* Given 'vlan', the VLAN that a packet belongs to, and
4760 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4761 * that should be included in the 802.1Q header. (If the return value is 0,
4762 * then the 802.1Q header should only be included in the packet if there is a
4765 * Both 'vlan' and the return value are in the range 0...4095. */
4767 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4769 switch (out_bundle->vlan_mode) {
4770 case PORT_VLAN_ACCESS:
4773 case PORT_VLAN_TRUNK:
4774 case PORT_VLAN_NATIVE_TAGGED:
4777 case PORT_VLAN_NATIVE_UNTAGGED:
4778 return vlan == out_bundle->vlan ? 0 : vlan;
4786 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4789 struct ofport_dpif *port;
4791 ovs_be16 tci, old_tci;
4793 vid = output_vlan_to_vid(out_bundle, vlan);
4794 if (!out_bundle->bond) {
4795 port = ofbundle_get_a_port(out_bundle);
4797 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4800 /* No slaves enabled, so drop packet. */
4805 old_tci = ctx->flow.vlan_tci;
4807 if (tci || out_bundle->use_priority_tags) {
4808 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4810 tci |= htons(VLAN_CFI);
4813 ctx->flow.vlan_tci = tci;
4815 compose_output_action(ctx, port->up.ofp_port);
4816 ctx->flow.vlan_tci = old_tci;
4820 mirror_mask_ffs(mirror_mask_t mask)
4822 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4827 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4829 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4830 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4834 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4836 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4839 /* Returns an arbitrary interface within 'bundle'. */
4840 static struct ofport_dpif *
4841 ofbundle_get_a_port(const struct ofbundle *bundle)
4843 return CONTAINER_OF(list_front(&bundle->ports),
4844 struct ofport_dpif, bundle_node);
4847 static mirror_mask_t
4848 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4849 const struct ofbundle *in_bundle,
4850 const struct ofbundle *out_bundle)
4852 mirror_mask_t dst_mirrors = 0;
4854 if (out_bundle == OFBUNDLE_FLOOD) {
4855 struct ofbundle *bundle;
4857 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4858 if (bundle != in_bundle
4859 && ofbundle_includes_vlan(bundle, vlan)
4860 && bundle->floodable
4861 && !bundle->mirror_out) {
4862 output_normal(ctx, bundle, vlan);
4863 dst_mirrors |= bundle->dst_mirrors;
4866 ctx->nf_output_iface = NF_OUT_FLOOD;
4867 } else if (out_bundle) {
4868 output_normal(ctx, out_bundle, vlan);
4869 dst_mirrors = out_bundle->dst_mirrors;
4876 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4878 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4881 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4882 * to a VLAN. In general most packets may be mirrored but we want to drop
4883 * protocols that may confuse switches. */
4885 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4887 /* If you change this function's behavior, please update corresponding
4888 * documentation in vswitch.xml at the same time. */
4889 if (dst[0] != 0x01) {
4890 /* All the currently banned MACs happen to start with 01 currently, so
4891 * this is a quick way to eliminate most of the good ones. */
4893 if (eth_addr_is_reserved(dst)) {
4894 /* Drop STP, IEEE pause frames, and other reserved protocols
4895 * (01-80-c2-00-00-0x). */
4899 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4901 if ((dst[3] & 0xfe) == 0xcc &&
4902 (dst[4] & 0xfe) == 0xcc &&
4903 (dst[5] & 0xfe) == 0xcc) {
4904 /* Drop the following protocols plus others following the same
4907 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4908 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4909 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4913 if (!(dst[3] | dst[4] | dst[5])) {
4914 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4923 output_mirrors(struct action_xlate_ctx *ctx,
4924 uint16_t vlan, const struct ofbundle *in_bundle,
4925 mirror_mask_t dst_mirrors)
4927 struct ofproto_dpif *ofproto = ctx->ofproto;
4928 mirror_mask_t mirrors;
4930 mirrors = in_bundle->src_mirrors | dst_mirrors;
4938 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4940 if (!vlan_is_mirrored(m, vlan)) {
4941 mirrors &= mirrors - 1;
4945 mirrors &= ~m->dup_mirrors;
4947 output_normal(ctx, m->out, vlan);
4948 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)
4949 && vlan != m->out_vlan) {
4950 struct ofbundle *bundle;
4952 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4953 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4954 && !bundle->mirror_out) {
4955 output_normal(ctx, bundle, m->out_vlan);
4962 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4963 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4964 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4966 is_gratuitous_arp(const struct flow *flow)
4968 return (flow->dl_type == htons(ETH_TYPE_ARP)
4969 && eth_addr_is_broadcast(flow->dl_dst)
4970 && (flow->nw_proto == ARP_OP_REPLY
4971 || (flow->nw_proto == ARP_OP_REQUEST
4972 && flow->nw_src == flow->nw_dst)));
4976 update_learning_table(struct ofproto_dpif *ofproto,
4977 const struct flow *flow, int vlan,
4978 struct ofbundle *in_bundle)
4980 struct mac_entry *mac;
4982 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4986 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4987 if (is_gratuitous_arp(flow)) {
4988 /* We don't want to learn from gratuitous ARP packets that are
4989 * reflected back over bond slaves so we lock the learning table. */
4990 if (!in_bundle->bond) {
4991 mac_entry_set_grat_arp_lock(mac);
4992 } else if (mac_entry_is_grat_arp_locked(mac)) {
4997 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4998 /* The log messages here could actually be useful in debugging,
4999 * so keep the rate limit relatively high. */
5000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
5001 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
5002 "on port %s in VLAN %d",
5003 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
5004 in_bundle->name, vlan);
5006 mac->port.p = in_bundle;
5007 tag_set_add(&ofproto->revalidate_set,
5008 mac_learning_changed(ofproto->ml, mac));
5012 static struct ofport_dpif *
5013 lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
5015 struct ofport_dpif *ofport;
5017 /* Find the port and bundle for the received packet. */
5018 ofport = get_ofp_port(ofproto, in_port);
5019 if (ofport && ofport->bundle) {
5023 /* Odd. A few possible reasons here:
5025 * - We deleted a port but there are still a few packets queued up
5028 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
5029 * we don't know about.
5031 * - The ofproto client didn't configure the port as part of a bundle.
5034 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5036 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
5037 "port %"PRIu16, ofproto->up.name, in_port);
5042 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
5043 * dropped. Returns true if they may be forwarded, false if they should be
5046 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
5047 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
5049 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
5050 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
5051 * checked by input_vid_is_valid().
5053 * May also add tags to '*tags', although the current implementation only does
5054 * so in one special case.
5057 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
5058 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
5060 struct ofbundle *in_bundle = in_port->bundle;
5062 /* Drop frames for reserved multicast addresses
5063 * only if forward_bpdu option is absent. */
5064 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
5068 if (in_bundle->bond) {
5069 struct mac_entry *mac;
5071 switch (bond_check_admissibility(in_bundle->bond, in_port,
5072 flow->dl_dst, tags)) {
5079 case BV_DROP_IF_MOVED:
5080 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
5081 if (mac && mac->port.p != in_bundle &&
5082 (!is_gratuitous_arp(flow)
5083 || mac_entry_is_grat_arp_locked(mac))) {
5094 xlate_normal(struct action_xlate_ctx *ctx)
5096 mirror_mask_t dst_mirrors = 0;
5097 struct ofport_dpif *in_port;
5098 struct ofbundle *in_bundle;
5099 struct ofbundle *out_bundle;
5100 struct mac_entry *mac;
5104 ctx->has_normal = true;
5106 /* Obtain in_port from ctx->flow.in_port.
5108 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5109 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
5110 ctx->packet != NULL);
5114 in_bundle = in_port->bundle;
5116 /* Drop malformed frames. */
5117 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
5118 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
5119 if (ctx->packet != NULL) {
5120 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5121 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
5122 "VLAN tag received on port %s",
5123 ctx->ofproto->up.name, in_bundle->name);
5128 /* Drop frames on bundles reserved for mirroring. */
5129 if (in_bundle->mirror_out) {
5130 if (ctx->packet != NULL) {
5131 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5132 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5133 "%s, which is reserved exclusively for mirroring",
5134 ctx->ofproto->up.name, in_bundle->name);
5140 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
5141 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5144 vlan = input_vid_to_vlan(in_bundle, vid);
5146 /* Check other admissibility requirements. */
5147 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
5148 output_mirrors(ctx, vlan, in_bundle, 0);
5152 /* Learn source MAC. */
5153 if (ctx->may_learn) {
5154 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
5157 /* Determine output bundle. */
5158 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
5161 out_bundle = mac->port.p;
5162 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
5163 /* If we are revalidating but don't have a learning entry then eject
5164 * the flow. Installing a flow that floods packets opens up a window
5165 * of time where we could learn from a packet reflected on a bond and
5166 * blackhole packets before the learning table is updated to reflect
5167 * the correct port. */
5168 ctx->may_set_up_flow = false;
5171 out_bundle = OFBUNDLE_FLOOD;
5174 /* Don't send packets out their input bundles. */
5175 if (in_bundle != out_bundle) {
5176 dst_mirrors = compose_dsts(ctx, vlan, in_bundle, out_bundle);
5178 output_mirrors(ctx, vlan, in_bundle, dst_mirrors);
5181 /* Optimized flow revalidation.
5183 * It's a difficult problem, in general, to tell which facets need to have
5184 * their actions recalculated whenever the OpenFlow flow table changes. We
5185 * don't try to solve that general problem: for most kinds of OpenFlow flow
5186 * table changes, we recalculate the actions for every facet. This is
5187 * relatively expensive, but it's good enough if the OpenFlow flow table
5188 * doesn't change very often.
5190 * However, we can expect one particular kind of OpenFlow flow table change to
5191 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5192 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5193 * table, we add a special case that applies to flow tables in which every rule
5194 * has the same form (that is, the same wildcards), except that the table is
5195 * also allowed to have a single "catch-all" flow that matches all packets. We
5196 * optimize this case by tagging all of the facets that resubmit into the table
5197 * and invalidating the same tag whenever a flow changes in that table. The
5198 * end result is that we revalidate just the facets that need it (and sometimes
5199 * a few more, but not all of the facets or even all of the facets that
5200 * resubmit to the table modified by MAC learning). */
5202 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5203 * into an OpenFlow table with the given 'basis'. */
5205 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5208 if (flow_wildcards_is_catchall(wc)) {
5211 struct flow tag_flow = *flow;
5212 flow_zero_wildcards(&tag_flow, wc);
5213 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5217 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5218 * taggability of that table.
5220 * This function must be called after *each* change to a flow table. If you
5221 * skip calling it on some changes then the pointer comparisons at the end can
5222 * be invalid if you get unlucky. For example, if a flow removal causes a
5223 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5224 * different wildcards to be created with the same address, then this function
5225 * will incorrectly skip revalidation. */
5227 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5229 struct table_dpif *table = &ofproto->tables[table_id];
5230 const struct classifier *cls = &ofproto->up.tables[table_id];
5231 struct cls_table *catchall, *other;
5232 struct cls_table *t;
5234 catchall = other = NULL;
5236 switch (hmap_count(&cls->tables)) {
5238 /* We could tag this OpenFlow table but it would make the logic a
5239 * little harder and it's a corner case that doesn't seem worth it
5245 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5246 if (cls_table_is_catchall(t)) {
5248 } else if (!other) {
5251 /* Indicate that we can't tag this by setting both tables to
5252 * NULL. (We know that 'catchall' is already NULL.) */
5259 /* Can't tag this table. */
5263 if (table->catchall_table != catchall || table->other_table != other) {
5264 table->catchall_table = catchall;
5265 table->other_table = other;
5266 ofproto->need_revalidate = true;
5270 /* Given 'rule' that has changed in some way (either it is a rule being
5271 * inserted, a rule being deleted, or a rule whose actions are being
5272 * modified), marks facets for revalidation to ensure that packets will be
5273 * forwarded correctly according to the new state of the flow table.
5275 * This function must be called after *each* change to a flow table. See
5276 * the comment on table_update_taggable() for more information. */
5278 rule_invalidate(const struct rule_dpif *rule)
5280 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5282 table_update_taggable(ofproto, rule->up.table_id);
5284 if (!ofproto->need_revalidate) {
5285 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5287 if (table->other_table && rule->tag) {
5288 tag_set_add(&ofproto->revalidate_set, rule->tag);
5290 ofproto->need_revalidate = true;
5296 set_frag_handling(struct ofproto *ofproto_,
5297 enum ofp_config_flags frag_handling)
5299 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5301 if (frag_handling != OFPC_FRAG_REASM) {
5302 ofproto->need_revalidate = true;
5310 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5311 const struct flow *flow,
5312 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5314 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5317 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5318 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5321 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5322 ofproto->max_ports);
5324 struct odputil_keybuf keybuf;
5325 struct action_xlate_ctx ctx;
5326 struct ofpbuf *odp_actions;
5329 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5330 odp_flow_key_from_flow(&key, flow);
5332 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
5333 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5334 dpif_execute(ofproto->dpif, key.data, key.size,
5335 odp_actions->data, odp_actions->size, packet);
5336 ofpbuf_delete(odp_actions);
5344 set_netflow(struct ofproto *ofproto_,
5345 const struct netflow_options *netflow_options)
5347 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5349 if (netflow_options) {
5350 if (!ofproto->netflow) {
5351 ofproto->netflow = netflow_create();
5353 return netflow_set_options(ofproto->netflow, netflow_options);
5355 netflow_destroy(ofproto->netflow);
5356 ofproto->netflow = NULL;
5362 get_netflow_ids(const struct ofproto *ofproto_,
5363 uint8_t *engine_type, uint8_t *engine_id)
5365 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5367 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5371 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5373 if (!facet_is_controller_flow(facet) &&
5374 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5375 struct subfacet *subfacet;
5376 struct ofexpired expired;
5378 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5379 if (subfacet->installed) {
5380 struct dpif_flow_stats stats;
5382 subfacet_install(ofproto, subfacet, subfacet->actions,
5383 subfacet->actions_len, &stats);
5384 subfacet_update_stats(ofproto, subfacet, &stats);
5388 expired.flow = facet->flow;
5389 expired.packet_count = facet->packet_count;
5390 expired.byte_count = facet->byte_count;
5391 expired.used = facet->used;
5392 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5397 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5399 struct facet *facet;
5401 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5402 send_active_timeout(ofproto, facet);
5406 static struct ofproto_dpif *
5407 ofproto_dpif_lookup(const char *name)
5409 struct ofproto *ofproto = ofproto_lookup(name);
5410 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5411 ? ofproto_dpif_cast(ofproto)
5416 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5417 const char *args, void *aux OVS_UNUSED)
5419 const struct ofproto_dpif *ofproto;
5421 ofproto = ofproto_dpif_lookup(args);
5423 unixctl_command_reply(conn, 501, "no such bridge");
5426 mac_learning_flush(ofproto->ml);
5428 unixctl_command_reply(conn, 200, "table successfully flushed");
5432 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5433 const char *args, void *aux OVS_UNUSED)
5435 struct ds ds = DS_EMPTY_INITIALIZER;
5436 const struct ofproto_dpif *ofproto;
5437 const struct mac_entry *e;
5439 ofproto = ofproto_dpif_lookup(args);
5441 unixctl_command_reply(conn, 501, "no such bridge");
5445 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5446 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5447 struct ofbundle *bundle = e->port.p;
5448 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5449 ofbundle_get_a_port(bundle)->odp_port,
5450 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5452 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5456 struct ofproto_trace {
5457 struct action_xlate_ctx ctx;
5463 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5464 const struct rule_dpif *rule)
5466 ds_put_char_multiple(result, '\t', level);
5468 ds_put_cstr(result, "No match\n");
5472 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5473 table_id, ntohll(rule->up.flow_cookie));
5474 cls_rule_format(&rule->up.cr, result);
5475 ds_put_char(result, '\n');
5477 ds_put_char_multiple(result, '\t', level);
5478 ds_put_cstr(result, "OpenFlow ");
5479 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5480 ds_put_char(result, '\n');
5484 trace_format_flow(struct ds *result, int level, const char *title,
5485 struct ofproto_trace *trace)
5487 ds_put_char_multiple(result, '\t', level);
5488 ds_put_format(result, "%s: ", title);
5489 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5490 ds_put_cstr(result, "unchanged");
5492 flow_format(result, &trace->ctx.flow);
5493 trace->flow = trace->ctx.flow;
5495 ds_put_char(result, '\n');
5499 trace_format_regs(struct ds *result, int level, const char *title,
5500 struct ofproto_trace *trace)
5504 ds_put_char_multiple(result, '\t', level);
5505 ds_put_format(result, "%s:", title);
5506 for (i = 0; i < FLOW_N_REGS; i++) {
5507 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5509 ds_put_char(result, '\n');
5513 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5515 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5516 struct ds *result = trace->result;
5518 ds_put_char(result, '\n');
5519 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5520 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5521 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5525 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5526 void *aux OVS_UNUSED)
5528 char *dpname, *arg1, *arg2, *arg3, *arg4;
5529 char *args = xstrdup(args_);
5530 char *save_ptr = NULL;
5531 struct ofproto_dpif *ofproto;
5532 struct ofpbuf odp_key;
5533 struct ofpbuf *packet;
5534 struct rule_dpif *rule;
5535 ovs_be16 initial_tci;
5541 ofpbuf_init(&odp_key, 0);
5544 dpname = strtok_r(args, " ", &save_ptr);
5546 unixctl_command_reply(conn, 501, "Bad command syntax");
5550 ofproto = ofproto_dpif_lookup(dpname);
5552 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5556 arg1 = strtok_r(NULL, " ", &save_ptr);
5557 arg2 = strtok_r(NULL, " ", &save_ptr);
5558 arg3 = strtok_r(NULL, " ", &save_ptr);
5559 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5560 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5561 /* ofproto/trace dpname flow [-generate] */
5564 /* Convert string to datapath key. */
5565 ofpbuf_init(&odp_key, 0);
5566 error = odp_flow_key_from_string(arg1, NULL, &odp_key);
5568 unixctl_command_reply(conn, 501, "Bad flow syntax");
5572 /* Convert odp_key to flow. */
5573 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
5574 odp_key.size, &flow,
5576 if (error == ODP_FIT_ERROR) {
5577 unixctl_command_reply(conn, 501, "Invalid flow");
5581 /* Generate a packet, if requested. */
5583 packet = ofpbuf_new(0);
5584 flow_compose(packet, &flow);
5586 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5587 /* ofproto/trace dpname priority tun_id in_port packet */
5592 priority = atoi(arg1);
5593 tun_id = htonll(strtoull(arg2, NULL, 0));
5594 in_port = ofp_port_to_odp_port(atoi(arg3));
5596 packet = ofpbuf_new(strlen(args) / 2);
5597 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5598 arg4 += strspn(arg4, " ");
5599 if (*arg4 != '\0') {
5600 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5603 if (packet->size < ETH_HEADER_LEN) {
5604 unixctl_command_reply(conn, 501,
5605 "Packet data too short for Ethernet");
5609 ds_put_cstr(&result, "Packet: ");
5610 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5611 ds_put_cstr(&result, s);
5614 flow_extract(packet, priority, tun_id, in_port, &flow);
5615 initial_tci = flow.vlan_tci;
5617 unixctl_command_reply(conn, 501, "Bad command syntax");
5621 ds_put_cstr(&result, "Flow: ");
5622 flow_format(&result, &flow);
5623 ds_put_char(&result, '\n');
5625 rule = rule_dpif_lookup(ofproto, &flow, 0);
5626 trace_format_rule(&result, 0, 0, rule);
5628 struct ofproto_trace trace;
5629 struct ofpbuf *odp_actions;
5631 trace.result = &result;
5633 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, initial_tci, packet);
5634 trace.ctx.resubmit_hook = trace_resubmit;
5635 odp_actions = xlate_actions(&trace.ctx,
5636 rule->up.actions, rule->up.n_actions);
5638 ds_put_char(&result, '\n');
5639 trace_format_flow(&result, 0, "Final flow", &trace);
5640 ds_put_cstr(&result, "Datapath actions: ");
5641 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5642 ofpbuf_delete(odp_actions);
5644 if (!trace.ctx.may_set_up_flow) {
5646 ds_put_cstr(&result, "\nThis flow is not cachable.");
5648 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5649 "for complete actions, please supply a packet.");
5654 unixctl_command_reply(conn, 200, ds_cstr(&result));
5657 ds_destroy(&result);
5658 ofpbuf_delete(packet);
5659 ofpbuf_uninit(&odp_key);
5664 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5665 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5668 unixctl_command_reply(conn, 200, NULL);
5672 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5673 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5676 unixctl_command_reply(conn, 200, NULL);
5680 ofproto_dpif_unixctl_init(void)
5682 static bool registered;
5688 unixctl_command_register("ofproto/trace",
5689 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5690 ofproto_unixctl_trace, NULL);
5691 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5693 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5695 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5696 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5699 const struct ofproto_class ofproto_dpif_class = {
5726 port_is_lacp_current,
5727 NULL, /* rule_choose_table */
5734 rule_modify_actions,
5742 get_cfm_remote_mpids,
5746 get_stp_port_status,
5752 is_mirror_output_bundle,
5753 forward_bpdu_changed,