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_settings *);
301 struct ofproto_dpif {
310 struct netflow *netflow;
311 struct ofproto_sflow *sflow;
312 struct hmap bundles; /* Contains "struct ofbundle"s. */
313 struct mac_learning *ml;
314 struct ofmirror *mirrors[MAX_MIRRORS];
315 bool has_bonded_bundles;
318 struct timer next_expiration;
322 bool need_revalidate;
323 struct tag_set revalidate_set;
326 static void ofproto_dpif_unixctl_init(void);
328 static struct ofproto_dpif *
329 ofproto_dpif_cast(const struct ofproto *ofproto)
331 assert(ofproto->ofproto_class == &ofproto_dpif_class);
332 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
335 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
337 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
340 /* Packet processing. */
341 static void update_learning_table(struct ofproto_dpif *,
342 const struct flow *, int vlan,
344 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
345 bool have_packet, tag_type *, int *vlanp,
346 struct ofbundle **in_bundlep);
347 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
349 /* Flow expiration. */
350 static int expire(struct ofproto_dpif *);
353 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
354 const struct ofpbuf *packet);
356 /* Global variables. */
357 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
359 /* Factory functions. */
362 enumerate_types(struct sset *types)
364 dp_enumerate_types(types);
368 enumerate_names(const char *type, struct sset *names)
370 return dp_enumerate_names(type, names);
374 del(const char *type, const char *name)
379 error = dpif_open(name, type, &dpif);
381 error = dpif_delete(dpif);
387 /* Basic life-cycle. */
389 static struct ofproto *
392 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
397 dealloc(struct ofproto *ofproto_)
399 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
404 construct(struct ofproto *ofproto_)
406 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
407 const char *name = ofproto->up.name;
411 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
413 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
417 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
418 ofproto->n_matches = 0;
420 error = dpif_recv_set_mask(ofproto->dpif,
421 ((1u << DPIF_UC_MISS) |
422 (1u << DPIF_UC_ACTION) |
423 (1u << DPIF_UC_SAMPLE)));
425 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
426 dpif_close(ofproto->dpif);
429 dpif_flow_flush(ofproto->dpif);
430 dpif_recv_purge(ofproto->dpif);
432 ofproto->netflow = NULL;
433 ofproto->sflow = NULL;
434 hmap_init(&ofproto->bundles);
435 ofproto->ml = mac_learning_create();
436 for (i = 0; i < MAX_MIRRORS; i++) {
437 ofproto->mirrors[i] = NULL;
439 ofproto->has_bonded_bundles = false;
441 timer_set_duration(&ofproto->next_expiration, 1000);
443 hmap_init(&ofproto->facets);
444 ofproto->need_revalidate = false;
445 tag_set_init(&ofproto->revalidate_set);
447 ofproto->up.tables = xmalloc(sizeof *ofproto->up.tables);
448 classifier_init(&ofproto->up.tables[0]);
449 ofproto->up.n_tables = 1;
451 ofproto_dpif_unixctl_init();
457 destruct(struct ofproto *ofproto_)
459 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
462 for (i = 0; i < MAX_MIRRORS; i++) {
463 mirror_destroy(ofproto->mirrors[i]);
466 netflow_destroy(ofproto->netflow);
467 ofproto_sflow_destroy(ofproto->sflow);
468 hmap_destroy(&ofproto->bundles);
469 mac_learning_destroy(ofproto->ml);
471 hmap_destroy(&ofproto->facets);
473 dpif_close(ofproto->dpif);
477 run(struct ofproto *ofproto_)
479 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
480 struct ofport_dpif *ofport;
481 struct ofbundle *bundle;
484 dpif_run(ofproto->dpif);
486 for (i = 0; i < 50; i++) {
487 struct dpif_upcall packet;
490 error = dpif_recv(ofproto->dpif, &packet);
492 if (error == ENODEV) {
493 /* Datapath destroyed. */
499 handle_upcall(ofproto, &packet);
502 if (timer_expired(&ofproto->next_expiration)) {
503 int delay = expire(ofproto);
504 timer_set_duration(&ofproto->next_expiration, delay);
507 if (ofproto->netflow) {
508 netflow_run(ofproto->netflow);
510 if (ofproto->sflow) {
511 ofproto_sflow_run(ofproto->sflow);
514 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
517 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
521 /* Now revalidate if there's anything to do. */
522 if (ofproto->need_revalidate
523 || !tag_set_is_empty(&ofproto->revalidate_set)) {
524 struct tag_set revalidate_set = ofproto->revalidate_set;
525 bool revalidate_all = ofproto->need_revalidate;
526 struct facet *facet, *next;
528 /* Clear the revalidation flags. */
529 tag_set_init(&ofproto->revalidate_set);
530 ofproto->need_revalidate = false;
532 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
534 || tag_set_intersects(&revalidate_set, facet->tags)) {
535 facet_revalidate(ofproto, facet);
544 wait(struct ofproto *ofproto_)
546 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
547 struct ofport_dpif *ofport;
548 struct ofbundle *bundle;
550 dpif_wait(ofproto->dpif);
551 dpif_recv_wait(ofproto->dpif);
552 if (ofproto->sflow) {
553 ofproto_sflow_wait(ofproto->sflow);
555 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
556 poll_immediate_wake();
558 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
561 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
564 if (ofproto->need_revalidate) {
565 /* Shouldn't happen, but if it does just go around again. */
566 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
567 poll_immediate_wake();
569 timer_wait(&ofproto->next_expiration);
574 flush(struct ofproto *ofproto_)
576 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
577 struct facet *facet, *next_facet;
579 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
580 /* Mark the facet as not installed so that facet_remove() doesn't
581 * bother trying to uninstall it. There is no point in uninstalling it
582 * individually since we are about to blow away all the facets with
583 * dpif_flow_flush(). */
584 facet->installed = false;
585 facet->dp_packet_count = 0;
586 facet->dp_byte_count = 0;
587 facet_remove(ofproto, facet);
589 dpif_flow_flush(ofproto->dpif);
593 get_features(struct ofproto *ofproto_ OVS_UNUSED,
594 bool *arp_match_ip, uint32_t *actions)
596 *arp_match_ip = true;
597 *actions = ((1u << OFPAT_OUTPUT) |
598 (1u << OFPAT_SET_VLAN_VID) |
599 (1u << OFPAT_SET_VLAN_PCP) |
600 (1u << OFPAT_STRIP_VLAN) |
601 (1u << OFPAT_SET_DL_SRC) |
602 (1u << OFPAT_SET_DL_DST) |
603 (1u << OFPAT_SET_NW_SRC) |
604 (1u << OFPAT_SET_NW_DST) |
605 (1u << OFPAT_SET_NW_TOS) |
606 (1u << OFPAT_SET_TP_SRC) |
607 (1u << OFPAT_SET_TP_DST) |
608 (1u << OFPAT_ENQUEUE));
612 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
614 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
617 strcpy(ots->name, "classifier");
619 dpif_get_dp_stats(ofproto->dpif, &s);
620 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
621 put_32aligned_be64(&ots->matched_count,
622 htonll(s.n_hit + ofproto->n_matches));
626 set_netflow(struct ofproto *ofproto_,
627 const struct netflow_options *netflow_options)
629 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
631 if (netflow_options) {
632 if (!ofproto->netflow) {
633 ofproto->netflow = netflow_create();
635 return netflow_set_options(ofproto->netflow, netflow_options);
637 netflow_destroy(ofproto->netflow);
638 ofproto->netflow = NULL;
643 static struct ofport *
646 struct ofport_dpif *port = xmalloc(sizeof *port);
651 port_dealloc(struct ofport *port_)
653 struct ofport_dpif *port = ofport_dpif_cast(port_);
658 port_construct(struct ofport *port_)
660 struct ofport_dpif *port = ofport_dpif_cast(port_);
661 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
663 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
666 port->tag = tag_create_random();
668 if (ofproto->sflow) {
669 ofproto_sflow_add_port(ofproto->sflow, port->odp_port,
670 netdev_get_name(port->up.netdev));
677 port_destruct(struct ofport *port_)
679 struct ofport_dpif *port = ofport_dpif_cast(port_);
680 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
682 bundle_remove(port_);
683 set_cfm(port_, NULL);
684 if (ofproto->sflow) {
685 ofproto_sflow_del_port(ofproto->sflow, port->odp_port);
690 port_modified(struct ofport *port_)
692 struct ofport_dpif *port = ofport_dpif_cast(port_);
694 if (port->bundle && port->bundle->bond) {
695 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
700 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
702 struct ofport_dpif *port = ofport_dpif_cast(port_);
703 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
704 ovs_be32 changed = old_config ^ port->up.opp.config;
706 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
707 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
708 ofproto->need_revalidate = true;
713 set_sflow(struct ofproto *ofproto_,
714 const struct ofproto_sflow_options *sflow_options)
716 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
717 struct ofproto_sflow *os = ofproto->sflow;
720 struct ofport_dpif *ofport;
722 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
723 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
724 ofproto_sflow_add_port(os, ofport->odp_port,
725 netdev_get_name(ofport->up.netdev));
728 ofproto_sflow_set_options(os, sflow_options);
730 ofproto_sflow_destroy(os);
731 ofproto->sflow = NULL;
737 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
739 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
746 ofport->cfm = cfm_create();
749 if (cfm_configure(ofport->cfm, s)) {
755 cfm_destroy(ofport->cfm);
761 get_cfm_fault(const struct ofport *ofport_)
763 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
765 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
770 /* Expires all MAC learning entries associated with 'port' and forces ofproto
771 * to revalidate every flow. */
773 bundle_flush_macs(struct ofbundle *bundle)
775 struct ofproto_dpif *ofproto = bundle->ofproto;
776 struct mac_learning *ml = ofproto->ml;
777 struct mac_entry *mac, *next_mac;
779 ofproto->need_revalidate = true;
780 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
781 if (mac->port.p == bundle) {
782 mac_learning_expire(ml, mac);
787 static struct ofbundle *
788 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
790 struct ofbundle *bundle;
792 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
794 if (bundle->aux == aux) {
801 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
802 * ones that are found to 'bundles'. */
804 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
805 void **auxes, size_t n_auxes,
806 struct hmapx *bundles)
811 for (i = 0; i < n_auxes; i++) {
812 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
814 hmapx_add(bundles, bundle);
820 bundle_del_port(struct ofport_dpif *port)
822 struct ofbundle *bundle = port->bundle;
824 bundle->ofproto->need_revalidate = true;
826 list_remove(&port->bundle_node);
830 lacp_slave_unregister(bundle->lacp, port);
833 bond_slave_unregister(bundle->bond, port);
836 bundle->floodable = true;
837 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
838 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
839 bundle->floodable = false;
845 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
846 struct lacp_slave_settings *lacp,
847 uint32_t bond_stable_id)
849 struct ofport_dpif *port;
851 port = get_ofp_port(bundle->ofproto, ofp_port);
856 if (port->bundle != bundle) {
857 bundle->ofproto->need_revalidate = true;
859 bundle_del_port(port);
862 port->bundle = bundle;
863 list_push_back(&bundle->ports, &port->bundle_node);
864 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
865 bundle->floodable = false;
869 lacp_slave_register(bundle->lacp, port, lacp);
872 port->bond_stable_id = bond_stable_id;
878 bundle_destroy(struct ofbundle *bundle)
880 struct ofproto_dpif *ofproto;
881 struct ofport_dpif *port, *next_port;
888 ofproto = bundle->ofproto;
889 for (i = 0; i < MAX_MIRRORS; i++) {
890 struct ofmirror *m = ofproto->mirrors[i];
892 if (m->out == bundle) {
894 } else if (hmapx_find_and_delete(&m->srcs, bundle)
895 || hmapx_find_and_delete(&m->dsts, bundle)) {
896 ofproto->need_revalidate = true;
901 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
902 bundle_del_port(port);
905 bundle_flush_macs(bundle);
906 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
908 free(bundle->trunks);
909 lacp_destroy(bundle->lacp);
910 bond_destroy(bundle->bond);
915 bundle_set(struct ofproto *ofproto_, void *aux,
916 const struct ofproto_bundle_settings *s)
918 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
919 bool need_flush = false;
920 const unsigned long *trunks;
921 struct ofport_dpif *port;
922 struct ofbundle *bundle;
927 bundle_destroy(bundle_lookup(ofproto, aux));
931 assert(s->n_slaves == 1 || s->bond != NULL);
932 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
934 bundle = bundle_lookup(ofproto, aux);
936 bundle = xmalloc(sizeof *bundle);
938 bundle->ofproto = ofproto;
939 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
940 hash_pointer(aux, 0));
944 list_init(&bundle->ports);
946 bundle->trunks = NULL;
950 bundle->floodable = true;
952 bundle->src_mirrors = 0;
953 bundle->dst_mirrors = 0;
954 bundle->mirror_out = 0;
957 if (!bundle->name || strcmp(s->name, bundle->name)) {
959 bundle->name = xstrdup(s->name);
965 bundle->lacp = lacp_create();
967 lacp_configure(bundle->lacp, s->lacp);
969 lacp_destroy(bundle->lacp);
973 /* Update set of ports. */
975 for (i = 0; i < s->n_slaves; i++) {
976 if (!bundle_add_port(bundle, s->slaves[i],
977 s->lacp ? &s->lacp_slaves[i] : NULL,
978 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
982 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
983 struct ofport_dpif *next_port;
985 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
986 for (i = 0; i < s->n_slaves; i++) {
987 if (s->slaves[i] == port->up.ofp_port) {
992 bundle_del_port(port);
996 assert(list_size(&bundle->ports) <= s->n_slaves);
998 if (list_is_empty(&bundle->ports)) {
999 bundle_destroy(bundle);
1004 if (s->vlan != bundle->vlan) {
1005 bundle->vlan = s->vlan;
1009 /* Get trunked VLANs. */
1010 trunks = s->vlan == -1 ? NULL : s->trunks;
1011 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1012 free(bundle->trunks);
1013 bundle->trunks = vlan_bitmap_clone(trunks);
1018 if (!list_is_short(&bundle->ports)) {
1019 bundle->ofproto->has_bonded_bundles = true;
1021 if (bond_reconfigure(bundle->bond, s->bond)) {
1022 ofproto->need_revalidate = true;
1025 bundle->bond = bond_create(s->bond);
1026 ofproto->need_revalidate = true;
1029 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1030 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1034 bond_destroy(bundle->bond);
1035 bundle->bond = NULL;
1038 /* If we changed something that would affect MAC learning, un-learn
1039 * everything on this port and force flow revalidation. */
1041 bundle_flush_macs(bundle);
1048 bundle_remove(struct ofport *port_)
1050 struct ofport_dpif *port = ofport_dpif_cast(port_);
1051 struct ofbundle *bundle = port->bundle;
1054 bundle_del_port(port);
1055 if (list_is_empty(&bundle->ports)) {
1056 bundle_destroy(bundle);
1057 } else if (list_is_short(&bundle->ports)) {
1058 bond_destroy(bundle->bond);
1059 bundle->bond = NULL;
1065 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1067 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1068 struct ofport_dpif *port = port_;
1069 uint8_t ea[ETH_ADDR_LEN];
1072 error = netdev_get_etheraddr(port->up.netdev, ea);
1074 struct lacp_pdu *packet_pdu;
1075 struct ofpbuf packet;
1077 ofpbuf_init(&packet, 0);
1078 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1079 sizeof *packet_pdu);
1081 error = netdev_send(port->up.netdev, &packet);
1083 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1084 "(%s)", port->bundle->name,
1085 netdev_get_name(port->up.netdev), strerror(error));
1087 ofpbuf_uninit(&packet);
1089 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1090 "%s (%s)", port->bundle->name,
1091 netdev_get_name(port->up.netdev), strerror(error));
1096 bundle_send_learning_packets(struct ofbundle *bundle)
1098 struct ofproto_dpif *ofproto = bundle->ofproto;
1099 int error, n_packets, n_errors;
1100 struct mac_entry *e;
1102 error = n_packets = n_errors = 0;
1103 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1104 if (e->port.p != bundle) {
1105 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1115 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1116 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1117 "packets, last error was: %s",
1118 bundle->name, n_errors, n_packets, strerror(error));
1120 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1121 bundle->name, n_packets);
1126 bundle_run(struct ofbundle *bundle)
1129 lacp_run(bundle->lacp, send_pdu_cb);
1132 struct ofport_dpif *port;
1134 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1135 bool may_enable = lacp_slave_may_enable(bundle->lacp, port);
1136 bond_slave_set_lacp_may_enable(bundle->bond, port, may_enable);
1139 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1140 lacp_negotiated(bundle->lacp));
1141 if (bond_should_send_learning_packets(bundle->bond)) {
1142 bundle_send_learning_packets(bundle);
1148 bundle_wait(struct ofbundle *bundle)
1151 lacp_wait(bundle->lacp);
1154 bond_wait(bundle->bond);
1161 mirror_scan(struct ofproto_dpif *ofproto)
1165 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1166 if (!ofproto->mirrors[idx]) {
1173 static struct ofmirror *
1174 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1178 for (i = 0; i < MAX_MIRRORS; i++) {
1179 struct ofmirror *mirror = ofproto->mirrors[i];
1180 if (mirror && mirror->aux == aux) {
1189 mirror_set(struct ofproto *ofproto_, void *aux,
1190 const struct ofproto_mirror_settings *s)
1192 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1193 mirror_mask_t mirror_bit;
1194 struct ofbundle *bundle;
1195 struct ofmirror *mirror;
1196 struct ofbundle *out;
1197 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1198 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1201 mirror = mirror_lookup(ofproto, aux);
1203 mirror_destroy(mirror);
1209 idx = mirror_scan(ofproto);
1211 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1213 ofproto->up.name, MAX_MIRRORS, s->name);
1217 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1218 mirror->ofproto = ofproto;
1220 mirror->out_vlan = -1;
1221 mirror->name = NULL;
1224 if (!mirror->name || strcmp(s->name, mirror->name)) {
1226 mirror->name = xstrdup(s->name);
1229 /* Get the new configuration. */
1230 if (s->out_bundle) {
1231 out = bundle_lookup(ofproto, s->out_bundle);
1233 mirror_destroy(mirror);
1239 out_vlan = s->out_vlan;
1241 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1242 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1244 /* If the configuration has not changed, do nothing. */
1245 if (hmapx_equals(&srcs, &mirror->srcs)
1246 && hmapx_equals(&dsts, &mirror->dsts)
1247 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1248 && mirror->out == out
1249 && mirror->out_vlan == out_vlan)
1251 hmapx_destroy(&srcs);
1252 hmapx_destroy(&dsts);
1256 hmapx_swap(&srcs, &mirror->srcs);
1257 hmapx_destroy(&srcs);
1259 hmapx_swap(&dsts, &mirror->dsts);
1260 hmapx_destroy(&dsts);
1262 free(mirror->vlans);
1263 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1266 mirror->out_vlan = out_vlan;
1268 /* Update bundles. */
1269 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1270 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1271 if (hmapx_contains(&mirror->srcs, bundle)) {
1272 bundle->src_mirrors |= mirror_bit;
1274 bundle->src_mirrors &= ~mirror_bit;
1277 if (hmapx_contains(&mirror->dsts, bundle)) {
1278 bundle->dst_mirrors |= mirror_bit;
1280 bundle->dst_mirrors &= ~mirror_bit;
1283 if (mirror->out == bundle) {
1284 bundle->mirror_out |= mirror_bit;
1286 bundle->mirror_out &= ~mirror_bit;
1290 ofproto->need_revalidate = true;
1291 mac_learning_flush(ofproto->ml);
1297 mirror_destroy(struct ofmirror *mirror)
1299 struct ofproto_dpif *ofproto;
1300 mirror_mask_t mirror_bit;
1301 struct ofbundle *bundle;
1307 ofproto = mirror->ofproto;
1308 ofproto->need_revalidate = true;
1309 mac_learning_flush(ofproto->ml);
1311 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1312 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1313 bundle->src_mirrors &= ~mirror_bit;
1314 bundle->dst_mirrors &= ~mirror_bit;
1315 bundle->mirror_out &= ~mirror_bit;
1318 hmapx_destroy(&mirror->srcs);
1319 hmapx_destroy(&mirror->dsts);
1320 free(mirror->vlans);
1322 ofproto->mirrors[mirror->idx] = NULL;
1328 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1330 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1331 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1332 ofproto->need_revalidate = true;
1333 mac_learning_flush(ofproto->ml);
1339 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1341 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1342 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1343 return bundle && bundle->mirror_out != 0;
1348 static struct ofport_dpif *
1349 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1351 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1352 return ofport ? ofport_dpif_cast(ofport) : NULL;
1355 static struct ofport_dpif *
1356 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1358 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1362 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1363 struct dpif_port *dpif_port)
1365 ofproto_port->name = dpif_port->name;
1366 ofproto_port->type = dpif_port->type;
1367 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1371 port_run(struct ofport_dpif *ofport)
1374 cfm_run(ofport->cfm);
1376 if (cfm_should_send_ccm(ofport->cfm)) {
1377 struct ofpbuf packet;
1379 ofpbuf_init(&packet, 0);
1380 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1381 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1382 ofport->odp_port, &packet);
1383 ofpbuf_uninit(&packet);
1389 port_wait(struct ofport_dpif *ofport)
1392 cfm_wait(ofport->cfm);
1397 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1398 struct ofproto_port *ofproto_port)
1400 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1401 struct dpif_port dpif_port;
1404 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1406 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1412 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1414 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1418 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1420 *ofp_portp = odp_port_to_ofp_port(odp_port);
1426 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1428 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1431 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1433 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1435 /* The caller is going to close ofport->up.netdev. If this is a
1436 * bonded port, then the bond is using that netdev, so remove it
1437 * from the bond. The client will need to reconfigure everything
1438 * after deleting ports, so then the slave will get re-added. */
1439 bundle_remove(&ofport->up);
1445 struct port_dump_state {
1446 struct dpif_port_dump dump;
1451 port_dump_start(const struct ofproto *ofproto_, void **statep)
1453 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1454 struct port_dump_state *state;
1456 *statep = state = xmalloc(sizeof *state);
1457 dpif_port_dump_start(&state->dump, ofproto->dpif);
1458 state->done = false;
1463 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1464 struct ofproto_port *port)
1466 struct port_dump_state *state = state_;
1467 struct dpif_port dpif_port;
1469 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1470 ofproto_port_from_dpif_port(port, &dpif_port);
1473 int error = dpif_port_dump_done(&state->dump);
1475 return error ? error : EOF;
1480 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1482 struct port_dump_state *state = state_;
1485 dpif_port_dump_done(&state->dump);
1492 port_poll(const struct ofproto *ofproto_, char **devnamep)
1494 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1495 return dpif_port_poll(ofproto->dpif, devnamep);
1499 port_poll_wait(const struct ofproto *ofproto_)
1501 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1502 dpif_port_poll_wait(ofproto->dpif);
1506 port_is_lacp_current(const struct ofport *ofport_)
1508 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1509 return (ofport->bundle && ofport->bundle->lacp
1510 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1514 /* Upcall handling. */
1516 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1517 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1518 * their individual configurations.
1520 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1521 * Otherwise, ownership is transferred to this function. */
1523 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1524 const struct flow *flow, bool clone)
1526 struct ofputil_packet_in pin;
1528 pin.packet = upcall->packet;
1529 pin.in_port = flow->in_port;
1530 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1531 pin.buffer_id = 0; /* not yet known */
1532 pin.send_len = upcall->userdata;
1533 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1534 clone ? NULL : upcall->packet);
1538 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1539 const struct ofpbuf *packet)
1541 if (cfm_should_process_flow(flow)) {
1542 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1543 if (ofport && ofport->cfm) {
1544 cfm_process_heartbeat(ofport->cfm, packet);
1547 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1548 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1549 if (port && port->bundle && port->bundle->lacp) {
1550 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1552 lacp_process_pdu(port->bundle->lacp, port, pdu);
1561 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1563 struct facet *facet;
1566 /* Obtain in_port and tun_id, at least. */
1567 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1569 /* Set header pointers in 'flow'. */
1570 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1572 /* Handle 802.1ag and LACP. */
1573 if (process_special(ofproto, &flow, upcall->packet)) {
1574 ofpbuf_delete(upcall->packet);
1575 ofproto->n_matches++;
1579 /* Check with in-band control to see if this packet should be sent
1580 * to the local port regardless of the flow table. */
1581 if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1582 send_packet(ofproto, ODPP_LOCAL, upcall->packet);
1585 facet = facet_lookup_valid(ofproto, &flow);
1587 struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow);
1589 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1590 struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1592 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1593 COVERAGE_INC(ofproto_dpif_no_packet_in);
1594 /* XXX install 'drop' flow entry */
1595 ofpbuf_delete(upcall->packet);
1599 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1603 send_packet_in(ofproto, upcall, &flow, false);
1607 facet = facet_create(rule, &flow, upcall->packet);
1608 } else if (!facet->may_install) {
1609 /* The facet is not installable, that is, we need to process every
1610 * packet, so process the current packet's actions into 'facet'. */
1611 facet_make_actions(ofproto, facet, upcall->packet);
1614 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1616 * Extra-special case for fail-open mode.
1618 * We are in fail-open mode and the packet matched the fail-open rule,
1619 * but we are connected to a controller too. We should send the packet
1620 * up to the controller in the hope that it will try to set up a flow
1621 * and thereby allow us to exit fail-open.
1623 * See the top-level comment in fail-open.c for more information.
1625 send_packet_in(ofproto, upcall, &flow, true);
1628 facet_execute(ofproto, facet, upcall->packet);
1629 facet_install(ofproto, facet, false);
1630 ofproto->n_matches++;
1634 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1638 switch (upcall->type) {
1639 case DPIF_UC_ACTION:
1640 COVERAGE_INC(ofproto_dpif_ctlr_action);
1641 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1642 send_packet_in(ofproto, upcall, &flow, false);
1645 case DPIF_UC_SAMPLE:
1646 if (ofproto->sflow) {
1647 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1648 ofproto_sflow_received(ofproto->sflow, upcall, &flow);
1650 ofpbuf_delete(upcall->packet);
1654 handle_miss_upcall(ofproto, upcall);
1657 case DPIF_N_UC_TYPES:
1659 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1664 /* Flow expiration. */
1666 static int facet_max_idle(const struct ofproto_dpif *);
1667 static void update_stats(struct ofproto_dpif *);
1668 static void rule_expire(struct rule_dpif *);
1669 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1671 /* This function is called periodically by run(). Its job is to collect
1672 * updates for the flows that have been installed into the datapath, most
1673 * importantly when they last were used, and then use that information to
1674 * expire flows that have not been used recently.
1676 * Returns the number of milliseconds after which it should be called again. */
1678 expire(struct ofproto_dpif *ofproto)
1680 struct rule_dpif *rule, *next_rule;
1681 struct cls_cursor cursor;
1684 /* Update stats for each flow in the datapath. */
1685 update_stats(ofproto);
1687 /* Expire facets that have been idle too long. */
1688 dp_max_idle = facet_max_idle(ofproto);
1689 expire_facets(ofproto, dp_max_idle);
1691 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1692 cls_cursor_init(&cursor, &ofproto->up.tables[0], NULL);
1693 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1697 /* All outstanding data in existing flows has been accounted, so it's a
1698 * good time to do bond rebalancing. */
1699 if (ofproto->has_bonded_bundles) {
1700 struct ofbundle *bundle;
1702 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1704 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1709 return MIN(dp_max_idle, 1000);
1712 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1714 * This function also pushes statistics updates to rules which each facet
1715 * resubmits into. Generally these statistics will be accurate. However, if a
1716 * facet changes the rule it resubmits into at some time in between
1717 * update_stats() runs, it is possible that statistics accrued to the
1718 * old rule will be incorrectly attributed to the new rule. This could be
1719 * avoided by calling update_stats() whenever rules are created or
1720 * deleted. However, the performance impact of making so many calls to the
1721 * datapath do not justify the benefit of having perfectly accurate statistics.
1724 update_stats(struct ofproto_dpif *p)
1726 const struct dpif_flow_stats *stats;
1727 struct dpif_flow_dump dump;
1728 const struct nlattr *key;
1731 dpif_flow_dump_start(&dump, p->dpif);
1732 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1733 struct facet *facet;
1736 if (odp_flow_key_to_flow(key, key_len, &flow)) {
1740 odp_flow_key_format(key, key_len, &s);
1741 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
1747 facet = facet_find(p, &flow);
1749 if (facet && facet->installed) {
1751 if (stats->n_packets >= facet->dp_packet_count) {
1752 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1753 facet->packet_count += extra;
1755 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1758 if (stats->n_bytes >= facet->dp_byte_count) {
1759 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1761 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1764 facet->dp_packet_count = stats->n_packets;
1765 facet->dp_byte_count = stats->n_bytes;
1767 facet_update_time(p, facet, stats->used);
1768 facet_account(p, facet, stats->n_bytes);
1769 facet_push_stats(facet);
1771 /* There's a flow in the datapath that we know nothing about.
1773 COVERAGE_INC(facet_unexpected);
1774 dpif_flow_del(p->dpif, key, key_len, NULL);
1777 dpif_flow_dump_done(&dump);
1780 /* Calculates and returns the number of milliseconds of idle time after which
1781 * facets should expire from the datapath and we should fold their statistics
1782 * into their parent rules in userspace. */
1784 facet_max_idle(const struct ofproto_dpif *ofproto)
1787 * Idle time histogram.
1789 * Most of the time a switch has a relatively small number of facets. When
1790 * this is the case we might as well keep statistics for all of them in
1791 * userspace and to cache them in the kernel datapath for performance as
1794 * As the number of facets increases, the memory required to maintain
1795 * statistics about them in userspace and in the kernel becomes
1796 * significant. However, with a large number of facets it is likely that
1797 * only a few of them are "heavy hitters" that consume a large amount of
1798 * bandwidth. At this point, only heavy hitters are worth caching in the
1799 * kernel and maintaining in userspaces; other facets we can discard.
1801 * The technique used to compute the idle time is to build a histogram with
1802 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
1803 * that is installed in the kernel gets dropped in the appropriate bucket.
1804 * After the histogram has been built, we compute the cutoff so that only
1805 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
1806 * cached. At least the most-recently-used bucket of facets is kept, so
1807 * actually an arbitrary number of facets can be kept in any given
1808 * expiration run (though the next run will delete most of those unless
1809 * they receive additional data).
1811 * This requires a second pass through the facets, in addition to the pass
1812 * made by update_stats(), because the former function never looks
1813 * at uninstallable facets.
1815 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1816 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1817 int buckets[N_BUCKETS] = { 0 };
1818 struct facet *facet;
1823 total = hmap_count(&ofproto->facets);
1824 if (total <= 1000) {
1825 return N_BUCKETS * BUCKET_WIDTH;
1828 /* Build histogram. */
1830 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1831 long long int idle = now - facet->used;
1832 int bucket = (idle <= 0 ? 0
1833 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1834 : (unsigned int) idle / BUCKET_WIDTH);
1838 /* Find the first bucket whose flows should be expired. */
1839 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
1840 if (buckets[bucket]) {
1843 subtotal += buckets[bucket++];
1844 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
1849 if (VLOG_IS_DBG_ENABLED()) {
1853 ds_put_cstr(&s, "keep");
1854 for (i = 0; i < N_BUCKETS; i++) {
1856 ds_put_cstr(&s, ", drop");
1859 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1862 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1866 return bucket * BUCKET_WIDTH;
1870 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1872 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1873 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1874 struct ofexpired expired;
1876 if (facet->installed) {
1877 struct dpif_flow_stats stats;
1879 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1881 facet_update_stats(ofproto, facet, &stats);
1884 expired.flow = facet->flow;
1885 expired.packet_count = facet->packet_count;
1886 expired.byte_count = facet->byte_count;
1887 expired.used = facet->used;
1888 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1893 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1895 long long int cutoff = time_msec() - dp_max_idle;
1896 struct facet *facet, *next_facet;
1898 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1899 facet_active_timeout(ofproto, facet);
1900 if (facet->used < cutoff) {
1901 facet_remove(ofproto, facet);
1906 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1907 * then delete it entirely. */
1909 rule_expire(struct rule_dpif *rule)
1911 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1912 struct facet *facet, *next_facet;
1916 /* Has 'rule' expired? */
1918 if (rule->up.hard_timeout
1919 && now > rule->up.created + rule->up.hard_timeout * 1000) {
1920 reason = OFPRR_HARD_TIMEOUT;
1921 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1922 && now > rule->used + rule->up.idle_timeout * 1000) {
1923 reason = OFPRR_IDLE_TIMEOUT;
1928 COVERAGE_INC(ofproto_dpif_expired);
1930 /* Update stats. (This is a no-op if the rule expired due to an idle
1931 * timeout, because that only happens when the rule has no facets left.) */
1932 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1933 facet_remove(ofproto, facet);
1936 /* Get rid of the rule. */
1937 ofproto_rule_expire(&rule->up, reason);
1942 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
1943 * example 'packet' within that flow.
1945 * The caller must already have determined that no facet with an identical
1946 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1947 * the ofproto's classifier table. */
1948 static struct facet *
1949 facet_create(struct rule_dpif *rule, const struct flow *flow,
1950 const struct ofpbuf *packet)
1952 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1953 struct facet *facet;
1955 facet = xzalloc(sizeof *facet);
1956 facet->used = time_msec();
1957 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1958 list_push_back(&rule->facets, &facet->list_node);
1960 facet->flow = *flow;
1961 netflow_flow_init(&facet->nf_flow);
1962 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1964 facet_make_actions(ofproto, facet, packet);
1970 facet_free(struct facet *facet)
1972 free(facet->actions);
1976 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1977 * 'packet', which arrived on 'in_port'.
1979 * Takes ownership of 'packet'. */
1981 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
1982 const struct nlattr *odp_actions, size_t actions_len,
1983 struct ofpbuf *packet)
1985 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1986 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1987 /* As an optimization, avoid a round-trip from userspace to kernel to
1988 * userspace. This also avoids possibly filling up kernel packet
1989 * buffers along the way. */
1990 struct dpif_upcall upcall;
1992 upcall.type = DPIF_UC_ACTION;
1993 upcall.packet = packet;
1996 upcall.userdata = nl_attr_get_u64(odp_actions);
1997 upcall.sample_pool = 0;
1998 upcall.actions = NULL;
1999 upcall.actions_len = 0;
2001 send_packet_in(ofproto, &upcall, flow, false);
2007 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
2008 ofpbuf_delete(packet);
2013 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2014 * statistics appropriately. 'packet' must have at least sizeof(struct
2015 * ofp_packet_in) bytes of headroom.
2017 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2018 * applying flow_extract() to 'packet' would yield the same flow as
2021 * 'facet' must have accurately composed ODP actions; that is, it must not be
2022 * in need of revalidation.
2024 * Takes ownership of 'packet'. */
2026 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2027 struct ofpbuf *packet)
2029 struct dpif_flow_stats stats;
2031 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2033 flow_extract_stats(&facet->flow, packet, &stats);
2034 stats.used = time_msec();
2035 if (execute_odp_actions(ofproto, &facet->flow,
2036 facet->actions, facet->actions_len, packet)) {
2037 facet_update_stats(ofproto, facet, &stats);
2041 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2043 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2044 * rule's statistics, via facet_uninstall().
2046 * - Removes 'facet' from its rule and from ofproto->facets.
2049 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2051 facet_uninstall(ofproto, facet);
2052 facet_flush_stats(ofproto, facet);
2053 hmap_remove(&ofproto->facets, &facet->hmap_node);
2054 list_remove(&facet->list_node);
2058 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2060 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2061 const struct ofpbuf *packet)
2063 const struct rule_dpif *rule = facet->rule;
2064 struct ofpbuf *odp_actions;
2065 struct action_xlate_ctx ctx;
2067 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2068 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2069 facet->tags = ctx.tags;
2070 facet->may_install = ctx.may_set_up_flow;
2071 facet->nf_flow.output_iface = ctx.nf_output_iface;
2073 if (facet->actions_len != odp_actions->size
2074 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2075 free(facet->actions);
2076 facet->actions_len = odp_actions->size;
2077 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2080 ofpbuf_delete(odp_actions);
2084 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2085 const struct nlattr *actions, size_t actions_len,
2086 struct dpif_flow_stats *stats)
2088 struct odputil_keybuf keybuf;
2089 enum dpif_flow_put_flags flags;
2092 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2094 flags |= DPIF_FP_ZERO_STATS;
2095 facet->dp_packet_count = 0;
2096 facet->dp_byte_count = 0;
2099 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2100 odp_flow_key_from_flow(&key, &facet->flow);
2102 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2103 actions, actions_len, stats);
2106 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2107 * 'zero_stats' is true, clears any existing statistics from the datapath for
2110 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2112 struct dpif_flow_stats stats;
2114 if (facet->may_install
2115 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2116 zero_stats ? &stats : NULL)) {
2117 facet->installed = true;
2122 vlan_tci_to_openflow_vlan(ovs_be16 vlan_tci)
2124 return vlan_tci != htons(0) ? vlan_tci_to_vid(vlan_tci) : OFP_VLAN_NONE;
2128 facet_account(struct ofproto_dpif *ofproto,
2129 struct facet *facet, uint64_t extra_bytes)
2131 uint64_t total_bytes, n_bytes;
2132 struct ofbundle *in_bundle;
2133 const struct nlattr *a;
2139 total_bytes = facet->byte_count + extra_bytes;
2140 if (total_bytes <= facet->accounted_bytes) {
2143 n_bytes = total_bytes - facet->accounted_bytes;
2144 facet->accounted_bytes = total_bytes;
2146 /* Test that 'tags' is nonzero to ensure that only flows that include an
2147 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2148 * This works because OFPP_NORMAL always sets a nonzero tag value.
2150 * Feed information from the active flows back into the learning table to
2151 * ensure that table is always in sync with what is actually flowing
2152 * through the datapath. */
2154 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2155 &vlan, &in_bundle)) {
2159 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2161 if (!ofproto->has_bonded_bundles) {
2165 /* This loop feeds byte counters to bond_account() for rebalancing to use
2166 * as a basis. We also need to track the actual VLAN on which the packet
2167 * is going to be sent to ensure that it matches the one passed to
2168 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2170 vlan_tci = facet->flow.vlan_tci;
2171 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2172 struct ofport_dpif *port;
2174 switch (nl_attr_type(a)) {
2175 case ODP_ACTION_ATTR_OUTPUT:
2176 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2177 if (port && port->bundle && port->bundle->bond) {
2178 bond_account(port->bundle->bond, &facet->flow,
2179 vlan_tci_to_openflow_vlan(vlan_tci), n_bytes);
2183 case ODP_ACTION_ATTR_STRIP_VLAN:
2184 vlan_tci = htons(0);
2187 case ODP_ACTION_ATTR_SET_DL_TCI:
2188 vlan_tci = nl_attr_get_be16(a);
2194 /* If 'rule' is installed in the datapath, uninstalls it. */
2196 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2198 if (facet->installed) {
2199 struct odputil_keybuf keybuf;
2200 struct dpif_flow_stats stats;
2203 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2204 odp_flow_key_from_flow(&key, &facet->flow);
2206 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
2207 facet_update_stats(p, facet, &stats);
2209 facet->installed = false;
2210 facet->dp_packet_count = 0;
2211 facet->dp_byte_count = 0;
2213 assert(facet->dp_packet_count == 0);
2214 assert(facet->dp_byte_count == 0);
2218 /* Returns true if the only action for 'facet' is to send to the controller.
2219 * (We don't report NetFlow expiration messages for such facets because they
2220 * are just part of the control logic for the network, not real traffic). */
2222 facet_is_controller_flow(struct facet *facet)
2225 && facet->rule->up.n_actions == 1
2226 && action_outputs_to_port(&facet->rule->up.actions[0],
2227 htons(OFPP_CONTROLLER)));
2230 /* Folds all of 'facet''s statistics into its rule. Also updates the
2231 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2232 * 'facet''s statistics in the datapath should have been zeroed and folded into
2233 * its packet and byte counts before this function is called. */
2235 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2237 assert(!facet->dp_byte_count);
2238 assert(!facet->dp_packet_count);
2240 facet_push_stats(facet);
2241 facet_account(ofproto, facet, 0);
2243 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2244 struct ofexpired expired;
2245 expired.flow = facet->flow;
2246 expired.packet_count = facet->packet_count;
2247 expired.byte_count = facet->byte_count;
2248 expired.used = facet->used;
2249 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2252 facet->rule->packet_count += facet->packet_count;
2253 facet->rule->byte_count += facet->byte_count;
2255 /* Reset counters to prevent double counting if 'facet' ever gets
2257 facet->packet_count = 0;
2258 facet->byte_count = 0;
2259 facet->rs_packet_count = 0;
2260 facet->rs_byte_count = 0;
2261 facet->accounted_bytes = 0;
2263 netflow_flow_clear(&facet->nf_flow);
2266 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2267 * Returns it if found, otherwise a null pointer.
2269 * The returned facet might need revalidation; use facet_lookup_valid()
2270 * instead if that is important. */
2271 static struct facet *
2272 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2274 struct facet *facet;
2276 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2278 if (flow_equal(flow, &facet->flow)) {
2286 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2287 * Returns it if found, otherwise a null pointer.
2289 * The returned facet is guaranteed to be valid. */
2290 static struct facet *
2291 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2293 struct facet *facet = facet_find(ofproto, flow);
2295 /* The facet we found might not be valid, since we could be in need of
2296 * revalidation. If it is not valid, don't return it. */
2298 && ofproto->need_revalidate
2299 && !facet_revalidate(ofproto, facet)) {
2300 COVERAGE_INC(facet_invalidated);
2307 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2309 * - If the rule found is different from 'facet''s current rule, moves
2310 * 'facet' to the new rule and recompiles its actions.
2312 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2313 * where it is and recompiles its actions anyway.
2315 * - If there is none, destroys 'facet'.
2317 * Returns true if 'facet' still exists, false if it has been destroyed. */
2319 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2321 struct action_xlate_ctx ctx;
2322 struct ofpbuf *odp_actions;
2323 struct rule_dpif *new_rule;
2324 bool actions_changed;
2326 COVERAGE_INC(facet_revalidate);
2328 /* Determine the new rule. */
2329 new_rule = rule_dpif_lookup(ofproto, &facet->flow);
2331 /* No new rule, so delete the facet. */
2332 facet_remove(ofproto, facet);
2336 /* Calculate new ODP actions.
2338 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2339 * emit a NetFlow expiration and, if so, we need to have the old state
2340 * around to properly compose it. */
2341 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2342 odp_actions = xlate_actions(&ctx,
2343 new_rule->up.actions, new_rule->up.n_actions);
2344 actions_changed = (facet->actions_len != odp_actions->size
2345 || memcmp(facet->actions, odp_actions->data,
2346 facet->actions_len));
2348 /* If the ODP actions changed or the installability changed, then we need
2349 * to talk to the datapath. */
2350 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2351 if (ctx.may_set_up_flow) {
2352 struct dpif_flow_stats stats;
2354 facet_put__(ofproto, facet,
2355 odp_actions->data, odp_actions->size, &stats);
2356 facet_update_stats(ofproto, facet, &stats);
2358 facet_uninstall(ofproto, facet);
2361 /* The datapath flow is gone or has zeroed stats, so push stats out of
2362 * 'facet' into 'rule'. */
2363 facet_flush_stats(ofproto, facet);
2366 /* Update 'facet' now that we've taken care of all the old state. */
2367 facet->tags = ctx.tags;
2368 facet->nf_flow.output_iface = ctx.nf_output_iface;
2369 facet->may_install = ctx.may_set_up_flow;
2370 if (actions_changed) {
2371 free(facet->actions);
2372 facet->actions_len = odp_actions->size;
2373 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2375 if (facet->rule != new_rule) {
2376 COVERAGE_INC(facet_changed_rule);
2377 list_remove(&facet->list_node);
2378 list_push_back(&new_rule->facets, &facet->list_node);
2379 facet->rule = new_rule;
2380 facet->used = new_rule->up.created;
2381 facet->rs_used = facet->used;
2384 ofpbuf_delete(odp_actions);
2389 /* Updates 'facet''s used time. Caller is responsible for calling
2390 * facet_push_stats() to update the flows which 'facet' resubmits into. */
2392 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2395 if (used > facet->used) {
2397 if (used > facet->rule->used) {
2398 facet->rule->used = used;
2400 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2404 /* Folds the statistics from 'stats' into the counters in 'facet'.
2406 * Because of the meaning of a facet's counters, it only makes sense to do this
2407 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2408 * packet that was sent by hand or if it represents statistics that have been
2409 * cleared out of the datapath. */
2411 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2412 const struct dpif_flow_stats *stats)
2414 if (stats->n_packets || stats->used > facet->used) {
2415 facet_update_time(ofproto, facet, stats->used);
2416 facet->packet_count += stats->n_packets;
2417 facet->byte_count += stats->n_bytes;
2418 facet_push_stats(facet);
2419 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2424 facet_push_stats(struct facet *facet)
2426 uint64_t rs_packets, rs_bytes;
2428 assert(facet->packet_count >= facet->rs_packet_count);
2429 assert(facet->byte_count >= facet->rs_byte_count);
2430 assert(facet->used >= facet->rs_used);
2432 rs_packets = facet->packet_count - facet->rs_packet_count;
2433 rs_bytes = facet->byte_count - facet->rs_byte_count;
2435 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2436 facet->rs_packet_count = facet->packet_count;
2437 facet->rs_byte_count = facet->byte_count;
2438 facet->rs_used = facet->used;
2440 flow_push_stats(facet->rule, &facet->flow,
2441 rs_packets, rs_bytes, facet->used);
2445 struct ofproto_push {
2446 struct action_xlate_ctx ctx;
2453 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2455 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2458 rule->packet_count += push->packets;
2459 rule->byte_count += push->bytes;
2460 rule->used = MAX(push->used, rule->used);
2464 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2465 * 'rule''s actions. */
2467 flow_push_stats(const struct rule_dpif *rule,
2468 struct flow *flow, uint64_t packets, uint64_t bytes,
2471 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2472 struct ofproto_push push;
2474 push.packets = packets;
2478 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2479 push.ctx.resubmit_hook = push_resubmit;
2480 ofpbuf_delete(xlate_actions(&push.ctx,
2481 rule->up.actions, rule->up.n_actions));
2486 static struct rule_dpif *
2487 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
2489 return rule_dpif_cast(rule_from_cls_rule(
2490 classifier_lookup(&ofproto->up.tables[0],
2494 static struct rule *
2497 struct rule_dpif *rule = xmalloc(sizeof *rule);
2502 rule_dealloc(struct rule *rule_)
2504 struct rule_dpif *rule = rule_dpif_cast(rule_);
2509 rule_construct(struct rule *rule_)
2511 struct rule_dpif *rule = rule_dpif_cast(rule_);
2512 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2513 struct rule_dpif *old_rule;
2516 error = validate_actions(rule->up.actions, rule->up.n_actions,
2517 &rule->up.cr.flow, ofproto->max_ports);
2522 old_rule = rule_dpif_cast(rule_from_cls_rule(classifier_find_rule_exactly(
2523 &ofproto->up.tables[0],
2526 ofproto_rule_destroy(&old_rule->up);
2529 rule->used = rule->up.created;
2530 rule->packet_count = 0;
2531 rule->byte_count = 0;
2532 list_init(&rule->facets);
2533 classifier_insert(&ofproto->up.tables[0], &rule->up.cr);
2535 ofproto->need_revalidate = true;
2541 rule_destruct(struct rule *rule_)
2543 struct rule_dpif *rule = rule_dpif_cast(rule_);
2544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2545 struct facet *facet, *next_facet;
2547 classifier_remove(&ofproto->up.tables[0], &rule->up.cr);
2548 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2549 facet_revalidate(ofproto, facet);
2551 ofproto->need_revalidate = true;
2555 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2557 struct rule_dpif *rule = rule_dpif_cast(rule_);
2558 struct facet *facet;
2560 /* Start from historical data for 'rule' itself that are no longer tracked
2561 * in facets. This counts, for example, facets that have expired. */
2562 *packets = rule->packet_count;
2563 *bytes = rule->byte_count;
2565 /* Add any statistics that are tracked by facets. This includes
2566 * statistical data recently updated by ofproto_update_stats() as well as
2567 * stats for packets that were executed "by hand" via dpif_execute(). */
2568 LIST_FOR_EACH (facet, list_node, &rule->facets) {
2569 *packets += facet->packet_count;
2570 *bytes += facet->byte_count;
2575 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2577 struct rule_dpif *rule = rule_dpif_cast(rule_);
2578 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2579 struct action_xlate_ctx ctx;
2580 struct ofpbuf *odp_actions;
2581 struct facet *facet;
2584 /* First look for a related facet. If we find one, account it to that. */
2585 facet = facet_lookup_valid(ofproto, flow);
2586 if (facet && facet->rule == rule) {
2587 facet_execute(ofproto, facet, packet);
2591 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2592 * create a new facet for it and use that. */
2593 if (rule_dpif_lookup(ofproto, flow) == rule) {
2594 facet = facet_create(rule, flow, packet);
2595 facet_execute(ofproto, facet, packet);
2596 facet_install(ofproto, facet, true);
2600 /* We can't account anything to a facet. If we were to try, then that
2601 * facet would have a non-matching rule, busting our invariants. */
2602 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2603 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2604 size = packet->size;
2605 if (execute_odp_actions(ofproto, flow, odp_actions->data,
2606 odp_actions->size, packet)) {
2607 rule->used = time_msec();
2608 rule->packet_count++;
2609 rule->byte_count += size;
2610 flow_push_stats(rule, flow, 1, size, rule->used);
2612 ofpbuf_delete(odp_actions);
2618 rule_modify_actions(struct rule *rule_,
2619 const union ofp_action *actions, size_t n_actions)
2621 struct rule_dpif *rule = rule_dpif_cast(rule_);
2622 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2625 error = validate_actions(actions, n_actions, &rule->up.cr.flow,
2626 ofproto->max_ports);
2628 ofproto->need_revalidate = true;
2633 /* Sends 'packet' out of port 'odp_port' within 'p'.
2634 * Returns 0 if successful, otherwise a positive errno value. */
2636 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
2637 const struct ofpbuf *packet)
2639 struct ofpbuf odp_actions;
2642 ofpbuf_init(&odp_actions, 32);
2643 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2644 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
2646 ofpbuf_uninit(&odp_actions);
2649 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2650 ofproto->up.name, odp_port, strerror(error));
2655 /* OpenFlow to ODP action translation. */
2657 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2658 struct action_xlate_ctx *ctx);
2659 static bool xlate_normal(struct action_xlate_ctx *);
2662 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2664 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2665 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2668 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2669 /* Forwarding disabled on port. */
2674 * We don't have an ofport record for this port, but it doesn't hurt to
2675 * allow forwarding to it anyhow. Maybe such a port will appear later
2676 * and we're pre-populating the flow table.
2680 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2681 ctx->nf_output_iface = ofp_port;
2685 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2687 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2688 struct rule_dpif *rule;
2689 uint16_t old_in_port;
2691 /* Look up a flow with 'in_port' as the input port. Then restore the
2692 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2693 * have surprising behavior). */
2694 old_in_port = ctx->flow.in_port;
2695 ctx->flow.in_port = in_port;
2696 rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow);
2697 ctx->flow.in_port = old_in_port;
2699 if (ctx->resubmit_hook) {
2700 ctx->resubmit_hook(ctx, rule);
2705 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2709 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2711 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2712 MAX_RESUBMIT_RECURSION);
2717 flood_packets(struct ofproto_dpif *ofproto,
2718 uint16_t ofp_in_port, ovs_be32 mask,
2719 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2721 struct ofport_dpif *ofport;
2723 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2724 uint16_t ofp_port = ofport->up.ofp_port;
2725 if (ofp_port != ofp_in_port && !(ofport->up.opp.config & mask)) {
2726 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT,
2730 *nf_output_iface = NF_OUT_FLOOD;
2734 xlate_output_action__(struct action_xlate_ctx *ctx,
2735 uint16_t port, uint16_t max_len)
2737 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2739 ctx->nf_output_iface = NF_OUT_DROP;
2743 add_output_action(ctx, ctx->flow.in_port);
2746 xlate_table_action(ctx, ctx->flow.in_port);
2752 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD),
2753 &ctx->nf_output_iface, ctx->odp_actions);
2756 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0),
2757 &ctx->nf_output_iface, ctx->odp_actions);
2759 case OFPP_CONTROLLER:
2760 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2763 add_output_action(ctx, OFPP_LOCAL);
2766 if (port != ctx->flow.in_port) {
2767 add_output_action(ctx, port);
2772 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2773 ctx->nf_output_iface = NF_OUT_FLOOD;
2774 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2775 ctx->nf_output_iface = prev_nf_output_iface;
2776 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2777 ctx->nf_output_iface != NF_OUT_FLOOD) {
2778 ctx->nf_output_iface = NF_OUT_MULTI;
2783 xlate_output_action(struct action_xlate_ctx *ctx,
2784 const struct ofp_action_output *oao)
2786 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2789 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2790 * optimization, because we're going to add another action that sets the
2791 * priority immediately after, or because there are no actions following the
2794 remove_pop_action(struct action_xlate_ctx *ctx)
2796 if (ctx->odp_actions->size == ctx->last_pop_priority) {
2797 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2798 ctx->last_pop_priority = -1;
2803 add_pop_action(struct action_xlate_ctx *ctx)
2805 if (ctx->odp_actions->size != ctx->last_pop_priority) {
2806 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2807 ctx->last_pop_priority = ctx->odp_actions->size;
2812 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2813 const struct ofp_action_enqueue *oae)
2815 uint16_t ofp_port, odp_port;
2819 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2822 /* Fall back to ordinary output action. */
2823 xlate_output_action__(ctx, ntohs(oae->port), 0);
2827 /* Figure out ODP output port. */
2828 ofp_port = ntohs(oae->port);
2829 if (ofp_port == OFPP_IN_PORT) {
2830 ofp_port = ctx->flow.in_port;
2832 odp_port = ofp_port_to_odp_port(ofp_port);
2834 /* Add ODP actions. */
2835 remove_pop_action(ctx);
2836 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2837 add_output_action(ctx, odp_port);
2838 add_pop_action(ctx);
2840 /* Update NetFlow output port. */
2841 if (ctx->nf_output_iface == NF_OUT_DROP) {
2842 ctx->nf_output_iface = odp_port;
2843 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2844 ctx->nf_output_iface = NF_OUT_MULTI;
2849 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2850 const struct nx_action_set_queue *nasq)
2855 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2858 /* Couldn't translate queue to a priority, so ignore. A warning
2859 * has already been logged. */
2863 remove_pop_action(ctx);
2864 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2868 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2870 ovs_be16 tci = ctx->flow.vlan_tci;
2871 if (!(tci & htons(VLAN_CFI))) {
2872 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2874 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2875 tci & ~htons(VLAN_CFI));
2879 struct xlate_reg_state {
2885 save_reg_state(const struct action_xlate_ctx *ctx,
2886 struct xlate_reg_state *state)
2888 state->vlan_tci = ctx->flow.vlan_tci;
2889 state->tun_id = ctx->flow.tun_id;
2893 update_reg_state(struct action_xlate_ctx *ctx,
2894 const struct xlate_reg_state *state)
2896 if (ctx->flow.vlan_tci != state->vlan_tci) {
2897 xlate_set_dl_tci(ctx);
2899 if (ctx->flow.tun_id != state->tun_id) {
2900 nl_msg_put_be64(ctx->odp_actions,
2901 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2906 xlate_autopath(struct action_xlate_ctx *ctx,
2907 const struct nx_action_autopath *naa)
2909 uint16_t ofp_port = ntohl(naa->id);
2910 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
2912 if (!port || !port->bundle) {
2913 ofp_port = OFPP_NONE;
2914 } else if (port->bundle->bond) {
2915 /* Autopath does not support VLAN hashing. */
2916 struct ofport_dpif *slave = bond_choose_output_slave(
2917 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
2919 ofp_port = slave->up.ofp_port;
2922 autopath_execute(naa, &ctx->flow, ofp_port);
2926 xlate_nicira_action(struct action_xlate_ctx *ctx,
2927 const struct nx_action_header *nah)
2929 const struct nx_action_resubmit *nar;
2930 const struct nx_action_set_tunnel *nast;
2931 const struct nx_action_set_queue *nasq;
2932 const struct nx_action_multipath *nam;
2933 const struct nx_action_autopath *naa;
2934 enum nx_action_subtype subtype = ntohs(nah->subtype);
2935 struct xlate_reg_state state;
2938 assert(nah->vendor == htonl(NX_VENDOR_ID));
2940 case NXAST_RESUBMIT:
2941 nar = (const struct nx_action_resubmit *) nah;
2942 xlate_table_action(ctx, ntohs(nar->in_port));
2945 case NXAST_SET_TUNNEL:
2946 nast = (const struct nx_action_set_tunnel *) nah;
2947 tun_id = htonll(ntohl(nast->tun_id));
2948 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2949 ctx->flow.tun_id = tun_id;
2952 case NXAST_DROP_SPOOFED_ARP:
2953 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2954 nl_msg_put_flag(ctx->odp_actions,
2955 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2959 case NXAST_SET_QUEUE:
2960 nasq = (const struct nx_action_set_queue *) nah;
2961 xlate_set_queue_action(ctx, nasq);
2964 case NXAST_POP_QUEUE:
2965 add_pop_action(ctx);
2968 case NXAST_REG_MOVE:
2969 save_reg_state(ctx, &state);
2970 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2972 update_reg_state(ctx, &state);
2975 case NXAST_REG_LOAD:
2976 save_reg_state(ctx, &state);
2977 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2979 update_reg_state(ctx, &state);
2983 /* Nothing to do. */
2986 case NXAST_SET_TUNNEL64:
2987 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2988 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2989 ctx->flow.tun_id = tun_id;
2992 case NXAST_MULTIPATH:
2993 nam = (const struct nx_action_multipath *) nah;
2994 multipath_execute(nam, &ctx->flow);
2997 case NXAST_AUTOPATH:
2998 naa = (const struct nx_action_autopath *) nah;
2999 xlate_autopath(ctx, naa);
3002 /* If you add a new action here that modifies flow data, don't forget to
3003 * update the flow key in ctx->flow at the same time. */
3005 case NXAST_SNAT__OBSOLETE:
3007 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
3013 do_xlate_actions(const union ofp_action *in, size_t n_in,
3014 struct action_xlate_ctx *ctx)
3016 const struct ofport_dpif *port;
3017 struct actions_iterator iter;
3018 const union ofp_action *ia;
3020 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
3022 && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3023 port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3024 ? htonl(OFPPC_NO_RECV_STP)
3025 : htonl(OFPPC_NO_RECV))) {
3026 /* Drop this flow. */
3030 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
3031 enum ofp_action_type type = ntohs(ia->type);
3032 const struct ofp_action_dl_addr *oada;
3036 xlate_output_action(ctx, &ia->output);
3039 case OFPAT_SET_VLAN_VID:
3040 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3041 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3042 xlate_set_dl_tci(ctx);
3045 case OFPAT_SET_VLAN_PCP:
3046 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3047 ctx->flow.vlan_tci |= htons(
3048 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3049 xlate_set_dl_tci(ctx);
3052 case OFPAT_STRIP_VLAN:
3053 ctx->flow.vlan_tci = htons(0);
3054 xlate_set_dl_tci(ctx);
3057 case OFPAT_SET_DL_SRC:
3058 oada = ((struct ofp_action_dl_addr *) ia);
3059 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
3060 oada->dl_addr, ETH_ADDR_LEN);
3061 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3064 case OFPAT_SET_DL_DST:
3065 oada = ((struct ofp_action_dl_addr *) ia);
3066 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
3067 oada->dl_addr, ETH_ADDR_LEN);
3068 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3071 case OFPAT_SET_NW_SRC:
3072 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
3073 ia->nw_addr.nw_addr);
3074 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3077 case OFPAT_SET_NW_DST:
3078 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
3079 ia->nw_addr.nw_addr);
3080 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3083 case OFPAT_SET_NW_TOS:
3084 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
3086 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
3089 case OFPAT_SET_TP_SRC:
3090 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
3091 ia->tp_port.tp_port);
3092 ctx->flow.tp_src = ia->tp_port.tp_port;
3095 case OFPAT_SET_TP_DST:
3096 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
3097 ia->tp_port.tp_port);
3098 ctx->flow.tp_dst = ia->tp_port.tp_port;
3102 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
3106 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3110 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
3117 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3118 struct ofproto_dpif *ofproto, const struct flow *flow,
3119 const struct ofpbuf *packet)
3121 ctx->ofproto = ofproto;
3123 ctx->packet = packet;
3124 ctx->resubmit_hook = NULL;
3127 static struct ofpbuf *
3128 xlate_actions(struct action_xlate_ctx *ctx,
3129 const union ofp_action *in, size_t n_in)
3131 COVERAGE_INC(ofproto_dpif_xlate);
3133 ctx->odp_actions = ofpbuf_new(512);
3135 ctx->may_set_up_flow = true;
3136 ctx->nf_output_iface = NF_OUT_DROP;
3138 ctx->last_pop_priority = -1;
3140 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3141 ctx->may_set_up_flow = false;
3143 do_xlate_actions(in, n_in, ctx);
3146 remove_pop_action(ctx);
3148 /* Check with in-band control to see if we're allowed to set up this
3150 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3151 ctx->odp_actions->data,
3152 ctx->odp_actions->size)) {
3153 ctx->may_set_up_flow = false;
3156 return ctx->odp_actions;
3159 /* OFPP_NORMAL implementation. */
3162 struct ofport_dpif *port;
3167 struct dst builtin[32];
3169 size_t n, allocated;
3172 static void dst_set_init(struct dst_set *);
3173 static void dst_set_add(struct dst_set *, const struct dst *);
3174 static void dst_set_free(struct dst_set *);
3176 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3179 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3180 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3182 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3183 : in_bundle->vlan >= 0 ? in_bundle->vlan
3184 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3185 : vlan_tci_to_vid(ctx->flow.vlan_tci));
3187 dst->port = (!out_bundle->bond
3188 ? ofbundle_get_a_port(out_bundle)
3189 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3190 dst->vlan, &ctx->tags));
3192 return dst->port != NULL;
3196 mirror_mask_ffs(mirror_mask_t mask)
3198 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3203 dst_set_init(struct dst_set *set)
3205 set->dsts = set->builtin;
3207 set->allocated = ARRAY_SIZE(set->builtin);
3211 dst_set_add(struct dst_set *set, const struct dst *dst)
3213 if (set->n >= set->allocated) {
3214 size_t new_allocated;
3215 struct dst *new_dsts;
3217 new_allocated = set->allocated * 2;
3218 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3219 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3223 set->dsts = new_dsts;
3224 set->allocated = new_allocated;
3226 set->dsts[set->n++] = *dst;
3230 dst_set_free(struct dst_set *set)
3232 if (set->dsts != set->builtin) {
3238 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3241 for (i = 0; i < set->n; i++) {
3242 if (set->dsts[i].vlan == test->vlan
3243 && set->dsts[i].port == test->port) {
3251 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3253 return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan);
3257 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3259 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3262 /* Returns an arbitrary interface within 'bundle'. */
3263 static struct ofport_dpif *
3264 ofbundle_get_a_port(const struct ofbundle *bundle)
3266 return CONTAINER_OF(list_front(&bundle->ports),
3267 struct ofport_dpif, bundle_node);
3271 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3272 const struct ofbundle *in_bundle,
3273 const struct ofbundle *out_bundle, struct dst_set *set)
3277 if (out_bundle == OFBUNDLE_FLOOD) {
3278 struct ofbundle *bundle;
3280 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3281 if (bundle != in_bundle
3282 && ofbundle_includes_vlan(bundle, vlan)
3283 && bundle->floodable
3284 && !bundle->mirror_out
3285 && set_dst(ctx, &dst, in_bundle, bundle)) {
3286 dst_set_add(set, &dst);
3289 ctx->nf_output_iface = NF_OUT_FLOOD;
3290 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3291 dst_set_add(set, &dst);
3292 ctx->nf_output_iface = dst.port->odp_port;
3297 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3299 return vlan_bitmap_contains(m->vlans, vlan);
3303 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3304 uint16_t vlan, const struct ofbundle *in_bundle,
3305 struct dst_set *set)
3307 struct ofproto_dpif *ofproto = ctx->ofproto;
3308 mirror_mask_t mirrors;
3312 mirrors = in_bundle->src_mirrors;
3313 for (i = 0; i < set->n; i++) {
3314 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3321 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3322 if (flow_vlan == 0) {
3323 flow_vlan = OFP_VLAN_NONE;
3327 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3328 if (vlan_is_mirrored(m, vlan)) {
3332 if (set_dst(ctx, &dst, in_bundle, m->out)
3333 && !dst_is_duplicate(set, &dst)) {
3334 dst_set_add(set, &dst);
3337 struct ofbundle *bundle;
3339 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3340 if (ofbundle_includes_vlan(bundle, m->out_vlan)
3341 && set_dst(ctx, &dst, in_bundle, bundle))
3343 if (bundle->vlan < 0) {
3344 dst.vlan = m->out_vlan;
3346 if (dst_is_duplicate(set, &dst)) {
3350 /* Use the vlan tag on the original flow instead of
3351 * the one passed in the vlan parameter. This ensures
3352 * that we compare the vlan from before any implicit
3353 * tagging tags place. This is necessary because
3354 * dst->vlan is the final vlan, after removing implicit
3356 if (bundle == in_bundle && dst.vlan == flow_vlan) {
3357 /* Don't send out input port on same VLAN. */
3360 dst_set_add(set, &dst);
3365 mirrors &= mirrors - 1;
3370 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3371 const struct ofbundle *in_bundle,
3372 const struct ofbundle *out_bundle)
3374 uint16_t initial_vlan, cur_vlan;
3375 const struct dst *dst;
3379 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3380 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3382 /* Output all the packets we can without having to change the VLAN. */
3383 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3384 if (initial_vlan == 0) {
3385 initial_vlan = OFP_VLAN_NONE;
3387 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3388 if (dst->vlan != initial_vlan) {
3391 nl_msg_put_u32(ctx->odp_actions,
3392 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3395 /* Then output the rest. */
3396 cur_vlan = initial_vlan;
3397 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3398 if (dst->vlan == initial_vlan) {
3401 if (dst->vlan != cur_vlan) {
3402 if (dst->vlan == OFP_VLAN_NONE) {
3403 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3406 tci = htons(dst->vlan & VLAN_VID_MASK);
3407 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3408 nl_msg_put_be16(ctx->odp_actions,
3409 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3411 cur_vlan = dst->vlan;
3413 nl_msg_put_u32(ctx->odp_actions,
3414 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3420 /* Returns the effective vlan of a packet, taking into account both the
3421 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
3422 * the packet is untagged and -1 indicates it has an invalid header and
3423 * should be dropped. */
3425 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3426 struct ofbundle *in_bundle, bool have_packet)
3428 int vlan = vlan_tci_to_vid(flow->vlan_tci);
3429 if (in_bundle->vlan >= 0) {
3432 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3433 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3434 "packet received on port %s configured with "
3435 "implicit VLAN %"PRIu16,
3436 ofproto->up.name, vlan,
3437 in_bundle->name, in_bundle->vlan);
3441 vlan = in_bundle->vlan;
3443 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3445 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3446 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3447 "packet received on port %s not configured for "
3449 ofproto->up.name, vlan, in_bundle->name, vlan);
3458 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3459 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
3460 * indicate this; newer upstream kernels use gratuitous ARP requests. */
3462 is_gratuitous_arp(const struct flow *flow)
3464 return (flow->dl_type == htons(ETH_TYPE_ARP)
3465 && eth_addr_is_broadcast(flow->dl_dst)
3466 && (flow->nw_proto == ARP_OP_REPLY
3467 || (flow->nw_proto == ARP_OP_REQUEST
3468 && flow->nw_src == flow->nw_dst)));
3472 update_learning_table(struct ofproto_dpif *ofproto,
3473 const struct flow *flow, int vlan,
3474 struct ofbundle *in_bundle)
3476 struct mac_entry *mac;
3478 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3482 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3483 if (is_gratuitous_arp(flow)) {
3484 /* We don't want to learn from gratuitous ARP packets that are
3485 * reflected back over bond slaves so we lock the learning table. */
3486 if (!in_bundle->bond) {
3487 mac_entry_set_grat_arp_lock(mac);
3488 } else if (mac_entry_is_grat_arp_locked(mac)) {
3493 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3494 /* The log messages here could actually be useful in debugging,
3495 * so keep the rate limit relatively high. */
3496 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3497 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3498 "on port %s in VLAN %d",
3499 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3500 in_bundle->name, vlan);
3502 mac->port.p = in_bundle;
3503 tag_set_add(&ofproto->revalidate_set,
3504 mac_learning_changed(ofproto->ml, mac));
3508 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3509 * dropped. Returns true if they may be forwarded, false if they should be
3512 * If 'have_packet' is true, it indicates that the caller is processing a
3513 * received packet. If 'have_packet' is false, then the caller is just
3514 * revalidating an existing flow because configuration has changed. Either
3515 * way, 'have_packet' only affects logging (there is no point in logging errors
3516 * during revalidation).
3518 * Sets '*in_portp' to the input port. This will be a null pointer if
3519 * flow->in_port does not designate a known input port (in which case
3520 * is_admissible() returns false).
3522 * When returning true, sets '*vlanp' to the effective VLAN of the input
3523 * packet, as returned by flow_get_vlan().
3525 * May also add tags to '*tags', although the current implementation only does
3526 * so in one special case.
3529 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3531 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3533 struct ofport_dpif *in_port;
3534 struct ofbundle *in_bundle;
3537 /* Find the port and bundle for the received packet. */
3538 in_port = get_ofp_port(ofproto, flow->in_port);
3539 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
3540 if (!in_port || !in_bundle) {
3541 /* No interface? Something fishy... */
3543 /* Odd. A few possible reasons here:
3545 * - We deleted a port but there are still a few packets queued up
3548 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3549 * we don't know about.
3551 * - Packet arrived on the local port but the local port is not
3554 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3556 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3558 ofproto->up.name, flow->in_port);
3562 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3567 /* Drop frames for reserved multicast addresses. */
3568 if (eth_addr_is_reserved(flow->dl_dst)) {
3572 /* Drop frames on bundles reserved for mirroring. */
3573 if (in_bundle->mirror_out) {
3575 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3576 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3577 "%s, which is reserved exclusively for mirroring",
3578 ofproto->up.name, in_bundle->name);
3583 if (in_bundle->bond) {
3584 struct mac_entry *mac;
3586 switch (bond_check_admissibility(in_bundle->bond, in_port,
3587 flow->dl_dst, tags)) {
3594 case BV_DROP_IF_MOVED:
3595 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3596 if (mac && mac->port.p != in_bundle &&
3597 (!is_gratuitous_arp(flow)
3598 || mac_entry_is_grat_arp_locked(mac))) {
3608 /* If the composed actions may be applied to any packet in the given 'flow',
3609 * returns true. Otherwise, the actions should only be applied to 'packet', or
3610 * not at all, if 'packet' was NULL. */
3612 xlate_normal(struct action_xlate_ctx *ctx)
3614 struct ofbundle *in_bundle;
3615 struct ofbundle *out_bundle;
3616 struct mac_entry *mac;
3619 /* Check whether we should drop packets in this flow. */
3620 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3621 &ctx->tags, &vlan, &in_bundle)) {
3626 /* Learn source MAC (but don't try to learn from revalidation). */
3628 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3631 /* Determine output bundle. */
3632 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3635 out_bundle = mac->port.p;
3636 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3637 /* If we are revalidating but don't have a learning entry then eject
3638 * the flow. Installing a flow that floods packets opens up a window
3639 * of time where we could learn from a packet reflected on a bond and
3640 * blackhole packets before the learning table is updated to reflect
3641 * the correct port. */
3644 out_bundle = OFBUNDLE_FLOOD;
3647 /* Don't send packets out their input bundles. */
3648 if (in_bundle == out_bundle) {
3654 compose_actions(ctx, vlan, in_bundle, out_bundle);
3661 get_drop_frags(struct ofproto *ofproto_)
3663 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3666 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3671 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3673 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3675 dpif_set_drop_frags(ofproto->dpif, drop_frags);
3679 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3680 const struct flow *flow,
3681 const union ofp_action *ofp_actions, size_t n_ofp_actions)
3683 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3686 error = validate_actions(ofp_actions, n_ofp_actions, flow,
3687 ofproto->max_ports);
3689 struct action_xlate_ctx ctx;
3690 struct ofpbuf *odp_actions;
3692 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3693 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3694 dpif_execute(ofproto->dpif, odp_actions->data, odp_actions->size,
3696 ofpbuf_delete(odp_actions);
3702 get_netflow_ids(const struct ofproto *ofproto_,
3703 uint8_t *engine_type, uint8_t *engine_id)
3705 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3707 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3710 static struct ofproto_dpif *
3711 ofproto_dpif_lookup(const char *name)
3713 struct ofproto *ofproto = ofproto_lookup(name);
3714 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3715 ? ofproto_dpif_cast(ofproto)
3720 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3721 const char *args, void *aux OVS_UNUSED)
3723 struct ds ds = DS_EMPTY_INITIALIZER;
3724 const struct ofproto_dpif *ofproto;
3725 const struct mac_entry *e;
3727 ofproto = ofproto_dpif_lookup(args);
3729 unixctl_command_reply(conn, 501, "no such bridge");
3733 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3734 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3735 struct ofbundle *bundle = e->port.p;
3736 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
3737 ofbundle_get_a_port(bundle)->odp_port,
3738 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3740 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3744 struct ofproto_trace {
3745 struct action_xlate_ctx ctx;
3751 trace_format_rule(struct ds *result, int level, const struct rule *rule)
3753 ds_put_char_multiple(result, '\t', level);
3755 ds_put_cstr(result, "No match\n");
3759 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
3760 ntohll(rule->flow_cookie));
3761 cls_rule_format(&rule->cr, result);
3762 ds_put_char(result, '\n');
3764 ds_put_char_multiple(result, '\t', level);
3765 ds_put_cstr(result, "OpenFlow ");
3766 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
3767 rule->n_actions * sizeof *rule->actions);
3768 ds_put_char(result, '\n');
3772 trace_format_flow(struct ds *result, int level, const char *title,
3773 struct ofproto_trace *trace)
3775 ds_put_char_multiple(result, '\t', level);
3776 ds_put_format(result, "%s: ", title);
3777 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3778 ds_put_cstr(result, "unchanged");
3780 flow_format(result, &trace->ctx.flow);
3781 trace->flow = trace->ctx.flow;
3783 ds_put_char(result, '\n');
3787 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3789 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3790 struct ds *result = trace->result;
3792 ds_put_char(result, '\n');
3793 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
3794 trace_format_rule(result, ctx->recurse + 1, &rule->up);
3798 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
3799 void *aux OVS_UNUSED)
3801 char *dpname, *in_port_s, *tun_id_s, *packet_s;
3802 char *args = xstrdup(args_);
3803 char *save_ptr = NULL;
3804 struct ofproto_dpif *ofproto;
3805 struct ofpbuf packet;
3806 struct rule_dpif *rule;
3813 ofpbuf_init(&packet, strlen(args) / 2);
3816 dpname = strtok_r(args, " ", &save_ptr);
3817 tun_id_s = strtok_r(NULL, " ", &save_ptr);
3818 in_port_s = strtok_r(NULL, " ", &save_ptr);
3819 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
3820 if (!dpname || !in_port_s || !packet_s) {
3821 unixctl_command_reply(conn, 501, "Bad command syntax");
3825 ofproto = ofproto_dpif_lookup(dpname);
3827 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
3832 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
3833 in_port = ofp_port_to_odp_port(atoi(in_port_s));
3835 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
3836 packet_s += strspn(packet_s, " ");
3837 if (*packet_s != '\0') {
3838 unixctl_command_reply(conn, 501, "Trailing garbage in command");
3841 if (packet.size < ETH_HEADER_LEN) {
3842 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
3846 ds_put_cstr(&result, "Packet: ");
3847 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
3848 ds_put_cstr(&result, s);
3851 flow_extract(&packet, tun_id, in_port, &flow);
3852 ds_put_cstr(&result, "Flow: ");
3853 flow_format(&result, &flow);
3854 ds_put_char(&result, '\n');
3856 rule = rule_dpif_lookup(ofproto, &flow);
3857 trace_format_rule(&result, 0, &rule->up);
3859 struct ofproto_trace trace;
3860 struct ofpbuf *odp_actions;
3862 trace.result = &result;
3864 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
3865 trace.ctx.resubmit_hook = trace_resubmit;
3866 odp_actions = xlate_actions(&trace.ctx,
3867 rule->up.actions, rule->up.n_actions);
3869 ds_put_char(&result, '\n');
3870 trace_format_flow(&result, 0, "Final flow", &trace);
3871 ds_put_cstr(&result, "Datapath actions: ");
3872 format_odp_actions(&result, odp_actions->data, odp_actions->size);
3873 ofpbuf_delete(odp_actions);
3876 unixctl_command_reply(conn, 200, ds_cstr(&result));
3879 ds_destroy(&result);
3880 ofpbuf_uninit(&packet);
3885 ofproto_dpif_unixctl_init(void)
3887 static bool registered;
3893 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
3894 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
3897 const struct ofproto_class ofproto_dpif_class = {
3924 port_is_lacp_current,
3931 rule_modify_actions,
3944 is_mirror_output_bundle,