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 facet.
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 #define MAX_MIRRORS 32
110 typedef uint32_t mirror_mask_t;
111 #define MIRROR_MASK_C(X) UINT32_C(X)
112 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 size_t idx; /* In ofproto's "mirrors" array. */
116 void *aux; /* Key supplied by ofproto's client. */
117 char *name; /* Identifier for log messages. */
119 /* Selection criteria. */
120 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
121 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
122 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
124 /* Output (mutually exclusive). */
125 struct ofbundle *out; /* Output port or NULL. */
126 int out_vlan; /* Output VLAN or -1. */
129 static void mirror_destroy(struct ofmirror *);
131 /* A group of one or more OpenFlow ports. */
132 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
134 struct ofproto_dpif *ofproto; /* Owning ofproto. */
135 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
136 void *aux; /* Key supplied by ofproto's client. */
137 char *name; /* Identifier for log messages. */
140 struct list ports; /* Contains "struct ofport"s. */
141 enum port_vlan_mode vlan_mode; /* VLAN mode */
142 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
143 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
144 * NULL if all VLANs are trunked. */
145 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
146 struct bond *bond; /* Nonnull iff more than one port. */
149 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
151 /* Port mirroring info. */
152 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
153 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
154 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
157 static void bundle_remove(struct ofport *);
158 static void bundle_update(struct ofbundle *);
159 static void bundle_destroy(struct ofbundle *);
160 static void bundle_del_port(struct ofport_dpif *);
161 static void bundle_run(struct ofbundle *);
162 static void bundle_wait(struct ofbundle *);
164 struct action_xlate_ctx {
165 /* action_xlate_ctx_init() initializes these members. */
168 struct ofproto_dpif *ofproto;
170 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
171 * this flow when actions change header fields. */
174 /* The packet corresponding to 'flow', or a null pointer if we are
175 * revalidating without a packet to refer to. */
176 const struct ofpbuf *packet;
178 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
179 * want to execute them if we are actually processing a packet, or if we
180 * are accounting for packets that the datapath has processed, but not if
181 * we are just revalidating. */
184 /* If nonnull, called just before executing a resubmit action.
186 * This is normally null so the client has to set it manually after
187 * calling action_xlate_ctx_init(). */
188 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
190 /* xlate_actions() initializes and uses these members. The client might want
191 * to look at them after it returns. */
193 struct ofpbuf *odp_actions; /* Datapath actions. */
194 tag_type tags; /* Tags associated with actions. */
195 bool may_set_up_flow; /* True ordinarily; false if the actions must
196 * be reassessed for every packet. */
197 bool has_learn; /* Actions include NXAST_LEARN? */
198 bool has_normal; /* Actions output to OFPP_NORMAL? */
199 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
201 /* xlate_actions() initializes and uses these members, but the client has no
202 * reason to look at them. */
204 int recurse; /* Recursion level, via xlate_table_action. */
205 uint32_t priority; /* Current flow priority. 0 if none. */
206 struct flow base_flow; /* Flow at the last commit. */
207 uint32_t base_priority; /* Priority at the last commit. */
208 uint8_t table_id; /* OpenFlow table ID where flow was found. */
209 uint32_t sflow_n_outputs; /* Number of output ports. */
210 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
211 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
214 static void action_xlate_ctx_init(struct action_xlate_ctx *,
215 struct ofproto_dpif *, const struct flow *,
216 const struct ofpbuf *);
217 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
218 const union ofp_action *in, size_t n_in);
220 /* An exact-match instantiation of an OpenFlow flow. */
222 long long int used; /* Time last used; time created if not used. */
226 * - Do include packets and bytes sent "by hand", e.g. with
229 * - Do include packets and bytes that were obtained from the datapath
230 * when its statistics were reset (e.g. dpif_flow_put() with
231 * DPIF_FP_ZERO_STATS).
233 uint64_t packet_count; /* Number of packets received. */
234 uint64_t byte_count; /* Number of bytes received. */
236 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
237 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
239 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
240 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
241 long long int rs_used; /* Used time pushed to resubmit children. */
243 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
245 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
246 struct list list_node; /* In owning rule's 'facets' list. */
247 struct rule_dpif *rule; /* Owning rule. */
248 struct flow flow; /* Exact-match flow. */
249 bool installed; /* Installed in datapath? */
250 bool may_install; /* True ordinarily; false if actions must
251 * be reassessed for every packet. */
252 bool has_learn; /* Actions include NXAST_LEARN? */
253 bool has_normal; /* Actions output to OFPP_NORMAL? */
254 size_t actions_len; /* Number of bytes in actions[]. */
255 struct nlattr *actions; /* Datapath actions. */
256 tag_type tags; /* Tags. */
257 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
260 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
261 static void facet_remove(struct ofproto_dpif *, struct facet *);
262 static void facet_free(struct facet *);
264 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
265 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
266 const struct flow *);
267 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
269 static bool execute_controller_action(struct ofproto_dpif *,
271 const struct nlattr *odp_actions,
273 struct ofpbuf *packet);
274 static void facet_execute(struct ofproto_dpif *, struct facet *,
275 struct ofpbuf *packet);
277 static int facet_put__(struct ofproto_dpif *, struct facet *,
278 const struct nlattr *actions, size_t actions_len,
279 struct dpif_flow_stats *);
280 static void facet_install(struct ofproto_dpif *, struct facet *,
282 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
283 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
285 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
286 const struct ofpbuf *packet);
287 static void facet_update_time(struct ofproto_dpif *, struct facet *,
289 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
290 const struct dpif_flow_stats *);
291 static void facet_reset_counters(struct facet *);
292 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
293 static void facet_push_stats(struct facet *);
294 static void facet_account(struct ofproto_dpif *, struct facet *);
296 static bool facet_is_controller_flow(struct facet *);
298 static void flow_push_stats(const struct rule_dpif *,
299 struct flow *, uint64_t packets, uint64_t bytes,
302 static uint32_t rule_calculate_tag(const struct flow *,
303 const struct flow_wildcards *,
305 static void rule_invalidate(const struct rule_dpif *);
311 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
312 struct list bundle_node; /* In struct ofbundle's "ports" list. */
313 struct cfm *cfm; /* Connectivity Fault Management, if any. */
314 tag_type tag; /* Tag associated with this port. */
315 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
316 bool may_enable; /* May be enabled in bonds. */
319 static struct ofport_dpif *
320 ofport_dpif_cast(const struct ofport *ofport)
322 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
323 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
326 static void port_run(struct ofport_dpif *);
327 static void port_wait(struct ofport_dpif *);
328 static int set_cfm(struct ofport *, const struct cfm_settings *);
330 struct dpif_completion {
331 struct list list_node;
332 struct ofoperation *op;
335 /* Extra information about a classifier table.
336 * Currently used just for optimized flow revalidation. */
338 /* If either of these is nonnull, then this table has a form that allows
339 * flows to be tagged to avoid revalidating most flows for the most common
340 * kinds of flow table changes. */
341 struct cls_table *catchall_table; /* Table that wildcards all fields. */
342 struct cls_table *other_table; /* Table with any other wildcard set. */
343 uint32_t basis; /* Keeps each table's tags separate. */
346 struct ofproto_dpif {
355 struct netflow *netflow;
356 struct dpif_sflow *sflow;
357 struct hmap bundles; /* Contains "struct ofbundle"s. */
358 struct mac_learning *ml;
359 struct ofmirror *mirrors[MAX_MIRRORS];
360 bool has_bonded_bundles;
363 struct timer next_expiration;
369 struct table_dpif tables[N_TABLES];
370 bool need_revalidate;
371 struct tag_set revalidate_set;
373 /* Support for debugging async flow mods. */
374 struct list completions;
376 bool has_bundle_action; /* True when the first bundle action appears. */
379 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
380 * for debugging the asynchronous flow_mod implementation.) */
383 static void ofproto_dpif_unixctl_init(void);
385 static struct ofproto_dpif *
386 ofproto_dpif_cast(const struct ofproto *ofproto)
388 assert(ofproto->ofproto_class == &ofproto_dpif_class);
389 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
392 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
394 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
397 /* Packet processing. */
398 static void update_learning_table(struct ofproto_dpif *,
399 const struct flow *, int vlan,
401 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
402 bool have_packet, tag_type *, int *vlanp,
403 struct ofbundle **in_bundlep);
406 #define FLOW_MISS_MAX_BATCH 50
407 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
408 static void handle_miss_upcalls(struct ofproto_dpif *,
409 struct dpif_upcall *, size_t n);
411 /* Flow expiration. */
412 static int expire(struct ofproto_dpif *);
415 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
416 const struct ofpbuf *packet);
418 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
419 const struct flow *, uint32_t odp_port);
420 /* Global variables. */
421 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
423 /* Factory functions. */
426 enumerate_types(struct sset *types)
428 dp_enumerate_types(types);
432 enumerate_names(const char *type, struct sset *names)
434 return dp_enumerate_names(type, names);
438 del(const char *type, const char *name)
443 error = dpif_open(name, type, &dpif);
445 error = dpif_delete(dpif);
451 /* Basic life-cycle. */
453 static struct ofproto *
456 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
461 dealloc(struct ofproto *ofproto_)
463 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
468 construct(struct ofproto *ofproto_, int *n_tablesp)
470 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
471 const char *name = ofproto->up.name;
475 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
477 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
481 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
482 ofproto->n_matches = 0;
484 dpif_flow_flush(ofproto->dpif);
485 dpif_recv_purge(ofproto->dpif);
487 error = dpif_recv_set_mask(ofproto->dpif,
488 ((1u << DPIF_UC_MISS) |
489 (1u << DPIF_UC_ACTION)));
491 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
492 dpif_close(ofproto->dpif);
496 ofproto->netflow = NULL;
497 ofproto->sflow = NULL;
498 hmap_init(&ofproto->bundles);
499 ofproto->ml = mac_learning_create();
500 for (i = 0; i < MAX_MIRRORS; i++) {
501 ofproto->mirrors[i] = NULL;
503 ofproto->has_bonded_bundles = false;
505 timer_set_duration(&ofproto->next_expiration, 1000);
507 hmap_init(&ofproto->facets);
509 for (i = 0; i < N_TABLES; i++) {
510 struct table_dpif *table = &ofproto->tables[i];
512 table->catchall_table = NULL;
513 table->other_table = NULL;
514 table->basis = random_uint32();
516 ofproto->need_revalidate = false;
517 tag_set_init(&ofproto->revalidate_set);
519 list_init(&ofproto->completions);
521 ofproto_dpif_unixctl_init();
523 ofproto->has_bundle_action = false;
525 *n_tablesp = N_TABLES;
530 complete_operations(struct ofproto_dpif *ofproto)
532 struct dpif_completion *c, *next;
534 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
535 ofoperation_complete(c->op, 0);
536 list_remove(&c->list_node);
542 destruct(struct ofproto *ofproto_)
544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
545 struct rule_dpif *rule, *next_rule;
546 struct classifier *table;
549 complete_operations(ofproto);
551 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
552 struct cls_cursor cursor;
554 cls_cursor_init(&cursor, table, NULL);
555 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
556 ofproto_rule_destroy(&rule->up);
560 for (i = 0; i < MAX_MIRRORS; i++) {
561 mirror_destroy(ofproto->mirrors[i]);
564 netflow_destroy(ofproto->netflow);
565 dpif_sflow_destroy(ofproto->sflow);
566 hmap_destroy(&ofproto->bundles);
567 mac_learning_destroy(ofproto->ml);
569 hmap_destroy(&ofproto->facets);
571 dpif_close(ofproto->dpif);
575 run(struct ofproto *ofproto_)
577 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
578 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
579 struct ofport_dpif *ofport;
580 struct ofbundle *bundle;
585 complete_operations(ofproto);
587 dpif_run(ofproto->dpif);
590 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
591 struct dpif_upcall *upcall = &misses[n_misses];
594 error = dpif_recv(ofproto->dpif, upcall);
596 if (error == ENODEV && n_misses == 0) {
602 if (upcall->type == DPIF_UC_MISS) {
603 /* Handle it later. */
606 handle_upcall(ofproto, upcall);
610 handle_miss_upcalls(ofproto, misses, n_misses);
612 if (timer_expired(&ofproto->next_expiration)) {
613 int delay = expire(ofproto);
614 timer_set_duration(&ofproto->next_expiration, delay);
617 if (ofproto->netflow) {
618 netflow_run(ofproto->netflow);
620 if (ofproto->sflow) {
621 dpif_sflow_run(ofproto->sflow);
624 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
627 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
631 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
633 /* Now revalidate if there's anything to do. */
634 if (ofproto->need_revalidate
635 || !tag_set_is_empty(&ofproto->revalidate_set)) {
636 struct tag_set revalidate_set = ofproto->revalidate_set;
637 bool revalidate_all = ofproto->need_revalidate;
638 struct facet *facet, *next;
640 /* Clear the revalidation flags. */
641 tag_set_init(&ofproto->revalidate_set);
642 ofproto->need_revalidate = false;
644 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
646 || tag_set_intersects(&revalidate_set, facet->tags)) {
647 facet_revalidate(ofproto, facet);
656 wait(struct ofproto *ofproto_)
658 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
659 struct ofport_dpif *ofport;
660 struct ofbundle *bundle;
662 if (!clogged && !list_is_empty(&ofproto->completions)) {
663 poll_immediate_wake();
666 dpif_wait(ofproto->dpif);
667 dpif_recv_wait(ofproto->dpif);
668 if (ofproto->sflow) {
669 dpif_sflow_wait(ofproto->sflow);
671 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
672 poll_immediate_wake();
674 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
677 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
680 mac_learning_wait(ofproto->ml);
681 if (ofproto->need_revalidate) {
682 /* Shouldn't happen, but if it does just go around again. */
683 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
684 poll_immediate_wake();
686 timer_wait(&ofproto->next_expiration);
691 flush(struct ofproto *ofproto_)
693 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
694 struct facet *facet, *next_facet;
696 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
697 /* Mark the facet as not installed so that facet_remove() doesn't
698 * bother trying to uninstall it. There is no point in uninstalling it
699 * individually since we are about to blow away all the facets with
700 * dpif_flow_flush(). */
701 facet->installed = false;
702 facet->dp_packet_count = 0;
703 facet->dp_byte_count = 0;
704 facet_remove(ofproto, facet);
706 dpif_flow_flush(ofproto->dpif);
710 get_features(struct ofproto *ofproto_ OVS_UNUSED,
711 bool *arp_match_ip, uint32_t *actions)
713 *arp_match_ip = true;
714 *actions = ((1u << OFPAT_OUTPUT) |
715 (1u << OFPAT_SET_VLAN_VID) |
716 (1u << OFPAT_SET_VLAN_PCP) |
717 (1u << OFPAT_STRIP_VLAN) |
718 (1u << OFPAT_SET_DL_SRC) |
719 (1u << OFPAT_SET_DL_DST) |
720 (1u << OFPAT_SET_NW_SRC) |
721 (1u << OFPAT_SET_NW_DST) |
722 (1u << OFPAT_SET_NW_TOS) |
723 (1u << OFPAT_SET_TP_SRC) |
724 (1u << OFPAT_SET_TP_DST) |
725 (1u << OFPAT_ENQUEUE));
729 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
731 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
732 struct dpif_dp_stats s;
734 strcpy(ots->name, "classifier");
736 dpif_get_dp_stats(ofproto->dpif, &s);
737 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
738 put_32aligned_be64(&ots->matched_count,
739 htonll(s.n_hit + ofproto->n_matches));
743 set_netflow(struct ofproto *ofproto_,
744 const struct netflow_options *netflow_options)
746 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
748 if (netflow_options) {
749 if (!ofproto->netflow) {
750 ofproto->netflow = netflow_create();
752 return netflow_set_options(ofproto->netflow, netflow_options);
754 netflow_destroy(ofproto->netflow);
755 ofproto->netflow = NULL;
760 static struct ofport *
763 struct ofport_dpif *port = xmalloc(sizeof *port);
768 port_dealloc(struct ofport *port_)
770 struct ofport_dpif *port = ofport_dpif_cast(port_);
775 port_construct(struct ofport *port_)
777 struct ofport_dpif *port = ofport_dpif_cast(port_);
778 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
780 ofproto->need_revalidate = true;
781 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
784 port->tag = tag_create_random();
785 port->may_enable = true;
787 if (ofproto->sflow) {
788 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
789 netdev_get_name(port->up.netdev));
796 port_destruct(struct ofport *port_)
798 struct ofport_dpif *port = ofport_dpif_cast(port_);
799 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
801 ofproto->need_revalidate = true;
802 bundle_remove(port_);
803 set_cfm(port_, NULL);
804 if (ofproto->sflow) {
805 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
810 port_modified(struct ofport *port_)
812 struct ofport_dpif *port = ofport_dpif_cast(port_);
814 if (port->bundle && port->bundle->bond) {
815 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
820 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
822 struct ofport_dpif *port = ofport_dpif_cast(port_);
823 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
824 ovs_be32 changed = old_config ^ port->up.opp.config;
826 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
827 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
828 ofproto->need_revalidate = true;
830 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
831 bundle_update(port->bundle);
837 set_sflow(struct ofproto *ofproto_,
838 const struct ofproto_sflow_options *sflow_options)
840 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
841 struct dpif_sflow *ds = ofproto->sflow;
845 struct ofport_dpif *ofport;
847 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
848 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
849 dpif_sflow_add_port(ds, ofport->odp_port,
850 netdev_get_name(ofport->up.netdev));
852 ofproto->need_revalidate = true;
854 dpif_sflow_set_options(ds, sflow_options);
857 dpif_sflow_destroy(ds);
858 ofproto->need_revalidate = true;
859 ofproto->sflow = NULL;
866 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
868 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
875 struct ofproto_dpif *ofproto;
877 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
878 ofproto->need_revalidate = true;
879 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
882 if (cfm_configure(ofport->cfm, s)) {
888 cfm_destroy(ofport->cfm);
894 get_cfm_fault(const struct ofport *ofport_)
896 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
898 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
902 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
905 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
908 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
917 /* Expires all MAC learning entries associated with 'port' and forces ofproto
918 * to revalidate every flow. */
920 bundle_flush_macs(struct ofbundle *bundle)
922 struct ofproto_dpif *ofproto = bundle->ofproto;
923 struct mac_learning *ml = ofproto->ml;
924 struct mac_entry *mac, *next_mac;
926 ofproto->need_revalidate = true;
927 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
928 if (mac->port.p == bundle) {
929 mac_learning_expire(ml, mac);
934 static struct ofbundle *
935 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
937 struct ofbundle *bundle;
939 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
941 if (bundle->aux == aux) {
948 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
949 * ones that are found to 'bundles'. */
951 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
952 void **auxes, size_t n_auxes,
953 struct hmapx *bundles)
958 for (i = 0; i < n_auxes; i++) {
959 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
961 hmapx_add(bundles, bundle);
967 bundle_update(struct ofbundle *bundle)
969 struct ofport_dpif *port;
971 bundle->floodable = true;
972 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
973 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
974 bundle->floodable = false;
981 bundle_del_port(struct ofport_dpif *port)
983 struct ofbundle *bundle = port->bundle;
985 bundle->ofproto->need_revalidate = true;
987 list_remove(&port->bundle_node);
991 lacp_slave_unregister(bundle->lacp, port);
994 bond_slave_unregister(bundle->bond, port);
997 bundle_update(bundle);
1001 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1002 struct lacp_slave_settings *lacp,
1003 uint32_t bond_stable_id)
1005 struct ofport_dpif *port;
1007 port = get_ofp_port(bundle->ofproto, ofp_port);
1012 if (port->bundle != bundle) {
1013 bundle->ofproto->need_revalidate = true;
1015 bundle_del_port(port);
1018 port->bundle = bundle;
1019 list_push_back(&bundle->ports, &port->bundle_node);
1020 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1021 bundle->floodable = false;
1025 port->bundle->ofproto->need_revalidate = true;
1026 lacp_slave_register(bundle->lacp, port, lacp);
1029 port->bond_stable_id = bond_stable_id;
1035 bundle_destroy(struct ofbundle *bundle)
1037 struct ofproto_dpif *ofproto;
1038 struct ofport_dpif *port, *next_port;
1045 ofproto = bundle->ofproto;
1046 for (i = 0; i < MAX_MIRRORS; i++) {
1047 struct ofmirror *m = ofproto->mirrors[i];
1049 if (m->out == bundle) {
1051 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1052 || hmapx_find_and_delete(&m->dsts, bundle)) {
1053 ofproto->need_revalidate = true;
1058 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1059 bundle_del_port(port);
1062 bundle_flush_macs(bundle);
1063 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1065 free(bundle->trunks);
1066 lacp_destroy(bundle->lacp);
1067 bond_destroy(bundle->bond);
1072 bundle_set(struct ofproto *ofproto_, void *aux,
1073 const struct ofproto_bundle_settings *s)
1075 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1076 bool need_flush = false;
1077 struct ofport_dpif *port;
1078 struct ofbundle *bundle;
1079 unsigned long *trunks;
1085 bundle_destroy(bundle_lookup(ofproto, aux));
1089 assert(s->n_slaves == 1 || s->bond != NULL);
1090 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1092 bundle = bundle_lookup(ofproto, aux);
1094 bundle = xmalloc(sizeof *bundle);
1096 bundle->ofproto = ofproto;
1097 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1098 hash_pointer(aux, 0));
1100 bundle->name = NULL;
1102 list_init(&bundle->ports);
1103 bundle->vlan_mode = PORT_VLAN_TRUNK;
1105 bundle->trunks = NULL;
1106 bundle->lacp = NULL;
1107 bundle->bond = NULL;
1109 bundle->floodable = true;
1111 bundle->src_mirrors = 0;
1112 bundle->dst_mirrors = 0;
1113 bundle->mirror_out = 0;
1116 if (!bundle->name || strcmp(s->name, bundle->name)) {
1118 bundle->name = xstrdup(s->name);
1123 if (!bundle->lacp) {
1124 ofproto->need_revalidate = true;
1125 bundle->lacp = lacp_create();
1127 lacp_configure(bundle->lacp, s->lacp);
1129 lacp_destroy(bundle->lacp);
1130 bundle->lacp = NULL;
1133 /* Update set of ports. */
1135 for (i = 0; i < s->n_slaves; i++) {
1136 if (!bundle_add_port(bundle, s->slaves[i],
1137 s->lacp ? &s->lacp_slaves[i] : NULL,
1138 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1142 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1143 struct ofport_dpif *next_port;
1145 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1146 for (i = 0; i < s->n_slaves; i++) {
1147 if (s->slaves[i] == port->up.ofp_port) {
1152 bundle_del_port(port);
1156 assert(list_size(&bundle->ports) <= s->n_slaves);
1158 if (list_is_empty(&bundle->ports)) {
1159 bundle_destroy(bundle);
1163 /* Set VLAN tagging mode */
1164 if (s->vlan_mode != bundle->vlan_mode) {
1165 bundle->vlan_mode = s->vlan_mode;
1170 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1171 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1173 if (vlan != bundle->vlan) {
1174 bundle->vlan = vlan;
1178 /* Get trunked VLANs. */
1179 switch (s->vlan_mode) {
1180 case PORT_VLAN_ACCESS:
1184 case PORT_VLAN_TRUNK:
1185 trunks = (unsigned long *) s->trunks;
1188 case PORT_VLAN_NATIVE_UNTAGGED:
1189 case PORT_VLAN_NATIVE_TAGGED:
1190 if (vlan != 0 && (!s->trunks
1191 || !bitmap_is_set(s->trunks, vlan)
1192 || bitmap_is_set(s->trunks, 0))) {
1193 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1195 trunks = bitmap_clone(s->trunks, 4096);
1197 trunks = bitmap_allocate1(4096);
1199 bitmap_set1(trunks, vlan);
1200 bitmap_set0(trunks, 0);
1202 trunks = (unsigned long *) s->trunks;
1209 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1210 free(bundle->trunks);
1211 if (trunks == s->trunks) {
1212 bundle->trunks = vlan_bitmap_clone(trunks);
1214 bundle->trunks = trunks;
1219 if (trunks != s->trunks) {
1224 if (!list_is_short(&bundle->ports)) {
1225 bundle->ofproto->has_bonded_bundles = true;
1227 if (bond_reconfigure(bundle->bond, s->bond)) {
1228 ofproto->need_revalidate = true;
1231 bundle->bond = bond_create(s->bond);
1232 ofproto->need_revalidate = true;
1235 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1236 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1240 bond_destroy(bundle->bond);
1241 bundle->bond = NULL;
1244 /* If we changed something that would affect MAC learning, un-learn
1245 * everything on this port and force flow revalidation. */
1247 bundle_flush_macs(bundle);
1254 bundle_remove(struct ofport *port_)
1256 struct ofport_dpif *port = ofport_dpif_cast(port_);
1257 struct ofbundle *bundle = port->bundle;
1260 bundle_del_port(port);
1261 if (list_is_empty(&bundle->ports)) {
1262 bundle_destroy(bundle);
1263 } else if (list_is_short(&bundle->ports)) {
1264 bond_destroy(bundle->bond);
1265 bundle->bond = NULL;
1271 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1273 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1274 struct ofport_dpif *port = port_;
1275 uint8_t ea[ETH_ADDR_LEN];
1278 error = netdev_get_etheraddr(port->up.netdev, ea);
1280 struct ofpbuf packet;
1283 ofpbuf_init(&packet, 0);
1284 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1286 memcpy(packet_pdu, pdu, pdu_size);
1288 error = netdev_send(port->up.netdev, &packet);
1290 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1291 "(%s)", port->bundle->name,
1292 netdev_get_name(port->up.netdev), strerror(error));
1294 ofpbuf_uninit(&packet);
1296 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1297 "%s (%s)", port->bundle->name,
1298 netdev_get_name(port->up.netdev), strerror(error));
1303 bundle_send_learning_packets(struct ofbundle *bundle)
1305 struct ofproto_dpif *ofproto = bundle->ofproto;
1306 int error, n_packets, n_errors;
1307 struct mac_entry *e;
1309 error = n_packets = n_errors = 0;
1310 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1311 if (e->port.p != bundle) {
1312 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1323 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1324 "packets, last error was: %s",
1325 bundle->name, n_errors, n_packets, strerror(error));
1327 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1328 bundle->name, n_packets);
1333 bundle_run(struct ofbundle *bundle)
1336 lacp_run(bundle->lacp, send_pdu_cb);
1339 struct ofport_dpif *port;
1341 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1342 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1345 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1346 lacp_negotiated(bundle->lacp));
1347 if (bond_should_send_learning_packets(bundle->bond)) {
1348 bundle_send_learning_packets(bundle);
1354 bundle_wait(struct ofbundle *bundle)
1357 lacp_wait(bundle->lacp);
1360 bond_wait(bundle->bond);
1367 mirror_scan(struct ofproto_dpif *ofproto)
1371 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1372 if (!ofproto->mirrors[idx]) {
1379 static struct ofmirror *
1380 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1384 for (i = 0; i < MAX_MIRRORS; i++) {
1385 struct ofmirror *mirror = ofproto->mirrors[i];
1386 if (mirror && mirror->aux == aux) {
1395 mirror_set(struct ofproto *ofproto_, void *aux,
1396 const struct ofproto_mirror_settings *s)
1398 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1399 mirror_mask_t mirror_bit;
1400 struct ofbundle *bundle;
1401 struct ofmirror *mirror;
1402 struct ofbundle *out;
1403 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1404 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1407 mirror = mirror_lookup(ofproto, aux);
1409 mirror_destroy(mirror);
1415 idx = mirror_scan(ofproto);
1417 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1419 ofproto->up.name, MAX_MIRRORS, s->name);
1423 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1424 mirror->ofproto = ofproto;
1427 mirror->out_vlan = -1;
1428 mirror->name = NULL;
1431 if (!mirror->name || strcmp(s->name, mirror->name)) {
1433 mirror->name = xstrdup(s->name);
1436 /* Get the new configuration. */
1437 if (s->out_bundle) {
1438 out = bundle_lookup(ofproto, s->out_bundle);
1440 mirror_destroy(mirror);
1446 out_vlan = s->out_vlan;
1448 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1449 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1451 /* If the configuration has not changed, do nothing. */
1452 if (hmapx_equals(&srcs, &mirror->srcs)
1453 && hmapx_equals(&dsts, &mirror->dsts)
1454 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1455 && mirror->out == out
1456 && mirror->out_vlan == out_vlan)
1458 hmapx_destroy(&srcs);
1459 hmapx_destroy(&dsts);
1463 hmapx_swap(&srcs, &mirror->srcs);
1464 hmapx_destroy(&srcs);
1466 hmapx_swap(&dsts, &mirror->dsts);
1467 hmapx_destroy(&dsts);
1469 free(mirror->vlans);
1470 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1473 mirror->out_vlan = out_vlan;
1475 /* Update bundles. */
1476 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1477 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1478 if (hmapx_contains(&mirror->srcs, bundle)) {
1479 bundle->src_mirrors |= mirror_bit;
1481 bundle->src_mirrors &= ~mirror_bit;
1484 if (hmapx_contains(&mirror->dsts, bundle)) {
1485 bundle->dst_mirrors |= mirror_bit;
1487 bundle->dst_mirrors &= ~mirror_bit;
1490 if (mirror->out == bundle) {
1491 bundle->mirror_out |= mirror_bit;
1493 bundle->mirror_out &= ~mirror_bit;
1497 ofproto->need_revalidate = true;
1498 mac_learning_flush(ofproto->ml);
1504 mirror_destroy(struct ofmirror *mirror)
1506 struct ofproto_dpif *ofproto;
1507 mirror_mask_t mirror_bit;
1508 struct ofbundle *bundle;
1514 ofproto = mirror->ofproto;
1515 ofproto->need_revalidate = true;
1516 mac_learning_flush(ofproto->ml);
1518 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1519 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1520 bundle->src_mirrors &= ~mirror_bit;
1521 bundle->dst_mirrors &= ~mirror_bit;
1522 bundle->mirror_out &= ~mirror_bit;
1525 hmapx_destroy(&mirror->srcs);
1526 hmapx_destroy(&mirror->dsts);
1527 free(mirror->vlans);
1529 ofproto->mirrors[mirror->idx] = NULL;
1535 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1537 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1538 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1539 ofproto->need_revalidate = true;
1540 mac_learning_flush(ofproto->ml);
1546 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1548 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1549 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1550 return bundle && bundle->mirror_out != 0;
1554 forward_bpdu_changed(struct ofproto *ofproto_)
1556 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1557 /* Revalidate cached flows whenever forward_bpdu option changes. */
1558 ofproto->need_revalidate = true;
1563 static struct ofport_dpif *
1564 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1566 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1567 return ofport ? ofport_dpif_cast(ofport) : NULL;
1570 static struct ofport_dpif *
1571 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1573 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1577 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1578 struct dpif_port *dpif_port)
1580 ofproto_port->name = dpif_port->name;
1581 ofproto_port->type = dpif_port->type;
1582 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1586 port_run(struct ofport_dpif *ofport)
1588 bool enable = netdev_get_carrier(ofport->up.netdev);
1591 cfm_run(ofport->cfm);
1593 if (cfm_should_send_ccm(ofport->cfm)) {
1594 struct ofpbuf packet;
1596 ofpbuf_init(&packet, 0);
1597 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1598 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1599 ofport->odp_port, &packet);
1600 ofpbuf_uninit(&packet);
1603 enable = enable && !cfm_get_fault(ofport->cfm)
1604 && cfm_get_opup(ofport->cfm);
1607 if (ofport->bundle) {
1608 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1611 if (ofport->may_enable != enable) {
1612 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1614 if (ofproto->has_bundle_action) {
1615 ofproto->need_revalidate = true;
1619 ofport->may_enable = enable;
1623 port_wait(struct ofport_dpif *ofport)
1626 cfm_wait(ofport->cfm);
1631 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1632 struct ofproto_port *ofproto_port)
1634 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1635 struct dpif_port dpif_port;
1638 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1640 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1646 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1648 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1652 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1654 *ofp_portp = odp_port_to_ofp_port(odp_port);
1660 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1662 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1665 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1667 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1669 /* The caller is going to close ofport->up.netdev. If this is a
1670 * bonded port, then the bond is using that netdev, so remove it
1671 * from the bond. The client will need to reconfigure everything
1672 * after deleting ports, so then the slave will get re-added. */
1673 bundle_remove(&ofport->up);
1679 struct port_dump_state {
1680 struct dpif_port_dump dump;
1685 port_dump_start(const struct ofproto *ofproto_, void **statep)
1687 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1688 struct port_dump_state *state;
1690 *statep = state = xmalloc(sizeof *state);
1691 dpif_port_dump_start(&state->dump, ofproto->dpif);
1692 state->done = false;
1697 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1698 struct ofproto_port *port)
1700 struct port_dump_state *state = state_;
1701 struct dpif_port dpif_port;
1703 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1704 ofproto_port_from_dpif_port(port, &dpif_port);
1707 int error = dpif_port_dump_done(&state->dump);
1709 return error ? error : EOF;
1714 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1716 struct port_dump_state *state = state_;
1719 dpif_port_dump_done(&state->dump);
1726 port_poll(const struct ofproto *ofproto_, char **devnamep)
1728 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1729 return dpif_port_poll(ofproto->dpif, devnamep);
1733 port_poll_wait(const struct ofproto *ofproto_)
1735 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1736 dpif_port_poll_wait(ofproto->dpif);
1740 port_is_lacp_current(const struct ofport *ofport_)
1742 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1743 return (ofport->bundle && ofport->bundle->lacp
1744 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1748 /* Upcall handling. */
1750 /* Flow miss batching.
1752 * Some dpifs implement operations faster when you hand them off in a batch.
1753 * To allow batching, "struct flow_miss" queues the dpif-related work needed
1754 * for a given flow. Each "struct flow_miss" corresponds to sending one or
1755 * more packets, plus possibly installing the flow in the dpif.
1757 * So far we only batch the operations that affect flow setup time the most.
1758 * It's possible to batch more than that, but the benefit might be minimal. */
1760 struct hmap_node hmap_node;
1762 const struct nlattr *key;
1764 struct list packets;
1767 struct flow_miss_op {
1768 union dpif_op dpif_op;
1769 struct facet *facet;
1772 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
1773 * OpenFlow controller as necessary according to their individual
1776 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
1777 * ownership is transferred to this function. */
1779 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
1780 const struct flow *flow, bool clone)
1782 struct ofputil_packet_in pin;
1784 pin.packet = packet;
1785 pin.in_port = flow->in_port;
1786 pin.reason = OFPR_NO_MATCH;
1787 pin.buffer_id = 0; /* not yet known */
1788 pin.send_len = 0; /* not used for flow table misses */
1789 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1790 clone ? NULL : packet);
1793 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
1794 * OpenFlow controller as necessary according to their individual
1797 * 'send_len' should be the number of bytes of 'packet' to send to the
1798 * controller, as specified in the action that caused the packet to be sent.
1800 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1801 * Otherwise, ownership is transferred to this function. */
1803 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
1804 uint64_t userdata, const struct flow *flow, bool clone)
1806 struct ofputil_packet_in pin;
1807 struct user_action_cookie cookie;
1809 memcpy(&cookie, &userdata, sizeof(cookie));
1811 pin.packet = packet;
1812 pin.in_port = flow->in_port;
1813 pin.reason = OFPR_ACTION;
1814 pin.buffer_id = 0; /* not yet known */
1815 pin.send_len = cookie.data;
1816 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1817 clone ? NULL : packet);
1821 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1822 const struct ofpbuf *packet)
1824 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1830 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
1832 cfm_process_heartbeat(ofport->cfm, packet);
1835 } else if (ofport->bundle && ofport->bundle->lacp
1836 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1838 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
1845 static struct flow_miss *
1846 flow_miss_create(struct hmap *todo, const struct flow *flow,
1847 const struct nlattr *key, size_t key_len)
1849 uint32_t hash = flow_hash(flow, 0);
1850 struct flow_miss *miss;
1852 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
1853 if (flow_equal(&miss->flow, flow)) {
1858 miss = xmalloc(sizeof *miss);
1859 hmap_insert(todo, &miss->hmap_node, hash);
1862 miss->key_len = key_len;
1863 list_init(&miss->packets);
1868 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
1869 struct flow_miss_op *ops, size_t *n_ops)
1871 const struct flow *flow = &miss->flow;
1872 struct ofpbuf *packet, *next_packet;
1873 struct facet *facet;
1875 facet = facet_lookup_valid(ofproto, flow);
1877 struct rule_dpif *rule;
1879 rule = rule_dpif_lookup(ofproto, flow, 0);
1881 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1882 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1884 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1885 COVERAGE_INC(ofproto_dpif_no_packet_in);
1886 /* XXX install 'drop' flow entry */
1890 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1894 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
1896 list_remove(&packet->list_node);
1897 send_packet_in_miss(ofproto, packet, flow, false);
1903 facet = facet_create(rule, flow);
1906 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
1907 list_remove(&packet->list_node);
1908 ofproto->n_matches++;
1910 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1912 * Extra-special case for fail-open mode.
1914 * We are in fail-open mode and the packet matched the fail-open
1915 * rule, but we are connected to a controller too. We should send
1916 * the packet up to the controller in the hope that it will try to
1917 * set up a flow and thereby allow us to exit fail-open.
1919 * See the top-level comment in fail-open.c for more information.
1921 send_packet_in_miss(ofproto, packet, flow, true);
1924 if (!facet->may_install) {
1925 facet_make_actions(ofproto, facet, packet);
1927 if (!execute_controller_action(ofproto, &facet->flow,
1928 facet->actions, facet->actions_len,
1930 struct flow_miss_op *op = &ops[(*n_ops)++];
1931 struct dpif_execute *execute = &op->dpif_op.execute;
1934 execute->type = DPIF_OP_EXECUTE;
1935 execute->key = miss->key;
1936 execute->key_len = miss->key_len;
1938 = (facet->may_install
1940 : xmemdup(facet->actions, facet->actions_len));
1941 execute->actions_len = facet->actions_len;
1942 execute->packet = packet;
1946 if (facet->may_install) {
1947 struct flow_miss_op *op = &ops[(*n_ops)++];
1948 struct dpif_flow_put *put = &op->dpif_op.flow_put;
1951 put->type = DPIF_OP_FLOW_PUT;
1952 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1953 put->key = miss->key;
1954 put->key_len = miss->key_len;
1955 put->actions = facet->actions;
1956 put->actions_len = facet->actions_len;
1962 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
1965 struct dpif_upcall *upcall;
1966 struct flow_miss *miss, *next_miss;
1967 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
1968 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
1977 /* Construct the to-do list.
1979 * This just amounts to extracting the flow from each packet and sticking
1980 * the packets that have the same flow in the same "flow_miss" structure so
1981 * that we can process them together. */
1983 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
1984 struct flow_miss *miss;
1987 /* Obtain in_port and tun_id, at least, then set 'flow''s header
1989 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1990 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1992 /* Handle 802.1ag and LACP specially. */
1993 if (process_special(ofproto, &flow, upcall->packet)) {
1994 ofpbuf_delete(upcall->packet);
1995 ofproto->n_matches++;
1999 /* Add other packets to a to-do list. */
2000 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2001 list_push_back(&miss->packets, &upcall->packet->list_node);
2004 /* Process each element in the to-do list, constructing the set of
2005 * operations to batch. */
2007 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2008 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2009 ofpbuf_list_delete(&miss->packets);
2010 hmap_remove(&todo, &miss->hmap_node);
2013 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2014 hmap_destroy(&todo);
2016 /* Execute batch. */
2017 for (i = 0; i < n_ops; i++) {
2018 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2020 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2022 /* Free memory and update facets. */
2023 for (i = 0; i < n_ops; i++) {
2024 struct flow_miss_op *op = &flow_miss_ops[i];
2025 struct dpif_execute *execute;
2026 struct dpif_flow_put *put;
2028 switch (op->dpif_op.type) {
2029 case DPIF_OP_EXECUTE:
2030 execute = &op->dpif_op.execute;
2031 if (op->facet->actions != execute->actions) {
2032 free((struct nlattr *) execute->actions);
2034 ofpbuf_delete((struct ofpbuf *) execute->packet);
2037 case DPIF_OP_FLOW_PUT:
2038 put = &op->dpif_op.flow_put;
2040 op->facet->installed = true;
2048 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2049 struct dpif_upcall *upcall)
2052 struct user_action_cookie cookie;
2054 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2056 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2057 if (ofproto->sflow) {
2058 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2059 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2061 ofpbuf_delete(upcall->packet);
2063 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2064 COVERAGE_INC(ofproto_dpif_ctlr_action);
2065 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2066 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2069 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2074 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2076 switch (upcall->type) {
2077 case DPIF_UC_ACTION:
2078 handle_userspace_upcall(ofproto, upcall);
2082 /* The caller handles these. */
2085 case DPIF_N_UC_TYPES:
2087 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2092 /* Flow expiration. */
2094 static int facet_max_idle(const struct ofproto_dpif *);
2095 static void update_stats(struct ofproto_dpif *);
2096 static void rule_expire(struct rule_dpif *);
2097 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2099 /* This function is called periodically by run(). Its job is to collect
2100 * updates for the flows that have been installed into the datapath, most
2101 * importantly when they last were used, and then use that information to
2102 * expire flows that have not been used recently.
2104 * Returns the number of milliseconds after which it should be called again. */
2106 expire(struct ofproto_dpif *ofproto)
2108 struct rule_dpif *rule, *next_rule;
2109 struct classifier *table;
2112 /* Update stats for each flow in the datapath. */
2113 update_stats(ofproto);
2115 /* Expire facets that have been idle too long. */
2116 dp_max_idle = facet_max_idle(ofproto);
2117 expire_facets(ofproto, dp_max_idle);
2119 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2120 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2121 struct cls_cursor cursor;
2123 cls_cursor_init(&cursor, table, NULL);
2124 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2129 /* All outstanding data in existing flows has been accounted, so it's a
2130 * good time to do bond rebalancing. */
2131 if (ofproto->has_bonded_bundles) {
2132 struct ofbundle *bundle;
2134 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2136 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2141 return MIN(dp_max_idle, 1000);
2144 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2146 * This function also pushes statistics updates to rules which each facet
2147 * resubmits into. Generally these statistics will be accurate. However, if a
2148 * facet changes the rule it resubmits into at some time in between
2149 * update_stats() runs, it is possible that statistics accrued to the
2150 * old rule will be incorrectly attributed to the new rule. This could be
2151 * avoided by calling update_stats() whenever rules are created or
2152 * deleted. However, the performance impact of making so many calls to the
2153 * datapath do not justify the benefit of having perfectly accurate statistics.
2156 update_stats(struct ofproto_dpif *p)
2158 const struct dpif_flow_stats *stats;
2159 struct dpif_flow_dump dump;
2160 const struct nlattr *key;
2163 dpif_flow_dump_start(&dump, p->dpif);
2164 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2165 struct facet *facet;
2168 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2172 odp_flow_key_format(key, key_len, &s);
2173 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2179 facet = facet_find(p, &flow);
2181 if (facet && facet->installed) {
2183 if (stats->n_packets >= facet->dp_packet_count) {
2184 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2185 facet->packet_count += extra;
2187 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2190 if (stats->n_bytes >= facet->dp_byte_count) {
2191 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2193 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2196 facet->dp_packet_count = stats->n_packets;
2197 facet->dp_byte_count = stats->n_bytes;
2199 facet_update_time(p, facet, stats->used);
2200 facet_account(p, facet);
2201 facet_push_stats(facet);
2203 /* There's a flow in the datapath that we know nothing about.
2205 COVERAGE_INC(facet_unexpected);
2206 dpif_flow_del(p->dpif, key, key_len, NULL);
2209 dpif_flow_dump_done(&dump);
2212 /* Calculates and returns the number of milliseconds of idle time after which
2213 * facets should expire from the datapath and we should fold their statistics
2214 * into their parent rules in userspace. */
2216 facet_max_idle(const struct ofproto_dpif *ofproto)
2219 * Idle time histogram.
2221 * Most of the time a switch has a relatively small number of facets. When
2222 * this is the case we might as well keep statistics for all of them in
2223 * userspace and to cache them in the kernel datapath for performance as
2226 * As the number of facets increases, the memory required to maintain
2227 * statistics about them in userspace and in the kernel becomes
2228 * significant. However, with a large number of facets it is likely that
2229 * only a few of them are "heavy hitters" that consume a large amount of
2230 * bandwidth. At this point, only heavy hitters are worth caching in the
2231 * kernel and maintaining in userspaces; other facets we can discard.
2233 * The technique used to compute the idle time is to build a histogram with
2234 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2235 * that is installed in the kernel gets dropped in the appropriate bucket.
2236 * After the histogram has been built, we compute the cutoff so that only
2237 * the most-recently-used 1% of facets (but at least
2238 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2239 * the most-recently-used bucket of facets is kept, so actually an
2240 * arbitrary number of facets can be kept in any given expiration run
2241 * (though the next run will delete most of those unless they receive
2244 * This requires a second pass through the facets, in addition to the pass
2245 * made by update_stats(), because the former function never looks
2246 * at uninstallable facets.
2248 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2249 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2250 int buckets[N_BUCKETS] = { 0 };
2251 int total, subtotal, bucket;
2252 struct facet *facet;
2256 total = hmap_count(&ofproto->facets);
2257 if (total <= ofproto->up.flow_eviction_threshold) {
2258 return N_BUCKETS * BUCKET_WIDTH;
2261 /* Build histogram. */
2263 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2264 long long int idle = now - facet->used;
2265 int bucket = (idle <= 0 ? 0
2266 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2267 : (unsigned int) idle / BUCKET_WIDTH);
2271 /* Find the first bucket whose flows should be expired. */
2272 subtotal = bucket = 0;
2274 subtotal += buckets[bucket++];
2275 } while (bucket < N_BUCKETS &&
2276 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2278 if (VLOG_IS_DBG_ENABLED()) {
2282 ds_put_cstr(&s, "keep");
2283 for (i = 0; i < N_BUCKETS; i++) {
2285 ds_put_cstr(&s, ", drop");
2288 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2291 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2295 return bucket * BUCKET_WIDTH;
2299 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2301 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2302 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2303 struct ofexpired expired;
2305 if (facet->installed) {
2306 struct dpif_flow_stats stats;
2308 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2310 facet_update_stats(ofproto, facet, &stats);
2313 expired.flow = facet->flow;
2314 expired.packet_count = facet->packet_count;
2315 expired.byte_count = facet->byte_count;
2316 expired.used = facet->used;
2317 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2322 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2324 long long int cutoff = time_msec() - dp_max_idle;
2325 struct facet *facet, *next_facet;
2327 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2328 facet_active_timeout(ofproto, facet);
2329 if (facet->used < cutoff) {
2330 facet_remove(ofproto, facet);
2335 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2336 * then delete it entirely. */
2338 rule_expire(struct rule_dpif *rule)
2340 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2341 struct facet *facet, *next_facet;
2345 /* Has 'rule' expired? */
2347 if (rule->up.hard_timeout
2348 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2349 reason = OFPRR_HARD_TIMEOUT;
2350 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2351 && now > rule->used + rule->up.idle_timeout * 1000) {
2352 reason = OFPRR_IDLE_TIMEOUT;
2357 COVERAGE_INC(ofproto_dpif_expired);
2359 /* Update stats. (This is a no-op if the rule expired due to an idle
2360 * timeout, because that only happens when the rule has no facets left.) */
2361 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2362 facet_remove(ofproto, facet);
2365 /* Get rid of the rule. */
2366 ofproto_rule_expire(&rule->up, reason);
2371 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2373 * The caller must already have determined that no facet with an identical
2374 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2375 * the ofproto's classifier table.
2377 * The facet will initially have no ODP actions. The caller should fix that
2378 * by calling facet_make_actions(). */
2379 static struct facet *
2380 facet_create(struct rule_dpif *rule, const struct flow *flow)
2382 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2383 struct facet *facet;
2385 facet = xzalloc(sizeof *facet);
2386 facet->used = time_msec();
2387 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2388 list_push_back(&rule->facets, &facet->list_node);
2390 facet->flow = *flow;
2391 netflow_flow_init(&facet->nf_flow);
2392 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2398 facet_free(struct facet *facet)
2400 free(facet->actions);
2405 execute_controller_action(struct ofproto_dpif *ofproto,
2406 const struct flow *flow,
2407 const struct nlattr *odp_actions, size_t actions_len,
2408 struct ofpbuf *packet)
2411 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2412 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2413 /* As an optimization, avoid a round-trip from userspace to kernel to
2414 * userspace. This also avoids possibly filling up kernel packet
2415 * buffers along the way.
2417 * This optimization will not accidentally catch sFlow
2418 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2419 * inside OVS_ACTION_ATTR_SAMPLE. */
2420 const struct nlattr *nla;
2422 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2423 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2431 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2432 * 'packet', which arrived on 'in_port'.
2434 * Takes ownership of 'packet'. */
2436 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2437 const struct nlattr *odp_actions, size_t actions_len,
2438 struct ofpbuf *packet)
2440 struct odputil_keybuf keybuf;
2444 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2449 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2450 odp_flow_key_from_flow(&key, flow);
2452 error = dpif_execute(ofproto->dpif, key.data, key.size,
2453 odp_actions, actions_len, packet);
2455 ofpbuf_delete(packet);
2459 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2460 * statistics appropriately. 'packet' must have at least sizeof(struct
2461 * ofp_packet_in) bytes of headroom.
2463 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2464 * applying flow_extract() to 'packet' would yield the same flow as
2467 * 'facet' must have accurately composed datapath actions; that is, it must
2468 * not be in need of revalidation.
2470 * Takes ownership of 'packet'. */
2472 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2473 struct ofpbuf *packet)
2475 struct dpif_flow_stats stats;
2477 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2479 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2480 stats.used = time_msec();
2481 if (execute_odp_actions(ofproto, &facet->flow,
2482 facet->actions, facet->actions_len, packet)) {
2483 facet_update_stats(ofproto, facet, &stats);
2487 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2489 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2490 * rule's statistics, via facet_uninstall().
2492 * - Removes 'facet' from its rule and from ofproto->facets.
2495 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2497 facet_uninstall(ofproto, facet);
2498 facet_flush_stats(ofproto, facet);
2499 hmap_remove(&ofproto->facets, &facet->hmap_node);
2500 list_remove(&facet->list_node);
2504 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2506 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2507 const struct ofpbuf *packet)
2509 const struct rule_dpif *rule = facet->rule;
2510 struct ofpbuf *odp_actions;
2511 struct action_xlate_ctx ctx;
2513 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2514 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2515 facet->tags = ctx.tags;
2516 facet->may_install = ctx.may_set_up_flow;
2517 facet->has_learn = ctx.has_learn;
2518 facet->has_normal = ctx.has_normal;
2519 facet->nf_flow.output_iface = ctx.nf_output_iface;
2521 if (facet->actions_len != odp_actions->size
2522 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2523 free(facet->actions);
2524 facet->actions_len = odp_actions->size;
2525 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2528 ofpbuf_delete(odp_actions);
2531 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2532 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2533 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2534 * since 'facet' was last updated.
2536 * Returns 0 if successful, otherwise a positive errno value.*/
2538 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2539 const struct nlattr *actions, size_t actions_len,
2540 struct dpif_flow_stats *stats)
2542 struct odputil_keybuf keybuf;
2543 enum dpif_flow_put_flags flags;
2547 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2549 flags |= DPIF_FP_ZERO_STATS;
2552 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2553 odp_flow_key_from_flow(&key, &facet->flow);
2555 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2556 actions, actions_len, stats);
2559 facet_reset_dp_stats(facet, stats);
2565 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2566 * 'zero_stats' is true, clears any existing statistics from the datapath for
2569 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2571 struct dpif_flow_stats stats;
2573 if (facet->may_install
2574 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2575 zero_stats ? &stats : NULL)) {
2576 facet->installed = true;
2581 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2584 const struct nlattr *a;
2588 if (facet->byte_count <= facet->accounted_bytes) {
2591 n_bytes = facet->byte_count - facet->accounted_bytes;
2592 facet->accounted_bytes = facet->byte_count;
2594 /* Feed information from the active flows back into the learning table to
2595 * ensure that table is always in sync with what is actually flowing
2596 * through the datapath. */
2597 if (facet->has_learn || facet->has_normal) {
2598 struct action_xlate_ctx ctx;
2600 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2601 ctx.may_learn = true;
2602 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2603 facet->rule->up.n_actions));
2606 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2610 /* This loop feeds byte counters to bond_account() for rebalancing to use
2611 * as a basis. We also need to track the actual VLAN on which the packet
2612 * is going to be sent to ensure that it matches the one passed to
2613 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2615 vlan_tci = facet->flow.vlan_tci;
2616 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2617 struct ofport_dpif *port;
2619 switch (nl_attr_type(a)) {
2620 const struct nlattr *nested;
2621 case OVS_ACTION_ATTR_OUTPUT:
2622 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2623 if (port && port->bundle && port->bundle->bond) {
2624 bond_account(port->bundle->bond, &facet->flow,
2625 vlan_tci_to_vid(vlan_tci), n_bytes);
2629 case OVS_ACTION_ATTR_POP:
2630 if (nl_attr_get_u16(a) == OVS_KEY_ATTR_8021Q) {
2631 vlan_tci = htons(0);
2635 case OVS_ACTION_ATTR_PUSH:
2636 nested = nl_attr_get(a);
2637 if (nl_attr_type(nested) == OVS_KEY_ATTR_8021Q) {
2638 const struct ovs_key_8021q *q_key;
2640 q_key = nl_attr_get_unspec(nested, sizeof(*q_key));
2641 vlan_tci = q_key->q_tci;
2648 /* If 'rule' is installed in the datapath, uninstalls it. */
2650 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2652 if (facet->installed) {
2653 struct odputil_keybuf keybuf;
2654 struct dpif_flow_stats stats;
2658 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2659 odp_flow_key_from_flow(&key, &facet->flow);
2661 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2662 facet_reset_dp_stats(facet, &stats);
2664 facet_update_stats(p, facet, &stats);
2666 facet->installed = false;
2668 assert(facet->dp_packet_count == 0);
2669 assert(facet->dp_byte_count == 0);
2673 /* Returns true if the only action for 'facet' is to send to the controller.
2674 * (We don't report NetFlow expiration messages for such facets because they
2675 * are just part of the control logic for the network, not real traffic). */
2677 facet_is_controller_flow(struct facet *facet)
2680 && facet->rule->up.n_actions == 1
2681 && action_outputs_to_port(&facet->rule->up.actions[0],
2682 htons(OFPP_CONTROLLER)));
2685 /* Resets 'facet''s datapath statistics counters. This should be called when
2686 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2687 * it should contain the statistics returned by dpif when 'facet' was reset in
2688 * the datapath. 'stats' will be modified to only included statistics new
2689 * since 'facet' was last updated. */
2691 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2693 if (stats && facet->dp_packet_count <= stats->n_packets
2694 && facet->dp_byte_count <= stats->n_bytes) {
2695 stats->n_packets -= facet->dp_packet_count;
2696 stats->n_bytes -= facet->dp_byte_count;
2699 facet->dp_packet_count = 0;
2700 facet->dp_byte_count = 0;
2703 /* Folds all of 'facet''s statistics into its rule. Also updates the
2704 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2705 * 'facet''s statistics in the datapath should have been zeroed and folded into
2706 * its packet and byte counts before this function is called. */
2708 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2710 assert(!facet->dp_byte_count);
2711 assert(!facet->dp_packet_count);
2713 facet_push_stats(facet);
2714 facet_account(ofproto, facet);
2716 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2717 struct ofexpired expired;
2718 expired.flow = facet->flow;
2719 expired.packet_count = facet->packet_count;
2720 expired.byte_count = facet->byte_count;
2721 expired.used = facet->used;
2722 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2725 facet->rule->packet_count += facet->packet_count;
2726 facet->rule->byte_count += facet->byte_count;
2728 /* Reset counters to prevent double counting if 'facet' ever gets
2730 facet_reset_counters(facet);
2732 netflow_flow_clear(&facet->nf_flow);
2735 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2736 * Returns it if found, otherwise a null pointer.
2738 * The returned facet might need revalidation; use facet_lookup_valid()
2739 * instead if that is important. */
2740 static struct facet *
2741 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2743 struct facet *facet;
2745 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2747 if (flow_equal(flow, &facet->flow)) {
2755 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2756 * Returns it if found, otherwise a null pointer.
2758 * The returned facet is guaranteed to be valid. */
2759 static struct facet *
2760 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2762 struct facet *facet = facet_find(ofproto, flow);
2764 /* The facet we found might not be valid, since we could be in need of
2765 * revalidation. If it is not valid, don't return it. */
2767 && (ofproto->need_revalidate
2768 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
2769 && !facet_revalidate(ofproto, facet)) {
2770 COVERAGE_INC(facet_invalidated);
2777 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2779 * - If the rule found is different from 'facet''s current rule, moves
2780 * 'facet' to the new rule and recompiles its actions.
2782 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2783 * where it is and recompiles its actions anyway.
2785 * - If there is none, destroys 'facet'.
2787 * Returns true if 'facet' still exists, false if it has been destroyed. */
2789 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2791 struct action_xlate_ctx ctx;
2792 struct ofpbuf *odp_actions;
2793 struct rule_dpif *new_rule;
2794 bool actions_changed;
2796 COVERAGE_INC(facet_revalidate);
2798 /* Determine the new rule. */
2799 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
2801 /* No new rule, so delete the facet. */
2802 facet_remove(ofproto, facet);
2806 /* Calculate new datapath actions.
2808 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2809 * emit a NetFlow expiration and, if so, we need to have the old state
2810 * around to properly compose it. */
2811 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2812 odp_actions = xlate_actions(&ctx,
2813 new_rule->up.actions, new_rule->up.n_actions);
2814 actions_changed = (facet->actions_len != odp_actions->size
2815 || memcmp(facet->actions, odp_actions->data,
2816 facet->actions_len));
2818 /* If the datapath actions changed or the installability changed,
2819 * then we need to talk to the datapath. */
2820 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2821 if (ctx.may_set_up_flow) {
2822 struct dpif_flow_stats stats;
2824 facet_put__(ofproto, facet,
2825 odp_actions->data, odp_actions->size, &stats);
2826 facet_update_stats(ofproto, facet, &stats);
2828 facet_uninstall(ofproto, facet);
2831 /* The datapath flow is gone or has zeroed stats, so push stats out of
2832 * 'facet' into 'rule'. */
2833 facet_flush_stats(ofproto, facet);
2836 /* Update 'facet' now that we've taken care of all the old state. */
2837 facet->tags = ctx.tags;
2838 facet->nf_flow.output_iface = ctx.nf_output_iface;
2839 facet->may_install = ctx.may_set_up_flow;
2840 facet->has_learn = ctx.has_learn;
2841 facet->has_normal = ctx.has_normal;
2842 if (actions_changed) {
2843 free(facet->actions);
2844 facet->actions_len = odp_actions->size;
2845 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2847 if (facet->rule != new_rule) {
2848 COVERAGE_INC(facet_changed_rule);
2849 list_remove(&facet->list_node);
2850 list_push_back(&new_rule->facets, &facet->list_node);
2851 facet->rule = new_rule;
2852 facet->used = new_rule->up.created;
2853 facet->rs_used = facet->used;
2856 ofpbuf_delete(odp_actions);
2861 /* Updates 'facet''s used time. Caller is responsible for calling
2862 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2864 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2867 if (used > facet->used) {
2869 if (used > facet->rule->used) {
2870 facet->rule->used = used;
2872 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2876 /* Folds the statistics from 'stats' into the counters in 'facet'.
2878 * Because of the meaning of a facet's counters, it only makes sense to do this
2879 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2880 * packet that was sent by hand or if it represents statistics that have been
2881 * cleared out of the datapath. */
2883 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2884 const struct dpif_flow_stats *stats)
2886 if (stats->n_packets || stats->used > facet->used) {
2887 facet_update_time(ofproto, facet, stats->used);
2888 facet->packet_count += stats->n_packets;
2889 facet->byte_count += stats->n_bytes;
2890 facet_push_stats(facet);
2891 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2896 facet_reset_counters(struct facet *facet)
2898 facet->packet_count = 0;
2899 facet->byte_count = 0;
2900 facet->rs_packet_count = 0;
2901 facet->rs_byte_count = 0;
2902 facet->accounted_bytes = 0;
2906 facet_push_stats(struct facet *facet)
2908 uint64_t rs_packets, rs_bytes;
2910 assert(facet->packet_count >= facet->rs_packet_count);
2911 assert(facet->byte_count >= facet->rs_byte_count);
2912 assert(facet->used >= facet->rs_used);
2914 rs_packets = facet->packet_count - facet->rs_packet_count;
2915 rs_bytes = facet->byte_count - facet->rs_byte_count;
2917 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2918 facet->rs_packet_count = facet->packet_count;
2919 facet->rs_byte_count = facet->byte_count;
2920 facet->rs_used = facet->used;
2922 flow_push_stats(facet->rule, &facet->flow,
2923 rs_packets, rs_bytes, facet->used);
2927 struct ofproto_push {
2928 struct action_xlate_ctx ctx;
2935 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2937 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2940 rule->packet_count += push->packets;
2941 rule->byte_count += push->bytes;
2942 rule->used = MAX(push->used, rule->used);
2946 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2947 * 'rule''s actions. */
2949 flow_push_stats(const struct rule_dpif *rule,
2950 struct flow *flow, uint64_t packets, uint64_t bytes,
2953 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2954 struct ofproto_push push;
2956 push.packets = packets;
2960 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2961 push.ctx.resubmit_hook = push_resubmit;
2962 ofpbuf_delete(xlate_actions(&push.ctx,
2963 rule->up.actions, rule->up.n_actions));
2968 static struct rule_dpif *
2969 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2972 struct cls_rule *cls_rule;
2973 struct classifier *cls;
2975 if (table_id >= N_TABLES) {
2979 cls = &ofproto->up.tables[table_id];
2980 if (flow->tos_frag & FLOW_FRAG_ANY
2981 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
2982 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
2983 * are unavailable. */
2984 struct flow ofpc_normal_flow = *flow;
2985 ofpc_normal_flow.tp_src = htons(0);
2986 ofpc_normal_flow.tp_dst = htons(0);
2987 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
2989 cls_rule = classifier_lookup(cls, flow);
2991 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
2995 complete_operation(struct rule_dpif *rule)
2997 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2999 rule_invalidate(rule);
3001 struct dpif_completion *c = xmalloc(sizeof *c);
3002 c->op = rule->up.pending;
3003 list_push_back(&ofproto->completions, &c->list_node);
3005 ofoperation_complete(rule->up.pending, 0);
3009 static struct rule *
3012 struct rule_dpif *rule = xmalloc(sizeof *rule);
3017 rule_dealloc(struct rule *rule_)
3019 struct rule_dpif *rule = rule_dpif_cast(rule_);
3024 rule_construct(struct rule *rule_)
3026 struct rule_dpif *rule = rule_dpif_cast(rule_);
3027 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3028 struct rule_dpif *victim;
3032 error = validate_actions(rule->up.actions, rule->up.n_actions,
3033 &rule->up.cr.flow, ofproto->max_ports);
3038 rule->used = rule->up.created;
3039 rule->packet_count = 0;
3040 rule->byte_count = 0;
3042 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3043 if (victim && !list_is_empty(&victim->facets)) {
3044 struct facet *facet;
3046 rule->facets = victim->facets;
3047 list_moved(&rule->facets);
3048 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3049 /* XXX: We're only clearing our local counters here. It's possible
3050 * that quite a few packets are unaccounted for in the datapath
3051 * statistics. These will be accounted to the new rule instead of
3052 * cleared as required. This could be fixed by clearing out the
3053 * datapath statistics for this facet, but currently it doesn't
3055 facet_reset_counters(facet);
3059 /* Must avoid list_moved() in this case. */
3060 list_init(&rule->facets);
3063 table_id = rule->up.table_id;
3064 rule->tag = (victim ? victim->tag
3066 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3067 ofproto->tables[table_id].basis));
3069 complete_operation(rule);
3074 rule_destruct(struct rule *rule_)
3076 struct rule_dpif *rule = rule_dpif_cast(rule_);
3077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3078 struct facet *facet, *next_facet;
3080 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3081 facet_revalidate(ofproto, facet);
3084 complete_operation(rule);
3088 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3090 struct rule_dpif *rule = rule_dpif_cast(rule_);
3091 struct facet *facet;
3093 /* Start from historical data for 'rule' itself that are no longer tracked
3094 * in facets. This counts, for example, facets that have expired. */
3095 *packets = rule->packet_count;
3096 *bytes = rule->byte_count;
3098 /* Add any statistics that are tracked by facets. This includes
3099 * statistical data recently updated by ofproto_update_stats() as well as
3100 * stats for packets that were executed "by hand" via dpif_execute(). */
3101 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3102 *packets += facet->packet_count;
3103 *bytes += facet->byte_count;
3108 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
3110 struct rule_dpif *rule = rule_dpif_cast(rule_);
3111 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3112 struct action_xlate_ctx ctx;
3113 struct ofpbuf *odp_actions;
3114 struct facet *facet;
3117 /* First look for a related facet. If we find one, account it to that. */
3118 facet = facet_lookup_valid(ofproto, flow);
3119 if (facet && facet->rule == rule) {
3120 if (!facet->may_install) {
3121 facet_make_actions(ofproto, facet, packet);
3123 facet_execute(ofproto, facet, packet);
3127 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3128 * create a new facet for it and use that. */
3129 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3130 facet = facet_create(rule, flow);
3131 facet_make_actions(ofproto, facet, packet);
3132 facet_execute(ofproto, facet, packet);
3133 facet_install(ofproto, facet, true);
3137 /* We can't account anything to a facet. If we were to try, then that
3138 * facet would have a non-matching rule, busting our invariants. */
3139 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3140 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3141 size = packet->size;
3142 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3143 odp_actions->size, packet)) {
3144 rule->used = time_msec();
3145 rule->packet_count++;
3146 rule->byte_count += size;
3147 flow_push_stats(rule, flow, 1, size, rule->used);
3149 ofpbuf_delete(odp_actions);
3155 rule_modify_actions(struct rule *rule_)
3157 struct rule_dpif *rule = rule_dpif_cast(rule_);
3158 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3161 error = validate_actions(rule->up.actions, rule->up.n_actions,
3162 &rule->up.cr.flow, ofproto->max_ports);
3164 ofoperation_complete(rule->up.pending, error);
3168 complete_operation(rule);
3171 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3172 * Returns 0 if successful, otherwise a positive errno value. */
3174 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3175 const struct ofpbuf *packet)
3177 struct ofpbuf key, odp_actions;
3178 struct odputil_keybuf keybuf;
3182 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
3183 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3184 odp_flow_key_from_flow(&key, &flow);
3186 ofpbuf_init(&odp_actions, 32);
3187 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3189 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3190 error = dpif_execute(ofproto->dpif,
3192 odp_actions.data, odp_actions.size,
3194 ofpbuf_uninit(&odp_actions);
3197 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3198 ofproto->up.name, odp_port, strerror(error));
3203 /* OpenFlow to datapath action translation. */
3205 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3206 struct action_xlate_ctx *ctx);
3207 static void xlate_normal(struct action_xlate_ctx *);
3210 put_userspace_action(const struct ofproto_dpif *ofproto,
3211 struct ofpbuf *odp_actions,
3212 const struct flow *flow,
3213 const struct user_action_cookie *cookie)
3218 pid = dpif_port_get_pid(ofproto->dpif,
3219 ofp_port_to_odp_port(flow->in_port));
3221 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3222 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3223 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3224 cookie, sizeof *cookie);
3225 nl_msg_end_nested(odp_actions, offset);
3227 return odp_actions->size - NLA_ALIGN(sizeof *cookie);
3230 /* Compose SAMPLE action for sFlow. */
3232 compose_sflow_action(const struct ofproto_dpif *ofproto,
3233 struct ofpbuf *odp_actions,
3234 const struct flow *flow,
3237 uint32_t port_ifindex;
3238 uint32_t probability;
3239 struct user_action_cookie cookie;
3240 size_t sample_offset, actions_offset;
3241 int cookie_offset, n_output;
3243 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3247 if (odp_port == OVSP_NONE) {
3251 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3255 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3257 /* Number of packets out of UINT_MAX to sample. */
3258 probability = dpif_sflow_get_probability(ofproto->sflow);
3259 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3261 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3263 cookie.type = USER_ACTION_COOKIE_SFLOW;
3264 cookie.data = port_ifindex;
3265 cookie.n_output = n_output;
3266 cookie.vlan_tci = 0;
3267 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3269 nl_msg_end_nested(odp_actions, actions_offset);
3270 nl_msg_end_nested(odp_actions, sample_offset);
3271 return cookie_offset;
3274 /* SAMPLE action must be first action in any given list of actions.
3275 * At this point we do not have all information required to build it. So try to
3276 * build sample action as complete as possible. */
3278 add_sflow_action(struct action_xlate_ctx *ctx)
3280 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3282 &ctx->flow, OVSP_NONE);
3283 ctx->sflow_odp_port = 0;
3284 ctx->sflow_n_outputs = 0;
3287 /* Fix SAMPLE action according to data collected while composing ODP actions.
3288 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3289 * USERSPACE action's user-cookie which is required for sflow. */
3291 fix_sflow_action(struct action_xlate_ctx *ctx)
3293 const struct flow *base = &ctx->base_flow;
3294 struct user_action_cookie *cookie;
3296 if (!ctx->user_cookie_offset) {
3300 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3302 assert(cookie != NULL);
3303 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3305 if (ctx->sflow_n_outputs) {
3306 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3307 ctx->sflow_odp_port);
3309 if (ctx->sflow_n_outputs >= 255) {
3310 cookie->n_output = 255;
3312 cookie->n_output = ctx->sflow_n_outputs;
3314 cookie->vlan_tci = base->vlan_tci;
3318 commit_action__(struct ofpbuf *odp_actions,
3319 enum ovs_action_attr act_type,
3320 enum ovs_key_attr key_type,
3321 const void *key, size_t key_size)
3323 size_t offset = nl_msg_start_nested(odp_actions, act_type);
3325 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3326 nl_msg_end_nested(odp_actions, offset);
3330 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3331 struct ofpbuf *odp_actions)
3333 if (base->tun_id == flow->tun_id) {
3336 base->tun_id = flow->tun_id;
3338 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3339 OVS_KEY_ATTR_TUN_ID, &base->tun_id, sizeof(base->tun_id));
3343 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3344 struct ofpbuf *odp_actions)
3346 struct ovs_key_ethernet eth_key;
3348 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3349 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3353 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3354 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3356 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3357 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3359 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3360 OVS_KEY_ATTR_ETHERNET, ð_key, sizeof(eth_key));
3364 commit_vlan_action(struct action_xlate_ctx *ctx, ovs_be16 new_tci)
3366 struct flow *base = &ctx->base_flow;
3368 if (base->vlan_tci == new_tci) {
3372 if (base->vlan_tci & htons(VLAN_CFI)) {
3373 nl_msg_put_u16(ctx->odp_actions, OVS_ACTION_ATTR_POP,
3374 OVS_KEY_ATTR_8021Q);
3377 if (new_tci & htons(VLAN_CFI)) {
3378 struct ovs_key_8021q q_key;
3380 q_key.q_tpid = htons(ETH_TYPE_VLAN);
3381 q_key.q_tci = new_tci & ~htons(VLAN_CFI);
3383 commit_action__(ctx->odp_actions, OVS_ACTION_ATTR_PUSH,
3384 OVS_KEY_ATTR_8021Q, &q_key, sizeof(q_key));
3386 base->vlan_tci = new_tci;
3390 commit_set_nw_action(const struct flow *flow, struct flow *base,
3391 struct ofpbuf *odp_actions)
3393 int frag = base->tos_frag & FLOW_FRAG_MASK;
3394 struct ovs_key_ipv4 ipv4_key;
3396 if (base->dl_type != htons(ETH_TYPE_IP) ||
3397 !base->nw_src || !base->nw_dst) {
3401 if (base->nw_src == flow->nw_src &&
3402 base->nw_dst == flow->nw_dst &&
3403 base->tos_frag == flow->tos_frag) {
3408 memset(&ipv4_key, 0, sizeof(ipv4_key));
3409 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3410 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3411 ipv4_key.ipv4_proto = base->nw_proto;
3412 ipv4_key.ipv4_tos = flow->tos_frag & IP_DSCP_MASK;
3413 ipv4_key.ipv4_frag = (frag == 0 ? OVS_FRAG_TYPE_NONE
3414 : frag == FLOW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
3415 : OVS_FRAG_TYPE_LATER);
3417 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3418 OVS_KEY_ATTR_IPV4, &ipv4_key, sizeof(ipv4_key));
3422 commit_set_port_action(const struct flow *flow, struct flow *base,
3423 struct ofpbuf *odp_actions)
3425 if (!base->tp_src || !base->tp_dst) {
3429 if (base->tp_src == flow->tp_src &&
3430 base->tp_dst == flow->tp_dst) {
3434 if (flow->nw_proto == IPPROTO_TCP) {
3435 struct ovs_key_tcp port_key;
3437 port_key.tcp_src = base->tp_src = flow->tp_src;
3438 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3440 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3441 OVS_KEY_ATTR_TCP, &port_key, sizeof(port_key));
3443 } else if (flow->nw_proto == IPPROTO_UDP) {
3444 struct ovs_key_udp port_key;
3446 port_key.udp_src = base->tp_src = flow->tp_src;
3447 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3449 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3450 OVS_KEY_ATTR_UDP, &port_key, sizeof(port_key));
3455 commit_priority_action(struct action_xlate_ctx *ctx)
3457 if (ctx->base_priority == ctx->priority) {
3461 if (ctx->priority) {
3462 nl_msg_put_u32(ctx->odp_actions,
3463 OVS_ACTION_ATTR_SET_PRIORITY, ctx->priority);
3465 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_PRIORITY);
3467 ctx->base_priority = ctx->priority;
3471 commit_odp_actions(struct action_xlate_ctx *ctx)
3473 const struct flow *flow = &ctx->flow;
3474 struct flow *base = &ctx->base_flow;
3475 struct ofpbuf *odp_actions = ctx->odp_actions;
3477 commit_set_tun_id_action(flow, base, odp_actions);
3478 commit_set_ether_addr_action(flow, base, odp_actions);
3479 commit_vlan_action(ctx, flow->vlan_tci);
3480 commit_set_nw_action(flow, base, odp_actions);
3481 commit_set_port_action(flow, base, odp_actions);
3482 commit_priority_action(ctx);
3486 compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port)
3488 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3489 ctx->sflow_odp_port = odp_port;
3490 ctx->sflow_n_outputs++;
3494 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3496 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3497 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3500 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
3501 /* Forwarding disabled on port. */
3506 * We don't have an ofport record for this port, but it doesn't hurt to
3507 * allow forwarding to it anyhow. Maybe such a port will appear later
3508 * and we're pre-populating the flow table.
3512 commit_odp_actions(ctx);
3513 compose_output_action(ctx, odp_port);
3514 ctx->nf_output_iface = ofp_port;
3518 xlate_table_action(struct action_xlate_ctx *ctx,
3519 uint16_t in_port, uint8_t table_id)
3521 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3522 struct ofproto_dpif *ofproto = ctx->ofproto;
3523 struct rule_dpif *rule;
3524 uint16_t old_in_port;
3525 uint8_t old_table_id;
3527 old_table_id = ctx->table_id;
3528 ctx->table_id = table_id;
3530 /* Look up a flow with 'in_port' as the input port. */
3531 old_in_port = ctx->flow.in_port;
3532 ctx->flow.in_port = in_port;
3533 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3536 if (table_id > 0 && table_id < N_TABLES) {
3537 struct table_dpif *table = &ofproto->tables[table_id];
3538 if (table->other_table) {
3541 : rule_calculate_tag(&ctx->flow,
3542 &table->other_table->wc,
3547 /* Restore the original input port. Otherwise OFPP_NORMAL and
3548 * OFPP_IN_PORT will have surprising behavior. */
3549 ctx->flow.in_port = old_in_port;
3551 if (ctx->resubmit_hook) {
3552 ctx->resubmit_hook(ctx, rule);
3557 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3561 ctx->table_id = old_table_id;
3563 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3565 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3566 MAX_RESUBMIT_RECURSION);
3571 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3572 const struct nx_action_resubmit *nar)
3577 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3579 : ntohs(nar->in_port));
3580 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3582 xlate_table_action(ctx, in_port, table_id);
3586 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3588 struct ofport_dpif *ofport;
3590 commit_odp_actions(ctx);
3591 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3592 uint16_t ofp_port = ofport->up.ofp_port;
3593 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
3594 compose_output_action(ctx, ofport->odp_port);
3598 ctx->nf_output_iface = NF_OUT_FLOOD;
3602 compose_controller_action(struct action_xlate_ctx *ctx, int len)
3604 struct user_action_cookie cookie;
3606 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
3608 cookie.n_output = 0;
3609 cookie.vlan_tci = 0;
3610 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
3614 xlate_output_action__(struct action_xlate_ctx *ctx,
3615 uint16_t port, uint16_t max_len)
3617 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3619 ctx->nf_output_iface = NF_OUT_DROP;
3623 add_output_action(ctx, ctx->flow.in_port);
3626 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3632 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3635 flood_packets(ctx, htonl(0));
3637 case OFPP_CONTROLLER:
3638 commit_odp_actions(ctx);
3639 compose_controller_action(ctx, max_len);
3642 add_output_action(ctx, OFPP_LOCAL);
3647 if (port != ctx->flow.in_port) {
3648 add_output_action(ctx, port);
3653 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3654 ctx->nf_output_iface = NF_OUT_FLOOD;
3655 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3656 ctx->nf_output_iface = prev_nf_output_iface;
3657 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3658 ctx->nf_output_iface != NF_OUT_FLOOD) {
3659 ctx->nf_output_iface = NF_OUT_MULTI;
3664 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3665 const struct nx_action_output_reg *naor)
3669 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3671 if (ofp_port <= UINT16_MAX) {
3672 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3677 xlate_output_action(struct action_xlate_ctx *ctx,
3678 const struct ofp_action_output *oao)
3680 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3684 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3685 const struct ofp_action_enqueue *oae)
3687 uint16_t ofp_port, odp_port;
3688 uint32_t ctx_priority, priority;
3691 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3694 /* Fall back to ordinary output action. */
3695 xlate_output_action__(ctx, ntohs(oae->port), 0);
3699 /* Figure out datapath output port. */
3700 ofp_port = ntohs(oae->port);
3701 if (ofp_port == OFPP_IN_PORT) {
3702 ofp_port = ctx->flow.in_port;
3703 } else if (ofp_port == ctx->flow.in_port) {
3706 odp_port = ofp_port_to_odp_port(ofp_port);
3708 /* Add datapath actions. */
3709 ctx_priority = ctx->priority;
3710 ctx->priority = priority;
3711 add_output_action(ctx, odp_port);
3712 ctx->priority = ctx_priority;
3714 /* Update NetFlow output port. */
3715 if (ctx->nf_output_iface == NF_OUT_DROP) {
3716 ctx->nf_output_iface = odp_port;
3717 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3718 ctx->nf_output_iface = NF_OUT_MULTI;
3723 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3724 const struct nx_action_set_queue *nasq)
3729 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3732 /* Couldn't translate queue to a priority, so ignore. A warning
3733 * has already been logged. */
3737 ctx->priority = priority;
3740 struct xlate_reg_state {
3746 xlate_autopath(struct action_xlate_ctx *ctx,
3747 const struct nx_action_autopath *naa)
3749 uint16_t ofp_port = ntohl(naa->id);
3750 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3752 if (!port || !port->bundle) {
3753 ofp_port = OFPP_NONE;
3754 } else if (port->bundle->bond) {
3755 /* Autopath does not support VLAN hashing. */
3756 struct ofport_dpif *slave = bond_choose_output_slave(
3757 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
3759 ofp_port = slave->up.ofp_port;
3762 autopath_execute(naa, &ctx->flow, ofp_port);
3766 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3768 struct ofproto_dpif *ofproto = ofproto_;
3769 struct ofport_dpif *port;
3779 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3782 port = get_ofp_port(ofproto, ofp_port);
3783 return port ? port->may_enable : false;
3788 xlate_learn_action(struct action_xlate_ctx *ctx,
3789 const struct nx_action_learn *learn)
3791 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
3792 struct ofputil_flow_mod fm;
3795 learn_execute(learn, &ctx->flow, &fm);
3797 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
3798 if (error && !VLOG_DROP_WARN(&rl)) {
3799 char *msg = ofputil_error_to_string(error);
3800 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
3808 do_xlate_actions(const union ofp_action *in, size_t n_in,
3809 struct action_xlate_ctx *ctx)
3811 const struct ofport_dpif *port;
3812 const union ofp_action *ia;
3815 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3817 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3818 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3819 ? htonl(OFPPC_NO_RECV_STP)
3820 : htonl(OFPPC_NO_RECV))) {
3821 /* Drop this flow. */
3825 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3826 const struct ofp_action_dl_addr *oada;
3827 const struct nx_action_resubmit *nar;
3828 const struct nx_action_set_tunnel *nast;
3829 const struct nx_action_set_queue *nasq;
3830 const struct nx_action_multipath *nam;
3831 const struct nx_action_autopath *naa;
3832 const struct nx_action_bundle *nab;
3833 const struct nx_action_output_reg *naor;
3834 enum ofputil_action_code code;
3837 code = ofputil_decode_action_unsafe(ia);
3839 case OFPUTIL_OFPAT_OUTPUT:
3840 xlate_output_action(ctx, &ia->output);
3843 case OFPUTIL_OFPAT_SET_VLAN_VID:
3844 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3845 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3848 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3849 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3850 ctx->flow.vlan_tci |= htons(
3851 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3854 case OFPUTIL_OFPAT_STRIP_VLAN:
3855 ctx->flow.vlan_tci = htons(0);
3858 case OFPUTIL_OFPAT_SET_DL_SRC:
3859 oada = ((struct ofp_action_dl_addr *) ia);
3860 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3863 case OFPUTIL_OFPAT_SET_DL_DST:
3864 oada = ((struct ofp_action_dl_addr *) ia);
3865 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3868 case OFPUTIL_OFPAT_SET_NW_SRC:
3869 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3872 case OFPUTIL_OFPAT_SET_NW_DST:
3873 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3876 case OFPUTIL_OFPAT_SET_NW_TOS:
3877 ctx->flow.tos_frag &= ~IP_DSCP_MASK;
3878 ctx->flow.tos_frag |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
3881 case OFPUTIL_OFPAT_SET_TP_SRC:
3882 ctx->flow.tp_src = ia->tp_port.tp_port;
3885 case OFPUTIL_OFPAT_SET_TP_DST:
3886 ctx->flow.tp_dst = ia->tp_port.tp_port;
3889 case OFPUTIL_OFPAT_ENQUEUE:
3890 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3893 case OFPUTIL_NXAST_RESUBMIT:
3894 nar = (const struct nx_action_resubmit *) ia;
3895 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
3898 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3899 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
3902 case OFPUTIL_NXAST_SET_TUNNEL:
3903 nast = (const struct nx_action_set_tunnel *) ia;
3904 tun_id = htonll(ntohl(nast->tun_id));
3905 ctx->flow.tun_id = tun_id;
3908 case OFPUTIL_NXAST_SET_QUEUE:
3909 nasq = (const struct nx_action_set_queue *) ia;
3910 xlate_set_queue_action(ctx, nasq);
3913 case OFPUTIL_NXAST_POP_QUEUE:
3917 case OFPUTIL_NXAST_REG_MOVE:
3918 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3922 case OFPUTIL_NXAST_REG_LOAD:
3923 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3927 case OFPUTIL_NXAST_NOTE:
3928 /* Nothing to do. */
3931 case OFPUTIL_NXAST_SET_TUNNEL64:
3932 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3933 ctx->flow.tun_id = tun_id;
3936 case OFPUTIL_NXAST_MULTIPATH:
3937 nam = (const struct nx_action_multipath *) ia;
3938 multipath_execute(nam, &ctx->flow);
3941 case OFPUTIL_NXAST_AUTOPATH:
3942 naa = (const struct nx_action_autopath *) ia;
3943 xlate_autopath(ctx, naa);
3946 case OFPUTIL_NXAST_BUNDLE:
3947 ctx->ofproto->has_bundle_action = true;
3948 nab = (const struct nx_action_bundle *) ia;
3949 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3954 case OFPUTIL_NXAST_BUNDLE_LOAD:
3955 ctx->ofproto->has_bundle_action = true;
3956 nab = (const struct nx_action_bundle *) ia;
3957 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3961 case OFPUTIL_NXAST_OUTPUT_REG:
3962 naor = (const struct nx_action_output_reg *) ia;
3963 xlate_output_reg_action(ctx, naor);
3966 case OFPUTIL_NXAST_LEARN:
3967 ctx->has_learn = true;
3968 if (ctx->may_learn) {
3969 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
3977 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3978 struct ofproto_dpif *ofproto, const struct flow *flow,
3979 const struct ofpbuf *packet)
3981 ctx->ofproto = ofproto;
3983 ctx->packet = packet;
3984 ctx->may_learn = packet != NULL;
3985 ctx->resubmit_hook = NULL;
3988 static struct ofpbuf *
3989 xlate_actions(struct action_xlate_ctx *ctx,
3990 const union ofp_action *in, size_t n_in)
3992 COVERAGE_INC(ofproto_dpif_xlate);
3994 ctx->odp_actions = ofpbuf_new(512);
3995 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
3997 if (ctx->flow.tos_frag & FLOW_FRAG_ANY) {
3998 switch (ctx->ofproto->up.frag_handling) {
3999 case OFPC_FRAG_NORMAL:
4000 /* We must pretend that transport ports are unavailable. */
4001 ctx->flow.tp_src = htons(0);
4002 ctx->flow.tp_dst = htons(0);
4005 case OFPC_FRAG_DROP:
4006 return ctx->odp_actions;
4008 case OFPC_FRAG_REASM:
4011 case OFPC_FRAG_NX_MATCH:
4012 /* Nothing to do. */
4018 ctx->may_set_up_flow = true;
4019 ctx->has_learn = false;
4020 ctx->has_normal = false;
4021 ctx->nf_output_iface = NF_OUT_DROP;
4024 ctx->base_priority = 0;
4025 ctx->base_flow = ctx->flow;
4026 ctx->base_flow.tun_id = 0;
4029 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4030 ctx->may_set_up_flow = false;
4031 return ctx->odp_actions;
4033 add_sflow_action(ctx);
4034 do_xlate_actions(in, n_in, ctx);
4036 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4037 ctx->odp_actions->data,
4038 ctx->odp_actions->size)) {
4039 ctx->may_set_up_flow = false;
4041 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4043 compose_output_action(ctx, OVSP_LOCAL);
4046 fix_sflow_action(ctx);
4049 return ctx->odp_actions;
4052 /* OFPP_NORMAL implementation. */
4055 struct ofport_dpif *port;
4060 struct dst builtin[32];
4062 size_t n, allocated;
4065 static void dst_set_init(struct dst_set *);
4066 static void dst_set_add(struct dst_set *, const struct dst *);
4067 static void dst_set_free(struct dst_set *);
4069 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4071 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4072 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4073 * the bundle on which the packet was received, returns the VLAN to which the
4076 * Both 'vid' and the return value are in the range 0...4095. */
4078 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4080 switch (in_bundle->vlan_mode) {
4081 case PORT_VLAN_ACCESS:
4082 return in_bundle->vlan;
4085 case PORT_VLAN_TRUNK:
4088 case PORT_VLAN_NATIVE_UNTAGGED:
4089 case PORT_VLAN_NATIVE_TAGGED:
4090 return vid ? vid : in_bundle->vlan;
4097 /* Given 'vlan', the VLAN that a packet belongs to, and
4098 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4099 * that should be included in the 802.1Q header. (If the return value is 0,
4100 * then the 802.1Q header should only be included in the packet if there is a
4103 * Both 'vlan' and the return value are in the range 0...4095. */
4105 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4107 switch (out_bundle->vlan_mode) {
4108 case PORT_VLAN_ACCESS:
4111 case PORT_VLAN_TRUNK:
4112 case PORT_VLAN_NATIVE_TAGGED:
4115 case PORT_VLAN_NATIVE_UNTAGGED:
4116 return vlan == out_bundle->vlan ? 0 : vlan;
4124 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
4125 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
4129 vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci));
4130 dst->vid = output_vlan_to_vid(out_bundle, vlan);
4132 dst->port = (!out_bundle->bond
4133 ? ofbundle_get_a_port(out_bundle)
4134 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4135 dst->vid, &ctx->tags));
4136 return dst->port != NULL;
4140 mirror_mask_ffs(mirror_mask_t mask)
4142 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4147 dst_set_init(struct dst_set *set)
4149 set->dsts = set->builtin;
4151 set->allocated = ARRAY_SIZE(set->builtin);
4155 dst_set_add(struct dst_set *set, const struct dst *dst)
4157 if (set->n >= set->allocated) {
4158 size_t new_allocated;
4159 struct dst *new_dsts;
4161 new_allocated = set->allocated * 2;
4162 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
4163 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
4167 set->dsts = new_dsts;
4168 set->allocated = new_allocated;
4170 set->dsts[set->n++] = *dst;
4174 dst_set_free(struct dst_set *set)
4176 if (set->dsts != set->builtin) {
4182 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
4185 for (i = 0; i < set->n; i++) {
4186 if (set->dsts[i].vid == test->vid
4187 && set->dsts[i].port == test->port) {
4195 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4197 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4198 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4202 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4204 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4207 /* Returns an arbitrary interface within 'bundle'. */
4208 static struct ofport_dpif *
4209 ofbundle_get_a_port(const struct ofbundle *bundle)
4211 return CONTAINER_OF(list_front(&bundle->ports),
4212 struct ofport_dpif, bundle_node);
4216 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4217 const struct ofbundle *in_bundle,
4218 const struct ofbundle *out_bundle, struct dst_set *set)
4222 if (out_bundle == OFBUNDLE_FLOOD) {
4223 struct ofbundle *bundle;
4225 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4226 if (bundle != in_bundle
4227 && ofbundle_includes_vlan(bundle, vlan)
4228 && bundle->floodable
4229 && !bundle->mirror_out
4230 && set_dst(ctx, &dst, in_bundle, bundle)) {
4231 dst_set_add(set, &dst);
4234 ctx->nf_output_iface = NF_OUT_FLOOD;
4235 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
4236 dst_set_add(set, &dst);
4237 ctx->nf_output_iface = dst.port->odp_port;
4242 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4244 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4247 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4248 * to a VLAN. In general most packets may be mirrored but we want to drop
4249 * protocols that may confuse switches. */
4251 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4253 /* If you change this function's behavior, please update corresponding
4254 * documentation in vswitch.xml at the same time. */
4255 if (dst[0] != 0x01) {
4256 /* All the currently banned MACs happen to start with 01 currently, so
4257 * this is a quick way to eliminate most of the good ones. */
4259 if (eth_addr_is_reserved(dst)) {
4260 /* Drop STP, IEEE pause frames, and other reserved protocols
4261 * (01-80-c2-00-00-0x). */
4265 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4267 if ((dst[3] & 0xfe) == 0xcc &&
4268 (dst[4] & 0xfe) == 0xcc &&
4269 (dst[5] & 0xfe) == 0xcc) {
4270 /* Drop the following protocols plus others following the same
4273 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4274 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4275 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4279 if (!(dst[3] | dst[4] | dst[5])) {
4280 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4289 compose_mirror_dsts(struct action_xlate_ctx *ctx,
4290 uint16_t vlan, const struct ofbundle *in_bundle,
4291 struct dst_set *set)
4293 struct ofproto_dpif *ofproto = ctx->ofproto;
4294 mirror_mask_t mirrors;
4298 mirrors = in_bundle->src_mirrors;
4299 for (i = 0; i < set->n; i++) {
4300 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
4307 flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4309 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4310 if (vlan_is_mirrored(m, vlan)) {
4314 if (set_dst(ctx, &dst, in_bundle, m->out)
4315 && !dst_is_duplicate(set, &dst)) {
4316 dst_set_add(set, &dst);
4318 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
4319 struct ofbundle *bundle;
4321 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4322 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4323 && set_dst(ctx, &dst, in_bundle, bundle))
4325 /* set_dst() got dst->vid from the input packet's VLAN,
4326 * not from m->out_vlan, so recompute it. */
4327 dst.vid = output_vlan_to_vid(bundle, m->out_vlan);
4329 if (dst_is_duplicate(set, &dst)) {
4333 if (bundle == in_bundle && dst.vid == flow_vid) {
4334 /* Don't send out input port on same VLAN. */
4337 dst_set_add(set, &dst);
4342 mirrors &= mirrors - 1;
4347 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
4348 const struct ofbundle *in_bundle,
4349 const struct ofbundle *out_bundle)
4351 uint16_t initial_vid, cur_vid;
4352 const struct dst *dst;
4356 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
4357 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
4363 /* Output all the packets we can without having to change the VLAN. */
4364 commit_odp_actions(ctx);
4365 initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4366 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4367 if (dst->vid != initial_vid) {
4370 compose_output_action(ctx, dst->port->odp_port);
4373 /* Then output the rest. */
4374 cur_vid = initial_vid;
4375 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4376 if (dst->vid == initial_vid) {
4379 if (dst->vid != cur_vid) {
4382 tci = htons(dst->vid);
4383 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4385 tci |= htons(VLAN_CFI);
4387 commit_vlan_action(ctx, tci);
4391 compose_output_action(ctx, dst->port->odp_port);
4397 /* Returns the effective vlan of a packet, taking into account both the
4398 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
4399 * the packet is untagged and -1 indicates it has an invalid header and
4400 * should be dropped. */
4402 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
4403 struct ofbundle *in_bundle, bool have_packet)
4405 int vlan = vlan_tci_to_vid(flow->vlan_tci);
4407 if (in_bundle->vlan_mode == PORT_VLAN_ACCESS) {
4408 /* Drop tagged packet on access port */
4410 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4411 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4412 "packet received on port %s configured with "
4413 "implicit VLAN %"PRIu16,
4414 ofproto->up.name, vlan,
4415 in_bundle->name, in_bundle->vlan);
4418 } else if (ofbundle_includes_vlan(in_bundle, vlan)) {
4421 /* Drop packets from a VLAN not member of the trunk */
4423 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4424 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4425 "packet received on port %s not configured for "
4427 ofproto->up.name, vlan, in_bundle->name, vlan);
4432 if (in_bundle->vlan_mode != PORT_VLAN_TRUNK) {
4433 return in_bundle->vlan;
4435 return ofbundle_includes_vlan(in_bundle, 0) ? 0 : -1;
4440 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4441 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4442 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4444 is_gratuitous_arp(const struct flow *flow)
4446 return (flow->dl_type == htons(ETH_TYPE_ARP)
4447 && eth_addr_is_broadcast(flow->dl_dst)
4448 && (flow->nw_proto == ARP_OP_REPLY
4449 || (flow->nw_proto == ARP_OP_REQUEST
4450 && flow->nw_src == flow->nw_dst)));
4454 update_learning_table(struct ofproto_dpif *ofproto,
4455 const struct flow *flow, int vlan,
4456 struct ofbundle *in_bundle)
4458 struct mac_entry *mac;
4460 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4464 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4465 if (is_gratuitous_arp(flow)) {
4466 /* We don't want to learn from gratuitous ARP packets that are
4467 * reflected back over bond slaves so we lock the learning table. */
4468 if (!in_bundle->bond) {
4469 mac_entry_set_grat_arp_lock(mac);
4470 } else if (mac_entry_is_grat_arp_locked(mac)) {
4475 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4476 /* The log messages here could actually be useful in debugging,
4477 * so keep the rate limit relatively high. */
4478 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4479 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4480 "on port %s in VLAN %d",
4481 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4482 in_bundle->name, vlan);
4484 mac->port.p = in_bundle;
4485 tag_set_add(&ofproto->revalidate_set,
4486 mac_learning_changed(ofproto->ml, mac));
4490 /* Determines whether packets in 'flow' within 'br' should be forwarded or
4491 * dropped. Returns true if they may be forwarded, false if they should be
4494 * If 'have_packet' is true, it indicates that the caller is processing a
4495 * received packet. If 'have_packet' is false, then the caller is just
4496 * revalidating an existing flow because configuration has changed. Either
4497 * way, 'have_packet' only affects logging (there is no point in logging errors
4498 * during revalidation).
4500 * Sets '*in_portp' to the input port. This will be a null pointer if
4501 * flow->in_port does not designate a known input port (in which case
4502 * is_admissible() returns false).
4504 * When returning true, sets '*vlanp' to the effective VLAN of the input
4505 * packet, as returned by flow_get_vlan().
4507 * May also add tags to '*tags', although the current implementation only does
4508 * so in one special case.
4511 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4513 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
4515 struct ofport_dpif *in_port;
4516 struct ofbundle *in_bundle;
4519 /* Find the port and bundle for the received packet. */
4520 in_port = get_ofp_port(ofproto, flow->in_port);
4521 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
4522 if (!in_port || !in_bundle) {
4523 /* No interface? Something fishy... */
4525 /* Odd. A few possible reasons here:
4527 * - We deleted a port but there are still a few packets queued up
4530 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4531 * we don't know about.
4533 * - Packet arrived on the local port but the local port is not
4536 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4538 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4540 ofproto->up.name, flow->in_port);
4545 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
4550 /* Drop frames for reserved multicast addresses
4551 * only if forward_bpdu option is absent. */
4552 if (eth_addr_is_reserved(flow->dl_dst) &&
4553 !ofproto->up.forward_bpdu) {
4557 /* Drop frames on bundles reserved for mirroring. */
4558 if (in_bundle->mirror_out) {
4560 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4561 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4562 "%s, which is reserved exclusively for mirroring",
4563 ofproto->up.name, in_bundle->name);
4568 if (in_bundle->bond) {
4569 struct mac_entry *mac;
4571 switch (bond_check_admissibility(in_bundle->bond, in_port,
4572 flow->dl_dst, tags)) {
4579 case BV_DROP_IF_MOVED:
4580 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4581 if (mac && mac->port.p != in_bundle &&
4582 (!is_gratuitous_arp(flow)
4583 || mac_entry_is_grat_arp_locked(mac))) {
4594 xlate_normal(struct action_xlate_ctx *ctx)
4596 struct ofbundle *in_bundle;
4597 struct ofbundle *out_bundle;
4598 struct mac_entry *mac;
4601 ctx->has_normal = true;
4603 /* Check whether we should drop packets in this flow. */
4604 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
4605 &ctx->tags, &vlan, &in_bundle)) {
4610 /* Learn source MAC. */
4611 if (ctx->may_learn) {
4612 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4615 /* Determine output bundle. */
4616 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4619 out_bundle = mac->port.p;
4620 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4621 /* If we are revalidating but don't have a learning entry then eject
4622 * the flow. Installing a flow that floods packets opens up a window
4623 * of time where we could learn from a packet reflected on a bond and
4624 * blackhole packets before the learning table is updated to reflect
4625 * the correct port. */
4626 ctx->may_set_up_flow = false;
4629 out_bundle = OFBUNDLE_FLOOD;
4632 /* Don't send packets out their input bundles. */
4633 if (in_bundle == out_bundle) {
4639 compose_actions(ctx, vlan, in_bundle, out_bundle);
4643 /* Optimized flow revalidation.
4645 * It's a difficult problem, in general, to tell which facets need to have
4646 * their actions recalculated whenever the OpenFlow flow table changes. We
4647 * don't try to solve that general problem: for most kinds of OpenFlow flow
4648 * table changes, we recalculate the actions for every facet. This is
4649 * relatively expensive, but it's good enough if the OpenFlow flow table
4650 * doesn't change very often.
4652 * However, we can expect one particular kind of OpenFlow flow table change to
4653 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4654 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4655 * table, we add a special case that applies to flow tables in which every rule
4656 * has the same form (that is, the same wildcards), except that the table is
4657 * also allowed to have a single "catch-all" flow that matches all packets. We
4658 * optimize this case by tagging all of the facets that resubmit into the table
4659 * and invalidating the same tag whenever a flow changes in that table. The
4660 * end result is that we revalidate just the facets that need it (and sometimes
4661 * a few more, but not all of the facets or even all of the facets that
4662 * resubmit to the table modified by MAC learning). */
4664 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4665 * into an OpenFlow table with the given 'basis'. */
4667 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
4670 if (flow_wildcards_is_catchall(wc)) {
4673 struct flow tag_flow = *flow;
4674 flow_zero_wildcards(&tag_flow, wc);
4675 return tag_create_deterministic(flow_hash(&tag_flow, secret));
4679 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
4680 * taggability of that table.
4682 * This function must be called after *each* change to a flow table. If you
4683 * skip calling it on some changes then the pointer comparisons at the end can
4684 * be invalid if you get unlucky. For example, if a flow removal causes a
4685 * cls_table to be destroyed and then a flow insertion causes a cls_table with
4686 * different wildcards to be created with the same address, then this function
4687 * will incorrectly skip revalidation. */
4689 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
4691 struct table_dpif *table = &ofproto->tables[table_id];
4692 const struct classifier *cls = &ofproto->up.tables[table_id];
4693 struct cls_table *catchall, *other;
4694 struct cls_table *t;
4696 catchall = other = NULL;
4698 switch (hmap_count(&cls->tables)) {
4700 /* We could tag this OpenFlow table but it would make the logic a
4701 * little harder and it's a corner case that doesn't seem worth it
4707 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
4708 if (cls_table_is_catchall(t)) {
4710 } else if (!other) {
4713 /* Indicate that we can't tag this by setting both tables to
4714 * NULL. (We know that 'catchall' is already NULL.) */
4721 /* Can't tag this table. */
4725 if (table->catchall_table != catchall || table->other_table != other) {
4726 table->catchall_table = catchall;
4727 table->other_table = other;
4728 ofproto->need_revalidate = true;
4732 /* Given 'rule' that has changed in some way (either it is a rule being
4733 * inserted, a rule being deleted, or a rule whose actions are being
4734 * modified), marks facets for revalidation to ensure that packets will be
4735 * forwarded correctly according to the new state of the flow table.
4737 * This function must be called after *each* change to a flow table. See
4738 * the comment on table_update_taggable() for more information. */
4740 rule_invalidate(const struct rule_dpif *rule)
4742 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4744 table_update_taggable(ofproto, rule->up.table_id);
4746 if (!ofproto->need_revalidate) {
4747 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
4749 if (table->other_table && rule->tag) {
4750 tag_set_add(&ofproto->revalidate_set, rule->tag);
4752 ofproto->need_revalidate = true;
4758 set_frag_handling(struct ofproto *ofproto_,
4759 enum ofp_config_flags frag_handling)
4761 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4763 if (frag_handling != OFPC_FRAG_REASM) {
4764 ofproto->need_revalidate = true;
4772 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
4773 const struct flow *flow,
4774 const union ofp_action *ofp_actions, size_t n_ofp_actions)
4776 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4779 error = validate_actions(ofp_actions, n_ofp_actions, flow,
4780 ofproto->max_ports);
4782 struct odputil_keybuf keybuf;
4783 struct action_xlate_ctx ctx;
4784 struct ofpbuf *odp_actions;
4787 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4788 odp_flow_key_from_flow(&key, flow);
4790 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
4791 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
4792 dpif_execute(ofproto->dpif, key.data, key.size,
4793 odp_actions->data, odp_actions->size, packet);
4794 ofpbuf_delete(odp_actions);
4800 get_netflow_ids(const struct ofproto *ofproto_,
4801 uint8_t *engine_type, uint8_t *engine_id)
4803 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4805 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
4808 static struct ofproto_dpif *
4809 ofproto_dpif_lookup(const char *name)
4811 struct ofproto *ofproto = ofproto_lookup(name);
4812 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
4813 ? ofproto_dpif_cast(ofproto)
4818 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
4819 const char *args, void *aux OVS_UNUSED)
4821 struct ds ds = DS_EMPTY_INITIALIZER;
4822 const struct ofproto_dpif *ofproto;
4823 const struct mac_entry *e;
4825 ofproto = ofproto_dpif_lookup(args);
4827 unixctl_command_reply(conn, 501, "no such bridge");
4831 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4832 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4833 struct ofbundle *bundle = e->port.p;
4834 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
4835 ofbundle_get_a_port(bundle)->odp_port,
4836 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
4838 unixctl_command_reply(conn, 200, ds_cstr(&ds));
4842 struct ofproto_trace {
4843 struct action_xlate_ctx ctx;
4849 trace_format_rule(struct ds *result, uint8_t table_id, int level,
4850 const struct rule_dpif *rule)
4852 ds_put_char_multiple(result, '\t', level);
4854 ds_put_cstr(result, "No match\n");
4858 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4859 table_id, ntohll(rule->up.flow_cookie));
4860 cls_rule_format(&rule->up.cr, result);
4861 ds_put_char(result, '\n');
4863 ds_put_char_multiple(result, '\t', level);
4864 ds_put_cstr(result, "OpenFlow ");
4865 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
4866 ds_put_char(result, '\n');
4870 trace_format_flow(struct ds *result, int level, const char *title,
4871 struct ofproto_trace *trace)
4873 ds_put_char_multiple(result, '\t', level);
4874 ds_put_format(result, "%s: ", title);
4875 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
4876 ds_put_cstr(result, "unchanged");
4878 flow_format(result, &trace->ctx.flow);
4879 trace->flow = trace->ctx.flow;
4881 ds_put_char(result, '\n');
4885 trace_format_regs(struct ds *result, int level, const char *title,
4886 struct ofproto_trace *trace)
4890 ds_put_char_multiple(result, '\t', level);
4891 ds_put_format(result, "%s:", title);
4892 for (i = 0; i < FLOW_N_REGS; i++) {
4893 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
4895 ds_put_char(result, '\n');
4899 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
4901 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
4902 struct ds *result = trace->result;
4904 ds_put_char(result, '\n');
4905 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
4906 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
4907 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
4911 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
4912 void *aux OVS_UNUSED)
4914 char *dpname, *arg1, *arg2, *arg3;
4915 char *args = xstrdup(args_);
4916 char *save_ptr = NULL;
4917 struct ofproto_dpif *ofproto;
4918 struct ofpbuf odp_key;
4919 struct ofpbuf *packet;
4920 struct rule_dpif *rule;
4926 ofpbuf_init(&odp_key, 0);
4929 dpname = strtok_r(args, " ", &save_ptr);
4930 arg1 = strtok_r(NULL, " ", &save_ptr);
4931 arg2 = strtok_r(NULL, " ", &save_ptr);
4932 arg3 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4933 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
4934 /* ofproto/trace dpname flow [-generate] */
4937 /* Convert string to datapath key. */
4938 ofpbuf_init(&odp_key, 0);
4939 error = odp_flow_key_from_string(arg1, &odp_key);
4941 unixctl_command_reply(conn, 501, "Bad flow syntax");
4945 /* Convert odp_key to flow. */
4946 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
4948 unixctl_command_reply(conn, 501, "Invalid flow");
4952 /* Generate a packet, if requested. */
4954 packet = ofpbuf_new(0);
4955 flow_compose(packet, &flow);
4957 } else if (dpname && arg1 && arg2 && arg3) {
4958 /* ofproto/trace dpname tun_id in_port packet */
4962 tun_id = htonll(strtoull(arg1, NULL, 0));
4963 in_port = ofp_port_to_odp_port(atoi(arg2));
4965 packet = ofpbuf_new(strlen(args) / 2);
4966 arg3 = ofpbuf_put_hex(packet, arg3, NULL);
4967 arg3 += strspn(arg3, " ");
4968 if (*arg3 != '\0') {
4969 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4972 if (packet->size < ETH_HEADER_LEN) {
4973 unixctl_command_reply(conn, 501,
4974 "Packet data too short for Ethernet");
4978 ds_put_cstr(&result, "Packet: ");
4979 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
4980 ds_put_cstr(&result, s);
4983 flow_extract(packet, tun_id, in_port, &flow);
4985 unixctl_command_reply(conn, 501, "Bad command syntax");
4989 ofproto = ofproto_dpif_lookup(dpname);
4991 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4996 ds_put_cstr(&result, "Flow: ");
4997 flow_format(&result, &flow);
4998 ds_put_char(&result, '\n');
5000 rule = rule_dpif_lookup(ofproto, &flow, 0);
5001 trace_format_rule(&result, 0, 0, rule);
5003 struct ofproto_trace trace;
5004 struct ofpbuf *odp_actions;
5006 trace.result = &result;
5008 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5009 trace.ctx.resubmit_hook = trace_resubmit;
5010 odp_actions = xlate_actions(&trace.ctx,
5011 rule->up.actions, rule->up.n_actions);
5013 ds_put_char(&result, '\n');
5014 trace_format_flow(&result, 0, "Final flow", &trace);
5015 ds_put_cstr(&result, "Datapath actions: ");
5016 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5017 ofpbuf_delete(odp_actions);
5019 if (!trace.ctx.may_set_up_flow) {
5021 ds_put_cstr(&result, "\nThis flow is not cachable.");
5023 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5024 "for complete actions, please supply a packet.");
5029 unixctl_command_reply(conn, 200, ds_cstr(&result));
5032 ds_destroy(&result);
5033 ofpbuf_delete(packet);
5034 ofpbuf_uninit(&odp_key);
5039 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5040 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5043 unixctl_command_reply(conn, 200, NULL);
5047 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5048 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5051 unixctl_command_reply(conn, 200, NULL);
5055 ofproto_dpif_unixctl_init(void)
5057 static bool registered;
5063 unixctl_command_register("ofproto/trace",
5064 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5065 ofproto_unixctl_trace, NULL);
5066 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5068 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5069 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5072 const struct ofproto_class ofproto_dpif_class = {
5099 port_is_lacp_current,
5100 NULL, /* rule_choose_table */
5107 rule_modify_actions,
5115 get_cfm_remote_mpids,
5120 is_mirror_output_bundle,
5121 forward_bpdu_changed,