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/private.h"
25 #include "byte-order.h"
30 #include "dynamic-string.h"
31 #include "fail-open.h"
34 #include "mac-learning.h"
35 #include "multipath.h"
42 #include "ofp-print.h"
43 #include "ofproto-sflow.h"
44 #include "poll-loop.h"
46 #include "unaligned.h"
48 #include "vlan-bitmap.h"
51 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
53 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
54 COVERAGE_DEFINE(ofproto_dpif_expired);
55 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
56 COVERAGE_DEFINE(ofproto_dpif_xlate);
57 COVERAGE_DEFINE(facet_changed_rule);
58 COVERAGE_DEFINE(facet_invalidated);
59 COVERAGE_DEFINE(facet_revalidate);
60 COVERAGE_DEFINE(facet_unexpected);
62 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
63 * flow translation. */
64 #define MAX_RESUBMIT_RECURSION 16
72 long long int used; /* Time last used; time created if not used. */
76 * - Do include packets and bytes from facets that have been deleted or
77 * whose own statistics have been folded into the rule.
79 * - Do include packets and bytes sent "by hand" that were accounted to
80 * the rule without any facet being involved (this is a rare corner
81 * case in rule_execute()).
83 * - Do not include packet or bytes that can be obtained from any facet's
84 * packet_count or byte_count member or that can be obtained from the
85 * datapath by, e.g., dpif_flow_get() for any facet.
87 uint64_t packet_count; /* Number of packets received. */
88 uint64_t byte_count; /* Number of bytes received. */
90 struct list facets; /* List of "struct facet"s. */
93 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
95 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
98 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *ofproto,
99 const struct flow *flow);
101 #define MAX_MIRRORS 32
102 typedef uint32_t mirror_mask_t;
103 #define MIRROR_MASK_C(X) UINT32_C(X)
104 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
106 struct ofproto_dpif *ofproto; /* Owning ofproto. */
107 size_t idx; /* In ofproto's "mirrors" array. */
108 void *aux; /* Key supplied by ofproto's client. */
109 char *name; /* Identifier for log messages. */
111 /* Selection criteria. */
112 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
113 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
114 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
116 /* Output (mutually exclusive). */
117 struct ofbundle *out; /* Output port or NULL. */
118 int out_vlan; /* Output VLAN or -1. */
121 static void mirror_destroy(struct ofmirror *);
123 /* A group of one or more OpenFlow ports. */
124 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
126 struct ofproto_dpif *ofproto; /* Owning ofproto. */
127 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
128 void *aux; /* Key supplied by ofproto's client. */
129 char *name; /* Identifier for log messages. */
132 struct list ports; /* Contains "struct ofport"s. */
133 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
134 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
135 * NULL if all VLANs are trunked. */
136 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
137 struct bond *bond; /* Nonnull iff more than one port. */
140 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
142 /* Port mirroring info. */
143 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
144 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
145 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
148 static void bundle_remove(struct ofport *);
149 static void bundle_destroy(struct ofbundle *);
150 static void bundle_del_port(struct ofport_dpif *);
151 static void bundle_run(struct ofbundle *);
152 static void bundle_wait(struct ofbundle *);
154 struct action_xlate_ctx {
155 /* action_xlate_ctx_init() initializes these members. */
158 struct ofproto_dpif *ofproto;
160 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
161 * this flow when actions change header fields. */
164 /* The packet corresponding to 'flow', or a null pointer if we are
165 * revalidating without a packet to refer to. */
166 const struct ofpbuf *packet;
168 /* If nonnull, called just before executing a resubmit action.
170 * This is normally null so the client has to set it manually after
171 * calling action_xlate_ctx_init(). */
172 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
174 /* xlate_actions() initializes and uses these members. The client might want
175 * to look at them after it returns. */
177 struct ofpbuf *odp_actions; /* Datapath actions. */
178 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
179 bool may_set_up_flow; /* True ordinarily; false if the actions must
180 * be reassessed for every packet. */
181 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
183 /* xlate_actions() initializes and uses these members, but the client has no
184 * reason to look at them. */
186 int recurse; /* Recursion level, via xlate_table_action. */
187 int last_pop_priority; /* Offset in 'odp_actions' just past most
188 * recent ODP_ACTION_ATTR_SET_PRIORITY. */
191 static void action_xlate_ctx_init(struct action_xlate_ctx *,
192 struct ofproto_dpif *, const struct flow *,
193 const struct ofpbuf *);
194 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
195 const union ofp_action *in, size_t n_in);
197 /* An exact-match instantiation of an OpenFlow flow. */
199 long long int used; /* Time last used; time created if not used. */
203 * - Do include packets and bytes sent "by hand", e.g. with
206 * - Do include packets and bytes that were obtained from the datapath
207 * when a flow was deleted (e.g. dpif_flow_del()) or when its
208 * statistics were reset (e.g. dpif_flow_put() with
209 * DPIF_FP_ZERO_STATS).
211 * - Do not include any packets or bytes that can currently be obtained
212 * from the datapath by, e.g., dpif_flow_get().
214 uint64_t packet_count; /* Number of packets received. */
215 uint64_t byte_count; /* Number of bytes received. */
217 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
218 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
220 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
221 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
222 long long int rs_used; /* Used time pushed to resubmit children. */
224 /* Number of bytes passed to account_cb. This may include bytes that can
225 * currently obtained from the datapath (thus, it can be greater than
227 uint64_t accounted_bytes;
229 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
230 struct list list_node; /* In owning rule's 'facets' list. */
231 struct rule_dpif *rule; /* Owning rule. */
232 struct flow flow; /* Exact-match flow. */
233 bool installed; /* Installed in datapath? */
234 bool may_install; /* True ordinarily; false if actions must
235 * be reassessed for every packet. */
236 size_t actions_len; /* Number of bytes in actions[]. */
237 struct nlattr *actions; /* Datapath actions. */
238 tag_type tags; /* Tags. */
239 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
242 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
243 const struct ofpbuf *packet);
244 static void facet_remove(struct ofproto_dpif *, struct facet *);
245 static void facet_free(struct facet *);
247 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
248 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
249 const struct flow *);
250 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
252 static void facet_execute(struct ofproto_dpif *, struct facet *,
253 struct ofpbuf *packet);
255 static int facet_put__(struct ofproto_dpif *, struct facet *,
256 const struct nlattr *actions, size_t actions_len,
257 struct dpif_flow_stats *);
258 static void facet_install(struct ofproto_dpif *, struct facet *,
260 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
261 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
263 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
264 const struct ofpbuf *packet);
265 static void facet_update_time(struct ofproto_dpif *, struct facet *,
267 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
268 const struct dpif_flow_stats *);
269 static void facet_push_stats(struct facet *);
270 static void facet_account(struct ofproto_dpif *, struct facet *,
271 uint64_t extra_bytes);
273 static bool facet_is_controller_flow(struct facet *);
275 static void flow_push_stats(const struct rule_dpif *,
276 struct flow *, uint64_t packets, uint64_t bytes,
283 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
284 struct list bundle_node; /* In struct ofbundle's "ports" list. */
285 struct cfm *cfm; /* Connectivity Fault Management, if any. */
286 tag_type tag; /* Tag associated with this port. */
287 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
290 static struct ofport_dpif *
291 ofport_dpif_cast(const struct ofport *ofport)
293 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
294 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
297 static void port_run(struct ofport_dpif *);
298 static void port_wait(struct ofport_dpif *);
299 static int set_cfm(struct ofport *, const struct cfm *,
300 const uint16_t *remote_mps, size_t n_remote_mps);
302 struct ofproto_dpif {
311 struct netflow *netflow;
312 struct ofproto_sflow *sflow;
313 struct hmap bundles; /* Contains "struct ofbundle"s. */
314 struct mac_learning *ml;
315 struct ofmirror *mirrors[MAX_MIRRORS];
316 bool has_bonded_bundles;
319 struct timer next_expiration;
323 bool need_revalidate;
324 struct tag_set revalidate_set;
327 static void ofproto_dpif_unixctl_init(void);
329 static struct ofproto_dpif *
330 ofproto_dpif_cast(const struct ofproto *ofproto)
332 assert(ofproto->ofproto_class == &ofproto_dpif_class);
333 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
336 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
338 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
341 /* Packet processing. */
342 static void update_learning_table(struct ofproto_dpif *,
343 const struct flow *, int vlan,
345 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
346 bool have_packet, tag_type *, int *vlanp,
347 struct ofbundle **in_bundlep);
348 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
350 /* Flow expiration. */
351 static int expire(struct ofproto_dpif *);
354 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
355 const struct ofpbuf *packet);
357 /* Global variables. */
358 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
360 /* Factory functions. */
363 enumerate_types(struct sset *types)
365 dp_enumerate_types(types);
369 enumerate_names(const char *type, struct sset *names)
371 return dp_enumerate_names(type, names);
375 del(const char *type, const char *name)
380 error = dpif_open(name, type, &dpif);
382 error = dpif_delete(dpif);
388 /* Basic life-cycle. */
390 static struct ofproto *
393 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
398 dealloc(struct ofproto *ofproto_)
400 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
405 construct(struct ofproto *ofproto_)
407 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
408 const char *name = ofproto->up.name;
412 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
414 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
418 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
419 ofproto->n_matches = 0;
421 error = dpif_recv_set_mask(ofproto->dpif,
422 ((1u << DPIF_UC_MISS) |
423 (1u << DPIF_UC_ACTION) |
424 (1u << DPIF_UC_SAMPLE)));
426 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
427 dpif_close(ofproto->dpif);
430 dpif_flow_flush(ofproto->dpif);
431 dpif_recv_purge(ofproto->dpif);
433 ofproto->netflow = NULL;
434 ofproto->sflow = NULL;
435 hmap_init(&ofproto->bundles);
436 ofproto->ml = mac_learning_create();
437 for (i = 0; i < MAX_MIRRORS; i++) {
438 ofproto->mirrors[i] = NULL;
440 ofproto->has_bonded_bundles = false;
442 timer_set_duration(&ofproto->next_expiration, 1000);
444 hmap_init(&ofproto->facets);
445 ofproto->need_revalidate = false;
446 tag_set_init(&ofproto->revalidate_set);
448 ofproto->up.tables = xmalloc(sizeof *ofproto->up.tables);
449 classifier_init(&ofproto->up.tables[0]);
450 ofproto->up.n_tables = 1;
452 ofproto_dpif_unixctl_init();
458 destruct(struct ofproto *ofproto_)
460 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
463 for (i = 0; i < MAX_MIRRORS; i++) {
464 mirror_destroy(ofproto->mirrors[i]);
467 netflow_destroy(ofproto->netflow);
468 ofproto_sflow_destroy(ofproto->sflow);
469 hmap_destroy(&ofproto->bundles);
470 mac_learning_destroy(ofproto->ml);
472 hmap_destroy(&ofproto->facets);
474 dpif_close(ofproto->dpif);
478 run(struct ofproto *ofproto_)
480 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
481 struct ofport_dpif *ofport;
482 struct ofbundle *bundle;
485 dpif_run(ofproto->dpif);
487 for (i = 0; i < 50; i++) {
488 struct dpif_upcall packet;
491 error = dpif_recv(ofproto->dpif, &packet);
493 if (error == ENODEV) {
494 /* Datapath destroyed. */
500 handle_upcall(ofproto, &packet);
503 if (timer_expired(&ofproto->next_expiration)) {
504 int delay = expire(ofproto);
505 timer_set_duration(&ofproto->next_expiration, delay);
508 if (ofproto->netflow) {
509 netflow_run(ofproto->netflow);
511 if (ofproto->sflow) {
512 ofproto_sflow_run(ofproto->sflow);
515 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
518 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
522 /* Now revalidate if there's anything to do. */
523 if (ofproto->need_revalidate
524 || !tag_set_is_empty(&ofproto->revalidate_set)) {
525 struct tag_set revalidate_set = ofproto->revalidate_set;
526 bool revalidate_all = ofproto->need_revalidate;
527 struct facet *facet, *next;
529 /* Clear the revalidation flags. */
530 tag_set_init(&ofproto->revalidate_set);
531 ofproto->need_revalidate = false;
533 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
535 || tag_set_intersects(&revalidate_set, facet->tags)) {
536 facet_revalidate(ofproto, facet);
545 wait(struct ofproto *ofproto_)
547 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
548 struct ofport_dpif *ofport;
549 struct ofbundle *bundle;
551 dpif_wait(ofproto->dpif);
552 dpif_recv_wait(ofproto->dpif);
553 if (ofproto->sflow) {
554 ofproto_sflow_wait(ofproto->sflow);
556 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
557 poll_immediate_wake();
559 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
562 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
565 if (ofproto->need_revalidate) {
566 /* Shouldn't happen, but if it does just go around again. */
567 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
568 poll_immediate_wake();
570 timer_wait(&ofproto->next_expiration);
575 flush(struct ofproto *ofproto_)
577 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
578 struct facet *facet, *next_facet;
580 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
581 /* Mark the facet as not installed so that facet_remove() doesn't
582 * bother trying to uninstall it. There is no point in uninstalling it
583 * individually since we are about to blow away all the facets with
584 * dpif_flow_flush(). */
585 facet->installed = false;
586 facet->dp_packet_count = 0;
587 facet->dp_byte_count = 0;
588 facet_remove(ofproto, facet);
590 dpif_flow_flush(ofproto->dpif);
594 get_features(struct ofproto *ofproto_ OVS_UNUSED,
595 bool *arp_match_ip, uint32_t *actions)
597 *arp_match_ip = true;
598 *actions = ((1u << OFPAT_OUTPUT) |
599 (1u << OFPAT_SET_VLAN_VID) |
600 (1u << OFPAT_SET_VLAN_PCP) |
601 (1u << OFPAT_STRIP_VLAN) |
602 (1u << OFPAT_SET_DL_SRC) |
603 (1u << OFPAT_SET_DL_DST) |
604 (1u << OFPAT_SET_NW_SRC) |
605 (1u << OFPAT_SET_NW_DST) |
606 (1u << OFPAT_SET_NW_TOS) |
607 (1u << OFPAT_SET_TP_SRC) |
608 (1u << OFPAT_SET_TP_DST) |
609 (1u << OFPAT_ENQUEUE));
613 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
615 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
618 strcpy(ots->name, "classifier");
620 dpif_get_dp_stats(ofproto->dpif, &s);
621 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
622 put_32aligned_be64(&ots->matched_count,
623 htonll(s.n_hit + ofproto->n_matches));
627 set_netflow(struct ofproto *ofproto_,
628 const struct netflow_options *netflow_options)
630 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
632 if (netflow_options) {
633 if (!ofproto->netflow) {
634 ofproto->netflow = netflow_create();
636 return netflow_set_options(ofproto->netflow, netflow_options);
638 netflow_destroy(ofproto->netflow);
639 ofproto->netflow = NULL;
644 static struct ofport *
647 struct ofport_dpif *port = xmalloc(sizeof *port);
652 port_dealloc(struct ofport *port_)
654 struct ofport_dpif *port = ofport_dpif_cast(port_);
659 port_construct(struct ofport *port_)
661 struct ofport_dpif *port = ofport_dpif_cast(port_);
662 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
664 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
667 port->tag = tag_create_random();
669 if (ofproto->sflow) {
670 ofproto_sflow_add_port(ofproto->sflow, port->odp_port,
671 netdev_get_name(port->up.netdev));
678 port_destruct(struct ofport *port_)
680 struct ofport_dpif *port = ofport_dpif_cast(port_);
681 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
683 bundle_remove(port_);
684 set_cfm(port_, NULL, NULL, 0);
685 if (ofproto->sflow) {
686 ofproto_sflow_del_port(ofproto->sflow, port->odp_port);
691 port_modified(struct ofport *port_)
693 struct ofport_dpif *port = ofport_dpif_cast(port_);
695 if (port->bundle && port->bundle->bond) {
696 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
701 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
703 struct ofport_dpif *port = ofport_dpif_cast(port_);
704 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
705 ovs_be32 changed = old_config ^ port->up.opp.config;
707 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
708 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
709 ofproto->need_revalidate = true;
714 set_sflow(struct ofproto *ofproto_,
715 const struct ofproto_sflow_options *sflow_options)
717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
718 struct ofproto_sflow *os = ofproto->sflow;
721 struct ofport_dpif *ofport;
723 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
724 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
725 ofproto_sflow_add_port(os, ofport->odp_port,
726 netdev_get_name(ofport->up.netdev));
729 ofproto_sflow_set_options(os, sflow_options);
731 ofproto_sflow_destroy(os);
732 ofproto->sflow = NULL;
738 set_cfm(struct ofport *ofport_, const struct cfm *cfm,
739 const uint16_t *remote_mps, size_t n_remote_mps)
741 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
748 ofport->cfm = cfm_create();
751 ofport->cfm->mpid = cfm->mpid;
752 ofport->cfm->interval = cfm->interval;
753 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
755 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
757 if (cfm_configure(ofport->cfm)) {
763 cfm_destroy(ofport->cfm);
769 get_cfm(struct ofport *ofport_, const struct cfm **cfmp)
771 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
778 /* Expires all MAC learning entries associated with 'port' and forces ofproto
779 * to revalidate every flow. */
781 bundle_flush_macs(struct ofbundle *bundle)
783 struct ofproto_dpif *ofproto = bundle->ofproto;
784 struct mac_learning *ml = ofproto->ml;
785 struct mac_entry *mac, *next_mac;
787 ofproto->need_revalidate = true;
788 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
789 if (mac->port.p == bundle) {
790 mac_learning_expire(ml, mac);
795 static struct ofbundle *
796 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
798 struct ofbundle *bundle;
800 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
802 if (bundle->aux == aux) {
809 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
810 * ones that are found to 'bundles'. */
812 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
813 void **auxes, size_t n_auxes,
814 struct hmapx *bundles)
819 for (i = 0; i < n_auxes; i++) {
820 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
822 hmapx_add(bundles, bundle);
828 bundle_del_port(struct ofport_dpif *port)
830 struct ofbundle *bundle = port->bundle;
832 bundle->ofproto->need_revalidate = true;
834 list_remove(&port->bundle_node);
838 lacp_slave_unregister(bundle->lacp, port);
841 bond_slave_unregister(bundle->bond, port);
844 bundle->floodable = true;
845 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
846 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
847 bundle->floodable = false;
853 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
854 struct lacp_slave_settings *lacp,
855 uint32_t bond_stable_id)
857 struct ofport_dpif *port;
859 port = get_ofp_port(bundle->ofproto, ofp_port);
864 if (port->bundle != bundle) {
865 bundle->ofproto->need_revalidate = true;
867 bundle_del_port(port);
870 port->bundle = bundle;
871 list_push_back(&bundle->ports, &port->bundle_node);
872 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
873 bundle->floodable = false;
877 lacp_slave_register(bundle->lacp, port, lacp);
880 port->bond_stable_id = bond_stable_id;
886 bundle_destroy(struct ofbundle *bundle)
888 struct ofproto_dpif *ofproto;
889 struct ofport_dpif *port, *next_port;
896 ofproto = bundle->ofproto;
897 for (i = 0; i < MAX_MIRRORS; i++) {
898 struct ofmirror *m = ofproto->mirrors[i];
900 if (m->out == bundle) {
902 } else if (hmapx_find_and_delete(&m->srcs, bundle)
903 || hmapx_find_and_delete(&m->dsts, bundle)) {
904 ofproto->need_revalidate = true;
909 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
910 bundle_del_port(port);
913 bundle_flush_macs(bundle);
914 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
916 free(bundle->trunks);
917 lacp_destroy(bundle->lacp);
918 bond_destroy(bundle->bond);
923 bundle_set(struct ofproto *ofproto_, void *aux,
924 const struct ofproto_bundle_settings *s)
926 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
927 bool need_flush = false;
928 const unsigned long *trunks;
929 struct ofport_dpif *port;
930 struct ofbundle *bundle;
935 bundle_destroy(bundle_lookup(ofproto, aux));
939 assert(s->n_slaves == 1 || s->bond != NULL);
940 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
942 bundle = bundle_lookup(ofproto, aux);
944 bundle = xmalloc(sizeof *bundle);
946 bundle->ofproto = ofproto;
947 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
948 hash_pointer(aux, 0));
952 list_init(&bundle->ports);
954 bundle->trunks = NULL;
958 bundle->floodable = true;
960 bundle->src_mirrors = 0;
961 bundle->dst_mirrors = 0;
962 bundle->mirror_out = 0;
965 if (!bundle->name || strcmp(s->name, bundle->name)) {
967 bundle->name = xstrdup(s->name);
973 bundle->lacp = lacp_create();
975 lacp_configure(bundle->lacp, s->lacp);
977 lacp_destroy(bundle->lacp);
981 /* Update set of ports. */
983 for (i = 0; i < s->n_slaves; i++) {
984 if (!bundle_add_port(bundle, s->slaves[i],
985 s->lacp ? &s->lacp_slaves[i] : NULL,
986 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
990 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
991 struct ofport_dpif *next_port;
993 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
994 for (i = 0; i < s->n_slaves; i++) {
995 if (s->slaves[i] == port->up.ofp_port) {
1000 bundle_del_port(port);
1004 assert(list_size(&bundle->ports) <= s->n_slaves);
1006 if (list_is_empty(&bundle->ports)) {
1007 bundle_destroy(bundle);
1012 if (s->vlan != bundle->vlan) {
1013 bundle->vlan = s->vlan;
1017 /* Get trunked VLANs. */
1018 trunks = s->vlan == -1 ? NULL : s->trunks;
1019 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1020 free(bundle->trunks);
1021 bundle->trunks = vlan_bitmap_clone(trunks);
1026 if (!list_is_short(&bundle->ports)) {
1027 bundle->ofproto->has_bonded_bundles = true;
1029 if (bond_reconfigure(bundle->bond, s->bond)) {
1030 ofproto->need_revalidate = true;
1033 bundle->bond = bond_create(s->bond);
1034 ofproto->need_revalidate = true;
1037 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1038 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1042 bond_destroy(bundle->bond);
1043 bundle->bond = NULL;
1046 /* If we changed something that would affect MAC learning, un-learn
1047 * everything on this port and force flow revalidation. */
1049 bundle_flush_macs(bundle);
1056 bundle_remove(struct ofport *port_)
1058 struct ofport_dpif *port = ofport_dpif_cast(port_);
1059 struct ofbundle *bundle = port->bundle;
1062 bundle_del_port(port);
1063 if (list_is_empty(&bundle->ports)) {
1064 bundle_destroy(bundle);
1065 } else if (list_is_short(&bundle->ports)) {
1066 bond_destroy(bundle->bond);
1067 bundle->bond = NULL;
1073 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1075 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1076 struct ofport_dpif *port = port_;
1077 uint8_t ea[ETH_ADDR_LEN];
1080 error = netdev_get_etheraddr(port->up.netdev, ea);
1082 struct lacp_pdu *packet_pdu;
1083 struct ofpbuf packet;
1085 ofpbuf_init(&packet, 0);
1086 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1087 sizeof *packet_pdu);
1089 error = netdev_send(port->up.netdev, &packet);
1091 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1092 "(%s)", port->bundle->name,
1093 netdev_get_name(port->up.netdev), strerror(error));
1095 ofpbuf_uninit(&packet);
1097 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1098 "%s (%s)", port->bundle->name,
1099 netdev_get_name(port->up.netdev), strerror(error));
1104 bundle_send_learning_packets(struct ofbundle *bundle)
1106 struct ofproto_dpif *ofproto = bundle->ofproto;
1107 int error, n_packets, n_errors;
1108 struct mac_entry *e;
1110 error = n_packets = n_errors = 0;
1111 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1112 if (e->port.p != bundle) {
1113 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1123 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1124 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1125 "packets, last error was: %s",
1126 bundle->name, n_errors, n_packets, strerror(error));
1128 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1129 bundle->name, n_packets);
1134 bundle_run(struct ofbundle *bundle)
1137 lacp_run(bundle->lacp, send_pdu_cb);
1140 struct ofport_dpif *port;
1142 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1143 bool may_enable = lacp_slave_may_enable(bundle->lacp, port);
1144 bond_slave_set_lacp_may_enable(bundle->bond, port, may_enable);
1147 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1148 lacp_negotiated(bundle->lacp));
1149 if (bond_should_send_learning_packets(bundle->bond)) {
1150 bundle_send_learning_packets(bundle);
1156 bundle_wait(struct ofbundle *bundle)
1159 lacp_wait(bundle->lacp);
1162 bond_wait(bundle->bond);
1169 mirror_scan(struct ofproto_dpif *ofproto)
1173 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1174 if (!ofproto->mirrors[idx]) {
1181 static struct ofmirror *
1182 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1186 for (i = 0; i < MAX_MIRRORS; i++) {
1187 struct ofmirror *mirror = ofproto->mirrors[i];
1188 if (mirror && mirror->aux == aux) {
1197 mirror_set(struct ofproto *ofproto_, void *aux,
1198 const struct ofproto_mirror_settings *s)
1200 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1201 mirror_mask_t mirror_bit;
1202 struct ofbundle *bundle;
1203 struct ofmirror *mirror;
1204 struct ofbundle *out;
1205 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1206 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1209 mirror = mirror_lookup(ofproto, aux);
1211 mirror_destroy(mirror);
1217 idx = mirror_scan(ofproto);
1219 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1221 ofproto->up.name, MAX_MIRRORS, s->name);
1225 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1226 mirror->ofproto = ofproto;
1228 mirror->out_vlan = -1;
1229 mirror->name = NULL;
1232 if (!mirror->name || strcmp(s->name, mirror->name)) {
1234 mirror->name = xstrdup(s->name);
1237 /* Get the new configuration. */
1238 if (s->out_bundle) {
1239 out = bundle_lookup(ofproto, s->out_bundle);
1241 mirror_destroy(mirror);
1247 out_vlan = s->out_vlan;
1249 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1250 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1252 /* If the configuration has not changed, do nothing. */
1253 if (hmapx_equals(&srcs, &mirror->srcs)
1254 && hmapx_equals(&dsts, &mirror->dsts)
1255 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1256 && mirror->out == out
1257 && mirror->out_vlan == out_vlan)
1259 hmapx_destroy(&srcs);
1260 hmapx_destroy(&dsts);
1264 hmapx_swap(&srcs, &mirror->srcs);
1265 hmapx_destroy(&srcs);
1267 hmapx_swap(&dsts, &mirror->dsts);
1268 hmapx_destroy(&dsts);
1270 free(mirror->vlans);
1271 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1274 mirror->out_vlan = out_vlan;
1276 /* Update bundles. */
1277 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1278 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1279 if (hmapx_contains(&mirror->srcs, bundle)) {
1280 bundle->src_mirrors |= mirror_bit;
1282 bundle->src_mirrors &= ~mirror_bit;
1285 if (hmapx_contains(&mirror->dsts, bundle)) {
1286 bundle->dst_mirrors |= mirror_bit;
1288 bundle->dst_mirrors &= ~mirror_bit;
1291 if (mirror->out == bundle) {
1292 bundle->mirror_out |= mirror_bit;
1294 bundle->mirror_out &= ~mirror_bit;
1298 ofproto->need_revalidate = true;
1299 mac_learning_flush(ofproto->ml);
1305 mirror_destroy(struct ofmirror *mirror)
1307 struct ofproto_dpif *ofproto;
1308 mirror_mask_t mirror_bit;
1309 struct ofbundle *bundle;
1315 ofproto = mirror->ofproto;
1316 ofproto->need_revalidate = true;
1317 mac_learning_flush(ofproto->ml);
1319 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1320 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1321 bundle->src_mirrors &= ~mirror_bit;
1322 bundle->dst_mirrors &= ~mirror_bit;
1323 bundle->mirror_out &= ~mirror_bit;
1326 hmapx_destroy(&mirror->srcs);
1327 hmapx_destroy(&mirror->dsts);
1328 free(mirror->vlans);
1330 ofproto->mirrors[mirror->idx] = NULL;
1336 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1338 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1339 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1340 ofproto->need_revalidate = true;
1341 mac_learning_flush(ofproto->ml);
1347 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1349 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1350 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1351 return bundle && bundle->mirror_out != 0;
1356 static struct ofport_dpif *
1357 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1359 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1360 return ofport ? ofport_dpif_cast(ofport) : NULL;
1363 static struct ofport_dpif *
1364 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1366 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1370 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1371 struct dpif_port *dpif_port)
1373 ofproto_port->name = dpif_port->name;
1374 ofproto_port->type = dpif_port->type;
1375 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1379 port_run(struct ofport_dpif *ofport)
1382 cfm_run(ofport->cfm);
1384 if (cfm_should_send_ccm(ofport->cfm)) {
1385 struct ofpbuf packet;
1388 ofpbuf_init(&packet, 0);
1389 ccm = eth_compose(&packet, eth_addr_ccm, ofport->up.opp.hw_addr,
1390 ETH_TYPE_CFM, sizeof *ccm);
1391 cfm_compose_ccm(ofport->cfm, ccm);
1392 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1393 ofport->odp_port, &packet);
1394 ofpbuf_uninit(&packet);
1400 port_wait(struct ofport_dpif *ofport)
1403 cfm_wait(ofport->cfm);
1408 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1409 struct ofproto_port *ofproto_port)
1411 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1412 struct dpif_port dpif_port;
1415 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1417 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1423 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1425 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1429 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1431 *ofp_portp = odp_port_to_ofp_port(odp_port);
1437 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1439 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1442 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1444 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1446 /* The caller is going to close ofport->up.netdev. If this is a
1447 * bonded port, then the bond is using that netdev, so remove it
1448 * from the bond. The client will need to reconfigure everything
1449 * after deleting ports, so then the slave will get re-added. */
1450 bundle_remove(&ofport->up);
1456 struct port_dump_state {
1457 struct dpif_port_dump dump;
1462 port_dump_start(const struct ofproto *ofproto_, void **statep)
1464 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1465 struct port_dump_state *state;
1467 *statep = state = xmalloc(sizeof *state);
1468 dpif_port_dump_start(&state->dump, ofproto->dpif);
1469 state->done = false;
1474 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1475 struct ofproto_port *port)
1477 struct port_dump_state *state = state_;
1478 struct dpif_port dpif_port;
1480 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1481 ofproto_port_from_dpif_port(port, &dpif_port);
1484 int error = dpif_port_dump_done(&state->dump);
1486 return error ? error : EOF;
1491 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1493 struct port_dump_state *state = state_;
1496 dpif_port_dump_done(&state->dump);
1503 port_poll(const struct ofproto *ofproto_, char **devnamep)
1505 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1506 return dpif_port_poll(ofproto->dpif, devnamep);
1510 port_poll_wait(const struct ofproto *ofproto_)
1512 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1513 dpif_port_poll_wait(ofproto->dpif);
1517 port_is_lacp_current(const struct ofport *ofport_)
1519 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1520 return (ofport->bundle && ofport->bundle->lacp
1521 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1525 /* Upcall handling. */
1527 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1528 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1529 * their individual configurations.
1531 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1532 * Otherwise, ownership is transferred to this function. */
1534 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1535 const struct flow *flow, bool clone)
1537 struct ofputil_packet_in pin;
1539 pin.packet = upcall->packet;
1540 pin.in_port = flow->in_port;
1541 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1542 pin.buffer_id = 0; /* not yet known */
1543 pin.send_len = upcall->userdata;
1544 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1545 clone ? NULL : upcall->packet);
1549 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1550 const struct ofpbuf *packet)
1552 if (cfm_should_process_flow(flow)) {
1553 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1554 if (ofport && ofport->cfm) {
1555 cfm_process_heartbeat(ofport->cfm, packet);
1558 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1559 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1560 if (port && port->bundle && port->bundle->lacp) {
1561 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1563 lacp_process_pdu(port->bundle->lacp, port, pdu);
1572 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1574 struct facet *facet;
1577 /* Obtain in_port and tun_id, at least. */
1578 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1580 /* Set header pointers in 'flow'. */
1581 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1583 /* Handle 802.1ag and LACP. */
1584 if (process_special(ofproto, &flow, upcall->packet)) {
1585 ofpbuf_delete(upcall->packet);
1586 ofproto->n_matches++;
1590 /* Check with in-band control to see if this packet should be sent
1591 * to the local port regardless of the flow table. */
1592 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1593 send_packet(ofproto, OFPP_LOCAL, upcall->packet);
1596 facet = facet_lookup_valid(ofproto, &flow);
1598 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow);
1600 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1601 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1603 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1604 COVERAGE_INC(ofproto_dpif_no_packet_in);
1605 /* XXX install 'drop' flow entry */
1606 ofpbuf_delete(upcall->packet);
1610 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1614 send_packet_in(ofproto, upcall, &flow, false);
1618 facet = facet_create(rule, &flow, upcall->packet);
1619 } else if (!facet->may_install) {
1620 /* The facet is not installable, that is, we need to process every
1621 * packet, so process the current packet's actions into 'facet'. */
1622 facet_make_actions(ofproto, facet, upcall->packet);
1625 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1627 * Extra-special case for fail-open mode.
1629 * We are in fail-open mode and the packet matched the fail-open rule,
1630 * but we are connected to a controller too. We should send the packet
1631 * up to the controller in the hope that it will try to set up a flow
1632 * and thereby allow us to exit fail-open.
1634 * See the top-level comment in fail-open.c for more information.
1636 send_packet_in(ofproto, upcall, &flow, true);
1639 facet_execute(ofproto, facet, upcall->packet);
1640 facet_install(ofproto, facet, false);
1641 ofproto->n_matches++;
1645 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1649 switch (upcall->type) {
1650 case DPIF_UC_ACTION:
1651 COVERAGE_INC(ofproto_dpif_ctlr_action);
1652 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1653 send_packet_in(ofproto, upcall, &flow, false);
1656 case DPIF_UC_SAMPLE:
1657 if (ofproto->sflow) {
1658 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1659 ofproto_sflow_received(ofproto->sflow, upcall, &flow);
1661 ofpbuf_delete(upcall->packet);
1665 handle_miss_upcall(ofproto, upcall);
1668 case DPIF_N_UC_TYPES:
1670 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1675 /* Flow expiration. */
1677 static int facet_max_idle(const struct ofproto_dpif *);
1678 static void update_stats(struct ofproto_dpif *);
1679 static void rule_expire(struct rule_dpif *);
1680 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1682 /* This function is called periodically by run(). Its job is to collect
1683 * updates for the flows that have been installed into the datapath, most
1684 * importantly when they last were used, and then use that information to
1685 * expire flows that have not been used recently.
1687 * Returns the number of milliseconds after which it should be called again. */
1689 expire(struct ofproto_dpif *ofproto)
1691 struct rule_dpif *rule, *next_rule;
1692 struct cls_cursor cursor;
1695 /* Update stats for each flow in the datapath. */
1696 update_stats(ofproto);
1698 /* Expire facets that have been idle too long. */
1699 dp_max_idle = facet_max_idle(ofproto);
1700 expire_facets(ofproto, dp_max_idle);
1702 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1703 cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL);
1704 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1708 /* All outstanding data in existing flows has been accounted, so it's a
1709 * good time to do bond rebalancing. */
1710 if (ofproto->has_bonded_bundles) {
1711 struct ofbundle *bundle;
1713 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1715 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1720 return MIN(dp_max_idle, 1000);
1723 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1725 * This function also pushes statistics updates to rules which each facet
1726 * resubmits into. Generally these statistics will be accurate. However, if a
1727 * facet changes the rule it resubmits into at some time in between
1728 * update_stats() runs, it is possible that statistics accrued to the
1729 * old rule will be incorrectly attributed to the new rule. This could be
1730 * avoided by calling update_stats() whenever rules are created or
1731 * deleted. However, the performance impact of making so many calls to the
1732 * datapath do not justify the benefit of having perfectly accurate statistics.
1735 update_stats(struct ofproto_dpif *p)
1737 const struct dpif_flow_stats *stats;
1738 struct dpif_flow_dump dump;
1739 const struct nlattr *key;
1742 dpif_flow_dump_start(&dump, p->dpif);
1743 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1744 struct facet *facet;
1747 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1751 odp_flow_key_format(key, key_len, &s);
1752 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
1758 facet = facet_find(p, &flow);
1760 if (facet && facet->installed) {
1762 if (stats->n_packets >= facet->dp_packet_count) {
1763 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1764 facet->packet_count += extra;
1766 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1769 if (stats->n_bytes >= facet->dp_byte_count) {
1770 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1772 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1775 facet->dp_packet_count = stats->n_packets;
1776 facet->dp_byte_count = stats->n_bytes;
1778 facet_update_time(p, facet, stats->used);
1779 facet_account(p, facet, stats->n_bytes);
1780 facet_push_stats(facet);
1782 /* There's a flow in the datapath that we know nothing about.
1784 COVERAGE_INC(facet_unexpected);
1785 dpif_flow_del(p->dpif, key, key_len, NULL);
1788 dpif_flow_dump_done(&dump);
1791 /* Calculates and returns the number of milliseconds of idle time after which
1792 * facets should expire from the datapath and we should fold their statistics
1793 * into their parent rules in userspace. */
1795 facet_max_idle(const struct ofproto_dpif *ofproto)
1798 * Idle time histogram.
1800 * Most of the time a switch has a relatively small number of facets. When
1801 * this is the case we might as well keep statistics for all of them in
1802 * userspace and to cache them in the kernel datapath for performance as
1805 * As the number of facets increases, the memory required to maintain
1806 * statistics about them in userspace and in the kernel becomes
1807 * significant. However, with a large number of facets it is likely that
1808 * only a few of them are "heavy hitters" that consume a large amount of
1809 * bandwidth. At this point, only heavy hitters are worth caching in the
1810 * kernel and maintaining in userspaces; other facets we can discard.
1812 * The technique used to compute the idle time is to build a histogram with
1813 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1814 * that is installed in the kernel gets dropped in the appropriate bucket.
1815 * After the histogram has been built, we compute the cutoff so that only
1816 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
1817 * cached. At least the most-recently-used bucket of facets is kept, so
1818 * actually an arbitrary number of facets can be kept in any given
1819 * expiration run (though the next run will delete most of those unless
1820 * they receive additional data).
1822 * This requires a second pass through the facets, in addition to the pass
1823 * made by update_stats(), because the former function never looks
1824 * at uninstallable facets.
1826 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1827 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1828 int buckets[N_BUCKETS] = { 0 };
1829 struct facet *facet;
1834 total = hmap_count(&ofproto->facets);
1835 if (total <= 1000) {
1836 return N_BUCKETS * BUCKET_WIDTH;
1839 /* Build histogram. */
1841 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1842 long long int idle = now - facet->used;
1843 int bucket = (idle <= 0 ? 0
1844 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1845 : (unsigned int) idle / BUCKET_WIDTH);
1849 /* Find the first bucket whose flows should be expired. */
1850 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
1851 if (buckets[bucket]) {
1854 subtotal += buckets[bucket++];
1855 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
1860 if (VLOG_IS_DBG_ENABLED()) {
1864 ds_put_cstr(&s, "keep");
1865 for (i = 0; i < N_BUCKETS; i++) {
1867 ds_put_cstr(&s, ", drop");
1870 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1873 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1877 return bucket * BUCKET_WIDTH;
1881 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1883 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1884 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1885 struct ofexpired expired;
1887 if (facet->installed) {
1888 struct dpif_flow_stats stats;
1890 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1892 facet_update_stats(ofproto, facet, &stats);
1895 expired.flow = facet->flow;
1896 expired.packet_count = facet->packet_count;
1897 expired.byte_count = facet->byte_count;
1898 expired.used = facet->used;
1899 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1904 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1906 long long int cutoff = time_msec() - dp_max_idle;
1907 struct facet *facet, *next_facet;
1909 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1910 facet_active_timeout(ofproto, facet);
1911 if (facet->used < cutoff) {
1912 facet_remove(ofproto, facet);
1917 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1918 * then delete it entirely. */
1920 rule_expire(struct rule_dpif *rule)
1922 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1923 struct facet *facet, *next_facet;
1927 /* Has 'rule' expired? */
1929 if (rule->up.hard_timeout
1930 && now > rule->up.created + rule->up.hard_timeout * 1000) {
1931 reason = OFPRR_HARD_TIMEOUT;
1932 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1933 && now > rule->used + rule->up.idle_timeout * 1000) {
1934 reason = OFPRR_IDLE_TIMEOUT;
1939 COVERAGE_INC(ofproto_dpif_expired);
1941 /* Update stats. (This is a no-op if the rule expired due to an idle
1942 * timeout, because that only happens when the rule has no facets left.) */
1943 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1944 facet_remove(ofproto, facet);
1947 /* Get rid of the rule. */
1948 ofproto_rule_expire(&rule->up, reason);
1953 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
1954 * example 'packet' within that flow.
1956 * The caller must already have determined that no facet with an identical
1957 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1958 * the ofproto's classifier table. */
1959 static struct facet *
1960 facet_create(struct rule_dpif *rule, const struct flow *flow,
1961 const struct ofpbuf *packet)
1963 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1964 struct facet *facet;
1966 facet = xzalloc(sizeof *facet);
1967 facet->used = time_msec();
1968 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1969 list_push_back(&rule->facets, &facet->list_node);
1971 facet->flow = *flow;
1972 netflow_flow_init(&facet->nf_flow);
1973 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1975 facet_make_actions(ofproto, facet, packet);
1981 facet_free(struct facet *facet)
1983 free(facet->actions);
1987 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1988 * 'packet', which arrived on 'in_port'.
1990 * Takes ownership of 'packet'. */
1992 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
1993 const struct nlattr *odp_actions, size_t actions_len,
1994 struct ofpbuf *packet)
1996 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1997 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1998 /* As an optimization, avoid a round-trip from userspace to kernel to
1999 * userspace. This also avoids possibly filling up kernel packet
2000 * buffers along the way. */
2001 struct dpif_upcall upcall;
2003 upcall.type = DPIF_UC_ACTION;
2004 upcall.packet = packet;
2007 upcall.userdata = nl_attr_get_u64(odp_actions);
2008 upcall.sample_pool = 0;
2009 upcall.actions = NULL;
2010 upcall.actions_len = 0;
2012 send_packet_in(ofproto, &upcall, flow, false);
2018 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
2019 ofpbuf_delete(packet);
2024 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2025 * statistics appropriately. 'packet' must have at least sizeof(struct
2026 * ofp_packet_in) bytes of headroom.
2028 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2029 * applying flow_extract() to 'packet' would yield the same flow as
2032 * 'facet' must have accurately composed ODP actions; that is, it must not be
2033 * in need of revalidation.
2035 * Takes ownership of 'packet'. */
2037 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2038 struct ofpbuf *packet)
2040 struct dpif_flow_stats stats;
2042 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2044 flow_extract_stats(&facet->flow, packet, &stats);
2045 stats.used = time_msec();
2046 if (execute_odp_actions(ofproto, &facet->flow,
2047 facet->actions, facet->actions_len, packet)) {
2048 facet_update_stats(ofproto, facet, &stats);
2052 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2054 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2055 * rule's statistics, via facet_uninstall().
2057 * - Removes 'facet' from its rule and from ofproto->facets.
2060 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2062 facet_uninstall(ofproto, facet);
2063 facet_flush_stats(ofproto, facet);
2064 hmap_remove(&ofproto->facets, &facet->hmap_node);
2065 list_remove(&facet->list_node);
2069 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2071 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2072 const struct ofpbuf *packet)
2074 const struct rule_dpif *rule = facet->rule;
2075 struct ofpbuf *odp_actions;
2076 struct action_xlate_ctx ctx;
2078 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2079 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2080 facet->tags = ctx.tags;
2081 facet->may_install = ctx.may_set_up_flow;
2082 facet->nf_flow.output_iface = ctx.nf_output_iface;
2084 if (facet->actions_len != odp_actions->size
2085 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2086 free(facet->actions);
2087 facet->actions_len = odp_actions->size;
2088 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2091 ofpbuf_delete(odp_actions);
2095 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2096 const struct nlattr *actions, size_t actions_len,
2097 struct dpif_flow_stats *stats)
2099 struct odputil_keybuf keybuf;
2100 enum dpif_flow_put_flags flags;
2103 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2105 flags |= DPIF_FP_ZERO_STATS;
2106 facet->dp_packet_count = 0;
2107 facet->dp_byte_count = 0;
2110 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2111 odp_flow_key_from_flow(&key, &facet->flow);
2113 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2114 actions, actions_len, stats);
2117 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2118 * 'zero_stats' is true, clears any existing statistics from the datapath for
2121 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2123 struct dpif_flow_stats stats;
2125 if (facet->may_install
2126 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2127 zero_stats ? &stats : NULL)) {
2128 facet->installed = true;
2133 facet_account(struct ofproto_dpif *ofproto,
2134 struct facet *facet, uint64_t extra_bytes)
2136 uint64_t total_bytes, n_bytes;
2137 struct ofbundle *in_bundle;
2138 const struct nlattr *a;
2143 total_bytes = facet->byte_count + extra_bytes;
2144 if (total_bytes <= facet->accounted_bytes) {
2147 n_bytes = total_bytes - facet->accounted_bytes;
2148 facet->accounted_bytes = total_bytes;
2150 /* Test that 'tags' is nonzero to ensure that only flows that include an
2151 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2152 * This works because OFPP_NORMAL always sets a nonzero tag value.
2154 * Feed information from the active flows back into the learning table to
2155 * ensure that table is always in sync with what is actually flowing
2156 * through the datapath. */
2158 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2159 &vlan, &in_bundle)) {
2163 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2165 if (!ofproto->has_bonded_bundles) {
2168 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2169 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2170 struct ofport_dpif *port;
2172 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2173 if (port && port->bundle && port->bundle->bond) {
2174 bond_account(port->bundle->bond, &facet->flow, vlan, n_bytes);
2180 /* If 'rule' is installed in the datapath, uninstalls it. */
2182 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2184 if (facet->installed) {
2185 struct odputil_keybuf keybuf;
2186 struct dpif_flow_stats stats;
2189 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2190 odp_flow_key_from_flow(&key, &facet->flow);
2192 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
2193 facet_update_stats(p, facet, &stats);
2195 facet->installed = false;
2196 facet->dp_packet_count = 0;
2197 facet->dp_byte_count = 0;
2199 assert(facet->dp_packet_count == 0);
2200 assert(facet->dp_byte_count == 0);
2204 /* Returns true if the only action for 'facet' is to send to the controller.
2205 * (We don't report NetFlow expiration messages for such facets because they
2206 * are just part of the control logic for the network, not real traffic). */
2208 facet_is_controller_flow(struct facet *facet)
2211 && facet->rule->up.n_actions == 1
2212 && action_outputs_to_port(&facet->rule->up.actions[0],
2213 htons(OFPP_CONTROLLER)));
2216 /* Folds all of 'facet''s statistics into its rule. Also updates the
2217 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2218 * 'facet''s statistics in the datapath should have been zeroed and folded into
2219 * its packet and byte counts before this function is called. */
2221 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2223 assert(!facet->dp_byte_count);
2224 assert(!facet->dp_packet_count);
2226 facet_push_stats(facet);
2227 facet_account(ofproto, facet, 0);
2229 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2230 struct ofexpired expired;
2231 expired.flow = facet->flow;
2232 expired.packet_count = facet->packet_count;
2233 expired.byte_count = facet->byte_count;
2234 expired.used = facet->used;
2235 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2238 facet->rule->packet_count += facet->packet_count;
2239 facet->rule->byte_count += facet->byte_count;
2241 /* Reset counters to prevent double counting if 'facet' ever gets
2243 facet->packet_count = 0;
2244 facet->byte_count = 0;
2245 facet->rs_packet_count = 0;
2246 facet->rs_byte_count = 0;
2247 facet->accounted_bytes = 0;
2249 netflow_flow_clear(&facet->nf_flow);
2252 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2253 * Returns it if found, otherwise a null pointer.
2255 * The returned facet might need revalidation; use facet_lookup_valid()
2256 * instead if that is important. */
2257 static struct facet *
2258 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2260 struct facet *facet;
2262 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2264 if (flow_equal(flow, &facet->flow)) {
2272 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2273 * Returns it if found, otherwise a null pointer.
2275 * The returned facet is guaranteed to be valid. */
2276 static struct facet *
2277 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2279 struct facet *facet = facet_find(ofproto, flow);
2281 /* The facet we found might not be valid, since we could be in need of
2282 * revalidation. If it is not valid, don't return it. */
2284 && ofproto->need_revalidate
2285 && !facet_revalidate(ofproto, facet)) {
2286 COVERAGE_INC(facet_invalidated);
2293 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2295 * - If the rule found is different from 'facet''s current rule, moves
2296 * 'facet' to the new rule and recompiles its actions.
2298 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2299 * where it is and recompiles its actions anyway.
2301 * - If there is none, destroys 'facet'.
2303 * Returns true if 'facet' still exists, false if it has been destroyed. */
2305 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2307 struct action_xlate_ctx ctx;
2308 struct ofpbuf *odp_actions;
2309 struct rule_dpif *new_rule;
2310 bool actions_changed;
2312 COVERAGE_INC(facet_revalidate);
2314 /* Determine the new rule. */
2315 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
2317 /* No new rule, so delete the facet. */
2318 facet_remove(ofproto, facet);
2322 /* Calculate new ODP actions.
2324 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2325 * emit a NetFlow expiration and, if so, we need to have the old state
2326 * around to properly compose it. */
2327 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2328 odp_actions = xlate_actions(&ctx,
2329 new_rule->up.actions, new_rule->up.n_actions);
2330 actions_changed = (facet->actions_len != odp_actions->size
2331 || memcmp(facet->actions, odp_actions->data,
2332 facet->actions_len));
2334 /* If the ODP actions changed or the installability changed, then we need
2335 * to talk to the datapath. */
2336 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2337 if (ctx.may_set_up_flow) {
2338 struct dpif_flow_stats stats;
2340 facet_put__(ofproto, facet,
2341 odp_actions->data, odp_actions->size, &stats);
2342 facet_update_stats(ofproto, facet, &stats);
2344 facet_uninstall(ofproto, facet);
2347 /* The datapath flow is gone or has zeroed stats, so push stats out of
2348 * 'facet' into 'rule'. */
2349 facet_flush_stats(ofproto, facet);
2352 /* Update 'facet' now that we've taken care of all the old state. */
2353 facet->tags = ctx.tags;
2354 facet->nf_flow.output_iface = ctx.nf_output_iface;
2355 facet->may_install = ctx.may_set_up_flow;
2356 if (actions_changed) {
2357 free(facet->actions);
2358 facet->actions_len = odp_actions->size;
2359 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2361 if (facet->rule != new_rule) {
2362 COVERAGE_INC(facet_changed_rule);
2363 list_remove(&facet->list_node);
2364 list_push_back(&new_rule->facets, &facet->list_node);
2365 facet->rule = new_rule;
2366 facet->used = new_rule->up.created;
2367 facet->rs_used = facet->used;
2370 ofpbuf_delete(odp_actions);
2375 /* Updates 'facet''s used time. Caller is responsible for calling
2376 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2378 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2381 if (used > facet->used) {
2383 if (used > facet->rule->used) {
2384 facet->rule->used = used;
2386 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2390 /* Folds the statistics from 'stats' into the counters in 'facet'.
2392 * Because of the meaning of a facet's counters, it only makes sense to do this
2393 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2394 * packet that was sent by hand or if it represents statistics that have been
2395 * cleared out of the datapath. */
2397 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2398 const struct dpif_flow_stats *stats)
2400 if (stats->n_packets || stats->used > facet->used) {
2401 facet_update_time(ofproto, facet, stats->used);
2402 facet->packet_count += stats->n_packets;
2403 facet->byte_count += stats->n_bytes;
2404 facet_push_stats(facet);
2405 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2410 facet_push_stats(struct facet *facet)
2412 uint64_t rs_packets, rs_bytes;
2414 assert(facet->packet_count >= facet->rs_packet_count);
2415 assert(facet->byte_count >= facet->rs_byte_count);
2416 assert(facet->used >= facet->rs_used);
2418 rs_packets = facet->packet_count - facet->rs_packet_count;
2419 rs_bytes = facet->byte_count - facet->rs_byte_count;
2421 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2422 facet->rs_packet_count = facet->packet_count;
2423 facet->rs_byte_count = facet->byte_count;
2424 facet->rs_used = facet->used;
2426 flow_push_stats(facet->rule, &facet->flow,
2427 rs_packets, rs_bytes, facet->used);
2431 struct ofproto_push {
2432 struct action_xlate_ctx ctx;
2439 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2441 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2444 rule->packet_count += push->packets;
2445 rule->byte_count += push->bytes;
2446 rule->used = MAX(push->used, rule->used);
2450 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2451 * 'rule''s actions. */
2453 flow_push_stats(const struct rule_dpif *rule,
2454 struct flow *flow, uint64_t packets, uint64_t bytes,
2457 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2458 struct ofproto_push push;
2460 push.packets = packets;
2464 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2465 push.ctx.resubmit_hook = push_resubmit;
2466 ofpbuf_delete(xlate_actions(&push.ctx,
2467 rule->up.actions, rule->up.n_actions));
2472 static struct rule_dpif *
2473 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
2475 return rule_dpif_cast(rule_from_cls_rule(
2476 classifier_lookup(&ofproto->up.tables[0],
2480 static struct rule *
2483 struct rule_dpif *rule = xmalloc(sizeof *rule);
2488 rule_dealloc(struct rule *rule_)
2490 struct rule_dpif *rule = rule_dpif_cast(rule_);
2495 rule_construct(struct rule *rule_)
2497 struct rule_dpif *rule = rule_dpif_cast(rule_);
2498 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2499 struct rule_dpif *old_rule;
2502 error = validate_actions(rule->up.actions, rule->up.n_actions,
2503 &rule->up.cr.flow, ofproto->max_ports);
2508 old_rule = rule_dpif_cast(rule_from_cls_rule(classifier_find_rule_exactly(
2509 &ofproto->up.tables[0],
2512 ofproto_rule_destroy(&old_rule->up);
2515 rule->used = rule->up.created;
2516 rule->packet_count = 0;
2517 rule->byte_count = 0;
2518 list_init(&rule->facets);
2519 classifier_insert(&ofproto->up.tables[0], &rule->up.cr);
2521 ofproto->need_revalidate = true;
2527 rule_destruct(struct rule *rule_)
2529 struct rule_dpif *rule = rule_dpif_cast(rule_);
2530 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2531 struct facet *facet, *next_facet;
2533 classifier_remove(&ofproto->up.tables[0], &rule->up.cr);
2534 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2535 facet_revalidate(ofproto, facet);
2537 ofproto->need_revalidate = true;
2541 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2543 struct rule_dpif *rule = rule_dpif_cast(rule_);
2544 struct facet *facet;
2546 /* Start from historical data for 'rule' itself that are no longer tracked
2547 * in facets. This counts, for example, facets that have expired. */
2548 *packets = rule->packet_count;
2549 *bytes = rule->byte_count;
2551 /* Add any statistics that are tracked by facets. This includes
2552 * statistical data recently updated by ofproto_update_stats() as well as
2553 * stats for packets that were executed "by hand" via dpif_execute(). */
2554 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2555 *packets += facet->packet_count;
2556 *bytes += facet->byte_count;
2561 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2563 struct rule_dpif *rule = rule_dpif_cast(rule_);
2564 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2565 struct action_xlate_ctx ctx;
2566 struct ofpbuf *odp_actions;
2567 struct facet *facet;
2570 /* First look for a related facet. If we find one, account it to that. */
2571 facet = facet_lookup_valid(ofproto, flow);
2572 if (facet && facet->rule == rule) {
2573 facet_execute(ofproto, facet, packet);
2577 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2578 * create a new facet for it and use that. */
2579 if (rule_dpif_lookup(ofproto, flow) == rule) {
2580 facet = facet_create(rule, flow, packet);
2581 facet_execute(ofproto, facet, packet);
2582 facet_install(ofproto, facet, true);
2586 /* We can't account anything to a facet. If we were to try, then that
2587 * facet would have a non-matching rule, busting our invariants. */
2588 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2589 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2590 size = packet->size;
2591 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2592 odp_actions->size, packet)) {
2593 rule->used = time_msec();
2594 rule->packet_count++;
2595 rule->byte_count += size;
2596 flow_push_stats(rule, flow, 1, size, rule->used);
2598 ofpbuf_delete(odp_actions);
2604 rule_modify_actions(struct rule *rule_,
2605 const union ofp_action *actions, size_t n_actions)
2607 struct rule_dpif *rule = rule_dpif_cast(rule_);
2608 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2611 error = validate_actions(actions, n_actions, &rule->up.cr.flow,
2612 ofproto->max_ports);
2614 ofproto->need_revalidate = true;
2619 /* Sends 'packet' out of port 'odp_port' within 'p'.
2620 * Returns 0 if successful, otherwise a positive errno value. */
2622 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2623 const struct ofpbuf *packet)
2625 struct ofpbuf odp_actions;
2628 ofpbuf_init(&odp_actions, 32);
2629 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2630 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
2632 ofpbuf_uninit(&odp_actions);
2635 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2636 ofproto->up.name, odp_port, strerror(error));
2641 /* OpenFlow to ODP action translation. */
2643 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2644 struct action_xlate_ctx *ctx);
2645 static bool xlate_normal(struct action_xlate_ctx *);
2648 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2650 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2651 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2654 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2655 /* Forwarding disabled on port. */
2660 * We don't have an ofport record for this port, but it doesn't hurt to
2661 * allow forwarding to it anyhow. Maybe such a port will appear later
2662 * and we're pre-populating the flow table.
2666 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2667 ctx->nf_output_iface = ofp_port;
2671 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2673 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2674 struct rule_dpif *rule;
2675 uint16_t old_in_port;
2677 /* Look up a flow with 'in_port' as the input port. Then restore the
2678 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2679 * have surprising behavior). */
2680 old_in_port = ctx->flow.in_port;
2681 ctx->flow.in_port = in_port;
2682 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow);
2683 ctx->flow.in_port = old_in_port;
2685 if (ctx->resubmit_hook) {
2686 ctx->resubmit_hook(ctx, rule);
2691 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2695 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2697 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2698 MAX_RESUBMIT_RECURSION);
2703 flood_packets(struct ofproto_dpif *ofproto,
2704 uint16_t ofp_in_port, ovs_be32 mask,
2705 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2707 struct ofport_dpif *ofport;
2709 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2710 uint16_t ofp_port = ofport->up.ofp_port;
2711 if (ofp_port != ofp_in_port && !(ofport->up.opp.config & mask)) {
2712 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT,
2716 *nf_output_iface = NF_OUT_FLOOD;
2720 xlate_output_action__(struct action_xlate_ctx *ctx,
2721 uint16_t port, uint16_t max_len)
2723 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2725 ctx->nf_output_iface = NF_OUT_DROP;
2729 add_output_action(ctx, ctx->flow.in_port);
2732 xlate_table_action(ctx, ctx->flow.in_port);
2738 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD),
2739 &ctx->nf_output_iface, ctx->odp_actions);
2742 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0),
2743 &ctx->nf_output_iface, ctx->odp_actions);
2745 case OFPP_CONTROLLER:
2746 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2749 add_output_action(ctx, OFPP_LOCAL);
2752 if (port != ctx->flow.in_port) {
2753 add_output_action(ctx, port);
2758 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2759 ctx->nf_output_iface = NF_OUT_FLOOD;
2760 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2761 ctx->nf_output_iface = prev_nf_output_iface;
2762 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2763 ctx->nf_output_iface != NF_OUT_FLOOD) {
2764 ctx->nf_output_iface = NF_OUT_MULTI;
2769 xlate_output_action(struct action_xlate_ctx *ctx,
2770 const struct ofp_action_output *oao)
2772 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2775 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2776 * optimization, because we're going to add another action that sets the
2777 * priority immediately after, or because there are no actions following the
2780 remove_pop_action(struct action_xlate_ctx *ctx)
2782 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2783 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2784 ctx->last_pop_priority = -1;
2789 add_pop_action(struct action_xlate_ctx *ctx)
2791 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2792 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2793 ctx->last_pop_priority = ctx->odp_actions->size;
2798 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2799 const struct ofp_action_enqueue *oae)
2801 uint16_t ofp_port, odp_port;
2805 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2808 /* Fall back to ordinary output action. */
2809 xlate_output_action__(ctx, ntohs(oae->port), 0);
2813 /* Figure out ODP output port. */
2814 ofp_port = ntohs(oae->port);
2815 if (ofp_port == OFPP_IN_PORT) {
2816 ofp_port = ctx->flow.in_port;
2818 odp_port = ofp_port_to_odp_port(ofp_port);
2820 /* Add ODP actions. */
2821 remove_pop_action(ctx);
2822 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2823 add_output_action(ctx, odp_port);
2824 add_pop_action(ctx);
2826 /* Update NetFlow output port. */
2827 if (ctx->nf_output_iface == NF_OUT_DROP) {
2828 ctx->nf_output_iface = odp_port;
2829 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2830 ctx->nf_output_iface = NF_OUT_MULTI;
2835 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2836 const struct nx_action_set_queue *nasq)
2841 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2844 /* Couldn't translate queue to a priority, so ignore. A warning
2845 * has already been logged. */
2849 remove_pop_action(ctx);
2850 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2854 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2856 ovs_be16 tci = ctx->flow.vlan_tci;
2857 if (!(tci & htons(VLAN_CFI))) {
2858 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2860 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2861 tci & ~htons(VLAN_CFI));
2865 struct xlate_reg_state {
2871 save_reg_state(const struct action_xlate_ctx *ctx,
2872 struct xlate_reg_state *state)
2874 state->vlan_tci = ctx->flow.vlan_tci;
2875 state->tun_id = ctx->flow.tun_id;
2879 update_reg_state(struct action_xlate_ctx *ctx,
2880 const struct xlate_reg_state *state)
2882 if (ctx->flow.vlan_tci != state->vlan_tci) {
2883 xlate_set_dl_tci(ctx);
2885 if (ctx->flow.tun_id != state->tun_id) {
2886 nl_msg_put_be64(ctx->odp_actions,
2887 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2892 xlate_autopath(struct action_xlate_ctx *ctx,
2893 const struct nx_action_autopath *naa)
2895 uint16_t ofp_port = ntohl(naa->id);
2896 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
2898 if (!port || !port->bundle) {
2899 ofp_port = OFPP_NONE;
2900 } else if (port->bundle->bond) {
2901 /* Autopath does not support VLAN hashing. */
2902 struct ofport_dpif *slave = bond_choose_output_slave(
2903 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
2905 ofp_port = slave->up.ofp_port;
2908 autopath_execute(naa, &ctx->flow, ofp_port);
2912 xlate_nicira_action(struct action_xlate_ctx *ctx,
2913 const struct nx_action_header *nah)
2915 const struct nx_action_resubmit *nar;
2916 const struct nx_action_set_tunnel *nast;
2917 const struct nx_action_set_queue *nasq;
2918 const struct nx_action_multipath *nam;
2919 const struct nx_action_autopath *naa;
2920 enum nx_action_subtype subtype = ntohs(nah->subtype);
2921 struct xlate_reg_state state;
2924 assert(nah->vendor == htonl(NX_VENDOR_ID));
2926 case NXAST_RESUBMIT:
2927 nar = (const struct nx_action_resubmit *) nah;
2928 xlate_table_action(ctx, ntohs(nar->in_port));
2931 case NXAST_SET_TUNNEL:
2932 nast = (const struct nx_action_set_tunnel *) nah;
2933 tun_id = htonll(ntohl(nast->tun_id));
2934 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2935 ctx->flow.tun_id = tun_id;
2938 case NXAST_DROP_SPOOFED_ARP:
2939 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2940 nl_msg_put_flag(ctx->odp_actions,
2941 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2945 case NXAST_SET_QUEUE:
2946 nasq = (const struct nx_action_set_queue *) nah;
2947 xlate_set_queue_action(ctx, nasq);
2950 case NXAST_POP_QUEUE:
2951 add_pop_action(ctx);
2954 case NXAST_REG_MOVE:
2955 save_reg_state(ctx, &state);
2956 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2958 update_reg_state(ctx, &state);
2961 case NXAST_REG_LOAD:
2962 save_reg_state(ctx, &state);
2963 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2965 update_reg_state(ctx, &state);
2969 /* Nothing to do. */
2972 case NXAST_SET_TUNNEL64:
2973 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2974 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2975 ctx->flow.tun_id = tun_id;
2978 case NXAST_MULTIPATH:
2979 nam = (const struct nx_action_multipath *) nah;
2980 multipath_execute(nam, &ctx->flow);
2983 case NXAST_AUTOPATH:
2984 naa = (const struct nx_action_autopath *) nah;
2985 xlate_autopath(ctx, naa);
2988 /* If you add a new action here that modifies flow data, don't forget to
2989 * update the flow key in ctx->flow at the same time. */
2991 case NXAST_SNAT__OBSOLETE:
2993 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2999 do_xlate_actions(const union ofp_action *in, size_t n_in,
3000 struct action_xlate_ctx *ctx)
3002 const struct ofport_dpif *port;
3003 struct actions_iterator iter;
3004 const union ofp_action *ia;
3006 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3008 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3009 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3010 ? htonl(OFPPC_NO_RECV_STP)
3011 : htonl(OFPPC_NO_RECV))) {
3012 /* Drop this flow. */
3016 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
3017 enum ofp_action_type type = ntohs(ia->type);
3018 const struct ofp_action_dl_addr *oada;
3022 xlate_output_action(ctx, &ia->output);
3025 case OFPAT_SET_VLAN_VID:
3026 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3027 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3028 xlate_set_dl_tci(ctx);
3031 case OFPAT_SET_VLAN_PCP:
3032 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3033 ctx->flow.vlan_tci |= htons(
3034 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3035 xlate_set_dl_tci(ctx);
3038 case OFPAT_STRIP_VLAN:
3039 ctx->flow.vlan_tci = htons(0);
3040 xlate_set_dl_tci(ctx);
3043 case OFPAT_SET_DL_SRC:
3044 oada = ((struct ofp_action_dl_addr *) ia);
3045 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
3046 oada->dl_addr, ETH_ADDR_LEN);
3047 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3050 case OFPAT_SET_DL_DST:
3051 oada = ((struct ofp_action_dl_addr *) ia);
3052 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
3053 oada->dl_addr, ETH_ADDR_LEN);
3054 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3057 case OFPAT_SET_NW_SRC:
3058 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
3059 ia->nw_addr.nw_addr);
3060 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3063 case OFPAT_SET_NW_DST:
3064 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
3065 ia->nw_addr.nw_addr);
3066 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3069 case OFPAT_SET_NW_TOS:
3070 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
3072 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
3075 case OFPAT_SET_TP_SRC:
3076 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
3077 ia->tp_port.tp_port);
3078 ctx->flow.tp_src = ia->tp_port.tp_port;
3081 case OFPAT_SET_TP_DST:
3082 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
3083 ia->tp_port.tp_port);
3084 ctx->flow.tp_dst = ia->tp_port.tp_port;
3088 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
3092 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3096 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
3103 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3104 struct ofproto_dpif *ofproto, const struct flow *flow,
3105 const struct ofpbuf *packet)
3107 ctx->ofproto = ofproto;
3109 ctx->packet = packet;
3110 ctx->resubmit_hook = NULL;
3113 static struct ofpbuf *
3114 xlate_actions(struct action_xlate_ctx *ctx,
3115 const union ofp_action *in, size_t n_in)
3117 COVERAGE_INC(ofproto_dpif_xlate);
3119 ctx->odp_actions = ofpbuf_new(512);
3121 ctx->may_set_up_flow = true;
3122 ctx->nf_output_iface = NF_OUT_DROP;
3124 ctx->last_pop_priority = -1;
3126 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3127 ctx->may_set_up_flow = false;
3129 do_xlate_actions(in, n_in, ctx);
3132 remove_pop_action(ctx);
3134 /* Check with in-band control to see if we're allowed to set up this
3136 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3137 ctx->odp_actions->data,
3138 ctx->odp_actions->size)) {
3139 ctx->may_set_up_flow = false;
3142 return ctx->odp_actions;
3145 /* OFPP_NORMAL implementation. */
3148 struct ofport_dpif *port;
3153 struct dst builtin[32];
3155 size_t n, allocated;
3158 static void dst_set_init(struct dst_set *);
3159 static void dst_set_add(struct dst_set *, const struct dst *);
3160 static void dst_set_free(struct dst_set *);
3162 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3165 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3166 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3168 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3169 : in_bundle->vlan >= 0 ? in_bundle->vlan
3170 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3171 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3173 dst->port = (!out_bundle->bond
3174 ? ofbundle_get_a_port(out_bundle)
3175 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3176 dst->vlan, &ctx->tags));
3178 return dst->port != NULL;
3182 mirror_mask_ffs(mirror_mask_t mask)
3184 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3189 dst_set_init(struct dst_set *set)
3191 set->dsts = set->builtin;
3193 set->allocated = ARRAY_SIZE(set->builtin);
3197 dst_set_add(struct dst_set *set, const struct dst *dst)
3199 if (set->n >= set->allocated) {
3200 size_t new_allocated;
3201 struct dst *new_dsts;
3203 new_allocated = set->allocated * 2;
3204 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3205 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3209 set->dsts = new_dsts;
3210 set->allocated = new_allocated;
3212 set->dsts[set->n++] = *dst;
3216 dst_set_free(struct dst_set *set)
3218 if (set->dsts != set->builtin) {
3224 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3227 for (i = 0; i < set->n; i++) {
3228 if (set->dsts[i].vlan == test->vlan
3229 && set->dsts[i].port == test->port) {
3237 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3239 return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan);
3243 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3245 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3248 /* Returns an arbitrary interface within 'bundle'. */
3249 static struct ofport_dpif *
3250 ofbundle_get_a_port(const struct ofbundle *bundle)
3252 return CONTAINER_OF(list_front(&bundle->ports),
3253 struct ofport_dpif, bundle_node);
3257 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3258 const struct ofbundle *in_bundle,
3259 const struct ofbundle *out_bundle, struct dst_set *set)
3263 if (out_bundle == OFBUNDLE_FLOOD) {
3264 struct ofbundle *bundle;
3266 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3267 if (bundle != in_bundle
3268 && ofbundle_includes_vlan(bundle, vlan)
3269 && bundle->floodable
3270 && !bundle->mirror_out
3271 && set_dst(ctx, &dst, in_bundle, bundle)) {
3272 dst_set_add(set, &dst);
3275 ctx->nf_output_iface = NF_OUT_FLOOD;
3276 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3277 dst_set_add(set, &dst);
3278 ctx->nf_output_iface = dst.port->odp_port;
3283 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3285 return vlan_bitmap_contains(m->vlans, vlan);
3289 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3290 uint16_t vlan, const struct ofbundle *in_bundle,
3291 struct dst_set *set)
3293 struct ofproto_dpif *ofproto = ctx->ofproto;
3294 mirror_mask_t mirrors;
3298 mirrors = in_bundle->src_mirrors;
3299 for (i = 0; i < set->n; i++) {
3300 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3307 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3308 if (flow_vlan == 0) {
3309 flow_vlan = OFP_VLAN_NONE;
3313 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3314 if (vlan_is_mirrored(m, vlan)) {
3318 if (set_dst(ctx, &dst, in_bundle, m->out)
3319 && !dst_is_duplicate(set, &dst)) {
3320 dst_set_add(set, &dst);
3323 struct ofbundle *bundle;
3325 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3326 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3327 && set_dst(ctx, &dst, in_bundle, bundle))
3329 if (bundle->vlan < 0) {
3330 dst.vlan = m->out_vlan;
3332 if (dst_is_duplicate(set, &dst)) {
3336 /* Use the vlan tag on the original flow instead of
3337 * the one passed in the vlan parameter. This ensures
3338 * that we compare the vlan from before any implicit
3339 * tagging tags place. This is necessary because
3340 * dst->vlan is the final vlan, after removing implicit
3342 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3343 /* Don't send out input port on same VLAN. */
3346 dst_set_add(set, &dst);
3351 mirrors &= mirrors - 1;
3356 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3357 const struct ofbundle *in_bundle,
3358 const struct ofbundle *out_bundle)
3360 uint16_t initial_vlan, cur_vlan;
3361 const struct dst *dst;
3365 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3366 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3368 /* Output all the packets we can without having to change the VLAN. */
3369 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3370 if (initial_vlan == 0) {
3371 initial_vlan = OFP_VLAN_NONE;
3373 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3374 if (dst->vlan != initial_vlan) {
3377 nl_msg_put_u32(ctx->odp_actions,
3378 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3381 /* Then output the rest. */
3382 cur_vlan = initial_vlan;
3383 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3384 if (dst->vlan == initial_vlan) {
3387 if (dst->vlan != cur_vlan) {
3388 if (dst->vlan == OFP_VLAN_NONE) {
3389 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3392 tci = htons(dst->vlan & VLAN_VID_MASK);
3393 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3394 nl_msg_put_be16(ctx->odp_actions,
3395 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3397 cur_vlan = dst->vlan;
3399 nl_msg_put_u32(ctx->odp_actions,
3400 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3406 /* Returns the effective vlan of a packet, taking into account both the
3407 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3408 * the packet is untagged and -1 indicates it has an invalid header and
3409 * should be dropped. */
3411 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3412 struct ofbundle *in_bundle, bool have_packet)
3414 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3415 if (in_bundle->vlan >= 0) {
3418 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3419 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3420 "packet received on port %s configured with "
3421 "implicit VLAN %"PRIu16,
3422 ofproto->up.name, vlan,
3423 in_bundle->name, in_bundle->vlan);
3427 vlan = in_bundle->vlan;
3429 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3431 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3432 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3433 "packet received on port %s not configured for "
3435 ofproto->up.name, vlan, in_bundle->name, vlan);
3444 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3445 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3446 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3448 is_gratuitous_arp(const struct flow *flow)
3450 return (flow->dl_type == htons(ETH_TYPE_ARP)
3451 && eth_addr_is_broadcast(flow->dl_dst)
3452 && (flow->nw_proto == ARP_OP_REPLY
3453 || (flow->nw_proto == ARP_OP_REQUEST
3454 && flow->nw_src == flow->nw_dst)));
3458 update_learning_table(struct ofproto_dpif *ofproto,
3459 const struct flow *flow, int vlan,
3460 struct ofbundle *in_bundle)
3462 struct mac_entry *mac;
3464 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3468 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3469 if (is_gratuitous_arp(flow)) {
3470 /* We don't want to learn from gratuitous ARP packets that are
3471 * reflected back over bond slaves so we lock the learning table. */
3472 if (!in_bundle->bond) {
3473 mac_entry_set_grat_arp_lock(mac);
3474 } else if (mac_entry_is_grat_arp_locked(mac)) {
3479 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3480 /* The log messages here could actually be useful in debugging,
3481 * so keep the rate limit relatively high. */
3482 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3483 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3484 "on port %s in VLAN %d",
3485 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3486 in_bundle->name, vlan);
3488 mac->port.p = in_bundle;
3489 tag_set_add(&ofproto->revalidate_set,
3490 mac_learning_changed(ofproto->ml, mac));
3494 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3495 * dropped. Returns true if they may be forwarded, false if they should be
3498 * If 'have_packet' is true, it indicates that the caller is processing a
3499 * received packet. If 'have_packet' is false, then the caller is just
3500 * revalidating an existing flow because configuration has changed. Either
3501 * way, 'have_packet' only affects logging (there is no point in logging errors
3502 * during revalidation).
3504 * Sets '*in_portp' to the input port. This will be a null pointer if
3505 * flow->in_port does not designate a known input port (in which case
3506 * is_admissible() returns false).
3508 * When returning true, sets '*vlanp' to the effective VLAN of the input
3509 * packet, as returned by flow_get_vlan().
3511 * May also add tags to '*tags', although the current implementation only does
3512 * so in one special case.
3515 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3517 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3519 struct ofport_dpif *in_port;
3520 struct ofbundle *in_bundle;
3523 /* Find the port and bundle for the received packet. */
3524 in_port = get_ofp_port(ofproto, flow->in_port);
3525 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3526 if (!in_port || !in_bundle) {
3527 /* No interface? Something fishy... */
3529 /* Odd. A few possible reasons here:
3531 * - We deleted a port but there are still a few packets queued up
3534 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3535 * we don't know about.
3537 * - Packet arrived on the local port but the local port is not
3540 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3542 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3544 ofproto->up.name, flow->in_port);
3548 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3553 /* Drop frames for reserved multicast addresses. */
3554 if (eth_addr_is_reserved(flow->dl_dst)) {
3558 /* Drop frames on bundles reserved for mirroring. */
3559 if (in_bundle->mirror_out) {
3561 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3562 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3563 "%s, which is reserved exclusively for mirroring",
3564 ofproto->up.name, in_bundle->name);
3569 if (in_bundle->bond) {
3570 struct mac_entry *mac;
3572 switch (bond_check_admissibility(in_bundle->bond, in_port,
3573 flow->dl_dst, tags)) {
3580 case BV_DROP_IF_MOVED:
3581 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3582 if (mac && mac->port.p != in_bundle &&
3583 (!is_gratuitous_arp(flow)
3584 || mac_entry_is_grat_arp_locked(mac))) {
3594 /* If the composed actions may be applied to any packet in the given 'flow',
3595 * returns true. Otherwise, the actions should only be applied to 'packet', or
3596 * not at all, if 'packet' was NULL. */
3598 xlate_normal(struct action_xlate_ctx *ctx)
3600 struct ofbundle *in_bundle;
3601 struct ofbundle *out_bundle;
3602 struct mac_entry *mac;
3605 /* Check whether we should drop packets in this flow. */
3606 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3607 &ctx->tags, &vlan, &in_bundle)) {
3612 /* Learn source MAC (but don't try to learn from revalidation). */
3614 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3617 /* Determine output bundle. */
3618 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3621 out_bundle = mac->port.p;
3622 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3623 /* If we are revalidating but don't have a learning entry then eject
3624 * the flow. Installing a flow that floods packets opens up a window
3625 * of time where we could learn from a packet reflected on a bond and
3626 * blackhole packets before the learning table is updated to reflect
3627 * the correct port. */
3630 out_bundle = OFBUNDLE_FLOOD;
3633 /* Don't send packets out their input bundles. */
3634 if (in_bundle == out_bundle) {
3640 compose_actions(ctx, vlan, in_bundle, out_bundle);
3647 get_drop_frags(struct ofproto *ofproto_)
3649 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3652 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3657 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3659 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3661 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3665 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3666 const struct flow *flow,
3667 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3669 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3672 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3673 ofproto->max_ports);
3675 struct action_xlate_ctx ctx;
3676 struct ofpbuf *odp_actions;
3678 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3679 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3680 dpif_execute(ofproto->dpif, odp_actions->data, odp_actions->size,
3682 ofpbuf_delete(odp_actions);
3688 get_netflow_ids(const struct ofproto *ofproto_,
3689 uint8_t *engine_type, uint8_t *engine_id)
3691 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3693 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3696 static struct ofproto_dpif *
3697 ofproto_dpif_lookup(const char *name)
3699 struct ofproto *ofproto = ofproto_lookup(name);
3700 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3701 ? ofproto_dpif_cast(ofproto)
3706 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3707 const char *args, void *aux OVS_UNUSED)
3709 struct ds ds = DS_EMPTY_INITIALIZER;
3710 const struct ofproto_dpif *ofproto;
3711 const struct mac_entry *e;
3713 ofproto = ofproto_dpif_lookup(args);
3715 unixctl_command_reply(conn, 501, "no such bridge");
3719 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3720 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3721 struct ofbundle *bundle = e->port.p;
3722 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
3723 ofbundle_get_a_port(bundle)->odp_port,
3724 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3726 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3730 struct ofproto_trace {
3731 struct action_xlate_ctx ctx;
3737 trace_format_rule(struct ds *result, int level, const struct rule *rule)
3739 ds_put_char_multiple(result, '\t', level);
3741 ds_put_cstr(result, "No match\n");
3745 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
3746 ntohll(rule->flow_cookie));
3747 cls_rule_format(&rule->cr, result);
3748 ds_put_char(result, '\n');
3750 ds_put_char_multiple(result, '\t', level);
3751 ds_put_cstr(result, "OpenFlow ");
3752 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
3753 rule->n_actions * sizeof *rule->actions);
3754 ds_put_char(result, '\n');
3758 trace_format_flow(struct ds *result, int level, const char *title,
3759 struct ofproto_trace *trace)
3761 ds_put_char_multiple(result, '\t', level);
3762 ds_put_format(result, "%s: ", title);
3763 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3764 ds_put_cstr(result, "unchanged");
3766 flow_format(result, &trace->ctx.flow);
3767 trace->flow = trace->ctx.flow;
3769 ds_put_char(result, '\n');
3773 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3775 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3776 struct ds *result = trace->result;
3778 ds_put_char(result, '\n');
3779 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
3780 trace_format_rule(result, ctx->recurse + 1, &rule->up);
3784 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
3785 void *aux OVS_UNUSED)
3787 char *dpname, *in_port_s, *tun_id_s, *packet_s;
3788 char *args = xstrdup(args_);
3789 char *save_ptr = NULL;
3790 struct ofproto_dpif *ofproto;
3791 struct ofpbuf packet;
3792 struct rule_dpif *rule;
3799 ofpbuf_init(&packet, strlen(args) / 2);
3802 dpname = strtok_r(args, " ", &save_ptr);
3803 tun_id_s = strtok_r(NULL, " ", &save_ptr);
3804 in_port_s = strtok_r(NULL, " ", &save_ptr);
3805 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
3806 if (!dpname || !in_port_s || !packet_s) {
3807 unixctl_command_reply(conn, 501, "Bad command syntax");
3811 ofproto = ofproto_dpif_lookup(dpname);
3813 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
3818 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
3819 in_port = ofp_port_to_odp_port(atoi(in_port_s));
3821 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
3822 packet_s += strspn(packet_s, " ");
3823 if (*packet_s != '\0') {
3824 unixctl_command_reply(conn, 501, "Trailing garbage in command");
3827 if (packet.size < ETH_HEADER_LEN) {
3828 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
3832 ds_put_cstr(&result, "Packet: ");
3833 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
3834 ds_put_cstr(&result, s);
3837 flow_extract(&packet, tun_id, in_port, &flow);
3838 ds_put_cstr(&result, "Flow: ");
3839 flow_format(&result, &flow);
3840 ds_put_char(&result, '\n');
3842 rule = rule_dpif_lookup(ofproto, &flow);
3843 trace_format_rule(&result, 0, &rule->up);
3845 struct ofproto_trace trace;
3846 struct ofpbuf *odp_actions;
3848 trace.result = &result;
3850 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
3851 trace.ctx.resubmit_hook = trace_resubmit;
3852 odp_actions = xlate_actions(&trace.ctx,
3853 rule->up.actions, rule->up.n_actions);
3855 ds_put_char(&result, '\n');
3856 trace_format_flow(&result, 0, "Final flow", &trace);
3857 ds_put_cstr(&result, "Datapath actions: ");
3858 format_odp_actions(&result, odp_actions->data, odp_actions->size);
3859 ofpbuf_delete(odp_actions);
3862 unixctl_command_reply(conn, 200, ds_cstr(&result));
3865 ds_destroy(&result);
3866 ofpbuf_uninit(&packet);
3871 ofproto_dpif_unixctl_init(void)
3873 static bool registered;
3879 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
3880 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
3883 const struct ofproto_class ofproto_dpif_class = {
3910 port_is_lacp_current,
3917 rule_modify_actions,
3930 is_mirror_output_bundle,