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 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? */
357 static struct subfacet *subfacet_create(struct ofproto_dpif *, struct facet *,
358 enum odp_key_fitness,
359 const struct nlattr *key,
361 static struct subfacet *subfacet_find(struct ofproto_dpif *,
362 const struct nlattr *key, size_t key_len,
363 const struct flow *);
364 static void subfacet_destroy(struct ofproto_dpif *, struct subfacet *);
365 static void subfacet_destroy__(struct ofproto_dpif *, struct subfacet *);
366 static void subfacet_reset_dp_stats(struct subfacet *,
367 struct dpif_flow_stats *);
368 static void subfacet_update_time(struct ofproto_dpif *, struct subfacet *,
370 static void subfacet_update_stats(struct ofproto_dpif *, struct subfacet *,
371 const struct dpif_flow_stats *);
372 static void subfacet_make_actions(struct ofproto_dpif *, struct subfacet *,
373 const struct ofpbuf *packet);
374 static int subfacet_install(struct ofproto_dpif *, struct subfacet *,
375 const struct nlattr *actions, size_t actions_len,
376 struct dpif_flow_stats *);
377 static void subfacet_uninstall(struct ofproto_dpif *, struct subfacet *);
383 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
384 struct list bundle_node; /* In struct ofbundle's "ports" list. */
385 struct cfm *cfm; /* Connectivity Fault Management, if any. */
386 tag_type tag; /* Tag associated with this port. */
387 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
388 bool may_enable; /* May be enabled in bonds. */
390 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
391 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
392 long long int stp_state_entered;
394 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
397 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
398 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
399 * traffic egressing the 'ofport' with that priority should be marked with. */
400 struct priority_to_dscp {
401 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
402 uint32_t priority; /* Priority of this queue (see struct flow). */
404 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
407 static struct ofport_dpif *
408 ofport_dpif_cast(const struct ofport *ofport)
410 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
411 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
414 static void port_run(struct ofport_dpif *);
415 static void port_wait(struct ofport_dpif *);
416 static int set_cfm(struct ofport *, const struct cfm_settings *);
417 static void ofport_clear_priorities(struct ofport_dpif *);
419 struct dpif_completion {
420 struct list list_node;
421 struct ofoperation *op;
424 /* Extra information about a classifier table.
425 * Currently used just for optimized flow revalidation. */
427 /* If either of these is nonnull, then this table has a form that allows
428 * flows to be tagged to avoid revalidating most flows for the most common
429 * kinds of flow table changes. */
430 struct cls_table *catchall_table; /* Table that wildcards all fields. */
431 struct cls_table *other_table; /* Table with any other wildcard set. */
432 uint32_t basis; /* Keeps each table's tags separate. */
435 struct ofproto_dpif {
444 struct netflow *netflow;
445 struct dpif_sflow *sflow;
446 struct hmap bundles; /* Contains "struct ofbundle"s. */
447 struct mac_learning *ml;
448 struct ofmirror *mirrors[MAX_MIRRORS];
449 bool has_bonded_bundles;
452 struct timer next_expiration;
456 struct hmap subfacets;
459 struct table_dpif tables[N_TABLES];
460 bool need_revalidate;
461 struct tag_set revalidate_set;
463 /* Support for debugging async flow mods. */
464 struct list completions;
466 bool has_bundle_action; /* True when the first bundle action appears. */
470 long long int stp_last_tick;
473 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
474 * for debugging the asynchronous flow_mod implementation.) */
477 static void ofproto_dpif_unixctl_init(void);
479 static struct ofproto_dpif *
480 ofproto_dpif_cast(const struct ofproto *ofproto)
482 assert(ofproto->ofproto_class == &ofproto_dpif_class);
483 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
486 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
488 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
491 /* Packet processing. */
492 static void update_learning_table(struct ofproto_dpif *,
493 const struct flow *, int vlan,
496 #define FLOW_MISS_MAX_BATCH 50
498 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
499 static void handle_miss_upcalls(struct ofproto_dpif *,
500 struct dpif_upcall *, size_t n);
502 /* Flow expiration. */
503 static int expire(struct ofproto_dpif *);
506 static void send_netflow_active_timeouts(struct ofproto_dpif *);
509 static int send_packet(const struct ofport_dpif *,
510 const struct ofpbuf *packet);
512 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
513 const struct flow *, uint32_t odp_port);
514 /* Global variables. */
515 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
517 /* Factory functions. */
520 enumerate_types(struct sset *types)
522 dp_enumerate_types(types);
526 enumerate_names(const char *type, struct sset *names)
528 return dp_enumerate_names(type, names);
532 del(const char *type, const char *name)
537 error = dpif_open(name, type, &dpif);
539 error = dpif_delete(dpif);
545 /* Basic life-cycle. */
547 static struct ofproto *
550 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
555 dealloc(struct ofproto *ofproto_)
557 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
562 construct(struct ofproto *ofproto_, int *n_tablesp)
564 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
565 const char *name = ofproto->up.name;
569 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
571 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
575 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
576 ofproto->n_matches = 0;
578 dpif_flow_flush(ofproto->dpif);
579 dpif_recv_purge(ofproto->dpif);
581 error = dpif_recv_set_mask(ofproto->dpif,
582 ((1u << DPIF_UC_MISS) |
583 (1u << DPIF_UC_ACTION)));
585 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
586 dpif_close(ofproto->dpif);
590 ofproto->netflow = NULL;
591 ofproto->sflow = NULL;
593 hmap_init(&ofproto->bundles);
594 ofproto->ml = mac_learning_create();
595 for (i = 0; i < MAX_MIRRORS; i++) {
596 ofproto->mirrors[i] = NULL;
598 ofproto->has_bonded_bundles = false;
600 timer_set_duration(&ofproto->next_expiration, 1000);
602 hmap_init(&ofproto->facets);
603 hmap_init(&ofproto->subfacets);
605 for (i = 0; i < N_TABLES; i++) {
606 struct table_dpif *table = &ofproto->tables[i];
608 table->catchall_table = NULL;
609 table->other_table = NULL;
610 table->basis = random_uint32();
612 ofproto->need_revalidate = false;
613 tag_set_init(&ofproto->revalidate_set);
615 list_init(&ofproto->completions);
617 ofproto_dpif_unixctl_init();
619 ofproto->has_bundle_action = false;
621 *n_tablesp = N_TABLES;
626 complete_operations(struct ofproto_dpif *ofproto)
628 struct dpif_completion *c, *next;
630 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
631 ofoperation_complete(c->op, 0);
632 list_remove(&c->list_node);
638 destruct(struct ofproto *ofproto_)
640 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
641 struct rule_dpif *rule, *next_rule;
642 struct classifier *table;
645 complete_operations(ofproto);
647 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
648 struct cls_cursor cursor;
650 cls_cursor_init(&cursor, table, NULL);
651 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
652 ofproto_rule_destroy(&rule->up);
656 for (i = 0; i < MAX_MIRRORS; i++) {
657 mirror_destroy(ofproto->mirrors[i]);
660 netflow_destroy(ofproto->netflow);
661 dpif_sflow_destroy(ofproto->sflow);
662 hmap_destroy(&ofproto->bundles);
663 mac_learning_destroy(ofproto->ml);
665 hmap_destroy(&ofproto->facets);
666 hmap_destroy(&ofproto->subfacets);
668 dpif_close(ofproto->dpif);
672 run(struct ofproto *ofproto_)
674 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
675 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
676 struct ofport_dpif *ofport;
677 struct ofbundle *bundle;
682 complete_operations(ofproto);
684 dpif_run(ofproto->dpif);
687 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
688 struct dpif_upcall *upcall = &misses[n_misses];
691 error = dpif_recv(ofproto->dpif, upcall);
693 if (error == ENODEV && n_misses == 0) {
699 if (upcall->type == DPIF_UC_MISS) {
700 /* Handle it later. */
703 handle_upcall(ofproto, upcall);
707 handle_miss_upcalls(ofproto, misses, n_misses);
709 if (timer_expired(&ofproto->next_expiration)) {
710 int delay = expire(ofproto);
711 timer_set_duration(&ofproto->next_expiration, delay);
714 if (ofproto->netflow) {
715 if (netflow_run(ofproto->netflow)) {
716 send_netflow_active_timeouts(ofproto);
719 if (ofproto->sflow) {
720 dpif_sflow_run(ofproto->sflow);
723 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
726 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
731 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
733 /* Now revalidate if there's anything to do. */
734 if (ofproto->need_revalidate
735 || !tag_set_is_empty(&ofproto->revalidate_set)) {
736 struct tag_set revalidate_set = ofproto->revalidate_set;
737 bool revalidate_all = ofproto->need_revalidate;
738 struct facet *facet, *next;
740 /* Clear the revalidation flags. */
741 tag_set_init(&ofproto->revalidate_set);
742 ofproto->need_revalidate = false;
744 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
746 || tag_set_intersects(&revalidate_set, facet->tags)) {
747 facet_revalidate(ofproto, facet);
756 wait(struct ofproto *ofproto_)
758 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
759 struct ofport_dpif *ofport;
760 struct ofbundle *bundle;
762 if (!clogged && !list_is_empty(&ofproto->completions)) {
763 poll_immediate_wake();
766 dpif_wait(ofproto->dpif);
767 dpif_recv_wait(ofproto->dpif);
768 if (ofproto->sflow) {
769 dpif_sflow_wait(ofproto->sflow);
771 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
772 poll_immediate_wake();
774 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
777 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
780 if (ofproto->netflow) {
781 netflow_wait(ofproto->netflow);
783 mac_learning_wait(ofproto->ml);
785 if (ofproto->need_revalidate) {
786 /* Shouldn't happen, but if it does just go around again. */
787 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
788 poll_immediate_wake();
790 timer_wait(&ofproto->next_expiration);
795 flush(struct ofproto *ofproto_)
797 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
798 struct facet *facet, *next_facet;
800 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
801 /* Mark the facet as not installed so that facet_remove() doesn't
802 * bother trying to uninstall it. There is no point in uninstalling it
803 * individually since we are about to blow away all the facets with
804 * dpif_flow_flush(). */
805 struct subfacet *subfacet;
807 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
808 subfacet->installed = false;
809 subfacet->dp_packet_count = 0;
810 subfacet->dp_byte_count = 0;
812 facet_remove(ofproto, facet);
814 dpif_flow_flush(ofproto->dpif);
818 get_features(struct ofproto *ofproto_ OVS_UNUSED,
819 bool *arp_match_ip, uint32_t *actions)
821 *arp_match_ip = true;
822 *actions = ((1u << OFPAT_OUTPUT) |
823 (1u << OFPAT_SET_VLAN_VID) |
824 (1u << OFPAT_SET_VLAN_PCP) |
825 (1u << OFPAT_STRIP_VLAN) |
826 (1u << OFPAT_SET_DL_SRC) |
827 (1u << OFPAT_SET_DL_DST) |
828 (1u << OFPAT_SET_NW_SRC) |
829 (1u << OFPAT_SET_NW_DST) |
830 (1u << OFPAT_SET_NW_TOS) |
831 (1u << OFPAT_SET_TP_SRC) |
832 (1u << OFPAT_SET_TP_DST) |
833 (1u << OFPAT_ENQUEUE));
837 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
839 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
840 struct dpif_dp_stats s;
842 strcpy(ots->name, "classifier");
844 dpif_get_dp_stats(ofproto->dpif, &s);
845 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
846 put_32aligned_be64(&ots->matched_count,
847 htonll(s.n_hit + ofproto->n_matches));
850 static struct ofport *
853 struct ofport_dpif *port = xmalloc(sizeof *port);
858 port_dealloc(struct ofport *port_)
860 struct ofport_dpif *port = ofport_dpif_cast(port_);
865 port_construct(struct ofport *port_)
867 struct ofport_dpif *port = ofport_dpif_cast(port_);
868 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
870 ofproto->need_revalidate = true;
871 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
874 port->tag = tag_create_random();
875 port->may_enable = true;
876 port->stp_port = NULL;
877 port->stp_state = STP_DISABLED;
878 hmap_init(&port->priorities);
880 if (ofproto->sflow) {
881 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
882 netdev_get_name(port->up.netdev));
889 port_destruct(struct ofport *port_)
891 struct ofport_dpif *port = ofport_dpif_cast(port_);
892 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
894 ofproto->need_revalidate = true;
895 bundle_remove(port_);
896 set_cfm(port_, NULL);
897 if (ofproto->sflow) {
898 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
901 ofport_clear_priorities(port);
902 hmap_destroy(&port->priorities);
906 port_modified(struct ofport *port_)
908 struct ofport_dpif *port = ofport_dpif_cast(port_);
910 if (port->bundle && port->bundle->bond) {
911 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
916 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
918 struct ofport_dpif *port = ofport_dpif_cast(port_);
919 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
920 ovs_be32 changed = old_config ^ port->up.opp.config;
922 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
923 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
924 ofproto->need_revalidate = true;
926 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
927 bundle_update(port->bundle);
933 set_sflow(struct ofproto *ofproto_,
934 const struct ofproto_sflow_options *sflow_options)
936 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
937 struct dpif_sflow *ds = ofproto->sflow;
941 struct ofport_dpif *ofport;
943 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
944 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
945 dpif_sflow_add_port(ds, ofport->odp_port,
946 netdev_get_name(ofport->up.netdev));
948 ofproto->need_revalidate = true;
950 dpif_sflow_set_options(ds, sflow_options);
953 dpif_sflow_destroy(ds);
954 ofproto->need_revalidate = true;
955 ofproto->sflow = NULL;
962 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
964 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
971 struct ofproto_dpif *ofproto;
973 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
974 ofproto->need_revalidate = true;
975 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
978 if (cfm_configure(ofport->cfm, s)) {
984 cfm_destroy(ofport->cfm);
990 get_cfm_fault(const struct ofport *ofport_)
992 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
994 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
998 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1001 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1004 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1011 /* Spanning Tree. */
1014 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1016 struct ofproto_dpif *ofproto = ofproto_;
1017 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1018 struct ofport_dpif *ofport;
1020 ofport = stp_port_get_aux(sp);
1022 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1023 ofproto->up.name, port_num);
1025 struct eth_header *eth = pkt->l2;
1027 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1028 if (eth_addr_is_zero(eth->eth_src)) {
1029 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1030 "with unknown MAC", ofproto->up.name, port_num);
1032 send_packet(ofport, pkt);
1038 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1040 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1042 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1044 /* Only revalidate flows if the configuration changed. */
1045 if (!s != !ofproto->stp) {
1046 ofproto->need_revalidate = true;
1050 if (!ofproto->stp) {
1051 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1052 send_bpdu_cb, ofproto);
1053 ofproto->stp_last_tick = time_msec();
1056 stp_set_bridge_id(ofproto->stp, s->system_id);
1057 stp_set_bridge_priority(ofproto->stp, s->priority);
1058 stp_set_hello_time(ofproto->stp, s->hello_time);
1059 stp_set_max_age(ofproto->stp, s->max_age);
1060 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1062 stp_destroy(ofproto->stp);
1063 ofproto->stp = NULL;
1070 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1072 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1076 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1077 s->designated_root = stp_get_designated_root(ofproto->stp);
1078 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1087 update_stp_port_state(struct ofport_dpif *ofport)
1089 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1090 enum stp_state state;
1092 /* Figure out new state. */
1093 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1097 if (ofport->stp_state != state) {
1101 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1102 netdev_get_name(ofport->up.netdev),
1103 stp_state_name(ofport->stp_state),
1104 stp_state_name(state));
1105 if (stp_learn_in_state(ofport->stp_state)
1106 != stp_learn_in_state(state)) {
1107 /* xxx Learning action flows should also be flushed. */
1108 mac_learning_flush(ofproto->ml);
1110 fwd_change = stp_forward_in_state(ofport->stp_state)
1111 != stp_forward_in_state(state);
1113 ofproto->need_revalidate = true;
1114 ofport->stp_state = state;
1115 ofport->stp_state_entered = time_msec();
1117 if (fwd_change && ofport->bundle) {
1118 bundle_update(ofport->bundle);
1121 /* Update the STP state bits in the OpenFlow port description. */
1122 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1123 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1124 : state == STP_LEARNING ? OFPPS_STP_LEARN
1125 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1126 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1128 ofproto_port_set_state(&ofport->up, of_state);
1132 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1133 * caller is responsible for assigning STP port numbers and ensuring
1134 * there are no duplicates. */
1136 set_stp_port(struct ofport *ofport_,
1137 const struct ofproto_port_stp_settings *s)
1139 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1140 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1141 struct stp_port *sp = ofport->stp_port;
1143 if (!s || !s->enable) {
1145 ofport->stp_port = NULL;
1146 stp_port_disable(sp);
1147 update_stp_port_state(ofport);
1150 } else if (sp && stp_port_no(sp) != s->port_num
1151 && ofport == stp_port_get_aux(sp)) {
1152 /* The port-id changed, so disable the old one if it's not
1153 * already in use by another port. */
1154 stp_port_disable(sp);
1157 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1158 stp_port_enable(sp);
1160 stp_port_set_aux(sp, ofport);
1161 stp_port_set_priority(sp, s->priority);
1162 stp_port_set_path_cost(sp, s->path_cost);
1164 update_stp_port_state(ofport);
1170 get_stp_port_status(struct ofport *ofport_,
1171 struct ofproto_port_stp_status *s)
1173 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1174 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1175 struct stp_port *sp = ofport->stp_port;
1177 if (!ofproto->stp || !sp) {
1183 s->port_id = stp_port_get_id(sp);
1184 s->state = stp_port_get_state(sp);
1185 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1186 s->role = stp_port_get_role(sp);
1187 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1193 stp_run(struct ofproto_dpif *ofproto)
1196 long long int now = time_msec();
1197 long long int elapsed = now - ofproto->stp_last_tick;
1198 struct stp_port *sp;
1201 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1202 ofproto->stp_last_tick = now;
1204 while (stp_get_changed_port(ofproto->stp, &sp)) {
1205 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1208 update_stp_port_state(ofport);
1215 stp_wait(struct ofproto_dpif *ofproto)
1218 poll_timer_wait(1000);
1222 /* Returns true if STP should process 'flow'. */
1224 stp_should_process_flow(const struct flow *flow)
1226 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1230 stp_process_packet(const struct ofport_dpif *ofport,
1231 const struct ofpbuf *packet)
1233 struct ofpbuf payload = *packet;
1234 struct eth_header *eth = payload.data;
1235 struct stp_port *sp = ofport->stp_port;
1237 /* Sink packets on ports that have STP disabled when the bridge has
1239 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1243 /* Trim off padding on payload. */
1244 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1245 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1248 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1249 stp_received_bpdu(sp, payload.data, payload.size);
1253 static struct priority_to_dscp *
1254 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1256 struct priority_to_dscp *pdscp;
1259 hash = hash_int(priority, 0);
1260 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1261 if (pdscp->priority == priority) {
1269 ofport_clear_priorities(struct ofport_dpif *ofport)
1271 struct priority_to_dscp *pdscp, *next;
1273 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1274 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1280 set_queues(struct ofport *ofport_,
1281 const struct ofproto_port_queue *qdscp_list,
1284 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1285 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1286 struct hmap new = HMAP_INITIALIZER(&new);
1289 for (i = 0; i < n_qdscp; i++) {
1290 struct priority_to_dscp *pdscp;
1294 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1295 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1300 pdscp = get_priority(ofport, priority);
1302 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1304 pdscp = xmalloc(sizeof *pdscp);
1305 pdscp->priority = priority;
1307 ofproto->need_revalidate = true;
1310 if (pdscp->dscp != dscp) {
1312 ofproto->need_revalidate = true;
1315 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1318 if (!hmap_is_empty(&ofport->priorities)) {
1319 ofport_clear_priorities(ofport);
1320 ofproto->need_revalidate = true;
1323 hmap_swap(&new, &ofport->priorities);
1331 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1332 * to revalidate every flow. */
1334 bundle_flush_macs(struct ofbundle *bundle)
1336 struct ofproto_dpif *ofproto = bundle->ofproto;
1337 struct mac_learning *ml = ofproto->ml;
1338 struct mac_entry *mac, *next_mac;
1340 ofproto->need_revalidate = true;
1341 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1342 if (mac->port.p == bundle) {
1343 mac_learning_expire(ml, mac);
1348 static struct ofbundle *
1349 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1351 struct ofbundle *bundle;
1353 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1354 &ofproto->bundles) {
1355 if (bundle->aux == aux) {
1362 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1363 * ones that are found to 'bundles'. */
1365 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1366 void **auxes, size_t n_auxes,
1367 struct hmapx *bundles)
1371 hmapx_init(bundles);
1372 for (i = 0; i < n_auxes; i++) {
1373 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1375 hmapx_add(bundles, bundle);
1381 bundle_update(struct ofbundle *bundle)
1383 struct ofport_dpif *port;
1385 bundle->floodable = true;
1386 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1387 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1388 bundle->floodable = false;
1395 bundle_del_port(struct ofport_dpif *port)
1397 struct ofbundle *bundle = port->bundle;
1399 bundle->ofproto->need_revalidate = true;
1401 list_remove(&port->bundle_node);
1402 port->bundle = NULL;
1405 lacp_slave_unregister(bundle->lacp, port);
1408 bond_slave_unregister(bundle->bond, port);
1411 bundle_update(bundle);
1415 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1416 struct lacp_slave_settings *lacp,
1417 uint32_t bond_stable_id)
1419 struct ofport_dpif *port;
1421 port = get_ofp_port(bundle->ofproto, ofp_port);
1426 if (port->bundle != bundle) {
1427 bundle->ofproto->need_revalidate = true;
1429 bundle_del_port(port);
1432 port->bundle = bundle;
1433 list_push_back(&bundle->ports, &port->bundle_node);
1434 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1435 bundle->floodable = false;
1439 port->bundle->ofproto->need_revalidate = true;
1440 lacp_slave_register(bundle->lacp, port, lacp);
1443 port->bond_stable_id = bond_stable_id;
1449 bundle_destroy(struct ofbundle *bundle)
1451 struct ofproto_dpif *ofproto;
1452 struct ofport_dpif *port, *next_port;
1459 ofproto = bundle->ofproto;
1460 for (i = 0; i < MAX_MIRRORS; i++) {
1461 struct ofmirror *m = ofproto->mirrors[i];
1463 if (m->out == bundle) {
1465 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1466 || hmapx_find_and_delete(&m->dsts, bundle)) {
1467 ofproto->need_revalidate = true;
1472 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1473 bundle_del_port(port);
1476 bundle_flush_macs(bundle);
1477 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1479 free(bundle->trunks);
1480 lacp_destroy(bundle->lacp);
1481 bond_destroy(bundle->bond);
1486 bundle_set(struct ofproto *ofproto_, void *aux,
1487 const struct ofproto_bundle_settings *s)
1489 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1490 bool need_flush = false;
1491 struct ofport_dpif *port;
1492 struct ofbundle *bundle;
1493 unsigned long *trunks;
1499 bundle_destroy(bundle_lookup(ofproto, aux));
1503 assert(s->n_slaves == 1 || s->bond != NULL);
1504 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1506 bundle = bundle_lookup(ofproto, aux);
1508 bundle = xmalloc(sizeof *bundle);
1510 bundle->ofproto = ofproto;
1511 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1512 hash_pointer(aux, 0));
1514 bundle->name = NULL;
1516 list_init(&bundle->ports);
1517 bundle->vlan_mode = PORT_VLAN_TRUNK;
1519 bundle->trunks = NULL;
1520 bundle->use_priority_tags = s->use_priority_tags;
1521 bundle->lacp = NULL;
1522 bundle->bond = NULL;
1524 bundle->floodable = true;
1526 bundle->src_mirrors = 0;
1527 bundle->dst_mirrors = 0;
1528 bundle->mirror_out = 0;
1531 if (!bundle->name || strcmp(s->name, bundle->name)) {
1533 bundle->name = xstrdup(s->name);
1538 if (!bundle->lacp) {
1539 ofproto->need_revalidate = true;
1540 bundle->lacp = lacp_create();
1542 lacp_configure(bundle->lacp, s->lacp);
1544 lacp_destroy(bundle->lacp);
1545 bundle->lacp = NULL;
1548 /* Update set of ports. */
1550 for (i = 0; i < s->n_slaves; i++) {
1551 if (!bundle_add_port(bundle, s->slaves[i],
1552 s->lacp ? &s->lacp_slaves[i] : NULL,
1553 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1557 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1558 struct ofport_dpif *next_port;
1560 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1561 for (i = 0; i < s->n_slaves; i++) {
1562 if (s->slaves[i] == port->up.ofp_port) {
1567 bundle_del_port(port);
1571 assert(list_size(&bundle->ports) <= s->n_slaves);
1573 if (list_is_empty(&bundle->ports)) {
1574 bundle_destroy(bundle);
1578 /* Set VLAN tagging mode */
1579 if (s->vlan_mode != bundle->vlan_mode
1580 || s->use_priority_tags != bundle->use_priority_tags) {
1581 bundle->vlan_mode = s->vlan_mode;
1582 bundle->use_priority_tags = s->use_priority_tags;
1587 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1588 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1590 if (vlan != bundle->vlan) {
1591 bundle->vlan = vlan;
1595 /* Get trunked VLANs. */
1596 switch (s->vlan_mode) {
1597 case PORT_VLAN_ACCESS:
1601 case PORT_VLAN_TRUNK:
1602 trunks = (unsigned long *) s->trunks;
1605 case PORT_VLAN_NATIVE_UNTAGGED:
1606 case PORT_VLAN_NATIVE_TAGGED:
1607 if (vlan != 0 && (!s->trunks
1608 || !bitmap_is_set(s->trunks, vlan)
1609 || bitmap_is_set(s->trunks, 0))) {
1610 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1612 trunks = bitmap_clone(s->trunks, 4096);
1614 trunks = bitmap_allocate1(4096);
1616 bitmap_set1(trunks, vlan);
1617 bitmap_set0(trunks, 0);
1619 trunks = (unsigned long *) s->trunks;
1626 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1627 free(bundle->trunks);
1628 if (trunks == s->trunks) {
1629 bundle->trunks = vlan_bitmap_clone(trunks);
1631 bundle->trunks = trunks;
1636 if (trunks != s->trunks) {
1641 if (!list_is_short(&bundle->ports)) {
1642 bundle->ofproto->has_bonded_bundles = true;
1644 if (bond_reconfigure(bundle->bond, s->bond)) {
1645 ofproto->need_revalidate = true;
1648 bundle->bond = bond_create(s->bond);
1649 ofproto->need_revalidate = true;
1652 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1653 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1657 bond_destroy(bundle->bond);
1658 bundle->bond = NULL;
1661 /* If we changed something that would affect MAC learning, un-learn
1662 * everything on this port and force flow revalidation. */
1664 bundle_flush_macs(bundle);
1671 bundle_remove(struct ofport *port_)
1673 struct ofport_dpif *port = ofport_dpif_cast(port_);
1674 struct ofbundle *bundle = port->bundle;
1677 bundle_del_port(port);
1678 if (list_is_empty(&bundle->ports)) {
1679 bundle_destroy(bundle);
1680 } else if (list_is_short(&bundle->ports)) {
1681 bond_destroy(bundle->bond);
1682 bundle->bond = NULL;
1688 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1690 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1691 struct ofport_dpif *port = port_;
1692 uint8_t ea[ETH_ADDR_LEN];
1695 error = netdev_get_etheraddr(port->up.netdev, ea);
1697 struct ofpbuf packet;
1700 ofpbuf_init(&packet, 0);
1701 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1703 memcpy(packet_pdu, pdu, pdu_size);
1705 send_packet(port, &packet);
1706 ofpbuf_uninit(&packet);
1708 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1709 "%s (%s)", port->bundle->name,
1710 netdev_get_name(port->up.netdev), strerror(error));
1715 bundle_send_learning_packets(struct ofbundle *bundle)
1717 struct ofproto_dpif *ofproto = bundle->ofproto;
1718 int error, n_packets, n_errors;
1719 struct mac_entry *e;
1721 error = n_packets = n_errors = 0;
1722 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1723 if (e->port.p != bundle) {
1724 struct ofpbuf *learning_packet;
1725 struct ofport_dpif *port;
1728 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1731 ret = send_packet(port, learning_packet);
1732 ofpbuf_delete(learning_packet);
1742 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1743 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1744 "packets, last error was: %s",
1745 bundle->name, n_errors, n_packets, strerror(error));
1747 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1748 bundle->name, n_packets);
1753 bundle_run(struct ofbundle *bundle)
1756 lacp_run(bundle->lacp, send_pdu_cb);
1759 struct ofport_dpif *port;
1761 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1762 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1765 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1766 lacp_negotiated(bundle->lacp));
1767 if (bond_should_send_learning_packets(bundle->bond)) {
1768 bundle_send_learning_packets(bundle);
1774 bundle_wait(struct ofbundle *bundle)
1777 lacp_wait(bundle->lacp);
1780 bond_wait(bundle->bond);
1787 mirror_scan(struct ofproto_dpif *ofproto)
1791 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1792 if (!ofproto->mirrors[idx]) {
1799 static struct ofmirror *
1800 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1804 for (i = 0; i < MAX_MIRRORS; i++) {
1805 struct ofmirror *mirror = ofproto->mirrors[i];
1806 if (mirror && mirror->aux == aux) {
1814 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1816 mirror_update_dups(struct ofproto_dpif *ofproto)
1820 for (i = 0; i < MAX_MIRRORS; i++) {
1821 struct ofmirror *m = ofproto->mirrors[i];
1824 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1828 for (i = 0; i < MAX_MIRRORS; i++) {
1829 struct ofmirror *m1 = ofproto->mirrors[i];
1836 for (j = i + 1; j < MAX_MIRRORS; j++) {
1837 struct ofmirror *m2 = ofproto->mirrors[j];
1839 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1840 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1841 m2->dup_mirrors |= m1->dup_mirrors;
1848 mirror_set(struct ofproto *ofproto_, void *aux,
1849 const struct ofproto_mirror_settings *s)
1851 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1852 mirror_mask_t mirror_bit;
1853 struct ofbundle *bundle;
1854 struct ofmirror *mirror;
1855 struct ofbundle *out;
1856 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1857 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1860 mirror = mirror_lookup(ofproto, aux);
1862 mirror_destroy(mirror);
1868 idx = mirror_scan(ofproto);
1870 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1872 ofproto->up.name, MAX_MIRRORS, s->name);
1876 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1877 mirror->ofproto = ofproto;
1880 mirror->out_vlan = -1;
1881 mirror->name = NULL;
1884 if (!mirror->name || strcmp(s->name, mirror->name)) {
1886 mirror->name = xstrdup(s->name);
1889 /* Get the new configuration. */
1890 if (s->out_bundle) {
1891 out = bundle_lookup(ofproto, s->out_bundle);
1893 mirror_destroy(mirror);
1899 out_vlan = s->out_vlan;
1901 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1902 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1904 /* If the configuration has not changed, do nothing. */
1905 if (hmapx_equals(&srcs, &mirror->srcs)
1906 && hmapx_equals(&dsts, &mirror->dsts)
1907 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1908 && mirror->out == out
1909 && mirror->out_vlan == out_vlan)
1911 hmapx_destroy(&srcs);
1912 hmapx_destroy(&dsts);
1916 hmapx_swap(&srcs, &mirror->srcs);
1917 hmapx_destroy(&srcs);
1919 hmapx_swap(&dsts, &mirror->dsts);
1920 hmapx_destroy(&dsts);
1922 free(mirror->vlans);
1923 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1926 mirror->out_vlan = out_vlan;
1928 /* Update bundles. */
1929 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1930 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1931 if (hmapx_contains(&mirror->srcs, bundle)) {
1932 bundle->src_mirrors |= mirror_bit;
1934 bundle->src_mirrors &= ~mirror_bit;
1937 if (hmapx_contains(&mirror->dsts, bundle)) {
1938 bundle->dst_mirrors |= mirror_bit;
1940 bundle->dst_mirrors &= ~mirror_bit;
1943 if (mirror->out == bundle) {
1944 bundle->mirror_out |= mirror_bit;
1946 bundle->mirror_out &= ~mirror_bit;
1950 ofproto->need_revalidate = true;
1951 mac_learning_flush(ofproto->ml);
1952 mirror_update_dups(ofproto);
1958 mirror_destroy(struct ofmirror *mirror)
1960 struct ofproto_dpif *ofproto;
1961 mirror_mask_t mirror_bit;
1962 struct ofbundle *bundle;
1968 ofproto = mirror->ofproto;
1969 ofproto->need_revalidate = true;
1970 mac_learning_flush(ofproto->ml);
1972 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1973 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1974 bundle->src_mirrors &= ~mirror_bit;
1975 bundle->dst_mirrors &= ~mirror_bit;
1976 bundle->mirror_out &= ~mirror_bit;
1979 hmapx_destroy(&mirror->srcs);
1980 hmapx_destroy(&mirror->dsts);
1981 free(mirror->vlans);
1983 ofproto->mirrors[mirror->idx] = NULL;
1987 mirror_update_dups(ofproto);
1991 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1993 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1994 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1995 ofproto->need_revalidate = true;
1996 mac_learning_flush(ofproto->ml);
2002 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2004 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2005 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2006 return bundle && bundle->mirror_out != 0;
2010 forward_bpdu_changed(struct ofproto *ofproto_)
2012 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2013 /* Revalidate cached flows whenever forward_bpdu option changes. */
2014 ofproto->need_revalidate = true;
2019 static struct ofport_dpif *
2020 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
2022 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2023 return ofport ? ofport_dpif_cast(ofport) : NULL;
2026 static struct ofport_dpif *
2027 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
2029 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2033 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2034 struct dpif_port *dpif_port)
2036 ofproto_port->name = dpif_port->name;
2037 ofproto_port->type = dpif_port->type;
2038 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2042 port_run(struct ofport_dpif *ofport)
2044 bool enable = netdev_get_carrier(ofport->up.netdev);
2047 cfm_run(ofport->cfm);
2049 if (cfm_should_send_ccm(ofport->cfm)) {
2050 struct ofpbuf packet;
2052 ofpbuf_init(&packet, 0);
2053 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
2054 send_packet(ofport, &packet);
2055 ofpbuf_uninit(&packet);
2058 enable = enable && !cfm_get_fault(ofport->cfm)
2059 && cfm_get_opup(ofport->cfm);
2062 if (ofport->bundle) {
2063 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2066 if (ofport->may_enable != enable) {
2067 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2069 if (ofproto->has_bundle_action) {
2070 ofproto->need_revalidate = true;
2074 ofport->may_enable = enable;
2078 port_wait(struct ofport_dpif *ofport)
2081 cfm_wait(ofport->cfm);
2086 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2087 struct ofproto_port *ofproto_port)
2089 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2090 struct dpif_port dpif_port;
2093 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2095 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2101 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2103 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2107 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2109 *ofp_portp = odp_port_to_ofp_port(odp_port);
2115 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2117 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2120 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2122 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2124 /* The caller is going to close ofport->up.netdev. If this is a
2125 * bonded port, then the bond is using that netdev, so remove it
2126 * from the bond. The client will need to reconfigure everything
2127 * after deleting ports, so then the slave will get re-added. */
2128 bundle_remove(&ofport->up);
2134 struct port_dump_state {
2135 struct dpif_port_dump dump;
2140 port_dump_start(const struct ofproto *ofproto_, void **statep)
2142 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2143 struct port_dump_state *state;
2145 *statep = state = xmalloc(sizeof *state);
2146 dpif_port_dump_start(&state->dump, ofproto->dpif);
2147 state->done = false;
2152 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2153 struct ofproto_port *port)
2155 struct port_dump_state *state = state_;
2156 struct dpif_port dpif_port;
2158 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2159 ofproto_port_from_dpif_port(port, &dpif_port);
2162 int error = dpif_port_dump_done(&state->dump);
2164 return error ? error : EOF;
2169 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2171 struct port_dump_state *state = state_;
2174 dpif_port_dump_done(&state->dump);
2181 port_poll(const struct ofproto *ofproto_, char **devnamep)
2183 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2184 return dpif_port_poll(ofproto->dpif, devnamep);
2188 port_poll_wait(const struct ofproto *ofproto_)
2190 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2191 dpif_port_poll_wait(ofproto->dpif);
2195 port_is_lacp_current(const struct ofport *ofport_)
2197 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2198 return (ofport->bundle && ofport->bundle->lacp
2199 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2203 /* Upcall handling. */
2205 /* Flow miss batching.
2207 * Some dpifs implement operations faster when you hand them off in a batch.
2208 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2209 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2210 * more packets, plus possibly installing the flow in the dpif.
2212 * So far we only batch the operations that affect flow setup time the most.
2213 * It's possible to batch more than that, but the benefit might be minimal. */
2215 struct hmap_node hmap_node;
2217 enum odp_key_fitness key_fitness;
2218 const struct nlattr *key;
2220 struct list packets;
2223 struct flow_miss_op {
2224 union dpif_op dpif_op;
2225 struct subfacet *subfacet;
2228 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2229 * OpenFlow controller as necessary according to their individual
2232 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2233 * ownership is transferred to this function. */
2235 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2236 const struct flow *flow, bool clone)
2238 struct ofputil_packet_in pin;
2240 pin.packet = packet;
2241 pin.in_port = flow->in_port;
2242 pin.reason = OFPR_NO_MATCH;
2243 pin.buffer_id = 0; /* not yet known */
2244 pin.send_len = 0; /* not used for flow table misses */
2245 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2246 clone ? NULL : packet);
2249 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2250 * OpenFlow controller as necessary according to their individual
2253 * 'send_len' should be the number of bytes of 'packet' to send to the
2254 * controller, as specified in the action that caused the packet to be sent.
2256 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2257 * Otherwise, ownership is transferred to this function. */
2259 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2260 uint64_t userdata, const struct flow *flow, bool clone)
2262 struct ofputil_packet_in pin;
2263 struct user_action_cookie cookie;
2265 memcpy(&cookie, &userdata, sizeof(cookie));
2267 pin.packet = packet;
2268 pin.in_port = flow->in_port;
2269 pin.reason = OFPR_ACTION;
2270 pin.buffer_id = 0; /* not yet known */
2271 pin.send_len = cookie.data;
2272 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2273 clone ? NULL : packet);
2277 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2278 const struct ofpbuf *packet)
2280 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2286 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2288 cfm_process_heartbeat(ofport->cfm, packet);
2291 } else if (ofport->bundle && ofport->bundle->lacp
2292 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2294 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2297 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2299 stp_process_packet(ofport, packet);
2306 static struct flow_miss *
2307 flow_miss_create(struct hmap *todo, const struct flow *flow,
2308 enum odp_key_fitness key_fitness,
2309 const struct nlattr *key, size_t key_len)
2311 uint32_t hash = flow_hash(flow, 0);
2312 struct flow_miss *miss;
2314 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2315 if (flow_equal(&miss->flow, flow)) {
2320 miss = xmalloc(sizeof *miss);
2321 hmap_insert(todo, &miss->hmap_node, hash);
2323 miss->key_fitness = key_fitness;
2325 miss->key_len = key_len;
2326 list_init(&miss->packets);
2331 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2332 struct flow_miss_op *ops, size_t *n_ops)
2334 const struct flow *flow = &miss->flow;
2335 struct ofpbuf *packet, *next_packet;
2336 struct subfacet *subfacet;
2337 struct facet *facet;
2339 facet = facet_lookup_valid(ofproto, flow);
2341 struct rule_dpif *rule;
2343 rule = rule_dpif_lookup(ofproto, flow, 0);
2345 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2346 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2348 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2349 COVERAGE_INC(ofproto_dpif_no_packet_in);
2350 /* XXX install 'drop' flow entry */
2354 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2358 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2360 list_remove(&packet->list_node);
2361 send_packet_in_miss(ofproto, packet, flow, false);
2367 facet = facet_create(rule, flow);
2370 subfacet = subfacet_create(ofproto, facet,
2371 miss->key_fitness, miss->key, miss->key_len);
2373 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2374 list_remove(&packet->list_node);
2375 ofproto->n_matches++;
2377 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2379 * Extra-special case for fail-open mode.
2381 * We are in fail-open mode and the packet matched the fail-open
2382 * rule, but we are connected to a controller too. We should send
2383 * the packet up to the controller in the hope that it will try to
2384 * set up a flow and thereby allow us to exit fail-open.
2386 * See the top-level comment in fail-open.c for more information.
2388 send_packet_in_miss(ofproto, packet, flow, true);
2391 if (!facet->may_install || !subfacet->actions) {
2392 subfacet_make_actions(ofproto, subfacet, packet);
2394 if (!execute_controller_action(ofproto, &facet->flow,
2396 subfacet->actions_len, packet)) {
2397 struct flow_miss_op *op = &ops[(*n_ops)++];
2398 struct dpif_execute *execute = &op->dpif_op.execute;
2400 op->subfacet = subfacet;
2401 execute->type = DPIF_OP_EXECUTE;
2402 execute->key = miss->key;
2403 execute->key_len = miss->key_len;
2405 = (facet->may_install
2407 : xmemdup(subfacet->actions, subfacet->actions_len));
2408 execute->actions_len = subfacet->actions_len;
2409 execute->packet = packet;
2413 if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
2414 struct flow_miss_op *op = &ops[(*n_ops)++];
2415 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2417 op->subfacet = subfacet;
2418 put->type = DPIF_OP_FLOW_PUT;
2419 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2420 put->key = miss->key;
2421 put->key_len = miss->key_len;
2422 put->actions = subfacet->actions;
2423 put->actions_len = subfacet->actions_len;
2429 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2432 struct dpif_upcall *upcall;
2433 struct flow_miss *miss, *next_miss;
2434 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2435 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2444 /* Construct the to-do list.
2446 * This just amounts to extracting the flow from each packet and sticking
2447 * the packets that have the same flow in the same "flow_miss" structure so
2448 * that we can process them together. */
2450 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2451 enum odp_key_fitness fitness;
2452 struct flow_miss *miss;
2455 /* Obtain metadata and check userspace/kernel agreement on flow match,
2456 * then set 'flow''s header pointers. */
2457 fitness = odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2458 if (fitness == ODP_FIT_ERROR) {
2461 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2462 flow.in_port, &flow);
2464 /* Handle 802.1ag, LACP, and STP specially. */
2465 if (process_special(ofproto, &flow, upcall->packet)) {
2466 ofpbuf_delete(upcall->packet);
2467 ofproto->n_matches++;
2471 /* Add other packets to a to-do list. */
2472 miss = flow_miss_create(&todo, &flow, fitness,
2473 upcall->key, upcall->key_len);
2474 list_push_back(&miss->packets, &upcall->packet->list_node);
2477 /* Process each element in the to-do list, constructing the set of
2478 * operations to batch. */
2480 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2481 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2482 ofpbuf_list_delete(&miss->packets);
2483 hmap_remove(&todo, &miss->hmap_node);
2486 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2487 hmap_destroy(&todo);
2489 /* Execute batch. */
2490 for (i = 0; i < n_ops; i++) {
2491 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2493 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2495 /* Free memory and update facets. */
2496 for (i = 0; i < n_ops; i++) {
2497 struct flow_miss_op *op = &flow_miss_ops[i];
2498 struct dpif_execute *execute;
2499 struct dpif_flow_put *put;
2501 switch (op->dpif_op.type) {
2502 case DPIF_OP_EXECUTE:
2503 execute = &op->dpif_op.execute;
2504 if (op->subfacet->actions != execute->actions) {
2505 free((struct nlattr *) execute->actions);
2507 ofpbuf_delete((struct ofpbuf *) execute->packet);
2510 case DPIF_OP_FLOW_PUT:
2511 put = &op->dpif_op.flow_put;
2513 op->subfacet->installed = true;
2521 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2522 struct dpif_upcall *upcall)
2525 struct user_action_cookie cookie;
2527 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2529 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2530 if (ofproto->sflow) {
2531 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2532 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2534 ofpbuf_delete(upcall->packet);
2536 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2537 COVERAGE_INC(ofproto_dpif_ctlr_action);
2538 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2539 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2542 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2547 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2549 switch (upcall->type) {
2550 case DPIF_UC_ACTION:
2551 handle_userspace_upcall(ofproto, upcall);
2555 /* The caller handles these. */
2558 case DPIF_N_UC_TYPES:
2560 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2565 /* Flow expiration. */
2567 static int subfacet_max_idle(const struct ofproto_dpif *);
2568 static void update_stats(struct ofproto_dpif *);
2569 static void rule_expire(struct rule_dpif *);
2570 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
2572 /* This function is called periodically by run(). Its job is to collect
2573 * updates for the flows that have been installed into the datapath, most
2574 * importantly when they last were used, and then use that information to
2575 * expire flows that have not been used recently.
2577 * Returns the number of milliseconds after which it should be called again. */
2579 expire(struct ofproto_dpif *ofproto)
2581 struct rule_dpif *rule, *next_rule;
2582 struct classifier *table;
2585 /* Update stats for each flow in the datapath. */
2586 update_stats(ofproto);
2588 /* Expire subfacets that have been idle too long. */
2589 dp_max_idle = subfacet_max_idle(ofproto);
2590 expire_subfacets(ofproto, dp_max_idle);
2592 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2593 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2594 struct cls_cursor cursor;
2596 cls_cursor_init(&cursor, table, NULL);
2597 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2602 /* All outstanding data in existing flows has been accounted, so it's a
2603 * good time to do bond rebalancing. */
2604 if (ofproto->has_bonded_bundles) {
2605 struct ofbundle *bundle;
2607 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2609 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2614 return MIN(dp_max_idle, 1000);
2617 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2619 * This function also pushes statistics updates to rules which each facet
2620 * resubmits into. Generally these statistics will be accurate. However, if a
2621 * facet changes the rule it resubmits into at some time in between
2622 * update_stats() runs, it is possible that statistics accrued to the
2623 * old rule will be incorrectly attributed to the new rule. This could be
2624 * avoided by calling update_stats() whenever rules are created or
2625 * deleted. However, the performance impact of making so many calls to the
2626 * datapath do not justify the benefit of having perfectly accurate statistics.
2629 update_stats(struct ofproto_dpif *p)
2631 const struct dpif_flow_stats *stats;
2632 struct dpif_flow_dump dump;
2633 const struct nlattr *key;
2636 dpif_flow_dump_start(&dump, p->dpif);
2637 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2638 enum odp_key_fitness fitness;
2639 struct subfacet *subfacet;
2642 fitness = odp_flow_key_to_flow(key, key_len, &flow);
2643 if (fitness == ODP_FIT_ERROR) {
2647 subfacet = subfacet_find(p, key, key_len, &flow);
2648 if (subfacet && subfacet->installed) {
2649 struct facet *facet = subfacet->facet;
2651 if (stats->n_packets >= subfacet->dp_packet_count) {
2652 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
2653 facet->packet_count += extra;
2655 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2658 if (stats->n_bytes >= subfacet->dp_byte_count) {
2659 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
2661 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2664 subfacet->dp_packet_count = stats->n_packets;
2665 subfacet->dp_byte_count = stats->n_bytes;
2667 subfacet_update_time(p, subfacet, stats->used);
2668 facet_account(p, facet);
2669 facet_push_stats(facet);
2671 /* There's a flow in the datapath that we know nothing about, or a
2672 * flow that shouldn't be installed but was anyway. Delete it. */
2673 COVERAGE_INC(facet_unexpected);
2674 dpif_flow_del(p->dpif, key, key_len, NULL);
2677 dpif_flow_dump_done(&dump);
2680 /* Calculates and returns the number of milliseconds of idle time after which
2681 * subfacets should expire from the datapath. When a subfacet expires, we fold
2682 * its statistics into its facet, and when a facet's last subfacet expires, we
2683 * fold its statistic into its rule. */
2685 subfacet_max_idle(const struct ofproto_dpif *ofproto)
2688 * Idle time histogram.
2690 * Most of the time a switch has a relatively small number of subfacets.
2691 * When this is the case we might as well keep statistics for all of them
2692 * in userspace and to cache them in the kernel datapath for performance as
2695 * As the number of subfacets increases, the memory required to maintain
2696 * statistics about them in userspace and in the kernel becomes
2697 * significant. However, with a large number of subfacets it is likely
2698 * that only a few of them are "heavy hitters" that consume a large amount
2699 * of bandwidth. At this point, only heavy hitters are worth caching in
2700 * the kernel and maintaining in userspaces; other subfacets we can
2703 * The technique used to compute the idle time is to build a histogram with
2704 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
2705 * that is installed in the kernel gets dropped in the appropriate bucket.
2706 * After the histogram has been built, we compute the cutoff so that only
2707 * the most-recently-used 1% of subfacets (but at least
2708 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2709 * the most-recently-used bucket of subfacets is kept, so actually an
2710 * arbitrary number of subfacets can be kept in any given expiration run
2711 * (though the next run will delete most of those unless they receive
2714 * This requires a second pass through the subfacets, in addition to the
2715 * pass made by update_stats(), because the former function never looks at
2716 * uninstallable subfacets.
2718 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2719 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2720 int buckets[N_BUCKETS] = { 0 };
2721 int total, subtotal, bucket;
2722 struct subfacet *subfacet;
2726 total = hmap_count(&ofproto->subfacets);
2727 if (total <= ofproto->up.flow_eviction_threshold) {
2728 return N_BUCKETS * BUCKET_WIDTH;
2731 /* Build histogram. */
2733 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
2734 long long int idle = now - subfacet->used;
2735 int bucket = (idle <= 0 ? 0
2736 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2737 : (unsigned int) idle / BUCKET_WIDTH);
2741 /* Find the first bucket whose flows should be expired. */
2742 subtotal = bucket = 0;
2744 subtotal += buckets[bucket++];
2745 } while (bucket < N_BUCKETS &&
2746 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2748 if (VLOG_IS_DBG_ENABLED()) {
2752 ds_put_cstr(&s, "keep");
2753 for (i = 0; i < N_BUCKETS; i++) {
2755 ds_put_cstr(&s, ", drop");
2758 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2761 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2765 return bucket * BUCKET_WIDTH;
2769 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
2771 long long int cutoff = time_msec() - dp_max_idle;
2772 struct subfacet *subfacet, *next_subfacet;
2774 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
2775 &ofproto->subfacets) {
2776 if (subfacet->used < cutoff) {
2777 subfacet_destroy(ofproto, subfacet);
2782 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2783 * then delete it entirely. */
2785 rule_expire(struct rule_dpif *rule)
2787 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2788 struct facet *facet, *next_facet;
2792 /* Has 'rule' expired? */
2794 if (rule->up.hard_timeout
2795 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2796 reason = OFPRR_HARD_TIMEOUT;
2797 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2798 && now > rule->used + rule->up.idle_timeout * 1000) {
2799 reason = OFPRR_IDLE_TIMEOUT;
2804 COVERAGE_INC(ofproto_dpif_expired);
2806 /* Update stats. (This is a no-op if the rule expired due to an idle
2807 * timeout, because that only happens when the rule has no facets left.) */
2808 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2809 facet_remove(ofproto, facet);
2812 /* Get rid of the rule. */
2813 ofproto_rule_expire(&rule->up, reason);
2818 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2820 * The caller must already have determined that no facet with an identical
2821 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2822 * the ofproto's classifier table.
2824 * The facet will initially have no subfacets. The caller should create (at
2825 * least) one subfacet with subfacet_create(). */
2826 static struct facet *
2827 facet_create(struct rule_dpif *rule, const struct flow *flow)
2829 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2830 struct facet *facet;
2832 facet = xzalloc(sizeof *facet);
2833 facet->used = time_msec();
2834 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2835 list_push_back(&rule->facets, &facet->list_node);
2837 facet->flow = *flow;
2838 list_init(&facet->subfacets);
2839 netflow_flow_init(&facet->nf_flow);
2840 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2846 facet_free(struct facet *facet)
2852 execute_controller_action(struct ofproto_dpif *ofproto,
2853 const struct flow *flow,
2854 const struct nlattr *odp_actions, size_t actions_len,
2855 struct ofpbuf *packet)
2858 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2859 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2860 /* As an optimization, avoid a round-trip from userspace to kernel to
2861 * userspace. This also avoids possibly filling up kernel packet
2862 * buffers along the way.
2864 * This optimization will not accidentally catch sFlow
2865 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2866 * inside OVS_ACTION_ATTR_SAMPLE. */
2867 const struct nlattr *nla;
2869 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2870 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2878 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2879 * 'packet', which arrived on 'in_port'.
2881 * Takes ownership of 'packet'. */
2883 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2884 const struct nlattr *odp_actions, size_t actions_len,
2885 struct ofpbuf *packet)
2887 struct odputil_keybuf keybuf;
2891 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2896 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2897 odp_flow_key_from_flow(&key, flow);
2899 error = dpif_execute(ofproto->dpif, key.data, key.size,
2900 odp_actions, actions_len, packet);
2902 ofpbuf_delete(packet);
2906 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2908 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2909 * rule's statistics, via subfacet_uninstall().
2911 * - Removes 'facet' from its rule and from ofproto->facets.
2914 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2916 struct subfacet *subfacet, *next_subfacet;
2918 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
2919 &facet->subfacets) {
2920 subfacet_destroy__(ofproto, subfacet);
2923 facet_flush_stats(ofproto, facet);
2924 hmap_remove(&ofproto->facets, &facet->hmap_node);
2925 list_remove(&facet->list_node);
2930 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2933 struct subfacet *subfacet;
2934 const struct nlattr *a;
2938 if (facet->byte_count <= facet->accounted_bytes) {
2941 n_bytes = facet->byte_count - facet->accounted_bytes;
2942 facet->accounted_bytes = facet->byte_count;
2944 /* Feed information from the active flows back into the learning table to
2945 * ensure that table is always in sync with what is actually flowing
2946 * through the datapath. */
2947 if (facet->has_learn || facet->has_normal) {
2948 struct action_xlate_ctx ctx;
2950 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2951 ctx.may_learn = true;
2952 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2953 facet->rule->up.n_actions));
2956 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2960 /* This loop feeds byte counters to bond_account() for rebalancing to use
2961 * as a basis. We also need to track the actual VLAN on which the packet
2962 * is going to be sent to ensure that it matches the one passed to
2963 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2966 * We use the actions from an arbitrary subfacet because they should all
2967 * be equally valid for our purpose. */
2968 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
2969 struct subfacet, list_node);
2970 vlan_tci = facet->flow.vlan_tci;
2971 NL_ATTR_FOR_EACH_UNSAFE (a, left,
2972 subfacet->actions, subfacet->actions_len) {
2973 const struct ovs_action_push_vlan *vlan;
2974 struct ofport_dpif *port;
2976 switch (nl_attr_type(a)) {
2977 case OVS_ACTION_ATTR_OUTPUT:
2978 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2979 if (port && port->bundle && port->bundle->bond) {
2980 bond_account(port->bundle->bond, &facet->flow,
2981 vlan_tci_to_vid(vlan_tci), n_bytes);
2985 case OVS_ACTION_ATTR_POP_VLAN:
2986 vlan_tci = htons(0);
2989 case OVS_ACTION_ATTR_PUSH_VLAN:
2990 vlan = nl_attr_get(a);
2991 vlan_tci = vlan->vlan_tci;
2997 /* Returns true if the only action for 'facet' is to send to the controller.
2998 * (We don't report NetFlow expiration messages for such facets because they
2999 * are just part of the control logic for the network, not real traffic). */
3001 facet_is_controller_flow(struct facet *facet)
3004 && facet->rule->up.n_actions == 1
3005 && action_outputs_to_port(&facet->rule->up.actions[0],
3006 htons(OFPP_CONTROLLER)));
3009 /* Folds all of 'facet''s statistics into its rule. Also updates the
3010 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3011 * 'facet''s statistics in the datapath should have been zeroed and folded into
3012 * its packet and byte counts before this function is called. */
3014 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3016 struct subfacet *subfacet;
3018 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3019 assert(!subfacet->dp_byte_count);
3020 assert(!subfacet->dp_packet_count);
3023 facet_push_stats(facet);
3024 facet_account(ofproto, facet);
3026 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3027 struct ofexpired expired;
3028 expired.flow = facet->flow;
3029 expired.packet_count = facet->packet_count;
3030 expired.byte_count = facet->byte_count;
3031 expired.used = facet->used;
3032 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3035 facet->rule->packet_count += facet->packet_count;
3036 facet->rule->byte_count += facet->byte_count;
3038 /* Reset counters to prevent double counting if 'facet' ever gets
3040 facet_reset_counters(facet);
3042 netflow_flow_clear(&facet->nf_flow);
3045 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3046 * Returns it if found, otherwise a null pointer.
3048 * The returned facet might need revalidation; use facet_lookup_valid()
3049 * instead if that is important. */
3050 static struct facet *
3051 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3053 struct facet *facet;
3055 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3057 if (flow_equal(flow, &facet->flow)) {
3065 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3066 * Returns it if found, otherwise a null pointer.
3068 * The returned facet is guaranteed to be valid. */
3069 static struct facet *
3070 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3072 struct facet *facet = facet_find(ofproto, flow);
3074 /* The facet we found might not be valid, since we could be in need of
3075 * revalidation. If it is not valid, don't return it. */
3077 && (ofproto->need_revalidate
3078 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3079 && !facet_revalidate(ofproto, facet)) {
3080 COVERAGE_INC(facet_invalidated);
3087 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3089 * - If the rule found is different from 'facet''s current rule, moves
3090 * 'facet' to the new rule and recompiles its actions.
3092 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3093 * where it is and recompiles its actions anyway.
3095 * - If there is none, destroys 'facet'.
3097 * Returns true if 'facet' still exists, false if it has been destroyed. */
3099 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3102 struct nlattr *odp_actions;
3105 struct actions *new_actions;
3107 struct action_xlate_ctx ctx;
3108 struct rule_dpif *new_rule;
3109 struct subfacet *subfacet;
3110 bool actions_changed;
3113 COVERAGE_INC(facet_revalidate);
3115 /* Determine the new rule. */
3116 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3118 /* No new rule, so delete the facet. */
3119 facet_remove(ofproto, facet);
3123 /* Calculate new datapath actions.
3125 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3126 * emit a NetFlow expiration and, if so, we need to have the old state
3127 * around to properly compose it. */
3129 /* If the datapath actions changed or the installability changed,
3130 * then we need to talk to the datapath. */
3133 memset(&ctx, 0, sizeof ctx);
3134 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3135 struct ofpbuf *odp_actions;
3136 bool should_install;
3138 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3139 odp_actions = xlate_actions(&ctx, new_rule->up.actions,
3140 new_rule->up.n_actions);
3141 actions_changed = (subfacet->actions_len != odp_actions->size
3142 || memcmp(subfacet->actions, odp_actions->data,
3143 subfacet->actions_len));
3145 should_install = (ctx.may_set_up_flow
3146 && subfacet->key_fitness != ODP_FIT_TOO_LITTLE);
3147 if (actions_changed || should_install != subfacet->installed) {
3148 if (should_install) {
3149 struct dpif_flow_stats stats;
3151 subfacet_install(ofproto, subfacet,
3152 odp_actions->data, odp_actions->size, &stats);
3153 subfacet_update_stats(ofproto, subfacet, &stats);
3155 subfacet_uninstall(ofproto, subfacet);
3159 new_actions = xcalloc(list_size(&facet->subfacets),
3160 sizeof *new_actions);
3162 new_actions[i].odp_actions = xmemdup(odp_actions->data,
3164 new_actions[i].actions_len = odp_actions->size;
3167 ofpbuf_delete(odp_actions);
3171 facet_flush_stats(ofproto, facet);
3174 /* Update 'facet' now that we've taken care of all the old state. */
3175 facet->tags = ctx.tags;
3176 facet->nf_flow.output_iface = ctx.nf_output_iface;
3177 facet->may_install = ctx.may_set_up_flow;
3178 facet->has_learn = ctx.has_learn;
3179 facet->has_normal = ctx.has_normal;
3182 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3183 if (new_actions[i].odp_actions) {
3184 free(subfacet->actions);
3185 subfacet->actions = new_actions[i].odp_actions;
3186 subfacet->actions_len = new_actions[i].actions_len;
3192 if (facet->rule != new_rule) {
3193 COVERAGE_INC(facet_changed_rule);
3194 list_remove(&facet->list_node);
3195 list_push_back(&new_rule->facets, &facet->list_node);
3196 facet->rule = new_rule;
3197 facet->used = new_rule->up.created;
3198 facet->rs_used = facet->used;
3204 /* Updates 'facet''s used time. Caller is responsible for calling
3205 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3207 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3210 if (used > facet->used) {
3212 if (used > facet->rule->used) {
3213 facet->rule->used = used;
3215 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3220 facet_reset_counters(struct facet *facet)
3222 facet->packet_count = 0;
3223 facet->byte_count = 0;
3224 facet->rs_packet_count = 0;
3225 facet->rs_byte_count = 0;
3226 facet->accounted_bytes = 0;
3230 facet_push_stats(struct facet *facet)
3232 uint64_t rs_packets, rs_bytes;
3234 assert(facet->packet_count >= facet->rs_packet_count);
3235 assert(facet->byte_count >= facet->rs_byte_count);
3236 assert(facet->used >= facet->rs_used);
3238 rs_packets = facet->packet_count - facet->rs_packet_count;
3239 rs_bytes = facet->byte_count - facet->rs_byte_count;
3241 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3242 facet->rs_packet_count = facet->packet_count;
3243 facet->rs_byte_count = facet->byte_count;
3244 facet->rs_used = facet->used;
3246 flow_push_stats(facet->rule, &facet->flow,
3247 rs_packets, rs_bytes, facet->used);
3251 struct ofproto_push {
3252 struct action_xlate_ctx ctx;
3259 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3261 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3264 rule->packet_count += push->packets;
3265 rule->byte_count += push->bytes;
3266 rule->used = MAX(push->used, rule->used);
3270 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3271 * 'rule''s actions. */
3273 flow_push_stats(const struct rule_dpif *rule,
3274 const struct flow *flow, uint64_t packets, uint64_t bytes,
3277 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3278 struct ofproto_push push;
3280 push.packets = packets;
3284 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3285 push.ctx.resubmit_hook = push_resubmit;
3286 ofpbuf_delete(xlate_actions(&push.ctx,
3287 rule->up.actions, rule->up.n_actions));
3292 static struct subfacet *
3293 subfacet_find__(struct ofproto_dpif *ofproto,
3294 const struct nlattr *key, size_t key_len, uint32_t key_hash,
3295 const struct flow *flow)
3297 struct subfacet *subfacet;
3299 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
3300 &ofproto->subfacets) {
3302 ? (subfacet->key_len == key_len
3303 && !memcmp(key, subfacet->key, key_len))
3304 : flow_equal(flow, &subfacet->facet->flow)) {
3312 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
3313 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
3314 * there is one, otherwise creates and returns a new subfacet.
3316 * If the returned subfacet is new, then subfacet->actions will be NULL, in
3317 * which case the caller must populate the actions with
3318 * subfacet_make_actions(). */
3319 static struct subfacet *
3320 subfacet_create(struct ofproto_dpif *ofproto, struct facet *facet,
3321 enum odp_key_fitness key_fitness,
3322 const struct nlattr *key, size_t key_len)
3324 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3325 struct subfacet *subfacet;
3327 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
3329 if (subfacet->facet == facet) {
3333 /* This shouldn't happen. */
3334 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3335 subfacet_destroy(ofproto, subfacet);
3338 subfacet = xzalloc(sizeof *subfacet);
3339 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
3340 list_push_back(&facet->subfacets, &subfacet->list_node);
3341 subfacet->facet = facet;
3342 subfacet->used = time_msec();
3343 subfacet->key_fitness = key_fitness;
3344 if (key_fitness != ODP_FIT_PERFECT) {
3345 subfacet->key = xmemdup(key, key_len);
3346 subfacet->key_len = key_len;
3348 subfacet->installed = false;
3353 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
3354 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
3355 static struct subfacet *
3356 subfacet_find(struct ofproto_dpif *ofproto,
3357 const struct nlattr *key, size_t key_len,
3358 const struct flow *flow)
3360 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3362 return subfacet_find__(ofproto, key, key_len, key_hash, flow);
3365 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
3366 * its facet within 'ofproto', and frees it. */
3368 subfacet_destroy__(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3370 subfacet_uninstall(ofproto, subfacet);
3371 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
3372 list_remove(&subfacet->list_node);
3373 free(subfacet->key);
3374 free(subfacet->actions);
3378 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
3379 * last remaining subfacet in its facet destroys the facet too. */
3381 subfacet_destroy(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3383 struct facet *facet = subfacet->facet;
3385 subfacet_destroy__(ofproto, subfacet);
3386 if (list_is_empty(&facet->subfacets)) {
3387 facet_remove(ofproto, facet);
3391 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
3392 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
3393 * for use as temporary storage. */
3395 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
3398 if (!subfacet->key) {
3399 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
3400 odp_flow_key_from_flow(key, &subfacet->facet->flow);
3402 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
3406 /* Composes the datapath actions for 'subfacet' based on its rule's actions. */
3408 subfacet_make_actions(struct ofproto_dpif *p, struct subfacet *subfacet,
3409 const struct ofpbuf *packet)
3411 struct facet *facet = subfacet->facet;
3412 const struct rule_dpif *rule = facet->rule;
3413 struct ofpbuf *odp_actions;
3414 struct action_xlate_ctx ctx;
3416 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
3417 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3418 facet->tags = ctx.tags;
3419 facet->may_install = ctx.may_set_up_flow;
3420 facet->has_learn = ctx.has_learn;
3421 facet->has_normal = ctx.has_normal;
3422 facet->nf_flow.output_iface = ctx.nf_output_iface;
3424 if (subfacet->actions_len != odp_actions->size
3425 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
3426 free(subfacet->actions);
3427 subfacet->actions_len = odp_actions->size;
3428 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
3431 ofpbuf_delete(odp_actions);
3434 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
3435 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
3436 * in the datapath will be zeroed and 'stats' will be updated with traffic new
3437 * since 'subfacet' was last updated.
3439 * Returns 0 if successful, otherwise a positive errno value. */
3441 subfacet_install(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3442 const struct nlattr *actions, size_t actions_len,
3443 struct dpif_flow_stats *stats)
3445 struct odputil_keybuf keybuf;
3446 enum dpif_flow_put_flags flags;
3450 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3452 flags |= DPIF_FP_ZERO_STATS;
3455 subfacet_get_key(subfacet, &keybuf, &key);
3456 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
3457 actions, actions_len, stats);
3460 subfacet_reset_dp_stats(subfacet, stats);
3466 /* If 'subfacet' is installed in the datapath, uninstalls it. */
3468 subfacet_uninstall(struct ofproto_dpif *p, struct subfacet *subfacet)
3470 if (subfacet->installed) {
3471 struct odputil_keybuf keybuf;
3472 struct dpif_flow_stats stats;
3476 subfacet_get_key(subfacet, &keybuf, &key);
3477 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3478 subfacet_reset_dp_stats(subfacet, &stats);
3480 subfacet_update_stats(p, subfacet, &stats);
3482 subfacet->installed = false;
3484 assert(subfacet->dp_packet_count == 0);
3485 assert(subfacet->dp_byte_count == 0);
3489 /* Resets 'subfacet''s datapath statistics counters. This should be called
3490 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
3491 * non-null, it should contain the statistics returned by dpif when 'subfacet'
3492 * was reset in the datapath. 'stats' will be modified to include only
3493 * statistics new since 'subfacet' was last updated. */
3495 subfacet_reset_dp_stats(struct subfacet *subfacet,
3496 struct dpif_flow_stats *stats)
3499 && subfacet->dp_packet_count <= stats->n_packets
3500 && subfacet->dp_byte_count <= stats->n_bytes) {
3501 stats->n_packets -= subfacet->dp_packet_count;
3502 stats->n_bytes -= subfacet->dp_byte_count;
3505 subfacet->dp_packet_count = 0;
3506 subfacet->dp_byte_count = 0;
3509 /* Updates 'subfacet''s used time. The caller is responsible for calling
3510 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
3512 subfacet_update_time(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3515 if (used > subfacet->used) {
3516 subfacet->used = used;
3517 facet_update_time(ofproto, subfacet->facet, used);
3521 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
3523 * Because of the meaning of a subfacet's counters, it only makes sense to do
3524 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
3525 * represents a packet that was sent by hand or if it represents statistics
3526 * that have been cleared out of the datapath. */
3528 subfacet_update_stats(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3529 const struct dpif_flow_stats *stats)
3531 if (stats->n_packets || stats->used > subfacet->used) {
3532 struct facet *facet = subfacet->facet;
3534 subfacet_update_time(ofproto, subfacet, stats->used);
3535 facet->packet_count += stats->n_packets;
3536 facet->byte_count += stats->n_bytes;
3537 facet_push_stats(facet);
3538 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3544 static struct rule_dpif *
3545 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3548 struct cls_rule *cls_rule;
3549 struct classifier *cls;
3551 if (table_id >= N_TABLES) {
3555 cls = &ofproto->up.tables[table_id];
3556 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3557 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3558 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3559 * are unavailable. */
3560 struct flow ofpc_normal_flow = *flow;
3561 ofpc_normal_flow.tp_src = htons(0);
3562 ofpc_normal_flow.tp_dst = htons(0);
3563 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3565 cls_rule = classifier_lookup(cls, flow);
3567 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3571 complete_operation(struct rule_dpif *rule)
3573 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3575 rule_invalidate(rule);
3577 struct dpif_completion *c = xmalloc(sizeof *c);
3578 c->op = rule->up.pending;
3579 list_push_back(&ofproto->completions, &c->list_node);
3581 ofoperation_complete(rule->up.pending, 0);
3585 static struct rule *
3588 struct rule_dpif *rule = xmalloc(sizeof *rule);
3593 rule_dealloc(struct rule *rule_)
3595 struct rule_dpif *rule = rule_dpif_cast(rule_);
3600 rule_construct(struct rule *rule_)
3602 struct rule_dpif *rule = rule_dpif_cast(rule_);
3603 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3604 struct rule_dpif *victim;
3608 error = validate_actions(rule->up.actions, rule->up.n_actions,
3609 &rule->up.cr.flow, ofproto->max_ports);
3614 rule->used = rule->up.created;
3615 rule->packet_count = 0;
3616 rule->byte_count = 0;
3618 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3619 if (victim && !list_is_empty(&victim->facets)) {
3620 struct facet *facet;
3622 rule->facets = victim->facets;
3623 list_moved(&rule->facets);
3624 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3625 /* XXX: We're only clearing our local counters here. It's possible
3626 * that quite a few packets are unaccounted for in the datapath
3627 * statistics. These will be accounted to the new rule instead of
3628 * cleared as required. This could be fixed by clearing out the
3629 * datapath statistics for this facet, but currently it doesn't
3631 facet_reset_counters(facet);
3635 /* Must avoid list_moved() in this case. */
3636 list_init(&rule->facets);
3639 table_id = rule->up.table_id;
3640 rule->tag = (victim ? victim->tag
3642 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3643 ofproto->tables[table_id].basis));
3645 complete_operation(rule);
3650 rule_destruct(struct rule *rule_)
3652 struct rule_dpif *rule = rule_dpif_cast(rule_);
3653 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3654 struct facet *facet, *next_facet;
3656 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3657 facet_revalidate(ofproto, facet);
3660 complete_operation(rule);
3664 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3666 struct rule_dpif *rule = rule_dpif_cast(rule_);
3667 struct facet *facet;
3669 /* Start from historical data for 'rule' itself that are no longer tracked
3670 * in facets. This counts, for example, facets that have expired. */
3671 *packets = rule->packet_count;
3672 *bytes = rule->byte_count;
3674 /* Add any statistics that are tracked by facets. This includes
3675 * statistical data recently updated by ofproto_update_stats() as well as
3676 * stats for packets that were executed "by hand" via dpif_execute(). */
3677 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3678 *packets += facet->packet_count;
3679 *bytes += facet->byte_count;
3684 rule_execute(struct rule *rule_, const struct flow *flow,
3685 struct ofpbuf *packet)
3687 struct rule_dpif *rule = rule_dpif_cast(rule_);
3688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3689 struct action_xlate_ctx ctx;
3690 struct ofpbuf *odp_actions;
3693 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3694 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3695 size = packet->size;
3696 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3697 odp_actions->size, packet)) {
3698 rule->used = time_msec();
3699 rule->packet_count++;
3700 rule->byte_count += size;
3701 flow_push_stats(rule, flow, 1, size, rule->used);
3703 ofpbuf_delete(odp_actions);
3709 rule_modify_actions(struct rule *rule_)
3711 struct rule_dpif *rule = rule_dpif_cast(rule_);
3712 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3715 error = validate_actions(rule->up.actions, rule->up.n_actions,
3716 &rule->up.cr.flow, ofproto->max_ports);
3718 ofoperation_complete(rule->up.pending, error);
3722 complete_operation(rule);
3725 /* Sends 'packet' out 'ofport'.
3726 * Returns 0 if successful, otherwise a positive errno value. */
3728 send_packet(const struct ofport_dpif *ofport, const struct ofpbuf *packet)
3730 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3731 uint16_t odp_port = ofport->odp_port;
3732 struct ofpbuf key, odp_actions;
3733 struct odputil_keybuf keybuf;
3737 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3738 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3739 odp_flow_key_from_flow(&key, &flow);
3741 ofpbuf_init(&odp_actions, 32);
3742 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3744 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3745 error = dpif_execute(ofproto->dpif,
3747 odp_actions.data, odp_actions.size,
3749 ofpbuf_uninit(&odp_actions);
3752 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3753 ofproto->up.name, odp_port, strerror(error));
3758 /* OpenFlow to datapath action translation. */
3760 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3761 struct action_xlate_ctx *ctx);
3762 static void xlate_normal(struct action_xlate_ctx *);
3765 put_userspace_action(const struct ofproto_dpif *ofproto,
3766 struct ofpbuf *odp_actions,
3767 const struct flow *flow,
3768 const struct user_action_cookie *cookie)
3772 pid = dpif_port_get_pid(ofproto->dpif,
3773 ofp_port_to_odp_port(flow->in_port));
3775 return odp_put_userspace_action(pid, cookie, odp_actions);
3778 /* Compose SAMPLE action for sFlow. */
3780 compose_sflow_action(const struct ofproto_dpif *ofproto,
3781 struct ofpbuf *odp_actions,
3782 const struct flow *flow,
3785 uint32_t port_ifindex;
3786 uint32_t probability;
3787 struct user_action_cookie cookie;
3788 size_t sample_offset, actions_offset;
3789 int cookie_offset, n_output;
3791 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3795 if (odp_port == OVSP_NONE) {
3799 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3803 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3805 /* Number of packets out of UINT_MAX to sample. */
3806 probability = dpif_sflow_get_probability(ofproto->sflow);
3807 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3809 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3811 cookie.type = USER_ACTION_COOKIE_SFLOW;
3812 cookie.data = port_ifindex;
3813 cookie.n_output = n_output;
3814 cookie.vlan_tci = 0;
3815 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3817 nl_msg_end_nested(odp_actions, actions_offset);
3818 nl_msg_end_nested(odp_actions, sample_offset);
3819 return cookie_offset;
3822 /* SAMPLE action must be first action in any given list of actions.
3823 * At this point we do not have all information required to build it. So try to
3824 * build sample action as complete as possible. */
3826 add_sflow_action(struct action_xlate_ctx *ctx)
3828 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3830 &ctx->flow, OVSP_NONE);
3831 ctx->sflow_odp_port = 0;
3832 ctx->sflow_n_outputs = 0;
3835 /* Fix SAMPLE action according to data collected while composing ODP actions.
3836 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3837 * USERSPACE action's user-cookie which is required for sflow. */
3839 fix_sflow_action(struct action_xlate_ctx *ctx)
3841 const struct flow *base = &ctx->base_flow;
3842 struct user_action_cookie *cookie;
3844 if (!ctx->user_cookie_offset) {
3848 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3850 assert(cookie != NULL);
3851 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3853 if (ctx->sflow_n_outputs) {
3854 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3855 ctx->sflow_odp_port);
3857 if (ctx->sflow_n_outputs >= 255) {
3858 cookie->n_output = 255;
3860 cookie->n_output = ctx->sflow_n_outputs;
3862 cookie->vlan_tci = base->vlan_tci;
3866 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3867 const void *key, size_t key_size)
3869 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3870 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3871 nl_msg_end_nested(odp_actions, offset);
3875 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3876 struct ofpbuf *odp_actions)
3878 if (base->tun_id == flow->tun_id) {
3881 base->tun_id = flow->tun_id;
3883 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
3884 &base->tun_id, sizeof(base->tun_id));
3888 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3889 struct ofpbuf *odp_actions)
3891 struct ovs_key_ethernet eth_key;
3893 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3894 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3898 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3899 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3901 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3902 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3904 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3905 ð_key, sizeof(eth_key));
3909 commit_vlan_action(const struct flow *flow, struct flow *base,
3910 struct ofpbuf *odp_actions)
3912 if (base->vlan_tci == flow->vlan_tci) {
3916 if (base->vlan_tci & htons(VLAN_CFI)) {
3917 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3920 if (flow->vlan_tci & htons(VLAN_CFI)) {
3921 struct ovs_action_push_vlan vlan;
3923 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3924 vlan.vlan_tci = flow->vlan_tci;
3925 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3926 &vlan, sizeof vlan);
3928 base->vlan_tci = flow->vlan_tci;
3932 commit_set_nw_action(const struct flow *flow, struct flow *base,
3933 struct ofpbuf *odp_actions)
3935 struct ovs_key_ipv4 ipv4_key;
3937 if (base->dl_type != htons(ETH_TYPE_IP) ||
3938 !base->nw_src || !base->nw_dst) {
3942 if (base->nw_src == flow->nw_src &&
3943 base->nw_dst == flow->nw_dst &&
3944 base->nw_tos == flow->nw_tos &&
3945 base->nw_ttl == flow->nw_ttl &&
3946 base->nw_frag == flow->nw_frag) {
3950 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3951 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3952 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3953 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3954 ipv4_key.ipv4_proto = base->nw_proto;
3955 ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE
3956 : base->nw_frag == FLOW_NW_FRAG_ANY
3957 ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER);
3959 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3960 &ipv4_key, sizeof(ipv4_key));
3964 commit_set_port_action(const struct flow *flow, struct flow *base,
3965 struct ofpbuf *odp_actions)
3967 if (!base->tp_src || !base->tp_dst) {
3971 if (base->tp_src == flow->tp_src &&
3972 base->tp_dst == flow->tp_dst) {
3976 if (flow->nw_proto == IPPROTO_TCP) {
3977 struct ovs_key_tcp port_key;
3979 port_key.tcp_src = base->tp_src = flow->tp_src;
3980 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3982 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3983 &port_key, sizeof(port_key));
3985 } else if (flow->nw_proto == IPPROTO_UDP) {
3986 struct ovs_key_udp port_key;
3988 port_key.udp_src = base->tp_src = flow->tp_src;
3989 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3991 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3992 &port_key, sizeof(port_key));
3997 commit_set_priority_action(const struct flow *flow, struct flow *base,
3998 struct ofpbuf *odp_actions)
4000 if (base->priority == flow->priority) {
4003 base->priority = flow->priority;
4005 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
4006 &base->priority, sizeof(base->priority));
4010 commit_odp_actions(struct action_xlate_ctx *ctx)
4012 const struct flow *flow = &ctx->flow;
4013 struct flow *base = &ctx->base_flow;
4014 struct ofpbuf *odp_actions = ctx->odp_actions;
4016 commit_set_tun_id_action(flow, base, odp_actions);
4017 commit_set_ether_addr_action(flow, base, odp_actions);
4018 commit_vlan_action(flow, base, odp_actions);
4019 commit_set_nw_action(flow, base, odp_actions);
4020 commit_set_port_action(flow, base, odp_actions);
4021 commit_set_priority_action(flow, base, odp_actions);
4025 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4028 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
4029 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
4030 uint8_t flow_nw_tos = ctx->flow.nw_tos;
4033 struct priority_to_dscp *pdscp;
4035 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
4036 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4040 pdscp = get_priority(ofport, ctx->flow.priority);
4042 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4043 ctx->flow.nw_tos |= pdscp->dscp;
4046 /* We may not have an ofport record for this port, but it doesn't hurt
4047 * to allow forwarding to it anyhow. Maybe such a port will appear
4048 * later and we're pre-populating the flow table. */
4051 commit_odp_actions(ctx);
4052 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
4053 ctx->sflow_odp_port = odp_port;
4054 ctx->sflow_n_outputs++;
4055 ctx->nf_output_iface = ofp_port;
4056 ctx->flow.nw_tos = flow_nw_tos;
4060 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
4062 compose_output_action__(ctx, ofp_port, true);
4066 xlate_table_action(struct action_xlate_ctx *ctx,
4067 uint16_t in_port, uint8_t table_id)
4069 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
4070 struct ofproto_dpif *ofproto = ctx->ofproto;
4071 struct rule_dpif *rule;
4072 uint16_t old_in_port;
4073 uint8_t old_table_id;
4075 old_table_id = ctx->table_id;
4076 ctx->table_id = table_id;
4078 /* Look up a flow with 'in_port' as the input port. */
4079 old_in_port = ctx->flow.in_port;
4080 ctx->flow.in_port = in_port;
4081 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
4084 if (table_id > 0 && table_id < N_TABLES) {
4085 struct table_dpif *table = &ofproto->tables[table_id];
4086 if (table->other_table) {
4089 : rule_calculate_tag(&ctx->flow,
4090 &table->other_table->wc,
4095 /* Restore the original input port. Otherwise OFPP_NORMAL and
4096 * OFPP_IN_PORT will have surprising behavior. */
4097 ctx->flow.in_port = old_in_port;
4099 if (ctx->resubmit_hook) {
4100 ctx->resubmit_hook(ctx, rule);
4105 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4109 ctx->table_id = old_table_id;
4111 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4113 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
4114 MAX_RESUBMIT_RECURSION);
4119 xlate_resubmit_table(struct action_xlate_ctx *ctx,
4120 const struct nx_action_resubmit *nar)
4125 in_port = (nar->in_port == htons(OFPP_IN_PORT)
4127 : ntohs(nar->in_port));
4128 table_id = nar->table == 255 ? ctx->table_id : nar->table;
4130 xlate_table_action(ctx, in_port, table_id);
4134 flood_packets(struct action_xlate_ctx *ctx, bool all)
4136 struct ofport_dpif *ofport;
4138 commit_odp_actions(ctx);
4139 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
4140 uint16_t ofp_port = ofport->up.ofp_port;
4142 if (ofp_port == ctx->flow.in_port) {
4147 compose_output_action__(ctx, ofp_port, false);
4148 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
4149 compose_output_action(ctx, ofp_port);
4153 ctx->nf_output_iface = NF_OUT_FLOOD;
4157 compose_controller_action(struct action_xlate_ctx *ctx, int len)
4159 struct user_action_cookie cookie;
4161 commit_odp_actions(ctx);
4162 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4164 cookie.n_output = 0;
4165 cookie.vlan_tci = 0;
4166 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
4170 xlate_output_action__(struct action_xlate_ctx *ctx,
4171 uint16_t port, uint16_t max_len)
4173 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4175 ctx->nf_output_iface = NF_OUT_DROP;
4179 compose_output_action(ctx, ctx->flow.in_port);
4182 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
4188 flood_packets(ctx, false);
4191 flood_packets(ctx, true);
4193 case OFPP_CONTROLLER:
4194 compose_controller_action(ctx, max_len);
4197 compose_output_action(ctx, OFPP_LOCAL);
4202 if (port != ctx->flow.in_port) {
4203 compose_output_action(ctx, port);
4208 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4209 ctx->nf_output_iface = NF_OUT_FLOOD;
4210 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4211 ctx->nf_output_iface = prev_nf_output_iface;
4212 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4213 ctx->nf_output_iface != NF_OUT_FLOOD) {
4214 ctx->nf_output_iface = NF_OUT_MULTI;
4219 xlate_output_reg_action(struct action_xlate_ctx *ctx,
4220 const struct nx_action_output_reg *naor)
4224 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4226 if (ofp_port <= UINT16_MAX) {
4227 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4232 xlate_output_action(struct action_xlate_ctx *ctx,
4233 const struct ofp_action_output *oao)
4235 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4239 xlate_enqueue_action(struct action_xlate_ctx *ctx,
4240 const struct ofp_action_enqueue *oae)
4243 uint32_t flow_priority, priority;
4246 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4249 /* Fall back to ordinary output action. */
4250 xlate_output_action__(ctx, ntohs(oae->port), 0);
4254 /* Figure out datapath output port. */
4255 ofp_port = ntohs(oae->port);
4256 if (ofp_port == OFPP_IN_PORT) {
4257 ofp_port = ctx->flow.in_port;
4258 } else if (ofp_port == ctx->flow.in_port) {
4262 /* Add datapath actions. */
4263 flow_priority = ctx->flow.priority;
4264 ctx->flow.priority = priority;
4265 compose_output_action(ctx, ofp_port);
4266 ctx->flow.priority = flow_priority;
4268 /* Update NetFlow output port. */
4269 if (ctx->nf_output_iface == NF_OUT_DROP) {
4270 ctx->nf_output_iface = ofp_port;
4271 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4272 ctx->nf_output_iface = NF_OUT_MULTI;
4277 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4278 const struct nx_action_set_queue *nasq)
4283 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4286 /* Couldn't translate queue to a priority, so ignore. A warning
4287 * has already been logged. */
4291 ctx->flow.priority = priority;
4294 struct xlate_reg_state {
4300 xlate_autopath(struct action_xlate_ctx *ctx,
4301 const struct nx_action_autopath *naa)
4303 uint16_t ofp_port = ntohl(naa->id);
4304 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4306 if (!port || !port->bundle) {
4307 ofp_port = OFPP_NONE;
4308 } else if (port->bundle->bond) {
4309 /* Autopath does not support VLAN hashing. */
4310 struct ofport_dpif *slave = bond_choose_output_slave(
4311 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4313 ofp_port = slave->up.ofp_port;
4316 autopath_execute(naa, &ctx->flow, ofp_port);
4320 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4322 struct ofproto_dpif *ofproto = ofproto_;
4323 struct ofport_dpif *port;
4333 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4336 port = get_ofp_port(ofproto, ofp_port);
4337 return port ? port->may_enable : false;
4342 xlate_learn_action(struct action_xlate_ctx *ctx,
4343 const struct nx_action_learn *learn)
4345 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4346 struct ofputil_flow_mod fm;
4349 learn_execute(learn, &ctx->flow, &fm);
4351 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4352 if (error && !VLOG_DROP_WARN(&rl)) {
4353 char *msg = ofputil_error_to_string(error);
4354 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4362 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4364 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4365 ? htonl(OFPPC_NO_RECV_STP)
4366 : htonl(OFPPC_NO_RECV))) {
4370 /* Only drop packets here if both forwarding and learning are
4371 * disabled. If just learning is enabled, we need to have
4372 * OFPP_NORMAL and the learning action have a look at the packet
4373 * before we can drop it. */
4374 if (!stp_forward_in_state(port->stp_state)
4375 && !stp_learn_in_state(port->stp_state)) {
4383 do_xlate_actions(const union ofp_action *in, size_t n_in,
4384 struct action_xlate_ctx *ctx)
4386 const struct ofport_dpif *port;
4387 const union ofp_action *ia;
4390 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4391 if (port && !may_receive(port, ctx)) {
4392 /* Drop this flow. */
4396 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4397 const struct ofp_action_dl_addr *oada;
4398 const struct nx_action_resubmit *nar;
4399 const struct nx_action_set_tunnel *nast;
4400 const struct nx_action_set_queue *nasq;
4401 const struct nx_action_multipath *nam;
4402 const struct nx_action_autopath *naa;
4403 const struct nx_action_bundle *nab;
4404 const struct nx_action_output_reg *naor;
4405 enum ofputil_action_code code;
4412 code = ofputil_decode_action_unsafe(ia);
4414 case OFPUTIL_OFPAT_OUTPUT:
4415 xlate_output_action(ctx, &ia->output);
4418 case OFPUTIL_OFPAT_SET_VLAN_VID:
4419 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4420 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4423 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4424 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4425 ctx->flow.vlan_tci |= htons(
4426 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4429 case OFPUTIL_OFPAT_STRIP_VLAN:
4430 ctx->flow.vlan_tci = htons(0);
4433 case OFPUTIL_OFPAT_SET_DL_SRC:
4434 oada = ((struct ofp_action_dl_addr *) ia);
4435 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4438 case OFPUTIL_OFPAT_SET_DL_DST:
4439 oada = ((struct ofp_action_dl_addr *) ia);
4440 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4443 case OFPUTIL_OFPAT_SET_NW_SRC:
4444 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4447 case OFPUTIL_OFPAT_SET_NW_DST:
4448 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4451 case OFPUTIL_OFPAT_SET_NW_TOS:
4452 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4453 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4456 case OFPUTIL_OFPAT_SET_TP_SRC:
4457 ctx->flow.tp_src = ia->tp_port.tp_port;
4460 case OFPUTIL_OFPAT_SET_TP_DST:
4461 ctx->flow.tp_dst = ia->tp_port.tp_port;
4464 case OFPUTIL_OFPAT_ENQUEUE:
4465 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4468 case OFPUTIL_NXAST_RESUBMIT:
4469 nar = (const struct nx_action_resubmit *) ia;
4470 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4473 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4474 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4477 case OFPUTIL_NXAST_SET_TUNNEL:
4478 nast = (const struct nx_action_set_tunnel *) ia;
4479 tun_id = htonll(ntohl(nast->tun_id));
4480 ctx->flow.tun_id = tun_id;
4483 case OFPUTIL_NXAST_SET_QUEUE:
4484 nasq = (const struct nx_action_set_queue *) ia;
4485 xlate_set_queue_action(ctx, nasq);
4488 case OFPUTIL_NXAST_POP_QUEUE:
4489 ctx->flow.priority = ctx->original_priority;
4492 case OFPUTIL_NXAST_REG_MOVE:
4493 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4497 case OFPUTIL_NXAST_REG_LOAD:
4498 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4502 case OFPUTIL_NXAST_NOTE:
4503 /* Nothing to do. */
4506 case OFPUTIL_NXAST_SET_TUNNEL64:
4507 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4508 ctx->flow.tun_id = tun_id;
4511 case OFPUTIL_NXAST_MULTIPATH:
4512 nam = (const struct nx_action_multipath *) ia;
4513 multipath_execute(nam, &ctx->flow);
4516 case OFPUTIL_NXAST_AUTOPATH:
4517 naa = (const struct nx_action_autopath *) ia;
4518 xlate_autopath(ctx, naa);
4521 case OFPUTIL_NXAST_BUNDLE:
4522 ctx->ofproto->has_bundle_action = true;
4523 nab = (const struct nx_action_bundle *) ia;
4524 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4529 case OFPUTIL_NXAST_BUNDLE_LOAD:
4530 ctx->ofproto->has_bundle_action = true;
4531 nab = (const struct nx_action_bundle *) ia;
4532 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4536 case OFPUTIL_NXAST_OUTPUT_REG:
4537 naor = (const struct nx_action_output_reg *) ia;
4538 xlate_output_reg_action(ctx, naor);
4541 case OFPUTIL_NXAST_LEARN:
4542 ctx->has_learn = true;
4543 if (ctx->may_learn) {
4544 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4548 case OFPUTIL_NXAST_EXIT:
4554 /* We've let OFPP_NORMAL and the learning action look at the packet,
4555 * so drop it now if forwarding is disabled. */
4556 if (port && !stp_forward_in_state(port->stp_state)) {
4557 ofpbuf_clear(ctx->odp_actions);
4558 add_sflow_action(ctx);
4563 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4564 struct ofproto_dpif *ofproto, const struct flow *flow,
4565 const struct ofpbuf *packet)
4567 ctx->ofproto = ofproto;
4569 ctx->packet = packet;
4570 ctx->may_learn = packet != NULL;
4571 ctx->resubmit_hook = NULL;
4574 static struct ofpbuf *
4575 xlate_actions(struct action_xlate_ctx *ctx,
4576 const union ofp_action *in, size_t n_in)
4578 COVERAGE_INC(ofproto_dpif_xlate);
4580 ctx->odp_actions = ofpbuf_new(512);
4581 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4583 ctx->may_set_up_flow = true;
4584 ctx->has_learn = false;
4585 ctx->has_normal = false;
4586 ctx->nf_output_iface = NF_OUT_DROP;
4588 ctx->original_priority = ctx->flow.priority;
4589 ctx->base_flow = ctx->flow;
4590 ctx->base_flow.tun_id = 0;
4594 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4595 switch (ctx->ofproto->up.frag_handling) {
4596 case OFPC_FRAG_NORMAL:
4597 /* We must pretend that transport ports are unavailable. */
4598 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4599 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4602 case OFPC_FRAG_DROP:
4603 return ctx->odp_actions;
4605 case OFPC_FRAG_REASM:
4608 case OFPC_FRAG_NX_MATCH:
4609 /* Nothing to do. */
4614 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4615 ctx->may_set_up_flow = false;
4616 return ctx->odp_actions;
4618 add_sflow_action(ctx);
4619 do_xlate_actions(in, n_in, ctx);
4621 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4622 ctx->odp_actions->data,
4623 ctx->odp_actions->size)) {
4624 ctx->may_set_up_flow = false;
4626 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4628 compose_output_action(ctx, OFPP_LOCAL);
4631 fix_sflow_action(ctx);
4634 return ctx->odp_actions;
4637 /* OFPP_NORMAL implementation. */
4639 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4641 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4642 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4643 * the bundle on which the packet was received, returns the VLAN to which the
4646 * Both 'vid' and the return value are in the range 0...4095. */
4648 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4650 switch (in_bundle->vlan_mode) {
4651 case PORT_VLAN_ACCESS:
4652 return in_bundle->vlan;
4655 case PORT_VLAN_TRUNK:
4658 case PORT_VLAN_NATIVE_UNTAGGED:
4659 case PORT_VLAN_NATIVE_TAGGED:
4660 return vid ? vid : in_bundle->vlan;
4667 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4668 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4671 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4672 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4675 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4677 switch (in_bundle->vlan_mode) {
4678 case PORT_VLAN_ACCESS:
4681 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4682 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4683 "packet received on port %s configured as VLAN "
4684 "%"PRIu16" access port",
4685 in_bundle->ofproto->up.name, vid,
4686 in_bundle->name, in_bundle->vlan);
4692 case PORT_VLAN_NATIVE_UNTAGGED:
4693 case PORT_VLAN_NATIVE_TAGGED:
4695 /* Port must always carry its native VLAN. */
4699 case PORT_VLAN_TRUNK:
4700 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4702 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4703 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4704 "received on port %s not configured for trunking "
4706 in_bundle->ofproto->up.name, vid,
4707 in_bundle->name, vid);
4719 /* Given 'vlan', the VLAN that a packet belongs to, and
4720 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4721 * that should be included in the 802.1Q header. (If the return value is 0,
4722 * then the 802.1Q header should only be included in the packet if there is a
4725 * Both 'vlan' and the return value are in the range 0...4095. */
4727 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4729 switch (out_bundle->vlan_mode) {
4730 case PORT_VLAN_ACCESS:
4733 case PORT_VLAN_TRUNK:
4734 case PORT_VLAN_NATIVE_TAGGED:
4737 case PORT_VLAN_NATIVE_UNTAGGED:
4738 return vlan == out_bundle->vlan ? 0 : vlan;
4746 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4749 struct ofport_dpif *port;
4751 ovs_be16 tci, old_tci;
4753 vid = output_vlan_to_vid(out_bundle, vlan);
4754 if (!out_bundle->bond) {
4755 port = ofbundle_get_a_port(out_bundle);
4757 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4760 /* No slaves enabled, so drop packet. */
4765 old_tci = ctx->flow.vlan_tci;
4767 if (tci || out_bundle->use_priority_tags) {
4768 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4770 tci |= htons(VLAN_CFI);
4773 ctx->flow.vlan_tci = tci;
4775 compose_output_action(ctx, port->up.ofp_port);
4776 ctx->flow.vlan_tci = old_tci;
4780 mirror_mask_ffs(mirror_mask_t mask)
4782 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4787 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4789 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4790 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4794 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4796 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4799 /* Returns an arbitrary interface within 'bundle'. */
4800 static struct ofport_dpif *
4801 ofbundle_get_a_port(const struct ofbundle *bundle)
4803 return CONTAINER_OF(list_front(&bundle->ports),
4804 struct ofport_dpif, bundle_node);
4807 static mirror_mask_t
4808 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4809 const struct ofbundle *in_bundle,
4810 const struct ofbundle *out_bundle)
4812 mirror_mask_t dst_mirrors = 0;
4814 if (out_bundle == OFBUNDLE_FLOOD) {
4815 struct ofbundle *bundle;
4817 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4818 if (bundle != in_bundle
4819 && ofbundle_includes_vlan(bundle, vlan)
4820 && bundle->floodable
4821 && !bundle->mirror_out) {
4822 output_normal(ctx, bundle, vlan);
4823 dst_mirrors |= bundle->dst_mirrors;
4826 ctx->nf_output_iface = NF_OUT_FLOOD;
4827 } else if (out_bundle) {
4828 output_normal(ctx, out_bundle, vlan);
4829 dst_mirrors = out_bundle->dst_mirrors;
4836 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4838 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4841 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4842 * to a VLAN. In general most packets may be mirrored but we want to drop
4843 * protocols that may confuse switches. */
4845 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4847 /* If you change this function's behavior, please update corresponding
4848 * documentation in vswitch.xml at the same time. */
4849 if (dst[0] != 0x01) {
4850 /* All the currently banned MACs happen to start with 01 currently, so
4851 * this is a quick way to eliminate most of the good ones. */
4853 if (eth_addr_is_reserved(dst)) {
4854 /* Drop STP, IEEE pause frames, and other reserved protocols
4855 * (01-80-c2-00-00-0x). */
4859 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4861 if ((dst[3] & 0xfe) == 0xcc &&
4862 (dst[4] & 0xfe) == 0xcc &&
4863 (dst[5] & 0xfe) == 0xcc) {
4864 /* Drop the following protocols plus others following the same
4867 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4868 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4869 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4873 if (!(dst[3] | dst[4] | dst[5])) {
4874 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4883 output_mirrors(struct action_xlate_ctx *ctx,
4884 uint16_t vlan, const struct ofbundle *in_bundle,
4885 mirror_mask_t dst_mirrors)
4887 struct ofproto_dpif *ofproto = ctx->ofproto;
4888 mirror_mask_t mirrors;
4890 mirrors = in_bundle->src_mirrors | dst_mirrors;
4898 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4900 if (!vlan_is_mirrored(m, vlan)) {
4901 mirrors &= mirrors - 1;
4905 mirrors &= ~m->dup_mirrors;
4907 output_normal(ctx, m->out, vlan);
4908 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)
4909 && vlan != m->out_vlan) {
4910 struct ofbundle *bundle;
4912 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4913 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4914 && !bundle->mirror_out) {
4915 output_normal(ctx, bundle, m->out_vlan);
4922 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4923 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4924 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4926 is_gratuitous_arp(const struct flow *flow)
4928 return (flow->dl_type == htons(ETH_TYPE_ARP)
4929 && eth_addr_is_broadcast(flow->dl_dst)
4930 && (flow->nw_proto == ARP_OP_REPLY
4931 || (flow->nw_proto == ARP_OP_REQUEST
4932 && flow->nw_src == flow->nw_dst)));
4936 update_learning_table(struct ofproto_dpif *ofproto,
4937 const struct flow *flow, int vlan,
4938 struct ofbundle *in_bundle)
4940 struct mac_entry *mac;
4942 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4946 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4947 if (is_gratuitous_arp(flow)) {
4948 /* We don't want to learn from gratuitous ARP packets that are
4949 * reflected back over bond slaves so we lock the learning table. */
4950 if (!in_bundle->bond) {
4951 mac_entry_set_grat_arp_lock(mac);
4952 } else if (mac_entry_is_grat_arp_locked(mac)) {
4957 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4958 /* The log messages here could actually be useful in debugging,
4959 * so keep the rate limit relatively high. */
4960 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4961 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4962 "on port %s in VLAN %d",
4963 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4964 in_bundle->name, vlan);
4966 mac->port.p = in_bundle;
4967 tag_set_add(&ofproto->revalidate_set,
4968 mac_learning_changed(ofproto->ml, mac));
4972 static struct ofport_dpif *
4973 lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
4975 struct ofport_dpif *ofport;
4977 /* Find the port and bundle for the received packet. */
4978 ofport = get_ofp_port(ofproto, in_port);
4979 if (ofport && ofport->bundle) {
4983 /* Odd. A few possible reasons here:
4985 * - We deleted a port but there are still a few packets queued up
4988 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4989 * we don't know about.
4991 * - The ofproto client didn't configure the port as part of a bundle.
4994 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4996 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4997 "port %"PRIu16, ofproto->up.name, in_port);
5002 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
5003 * dropped. Returns true if they may be forwarded, false if they should be
5006 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
5007 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
5009 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
5010 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
5011 * checked by input_vid_is_valid().
5013 * May also add tags to '*tags', although the current implementation only does
5014 * so in one special case.
5017 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
5018 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
5020 struct ofbundle *in_bundle = in_port->bundle;
5022 /* Drop frames for reserved multicast addresses
5023 * only if forward_bpdu option is absent. */
5024 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
5028 if (in_bundle->bond) {
5029 struct mac_entry *mac;
5031 switch (bond_check_admissibility(in_bundle->bond, in_port,
5032 flow->dl_dst, tags)) {
5039 case BV_DROP_IF_MOVED:
5040 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
5041 if (mac && mac->port.p != in_bundle &&
5042 (!is_gratuitous_arp(flow)
5043 || mac_entry_is_grat_arp_locked(mac))) {
5054 xlate_normal(struct action_xlate_ctx *ctx)
5056 mirror_mask_t dst_mirrors = 0;
5057 struct ofport_dpif *in_port;
5058 struct ofbundle *in_bundle;
5059 struct ofbundle *out_bundle;
5060 struct mac_entry *mac;
5064 ctx->has_normal = true;
5066 /* Obtain in_port from ctx->flow.in_port.
5068 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5069 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
5070 ctx->packet != NULL);
5074 in_bundle = in_port->bundle;
5076 /* Drop malformed frames. */
5077 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
5078 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
5079 if (ctx->packet != NULL) {
5080 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5081 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
5082 "VLAN tag received on port %s",
5083 ctx->ofproto->up.name, in_bundle->name);
5088 /* Drop frames on bundles reserved for mirroring. */
5089 if (in_bundle->mirror_out) {
5090 if (ctx->packet != NULL) {
5091 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5092 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5093 "%s, which is reserved exclusively for mirroring",
5094 ctx->ofproto->up.name, in_bundle->name);
5100 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
5101 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5104 vlan = input_vid_to_vlan(in_bundle, vid);
5106 /* Check other admissibility requirements. */
5107 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
5108 output_mirrors(ctx, vlan, in_bundle, 0);
5112 /* Learn source MAC. */
5113 if (ctx->may_learn) {
5114 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
5117 /* Determine output bundle. */
5118 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
5121 out_bundle = mac->port.p;
5122 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
5123 /* If we are revalidating but don't have a learning entry then eject
5124 * the flow. Installing a flow that floods packets opens up a window
5125 * of time where we could learn from a packet reflected on a bond and
5126 * blackhole packets before the learning table is updated to reflect
5127 * the correct port. */
5128 ctx->may_set_up_flow = false;
5131 out_bundle = OFBUNDLE_FLOOD;
5134 /* Don't send packets out their input bundles. */
5135 if (in_bundle != out_bundle) {
5136 dst_mirrors = compose_dsts(ctx, vlan, in_bundle, out_bundle);
5138 output_mirrors(ctx, vlan, in_bundle, dst_mirrors);
5141 /* Optimized flow revalidation.
5143 * It's a difficult problem, in general, to tell which facets need to have
5144 * their actions recalculated whenever the OpenFlow flow table changes. We
5145 * don't try to solve that general problem: for most kinds of OpenFlow flow
5146 * table changes, we recalculate the actions for every facet. This is
5147 * relatively expensive, but it's good enough if the OpenFlow flow table
5148 * doesn't change very often.
5150 * However, we can expect one particular kind of OpenFlow flow table change to
5151 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5152 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5153 * table, we add a special case that applies to flow tables in which every rule
5154 * has the same form (that is, the same wildcards), except that the table is
5155 * also allowed to have a single "catch-all" flow that matches all packets. We
5156 * optimize this case by tagging all of the facets that resubmit into the table
5157 * and invalidating the same tag whenever a flow changes in that table. The
5158 * end result is that we revalidate just the facets that need it (and sometimes
5159 * a few more, but not all of the facets or even all of the facets that
5160 * resubmit to the table modified by MAC learning). */
5162 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5163 * into an OpenFlow table with the given 'basis'. */
5165 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5168 if (flow_wildcards_is_catchall(wc)) {
5171 struct flow tag_flow = *flow;
5172 flow_zero_wildcards(&tag_flow, wc);
5173 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5177 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5178 * taggability of that table.
5180 * This function must be called after *each* change to a flow table. If you
5181 * skip calling it on some changes then the pointer comparisons at the end can
5182 * be invalid if you get unlucky. For example, if a flow removal causes a
5183 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5184 * different wildcards to be created with the same address, then this function
5185 * will incorrectly skip revalidation. */
5187 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5189 struct table_dpif *table = &ofproto->tables[table_id];
5190 const struct classifier *cls = &ofproto->up.tables[table_id];
5191 struct cls_table *catchall, *other;
5192 struct cls_table *t;
5194 catchall = other = NULL;
5196 switch (hmap_count(&cls->tables)) {
5198 /* We could tag this OpenFlow table but it would make the logic a
5199 * little harder and it's a corner case that doesn't seem worth it
5205 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5206 if (cls_table_is_catchall(t)) {
5208 } else if (!other) {
5211 /* Indicate that we can't tag this by setting both tables to
5212 * NULL. (We know that 'catchall' is already NULL.) */
5219 /* Can't tag this table. */
5223 if (table->catchall_table != catchall || table->other_table != other) {
5224 table->catchall_table = catchall;
5225 table->other_table = other;
5226 ofproto->need_revalidate = true;
5230 /* Given 'rule' that has changed in some way (either it is a rule being
5231 * inserted, a rule being deleted, or a rule whose actions are being
5232 * modified), marks facets for revalidation to ensure that packets will be
5233 * forwarded correctly according to the new state of the flow table.
5235 * This function must be called after *each* change to a flow table. See
5236 * the comment on table_update_taggable() for more information. */
5238 rule_invalidate(const struct rule_dpif *rule)
5240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5242 table_update_taggable(ofproto, rule->up.table_id);
5244 if (!ofproto->need_revalidate) {
5245 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5247 if (table->other_table && rule->tag) {
5248 tag_set_add(&ofproto->revalidate_set, rule->tag);
5250 ofproto->need_revalidate = true;
5256 set_frag_handling(struct ofproto *ofproto_,
5257 enum ofp_config_flags frag_handling)
5259 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5261 if (frag_handling != OFPC_FRAG_REASM) {
5262 ofproto->need_revalidate = true;
5270 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5271 const struct flow *flow,
5272 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5274 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5277 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5278 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5281 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5282 ofproto->max_ports);
5284 struct odputil_keybuf keybuf;
5285 struct action_xlate_ctx ctx;
5286 struct ofpbuf *odp_actions;
5289 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5290 odp_flow_key_from_flow(&key, flow);
5292 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5293 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5294 dpif_execute(ofproto->dpif, key.data, key.size,
5295 odp_actions->data, odp_actions->size, packet);
5296 ofpbuf_delete(odp_actions);
5304 set_netflow(struct ofproto *ofproto_,
5305 const struct netflow_options *netflow_options)
5307 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5309 if (netflow_options) {
5310 if (!ofproto->netflow) {
5311 ofproto->netflow = netflow_create();
5313 return netflow_set_options(ofproto->netflow, netflow_options);
5315 netflow_destroy(ofproto->netflow);
5316 ofproto->netflow = NULL;
5322 get_netflow_ids(const struct ofproto *ofproto_,
5323 uint8_t *engine_type, uint8_t *engine_id)
5325 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5327 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5331 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5333 if (!facet_is_controller_flow(facet) &&
5334 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5335 struct subfacet *subfacet;
5336 struct ofexpired expired;
5338 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5339 if (subfacet->installed) {
5340 struct dpif_flow_stats stats;
5342 subfacet_install(ofproto, subfacet, subfacet->actions,
5343 subfacet->actions_len, &stats);
5344 subfacet_update_stats(ofproto, subfacet, &stats);
5348 expired.flow = facet->flow;
5349 expired.packet_count = facet->packet_count;
5350 expired.byte_count = facet->byte_count;
5351 expired.used = facet->used;
5352 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5357 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5359 struct facet *facet;
5361 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5362 send_active_timeout(ofproto, facet);
5366 static struct ofproto_dpif *
5367 ofproto_dpif_lookup(const char *name)
5369 struct ofproto *ofproto = ofproto_lookup(name);
5370 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5371 ? ofproto_dpif_cast(ofproto)
5376 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5377 const char *args, void *aux OVS_UNUSED)
5379 const struct ofproto_dpif *ofproto;
5381 ofproto = ofproto_dpif_lookup(args);
5383 unixctl_command_reply(conn, 501, "no such bridge");
5386 mac_learning_flush(ofproto->ml);
5388 unixctl_command_reply(conn, 200, "table successfully flushed");
5392 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5393 const char *args, void *aux OVS_UNUSED)
5395 struct ds ds = DS_EMPTY_INITIALIZER;
5396 const struct ofproto_dpif *ofproto;
5397 const struct mac_entry *e;
5399 ofproto = ofproto_dpif_lookup(args);
5401 unixctl_command_reply(conn, 501, "no such bridge");
5405 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5406 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5407 struct ofbundle *bundle = e->port.p;
5408 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5409 ofbundle_get_a_port(bundle)->odp_port,
5410 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5412 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5416 struct ofproto_trace {
5417 struct action_xlate_ctx ctx;
5423 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5424 const struct rule_dpif *rule)
5426 ds_put_char_multiple(result, '\t', level);
5428 ds_put_cstr(result, "No match\n");
5432 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5433 table_id, ntohll(rule->up.flow_cookie));
5434 cls_rule_format(&rule->up.cr, result);
5435 ds_put_char(result, '\n');
5437 ds_put_char_multiple(result, '\t', level);
5438 ds_put_cstr(result, "OpenFlow ");
5439 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5440 ds_put_char(result, '\n');
5444 trace_format_flow(struct ds *result, int level, const char *title,
5445 struct ofproto_trace *trace)
5447 ds_put_char_multiple(result, '\t', level);
5448 ds_put_format(result, "%s: ", title);
5449 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5450 ds_put_cstr(result, "unchanged");
5452 flow_format(result, &trace->ctx.flow);
5453 trace->flow = trace->ctx.flow;
5455 ds_put_char(result, '\n');
5459 trace_format_regs(struct ds *result, int level, const char *title,
5460 struct ofproto_trace *trace)
5464 ds_put_char_multiple(result, '\t', level);
5465 ds_put_format(result, "%s:", title);
5466 for (i = 0; i < FLOW_N_REGS; i++) {
5467 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5469 ds_put_char(result, '\n');
5473 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5475 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5476 struct ds *result = trace->result;
5478 ds_put_char(result, '\n');
5479 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5480 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5481 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5485 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5486 void *aux OVS_UNUSED)
5488 char *dpname, *arg1, *arg2, *arg3, *arg4;
5489 char *args = xstrdup(args_);
5490 char *save_ptr = NULL;
5491 struct ofproto_dpif *ofproto;
5492 struct ofpbuf odp_key;
5493 struct ofpbuf *packet;
5494 struct rule_dpif *rule;
5500 ofpbuf_init(&odp_key, 0);
5503 dpname = strtok_r(args, " ", &save_ptr);
5504 arg1 = strtok_r(NULL, " ", &save_ptr);
5505 arg2 = strtok_r(NULL, " ", &save_ptr);
5506 arg3 = strtok_r(NULL, " ", &save_ptr);
5507 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5508 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5509 /* ofproto/trace dpname flow [-generate] */
5512 /* Convert string to datapath key. */
5513 ofpbuf_init(&odp_key, 0);
5514 error = odp_flow_key_from_string(arg1, NULL, &odp_key);
5516 unixctl_command_reply(conn, 501, "Bad flow syntax");
5520 /* Convert odp_key to flow. */
5521 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
5523 unixctl_command_reply(conn, 501, "Invalid flow");
5527 /* Generate a packet, if requested. */
5529 packet = ofpbuf_new(0);
5530 flow_compose(packet, &flow);
5532 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5533 /* ofproto/trace dpname priority tun_id in_port packet */
5538 priority = atoi(arg1);
5539 tun_id = htonll(strtoull(arg2, NULL, 0));
5540 in_port = ofp_port_to_odp_port(atoi(arg3));
5542 packet = ofpbuf_new(strlen(args) / 2);
5543 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5544 arg4 += strspn(arg4, " ");
5545 if (*arg4 != '\0') {
5546 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5549 if (packet->size < ETH_HEADER_LEN) {
5550 unixctl_command_reply(conn, 501,
5551 "Packet data too short for Ethernet");
5555 ds_put_cstr(&result, "Packet: ");
5556 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5557 ds_put_cstr(&result, s);
5560 flow_extract(packet, priority, tun_id, in_port, &flow);
5562 unixctl_command_reply(conn, 501, "Bad command syntax");
5566 ofproto = ofproto_dpif_lookup(dpname);
5568 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5573 ds_put_cstr(&result, "Flow: ");
5574 flow_format(&result, &flow);
5575 ds_put_char(&result, '\n');
5577 rule = rule_dpif_lookup(ofproto, &flow, 0);
5578 trace_format_rule(&result, 0, 0, rule);
5580 struct ofproto_trace trace;
5581 struct ofpbuf *odp_actions;
5583 trace.result = &result;
5585 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5586 trace.ctx.resubmit_hook = trace_resubmit;
5587 odp_actions = xlate_actions(&trace.ctx,
5588 rule->up.actions, rule->up.n_actions);
5590 ds_put_char(&result, '\n');
5591 trace_format_flow(&result, 0, "Final flow", &trace);
5592 ds_put_cstr(&result, "Datapath actions: ");
5593 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5594 ofpbuf_delete(odp_actions);
5596 if (!trace.ctx.may_set_up_flow) {
5598 ds_put_cstr(&result, "\nThis flow is not cachable.");
5600 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5601 "for complete actions, please supply a packet.");
5606 unixctl_command_reply(conn, 200, ds_cstr(&result));
5609 ds_destroy(&result);
5610 ofpbuf_delete(packet);
5611 ofpbuf_uninit(&odp_key);
5616 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5617 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5620 unixctl_command_reply(conn, 200, NULL);
5624 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5625 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5628 unixctl_command_reply(conn, 200, NULL);
5632 ofproto_dpif_unixctl_init(void)
5634 static bool registered;
5640 unixctl_command_register("ofproto/trace",
5641 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5642 ofproto_unixctl_trace, NULL);
5643 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5645 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5647 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5648 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5651 const struct ofproto_class ofproto_dpif_class = {
5678 port_is_lacp_current,
5679 NULL, /* rule_choose_table */
5686 rule_modify_actions,
5694 get_cfm_remote_mpids,
5698 get_stp_port_status,
5704 is_mirror_output_bundle,
5705 forward_bpdu_changed,