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"
35 #include "mac-learning.h"
36 #include "multipath.h"
43 #include "ofp-print.h"
44 #include "ofproto-dpif-sflow.h"
45 #include "poll-loop.h"
47 #include "unaligned.h"
49 #include "vlan-bitmap.h"
52 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
54 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
55 COVERAGE_DEFINE(ofproto_dpif_expired);
56 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
57 COVERAGE_DEFINE(ofproto_dpif_xlate);
58 COVERAGE_DEFINE(facet_changed_rule);
59 COVERAGE_DEFINE(facet_invalidated);
60 COVERAGE_DEFINE(facet_revalidate);
61 COVERAGE_DEFINE(facet_unexpected);
63 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
64 * flow translation. */
65 #define MAX_RESUBMIT_RECURSION 16
73 long long int used; /* Time last used; time created if not used. */
77 * - Do include packets and bytes from facets that have been deleted or
78 * whose own statistics have been folded into the rule.
80 * - Do include packets and bytes sent "by hand" that were accounted to
81 * the rule without any facet being involved (this is a rare corner
82 * case in rule_execute()).
84 * - Do not include packet or bytes that can be obtained from any facet's
85 * packet_count or byte_count member or that can be obtained from the
86 * datapath by, e.g., dpif_flow_get() for any facet.
88 uint64_t packet_count; /* Number of packets received. */
89 uint64_t byte_count; /* Number of bytes received. */
91 struct list facets; /* List of "struct facet"s. */
94 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
96 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
99 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *ofproto,
100 const struct flow *flow);
102 #define MAX_MIRRORS 32
103 typedef uint32_t mirror_mask_t;
104 #define MIRROR_MASK_C(X) UINT32_C(X)
105 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
107 struct ofproto_dpif *ofproto; /* Owning ofproto. */
108 size_t idx; /* In ofproto's "mirrors" array. */
109 void *aux; /* Key supplied by ofproto's client. */
110 char *name; /* Identifier for log messages. */
112 /* Selection criteria. */
113 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
114 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
115 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
117 /* Output (mutually exclusive). */
118 struct ofbundle *out; /* Output port or NULL. */
119 int out_vlan; /* Output VLAN or -1. */
122 static void mirror_destroy(struct ofmirror *);
124 /* A group of one or more OpenFlow ports. */
125 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
127 struct ofproto_dpif *ofproto; /* Owning ofproto. */
128 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
129 void *aux; /* Key supplied by ofproto's client. */
130 char *name; /* Identifier for log messages. */
133 struct list ports; /* Contains "struct ofport"s. */
134 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
135 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
136 * NULL if all VLANs are trunked. */
137 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
138 struct bond *bond; /* Nonnull iff more than one port. */
141 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
143 /* Port mirroring info. */
144 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
145 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
146 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
149 static void bundle_remove(struct ofport *);
150 static void bundle_destroy(struct ofbundle *);
151 static void bundle_del_port(struct ofport_dpif *);
152 static void bundle_run(struct ofbundle *);
153 static void bundle_wait(struct ofbundle *);
155 struct action_xlate_ctx {
156 /* action_xlate_ctx_init() initializes these members. */
159 struct ofproto_dpif *ofproto;
161 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
162 * this flow when actions change header fields. */
165 /* The packet corresponding to 'flow', or a null pointer if we are
166 * revalidating without a packet to refer to. */
167 const struct ofpbuf *packet;
169 /* If nonnull, called just before executing a resubmit action.
171 * This is normally null so the client has to set it manually after
172 * calling action_xlate_ctx_init(). */
173 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
175 /* xlate_actions() initializes and uses these members. The client might want
176 * to look at them after it returns. */
178 struct ofpbuf *odp_actions; /* Datapath actions. */
179 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
180 bool may_set_up_flow; /* True ordinarily; false if the actions must
181 * be reassessed for every packet. */
182 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
184 /* xlate_actions() initializes and uses these members, but the client has no
185 * reason to look at them. */
187 int recurse; /* Recursion level, via xlate_table_action. */
188 uint32_t priority; /* Current flow priority. 0 if none. */
189 struct flow base_flow; /* Flow at the last commit. */
190 uint32_t base_priority; /* Priority at the last commit. */
193 static void action_xlate_ctx_init(struct action_xlate_ctx *,
194 struct ofproto_dpif *, const struct flow *,
195 const struct ofpbuf *);
196 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
197 const union ofp_action *in, size_t n_in);
199 /* An exact-match instantiation of an OpenFlow flow. */
201 long long int used; /* Time last used; time created if not used. */
205 * - Do include packets and bytes sent "by hand", e.g. with
208 * - Do include packets and bytes that were obtained from the datapath
209 * when a flow was deleted (e.g. dpif_flow_del()) or when its
210 * statistics were reset (e.g. dpif_flow_put() with
211 * DPIF_FP_ZERO_STATS).
213 * - Do not include any packets or bytes that can currently be obtained
214 * from the datapath by, e.g., dpif_flow_get().
216 uint64_t packet_count; /* Number of packets received. */
217 uint64_t byte_count; /* Number of bytes received. */
219 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
220 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
222 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
223 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
224 long long int rs_used; /* Used time pushed to resubmit children. */
226 /* Number of bytes passed to account_cb. This may include bytes that can
227 * currently obtained from the datapath (thus, it can be greater than
229 uint64_t accounted_bytes;
231 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
232 struct list list_node; /* In owning rule's 'facets' list. */
233 struct rule_dpif *rule; /* Owning rule. */
234 struct flow flow; /* Exact-match flow. */
235 bool installed; /* Installed in datapath? */
236 bool may_install; /* True ordinarily; false if actions must
237 * be reassessed for every packet. */
238 size_t actions_len; /* Number of bytes in actions[]. */
239 struct nlattr *actions; /* Datapath actions. */
240 tag_type tags; /* Tags. */
241 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
244 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
245 const struct ofpbuf *packet);
246 static void facet_remove(struct ofproto_dpif *, struct facet *);
247 static void facet_free(struct facet *);
249 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
250 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
251 const struct flow *);
252 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
254 static void facet_execute(struct ofproto_dpif *, struct facet *,
255 struct ofpbuf *packet);
257 static int facet_put__(struct ofproto_dpif *, struct facet *,
258 const struct nlattr *actions, size_t actions_len,
259 struct dpif_flow_stats *);
260 static void facet_install(struct ofproto_dpif *, struct facet *,
262 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
263 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
265 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
266 const struct ofpbuf *packet);
267 static void facet_update_time(struct ofproto_dpif *, struct facet *,
269 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
270 const struct dpif_flow_stats *);
271 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
272 static void facet_push_stats(struct facet *);
273 static void facet_account(struct ofproto_dpif *, struct facet *,
274 uint64_t extra_bytes);
276 static bool facet_is_controller_flow(struct facet *);
278 static void flow_push_stats(const struct rule_dpif *,
279 struct flow *, uint64_t packets, uint64_t bytes,
286 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
287 struct list bundle_node; /* In struct ofbundle's "ports" list. */
288 struct cfm *cfm; /* Connectivity Fault Management, if any. */
289 tag_type tag; /* Tag associated with this port. */
290 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
291 bool may_enable; /* May be enabled in bonds. */
294 static struct ofport_dpif *
295 ofport_dpif_cast(const struct ofport *ofport)
297 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
298 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
301 static void port_run(struct ofport_dpif *);
302 static void port_wait(struct ofport_dpif *);
303 static int set_cfm(struct ofport *, const struct cfm_settings *);
305 struct dpif_completion {
306 struct list list_node;
307 struct ofoperation *op;
310 struct ofproto_dpif {
319 struct netflow *netflow;
320 struct dpif_sflow *sflow;
321 struct hmap bundles; /* Contains "struct ofbundle"s. */
322 struct mac_learning *ml;
323 struct ofmirror *mirrors[MAX_MIRRORS];
324 bool has_bonded_bundles;
327 struct timer next_expiration;
331 bool need_revalidate;
332 struct tag_set revalidate_set;
334 /* Support for debugging async flow mods. */
335 struct list completions;
337 bool has_bundle_action; /* True when the first bundle action appears. */
340 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
341 * for debugging the asynchronous flow_mod implementation.) */
344 static void ofproto_dpif_unixctl_init(void);
346 static struct ofproto_dpif *
347 ofproto_dpif_cast(const struct ofproto *ofproto)
349 assert(ofproto->ofproto_class == &ofproto_dpif_class);
350 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
353 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
355 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
358 /* Packet processing. */
359 static void update_learning_table(struct ofproto_dpif *,
360 const struct flow *, int vlan,
362 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
363 bool have_packet, tag_type *, int *vlanp,
364 struct ofbundle **in_bundlep);
365 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
367 /* Flow expiration. */
368 static int expire(struct ofproto_dpif *);
371 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
372 const struct ofpbuf *packet);
374 /* Global variables. */
375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
377 /* Factory functions. */
380 enumerate_types(struct sset *types)
382 dp_enumerate_types(types);
386 enumerate_names(const char *type, struct sset *names)
388 return dp_enumerate_names(type, names);
392 del(const char *type, const char *name)
397 error = dpif_open(name, type, &dpif);
399 error = dpif_delete(dpif);
405 /* Basic life-cycle. */
407 static struct ofproto *
410 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
415 dealloc(struct ofproto *ofproto_)
417 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
422 construct(struct ofproto *ofproto_)
424 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
425 const char *name = ofproto->up.name;
429 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
431 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
435 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
436 ofproto->n_matches = 0;
438 error = dpif_recv_set_mask(ofproto->dpif,
439 ((1u << DPIF_UC_MISS) |
440 (1u << DPIF_UC_ACTION) |
441 (1u << DPIF_UC_SAMPLE)));
443 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
444 dpif_close(ofproto->dpif);
447 dpif_flow_flush(ofproto->dpif);
448 dpif_recv_purge(ofproto->dpif);
450 ofproto->netflow = NULL;
451 ofproto->sflow = NULL;
452 hmap_init(&ofproto->bundles);
453 ofproto->ml = mac_learning_create();
454 for (i = 0; i < MAX_MIRRORS; i++) {
455 ofproto->mirrors[i] = NULL;
457 ofproto->has_bonded_bundles = false;
459 timer_set_duration(&ofproto->next_expiration, 1000);
461 hmap_init(&ofproto->facets);
462 ofproto->need_revalidate = false;
463 tag_set_init(&ofproto->revalidate_set);
465 list_init(&ofproto->completions);
467 ofproto->up.tables = xmalloc(sizeof *ofproto->up.tables);
468 classifier_init(&ofproto->up.tables[0]);
469 ofproto->up.n_tables = 1;
471 ofproto_dpif_unixctl_init();
473 ofproto->has_bundle_action = false;
479 complete_operations(struct ofproto_dpif *ofproto)
481 struct dpif_completion *c, *next;
483 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
484 ofoperation_complete(c->op, 0);
485 list_remove(&c->list_node);
491 destruct(struct ofproto *ofproto_)
493 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
494 struct rule_dpif *rule, *next_rule;
495 struct cls_cursor cursor;
498 complete_operations(ofproto);
500 cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL);
501 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
502 ofproto_rule_destroy(&rule->up);
505 for (i = 0; i < MAX_MIRRORS; i++) {
506 mirror_destroy(ofproto->mirrors[i]);
509 netflow_destroy(ofproto->netflow);
510 dpif_sflow_destroy(ofproto->sflow);
511 hmap_destroy(&ofproto->bundles);
512 mac_learning_destroy(ofproto->ml);
514 hmap_destroy(&ofproto->facets);
516 dpif_close(ofproto->dpif);
520 run(struct ofproto *ofproto_)
522 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
523 struct ofport_dpif *ofport;
524 struct ofbundle *bundle;
528 complete_operations(ofproto);
530 dpif_run(ofproto->dpif);
532 for (i = 0; i < 50; i++) {
533 struct dpif_upcall packet;
536 error = dpif_recv(ofproto->dpif, &packet);
538 if (error == ENODEV) {
539 /* Datapath destroyed. */
545 handle_upcall(ofproto, &packet);
548 if (timer_expired(&ofproto->next_expiration)) {
549 int delay = expire(ofproto);
550 timer_set_duration(&ofproto->next_expiration, delay);
553 if (ofproto->netflow) {
554 netflow_run(ofproto->netflow);
556 if (ofproto->sflow) {
557 dpif_sflow_run(ofproto->sflow);
560 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
563 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
567 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
569 /* Now revalidate if there's anything to do. */
570 if (ofproto->need_revalidate
571 || !tag_set_is_empty(&ofproto->revalidate_set)) {
572 struct tag_set revalidate_set = ofproto->revalidate_set;
573 bool revalidate_all = ofproto->need_revalidate;
574 struct facet *facet, *next;
576 /* Clear the revalidation flags. */
577 tag_set_init(&ofproto->revalidate_set);
578 ofproto->need_revalidate = false;
580 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
582 || tag_set_intersects(&revalidate_set, facet->tags)) {
583 facet_revalidate(ofproto, facet);
592 wait(struct ofproto *ofproto_)
594 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
595 struct ofport_dpif *ofport;
596 struct ofbundle *bundle;
598 if (!clogged && !list_is_empty(&ofproto->completions)) {
599 poll_immediate_wake();
602 dpif_wait(ofproto->dpif);
603 dpif_recv_wait(ofproto->dpif);
604 if (ofproto->sflow) {
605 dpif_sflow_wait(ofproto->sflow);
607 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
608 poll_immediate_wake();
610 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
613 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
616 mac_learning_wait(ofproto->ml);
617 if (ofproto->need_revalidate) {
618 /* Shouldn't happen, but if it does just go around again. */
619 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
620 poll_immediate_wake();
622 timer_wait(&ofproto->next_expiration);
627 flush(struct ofproto *ofproto_)
629 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
630 struct facet *facet, *next_facet;
632 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
633 /* Mark the facet as not installed so that facet_remove() doesn't
634 * bother trying to uninstall it. There is no point in uninstalling it
635 * individually since we are about to blow away all the facets with
636 * dpif_flow_flush(). */
637 facet->installed = false;
638 facet->dp_packet_count = 0;
639 facet->dp_byte_count = 0;
640 facet_remove(ofproto, facet);
642 dpif_flow_flush(ofproto->dpif);
646 get_features(struct ofproto *ofproto_ OVS_UNUSED,
647 bool *arp_match_ip, uint32_t *actions)
649 *arp_match_ip = true;
650 *actions = ((1u << OFPAT_OUTPUT) |
651 (1u << OFPAT_SET_VLAN_VID) |
652 (1u << OFPAT_SET_VLAN_PCP) |
653 (1u << OFPAT_STRIP_VLAN) |
654 (1u << OFPAT_SET_DL_SRC) |
655 (1u << OFPAT_SET_DL_DST) |
656 (1u << OFPAT_SET_NW_SRC) |
657 (1u << OFPAT_SET_NW_DST) |
658 (1u << OFPAT_SET_NW_TOS) |
659 (1u << OFPAT_SET_TP_SRC) |
660 (1u << OFPAT_SET_TP_DST) |
661 (1u << OFPAT_ENQUEUE));
665 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
667 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
670 strcpy(ots->name, "classifier");
672 dpif_get_dp_stats(ofproto->dpif, &s);
673 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
674 put_32aligned_be64(&ots->matched_count,
675 htonll(s.n_hit + ofproto->n_matches));
679 set_netflow(struct ofproto *ofproto_,
680 const struct netflow_options *netflow_options)
682 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
684 if (netflow_options) {
685 if (!ofproto->netflow) {
686 ofproto->netflow = netflow_create();
688 return netflow_set_options(ofproto->netflow, netflow_options);
690 netflow_destroy(ofproto->netflow);
691 ofproto->netflow = NULL;
696 static struct ofport *
699 struct ofport_dpif *port = xmalloc(sizeof *port);
704 port_dealloc(struct ofport *port_)
706 struct ofport_dpif *port = ofport_dpif_cast(port_);
711 port_construct(struct ofport *port_)
713 struct ofport_dpif *port = ofport_dpif_cast(port_);
714 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
716 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
719 port->tag = tag_create_random();
720 port->may_enable = true;
722 if (ofproto->sflow) {
723 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
724 netdev_get_name(port->up.netdev));
731 port_destruct(struct ofport *port_)
733 struct ofport_dpif *port = ofport_dpif_cast(port_);
734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
736 bundle_remove(port_);
737 set_cfm(port_, NULL);
738 if (ofproto->sflow) {
739 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
744 port_modified(struct ofport *port_)
746 struct ofport_dpif *port = ofport_dpif_cast(port_);
748 if (port->bundle && port->bundle->bond) {
749 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
754 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
756 struct ofport_dpif *port = ofport_dpif_cast(port_);
757 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
758 ovs_be32 changed = old_config ^ port->up.opp.config;
760 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
761 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
762 ofproto->need_revalidate = true;
767 set_sflow(struct ofproto *ofproto_,
768 const struct ofproto_sflow_options *sflow_options)
770 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
771 struct dpif_sflow *ds = ofproto->sflow;
774 struct ofport_dpif *ofport;
776 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
777 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
778 dpif_sflow_add_port(ds, ofport->odp_port,
779 netdev_get_name(ofport->up.netdev));
782 dpif_sflow_set_options(ds, sflow_options);
784 dpif_sflow_destroy(ds);
785 ofproto->sflow = NULL;
791 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
793 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
800 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
803 if (cfm_configure(ofport->cfm, s)) {
809 cfm_destroy(ofport->cfm);
815 get_cfm_fault(const struct ofport *ofport_)
817 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
819 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
824 /* Expires all MAC learning entries associated with 'port' and forces ofproto
825 * to revalidate every flow. */
827 bundle_flush_macs(struct ofbundle *bundle)
829 struct ofproto_dpif *ofproto = bundle->ofproto;
830 struct mac_learning *ml = ofproto->ml;
831 struct mac_entry *mac, *next_mac;
833 ofproto->need_revalidate = true;
834 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
835 if (mac->port.p == bundle) {
836 mac_learning_expire(ml, mac);
841 static struct ofbundle *
842 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
844 struct ofbundle *bundle;
846 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
848 if (bundle->aux == aux) {
855 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
856 * ones that are found to 'bundles'. */
858 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
859 void **auxes, size_t n_auxes,
860 struct hmapx *bundles)
865 for (i = 0; i < n_auxes; i++) {
866 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
868 hmapx_add(bundles, bundle);
874 bundle_del_port(struct ofport_dpif *port)
876 struct ofbundle *bundle = port->bundle;
878 bundle->ofproto->need_revalidate = true;
880 list_remove(&port->bundle_node);
884 lacp_slave_unregister(bundle->lacp, port);
887 bond_slave_unregister(bundle->bond, port);
890 bundle->floodable = true;
891 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
892 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
893 bundle->floodable = false;
899 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
900 struct lacp_slave_settings *lacp,
901 uint32_t bond_stable_id)
903 struct ofport_dpif *port;
905 port = get_ofp_port(bundle->ofproto, ofp_port);
910 if (port->bundle != bundle) {
911 bundle->ofproto->need_revalidate = true;
913 bundle_del_port(port);
916 port->bundle = bundle;
917 list_push_back(&bundle->ports, &port->bundle_node);
918 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
919 bundle->floodable = false;
923 lacp_slave_register(bundle->lacp, port, lacp);
926 port->bond_stable_id = bond_stable_id;
932 bundle_destroy(struct ofbundle *bundle)
934 struct ofproto_dpif *ofproto;
935 struct ofport_dpif *port, *next_port;
942 ofproto = bundle->ofproto;
943 for (i = 0; i < MAX_MIRRORS; i++) {
944 struct ofmirror *m = ofproto->mirrors[i];
946 if (m->out == bundle) {
948 } else if (hmapx_find_and_delete(&m->srcs, bundle)
949 || hmapx_find_and_delete(&m->dsts, bundle)) {
950 ofproto->need_revalidate = true;
955 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
956 bundle_del_port(port);
959 bundle_flush_macs(bundle);
960 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
962 free(bundle->trunks);
963 lacp_destroy(bundle->lacp);
964 bond_destroy(bundle->bond);
969 bundle_set(struct ofproto *ofproto_, void *aux,
970 const struct ofproto_bundle_settings *s)
972 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
973 bool need_flush = false;
974 const unsigned long *trunks;
975 struct ofport_dpif *port;
976 struct ofbundle *bundle;
981 bundle_destroy(bundle_lookup(ofproto, aux));
985 assert(s->n_slaves == 1 || s->bond != NULL);
986 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
988 bundle = bundle_lookup(ofproto, aux);
990 bundle = xmalloc(sizeof *bundle);
992 bundle->ofproto = ofproto;
993 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
994 hash_pointer(aux, 0));
998 list_init(&bundle->ports);
1000 bundle->trunks = NULL;
1001 bundle->lacp = NULL;
1002 bundle->bond = NULL;
1004 bundle->floodable = true;
1006 bundle->src_mirrors = 0;
1007 bundle->dst_mirrors = 0;
1008 bundle->mirror_out = 0;
1011 if (!bundle->name || strcmp(s->name, bundle->name)) {
1013 bundle->name = xstrdup(s->name);
1018 if (!bundle->lacp) {
1019 bundle->lacp = lacp_create();
1021 lacp_configure(bundle->lacp, s->lacp);
1023 lacp_destroy(bundle->lacp);
1024 bundle->lacp = NULL;
1027 /* Update set of ports. */
1029 for (i = 0; i < s->n_slaves; i++) {
1030 if (!bundle_add_port(bundle, s->slaves[i],
1031 s->lacp ? &s->lacp_slaves[i] : NULL,
1032 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1036 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1037 struct ofport_dpif *next_port;
1039 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1040 for (i = 0; i < s->n_slaves; i++) {
1041 if (s->slaves[i] == port->up.ofp_port) {
1046 bundle_del_port(port);
1050 assert(list_size(&bundle->ports) <= s->n_slaves);
1052 if (list_is_empty(&bundle->ports)) {
1053 bundle_destroy(bundle);
1058 if (s->vlan != bundle->vlan) {
1059 bundle->vlan = s->vlan;
1063 /* Get trunked VLANs. */
1064 trunks = s->vlan == -1 ? s->trunks : NULL;
1065 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1066 free(bundle->trunks);
1067 bundle->trunks = vlan_bitmap_clone(trunks);
1072 if (!list_is_short(&bundle->ports)) {
1073 bundle->ofproto->has_bonded_bundles = true;
1075 if (bond_reconfigure(bundle->bond, s->bond)) {
1076 ofproto->need_revalidate = true;
1079 bundle->bond = bond_create(s->bond);
1080 ofproto->need_revalidate = true;
1083 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1084 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1088 bond_destroy(bundle->bond);
1089 bundle->bond = NULL;
1092 /* If we changed something that would affect MAC learning, un-learn
1093 * everything on this port and force flow revalidation. */
1095 bundle_flush_macs(bundle);
1102 bundle_remove(struct ofport *port_)
1104 struct ofport_dpif *port = ofport_dpif_cast(port_);
1105 struct ofbundle *bundle = port->bundle;
1108 bundle_del_port(port);
1109 if (list_is_empty(&bundle->ports)) {
1110 bundle_destroy(bundle);
1111 } else if (list_is_short(&bundle->ports)) {
1112 bond_destroy(bundle->bond);
1113 bundle->bond = NULL;
1119 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1121 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1122 struct ofport_dpif *port = port_;
1123 uint8_t ea[ETH_ADDR_LEN];
1126 error = netdev_get_etheraddr(port->up.netdev, ea);
1128 struct lacp_pdu *packet_pdu;
1129 struct ofpbuf packet;
1131 ofpbuf_init(&packet, 0);
1132 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1133 sizeof *packet_pdu);
1135 error = netdev_send(port->up.netdev, &packet);
1137 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1138 "(%s)", port->bundle->name,
1139 netdev_get_name(port->up.netdev), strerror(error));
1141 ofpbuf_uninit(&packet);
1143 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1144 "%s (%s)", port->bundle->name,
1145 netdev_get_name(port->up.netdev), strerror(error));
1150 bundle_send_learning_packets(struct ofbundle *bundle)
1152 struct ofproto_dpif *ofproto = bundle->ofproto;
1153 int error, n_packets, n_errors;
1154 struct mac_entry *e;
1156 error = n_packets = n_errors = 0;
1157 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1158 if (e->port.p != bundle) {
1159 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1169 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1170 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1171 "packets, last error was: %s",
1172 bundle->name, n_errors, n_packets, strerror(error));
1174 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1175 bundle->name, n_packets);
1180 bundle_run(struct ofbundle *bundle)
1183 lacp_run(bundle->lacp, send_pdu_cb);
1186 struct ofport_dpif *port;
1188 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1189 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1192 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1193 lacp_negotiated(bundle->lacp));
1194 if (bond_should_send_learning_packets(bundle->bond)) {
1195 bundle_send_learning_packets(bundle);
1201 bundle_wait(struct ofbundle *bundle)
1204 lacp_wait(bundle->lacp);
1207 bond_wait(bundle->bond);
1214 mirror_scan(struct ofproto_dpif *ofproto)
1218 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1219 if (!ofproto->mirrors[idx]) {
1226 static struct ofmirror *
1227 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1231 for (i = 0; i < MAX_MIRRORS; i++) {
1232 struct ofmirror *mirror = ofproto->mirrors[i];
1233 if (mirror && mirror->aux == aux) {
1242 mirror_set(struct ofproto *ofproto_, void *aux,
1243 const struct ofproto_mirror_settings *s)
1245 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1246 mirror_mask_t mirror_bit;
1247 struct ofbundle *bundle;
1248 struct ofmirror *mirror;
1249 struct ofbundle *out;
1250 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1251 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1254 mirror = mirror_lookup(ofproto, aux);
1256 mirror_destroy(mirror);
1262 idx = mirror_scan(ofproto);
1264 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1266 ofproto->up.name, MAX_MIRRORS, s->name);
1270 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1271 mirror->ofproto = ofproto;
1274 mirror->out_vlan = -1;
1275 mirror->name = NULL;
1278 if (!mirror->name || strcmp(s->name, mirror->name)) {
1280 mirror->name = xstrdup(s->name);
1283 /* Get the new configuration. */
1284 if (s->out_bundle) {
1285 out = bundle_lookup(ofproto, s->out_bundle);
1287 mirror_destroy(mirror);
1293 out_vlan = s->out_vlan;
1295 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1296 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1298 /* If the configuration has not changed, do nothing. */
1299 if (hmapx_equals(&srcs, &mirror->srcs)
1300 && hmapx_equals(&dsts, &mirror->dsts)
1301 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1302 && mirror->out == out
1303 && mirror->out_vlan == out_vlan)
1305 hmapx_destroy(&srcs);
1306 hmapx_destroy(&dsts);
1310 hmapx_swap(&srcs, &mirror->srcs);
1311 hmapx_destroy(&srcs);
1313 hmapx_swap(&dsts, &mirror->dsts);
1314 hmapx_destroy(&dsts);
1316 free(mirror->vlans);
1317 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1320 mirror->out_vlan = out_vlan;
1322 /* Update bundles. */
1323 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1324 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1325 if (hmapx_contains(&mirror->srcs, bundle)) {
1326 bundle->src_mirrors |= mirror_bit;
1328 bundle->src_mirrors &= ~mirror_bit;
1331 if (hmapx_contains(&mirror->dsts, bundle)) {
1332 bundle->dst_mirrors |= mirror_bit;
1334 bundle->dst_mirrors &= ~mirror_bit;
1337 if (mirror->out == bundle) {
1338 bundle->mirror_out |= mirror_bit;
1340 bundle->mirror_out &= ~mirror_bit;
1344 ofproto->need_revalidate = true;
1345 mac_learning_flush(ofproto->ml);
1351 mirror_destroy(struct ofmirror *mirror)
1353 struct ofproto_dpif *ofproto;
1354 mirror_mask_t mirror_bit;
1355 struct ofbundle *bundle;
1361 ofproto = mirror->ofproto;
1362 ofproto->need_revalidate = true;
1363 mac_learning_flush(ofproto->ml);
1365 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1366 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1367 bundle->src_mirrors &= ~mirror_bit;
1368 bundle->dst_mirrors &= ~mirror_bit;
1369 bundle->mirror_out &= ~mirror_bit;
1372 hmapx_destroy(&mirror->srcs);
1373 hmapx_destroy(&mirror->dsts);
1374 free(mirror->vlans);
1376 ofproto->mirrors[mirror->idx] = NULL;
1382 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1384 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1385 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1386 ofproto->need_revalidate = true;
1387 mac_learning_flush(ofproto->ml);
1393 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1395 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1396 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1397 return bundle && bundle->mirror_out != 0;
1401 forward_bpdu_changed(struct ofproto *ofproto_)
1403 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1404 /* Revalidate cached flows whenever forward_bpdu option changes. */
1405 ofproto->need_revalidate = true;
1410 static struct ofport_dpif *
1411 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1413 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1414 return ofport ? ofport_dpif_cast(ofport) : NULL;
1417 static struct ofport_dpif *
1418 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1420 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1424 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1425 struct dpif_port *dpif_port)
1427 ofproto_port->name = dpif_port->name;
1428 ofproto_port->type = dpif_port->type;
1429 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1433 port_run(struct ofport_dpif *ofport)
1435 bool enable = netdev_get_carrier(ofport->up.netdev);
1438 cfm_run(ofport->cfm);
1440 if (cfm_should_send_ccm(ofport->cfm)) {
1441 struct ofpbuf packet;
1443 ofpbuf_init(&packet, 0);
1444 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1445 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1446 ofport->odp_port, &packet);
1447 ofpbuf_uninit(&packet);
1450 enable = enable && !cfm_get_fault(ofport->cfm);
1453 if (ofport->bundle) {
1454 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1457 if (ofport->may_enable != enable) {
1458 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1460 if (ofproto->has_bundle_action) {
1461 ofproto->need_revalidate = true;
1465 ofport->may_enable = enable;
1469 port_wait(struct ofport_dpif *ofport)
1472 cfm_wait(ofport->cfm);
1477 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1478 struct ofproto_port *ofproto_port)
1480 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1481 struct dpif_port dpif_port;
1484 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1486 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1492 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1494 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1498 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1500 *ofp_portp = odp_port_to_ofp_port(odp_port);
1506 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1508 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1511 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1513 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1515 /* The caller is going to close ofport->up.netdev. If this is a
1516 * bonded port, then the bond is using that netdev, so remove it
1517 * from the bond. The client will need to reconfigure everything
1518 * after deleting ports, so then the slave will get re-added. */
1519 bundle_remove(&ofport->up);
1525 struct port_dump_state {
1526 struct dpif_port_dump dump;
1531 port_dump_start(const struct ofproto *ofproto_, void **statep)
1533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1534 struct port_dump_state *state;
1536 *statep = state = xmalloc(sizeof *state);
1537 dpif_port_dump_start(&state->dump, ofproto->dpif);
1538 state->done = false;
1543 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1544 struct ofproto_port *port)
1546 struct port_dump_state *state = state_;
1547 struct dpif_port dpif_port;
1549 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1550 ofproto_port_from_dpif_port(port, &dpif_port);
1553 int error = dpif_port_dump_done(&state->dump);
1555 return error ? error : EOF;
1560 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1562 struct port_dump_state *state = state_;
1565 dpif_port_dump_done(&state->dump);
1572 port_poll(const struct ofproto *ofproto_, char **devnamep)
1574 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1575 return dpif_port_poll(ofproto->dpif, devnamep);
1579 port_poll_wait(const struct ofproto *ofproto_)
1581 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1582 dpif_port_poll_wait(ofproto->dpif);
1586 port_is_lacp_current(const struct ofport *ofport_)
1588 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1589 return (ofport->bundle && ofport->bundle->lacp
1590 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1594 /* Upcall handling. */
1596 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1597 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1598 * their individual configurations.
1600 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1601 * Otherwise, ownership is transferred to this function. */
1603 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1604 const struct flow *flow, bool clone)
1606 struct ofputil_packet_in pin;
1608 pin.packet = upcall->packet;
1609 pin.in_port = flow->in_port;
1610 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1611 pin.buffer_id = 0; /* not yet known */
1612 pin.send_len = upcall->userdata;
1613 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1614 clone ? NULL : upcall->packet);
1618 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1619 const struct ofpbuf *packet)
1621 if (cfm_should_process_flow(flow)) {
1622 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1623 if (packet && ofport && ofport->cfm) {
1624 cfm_process_heartbeat(ofport->cfm, packet);
1627 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1628 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1629 if (packet && port && port->bundle && port->bundle->lacp) {
1630 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1632 lacp_process_pdu(port->bundle->lacp, port, pdu);
1641 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1643 struct facet *facet;
1646 /* Obtain in_port and tun_id, at least. */
1647 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1649 /* Set header pointers in 'flow'. */
1650 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1652 /* Handle 802.1ag and LACP. */
1653 if (process_special(ofproto, &flow, upcall->packet)) {
1654 ofpbuf_delete(upcall->packet);
1655 ofproto->n_matches++;
1659 /* Check with in-band control to see if this packet should be sent
1660 * to the local port regardless of the flow table. */
1661 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1662 send_packet(ofproto, ODPP_LOCAL, upcall->packet);
1665 facet = facet_lookup_valid(ofproto, &flow);
1667 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow);
1669 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1670 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1672 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1673 COVERAGE_INC(ofproto_dpif_no_packet_in);
1674 /* XXX install 'drop' flow entry */
1675 ofpbuf_delete(upcall->packet);
1679 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1683 send_packet_in(ofproto, upcall, &flow, false);
1687 facet = facet_create(rule, &flow, upcall->packet);
1688 } else if (!facet->may_install) {
1689 /* The facet is not installable, that is, we need to process every
1690 * packet, so process the current packet's actions into 'facet'. */
1691 facet_make_actions(ofproto, facet, upcall->packet);
1694 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1696 * Extra-special case for fail-open mode.
1698 * We are in fail-open mode and the packet matched the fail-open rule,
1699 * but we are connected to a controller too. We should send the packet
1700 * up to the controller in the hope that it will try to set up a flow
1701 * and thereby allow us to exit fail-open.
1703 * See the top-level comment in fail-open.c for more information.
1705 send_packet_in(ofproto, upcall, &flow, true);
1708 facet_execute(ofproto, facet, upcall->packet);
1709 facet_install(ofproto, facet, false);
1710 ofproto->n_matches++;
1714 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1718 switch (upcall->type) {
1719 case DPIF_UC_ACTION:
1720 COVERAGE_INC(ofproto_dpif_ctlr_action);
1721 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1722 send_packet_in(ofproto, upcall, &flow, false);
1725 case DPIF_UC_SAMPLE:
1726 if (ofproto->sflow) {
1727 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1728 dpif_sflow_received(ofproto->sflow, upcall, &flow);
1730 ofpbuf_delete(upcall->packet);
1734 handle_miss_upcall(ofproto, upcall);
1737 case DPIF_N_UC_TYPES:
1739 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1744 /* Flow expiration. */
1746 static int facet_max_idle(const struct ofproto_dpif *);
1747 static void update_stats(struct ofproto_dpif *);
1748 static void rule_expire(struct rule_dpif *);
1749 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1751 /* This function is called periodically by run(). Its job is to collect
1752 * updates for the flows that have been installed into the datapath, most
1753 * importantly when they last were used, and then use that information to
1754 * expire flows that have not been used recently.
1756 * Returns the number of milliseconds after which it should be called again. */
1758 expire(struct ofproto_dpif *ofproto)
1760 struct rule_dpif *rule, *next_rule;
1761 struct cls_cursor cursor;
1764 /* Update stats for each flow in the datapath. */
1765 update_stats(ofproto);
1767 /* Expire facets that have been idle too long. */
1768 dp_max_idle = facet_max_idle(ofproto);
1769 expire_facets(ofproto, dp_max_idle);
1771 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1772 cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL);
1773 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1777 /* All outstanding data in existing flows has been accounted, so it's a
1778 * good time to do bond rebalancing. */
1779 if (ofproto->has_bonded_bundles) {
1780 struct ofbundle *bundle;
1782 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1784 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1789 return MIN(dp_max_idle, 1000);
1792 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1794 * This function also pushes statistics updates to rules which each facet
1795 * resubmits into. Generally these statistics will be accurate. However, if a
1796 * facet changes the rule it resubmits into at some time in between
1797 * update_stats() runs, it is possible that statistics accrued to the
1798 * old rule will be incorrectly attributed to the new rule. This could be
1799 * avoided by calling update_stats() whenever rules are created or
1800 * deleted. However, the performance impact of making so many calls to the
1801 * datapath do not justify the benefit of having perfectly accurate statistics.
1804 update_stats(struct ofproto_dpif *p)
1806 const struct dpif_flow_stats *stats;
1807 struct dpif_flow_dump dump;
1808 const struct nlattr *key;
1811 dpif_flow_dump_start(&dump, p->dpif);
1812 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1813 struct facet *facet;
1816 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1820 odp_flow_key_format(key, key_len, &s);
1821 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
1827 facet = facet_find(p, &flow);
1829 if (facet && facet->installed) {
1831 if (stats->n_packets >= facet->dp_packet_count) {
1832 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1833 facet->packet_count += extra;
1835 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1838 if (stats->n_bytes >= facet->dp_byte_count) {
1839 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1841 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1844 facet->dp_packet_count = stats->n_packets;
1845 facet->dp_byte_count = stats->n_bytes;
1847 facet_update_time(p, facet, stats->used);
1848 facet_account(p, facet, stats->n_bytes);
1849 facet_push_stats(facet);
1851 /* There's a flow in the datapath that we know nothing about.
1853 COVERAGE_INC(facet_unexpected);
1854 dpif_flow_del(p->dpif, key, key_len, NULL);
1857 dpif_flow_dump_done(&dump);
1860 /* Calculates and returns the number of milliseconds of idle time after which
1861 * facets should expire from the datapath and we should fold their statistics
1862 * into their parent rules in userspace. */
1864 facet_max_idle(const struct ofproto_dpif *ofproto)
1867 * Idle time histogram.
1869 * Most of the time a switch has a relatively small number of facets. When
1870 * this is the case we might as well keep statistics for all of them in
1871 * userspace and to cache them in the kernel datapath for performance as
1874 * As the number of facets increases, the memory required to maintain
1875 * statistics about them in userspace and in the kernel becomes
1876 * significant. However, with a large number of facets it is likely that
1877 * only a few of them are "heavy hitters" that consume a large amount of
1878 * bandwidth. At this point, only heavy hitters are worth caching in the
1879 * kernel and maintaining in userspaces; other facets we can discard.
1881 * The technique used to compute the idle time is to build a histogram with
1882 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1883 * that is installed in the kernel gets dropped in the appropriate bucket.
1884 * After the histogram has been built, we compute the cutoff so that only
1885 * the most-recently-used 1% of facets (but at least
1886 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
1887 * the most-recently-used bucket of facets is kept, so actually an
1888 * arbitrary number of facets can be kept in any given expiration run
1889 * (though the next run will delete most of those unless they receive
1892 * This requires a second pass through the facets, in addition to the pass
1893 * made by update_stats(), because the former function never looks
1894 * at uninstallable facets.
1896 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1897 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1898 int buckets[N_BUCKETS] = { 0 };
1899 int total, subtotal, bucket;
1900 struct facet *facet;
1904 total = hmap_count(&ofproto->facets);
1905 if (total <= ofproto->up.flow_eviction_threshold) {
1906 return N_BUCKETS * BUCKET_WIDTH;
1909 /* Build histogram. */
1911 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1912 long long int idle = now - facet->used;
1913 int bucket = (idle <= 0 ? 0
1914 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1915 : (unsigned int) idle / BUCKET_WIDTH);
1919 /* Find the first bucket whose flows should be expired. */
1920 subtotal = bucket = 0;
1922 subtotal += buckets[bucket++];
1923 } while (bucket < N_BUCKETS &&
1924 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
1926 if (VLOG_IS_DBG_ENABLED()) {
1930 ds_put_cstr(&s, "keep");
1931 for (i = 0; i < N_BUCKETS; i++) {
1933 ds_put_cstr(&s, ", drop");
1936 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1939 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1943 return bucket * BUCKET_WIDTH;
1947 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1949 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1950 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1951 struct ofexpired expired;
1953 if (facet->installed) {
1954 struct dpif_flow_stats stats;
1956 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1958 facet_update_stats(ofproto, facet, &stats);
1961 expired.flow = facet->flow;
1962 expired.packet_count = facet->packet_count;
1963 expired.byte_count = facet->byte_count;
1964 expired.used = facet->used;
1965 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1970 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1972 long long int cutoff = time_msec() - dp_max_idle;
1973 struct facet *facet, *next_facet;
1975 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1976 facet_active_timeout(ofproto, facet);
1977 if (facet->used < cutoff) {
1978 facet_remove(ofproto, facet);
1983 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1984 * then delete it entirely. */
1986 rule_expire(struct rule_dpif *rule)
1988 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1989 struct facet *facet, *next_facet;
1993 /* Has 'rule' expired? */
1995 if (rule->up.hard_timeout
1996 && now > rule->up.created + rule->up.hard_timeout * 1000) {
1997 reason = OFPRR_HARD_TIMEOUT;
1998 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1999 && now > rule->used + rule->up.idle_timeout * 1000) {
2000 reason = OFPRR_IDLE_TIMEOUT;
2005 COVERAGE_INC(ofproto_dpif_expired);
2007 /* Update stats. (This is a no-op if the rule expired due to an idle
2008 * timeout, because that only happens when the rule has no facets left.) */
2009 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2010 facet_remove(ofproto, facet);
2013 /* Get rid of the rule. */
2014 ofproto_rule_expire(&rule->up, reason);
2019 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
2020 * example 'packet' within that flow.
2022 * The caller must already have determined that no facet with an identical
2023 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2024 * the ofproto's classifier table. */
2025 static struct facet *
2026 facet_create(struct rule_dpif *rule, const struct flow *flow,
2027 const struct ofpbuf *packet)
2029 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2030 struct facet *facet;
2032 facet = xzalloc(sizeof *facet);
2033 facet->used = time_msec();
2034 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2035 list_push_back(&rule->facets, &facet->list_node);
2037 facet->flow = *flow;
2038 netflow_flow_init(&facet->nf_flow);
2039 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2041 facet_make_actions(ofproto, facet, packet);
2047 facet_free(struct facet *facet)
2049 free(facet->actions);
2053 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2054 * 'packet', which arrived on 'in_port'.
2056 * Takes ownership of 'packet'. */
2058 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2059 const struct nlattr *odp_actions, size_t actions_len,
2060 struct ofpbuf *packet)
2062 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2063 && odp_actions->nla_type == ODP_ACTION_ATTR_USERSPACE) {
2064 /* As an optimization, avoid a round-trip from userspace to kernel to
2065 * userspace. This also avoids possibly filling up kernel packet
2066 * buffers along the way. */
2067 struct dpif_upcall upcall;
2069 upcall.type = DPIF_UC_ACTION;
2070 upcall.packet = packet;
2073 upcall.userdata = nl_attr_get_u64(odp_actions);
2074 upcall.sample_pool = 0;
2075 upcall.actions = NULL;
2076 upcall.actions_len = 0;
2078 send_packet_in(ofproto, &upcall, flow, false);
2082 struct odputil_keybuf keybuf;
2086 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2087 odp_flow_key_from_flow(&key, flow);
2089 error = dpif_execute(ofproto->dpif, key.data, key.size,
2090 odp_actions, actions_len, packet);
2092 ofpbuf_delete(packet);
2097 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2098 * statistics appropriately. 'packet' must have at least sizeof(struct
2099 * ofp_packet_in) bytes of headroom.
2101 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2102 * applying flow_extract() to 'packet' would yield the same flow as
2105 * 'facet' must have accurately composed ODP actions; that is, it must not be
2106 * in need of revalidation.
2108 * Takes ownership of 'packet'. */
2110 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2111 struct ofpbuf *packet)
2113 struct dpif_flow_stats stats;
2115 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2117 flow_extract_stats(&facet->flow, packet, &stats);
2118 stats.used = time_msec();
2119 if (execute_odp_actions(ofproto, &facet->flow,
2120 facet->actions, facet->actions_len, packet)) {
2121 facet_update_stats(ofproto, facet, &stats);
2125 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2127 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2128 * rule's statistics, via facet_uninstall().
2130 * - Removes 'facet' from its rule and from ofproto->facets.
2133 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2135 facet_uninstall(ofproto, facet);
2136 facet_flush_stats(ofproto, facet);
2137 hmap_remove(&ofproto->facets, &facet->hmap_node);
2138 list_remove(&facet->list_node);
2142 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2144 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2145 const struct ofpbuf *packet)
2147 const struct rule_dpif *rule = facet->rule;
2148 struct ofpbuf *odp_actions;
2149 struct action_xlate_ctx ctx;
2151 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2152 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2153 facet->tags = ctx.tags;
2154 facet->may_install = ctx.may_set_up_flow;
2155 facet->nf_flow.output_iface = ctx.nf_output_iface;
2157 if (facet->actions_len != odp_actions->size
2158 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2159 free(facet->actions);
2160 facet->actions_len = odp_actions->size;
2161 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2164 ofpbuf_delete(odp_actions);
2167 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2168 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2169 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2170 * since 'facet' was last updated.
2172 * Returns 0 if successful, otherwise a positive errno value.*/
2174 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2175 const struct nlattr *actions, size_t actions_len,
2176 struct dpif_flow_stats *stats)
2178 struct odputil_keybuf keybuf;
2179 enum dpif_flow_put_flags flags;
2183 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2185 flags |= DPIF_FP_ZERO_STATS;
2188 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2189 odp_flow_key_from_flow(&key, &facet->flow);
2191 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2192 actions, actions_len, stats);
2195 facet_reset_dp_stats(facet, stats);
2201 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2202 * 'zero_stats' is true, clears any existing statistics from the datapath for
2205 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2207 struct dpif_flow_stats stats;
2209 if (facet->may_install
2210 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2211 zero_stats ? &stats : NULL)) {
2212 facet->installed = true;
2217 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2219 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2223 facet_account(struct ofproto_dpif *ofproto,
2224 struct facet *facet, uint64_t extra_bytes)
2226 uint64_t total_bytes, n_bytes;
2227 struct ofbundle *in_bundle;
2228 const struct nlattr *a;
2234 total_bytes = facet->byte_count + extra_bytes;
2235 if (total_bytes <= facet->accounted_bytes) {
2238 n_bytes = total_bytes - facet->accounted_bytes;
2239 facet->accounted_bytes = total_bytes;
2241 /* Test that 'tags' is nonzero to ensure that only flows that include an
2242 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2243 * This works because OFPP_NORMAL always sets a nonzero tag value.
2245 * Feed information from the active flows back into the learning table to
2246 * ensure that table is always in sync with what is actually flowing
2247 * through the datapath. */
2249 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2250 &vlan, &in_bundle)) {
2254 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2256 if (!ofproto->has_bonded_bundles) {
2260 /* This loop feeds byte counters to bond_account() for rebalancing to use
2261 * as a basis. We also need to track the actual VLAN on which the packet
2262 * is going to be sent to ensure that it matches the one passed to
2263 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2265 vlan_tci = facet->flow.vlan_tci;
2266 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2267 struct ofport_dpif *port;
2269 switch (nl_attr_type(a)) {
2270 case ODP_ACTION_ATTR_OUTPUT:
2271 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2272 if (port && port->bundle && port->bundle->bond) {
2273 bond_account(port->bundle->bond, &facet->flow,
2274 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2278 case ODP_ACTION_ATTR_STRIP_VLAN:
2279 vlan_tci = htons(0);
2282 case ODP_ACTION_ATTR_SET_DL_TCI:
2283 vlan_tci = nl_attr_get_be16(a);
2289 /* If 'rule' is installed in the datapath, uninstalls it. */
2291 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2293 if (facet->installed) {
2294 struct odputil_keybuf keybuf;
2295 struct dpif_flow_stats stats;
2299 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2300 odp_flow_key_from_flow(&key, &facet->flow);
2302 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2303 facet_reset_dp_stats(facet, &stats);
2305 facet_update_stats(p, facet, &stats);
2307 facet->installed = false;
2309 assert(facet->dp_packet_count == 0);
2310 assert(facet->dp_byte_count == 0);
2314 /* Returns true if the only action for 'facet' is to send to the controller.
2315 * (We don't report NetFlow expiration messages for such facets because they
2316 * are just part of the control logic for the network, not real traffic). */
2318 facet_is_controller_flow(struct facet *facet)
2321 && facet->rule->up.n_actions == 1
2322 && action_outputs_to_port(&facet->rule->up.actions[0],
2323 htons(OFPP_CONTROLLER)));
2326 /* Resets 'facet''s datapath statistics counters. This should be called when
2327 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2328 * it should contain the statistics returned by dpif when 'facet' was reset in
2329 * the datapath. 'stats' will be modified to only included statistics new
2330 * since 'facet' was last updated. */
2332 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2334 if (stats && facet->dp_packet_count <= stats->n_packets
2335 && facet->dp_byte_count <= stats->n_bytes) {
2336 stats->n_packets -= facet->dp_packet_count;
2337 stats->n_bytes -= facet->dp_byte_count;
2340 facet->dp_packet_count = 0;
2341 facet->dp_byte_count = 0;
2344 /* Folds all of 'facet''s statistics into its rule. Also updates the
2345 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2346 * 'facet''s statistics in the datapath should have been zeroed and folded into
2347 * its packet and byte counts before this function is called. */
2349 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2351 assert(!facet->dp_byte_count);
2352 assert(!facet->dp_packet_count);
2354 facet_push_stats(facet);
2355 facet_account(ofproto, facet, 0);
2357 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2358 struct ofexpired expired;
2359 expired.flow = facet->flow;
2360 expired.packet_count = facet->packet_count;
2361 expired.byte_count = facet->byte_count;
2362 expired.used = facet->used;
2363 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2366 facet->rule->packet_count += facet->packet_count;
2367 facet->rule->byte_count += facet->byte_count;
2369 /* Reset counters to prevent double counting if 'facet' ever gets
2371 facet->packet_count = 0;
2372 facet->byte_count = 0;
2373 facet->rs_packet_count = 0;
2374 facet->rs_byte_count = 0;
2375 facet->accounted_bytes = 0;
2377 netflow_flow_clear(&facet->nf_flow);
2380 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2381 * Returns it if found, otherwise a null pointer.
2383 * The returned facet might need revalidation; use facet_lookup_valid()
2384 * instead if that is important. */
2385 static struct facet *
2386 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2388 struct facet *facet;
2390 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2392 if (flow_equal(flow, &facet->flow)) {
2400 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2401 * Returns it if found, otherwise a null pointer.
2403 * The returned facet is guaranteed to be valid. */
2404 static struct facet *
2405 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2407 struct facet *facet = facet_find(ofproto, flow);
2409 /* The facet we found might not be valid, since we could be in need of
2410 * revalidation. If it is not valid, don't return it. */
2412 && ofproto->need_revalidate
2413 && !facet_revalidate(ofproto, facet)) {
2414 COVERAGE_INC(facet_invalidated);
2421 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2423 * - If the rule found is different from 'facet''s current rule, moves
2424 * 'facet' to the new rule and recompiles its actions.
2426 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2427 * where it is and recompiles its actions anyway.
2429 * - If there is none, destroys 'facet'.
2431 * Returns true if 'facet' still exists, false if it has been destroyed. */
2433 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2435 struct action_xlate_ctx ctx;
2436 struct ofpbuf *odp_actions;
2437 struct rule_dpif *new_rule;
2438 bool actions_changed;
2440 COVERAGE_INC(facet_revalidate);
2442 /* Determine the new rule. */
2443 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
2445 /* No new rule, so delete the facet. */
2446 facet_remove(ofproto, facet);
2450 /* Calculate new ODP actions.
2452 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2453 * emit a NetFlow expiration and, if so, we need to have the old state
2454 * around to properly compose it. */
2455 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2456 odp_actions = xlate_actions(&ctx,
2457 new_rule->up.actions, new_rule->up.n_actions);
2458 actions_changed = (facet->actions_len != odp_actions->size
2459 || memcmp(facet->actions, odp_actions->data,
2460 facet->actions_len));
2462 /* If the ODP actions changed or the installability changed, then we need
2463 * to talk to the datapath. */
2464 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2465 if (ctx.may_set_up_flow) {
2466 struct dpif_flow_stats stats;
2468 facet_put__(ofproto, facet,
2469 odp_actions->data, odp_actions->size, &stats);
2470 facet_update_stats(ofproto, facet, &stats);
2472 facet_uninstall(ofproto, facet);
2475 /* The datapath flow is gone or has zeroed stats, so push stats out of
2476 * 'facet' into 'rule'. */
2477 facet_flush_stats(ofproto, facet);
2480 /* Update 'facet' now that we've taken care of all the old state. */
2481 facet->tags = ctx.tags;
2482 facet->nf_flow.output_iface = ctx.nf_output_iface;
2483 facet->may_install = ctx.may_set_up_flow;
2484 if (actions_changed) {
2485 free(facet->actions);
2486 facet->actions_len = odp_actions->size;
2487 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2489 if (facet->rule != new_rule) {
2490 COVERAGE_INC(facet_changed_rule);
2491 list_remove(&facet->list_node);
2492 list_push_back(&new_rule->facets, &facet->list_node);
2493 facet->rule = new_rule;
2494 facet->used = new_rule->up.created;
2495 facet->rs_used = facet->used;
2498 ofpbuf_delete(odp_actions);
2503 /* Updates 'facet''s used time. Caller is responsible for calling
2504 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2506 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2509 if (used > facet->used) {
2511 if (used > facet->rule->used) {
2512 facet->rule->used = used;
2514 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2518 /* Folds the statistics from 'stats' into the counters in 'facet'.
2520 * Because of the meaning of a facet's counters, it only makes sense to do this
2521 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2522 * packet that was sent by hand or if it represents statistics that have been
2523 * cleared out of the datapath. */
2525 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2526 const struct dpif_flow_stats *stats)
2528 if (stats->n_packets || stats->used > facet->used) {
2529 facet_update_time(ofproto, facet, stats->used);
2530 facet->packet_count += stats->n_packets;
2531 facet->byte_count += stats->n_bytes;
2532 facet_push_stats(facet);
2533 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2538 facet_push_stats(struct facet *facet)
2540 uint64_t rs_packets, rs_bytes;
2542 assert(facet->packet_count >= facet->rs_packet_count);
2543 assert(facet->byte_count >= facet->rs_byte_count);
2544 assert(facet->used >= facet->rs_used);
2546 rs_packets = facet->packet_count - facet->rs_packet_count;
2547 rs_bytes = facet->byte_count - facet->rs_byte_count;
2549 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2550 facet->rs_packet_count = facet->packet_count;
2551 facet->rs_byte_count = facet->byte_count;
2552 facet->rs_used = facet->used;
2554 flow_push_stats(facet->rule, &facet->flow,
2555 rs_packets, rs_bytes, facet->used);
2559 struct ofproto_push {
2560 struct action_xlate_ctx ctx;
2567 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2569 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2572 rule->packet_count += push->packets;
2573 rule->byte_count += push->bytes;
2574 rule->used = MAX(push->used, rule->used);
2578 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2579 * 'rule''s actions. */
2581 flow_push_stats(const struct rule_dpif *rule,
2582 struct flow *flow, uint64_t packets, uint64_t bytes,
2585 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2586 struct ofproto_push push;
2588 push.packets = packets;
2592 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2593 push.ctx.resubmit_hook = push_resubmit;
2594 ofpbuf_delete(xlate_actions(&push.ctx,
2595 rule->up.actions, rule->up.n_actions));
2600 static struct rule_dpif *
2601 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
2603 return rule_dpif_cast(rule_from_cls_rule(
2604 classifier_lookup(&ofproto->up.tables[0],
2609 complete_operation(struct rule_dpif *rule)
2611 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2613 ofproto->need_revalidate = true;
2615 struct dpif_completion *c = xmalloc(sizeof *c);
2616 c->op = rule->up.pending;
2617 list_push_back(&ofproto->completions, &c->list_node);
2619 ofoperation_complete(rule->up.pending, 0);
2623 static struct rule *
2626 struct rule_dpif *rule = xmalloc(sizeof *rule);
2631 rule_dealloc(struct rule *rule_)
2633 struct rule_dpif *rule = rule_dpif_cast(rule_);
2638 rule_construct(struct rule *rule_)
2640 struct rule_dpif *rule = rule_dpif_cast(rule_);
2641 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2642 struct rule_dpif *victim;
2645 error = validate_actions(rule->up.actions, rule->up.n_actions,
2646 &rule->up.cr.flow, ofproto->max_ports);
2651 rule->used = rule->up.created;
2652 rule->packet_count = 0;
2653 rule->byte_count = 0;
2655 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
2656 if (victim && !list_is_empty(&victim->facets)) {
2657 struct facet *facet;
2659 rule->facets = victim->facets;
2660 list_moved(&rule->facets);
2661 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2665 /* Must avoid list_moved() in this case. */
2666 list_init(&rule->facets);
2669 complete_operation(rule);
2674 rule_destruct(struct rule *rule_)
2676 struct rule_dpif *rule = rule_dpif_cast(rule_);
2677 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2678 struct facet *facet, *next_facet;
2680 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2681 facet_revalidate(ofproto, facet);
2684 complete_operation(rule);
2688 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2690 struct rule_dpif *rule = rule_dpif_cast(rule_);
2691 struct facet *facet;
2693 /* Start from historical data for 'rule' itself that are no longer tracked
2694 * in facets. This counts, for example, facets that have expired. */
2695 *packets = rule->packet_count;
2696 *bytes = rule->byte_count;
2698 /* Add any statistics that are tracked by facets. This includes
2699 * statistical data recently updated by ofproto_update_stats() as well as
2700 * stats for packets that were executed "by hand" via dpif_execute(). */
2701 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2702 *packets += facet->packet_count;
2703 *bytes += facet->byte_count;
2708 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2710 struct rule_dpif *rule = rule_dpif_cast(rule_);
2711 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2712 struct action_xlate_ctx ctx;
2713 struct ofpbuf *odp_actions;
2714 struct facet *facet;
2717 /* First look for a related facet. If we find one, account it to that. */
2718 facet = facet_lookup_valid(ofproto, flow);
2719 if (facet && facet->rule == rule) {
2720 facet_execute(ofproto, facet, packet);
2724 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2725 * create a new facet for it and use that. */
2726 if (rule_dpif_lookup(ofproto, flow) == rule) {
2727 facet = facet_create(rule, flow, packet);
2728 facet_execute(ofproto, facet, packet);
2729 facet_install(ofproto, facet, true);
2733 /* We can't account anything to a facet. If we were to try, then that
2734 * facet would have a non-matching rule, busting our invariants. */
2735 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2736 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2737 size = packet->size;
2738 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2739 odp_actions->size, packet)) {
2740 rule->used = time_msec();
2741 rule->packet_count++;
2742 rule->byte_count += size;
2743 flow_push_stats(rule, flow, 1, size, rule->used);
2745 ofpbuf_delete(odp_actions);
2751 rule_modify_actions(struct rule *rule_)
2753 struct rule_dpif *rule = rule_dpif_cast(rule_);
2754 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2757 error = validate_actions(rule->up.actions, rule->up.n_actions,
2758 &rule->up.cr.flow, ofproto->max_ports);
2760 ofoperation_complete(rule->up.pending, error);
2764 complete_operation(rule);
2767 /* Sends 'packet' out of port 'odp_port' within 'p'.
2768 * Returns 0 if successful, otherwise a positive errno value. */
2770 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2771 const struct ofpbuf *packet)
2773 struct ofpbuf key, odp_actions;
2774 struct odputil_keybuf keybuf;
2778 flow_extract((struct ofpbuf *) packet, 0, 0, &flow);
2779 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2780 odp_flow_key_from_flow(&key, &flow);
2782 ofpbuf_init(&odp_actions, 32);
2783 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2784 error = dpif_execute(ofproto->dpif,
2786 odp_actions.data, odp_actions.size,
2788 ofpbuf_uninit(&odp_actions);
2791 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2792 ofproto->up.name, odp_port, strerror(error));
2797 /* OpenFlow to ODP action translation. */
2799 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2800 struct action_xlate_ctx *ctx);
2801 static void xlate_normal(struct action_xlate_ctx *);
2804 commit_odp_actions(struct action_xlate_ctx *ctx)
2806 const struct flow *flow = &ctx->flow;
2807 struct flow *base = &ctx->base_flow;
2808 struct ofpbuf *odp_actions = ctx->odp_actions;
2810 if (base->tun_id != flow->tun_id) {
2811 nl_msg_put_be64(odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, flow->tun_id);
2812 base->tun_id = flow->tun_id;
2815 if (base->nw_src != flow->nw_src) {
2816 nl_msg_put_be32(odp_actions, ODP_ACTION_ATTR_SET_NW_SRC, flow->nw_src);
2817 base->nw_src = flow->nw_src;
2820 if (base->nw_dst != flow->nw_dst) {
2821 nl_msg_put_be32(odp_actions, ODP_ACTION_ATTR_SET_NW_DST, flow->nw_dst);
2822 base->nw_dst = flow->nw_dst;
2825 if (base->nw_tos != flow->nw_tos) {
2826 nl_msg_put_u8(odp_actions, ODP_ACTION_ATTR_SET_NW_TOS, flow->nw_tos);
2827 base->nw_tos = flow->nw_tos;
2830 if (base->vlan_tci != flow->vlan_tci) {
2831 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2832 nl_msg_put_flag(odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2834 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2835 flow->vlan_tci & ~htons(VLAN_CFI));
2837 base->vlan_tci = flow->vlan_tci;
2840 if (base->tp_src != flow->tp_src) {
2841 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_TP_SRC, flow->tp_src);
2842 base->tp_src = flow->tp_src;
2845 if (base->tp_dst != flow->tp_dst) {
2846 nl_msg_put_be16(odp_actions, ODP_ACTION_ATTR_SET_TP_DST, flow->tp_dst);
2847 base->tp_dst = flow->tp_dst;
2850 if (!eth_addr_equals(base->dl_src, flow->dl_src)) {
2851 nl_msg_put_unspec(odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
2852 flow->dl_src, ETH_ADDR_LEN);
2853 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
2856 if (!eth_addr_equals(base->dl_dst, flow->dl_dst)) {
2857 nl_msg_put_unspec(odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
2858 flow->dl_dst, ETH_ADDR_LEN);
2859 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
2862 if (ctx->base_priority != ctx->priority) {
2863 if (ctx->priority) {
2864 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_SET_PRIORITY,
2867 nl_msg_put_flag(odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2869 ctx->base_priority = ctx->priority;
2874 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2876 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2877 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2880 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2881 /* Forwarding disabled on port. */
2886 * We don't have an ofport record for this port, but it doesn't hurt to
2887 * allow forwarding to it anyhow. Maybe such a port will appear later
2888 * and we're pre-populating the flow table.
2892 commit_odp_actions(ctx);
2893 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2894 ctx->nf_output_iface = ofp_port;
2898 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2900 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2901 struct rule_dpif *rule;
2902 uint16_t old_in_port;
2904 /* Look up a flow with 'in_port' as the input port. Then restore the
2905 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2906 * have surprising behavior). */
2907 old_in_port = ctx->flow.in_port;
2908 ctx->flow.in_port = in_port;
2909 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow);
2910 ctx->flow.in_port = old_in_port;
2912 if (ctx->resubmit_hook) {
2913 ctx->resubmit_hook(ctx, rule);
2918 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2922 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2924 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2925 MAX_RESUBMIT_RECURSION);
2930 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
2932 struct ofport_dpif *ofport;
2934 commit_odp_actions(ctx);
2935 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
2936 uint16_t ofp_port = ofport->up.ofp_port;
2937 if (ofp_port != ctx->flow.in_port && !(ofport->up.opp.config & mask)) {
2938 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT,
2943 ctx->nf_output_iface = NF_OUT_FLOOD;
2947 xlate_output_action__(struct action_xlate_ctx *ctx,
2948 uint16_t port, uint16_t max_len)
2950 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2952 ctx->nf_output_iface = NF_OUT_DROP;
2956 add_output_action(ctx, ctx->flow.in_port);
2959 xlate_table_action(ctx, ctx->flow.in_port);
2965 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
2968 flood_packets(ctx, htonl(0));
2970 case OFPP_CONTROLLER:
2971 commit_odp_actions(ctx);
2972 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_USERSPACE, max_len);
2975 add_output_action(ctx, OFPP_LOCAL);
2980 if (port != ctx->flow.in_port) {
2981 add_output_action(ctx, port);
2986 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2987 ctx->nf_output_iface = NF_OUT_FLOOD;
2988 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2989 ctx->nf_output_iface = prev_nf_output_iface;
2990 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2991 ctx->nf_output_iface != NF_OUT_FLOOD) {
2992 ctx->nf_output_iface = NF_OUT_MULTI;
2997 xlate_output_action(struct action_xlate_ctx *ctx,
2998 const struct ofp_action_output *oao)
3000 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3004 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3005 const struct ofp_action_enqueue *oae)
3007 uint16_t ofp_port, odp_port;
3008 uint32_t ctx_priority, priority;
3011 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3014 /* Fall back to ordinary output action. */
3015 xlate_output_action__(ctx, ntohs(oae->port), 0);
3019 /* Figure out ODP output port. */
3020 ofp_port = ntohs(oae->port);
3021 if (ofp_port == OFPP_IN_PORT) {
3022 ofp_port = ctx->flow.in_port;
3024 odp_port = ofp_port_to_odp_port(ofp_port);
3026 /* Add ODP actions. */
3027 ctx_priority = ctx->priority;
3028 ctx->priority = priority;
3029 add_output_action(ctx, odp_port);
3030 ctx->priority = ctx_priority;
3032 /* Update NetFlow output port. */
3033 if (ctx->nf_output_iface == NF_OUT_DROP) {
3034 ctx->nf_output_iface = odp_port;
3035 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3036 ctx->nf_output_iface = NF_OUT_MULTI;
3041 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3042 const struct nx_action_set_queue *nasq)
3047 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3050 /* Couldn't translate queue to a priority, so ignore. A warning
3051 * has already been logged. */
3055 ctx->priority = priority;
3058 struct xlate_reg_state {
3064 xlate_autopath(struct action_xlate_ctx *ctx,
3065 const struct nx_action_autopath *naa)
3067 uint16_t ofp_port = ntohl(naa->id);
3068 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
3070 if (!port || !port->bundle) {
3071 ofp_port = OFPP_NONE;
3072 } else if (port->bundle->bond) {
3073 /* Autopath does not support VLAN hashing. */
3074 struct ofport_dpif *slave = bond_choose_output_slave(
3075 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3077 ofp_port = slave->up.ofp_port;
3080 autopath_execute(naa, &ctx->flow, ofp_port);
3084 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
3086 struct ofproto_dpif *ofproto = ofproto_;
3087 struct ofport_dpif *port;
3097 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3100 port = get_ofp_port(ofproto, ofp_port);
3101 return port ? port->may_enable : false;
3106 do_xlate_actions(const union ofp_action *in, size_t n_in,
3107 struct action_xlate_ctx *ctx)
3109 const struct ofport_dpif *port;
3110 const union ofp_action *ia;
3113 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3115 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3116 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3117 ? htonl(OFPPC_NO_RECV_STP)
3118 : htonl(OFPPC_NO_RECV))) {
3119 /* Drop this flow. */
3123 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
3124 const struct ofp_action_dl_addr *oada;
3125 const struct nx_action_resubmit *nar;
3126 const struct nx_action_set_tunnel *nast;
3127 const struct nx_action_set_queue *nasq;
3128 const struct nx_action_multipath *nam;
3129 const struct nx_action_autopath *naa;
3130 const struct nx_action_bundle *nab;
3131 enum ofputil_action_code code;
3134 code = ofputil_decode_action_unsafe(ia);
3136 case OFPUTIL_OFPAT_OUTPUT:
3137 xlate_output_action(ctx, &ia->output);
3140 case OFPUTIL_OFPAT_SET_VLAN_VID:
3141 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3142 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3145 case OFPUTIL_OFPAT_SET_VLAN_PCP:
3146 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3147 ctx->flow.vlan_tci |= htons(
3148 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3151 case OFPUTIL_OFPAT_STRIP_VLAN:
3152 ctx->flow.vlan_tci = htons(0);
3155 case OFPUTIL_OFPAT_SET_DL_SRC:
3156 oada = ((struct ofp_action_dl_addr *) ia);
3157 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3160 case OFPUTIL_OFPAT_SET_DL_DST:
3161 oada = ((struct ofp_action_dl_addr *) ia);
3162 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3165 case OFPUTIL_OFPAT_SET_NW_SRC:
3166 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3169 case OFPUTIL_OFPAT_SET_NW_DST:
3170 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3173 case OFPUTIL_OFPAT_SET_NW_TOS:
3174 ctx->flow.nw_tos = ia->nw_tos.nw_tos & IP_DSCP_MASK;
3177 case OFPUTIL_OFPAT_SET_TP_SRC:
3178 ctx->flow.tp_src = ia->tp_port.tp_port;
3181 case OFPUTIL_OFPAT_SET_TP_DST:
3182 ctx->flow.tp_dst = ia->tp_port.tp_port;
3185 case OFPUTIL_OFPAT_ENQUEUE:
3186 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3189 case OFPUTIL_NXAST_RESUBMIT:
3190 nar = (const struct nx_action_resubmit *) ia;
3191 xlate_table_action(ctx, ntohs(nar->in_port));
3194 case OFPUTIL_NXAST_SET_TUNNEL:
3195 nast = (const struct nx_action_set_tunnel *) ia;
3196 tun_id = htonll(ntohl(nast->tun_id));
3197 ctx->flow.tun_id = tun_id;
3200 case OFPUTIL_NXAST_SET_QUEUE:
3201 nasq = (const struct nx_action_set_queue *) ia;
3202 xlate_set_queue_action(ctx, nasq);
3205 case OFPUTIL_NXAST_POP_QUEUE:
3209 case OFPUTIL_NXAST_REG_MOVE:
3210 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
3214 case OFPUTIL_NXAST_REG_LOAD:
3215 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
3219 case OFPUTIL_NXAST_NOTE:
3220 /* Nothing to do. */
3223 case OFPUTIL_NXAST_SET_TUNNEL64:
3224 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
3225 ctx->flow.tun_id = tun_id;
3228 case OFPUTIL_NXAST_MULTIPATH:
3229 nam = (const struct nx_action_multipath *) ia;
3230 multipath_execute(nam, &ctx->flow);
3233 case OFPUTIL_NXAST_AUTOPATH:
3234 naa = (const struct nx_action_autopath *) ia;
3235 xlate_autopath(ctx, naa);
3238 case OFPUTIL_NXAST_BUNDLE:
3239 ctx->ofproto->has_bundle_action = true;
3240 nab = (const struct nx_action_bundle *) ia;
3241 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
3246 case OFPUTIL_NXAST_BUNDLE_LOAD:
3247 ctx->ofproto->has_bundle_action = true;
3248 nab = (const struct nx_action_bundle *) ia;
3249 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
3257 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3258 struct ofproto_dpif *ofproto, const struct flow *flow,
3259 const struct ofpbuf *packet)
3261 ctx->ofproto = ofproto;
3263 ctx->packet = packet;
3264 ctx->resubmit_hook = NULL;
3267 static struct ofpbuf *
3268 xlate_actions(struct action_xlate_ctx *ctx,
3269 const union ofp_action *in, size_t n_in)
3271 COVERAGE_INC(ofproto_dpif_xlate);
3273 ctx->odp_actions = ofpbuf_new(512);
3275 ctx->may_set_up_flow = true;
3276 ctx->nf_output_iface = NF_OUT_DROP;
3279 ctx->base_priority = 0;
3280 ctx->base_flow = ctx->flow;
3281 ctx->base_flow.tun_id = 0;
3283 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3284 ctx->may_set_up_flow = false;
3286 do_xlate_actions(in, n_in, ctx);
3289 /* Check with in-band control to see if we're allowed to set up this
3291 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3292 ctx->odp_actions->data,
3293 ctx->odp_actions->size)) {
3294 ctx->may_set_up_flow = false;
3297 return ctx->odp_actions;
3300 /* OFPP_NORMAL implementation. */
3303 struct ofport_dpif *port;
3308 struct dst builtin[32];
3310 size_t n, allocated;
3313 static void dst_set_init(struct dst_set *);
3314 static void dst_set_add(struct dst_set *, const struct dst *);
3315 static void dst_set_free(struct dst_set *);
3317 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3320 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3321 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3323 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3324 : in_bundle->vlan >= 0 ? in_bundle->vlan
3325 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3326 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3328 dst->port = (!out_bundle->bond
3329 ? ofbundle_get_a_port(out_bundle)
3330 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3331 dst->vlan, &ctx->tags));
3333 return dst->port != NULL;
3337 mirror_mask_ffs(mirror_mask_t mask)
3339 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3344 dst_set_init(struct dst_set *set)
3346 set->dsts = set->builtin;
3348 set->allocated = ARRAY_SIZE(set->builtin);
3352 dst_set_add(struct dst_set *set, const struct dst *dst)
3354 if (set->n >= set->allocated) {
3355 size_t new_allocated;
3356 struct dst *new_dsts;
3358 new_allocated = set->allocated * 2;
3359 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3360 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3364 set->dsts = new_dsts;
3365 set->allocated = new_allocated;
3367 set->dsts[set->n++] = *dst;
3371 dst_set_free(struct dst_set *set)
3373 if (set->dsts != set->builtin) {
3379 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3382 for (i = 0; i < set->n; i++) {
3383 if (set->dsts[i].vlan == test->vlan
3384 && set->dsts[i].port == test->port) {
3392 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3394 return (bundle->vlan < 0
3395 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
3399 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3401 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3404 /* Returns an arbitrary interface within 'bundle'. */
3405 static struct ofport_dpif *
3406 ofbundle_get_a_port(const struct ofbundle *bundle)
3408 return CONTAINER_OF(list_front(&bundle->ports),
3409 struct ofport_dpif, bundle_node);
3413 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3414 const struct ofbundle *in_bundle,
3415 const struct ofbundle *out_bundle, struct dst_set *set)
3419 if (out_bundle == OFBUNDLE_FLOOD) {
3420 struct ofbundle *bundle;
3422 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3423 if (bundle != in_bundle
3424 && ofbundle_includes_vlan(bundle, vlan)
3425 && bundle->floodable
3426 && !bundle->mirror_out
3427 && set_dst(ctx, &dst, in_bundle, bundle)) {
3428 dst_set_add(set, &dst);
3431 ctx->nf_output_iface = NF_OUT_FLOOD;
3432 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3433 dst_set_add(set, &dst);
3434 ctx->nf_output_iface = dst.port->odp_port;
3439 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3441 return !m->vlans || bitmap_is_set(m->vlans, vlan);
3444 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
3445 * to a VLAN. In general most packets may be mirrored but we want to drop
3446 * protocols that may confuse switches. */
3448 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
3450 /* If you change this function's behavior, please update corresponding
3451 * documentation in vswitch.xml at the same time. */
3452 if (dst[0] != 0x01) {
3453 /* All the currently banned MACs happen to start with 01 currently, so
3454 * this is a quick way to eliminate most of the good ones. */
3456 if (eth_addr_is_reserved(dst)) {
3457 /* Drop STP, IEEE pause frames, and other reserved protocols
3458 * (01-80-c2-00-00-0x). */
3462 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
3464 if ((dst[3] & 0xfe) == 0xcc &&
3465 (dst[4] & 0xfe) == 0xcc &&
3466 (dst[5] & 0xfe) == 0xcc) {
3467 /* Drop the following protocols plus others following the same
3470 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
3471 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
3472 STP Uplink Fast (01-00-0c-cd-cd-cd) */
3476 if (!(dst[3] | dst[4] | dst[5])) {
3477 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
3486 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3487 uint16_t vlan, const struct ofbundle *in_bundle,
3488 struct dst_set *set)
3490 struct ofproto_dpif *ofproto = ctx->ofproto;
3491 mirror_mask_t mirrors;
3495 mirrors = in_bundle->src_mirrors;
3496 for (i = 0; i < set->n; i++) {
3497 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3504 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3505 if (flow_vlan == 0) {
3506 flow_vlan = OFP_VLAN_NONE;
3510 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3511 if (vlan_is_mirrored(m, vlan)) {
3515 if (set_dst(ctx, &dst, in_bundle, m->out)
3516 && !dst_is_duplicate(set, &dst)) {
3517 dst_set_add(set, &dst);
3519 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
3520 struct ofbundle *bundle;
3522 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3523 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3524 && set_dst(ctx, &dst, in_bundle, bundle))
3526 if (bundle->vlan < 0) {
3527 dst.vlan = m->out_vlan;
3529 if (dst_is_duplicate(set, &dst)) {
3533 /* Use the vlan tag on the original flow instead of
3534 * the one passed in the vlan parameter. This ensures
3535 * that we compare the vlan from before any implicit
3536 * tagging tags place. This is necessary because
3537 * dst->vlan is the final vlan, after removing implicit
3539 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3540 /* Don't send out input port on same VLAN. */
3543 dst_set_add(set, &dst);
3548 mirrors &= mirrors - 1;
3553 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3554 const struct ofbundle *in_bundle,
3555 const struct ofbundle *out_bundle)
3557 uint16_t initial_vlan, cur_vlan;
3558 const struct dst *dst;
3562 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3563 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3565 /* Output all the packets we can without having to change the VLAN. */
3566 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3567 if (initial_vlan == 0) {
3568 initial_vlan = OFP_VLAN_NONE;
3570 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3571 if (dst->vlan != initial_vlan) {
3574 nl_msg_put_u32(ctx->odp_actions,
3575 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3578 /* Then output the rest. */
3579 cur_vlan = initial_vlan;
3580 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3581 if (dst->vlan == initial_vlan) {
3584 if (dst->vlan != cur_vlan) {
3585 if (dst->vlan == OFP_VLAN_NONE) {
3586 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3589 tci = htons(dst->vlan & VLAN_VID_MASK);
3590 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3591 nl_msg_put_be16(ctx->odp_actions,
3592 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3594 cur_vlan = dst->vlan;
3596 nl_msg_put_u32(ctx->odp_actions,
3597 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3603 /* Returns the effective vlan of a packet, taking into account both the
3604 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3605 * the packet is untagged and -1 indicates it has an invalid header and
3606 * should be dropped. */
3608 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3609 struct ofbundle *in_bundle, bool have_packet)
3611 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3612 if (in_bundle->vlan >= 0) {
3615 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3616 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3617 "packet received on port %s configured with "
3618 "implicit VLAN %"PRIu16,
3619 ofproto->up.name, vlan,
3620 in_bundle->name, in_bundle->vlan);
3624 vlan = in_bundle->vlan;
3626 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3628 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3629 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3630 "packet received on port %s not configured for "
3632 ofproto->up.name, vlan, in_bundle->name, vlan);
3641 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3642 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3643 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3645 is_gratuitous_arp(const struct flow *flow)
3647 return (flow->dl_type == htons(ETH_TYPE_ARP)
3648 && eth_addr_is_broadcast(flow->dl_dst)
3649 && (flow->nw_proto == ARP_OP_REPLY
3650 || (flow->nw_proto == ARP_OP_REQUEST
3651 && flow->nw_src == flow->nw_dst)));
3655 update_learning_table(struct ofproto_dpif *ofproto,
3656 const struct flow *flow, int vlan,
3657 struct ofbundle *in_bundle)
3659 struct mac_entry *mac;
3661 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3665 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3666 if (is_gratuitous_arp(flow)) {
3667 /* We don't want to learn from gratuitous ARP packets that are
3668 * reflected back over bond slaves so we lock the learning table. */
3669 if (!in_bundle->bond) {
3670 mac_entry_set_grat_arp_lock(mac);
3671 } else if (mac_entry_is_grat_arp_locked(mac)) {
3676 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3677 /* The log messages here could actually be useful in debugging,
3678 * so keep the rate limit relatively high. */
3679 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3680 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3681 "on port %s in VLAN %d",
3682 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3683 in_bundle->name, vlan);
3685 mac->port.p = in_bundle;
3686 tag_set_add(&ofproto->revalidate_set,
3687 mac_learning_changed(ofproto->ml, mac));
3691 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3692 * dropped. Returns true if they may be forwarded, false if they should be
3695 * If 'have_packet' is true, it indicates that the caller is processing a
3696 * received packet. If 'have_packet' is false, then the caller is just
3697 * revalidating an existing flow because configuration has changed. Either
3698 * way, 'have_packet' only affects logging (there is no point in logging errors
3699 * during revalidation).
3701 * Sets '*in_portp' to the input port. This will be a null pointer if
3702 * flow->in_port does not designate a known input port (in which case
3703 * is_admissible() returns false).
3705 * When returning true, sets '*vlanp' to the effective VLAN of the input
3706 * packet, as returned by flow_get_vlan().
3708 * May also add tags to '*tags', although the current implementation only does
3709 * so in one special case.
3712 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3714 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3716 struct ofport_dpif *in_port;
3717 struct ofbundle *in_bundle;
3720 /* Find the port and bundle for the received packet. */
3721 in_port = get_ofp_port(ofproto, flow->in_port);
3722 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3723 if (!in_port || !in_bundle) {
3724 /* No interface? Something fishy... */
3726 /* Odd. A few possible reasons here:
3728 * - We deleted a port but there are still a few packets queued up
3731 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3732 * we don't know about.
3734 * - Packet arrived on the local port but the local port is not
3737 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3739 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3741 ofproto->up.name, flow->in_port);
3745 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3750 /* Drop frames for reserved multicast addresses
3751 * only if forward_bpdu option is absent. */
3752 if (eth_addr_is_reserved(flow->dl_dst) &&
3753 !ofproto->up.forward_bpdu) {
3757 /* Drop frames on bundles reserved for mirroring. */
3758 if (in_bundle->mirror_out) {
3760 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3761 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3762 "%s, which is reserved exclusively for mirroring",
3763 ofproto->up.name, in_bundle->name);
3768 if (in_bundle->bond) {
3769 struct mac_entry *mac;
3771 switch (bond_check_admissibility(in_bundle->bond, in_port,
3772 flow->dl_dst, tags)) {
3779 case BV_DROP_IF_MOVED:
3780 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3781 if (mac && mac->port.p != in_bundle &&
3782 (!is_gratuitous_arp(flow)
3783 || mac_entry_is_grat_arp_locked(mac))) {
3794 xlate_normal(struct action_xlate_ctx *ctx)
3796 struct ofbundle *in_bundle;
3797 struct ofbundle *out_bundle;
3798 struct mac_entry *mac;
3801 /* Check whether we should drop packets in this flow. */
3802 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3803 &ctx->tags, &vlan, &in_bundle)) {
3808 /* Learn source MAC (but don't try to learn from revalidation). */
3810 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3813 /* Determine output bundle. */
3814 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3817 out_bundle = mac->port.p;
3818 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3819 /* If we are revalidating but don't have a learning entry then eject
3820 * the flow. Installing a flow that floods packets opens up a window
3821 * of time where we could learn from a packet reflected on a bond and
3822 * blackhole packets before the learning table is updated to reflect
3823 * the correct port. */
3824 ctx->may_set_up_flow = false;
3827 out_bundle = OFBUNDLE_FLOOD;
3830 /* Don't send packets out their input bundles. */
3831 if (in_bundle == out_bundle) {
3837 compose_actions(ctx, vlan, in_bundle, out_bundle);
3842 get_drop_frags(struct ofproto *ofproto_)
3844 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3847 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3852 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3854 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3856 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3860 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3861 const struct flow *flow,
3862 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3864 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3867 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3868 ofproto->max_ports);
3870 struct odputil_keybuf keybuf;
3871 struct action_xlate_ctx ctx;
3872 struct ofpbuf *odp_actions;
3875 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3876 odp_flow_key_from_flow(&key, flow);
3878 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3879 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3880 dpif_execute(ofproto->dpif, key.data, key.size,
3881 odp_actions->data, odp_actions->size, packet);
3882 ofpbuf_delete(odp_actions);
3888 get_netflow_ids(const struct ofproto *ofproto_,
3889 uint8_t *engine_type, uint8_t *engine_id)
3891 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3893 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3896 static struct ofproto_dpif *
3897 ofproto_dpif_lookup(const char *name)
3899 struct ofproto *ofproto = ofproto_lookup(name);
3900 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3901 ? ofproto_dpif_cast(ofproto)
3906 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3907 const char *args, void *aux OVS_UNUSED)
3909 struct ds ds = DS_EMPTY_INITIALIZER;
3910 const struct ofproto_dpif *ofproto;
3911 const struct mac_entry *e;
3913 ofproto = ofproto_dpif_lookup(args);
3915 unixctl_command_reply(conn, 501, "no such bridge");
3919 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3920 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3921 struct ofbundle *bundle = e->port.p;
3922 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
3923 ofbundle_get_a_port(bundle)->odp_port,
3924 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3926 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3930 struct ofproto_trace {
3931 struct action_xlate_ctx ctx;
3937 trace_format_rule(struct ds *result, int level, const struct rule *rule)
3939 ds_put_char_multiple(result, '\t', level);
3941 ds_put_cstr(result, "No match\n");
3945 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
3946 ntohll(rule->flow_cookie));
3947 cls_rule_format(&rule->cr, result);
3948 ds_put_char(result, '\n');
3950 ds_put_char_multiple(result, '\t', level);
3951 ds_put_cstr(result, "OpenFlow ");
3952 ofp_print_actions(result, rule->actions, rule->n_actions);
3953 ds_put_char(result, '\n');
3957 trace_format_flow(struct ds *result, int level, const char *title,
3958 struct ofproto_trace *trace)
3960 ds_put_char_multiple(result, '\t', level);
3961 ds_put_format(result, "%s: ", title);
3962 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3963 ds_put_cstr(result, "unchanged");
3965 flow_format(result, &trace->ctx.flow);
3966 trace->flow = trace->ctx.flow;
3968 ds_put_char(result, '\n');
3972 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3974 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3975 struct ds *result = trace->result;
3977 ds_put_char(result, '\n');
3978 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
3979 trace_format_rule(result, ctx->recurse + 1, &rule->up);
3983 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
3984 void *aux OVS_UNUSED)
3986 char *dpname, *in_port_s, *tun_id_s, *packet_s;
3987 char *args = xstrdup(args_);
3988 char *save_ptr = NULL;
3989 struct ofproto_dpif *ofproto;
3990 struct ofpbuf packet;
3991 struct rule_dpif *rule;
3998 ofpbuf_init(&packet, strlen(args) / 2);
4001 dpname = strtok_r(args, " ", &save_ptr);
4002 tun_id_s = strtok_r(NULL, " ", &save_ptr);
4003 in_port_s = strtok_r(NULL, " ", &save_ptr);
4004 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
4005 if (!dpname || !in_port_s || !packet_s) {
4006 unixctl_command_reply(conn, 501, "Bad command syntax");
4010 ofproto = ofproto_dpif_lookup(dpname);
4012 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
4017 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
4018 in_port = ofp_port_to_odp_port(atoi(in_port_s));
4020 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
4021 packet_s += strspn(packet_s, " ");
4022 if (*packet_s != '\0') {
4023 unixctl_command_reply(conn, 501, "Trailing garbage in command");
4026 if (packet.size < ETH_HEADER_LEN) {
4027 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
4031 ds_put_cstr(&result, "Packet: ");
4032 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
4033 ds_put_cstr(&result, s);
4036 flow_extract(&packet, tun_id, in_port, &flow);
4037 ds_put_cstr(&result, "Flow: ");
4038 flow_format(&result, &flow);
4039 ds_put_char(&result, '\n');
4041 rule = rule_dpif_lookup(ofproto, &flow);
4042 trace_format_rule(&result, 0, &rule->up);
4044 struct ofproto_trace trace;
4045 struct ofpbuf *odp_actions;
4047 trace.result = &result;
4049 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
4050 trace.ctx.resubmit_hook = trace_resubmit;
4051 odp_actions = xlate_actions(&trace.ctx,
4052 rule->up.actions, rule->up.n_actions);
4054 ds_put_char(&result, '\n');
4055 trace_format_flow(&result, 0, "Final flow", &trace);
4056 ds_put_cstr(&result, "Datapath actions: ");
4057 format_odp_actions(&result, odp_actions->data, odp_actions->size);
4058 ofpbuf_delete(odp_actions);
4061 unixctl_command_reply(conn, 200, ds_cstr(&result));
4064 ds_destroy(&result);
4065 ofpbuf_uninit(&packet);
4070 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
4071 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4074 unixctl_command_reply(conn, 200, NULL);
4078 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
4079 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
4082 unixctl_command_reply(conn, 200, NULL);
4086 ofproto_dpif_unixctl_init(void)
4088 static bool registered;
4094 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
4095 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
4097 unixctl_command_register("ofproto/clog", ofproto_dpif_clog, NULL);
4098 unixctl_command_register("ofproto/unclog", ofproto_dpif_unclog, NULL);
4101 const struct ofproto_class ofproto_dpif_class = {
4128 port_is_lacp_current,
4129 NULL, /* rule_choose_table */
4136 rule_modify_actions,
4149 is_mirror_output_bundle,
4150 forward_bpdu_changed,