2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-provider.h"
26 #include "byte-order.h"
31 #include "dynamic-string.h"
32 #include "fail-open.h"
36 #include "mac-learning.h"
37 #include "multipath.h"
44 #include "ofp-print.h"
45 #include "ofproto-dpif-sflow.h"
46 #include "poll-loop.h"
48 #include "unaligned.h"
50 #include "vlan-bitmap.h"
53 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
55 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
56 COVERAGE_DEFINE(ofproto_dpif_expired);
57 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
58 COVERAGE_DEFINE(ofproto_dpif_xlate);
59 COVERAGE_DEFINE(facet_changed_rule);
60 COVERAGE_DEFINE(facet_invalidated);
61 COVERAGE_DEFINE(facet_revalidate);
62 COVERAGE_DEFINE(facet_unexpected);
64 /* Maximum depth of flow table recursion (due to resubmit actions) in a
65 * flow translation. */
66 #define MAX_RESUBMIT_RECURSION 32
68 /* Number of implemented OpenFlow tables. */
69 enum { N_TABLES = 255 };
70 BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255);
78 long long int used; /* Time last used; time created if not used. */
82 * - Do include packets and bytes from facets that have been deleted or
83 * whose own statistics have been folded into the rule.
85 * - Do include packets and bytes sent "by hand" that were accounted to
86 * the rule without any facet being involved (this is a rare corner
87 * case in rule_execute()).
89 * - Do not include packet or bytes that can be obtained from any facet's
90 * packet_count or byte_count member or that can be obtained from the
91 * datapath by, e.g., dpif_flow_get() for any facet.
93 uint64_t packet_count; /* Number of packets received. */
94 uint64_t byte_count; /* Number of bytes received. */
96 tag_type tag; /* Caches rule_calculate_tag() result. */
98 struct list facets; /* List of "struct facet"s. */
101 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
103 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
106 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
107 const struct flow *, uint8_t table);
109 #define MAX_MIRRORS 32
110 typedef uint32_t mirror_mask_t;
111 #define MIRROR_MASK_C(X) UINT32_C(X)
112 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 size_t idx; /* In ofproto's "mirrors" array. */
116 void *aux; /* Key supplied by ofproto's client. */
117 char *name; /* Identifier for log messages. */
119 /* Selection criteria. */
120 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
121 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
122 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
124 /* Output (mutually exclusive). */
125 struct ofbundle *out; /* Output port or NULL. */
126 int out_vlan; /* Output VLAN or -1. */
129 static void mirror_destroy(struct ofmirror *);
131 /* A group of one or more OpenFlow ports. */
132 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
134 struct ofproto_dpif *ofproto; /* Owning ofproto. */
135 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
136 void *aux; /* Key supplied by ofproto's client. */
137 char *name; /* Identifier for log messages. */
140 struct list ports; /* Contains "struct ofport"s. */
141 enum port_vlan_mode vlan_mode; /* VLAN mode */
142 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
143 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
144 * NULL if all VLANs are trunked. */
145 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
146 struct bond *bond; /* Nonnull iff more than one port. */
149 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
151 /* Port mirroring info. */
152 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
153 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
154 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
157 static void bundle_remove(struct ofport *);
158 static void bundle_update(struct ofbundle *);
159 static void bundle_destroy(struct ofbundle *);
160 static void bundle_del_port(struct ofport_dpif *);
161 static void bundle_run(struct ofbundle *);
162 static void bundle_wait(struct ofbundle *);
164 static void stp_run(struct ofproto_dpif *ofproto);
165 static void stp_wait(struct ofproto_dpif *ofproto);
167 struct action_xlate_ctx {
168 /* action_xlate_ctx_init() initializes these members. */
171 struct ofproto_dpif *ofproto;
173 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
174 * this flow when actions change header fields. */
177 /* The packet corresponding to 'flow', or a null pointer if we are
178 * revalidating without a packet to refer to. */
179 const struct ofpbuf *packet;
181 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
182 * want to execute them if we are actually processing a packet, or if we
183 * are accounting for packets that the datapath has processed, but not if
184 * we are just revalidating. */
187 /* If nonnull, called just before executing a resubmit action.
189 * This is normally null so the client has to set it manually after
190 * calling action_xlate_ctx_init(). */
191 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
193 /* xlate_actions() initializes and uses these members. The client might want
194 * to look at them after it returns. */
196 struct ofpbuf *odp_actions; /* Datapath actions. */
197 tag_type tags; /* Tags associated with actions. */
198 bool may_set_up_flow; /* True ordinarily; false if the actions must
199 * be reassessed for every packet. */
200 bool has_learn; /* Actions include NXAST_LEARN? */
201 bool has_normal; /* Actions output to OFPP_NORMAL? */
202 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
204 /* xlate_actions() initializes and uses these members, but the client has no
205 * reason to look at them. */
207 int recurse; /* Recursion level, via xlate_table_action. */
208 struct flow base_flow; /* Flow at the last commit. */
209 uint32_t original_priority; /* Priority when packet arrived. */
210 uint8_t table_id; /* OpenFlow table ID where flow was found. */
211 uint32_t sflow_n_outputs; /* Number of output ports. */
212 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
213 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
214 bool exit; /* No further actions should be processed. */
217 static void action_xlate_ctx_init(struct action_xlate_ctx *,
218 struct ofproto_dpif *, const struct flow *,
219 const struct ofpbuf *);
220 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
221 const union ofp_action *in, size_t n_in);
223 /* An exact-match instantiation of an OpenFlow flow. */
225 long long int used; /* Time last used; time created if not used. */
229 * - Do include packets and bytes sent "by hand", e.g. with
232 * - Do include packets and bytes that were obtained from the datapath
233 * when its statistics were reset (e.g. dpif_flow_put() with
234 * DPIF_FP_ZERO_STATS).
236 uint64_t packet_count; /* Number of packets received. */
237 uint64_t byte_count; /* Number of bytes received. */
239 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
240 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
242 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
243 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
244 long long int rs_used; /* Used time pushed to resubmit children. */
246 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
248 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
249 struct list list_node; /* In owning rule's 'facets' list. */
250 struct rule_dpif *rule; /* Owning rule. */
251 struct flow flow; /* Exact-match flow. */
252 bool installed; /* Installed in datapath? */
253 bool may_install; /* True ordinarily; false if actions must
254 * be reassessed for every packet. */
255 bool has_learn; /* Actions include NXAST_LEARN? */
256 bool has_normal; /* Actions output to OFPP_NORMAL? */
257 size_t actions_len; /* Number of bytes in actions[]. */
258 struct nlattr *actions; /* Datapath actions. */
259 tag_type tags; /* Tags. */
260 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
263 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
264 static void facet_remove(struct ofproto_dpif *, struct facet *);
265 static void facet_free(struct facet *);
267 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
268 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
269 const struct flow *);
270 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
272 static bool execute_controller_action(struct ofproto_dpif *,
274 const struct nlattr *odp_actions,
276 struct ofpbuf *packet);
277 static void facet_execute(struct ofproto_dpif *, struct facet *,
278 struct ofpbuf *packet);
280 static int facet_put__(struct ofproto_dpif *, struct facet *,
281 const struct nlattr *actions, size_t actions_len,
282 struct dpif_flow_stats *);
283 static void facet_install(struct ofproto_dpif *, struct facet *,
285 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
286 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
288 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
289 const struct ofpbuf *packet);
290 static void facet_update_time(struct ofproto_dpif *, struct facet *,
292 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
293 const struct dpif_flow_stats *);
294 static void facet_reset_counters(struct facet *);
295 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
296 static void facet_push_stats(struct facet *);
297 static void facet_account(struct ofproto_dpif *, struct facet *);
299 static bool facet_is_controller_flow(struct facet *);
301 static void flow_push_stats(const struct rule_dpif *,
302 struct flow *, uint64_t packets, uint64_t bytes,
305 static uint32_t rule_calculate_tag(const struct flow *,
306 const struct flow_wildcards *,
308 static void rule_invalidate(const struct rule_dpif *);
314 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
315 struct list bundle_node; /* In struct ofbundle's "ports" list. */
316 struct cfm *cfm; /* Connectivity Fault Management, if any. */
317 tag_type tag; /* Tag associated with this port. */
318 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
319 bool may_enable; /* May be enabled in bonds. */
321 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
322 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
323 long long int stp_state_entered;
326 static struct ofport_dpif *
327 ofport_dpif_cast(const struct ofport *ofport)
329 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
330 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
333 static void port_run(struct ofport_dpif *);
334 static void port_wait(struct ofport_dpif *);
335 static int set_cfm(struct ofport *, const struct cfm_settings *);
337 struct dpif_completion {
338 struct list list_node;
339 struct ofoperation *op;
342 /* Extra information about a classifier table.
343 * Currently used just for optimized flow revalidation. */
345 /* If either of these is nonnull, then this table has a form that allows
346 * flows to be tagged to avoid revalidating most flows for the most common
347 * kinds of flow table changes. */
348 struct cls_table *catchall_table; /* Table that wildcards all fields. */
349 struct cls_table *other_table; /* Table with any other wildcard set. */
350 uint32_t basis; /* Keeps each table's tags separate. */
353 struct ofproto_dpif {
362 struct netflow *netflow;
363 struct dpif_sflow *sflow;
364 struct hmap bundles; /* Contains "struct ofbundle"s. */
365 struct mac_learning *ml;
366 struct ofmirror *mirrors[MAX_MIRRORS];
367 bool has_bonded_bundles;
370 struct timer next_expiration;
376 struct table_dpif tables[N_TABLES];
377 bool need_revalidate;
378 struct tag_set revalidate_set;
380 /* Support for debugging async flow mods. */
381 struct list completions;
383 bool has_bundle_action; /* True when the first bundle action appears. */
387 long long int stp_last_tick;
390 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
391 * for debugging the asynchronous flow_mod implementation.) */
394 static void ofproto_dpif_unixctl_init(void);
396 static struct ofproto_dpif *
397 ofproto_dpif_cast(const struct ofproto *ofproto)
399 assert(ofproto->ofproto_class == &ofproto_dpif_class);
400 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
403 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
405 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
408 /* Packet processing. */
409 static void update_learning_table(struct ofproto_dpif *,
410 const struct flow *, int vlan,
412 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
413 bool have_packet, tag_type *, int *vlanp,
414 struct ofbundle **in_bundlep);
417 #define FLOW_MISS_MAX_BATCH 50
418 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
419 static void handle_miss_upcalls(struct ofproto_dpif *,
420 struct dpif_upcall *, size_t n);
422 /* Flow expiration. */
423 static int expire(struct ofproto_dpif *);
426 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
427 const struct ofpbuf *packet);
429 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
430 const struct flow *, uint32_t odp_port);
431 /* Global variables. */
432 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
434 /* Factory functions. */
437 enumerate_types(struct sset *types)
439 dp_enumerate_types(types);
443 enumerate_names(const char *type, struct sset *names)
445 return dp_enumerate_names(type, names);
449 del(const char *type, const char *name)
454 error = dpif_open(name, type, &dpif);
456 error = dpif_delete(dpif);
462 /* Basic life-cycle. */
464 static struct ofproto *
467 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
472 dealloc(struct ofproto *ofproto_)
474 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
479 construct(struct ofproto *ofproto_, int *n_tablesp)
481 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
482 const char *name = ofproto->up.name;
486 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
488 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
492 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
493 ofproto->n_matches = 0;
495 dpif_flow_flush(ofproto->dpif);
496 dpif_recv_purge(ofproto->dpif);
498 error = dpif_recv_set_mask(ofproto->dpif,
499 ((1u << DPIF_UC_MISS) |
500 (1u << DPIF_UC_ACTION)));
502 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
503 dpif_close(ofproto->dpif);
507 ofproto->netflow = NULL;
508 ofproto->sflow = NULL;
510 hmap_init(&ofproto->bundles);
511 ofproto->ml = mac_learning_create();
512 for (i = 0; i < MAX_MIRRORS; i++) {
513 ofproto->mirrors[i] = NULL;
515 ofproto->has_bonded_bundles = false;
517 timer_set_duration(&ofproto->next_expiration, 1000);
519 hmap_init(&ofproto->facets);
521 for (i = 0; i < N_TABLES; i++) {
522 struct table_dpif *table = &ofproto->tables[i];
524 table->catchall_table = NULL;
525 table->other_table = NULL;
526 table->basis = random_uint32();
528 ofproto->need_revalidate = false;
529 tag_set_init(&ofproto->revalidate_set);
531 list_init(&ofproto->completions);
533 ofproto_dpif_unixctl_init();
535 ofproto->has_bundle_action = false;
537 *n_tablesp = N_TABLES;
542 complete_operations(struct ofproto_dpif *ofproto)
544 struct dpif_completion *c, *next;
546 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
547 ofoperation_complete(c->op, 0);
548 list_remove(&c->list_node);
554 destruct(struct ofproto *ofproto_)
556 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
557 struct rule_dpif *rule, *next_rule;
558 struct classifier *table;
561 complete_operations(ofproto);
563 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
564 struct cls_cursor cursor;
566 cls_cursor_init(&cursor, table, NULL);
567 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
568 ofproto_rule_destroy(&rule->up);
572 for (i = 0; i < MAX_MIRRORS; i++) {
573 mirror_destroy(ofproto->mirrors[i]);
576 netflow_destroy(ofproto->netflow);
577 dpif_sflow_destroy(ofproto->sflow);
578 hmap_destroy(&ofproto->bundles);
579 mac_learning_destroy(ofproto->ml);
581 hmap_destroy(&ofproto->facets);
583 dpif_close(ofproto->dpif);
587 run(struct ofproto *ofproto_)
589 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
590 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
591 struct ofport_dpif *ofport;
592 struct ofbundle *bundle;
597 complete_operations(ofproto);
599 dpif_run(ofproto->dpif);
602 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
603 struct dpif_upcall *upcall = &misses[n_misses];
606 error = dpif_recv(ofproto->dpif, upcall);
608 if (error == ENODEV && n_misses == 0) {
614 if (upcall->type == DPIF_UC_MISS) {
615 /* Handle it later. */
618 handle_upcall(ofproto, upcall);
622 handle_miss_upcalls(ofproto, misses, n_misses);
624 if (timer_expired(&ofproto->next_expiration)) {
625 int delay = expire(ofproto);
626 timer_set_duration(&ofproto->next_expiration, delay);
629 if (ofproto->netflow) {
630 netflow_run(ofproto->netflow);
632 if (ofproto->sflow) {
633 dpif_sflow_run(ofproto->sflow);
636 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
639 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
644 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
646 /* Now revalidate if there's anything to do. */
647 if (ofproto->need_revalidate
648 || !tag_set_is_empty(&ofproto->revalidate_set)) {
649 struct tag_set revalidate_set = ofproto->revalidate_set;
650 bool revalidate_all = ofproto->need_revalidate;
651 struct facet *facet, *next;
653 /* Clear the revalidation flags. */
654 tag_set_init(&ofproto->revalidate_set);
655 ofproto->need_revalidate = false;
657 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
659 || tag_set_intersects(&revalidate_set, facet->tags)) {
660 facet_revalidate(ofproto, facet);
669 wait(struct ofproto *ofproto_)
671 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
672 struct ofport_dpif *ofport;
673 struct ofbundle *bundle;
675 if (!clogged && !list_is_empty(&ofproto->completions)) {
676 poll_immediate_wake();
679 dpif_wait(ofproto->dpif);
680 dpif_recv_wait(ofproto->dpif);
681 if (ofproto->sflow) {
682 dpif_sflow_wait(ofproto->sflow);
684 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
685 poll_immediate_wake();
687 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
690 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
693 mac_learning_wait(ofproto->ml);
695 if (ofproto->need_revalidate) {
696 /* Shouldn't happen, but if it does just go around again. */
697 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
698 poll_immediate_wake();
700 timer_wait(&ofproto->next_expiration);
705 flush(struct ofproto *ofproto_)
707 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
708 struct facet *facet, *next_facet;
710 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
711 /* Mark the facet as not installed so that facet_remove() doesn't
712 * bother trying to uninstall it. There is no point in uninstalling it
713 * individually since we are about to blow away all the facets with
714 * dpif_flow_flush(). */
715 facet->installed = false;
716 facet->dp_packet_count = 0;
717 facet->dp_byte_count = 0;
718 facet_remove(ofproto, facet);
720 dpif_flow_flush(ofproto->dpif);
724 get_features(struct ofproto *ofproto_ OVS_UNUSED,
725 bool *arp_match_ip, uint32_t *actions)
727 *arp_match_ip = true;
728 *actions = ((1u << OFPAT_OUTPUT) |
729 (1u << OFPAT_SET_VLAN_VID) |
730 (1u << OFPAT_SET_VLAN_PCP) |
731 (1u << OFPAT_STRIP_VLAN) |
732 (1u << OFPAT_SET_DL_SRC) |
733 (1u << OFPAT_SET_DL_DST) |
734 (1u << OFPAT_SET_NW_SRC) |
735 (1u << OFPAT_SET_NW_DST) |
736 (1u << OFPAT_SET_NW_TOS) |
737 (1u << OFPAT_SET_TP_SRC) |
738 (1u << OFPAT_SET_TP_DST) |
739 (1u << OFPAT_ENQUEUE));
743 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
745 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
746 struct dpif_dp_stats s;
748 strcpy(ots->name, "classifier");
750 dpif_get_dp_stats(ofproto->dpif, &s);
751 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
752 put_32aligned_be64(&ots->matched_count,
753 htonll(s.n_hit + ofproto->n_matches));
757 set_netflow(struct ofproto *ofproto_,
758 const struct netflow_options *netflow_options)
760 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
762 if (netflow_options) {
763 if (!ofproto->netflow) {
764 ofproto->netflow = netflow_create();
766 return netflow_set_options(ofproto->netflow, netflow_options);
768 netflow_destroy(ofproto->netflow);
769 ofproto->netflow = NULL;
774 static struct ofport *
777 struct ofport_dpif *port = xmalloc(sizeof *port);
782 port_dealloc(struct ofport *port_)
784 struct ofport_dpif *port = ofport_dpif_cast(port_);
789 port_construct(struct ofport *port_)
791 struct ofport_dpif *port = ofport_dpif_cast(port_);
792 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
794 ofproto->need_revalidate = true;
795 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
798 port->tag = tag_create_random();
799 port->may_enable = true;
800 port->stp_port = NULL;
801 port->stp_state = STP_DISABLED;
803 if (ofproto->sflow) {
804 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
805 netdev_get_name(port->up.netdev));
812 port_destruct(struct ofport *port_)
814 struct ofport_dpif *port = ofport_dpif_cast(port_);
815 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
817 ofproto->need_revalidate = true;
818 bundle_remove(port_);
819 set_cfm(port_, NULL);
820 if (ofproto->sflow) {
821 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
826 port_modified(struct ofport *port_)
828 struct ofport_dpif *port = ofport_dpif_cast(port_);
830 if (port->bundle && port->bundle->bond) {
831 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
836 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
838 struct ofport_dpif *port = ofport_dpif_cast(port_);
839 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
840 ovs_be32 changed = old_config ^ port->up.opp.config;
842 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
843 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
844 ofproto->need_revalidate = true;
846 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
847 bundle_update(port->bundle);
853 set_sflow(struct ofproto *ofproto_,
854 const struct ofproto_sflow_options *sflow_options)
856 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
857 struct dpif_sflow *ds = ofproto->sflow;
861 struct ofport_dpif *ofport;
863 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
864 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
865 dpif_sflow_add_port(ds, ofport->odp_port,
866 netdev_get_name(ofport->up.netdev));
868 ofproto->need_revalidate = true;
870 dpif_sflow_set_options(ds, sflow_options);
873 dpif_sflow_destroy(ds);
874 ofproto->need_revalidate = true;
875 ofproto->sflow = NULL;
882 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
884 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
891 struct ofproto_dpif *ofproto;
893 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
894 ofproto->need_revalidate = true;
895 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
898 if (cfm_configure(ofport->cfm, s)) {
904 cfm_destroy(ofport->cfm);
910 get_cfm_fault(const struct ofport *ofport_)
912 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
914 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
918 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
921 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
924 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
934 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
936 struct ofproto_dpif *ofproto = ofproto_;
937 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
938 struct ofport_dpif *ofport;
940 ofport = stp_port_get_aux(sp);
942 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
943 ofproto->up.name, port_num);
945 struct eth_header *eth = pkt->l2;
947 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
948 if (eth_addr_is_zero(eth->eth_src)) {
949 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
950 "with unknown MAC", ofproto->up.name, port_num);
952 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
953 ofport->odp_port, pkt);
959 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
961 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
963 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
965 /* Only revalidate flows if the configuration changed. */
966 if (!s != !ofproto->stp) {
967 ofproto->need_revalidate = true;
972 ofproto->stp = stp_create(ofproto_->name, s->system_id,
973 send_bpdu_cb, ofproto);
974 ofproto->stp_last_tick = time_msec();
977 stp_set_bridge_id(ofproto->stp, s->system_id);
978 stp_set_bridge_priority(ofproto->stp, s->priority);
979 stp_set_hello_time(ofproto->stp, s->hello_time);
980 stp_set_max_age(ofproto->stp, s->max_age);
981 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
983 stp_destroy(ofproto->stp);
991 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
993 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
997 s->bridge_id = stp_get_bridge_id(ofproto->stp);
998 s->designated_root = stp_get_designated_root(ofproto->stp);
999 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1008 update_stp_port_state(struct ofport_dpif *ofport)
1010 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1011 enum stp_state state;
1013 /* Figure out new state. */
1014 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1018 if (ofport->stp_state != state) {
1022 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1023 netdev_get_name(ofport->up.netdev),
1024 stp_state_name(ofport->stp_state),
1025 stp_state_name(state));
1026 if (stp_learn_in_state(ofport->stp_state)
1027 != stp_learn_in_state(state)) {
1028 /* xxx Learning action flows should also be flushed. */
1029 mac_learning_flush(ofproto->ml);
1031 fwd_change = stp_forward_in_state(ofport->stp_state)
1032 != stp_forward_in_state(state);
1034 ofproto->need_revalidate = true;
1035 ofport->stp_state = state;
1036 ofport->stp_state_entered = time_msec();
1039 bundle_update(ofport->bundle);
1042 /* Update the STP state bits in the OpenFlow port description. */
1043 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1044 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1045 : state == STP_LEARNING ? OFPPS_STP_LEARN
1046 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1047 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1049 ofproto_port_set_state(&ofport->up, of_state);
1053 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1054 * caller is responsible for assigning STP port numbers and ensuring
1055 * there are no duplicates. */
1057 set_stp_port(struct ofport *ofport_,
1058 const struct ofproto_port_stp_settings *s)
1060 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1061 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1062 struct stp_port *sp = ofport->stp_port;
1064 if (!s || !s->enable) {
1066 ofport->stp_port = NULL;
1067 stp_port_disable(sp);
1068 update_stp_port_state(ofport);
1071 } else if (sp && stp_port_no(sp) != s->port_num
1072 && ofport == stp_port_get_aux(sp)) {
1073 /* The port-id changed, so disable the old one if it's not
1074 * already in use by another port. */
1075 stp_port_disable(sp);
1078 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1079 stp_port_enable(sp);
1081 stp_port_set_aux(sp, ofport);
1082 stp_port_set_priority(sp, s->priority);
1083 stp_port_set_path_cost(sp, s->path_cost);
1085 update_stp_port_state(ofport);
1091 get_stp_port_status(struct ofport *ofport_,
1092 struct ofproto_port_stp_status *s)
1094 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1095 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1096 struct stp_port *sp = ofport->stp_port;
1098 if (!ofproto->stp || !sp) {
1104 s->port_id = stp_port_get_id(sp);
1105 s->state = stp_port_get_state(sp);
1106 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1107 s->role = stp_port_get_role(sp);
1113 stp_run(struct ofproto_dpif *ofproto)
1116 long long int now = time_msec();
1117 long long int elapsed = now - ofproto->stp_last_tick;
1118 struct stp_port *sp;
1121 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1122 ofproto->stp_last_tick = now;
1124 while (stp_get_changed_port(ofproto->stp, &sp)) {
1125 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1128 update_stp_port_state(ofport);
1135 stp_wait(struct ofproto_dpif *ofproto)
1138 poll_timer_wait(1000);
1142 /* Returns true if STP should process 'flow'. */
1144 stp_should_process_flow(const struct flow *flow)
1146 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1150 stp_process_packet(const struct ofport_dpif *ofport,
1151 const struct ofpbuf *packet)
1153 struct ofpbuf payload = *packet;
1154 struct eth_header *eth = payload.data;
1155 struct stp_port *sp = ofport->stp_port;
1157 /* Sink packets on ports that have STP disabled when the bridge has
1159 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1163 /* Trim off padding on payload. */
1164 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1165 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1168 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1169 stp_received_bpdu(sp, payload.data, payload.size);
1175 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1176 * to revalidate every flow. */
1178 bundle_flush_macs(struct ofbundle *bundle)
1180 struct ofproto_dpif *ofproto = bundle->ofproto;
1181 struct mac_learning *ml = ofproto->ml;
1182 struct mac_entry *mac, *next_mac;
1184 ofproto->need_revalidate = true;
1185 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1186 if (mac->port.p == bundle) {
1187 mac_learning_expire(ml, mac);
1192 static struct ofbundle *
1193 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1195 struct ofbundle *bundle;
1197 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1198 &ofproto->bundles) {
1199 if (bundle->aux == aux) {
1206 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1207 * ones that are found to 'bundles'. */
1209 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1210 void **auxes, size_t n_auxes,
1211 struct hmapx *bundles)
1215 hmapx_init(bundles);
1216 for (i = 0; i < n_auxes; i++) {
1217 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1219 hmapx_add(bundles, bundle);
1225 bundle_update(struct ofbundle *bundle)
1227 struct ofport_dpif *port;
1229 bundle->floodable = true;
1230 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1231 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1232 || !stp_forward_in_state(port->stp_state)) {
1233 bundle->floodable = false;
1240 bundle_del_port(struct ofport_dpif *port)
1242 struct ofbundle *bundle = port->bundle;
1244 bundle->ofproto->need_revalidate = true;
1246 list_remove(&port->bundle_node);
1247 port->bundle = NULL;
1250 lacp_slave_unregister(bundle->lacp, port);
1253 bond_slave_unregister(bundle->bond, port);
1256 bundle_update(bundle);
1260 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1261 struct lacp_slave_settings *lacp,
1262 uint32_t bond_stable_id)
1264 struct ofport_dpif *port;
1266 port = get_ofp_port(bundle->ofproto, ofp_port);
1271 if (port->bundle != bundle) {
1272 bundle->ofproto->need_revalidate = true;
1274 bundle_del_port(port);
1277 port->bundle = bundle;
1278 list_push_back(&bundle->ports, &port->bundle_node);
1279 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1280 || !stp_forward_in_state(port->stp_state)) {
1281 bundle->floodable = false;
1285 port->bundle->ofproto->need_revalidate = true;
1286 lacp_slave_register(bundle->lacp, port, lacp);
1289 port->bond_stable_id = bond_stable_id;
1295 bundle_destroy(struct ofbundle *bundle)
1297 struct ofproto_dpif *ofproto;
1298 struct ofport_dpif *port, *next_port;
1305 ofproto = bundle->ofproto;
1306 for (i = 0; i < MAX_MIRRORS; i++) {
1307 struct ofmirror *m = ofproto->mirrors[i];
1309 if (m->out == bundle) {
1311 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1312 || hmapx_find_and_delete(&m->dsts, bundle)) {
1313 ofproto->need_revalidate = true;
1318 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1319 bundle_del_port(port);
1322 bundle_flush_macs(bundle);
1323 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1325 free(bundle->trunks);
1326 lacp_destroy(bundle->lacp);
1327 bond_destroy(bundle->bond);
1332 bundle_set(struct ofproto *ofproto_, void *aux,
1333 const struct ofproto_bundle_settings *s)
1335 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1336 bool need_flush = false;
1337 struct ofport_dpif *port;
1338 struct ofbundle *bundle;
1339 unsigned long *trunks;
1345 bundle_destroy(bundle_lookup(ofproto, aux));
1349 assert(s->n_slaves == 1 || s->bond != NULL);
1350 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1352 bundle = bundle_lookup(ofproto, aux);
1354 bundle = xmalloc(sizeof *bundle);
1356 bundle->ofproto = ofproto;
1357 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1358 hash_pointer(aux, 0));
1360 bundle->name = NULL;
1362 list_init(&bundle->ports);
1363 bundle->vlan_mode = PORT_VLAN_TRUNK;
1365 bundle->trunks = NULL;
1366 bundle->lacp = NULL;
1367 bundle->bond = NULL;
1369 bundle->floodable = true;
1371 bundle->src_mirrors = 0;
1372 bundle->dst_mirrors = 0;
1373 bundle->mirror_out = 0;
1376 if (!bundle->name || strcmp(s->name, bundle->name)) {
1378 bundle->name = xstrdup(s->name);
1383 if (!bundle->lacp) {
1384 ofproto->need_revalidate = true;
1385 bundle->lacp = lacp_create();
1387 lacp_configure(bundle->lacp, s->lacp);
1389 lacp_destroy(bundle->lacp);
1390 bundle->lacp = NULL;
1393 /* Update set of ports. */
1395 for (i = 0; i < s->n_slaves; i++) {
1396 if (!bundle_add_port(bundle, s->slaves[i],
1397 s->lacp ? &s->lacp_slaves[i] : NULL,
1398 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1402 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1403 struct ofport_dpif *next_port;
1405 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1406 for (i = 0; i < s->n_slaves; i++) {
1407 if (s->slaves[i] == port->up.ofp_port) {
1412 bundle_del_port(port);
1416 assert(list_size(&bundle->ports) <= s->n_slaves);
1418 if (list_is_empty(&bundle->ports)) {
1419 bundle_destroy(bundle);
1423 /* Set VLAN tagging mode */
1424 if (s->vlan_mode != bundle->vlan_mode) {
1425 bundle->vlan_mode = s->vlan_mode;
1430 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1431 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1433 if (vlan != bundle->vlan) {
1434 bundle->vlan = vlan;
1438 /* Get trunked VLANs. */
1439 switch (s->vlan_mode) {
1440 case PORT_VLAN_ACCESS:
1444 case PORT_VLAN_TRUNK:
1445 trunks = (unsigned long *) s->trunks;
1448 case PORT_VLAN_NATIVE_UNTAGGED:
1449 case PORT_VLAN_NATIVE_TAGGED:
1450 if (vlan != 0 && (!s->trunks
1451 || !bitmap_is_set(s->trunks, vlan)
1452 || bitmap_is_set(s->trunks, 0))) {
1453 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1455 trunks = bitmap_clone(s->trunks, 4096);
1457 trunks = bitmap_allocate1(4096);
1459 bitmap_set1(trunks, vlan);
1460 bitmap_set0(trunks, 0);
1462 trunks = (unsigned long *) s->trunks;
1469 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1470 free(bundle->trunks);
1471 if (trunks == s->trunks) {
1472 bundle->trunks = vlan_bitmap_clone(trunks);
1474 bundle->trunks = trunks;
1479 if (trunks != s->trunks) {
1484 if (!list_is_short(&bundle->ports)) {
1485 bundle->ofproto->has_bonded_bundles = true;
1487 if (bond_reconfigure(bundle->bond, s->bond)) {
1488 ofproto->need_revalidate = true;
1491 bundle->bond = bond_create(s->bond);
1492 ofproto->need_revalidate = true;
1495 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1496 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1500 bond_destroy(bundle->bond);
1501 bundle->bond = NULL;
1504 /* If we changed something that would affect MAC learning, un-learn
1505 * everything on this port and force flow revalidation. */
1507 bundle_flush_macs(bundle);
1514 bundle_remove(struct ofport *port_)
1516 struct ofport_dpif *port = ofport_dpif_cast(port_);
1517 struct ofbundle *bundle = port->bundle;
1520 bundle_del_port(port);
1521 if (list_is_empty(&bundle->ports)) {
1522 bundle_destroy(bundle);
1523 } else if (list_is_short(&bundle->ports)) {
1524 bond_destroy(bundle->bond);
1525 bundle->bond = NULL;
1531 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1533 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1534 struct ofport_dpif *port = port_;
1535 uint8_t ea[ETH_ADDR_LEN];
1538 error = netdev_get_etheraddr(port->up.netdev, ea);
1540 struct ofpbuf packet;
1543 ofpbuf_init(&packet, 0);
1544 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1546 memcpy(packet_pdu, pdu, pdu_size);
1548 send_packet(ofproto_dpif_cast(port->up.ofproto), port->odp_port,
1550 ofpbuf_uninit(&packet);
1552 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1553 "%s (%s)", port->bundle->name,
1554 netdev_get_name(port->up.netdev), strerror(error));
1559 bundle_send_learning_packets(struct ofbundle *bundle)
1561 struct ofproto_dpif *ofproto = bundle->ofproto;
1562 int error, n_packets, n_errors;
1563 struct mac_entry *e;
1565 error = n_packets = n_errors = 0;
1566 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1567 if (e->port.p != bundle) {
1568 struct ofpbuf *learning_packet;
1569 struct ofport_dpif *port;
1572 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1575 ret = send_packet(ofproto_dpif_cast(port->up.ofproto),
1576 port->odp_port, learning_packet);
1577 ofpbuf_delete(learning_packet);
1587 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1588 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1589 "packets, last error was: %s",
1590 bundle->name, n_errors, n_packets, strerror(error));
1592 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1593 bundle->name, n_packets);
1598 bundle_run(struct ofbundle *bundle)
1601 lacp_run(bundle->lacp, send_pdu_cb);
1604 struct ofport_dpif *port;
1606 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1607 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1610 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1611 lacp_negotiated(bundle->lacp));
1612 if (bond_should_send_learning_packets(bundle->bond)) {
1613 bundle_send_learning_packets(bundle);
1619 bundle_wait(struct ofbundle *bundle)
1622 lacp_wait(bundle->lacp);
1625 bond_wait(bundle->bond);
1632 mirror_scan(struct ofproto_dpif *ofproto)
1636 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1637 if (!ofproto->mirrors[idx]) {
1644 static struct ofmirror *
1645 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1649 for (i = 0; i < MAX_MIRRORS; i++) {
1650 struct ofmirror *mirror = ofproto->mirrors[i];
1651 if (mirror && mirror->aux == aux) {
1660 mirror_set(struct ofproto *ofproto_, void *aux,
1661 const struct ofproto_mirror_settings *s)
1663 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1664 mirror_mask_t mirror_bit;
1665 struct ofbundle *bundle;
1666 struct ofmirror *mirror;
1667 struct ofbundle *out;
1668 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1669 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1672 mirror = mirror_lookup(ofproto, aux);
1674 mirror_destroy(mirror);
1680 idx = mirror_scan(ofproto);
1682 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1684 ofproto->up.name, MAX_MIRRORS, s->name);
1688 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1689 mirror->ofproto = ofproto;
1692 mirror->out_vlan = -1;
1693 mirror->name = NULL;
1696 if (!mirror->name || strcmp(s->name, mirror->name)) {
1698 mirror->name = xstrdup(s->name);
1701 /* Get the new configuration. */
1702 if (s->out_bundle) {
1703 out = bundle_lookup(ofproto, s->out_bundle);
1705 mirror_destroy(mirror);
1711 out_vlan = s->out_vlan;
1713 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1714 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1716 /* If the configuration has not changed, do nothing. */
1717 if (hmapx_equals(&srcs, &mirror->srcs)
1718 && hmapx_equals(&dsts, &mirror->dsts)
1719 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1720 && mirror->out == out
1721 && mirror->out_vlan == out_vlan)
1723 hmapx_destroy(&srcs);
1724 hmapx_destroy(&dsts);
1728 hmapx_swap(&srcs, &mirror->srcs);
1729 hmapx_destroy(&srcs);
1731 hmapx_swap(&dsts, &mirror->dsts);
1732 hmapx_destroy(&dsts);
1734 free(mirror->vlans);
1735 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1738 mirror->out_vlan = out_vlan;
1740 /* Update bundles. */
1741 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1742 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1743 if (hmapx_contains(&mirror->srcs, bundle)) {
1744 bundle->src_mirrors |= mirror_bit;
1746 bundle->src_mirrors &= ~mirror_bit;
1749 if (hmapx_contains(&mirror->dsts, bundle)) {
1750 bundle->dst_mirrors |= mirror_bit;
1752 bundle->dst_mirrors &= ~mirror_bit;
1755 if (mirror->out == bundle) {
1756 bundle->mirror_out |= mirror_bit;
1758 bundle->mirror_out &= ~mirror_bit;
1762 ofproto->need_revalidate = true;
1763 mac_learning_flush(ofproto->ml);
1769 mirror_destroy(struct ofmirror *mirror)
1771 struct ofproto_dpif *ofproto;
1772 mirror_mask_t mirror_bit;
1773 struct ofbundle *bundle;
1779 ofproto = mirror->ofproto;
1780 ofproto->need_revalidate = true;
1781 mac_learning_flush(ofproto->ml);
1783 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1784 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1785 bundle->src_mirrors &= ~mirror_bit;
1786 bundle->dst_mirrors &= ~mirror_bit;
1787 bundle->mirror_out &= ~mirror_bit;
1790 hmapx_destroy(&mirror->srcs);
1791 hmapx_destroy(&mirror->dsts);
1792 free(mirror->vlans);
1794 ofproto->mirrors[mirror->idx] = NULL;
1800 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1802 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1803 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1804 ofproto->need_revalidate = true;
1805 mac_learning_flush(ofproto->ml);
1811 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
1813 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1814 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1815 return bundle && bundle->mirror_out != 0;
1819 forward_bpdu_changed(struct ofproto *ofproto_)
1821 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1822 /* Revalidate cached flows whenever forward_bpdu option changes. */
1823 ofproto->need_revalidate = true;
1828 static struct ofport_dpif *
1829 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1831 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1832 return ofport ? ofport_dpif_cast(ofport) : NULL;
1835 static struct ofport_dpif *
1836 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1838 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1842 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1843 struct dpif_port *dpif_port)
1845 ofproto_port->name = dpif_port->name;
1846 ofproto_port->type = dpif_port->type;
1847 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1851 port_run(struct ofport_dpif *ofport)
1853 bool enable = netdev_get_carrier(ofport->up.netdev);
1856 cfm_run(ofport->cfm);
1858 if (cfm_should_send_ccm(ofport->cfm)) {
1859 struct ofpbuf packet;
1861 ofpbuf_init(&packet, 0);
1862 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1863 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1864 ofport->odp_port, &packet);
1865 ofpbuf_uninit(&packet);
1868 enable = enable && !cfm_get_fault(ofport->cfm)
1869 && cfm_get_opup(ofport->cfm);
1872 if (ofport->bundle) {
1873 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1876 if (ofport->may_enable != enable) {
1877 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1879 if (ofproto->has_bundle_action) {
1880 ofproto->need_revalidate = true;
1884 ofport->may_enable = enable;
1888 port_wait(struct ofport_dpif *ofport)
1891 cfm_wait(ofport->cfm);
1896 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1897 struct ofproto_port *ofproto_port)
1899 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1900 struct dpif_port dpif_port;
1903 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1905 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1911 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1913 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1917 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1919 *ofp_portp = odp_port_to_ofp_port(odp_port);
1925 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1927 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1930 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1932 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1934 /* The caller is going to close ofport->up.netdev. If this is a
1935 * bonded port, then the bond is using that netdev, so remove it
1936 * from the bond. The client will need to reconfigure everything
1937 * after deleting ports, so then the slave will get re-added. */
1938 bundle_remove(&ofport->up);
1944 struct port_dump_state {
1945 struct dpif_port_dump dump;
1950 port_dump_start(const struct ofproto *ofproto_, void **statep)
1952 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1953 struct port_dump_state *state;
1955 *statep = state = xmalloc(sizeof *state);
1956 dpif_port_dump_start(&state->dump, ofproto->dpif);
1957 state->done = false;
1962 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1963 struct ofproto_port *port)
1965 struct port_dump_state *state = state_;
1966 struct dpif_port dpif_port;
1968 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1969 ofproto_port_from_dpif_port(port, &dpif_port);
1972 int error = dpif_port_dump_done(&state->dump);
1974 return error ? error : EOF;
1979 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1981 struct port_dump_state *state = state_;
1984 dpif_port_dump_done(&state->dump);
1991 port_poll(const struct ofproto *ofproto_, char **devnamep)
1993 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1994 return dpif_port_poll(ofproto->dpif, devnamep);
1998 port_poll_wait(const struct ofproto *ofproto_)
2000 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2001 dpif_port_poll_wait(ofproto->dpif);
2005 port_is_lacp_current(const struct ofport *ofport_)
2007 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2008 return (ofport->bundle && ofport->bundle->lacp
2009 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2013 /* Upcall handling. */
2015 /* Flow miss batching.
2017 * Some dpifs implement operations faster when you hand them off in a batch.
2018 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2019 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2020 * more packets, plus possibly installing the flow in the dpif.
2022 * So far we only batch the operations that affect flow setup time the most.
2023 * It's possible to batch more than that, but the benefit might be minimal. */
2025 struct hmap_node hmap_node;
2027 const struct nlattr *key;
2029 struct list packets;
2032 struct flow_miss_op {
2033 union dpif_op dpif_op;
2034 struct facet *facet;
2037 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2038 * OpenFlow controller as necessary according to their individual
2041 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2042 * ownership is transferred to this function. */
2044 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2045 const struct flow *flow, bool clone)
2047 struct ofputil_packet_in pin;
2049 pin.packet = packet;
2050 pin.in_port = flow->in_port;
2051 pin.reason = OFPR_NO_MATCH;
2052 pin.buffer_id = 0; /* not yet known */
2053 pin.send_len = 0; /* not used for flow table misses */
2054 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2055 clone ? NULL : packet);
2058 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2059 * OpenFlow controller as necessary according to their individual
2062 * 'send_len' should be the number of bytes of 'packet' to send to the
2063 * controller, as specified in the action that caused the packet to be sent.
2065 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2066 * Otherwise, ownership is transferred to this function. */
2068 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2069 uint64_t userdata, const struct flow *flow, bool clone)
2071 struct ofputil_packet_in pin;
2072 struct user_action_cookie cookie;
2074 memcpy(&cookie, &userdata, sizeof(cookie));
2076 pin.packet = packet;
2077 pin.in_port = flow->in_port;
2078 pin.reason = OFPR_ACTION;
2079 pin.buffer_id = 0; /* not yet known */
2080 pin.send_len = cookie.data;
2081 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2082 clone ? NULL : packet);
2086 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2087 const struct ofpbuf *packet)
2089 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2095 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2097 cfm_process_heartbeat(ofport->cfm, packet);
2100 } else if (ofport->bundle && ofport->bundle->lacp
2101 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2103 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2106 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2108 stp_process_packet(ofport, packet);
2115 static struct flow_miss *
2116 flow_miss_create(struct hmap *todo, const struct flow *flow,
2117 const struct nlattr *key, size_t key_len)
2119 uint32_t hash = flow_hash(flow, 0);
2120 struct flow_miss *miss;
2122 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2123 if (flow_equal(&miss->flow, flow)) {
2128 miss = xmalloc(sizeof *miss);
2129 hmap_insert(todo, &miss->hmap_node, hash);
2132 miss->key_len = key_len;
2133 list_init(&miss->packets);
2138 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2139 struct flow_miss_op *ops, size_t *n_ops)
2141 const struct flow *flow = &miss->flow;
2142 struct ofpbuf *packet, *next_packet;
2143 struct facet *facet;
2145 facet = facet_lookup_valid(ofproto, flow);
2147 struct rule_dpif *rule;
2149 rule = rule_dpif_lookup(ofproto, flow, 0);
2151 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2152 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2154 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2155 COVERAGE_INC(ofproto_dpif_no_packet_in);
2156 /* XXX install 'drop' flow entry */
2160 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2164 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2166 list_remove(&packet->list_node);
2167 send_packet_in_miss(ofproto, packet, flow, false);
2173 facet = facet_create(rule, flow);
2176 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2177 list_remove(&packet->list_node);
2178 ofproto->n_matches++;
2180 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2182 * Extra-special case for fail-open mode.
2184 * We are in fail-open mode and the packet matched the fail-open
2185 * rule, but we are connected to a controller too. We should send
2186 * the packet up to the controller in the hope that it will try to
2187 * set up a flow and thereby allow us to exit fail-open.
2189 * See the top-level comment in fail-open.c for more information.
2191 send_packet_in_miss(ofproto, packet, flow, true);
2194 if (!facet->may_install) {
2195 facet_make_actions(ofproto, facet, packet);
2197 if (!execute_controller_action(ofproto, &facet->flow,
2198 facet->actions, facet->actions_len,
2200 struct flow_miss_op *op = &ops[(*n_ops)++];
2201 struct dpif_execute *execute = &op->dpif_op.execute;
2204 execute->type = DPIF_OP_EXECUTE;
2205 execute->key = miss->key;
2206 execute->key_len = miss->key_len;
2208 = (facet->may_install
2210 : xmemdup(facet->actions, facet->actions_len));
2211 execute->actions_len = facet->actions_len;
2212 execute->packet = packet;
2216 if (facet->may_install) {
2217 struct flow_miss_op *op = &ops[(*n_ops)++];
2218 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2221 put->type = DPIF_OP_FLOW_PUT;
2222 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2223 put->key = miss->key;
2224 put->key_len = miss->key_len;
2225 put->actions = facet->actions;
2226 put->actions_len = facet->actions_len;
2232 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2235 struct dpif_upcall *upcall;
2236 struct flow_miss *miss, *next_miss;
2237 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2238 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2247 /* Construct the to-do list.
2249 * This just amounts to extracting the flow from each packet and sticking
2250 * the packets that have the same flow in the same "flow_miss" structure so
2251 * that we can process them together. */
2253 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2254 struct flow_miss *miss;
2257 /* Obtain in_port and tun_id, at least, then set 'flow''s header
2259 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2260 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2261 flow.in_port, &flow);
2263 /* Handle 802.1ag, LACP, and STP specially. */
2264 if (process_special(ofproto, &flow, upcall->packet)) {
2265 ofpbuf_delete(upcall->packet);
2266 ofproto->n_matches++;
2270 /* Add other packets to a to-do list. */
2271 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2272 list_push_back(&miss->packets, &upcall->packet->list_node);
2275 /* Process each element in the to-do list, constructing the set of
2276 * operations to batch. */
2278 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2279 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2280 ofpbuf_list_delete(&miss->packets);
2281 hmap_remove(&todo, &miss->hmap_node);
2284 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2285 hmap_destroy(&todo);
2287 /* Execute batch. */
2288 for (i = 0; i < n_ops; i++) {
2289 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2291 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2293 /* Free memory and update facets. */
2294 for (i = 0; i < n_ops; i++) {
2295 struct flow_miss_op *op = &flow_miss_ops[i];
2296 struct dpif_execute *execute;
2297 struct dpif_flow_put *put;
2299 switch (op->dpif_op.type) {
2300 case DPIF_OP_EXECUTE:
2301 execute = &op->dpif_op.execute;
2302 if (op->facet->actions != execute->actions) {
2303 free((struct nlattr *) execute->actions);
2305 ofpbuf_delete((struct ofpbuf *) execute->packet);
2308 case DPIF_OP_FLOW_PUT:
2309 put = &op->dpif_op.flow_put;
2311 op->facet->installed = true;
2319 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2320 struct dpif_upcall *upcall)
2323 struct user_action_cookie cookie;
2325 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2327 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2328 if (ofproto->sflow) {
2329 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2330 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2332 ofpbuf_delete(upcall->packet);
2334 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2335 COVERAGE_INC(ofproto_dpif_ctlr_action);
2336 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2337 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2340 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2345 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2347 switch (upcall->type) {
2348 case DPIF_UC_ACTION:
2349 handle_userspace_upcall(ofproto, upcall);
2353 /* The caller handles these. */
2356 case DPIF_N_UC_TYPES:
2358 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2363 /* Flow expiration. */
2365 static int facet_max_idle(const struct ofproto_dpif *);
2366 static void update_stats(struct ofproto_dpif *);
2367 static void rule_expire(struct rule_dpif *);
2368 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2370 /* This function is called periodically by run(). Its job is to collect
2371 * updates for the flows that have been installed into the datapath, most
2372 * importantly when they last were used, and then use that information to
2373 * expire flows that have not been used recently.
2375 * Returns the number of milliseconds after which it should be called again. */
2377 expire(struct ofproto_dpif *ofproto)
2379 struct rule_dpif *rule, *next_rule;
2380 struct classifier *table;
2383 /* Update stats for each flow in the datapath. */
2384 update_stats(ofproto);
2386 /* Expire facets that have been idle too long. */
2387 dp_max_idle = facet_max_idle(ofproto);
2388 expire_facets(ofproto, dp_max_idle);
2390 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2391 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2392 struct cls_cursor cursor;
2394 cls_cursor_init(&cursor, table, NULL);
2395 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2400 /* All outstanding data in existing flows has been accounted, so it's a
2401 * good time to do bond rebalancing. */
2402 if (ofproto->has_bonded_bundles) {
2403 struct ofbundle *bundle;
2405 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2407 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2412 return MIN(dp_max_idle, 1000);
2415 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2417 * This function also pushes statistics updates to rules which each facet
2418 * resubmits into. Generally these statistics will be accurate. However, if a
2419 * facet changes the rule it resubmits into at some time in between
2420 * update_stats() runs, it is possible that statistics accrued to the
2421 * old rule will be incorrectly attributed to the new rule. This could be
2422 * avoided by calling update_stats() whenever rules are created or
2423 * deleted. However, the performance impact of making so many calls to the
2424 * datapath do not justify the benefit of having perfectly accurate statistics.
2427 update_stats(struct ofproto_dpif *p)
2429 const struct dpif_flow_stats *stats;
2430 struct dpif_flow_dump dump;
2431 const struct nlattr *key;
2434 dpif_flow_dump_start(&dump, p->dpif);
2435 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2436 struct facet *facet;
2439 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2443 odp_flow_key_format(key, key_len, &s);
2444 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2450 facet = facet_find(p, &flow);
2452 if (facet && facet->installed) {
2454 if (stats->n_packets >= facet->dp_packet_count) {
2455 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2456 facet->packet_count += extra;
2458 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2461 if (stats->n_bytes >= facet->dp_byte_count) {
2462 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2464 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2467 facet->dp_packet_count = stats->n_packets;
2468 facet->dp_byte_count = stats->n_bytes;
2470 facet_update_time(p, facet, stats->used);
2471 facet_account(p, facet);
2472 facet_push_stats(facet);
2474 /* There's a flow in the datapath that we know nothing about.
2476 COVERAGE_INC(facet_unexpected);
2477 dpif_flow_del(p->dpif, key, key_len, NULL);
2480 dpif_flow_dump_done(&dump);
2483 /* Calculates and returns the number of milliseconds of idle time after which
2484 * facets should expire from the datapath and we should fold their statistics
2485 * into their parent rules in userspace. */
2487 facet_max_idle(const struct ofproto_dpif *ofproto)
2490 * Idle time histogram.
2492 * Most of the time a switch has a relatively small number of facets. When
2493 * this is the case we might as well keep statistics for all of them in
2494 * userspace and to cache them in the kernel datapath for performance as
2497 * As the number of facets increases, the memory required to maintain
2498 * statistics about them in userspace and in the kernel becomes
2499 * significant. However, with a large number of facets it is likely that
2500 * only a few of them are "heavy hitters" that consume a large amount of
2501 * bandwidth. At this point, only heavy hitters are worth caching in the
2502 * kernel and maintaining in userspaces; other facets we can discard.
2504 * The technique used to compute the idle time is to build a histogram with
2505 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2506 * that is installed in the kernel gets dropped in the appropriate bucket.
2507 * After the histogram has been built, we compute the cutoff so that only
2508 * the most-recently-used 1% of facets (but at least
2509 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2510 * the most-recently-used bucket of facets is kept, so actually an
2511 * arbitrary number of facets can be kept in any given expiration run
2512 * (though the next run will delete most of those unless they receive
2515 * This requires a second pass through the facets, in addition to the pass
2516 * made by update_stats(), because the former function never looks
2517 * at uninstallable facets.
2519 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2520 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2521 int buckets[N_BUCKETS] = { 0 };
2522 int total, subtotal, bucket;
2523 struct facet *facet;
2527 total = hmap_count(&ofproto->facets);
2528 if (total <= ofproto->up.flow_eviction_threshold) {
2529 return N_BUCKETS * BUCKET_WIDTH;
2532 /* Build histogram. */
2534 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2535 long long int idle = now - facet->used;
2536 int bucket = (idle <= 0 ? 0
2537 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2538 : (unsigned int) idle / BUCKET_WIDTH);
2542 /* Find the first bucket whose flows should be expired. */
2543 subtotal = bucket = 0;
2545 subtotal += buckets[bucket++];
2546 } while (bucket < N_BUCKETS &&
2547 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2549 if (VLOG_IS_DBG_ENABLED()) {
2553 ds_put_cstr(&s, "keep");
2554 for (i = 0; i < N_BUCKETS; i++) {
2556 ds_put_cstr(&s, ", drop");
2559 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2562 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2566 return bucket * BUCKET_WIDTH;
2570 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2572 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2573 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2574 struct ofexpired expired;
2576 if (facet->installed) {
2577 struct dpif_flow_stats stats;
2579 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2581 facet_update_stats(ofproto, facet, &stats);
2584 expired.flow = facet->flow;
2585 expired.packet_count = facet->packet_count;
2586 expired.byte_count = facet->byte_count;
2587 expired.used = facet->used;
2588 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2593 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2595 long long int cutoff = time_msec() - dp_max_idle;
2596 struct facet *facet, *next_facet;
2598 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2599 facet_active_timeout(ofproto, facet);
2600 if (facet->used < cutoff) {
2601 facet_remove(ofproto, facet);
2606 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2607 * then delete it entirely. */
2609 rule_expire(struct rule_dpif *rule)
2611 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2612 struct facet *facet, *next_facet;
2616 /* Has 'rule' expired? */
2618 if (rule->up.hard_timeout
2619 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2620 reason = OFPRR_HARD_TIMEOUT;
2621 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2622 && now > rule->used + rule->up.idle_timeout * 1000) {
2623 reason = OFPRR_IDLE_TIMEOUT;
2628 COVERAGE_INC(ofproto_dpif_expired);
2630 /* Update stats. (This is a no-op if the rule expired due to an idle
2631 * timeout, because that only happens when the rule has no facets left.) */
2632 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2633 facet_remove(ofproto, facet);
2636 /* Get rid of the rule. */
2637 ofproto_rule_expire(&rule->up, reason);
2642 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2644 * The caller must already have determined that no facet with an identical
2645 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2646 * the ofproto's classifier table.
2648 * The facet will initially have no ODP actions. The caller should fix that
2649 * by calling facet_make_actions(). */
2650 static struct facet *
2651 facet_create(struct rule_dpif *rule, const struct flow *flow)
2653 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2654 struct facet *facet;
2656 facet = xzalloc(sizeof *facet);
2657 facet->used = time_msec();
2658 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2659 list_push_back(&rule->facets, &facet->list_node);
2661 facet->flow = *flow;
2662 netflow_flow_init(&facet->nf_flow);
2663 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2669 facet_free(struct facet *facet)
2671 free(facet->actions);
2676 execute_controller_action(struct ofproto_dpif *ofproto,
2677 const struct flow *flow,
2678 const struct nlattr *odp_actions, size_t actions_len,
2679 struct ofpbuf *packet)
2682 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2683 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2684 /* As an optimization, avoid a round-trip from userspace to kernel to
2685 * userspace. This also avoids possibly filling up kernel packet
2686 * buffers along the way.
2688 * This optimization will not accidentally catch sFlow
2689 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2690 * inside OVS_ACTION_ATTR_SAMPLE. */
2691 const struct nlattr *nla;
2693 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2694 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2702 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2703 * 'packet', which arrived on 'in_port'.
2705 * Takes ownership of 'packet'. */
2707 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2708 const struct nlattr *odp_actions, size_t actions_len,
2709 struct ofpbuf *packet)
2711 struct odputil_keybuf keybuf;
2715 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2720 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2721 odp_flow_key_from_flow(&key, flow);
2723 error = dpif_execute(ofproto->dpif, key.data, key.size,
2724 odp_actions, actions_len, packet);
2726 ofpbuf_delete(packet);
2730 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2731 * statistics appropriately. 'packet' must have at least sizeof(struct
2732 * ofp_packet_in) bytes of headroom.
2734 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2735 * applying flow_extract() to 'packet' would yield the same flow as
2738 * 'facet' must have accurately composed datapath actions; that is, it must
2739 * not be in need of revalidation.
2741 * Takes ownership of 'packet'. */
2743 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2744 struct ofpbuf *packet)
2746 struct dpif_flow_stats stats;
2748 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2750 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2751 stats.used = time_msec();
2752 if (execute_odp_actions(ofproto, &facet->flow,
2753 facet->actions, facet->actions_len, packet)) {
2754 facet_update_stats(ofproto, facet, &stats);
2758 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2760 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2761 * rule's statistics, via facet_uninstall().
2763 * - Removes 'facet' from its rule and from ofproto->facets.
2766 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2768 facet_uninstall(ofproto, facet);
2769 facet_flush_stats(ofproto, facet);
2770 hmap_remove(&ofproto->facets, &facet->hmap_node);
2771 list_remove(&facet->list_node);
2775 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2777 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2778 const struct ofpbuf *packet)
2780 const struct rule_dpif *rule = facet->rule;
2781 struct ofpbuf *odp_actions;
2782 struct action_xlate_ctx ctx;
2784 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2785 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2786 facet->tags = ctx.tags;
2787 facet->may_install = ctx.may_set_up_flow;
2788 facet->has_learn = ctx.has_learn;
2789 facet->has_normal = ctx.has_normal;
2790 facet->nf_flow.output_iface = ctx.nf_output_iface;
2792 if (facet->actions_len != odp_actions->size
2793 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2794 free(facet->actions);
2795 facet->actions_len = odp_actions->size;
2796 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2799 ofpbuf_delete(odp_actions);
2802 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2803 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2804 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2805 * since 'facet' was last updated.
2807 * Returns 0 if successful, otherwise a positive errno value.*/
2809 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2810 const struct nlattr *actions, size_t actions_len,
2811 struct dpif_flow_stats *stats)
2813 struct odputil_keybuf keybuf;
2814 enum dpif_flow_put_flags flags;
2818 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2820 flags |= DPIF_FP_ZERO_STATS;
2823 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2824 odp_flow_key_from_flow(&key, &facet->flow);
2826 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2827 actions, actions_len, stats);
2830 facet_reset_dp_stats(facet, stats);
2836 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2837 * 'zero_stats' is true, clears any existing statistics from the datapath for
2840 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2842 struct dpif_flow_stats stats;
2844 if (facet->may_install
2845 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2846 zero_stats ? &stats : NULL)) {
2847 facet->installed = true;
2852 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2855 const struct nlattr *a;
2859 if (facet->byte_count <= facet->accounted_bytes) {
2862 n_bytes = facet->byte_count - facet->accounted_bytes;
2863 facet->accounted_bytes = facet->byte_count;
2865 /* Feed information from the active flows back into the learning table to
2866 * ensure that table is always in sync with what is actually flowing
2867 * through the datapath. */
2868 if (facet->has_learn || facet->has_normal) {
2869 struct action_xlate_ctx ctx;
2871 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2872 ctx.may_learn = true;
2873 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2874 facet->rule->up.n_actions));
2877 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2881 /* This loop feeds byte counters to bond_account() for rebalancing to use
2882 * as a basis. We also need to track the actual VLAN on which the packet
2883 * is going to be sent to ensure that it matches the one passed to
2884 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2886 vlan_tci = facet->flow.vlan_tci;
2887 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2888 struct ofport_dpif *port;
2890 switch (nl_attr_type(a)) {
2891 const struct nlattr *nested;
2892 case OVS_ACTION_ATTR_OUTPUT:
2893 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2894 if (port && port->bundle && port->bundle->bond) {
2895 bond_account(port->bundle->bond, &facet->flow,
2896 vlan_tci_to_vid(vlan_tci), n_bytes);
2900 case OVS_ACTION_ATTR_POP:
2901 if (nl_attr_get_u16(a) == OVS_KEY_ATTR_8021Q) {
2902 vlan_tci = htons(0);
2906 case OVS_ACTION_ATTR_PUSH:
2907 nested = nl_attr_get(a);
2908 if (nl_attr_type(nested) == OVS_KEY_ATTR_8021Q) {
2909 const struct ovs_key_8021q *q_key;
2911 q_key = nl_attr_get_unspec(nested, sizeof(*q_key));
2912 vlan_tci = q_key->q_tci;
2919 /* If 'rule' is installed in the datapath, uninstalls it. */
2921 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2923 if (facet->installed) {
2924 struct odputil_keybuf keybuf;
2925 struct dpif_flow_stats stats;
2929 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2930 odp_flow_key_from_flow(&key, &facet->flow);
2932 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2933 facet_reset_dp_stats(facet, &stats);
2935 facet_update_stats(p, facet, &stats);
2937 facet->installed = false;
2939 assert(facet->dp_packet_count == 0);
2940 assert(facet->dp_byte_count == 0);
2944 /* Returns true if the only action for 'facet' is to send to the controller.
2945 * (We don't report NetFlow expiration messages for such facets because they
2946 * are just part of the control logic for the network, not real traffic). */
2948 facet_is_controller_flow(struct facet *facet)
2951 && facet->rule->up.n_actions == 1
2952 && action_outputs_to_port(&facet->rule->up.actions[0],
2953 htons(OFPP_CONTROLLER)));
2956 /* Resets 'facet''s datapath statistics counters. This should be called when
2957 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2958 * it should contain the statistics returned by dpif when 'facet' was reset in
2959 * the datapath. 'stats' will be modified to only included statistics new
2960 * since 'facet' was last updated. */
2962 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2964 if (stats && facet->dp_packet_count <= stats->n_packets
2965 && facet->dp_byte_count <= stats->n_bytes) {
2966 stats->n_packets -= facet->dp_packet_count;
2967 stats->n_bytes -= facet->dp_byte_count;
2970 facet->dp_packet_count = 0;
2971 facet->dp_byte_count = 0;
2974 /* Folds all of 'facet''s statistics into its rule. Also updates the
2975 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2976 * 'facet''s statistics in the datapath should have been zeroed and folded into
2977 * its packet and byte counts before this function is called. */
2979 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2981 assert(!facet->dp_byte_count);
2982 assert(!facet->dp_packet_count);
2984 facet_push_stats(facet);
2985 facet_account(ofproto, facet);
2987 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2988 struct ofexpired expired;
2989 expired.flow = facet->flow;
2990 expired.packet_count = facet->packet_count;
2991 expired.byte_count = facet->byte_count;
2992 expired.used = facet->used;
2993 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2996 facet->rule->packet_count += facet->packet_count;
2997 facet->rule->byte_count += facet->byte_count;
2999 /* Reset counters to prevent double counting if 'facet' ever gets
3001 facet_reset_counters(facet);
3003 netflow_flow_clear(&facet->nf_flow);
3006 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3007 * Returns it if found, otherwise a null pointer.
3009 * The returned facet might need revalidation; use facet_lookup_valid()
3010 * instead if that is important. */
3011 static struct facet *
3012 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3014 struct facet *facet;
3016 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3018 if (flow_equal(flow, &facet->flow)) {
3026 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3027 * Returns it if found, otherwise a null pointer.
3029 * The returned facet is guaranteed to be valid. */
3030 static struct facet *
3031 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3033 struct facet *facet = facet_find(ofproto, flow);
3035 /* The facet we found might not be valid, since we could be in need of
3036 * revalidation. If it is not valid, don't return it. */
3038 && (ofproto->need_revalidate
3039 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3040 && !facet_revalidate(ofproto, facet)) {
3041 COVERAGE_INC(facet_invalidated);
3048 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3050 * - If the rule found is different from 'facet''s current rule, moves
3051 * 'facet' to the new rule and recompiles its actions.
3053 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3054 * where it is and recompiles its actions anyway.
3056 * - If there is none, destroys 'facet'.
3058 * Returns true if 'facet' still exists, false if it has been destroyed. */
3060 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3062 struct action_xlate_ctx ctx;
3063 struct ofpbuf *odp_actions;
3064 struct rule_dpif *new_rule;
3065 bool actions_changed;
3067 COVERAGE_INC(facet_revalidate);
3069 /* Determine the new rule. */
3070 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3072 /* No new rule, so delete the facet. */
3073 facet_remove(ofproto, facet);
3077 /* Calculate new datapath actions.
3079 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3080 * emit a NetFlow expiration and, if so, we need to have the old state
3081 * around to properly compose it. */
3082 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3083 odp_actions = xlate_actions(&ctx,
3084 new_rule->up.actions, new_rule->up.n_actions);
3085 actions_changed = (facet->actions_len != odp_actions->size
3086 || memcmp(facet->actions, odp_actions->data,
3087 facet->actions_len));
3089 /* If the datapath actions changed or the installability changed,
3090 * then we need to talk to the datapath. */
3091 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
3092 if (ctx.may_set_up_flow) {
3093 struct dpif_flow_stats stats;
3095 facet_put__(ofproto, facet,
3096 odp_actions->data, odp_actions->size, &stats);
3097 facet_update_stats(ofproto, facet, &stats);
3099 facet_uninstall(ofproto, facet);
3102 /* The datapath flow is gone or has zeroed stats, so push stats out of
3103 * 'facet' into 'rule'. */
3104 facet_flush_stats(ofproto, facet);
3107 /* Update 'facet' now that we've taken care of all the old state. */
3108 facet->tags = ctx.tags;
3109 facet->nf_flow.output_iface = ctx.nf_output_iface;
3110 facet->may_install = ctx.may_set_up_flow;
3111 facet->has_learn = ctx.has_learn;
3112 facet->has_normal = ctx.has_normal;
3113 if (actions_changed) {
3114 free(facet->actions);
3115 facet->actions_len = odp_actions->size;
3116 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
3118 if (facet->rule != new_rule) {
3119 COVERAGE_INC(facet_changed_rule);
3120 list_remove(&facet->list_node);
3121 list_push_back(&new_rule->facets, &facet->list_node);
3122 facet->rule = new_rule;
3123 facet->used = new_rule->up.created;
3124 facet->rs_used = facet->used;
3127 ofpbuf_delete(odp_actions);
3132 /* Updates 'facet''s used time. Caller is responsible for calling
3133 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3135 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3138 if (used > facet->used) {
3140 if (used > facet->rule->used) {
3141 facet->rule->used = used;
3143 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3147 /* Folds the statistics from 'stats' into the counters in 'facet'.
3149 * Because of the meaning of a facet's counters, it only makes sense to do this
3150 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3151 * packet that was sent by hand or if it represents statistics that have been
3152 * cleared out of the datapath. */
3154 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
3155 const struct dpif_flow_stats *stats)
3157 if (stats->n_packets || stats->used > facet->used) {
3158 facet_update_time(ofproto, facet, stats->used);
3159 facet->packet_count += stats->n_packets;
3160 facet->byte_count += stats->n_bytes;
3161 facet_push_stats(facet);
3162 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3167 facet_reset_counters(struct facet *facet)
3169 facet->packet_count = 0;
3170 facet->byte_count = 0;
3171 facet->rs_packet_count = 0;
3172 facet->rs_byte_count = 0;
3173 facet->accounted_bytes = 0;
3177 facet_push_stats(struct facet *facet)
3179 uint64_t rs_packets, rs_bytes;
3181 assert(facet->packet_count >= facet->rs_packet_count);
3182 assert(facet->byte_count >= facet->rs_byte_count);
3183 assert(facet->used >= facet->rs_used);
3185 rs_packets = facet->packet_count - facet->rs_packet_count;
3186 rs_bytes = facet->byte_count - facet->rs_byte_count;
3188 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3189 facet->rs_packet_count = facet->packet_count;
3190 facet->rs_byte_count = facet->byte_count;
3191 facet->rs_used = facet->used;
3193 flow_push_stats(facet->rule, &facet->flow,
3194 rs_packets, rs_bytes, facet->used);
3198 struct ofproto_push {
3199 struct action_xlate_ctx ctx;
3206 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3208 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3211 rule->packet_count += push->packets;
3212 rule->byte_count += push->bytes;
3213 rule->used = MAX(push->used, rule->used);
3217 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3218 * 'rule''s actions. */
3220 flow_push_stats(const struct rule_dpif *rule,
3221 struct flow *flow, uint64_t packets, uint64_t bytes,
3224 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3225 struct ofproto_push push;
3227 push.packets = packets;
3231 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3232 push.ctx.resubmit_hook = push_resubmit;
3233 ofpbuf_delete(xlate_actions(&push.ctx,
3234 rule->up.actions, rule->up.n_actions));
3239 static struct rule_dpif *
3240 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3243 struct cls_rule *cls_rule;
3244 struct classifier *cls;
3246 if (table_id >= N_TABLES) {
3250 cls = &ofproto->up.tables[table_id];
3251 if (flow->frag & FLOW_FRAG_ANY
3252 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3253 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3254 * are unavailable. */
3255 struct flow ofpc_normal_flow = *flow;
3256 ofpc_normal_flow.tp_src = htons(0);
3257 ofpc_normal_flow.tp_dst = htons(0);
3258 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3260 cls_rule = classifier_lookup(cls, flow);
3262 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3266 complete_operation(struct rule_dpif *rule)
3268 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3270 rule_invalidate(rule);
3272 struct dpif_completion *c = xmalloc(sizeof *c);
3273 c->op = rule->up.pending;
3274 list_push_back(&ofproto->completions, &c->list_node);
3276 ofoperation_complete(rule->up.pending, 0);
3280 static struct rule *
3283 struct rule_dpif *rule = xmalloc(sizeof *rule);
3288 rule_dealloc(struct rule *rule_)
3290 struct rule_dpif *rule = rule_dpif_cast(rule_);
3295 rule_construct(struct rule *rule_)
3297 struct rule_dpif *rule = rule_dpif_cast(rule_);
3298 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3299 struct rule_dpif *victim;
3303 error = validate_actions(rule->up.actions, rule->up.n_actions,
3304 &rule->up.cr.flow, ofproto->max_ports);
3309 rule->used = rule->up.created;
3310 rule->packet_count = 0;
3311 rule->byte_count = 0;
3313 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3314 if (victim && !list_is_empty(&victim->facets)) {
3315 struct facet *facet;
3317 rule->facets = victim->facets;
3318 list_moved(&rule->facets);
3319 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3320 /* XXX: We're only clearing our local counters here. It's possible
3321 * that quite a few packets are unaccounted for in the datapath
3322 * statistics. These will be accounted to the new rule instead of
3323 * cleared as required. This could be fixed by clearing out the
3324 * datapath statistics for this facet, but currently it doesn't
3326 facet_reset_counters(facet);
3330 /* Must avoid list_moved() in this case. */
3331 list_init(&rule->facets);
3334 table_id = rule->up.table_id;
3335 rule->tag = (victim ? victim->tag
3337 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3338 ofproto->tables[table_id].basis));
3340 complete_operation(rule);
3345 rule_destruct(struct rule *rule_)
3347 struct rule_dpif *rule = rule_dpif_cast(rule_);
3348 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3349 struct facet *facet, *next_facet;
3351 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3352 facet_revalidate(ofproto, facet);
3355 complete_operation(rule);
3359 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3361 struct rule_dpif *rule = rule_dpif_cast(rule_);
3362 struct facet *facet;
3364 /* Start from historical data for 'rule' itself that are no longer tracked
3365 * in facets. This counts, for example, facets that have expired. */
3366 *packets = rule->packet_count;
3367 *bytes = rule->byte_count;
3369 /* Add any statistics that are tracked by facets. This includes
3370 * statistical data recently updated by ofproto_update_stats() as well as
3371 * stats for packets that were executed "by hand" via dpif_execute(). */
3372 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3373 *packets += facet->packet_count;
3374 *bytes += facet->byte_count;
3379 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
3381 struct rule_dpif *rule = rule_dpif_cast(rule_);
3382 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3383 struct action_xlate_ctx ctx;
3384 struct ofpbuf *odp_actions;
3385 struct facet *facet;
3388 /* First look for a related facet. If we find one, account it to that. */
3389 facet = facet_lookup_valid(ofproto, flow);
3390 if (facet && facet->rule == rule) {
3391 if (!facet->may_install) {
3392 facet_make_actions(ofproto, facet, packet);
3394 facet_execute(ofproto, facet, packet);
3398 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3399 * create a new facet for it and use that. */
3400 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3401 facet = facet_create(rule, flow);
3402 facet_make_actions(ofproto, facet, packet);
3403 facet_execute(ofproto, facet, packet);
3404 facet_install(ofproto, facet, true);
3408 /* We can't account anything to a facet. If we were to try, then that
3409 * facet would have a non-matching rule, busting our invariants. */
3410 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3411 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3412 size = packet->size;
3413 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3414 odp_actions->size, packet)) {
3415 rule->used = time_msec();
3416 rule->packet_count++;
3417 rule->byte_count += size;
3418 flow_push_stats(rule, flow, 1, size, rule->used);
3420 ofpbuf_delete(odp_actions);
3426 rule_modify_actions(struct rule *rule_)
3428 struct rule_dpif *rule = rule_dpif_cast(rule_);
3429 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3432 error = validate_actions(rule->up.actions, rule->up.n_actions,
3433 &rule->up.cr.flow, ofproto->max_ports);
3435 ofoperation_complete(rule->up.pending, error);
3439 complete_operation(rule);
3442 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3443 * Returns 0 if successful, otherwise a positive errno value. */
3445 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3446 const struct ofpbuf *packet)
3448 struct ofpbuf key, odp_actions;
3449 struct odputil_keybuf keybuf;
3453 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3454 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3455 odp_flow_key_from_flow(&key, &flow);
3457 ofpbuf_init(&odp_actions, 32);
3458 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3460 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3461 error = dpif_execute(ofproto->dpif,
3463 odp_actions.data, odp_actions.size,
3465 ofpbuf_uninit(&odp_actions);
3468 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3469 ofproto->up.name, odp_port, strerror(error));
3474 /* OpenFlow to datapath action translation. */
3476 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3477 struct action_xlate_ctx *ctx);
3478 static void xlate_normal(struct action_xlate_ctx *);
3481 put_userspace_action(const struct ofproto_dpif *ofproto,
3482 struct ofpbuf *odp_actions,
3483 const struct flow *flow,
3484 const struct user_action_cookie *cookie)
3489 pid = dpif_port_get_pid(ofproto->dpif,
3490 ofp_port_to_odp_port(flow->in_port));
3492 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3493 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3494 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3495 cookie, sizeof *cookie);
3496 nl_msg_end_nested(odp_actions, offset);
3498 return odp_actions->size - NLA_ALIGN(sizeof *cookie);
3501 /* Compose SAMPLE action for sFlow. */
3503 compose_sflow_action(const struct ofproto_dpif *ofproto,
3504 struct ofpbuf *odp_actions,
3505 const struct flow *flow,
3508 uint32_t port_ifindex;
3509 uint32_t probability;
3510 struct user_action_cookie cookie;
3511 size_t sample_offset, actions_offset;
3512 int cookie_offset, n_output;
3514 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3518 if (odp_port == OVSP_NONE) {
3522 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3526 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3528 /* Number of packets out of UINT_MAX to sample. */
3529 probability = dpif_sflow_get_probability(ofproto->sflow);
3530 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3532 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3534 cookie.type = USER_ACTION_COOKIE_SFLOW;
3535 cookie.data = port_ifindex;
3536 cookie.n_output = n_output;
3537 cookie.vlan_tci = 0;
3538 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3540 nl_msg_end_nested(odp_actions, actions_offset);
3541 nl_msg_end_nested(odp_actions, sample_offset);
3542 return cookie_offset;
3545 /* SAMPLE action must be first action in any given list of actions.
3546 * At this point we do not have all information required to build it. So try to
3547 * build sample action as complete as possible. */
3549 add_sflow_action(struct action_xlate_ctx *ctx)
3551 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3553 &ctx->flow, OVSP_NONE);
3554 ctx->sflow_odp_port = 0;
3555 ctx->sflow_n_outputs = 0;
3558 /* Fix SAMPLE action according to data collected while composing ODP actions.
3559 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3560 * USERSPACE action's user-cookie which is required for sflow. */
3562 fix_sflow_action(struct action_xlate_ctx *ctx)
3564 const struct flow *base = &ctx->base_flow;
3565 struct user_action_cookie *cookie;
3567 if (!ctx->user_cookie_offset) {
3571 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3573 assert(cookie != NULL);
3574 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3576 if (ctx->sflow_n_outputs) {
3577 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3578 ctx->sflow_odp_port);
3580 if (ctx->sflow_n_outputs >= 255) {
3581 cookie->n_output = 255;
3583 cookie->n_output = ctx->sflow_n_outputs;
3585 cookie->vlan_tci = base->vlan_tci;
3589 commit_action__(struct ofpbuf *odp_actions,
3590 enum ovs_action_attr act_type,
3591 enum ovs_key_attr key_type,
3592 const void *key, size_t key_size)
3594 size_t offset = nl_msg_start_nested(odp_actions, act_type);
3596 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3597 nl_msg_end_nested(odp_actions, offset);
3601 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3602 struct ofpbuf *odp_actions)
3604 if (base->tun_id == flow->tun_id) {
3607 base->tun_id = flow->tun_id;
3609 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3610 OVS_KEY_ATTR_TUN_ID, &base->tun_id, sizeof(base->tun_id));
3614 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3615 struct ofpbuf *odp_actions)
3617 struct ovs_key_ethernet eth_key;
3619 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3620 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3624 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3625 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3627 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3628 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3630 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3631 OVS_KEY_ATTR_ETHERNET, ð_key, sizeof(eth_key));
3635 commit_vlan_action(struct action_xlate_ctx *ctx, ovs_be16 new_tci)
3637 struct flow *base = &ctx->base_flow;
3639 if (base->vlan_tci == new_tci) {
3643 if (base->vlan_tci & htons(VLAN_CFI)) {
3644 nl_msg_put_u16(ctx->odp_actions, OVS_ACTION_ATTR_POP,
3645 OVS_KEY_ATTR_8021Q);
3648 if (new_tci & htons(VLAN_CFI)) {
3649 struct ovs_key_8021q q_key;
3651 q_key.q_tpid = htons(ETH_TYPE_VLAN);
3652 q_key.q_tci = new_tci & ~htons(VLAN_CFI);
3654 commit_action__(ctx->odp_actions, OVS_ACTION_ATTR_PUSH,
3655 OVS_KEY_ATTR_8021Q, &q_key, sizeof(q_key));
3657 base->vlan_tci = new_tci;
3661 commit_set_nw_action(const struct flow *flow, struct flow *base,
3662 struct ofpbuf *odp_actions)
3664 struct ovs_key_ipv4 ipv4_key;
3666 if (base->dl_type != htons(ETH_TYPE_IP) ||
3667 !base->nw_src || !base->nw_dst) {
3671 if (base->nw_src == flow->nw_src &&
3672 base->nw_dst == flow->nw_dst &&
3673 base->tos == flow->tos &&
3674 base->frag == flow->frag) {
3679 memset(&ipv4_key, 0, sizeof(ipv4_key));
3680 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3681 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3682 ipv4_key.ipv4_proto = base->nw_proto;
3683 ipv4_key.ipv4_tos = flow->tos & IP_DSCP_MASK;
3684 ipv4_key.ipv4_frag = (base->frag == 0 ? OVS_FRAG_TYPE_NONE
3685 : base->frag == FLOW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
3686 : OVS_FRAG_TYPE_LATER);
3688 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3689 OVS_KEY_ATTR_IPV4, &ipv4_key, sizeof(ipv4_key));
3693 commit_set_port_action(const struct flow *flow, struct flow *base,
3694 struct ofpbuf *odp_actions)
3696 if (!base->tp_src || !base->tp_dst) {
3700 if (base->tp_src == flow->tp_src &&
3701 base->tp_dst == flow->tp_dst) {
3705 if (flow->nw_proto == IPPROTO_TCP) {
3706 struct ovs_key_tcp port_key;
3708 port_key.tcp_src = base->tp_src = flow->tp_src;
3709 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3711 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3712 OVS_KEY_ATTR_TCP, &port_key, sizeof(port_key));
3714 } else if (flow->nw_proto == IPPROTO_UDP) {
3715 struct ovs_key_udp port_key;
3717 port_key.udp_src = base->tp_src = flow->tp_src;
3718 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3720 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3721 OVS_KEY_ATTR_UDP, &port_key, sizeof(port_key));
3726 commit_set_priority_action(const struct flow *flow, struct flow *base,
3727 struct ofpbuf *odp_actions)
3729 if (base->priority == flow->priority) {
3732 base->priority = flow->priority;
3734 commit_action__(odp_actions, OVS_ACTION_ATTR_SET,
3735 OVS_KEY_ATTR_PRIORITY, &base->priority,
3736 sizeof(base->priority));
3740 commit_odp_actions(struct action_xlate_ctx *ctx)
3742 const struct flow *flow = &ctx->flow;
3743 struct flow *base = &ctx->base_flow;
3744 struct ofpbuf *odp_actions = ctx->odp_actions;
3746 commit_set_tun_id_action(flow, base, odp_actions);
3747 commit_set_ether_addr_action(flow, base, odp_actions);
3748 commit_vlan_action(ctx, flow->vlan_tci);
3749 commit_set_nw_action(flow, base, odp_actions);
3750 commit_set_port_action(flow, base, odp_actions);
3751 commit_set_priority_action(flow, base, odp_actions);
3755 compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port)
3757 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3758 ctx->sflow_odp_port = odp_port;
3759 ctx->sflow_n_outputs++;
3763 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3765 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3766 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3769 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
3770 || !stp_forward_in_state(ofport->stp_state)) {
3771 /* Forwarding disabled on port. */
3776 * We don't have an ofport record for this port, but it doesn't hurt to
3777 * allow forwarding to it anyhow. Maybe such a port will appear later
3778 * and we're pre-populating the flow table.
3782 commit_odp_actions(ctx);
3783 compose_output_action(ctx, odp_port);
3784 ctx->nf_output_iface = ofp_port;
3788 xlate_table_action(struct action_xlate_ctx *ctx,
3789 uint16_t in_port, uint8_t table_id)
3791 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3792 struct ofproto_dpif *ofproto = ctx->ofproto;
3793 struct rule_dpif *rule;
3794 uint16_t old_in_port;
3795 uint8_t old_table_id;
3797 old_table_id = ctx->table_id;
3798 ctx->table_id = table_id;
3800 /* Look up a flow with 'in_port' as the input port. */
3801 old_in_port = ctx->flow.in_port;
3802 ctx->flow.in_port = in_port;
3803 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3806 if (table_id > 0 && table_id < N_TABLES) {
3807 struct table_dpif *table = &ofproto->tables[table_id];
3808 if (table->other_table) {
3811 : rule_calculate_tag(&ctx->flow,
3812 &table->other_table->wc,
3817 /* Restore the original input port. Otherwise OFPP_NORMAL and
3818 * OFPP_IN_PORT will have surprising behavior. */
3819 ctx->flow.in_port = old_in_port;
3821 if (ctx->resubmit_hook) {
3822 ctx->resubmit_hook(ctx, rule);
3827 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3831 ctx->table_id = old_table_id;
3833 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3835 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3836 MAX_RESUBMIT_RECURSION);
3841 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3842 const struct nx_action_resubmit *nar)
3847 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3849 : ntohs(nar->in_port));
3850 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3852 xlate_table_action(ctx, in_port, table_id);
3856 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3858 struct ofport_dpif *ofport;
3860 commit_odp_actions(ctx);
3861 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3862 uint16_t ofp_port = ofport->up.ofp_port;
3863 if (ofp_port != ctx->flow.in_port
3864 && !(ofport->up.opp.config & mask)
3865 && stp_forward_in_state(ofport->stp_state)) {
3866 compose_output_action(ctx, ofport->odp_port);
3870 ctx->nf_output_iface = NF_OUT_FLOOD;
3874 compose_controller_action(struct action_xlate_ctx *ctx, int len)
3876 struct user_action_cookie cookie;
3878 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
3880 cookie.n_output = 0;
3881 cookie.vlan_tci = 0;
3882 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
3886 xlate_output_action__(struct action_xlate_ctx *ctx,
3887 uint16_t port, uint16_t max_len)
3889 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3891 ctx->nf_output_iface = NF_OUT_DROP;
3895 add_output_action(ctx, ctx->flow.in_port);
3898 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3904 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3907 flood_packets(ctx, htonl(0));
3909 case OFPP_CONTROLLER:
3910 commit_odp_actions(ctx);
3911 compose_controller_action(ctx, max_len);
3914 add_output_action(ctx, OFPP_LOCAL);
3919 if (port != ctx->flow.in_port) {
3920 add_output_action(ctx, port);
3925 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3926 ctx->nf_output_iface = NF_OUT_FLOOD;
3927 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3928 ctx->nf_output_iface = prev_nf_output_iface;
3929 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3930 ctx->nf_output_iface != NF_OUT_FLOOD) {
3931 ctx->nf_output_iface = NF_OUT_MULTI;
3936 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3937 const struct nx_action_output_reg *naor)
3941 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3943 if (ofp_port <= UINT16_MAX) {
3944 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3949 xlate_output_action(struct action_xlate_ctx *ctx,
3950 const struct ofp_action_output *oao)
3952 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3956 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3957 const struct ofp_action_enqueue *oae)
3959 uint16_t ofp_port, odp_port;
3960 uint32_t flow_priority, priority;
3963 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3966 /* Fall back to ordinary output action. */
3967 xlate_output_action__(ctx, ntohs(oae->port), 0);
3971 /* Figure out datapath output port. */
3972 ofp_port = ntohs(oae->port);
3973 if (ofp_port == OFPP_IN_PORT) {
3974 ofp_port = ctx->flow.in_port;
3975 } else if (ofp_port == ctx->flow.in_port) {
3978 odp_port = ofp_port_to_odp_port(ofp_port);
3980 /* Add datapath actions. */
3981 flow_priority = ctx->flow.priority;
3982 ctx->flow.priority = priority;
3983 add_output_action(ctx, odp_port);
3984 ctx->flow.priority = flow_priority;
3986 /* Update NetFlow output port. */
3987 if (ctx->nf_output_iface == NF_OUT_DROP) {
3988 ctx->nf_output_iface = odp_port;
3989 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3990 ctx->nf_output_iface = NF_OUT_MULTI;
3995 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3996 const struct nx_action_set_queue *nasq)
4001 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4004 /* Couldn't translate queue to a priority, so ignore. A warning
4005 * has already been logged. */
4009 ctx->flow.priority = priority;
4012 struct xlate_reg_state {
4018 xlate_autopath(struct action_xlate_ctx *ctx,
4019 const struct nx_action_autopath *naa)
4021 uint16_t ofp_port = ntohl(naa->id);
4022 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4024 if (!port || !port->bundle) {
4025 ofp_port = OFPP_NONE;
4026 } else if (port->bundle->bond) {
4027 /* Autopath does not support VLAN hashing. */
4028 struct ofport_dpif *slave = bond_choose_output_slave(
4029 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4031 ofp_port = slave->up.ofp_port;
4034 autopath_execute(naa, &ctx->flow, ofp_port);
4038 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4040 struct ofproto_dpif *ofproto = ofproto_;
4041 struct ofport_dpif *port;
4051 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4054 port = get_ofp_port(ofproto, ofp_port);
4055 return port ? port->may_enable : false;
4060 xlate_learn_action(struct action_xlate_ctx *ctx,
4061 const struct nx_action_learn *learn)
4063 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4064 struct ofputil_flow_mod fm;
4067 learn_execute(learn, &ctx->flow, &fm);
4069 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4070 if (error && !VLOG_DROP_WARN(&rl)) {
4071 char *msg = ofputil_error_to_string(error);
4072 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4080 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4082 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4083 ? htonl(OFPPC_NO_RECV_STP)
4084 : htonl(OFPPC_NO_RECV))) {
4088 /* Only drop packets here if both forwarding and learning are
4089 * disabled. If just learning is enabled, we need to have
4090 * OFPP_NORMAL and the learning action have a look at the packet
4091 * before we can drop it. */
4092 if (!stp_forward_in_state(port->stp_state)
4093 && !stp_learn_in_state(port->stp_state)) {
4101 do_xlate_actions(const union ofp_action *in, size_t n_in,
4102 struct action_xlate_ctx *ctx)
4104 const struct ofport_dpif *port;
4105 const union ofp_action *ia;
4108 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4109 if (port && !may_receive(port, ctx)) {
4110 /* Drop this flow. */
4114 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4115 const struct ofp_action_dl_addr *oada;
4116 const struct nx_action_resubmit *nar;
4117 const struct nx_action_set_tunnel *nast;
4118 const struct nx_action_set_queue *nasq;
4119 const struct nx_action_multipath *nam;
4120 const struct nx_action_autopath *naa;
4121 const struct nx_action_bundle *nab;
4122 const struct nx_action_output_reg *naor;
4123 enum ofputil_action_code code;
4130 code = ofputil_decode_action_unsafe(ia);
4132 case OFPUTIL_OFPAT_OUTPUT:
4133 xlate_output_action(ctx, &ia->output);
4136 case OFPUTIL_OFPAT_SET_VLAN_VID:
4137 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4138 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4141 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4142 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4143 ctx->flow.vlan_tci |= htons(
4144 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4147 case OFPUTIL_OFPAT_STRIP_VLAN:
4148 ctx->flow.vlan_tci = htons(0);
4151 case OFPUTIL_OFPAT_SET_DL_SRC:
4152 oada = ((struct ofp_action_dl_addr *) ia);
4153 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4156 case OFPUTIL_OFPAT_SET_DL_DST:
4157 oada = ((struct ofp_action_dl_addr *) ia);
4158 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4161 case OFPUTIL_OFPAT_SET_NW_SRC:
4162 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4165 case OFPUTIL_OFPAT_SET_NW_DST:
4166 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4169 case OFPUTIL_OFPAT_SET_NW_TOS:
4170 ctx->flow.tos &= ~IP_DSCP_MASK;
4171 ctx->flow.tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4174 case OFPUTIL_OFPAT_SET_TP_SRC:
4175 ctx->flow.tp_src = ia->tp_port.tp_port;
4178 case OFPUTIL_OFPAT_SET_TP_DST:
4179 ctx->flow.tp_dst = ia->tp_port.tp_port;
4182 case OFPUTIL_OFPAT_ENQUEUE:
4183 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4186 case OFPUTIL_NXAST_RESUBMIT:
4187 nar = (const struct nx_action_resubmit *) ia;
4188 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4191 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4192 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4195 case OFPUTIL_NXAST_SET_TUNNEL:
4196 nast = (const struct nx_action_set_tunnel *) ia;
4197 tun_id = htonll(ntohl(nast->tun_id));
4198 ctx->flow.tun_id = tun_id;
4201 case OFPUTIL_NXAST_SET_QUEUE:
4202 nasq = (const struct nx_action_set_queue *) ia;
4203 xlate_set_queue_action(ctx, nasq);
4206 case OFPUTIL_NXAST_POP_QUEUE:
4207 ctx->flow.priority = ctx->original_priority;
4210 case OFPUTIL_NXAST_REG_MOVE:
4211 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4215 case OFPUTIL_NXAST_REG_LOAD:
4216 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4220 case OFPUTIL_NXAST_NOTE:
4221 /* Nothing to do. */
4224 case OFPUTIL_NXAST_SET_TUNNEL64:
4225 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4226 ctx->flow.tun_id = tun_id;
4229 case OFPUTIL_NXAST_MULTIPATH:
4230 nam = (const struct nx_action_multipath *) ia;
4231 multipath_execute(nam, &ctx->flow);
4234 case OFPUTIL_NXAST_AUTOPATH:
4235 naa = (const struct nx_action_autopath *) ia;
4236 xlate_autopath(ctx, naa);
4239 case OFPUTIL_NXAST_BUNDLE:
4240 ctx->ofproto->has_bundle_action = true;
4241 nab = (const struct nx_action_bundle *) ia;
4242 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4247 case OFPUTIL_NXAST_BUNDLE_LOAD:
4248 ctx->ofproto->has_bundle_action = true;
4249 nab = (const struct nx_action_bundle *) ia;
4250 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4254 case OFPUTIL_NXAST_OUTPUT_REG:
4255 naor = (const struct nx_action_output_reg *) ia;
4256 xlate_output_reg_action(ctx, naor);
4259 case OFPUTIL_NXAST_LEARN:
4260 ctx->has_learn = true;
4261 if (ctx->may_learn) {
4262 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4266 case OFPUTIL_NXAST_EXIT:
4272 /* We've let OFPP_NORMAL and the learning action look at the packet,
4273 * so drop it now if forwarding is disabled. */
4274 if (port && !stp_forward_in_state(port->stp_state)) {
4275 ofpbuf_clear(ctx->odp_actions);
4276 add_sflow_action(ctx);
4281 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4282 struct ofproto_dpif *ofproto, const struct flow *flow,
4283 const struct ofpbuf *packet)
4285 ctx->ofproto = ofproto;
4287 ctx->packet = packet;
4288 ctx->may_learn = packet != NULL;
4289 ctx->resubmit_hook = NULL;
4292 static struct ofpbuf *
4293 xlate_actions(struct action_xlate_ctx *ctx,
4294 const union ofp_action *in, size_t n_in)
4296 COVERAGE_INC(ofproto_dpif_xlate);
4298 ctx->odp_actions = ofpbuf_new(512);
4299 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4301 ctx->may_set_up_flow = true;
4302 ctx->has_learn = false;
4303 ctx->has_normal = false;
4304 ctx->nf_output_iface = NF_OUT_DROP;
4306 ctx->original_priority = ctx->flow.priority;
4307 ctx->base_flow = ctx->flow;
4308 ctx->base_flow.tun_id = 0;
4312 if (ctx->flow.frag & FLOW_FRAG_ANY) {
4313 switch (ctx->ofproto->up.frag_handling) {
4314 case OFPC_FRAG_NORMAL:
4315 /* We must pretend that transport ports are unavailable. */
4316 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4317 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4320 case OFPC_FRAG_DROP:
4321 return ctx->odp_actions;
4323 case OFPC_FRAG_REASM:
4326 case OFPC_FRAG_NX_MATCH:
4327 /* Nothing to do. */
4332 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4333 ctx->may_set_up_flow = false;
4334 return ctx->odp_actions;
4336 add_sflow_action(ctx);
4337 do_xlate_actions(in, n_in, ctx);
4339 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4340 ctx->odp_actions->data,
4341 ctx->odp_actions->size)) {
4342 ctx->may_set_up_flow = false;
4344 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4346 compose_output_action(ctx, OVSP_LOCAL);
4349 fix_sflow_action(ctx);
4352 return ctx->odp_actions;
4355 /* OFPP_NORMAL implementation. */
4358 struct ofport_dpif *port;
4363 struct dst builtin[32];
4365 size_t n, allocated;
4368 static void dst_set_init(struct dst_set *);
4369 static void dst_set_add(struct dst_set *, const struct dst *);
4370 static void dst_set_free(struct dst_set *);
4372 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4374 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4375 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4376 * the bundle on which the packet was received, returns the VLAN to which the
4379 * Both 'vid' and the return value are in the range 0...4095. */
4381 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4383 switch (in_bundle->vlan_mode) {
4384 case PORT_VLAN_ACCESS:
4385 return in_bundle->vlan;
4388 case PORT_VLAN_TRUNK:
4391 case PORT_VLAN_NATIVE_UNTAGGED:
4392 case PORT_VLAN_NATIVE_TAGGED:
4393 return vid ? vid : in_bundle->vlan;
4400 /* Given 'vlan', the VLAN that a packet belongs to, and
4401 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4402 * that should be included in the 802.1Q header. (If the return value is 0,
4403 * then the 802.1Q header should only be included in the packet if there is a
4406 * Both 'vlan' and the return value are in the range 0...4095. */
4408 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4410 switch (out_bundle->vlan_mode) {
4411 case PORT_VLAN_ACCESS:
4414 case PORT_VLAN_TRUNK:
4415 case PORT_VLAN_NATIVE_TAGGED:
4418 case PORT_VLAN_NATIVE_UNTAGGED:
4419 return vlan == out_bundle->vlan ? 0 : vlan;
4427 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
4428 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
4432 vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci));
4433 dst->vid = output_vlan_to_vid(out_bundle, vlan);
4435 dst->port = (!out_bundle->bond
4436 ? ofbundle_get_a_port(out_bundle)
4437 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4438 dst->vid, &ctx->tags));
4439 return dst->port != NULL;
4443 mirror_mask_ffs(mirror_mask_t mask)
4445 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4450 dst_set_init(struct dst_set *set)
4452 set->dsts = set->builtin;
4454 set->allocated = ARRAY_SIZE(set->builtin);
4458 dst_set_add(struct dst_set *set, const struct dst *dst)
4460 if (set->n >= set->allocated) {
4461 size_t new_allocated;
4462 struct dst *new_dsts;
4464 new_allocated = set->allocated * 2;
4465 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
4466 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
4470 set->dsts = new_dsts;
4471 set->allocated = new_allocated;
4473 set->dsts[set->n++] = *dst;
4477 dst_set_free(struct dst_set *set)
4479 if (set->dsts != set->builtin) {
4485 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
4488 for (i = 0; i < set->n; i++) {
4489 if (set->dsts[i].vid == test->vid
4490 && set->dsts[i].port == test->port) {
4498 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4500 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4501 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4505 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4507 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4510 /* Returns an arbitrary interface within 'bundle'. */
4511 static struct ofport_dpif *
4512 ofbundle_get_a_port(const struct ofbundle *bundle)
4514 return CONTAINER_OF(list_front(&bundle->ports),
4515 struct ofport_dpif, bundle_node);
4519 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4520 const struct ofbundle *in_bundle,
4521 const struct ofbundle *out_bundle, struct dst_set *set)
4525 if (out_bundle == OFBUNDLE_FLOOD) {
4526 struct ofbundle *bundle;
4528 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4529 if (bundle != in_bundle
4530 && ofbundle_includes_vlan(bundle, vlan)
4531 && bundle->floodable
4532 && !bundle->mirror_out
4533 && set_dst(ctx, &dst, in_bundle, bundle)) {
4534 dst_set_add(set, &dst);
4537 ctx->nf_output_iface = NF_OUT_FLOOD;
4538 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
4539 dst_set_add(set, &dst);
4540 ctx->nf_output_iface = dst.port->odp_port;
4545 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4547 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4550 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4551 * to a VLAN. In general most packets may be mirrored but we want to drop
4552 * protocols that may confuse switches. */
4554 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4556 /* If you change this function's behavior, please update corresponding
4557 * documentation in vswitch.xml at the same time. */
4558 if (dst[0] != 0x01) {
4559 /* All the currently banned MACs happen to start with 01 currently, so
4560 * this is a quick way to eliminate most of the good ones. */
4562 if (eth_addr_is_reserved(dst)) {
4563 /* Drop STP, IEEE pause frames, and other reserved protocols
4564 * (01-80-c2-00-00-0x). */
4568 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4570 if ((dst[3] & 0xfe) == 0xcc &&
4571 (dst[4] & 0xfe) == 0xcc &&
4572 (dst[5] & 0xfe) == 0xcc) {
4573 /* Drop the following protocols plus others following the same
4576 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4577 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4578 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4582 if (!(dst[3] | dst[4] | dst[5])) {
4583 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4592 compose_mirror_dsts(struct action_xlate_ctx *ctx,
4593 uint16_t vlan, const struct ofbundle *in_bundle,
4594 struct dst_set *set)
4596 struct ofproto_dpif *ofproto = ctx->ofproto;
4597 mirror_mask_t mirrors;
4601 mirrors = in_bundle->src_mirrors;
4602 for (i = 0; i < set->n; i++) {
4603 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
4610 flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4612 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4613 if (vlan_is_mirrored(m, vlan)) {
4617 if (set_dst(ctx, &dst, in_bundle, m->out)
4618 && !dst_is_duplicate(set, &dst)) {
4619 dst_set_add(set, &dst);
4621 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)) {
4622 struct ofbundle *bundle;
4624 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4625 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4626 && set_dst(ctx, &dst, in_bundle, bundle))
4628 /* set_dst() got dst->vid from the input packet's VLAN,
4629 * not from m->out_vlan, so recompute it. */
4630 dst.vid = output_vlan_to_vid(bundle, m->out_vlan);
4632 if (dst_is_duplicate(set, &dst)) {
4636 if (bundle == in_bundle && dst.vid == flow_vid) {
4637 /* Don't send out input port on same VLAN. */
4640 dst_set_add(set, &dst);
4645 mirrors &= mirrors - 1;
4650 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
4651 const struct ofbundle *in_bundle,
4652 const struct ofbundle *out_bundle)
4654 uint16_t initial_vid, cur_vid;
4655 const struct dst *dst;
4659 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
4660 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
4666 /* Output all the packets we can without having to change the VLAN. */
4667 commit_odp_actions(ctx);
4668 initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4669 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4670 if (dst->vid != initial_vid) {
4673 compose_output_action(ctx, dst->port->odp_port);
4676 /* Then output the rest. */
4677 cur_vid = initial_vid;
4678 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4679 if (dst->vid == initial_vid) {
4682 if (dst->vid != cur_vid) {
4685 tci = htons(dst->vid);
4686 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4688 tci |= htons(VLAN_CFI);
4690 commit_vlan_action(ctx, tci);
4694 compose_output_action(ctx, dst->port->odp_port);
4700 /* Returns the effective vlan of a packet, taking into account both the
4701 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
4702 * the packet is untagged and -1 indicates it has an invalid header and
4703 * should be dropped. */
4705 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
4706 struct ofbundle *in_bundle, bool have_packet)
4708 int vlan = vlan_tci_to_vid(flow->vlan_tci);
4710 if (in_bundle->vlan_mode == PORT_VLAN_ACCESS) {
4711 /* Drop tagged packet on access port */
4713 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4714 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4715 "packet received on port %s configured with "
4716 "implicit VLAN %"PRIu16,
4717 ofproto->up.name, vlan,
4718 in_bundle->name, in_bundle->vlan);
4721 } else if (ofbundle_includes_vlan(in_bundle, vlan)) {
4724 /* Drop packets from a VLAN not member of the trunk */
4726 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4727 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
4728 "packet received on port %s not configured for "
4730 ofproto->up.name, vlan, in_bundle->name, vlan);
4735 if (in_bundle->vlan_mode != PORT_VLAN_TRUNK) {
4736 return in_bundle->vlan;
4738 return ofbundle_includes_vlan(in_bundle, 0) ? 0 : -1;
4743 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4744 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4745 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4747 is_gratuitous_arp(const struct flow *flow)
4749 return (flow->dl_type == htons(ETH_TYPE_ARP)
4750 && eth_addr_is_broadcast(flow->dl_dst)
4751 && (flow->nw_proto == ARP_OP_REPLY
4752 || (flow->nw_proto == ARP_OP_REQUEST
4753 && flow->nw_src == flow->nw_dst)));
4757 update_learning_table(struct ofproto_dpif *ofproto,
4758 const struct flow *flow, int vlan,
4759 struct ofbundle *in_bundle)
4761 struct mac_entry *mac;
4763 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4767 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4768 if (is_gratuitous_arp(flow)) {
4769 /* We don't want to learn from gratuitous ARP packets that are
4770 * reflected back over bond slaves so we lock the learning table. */
4771 if (!in_bundle->bond) {
4772 mac_entry_set_grat_arp_lock(mac);
4773 } else if (mac_entry_is_grat_arp_locked(mac)) {
4778 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4779 /* The log messages here could actually be useful in debugging,
4780 * so keep the rate limit relatively high. */
4781 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4782 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4783 "on port %s in VLAN %d",
4784 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4785 in_bundle->name, vlan);
4787 mac->port.p = in_bundle;
4788 tag_set_add(&ofproto->revalidate_set,
4789 mac_learning_changed(ofproto->ml, mac));
4793 /* Determines whether packets in 'flow' within 'br' should be forwarded or
4794 * dropped. Returns true if they may be forwarded, false if they should be
4797 * If 'have_packet' is true, it indicates that the caller is processing a
4798 * received packet. If 'have_packet' is false, then the caller is just
4799 * revalidating an existing flow because configuration has changed. Either
4800 * way, 'have_packet' only affects logging (there is no point in logging errors
4801 * during revalidation).
4803 * Sets '*in_portp' to the input port. This will be a null pointer if
4804 * flow->in_port does not designate a known input port (in which case
4805 * is_admissible() returns false).
4807 * When returning true, sets '*vlanp' to the effective VLAN of the input
4808 * packet, as returned by flow_get_vlan().
4810 * May also add tags to '*tags', although the current implementation only does
4811 * so in one special case.
4814 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4816 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
4818 struct ofport_dpif *in_port;
4819 struct ofbundle *in_bundle;
4822 /* Find the port and bundle for the received packet. */
4823 in_port = get_ofp_port(ofproto, flow->in_port);
4824 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
4825 if (!in_port || !in_bundle) {
4826 /* No interface? Something fishy... */
4828 /* Odd. A few possible reasons here:
4830 * - We deleted a port but there are still a few packets queued up
4833 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4834 * we don't know about.
4836 * - Packet arrived on the local port but the local port is not
4839 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4841 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4843 ofproto->up.name, flow->in_port);
4848 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
4853 /* Drop frames for reserved multicast addresses only if forward_bpdu
4854 * option is absent. */
4855 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
4859 /* Drop frames on bundles reserved for mirroring. */
4860 if (in_bundle->mirror_out) {
4862 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4863 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4864 "%s, which is reserved exclusively for mirroring",
4865 ofproto->up.name, in_bundle->name);
4870 if (in_bundle->bond) {
4871 struct mac_entry *mac;
4873 switch (bond_check_admissibility(in_bundle->bond, in_port,
4874 flow->dl_dst, tags)) {
4881 case BV_DROP_IF_MOVED:
4882 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4883 if (mac && mac->port.p != in_bundle &&
4884 (!is_gratuitous_arp(flow)
4885 || mac_entry_is_grat_arp_locked(mac))) {
4896 xlate_normal(struct action_xlate_ctx *ctx)
4898 struct ofbundle *in_bundle;
4899 struct ofbundle *out_bundle;
4900 struct mac_entry *mac;
4903 ctx->has_normal = true;
4905 /* Check whether we should drop packets in this flow. */
4906 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
4907 &ctx->tags, &vlan, &in_bundle)) {
4912 /* Learn source MAC. */
4913 if (ctx->may_learn) {
4914 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4917 /* Determine output bundle. */
4918 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4921 out_bundle = mac->port.p;
4922 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4923 /* If we are revalidating but don't have a learning entry then eject
4924 * the flow. Installing a flow that floods packets opens up a window
4925 * of time where we could learn from a packet reflected on a bond and
4926 * blackhole packets before the learning table is updated to reflect
4927 * the correct port. */
4928 ctx->may_set_up_flow = false;
4931 out_bundle = OFBUNDLE_FLOOD;
4934 /* Don't send packets out their input bundles. */
4935 if (in_bundle == out_bundle) {
4941 compose_actions(ctx, vlan, in_bundle, out_bundle);
4945 /* Optimized flow revalidation.
4947 * It's a difficult problem, in general, to tell which facets need to have
4948 * their actions recalculated whenever the OpenFlow flow table changes. We
4949 * don't try to solve that general problem: for most kinds of OpenFlow flow
4950 * table changes, we recalculate the actions for every facet. This is
4951 * relatively expensive, but it's good enough if the OpenFlow flow table
4952 * doesn't change very often.
4954 * However, we can expect one particular kind of OpenFlow flow table change to
4955 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4956 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4957 * table, we add a special case that applies to flow tables in which every rule
4958 * has the same form (that is, the same wildcards), except that the table is
4959 * also allowed to have a single "catch-all" flow that matches all packets. We
4960 * optimize this case by tagging all of the facets that resubmit into the table
4961 * and invalidating the same tag whenever a flow changes in that table. The
4962 * end result is that we revalidate just the facets that need it (and sometimes
4963 * a few more, but not all of the facets or even all of the facets that
4964 * resubmit to the table modified by MAC learning). */
4966 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
4967 * into an OpenFlow table with the given 'basis'. */
4969 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
4972 if (flow_wildcards_is_catchall(wc)) {
4975 struct flow tag_flow = *flow;
4976 flow_zero_wildcards(&tag_flow, wc);
4977 return tag_create_deterministic(flow_hash(&tag_flow, secret));
4981 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
4982 * taggability of that table.
4984 * This function must be called after *each* change to a flow table. If you
4985 * skip calling it on some changes then the pointer comparisons at the end can
4986 * be invalid if you get unlucky. For example, if a flow removal causes a
4987 * cls_table to be destroyed and then a flow insertion causes a cls_table with
4988 * different wildcards to be created with the same address, then this function
4989 * will incorrectly skip revalidation. */
4991 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
4993 struct table_dpif *table = &ofproto->tables[table_id];
4994 const struct classifier *cls = &ofproto->up.tables[table_id];
4995 struct cls_table *catchall, *other;
4996 struct cls_table *t;
4998 catchall = other = NULL;
5000 switch (hmap_count(&cls->tables)) {
5002 /* We could tag this OpenFlow table but it would make the logic a
5003 * little harder and it's a corner case that doesn't seem worth it
5009 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5010 if (cls_table_is_catchall(t)) {
5012 } else if (!other) {
5015 /* Indicate that we can't tag this by setting both tables to
5016 * NULL. (We know that 'catchall' is already NULL.) */
5023 /* Can't tag this table. */
5027 if (table->catchall_table != catchall || table->other_table != other) {
5028 table->catchall_table = catchall;
5029 table->other_table = other;
5030 ofproto->need_revalidate = true;
5034 /* Given 'rule' that has changed in some way (either it is a rule being
5035 * inserted, a rule being deleted, or a rule whose actions are being
5036 * modified), marks facets for revalidation to ensure that packets will be
5037 * forwarded correctly according to the new state of the flow table.
5039 * This function must be called after *each* change to a flow table. See
5040 * the comment on table_update_taggable() for more information. */
5042 rule_invalidate(const struct rule_dpif *rule)
5044 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5046 table_update_taggable(ofproto, rule->up.table_id);
5048 if (!ofproto->need_revalidate) {
5049 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5051 if (table->other_table && rule->tag) {
5052 tag_set_add(&ofproto->revalidate_set, rule->tag);
5054 ofproto->need_revalidate = true;
5060 set_frag_handling(struct ofproto *ofproto_,
5061 enum ofp_config_flags frag_handling)
5063 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5065 if (frag_handling != OFPC_FRAG_REASM) {
5066 ofproto->need_revalidate = true;
5074 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5075 const struct flow *flow,
5076 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5078 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5081 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5082 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5085 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5086 ofproto->max_ports);
5088 struct odputil_keybuf keybuf;
5089 struct action_xlate_ctx ctx;
5090 struct ofpbuf *odp_actions;
5093 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5094 odp_flow_key_from_flow(&key, flow);
5096 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5097 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5098 dpif_execute(ofproto->dpif, key.data, key.size,
5099 odp_actions->data, odp_actions->size, packet);
5100 ofpbuf_delete(odp_actions);
5106 get_netflow_ids(const struct ofproto *ofproto_,
5107 uint8_t *engine_type, uint8_t *engine_id)
5109 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5111 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5114 static struct ofproto_dpif *
5115 ofproto_dpif_lookup(const char *name)
5117 struct ofproto *ofproto = ofproto_lookup(name);
5118 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5119 ? ofproto_dpif_cast(ofproto)
5124 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5125 const char *args, void *aux OVS_UNUSED)
5127 const struct ofproto_dpif *ofproto;
5129 ofproto = ofproto_dpif_lookup(args);
5131 unixctl_command_reply(conn, 501, "no such bridge");
5134 mac_learning_flush(ofproto->ml);
5136 unixctl_command_reply(conn, 200, "table successfully flushed");
5140 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5141 const char *args, void *aux OVS_UNUSED)
5143 struct ds ds = DS_EMPTY_INITIALIZER;
5144 const struct ofproto_dpif *ofproto;
5145 const struct mac_entry *e;
5147 ofproto = ofproto_dpif_lookup(args);
5149 unixctl_command_reply(conn, 501, "no such bridge");
5153 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5154 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5155 struct ofbundle *bundle = e->port.p;
5156 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5157 ofbundle_get_a_port(bundle)->odp_port,
5158 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5160 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5164 struct ofproto_trace {
5165 struct action_xlate_ctx ctx;
5171 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5172 const struct rule_dpif *rule)
5174 ds_put_char_multiple(result, '\t', level);
5176 ds_put_cstr(result, "No match\n");
5180 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5181 table_id, ntohll(rule->up.flow_cookie));
5182 cls_rule_format(&rule->up.cr, result);
5183 ds_put_char(result, '\n');
5185 ds_put_char_multiple(result, '\t', level);
5186 ds_put_cstr(result, "OpenFlow ");
5187 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5188 ds_put_char(result, '\n');
5192 trace_format_flow(struct ds *result, int level, const char *title,
5193 struct ofproto_trace *trace)
5195 ds_put_char_multiple(result, '\t', level);
5196 ds_put_format(result, "%s: ", title);
5197 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5198 ds_put_cstr(result, "unchanged");
5200 flow_format(result, &trace->ctx.flow);
5201 trace->flow = trace->ctx.flow;
5203 ds_put_char(result, '\n');
5207 trace_format_regs(struct ds *result, int level, const char *title,
5208 struct ofproto_trace *trace)
5212 ds_put_char_multiple(result, '\t', level);
5213 ds_put_format(result, "%s:", title);
5214 for (i = 0; i < FLOW_N_REGS; i++) {
5215 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5217 ds_put_char(result, '\n');
5221 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5223 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5224 struct ds *result = trace->result;
5226 ds_put_char(result, '\n');
5227 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5228 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5229 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5233 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5234 void *aux OVS_UNUSED)
5236 char *dpname, *arg1, *arg2, *arg3, *arg4;
5237 char *args = xstrdup(args_);
5238 char *save_ptr = NULL;
5239 struct ofproto_dpif *ofproto;
5240 struct ofpbuf odp_key;
5241 struct ofpbuf *packet;
5242 struct rule_dpif *rule;
5248 ofpbuf_init(&odp_key, 0);
5251 dpname = strtok_r(args, " ", &save_ptr);
5252 arg1 = strtok_r(NULL, " ", &save_ptr);
5253 arg2 = strtok_r(NULL, " ", &save_ptr);
5254 arg3 = strtok_r(NULL, " ", &save_ptr);
5255 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5256 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5257 /* ofproto/trace dpname flow [-generate] */
5260 /* Convert string to datapath key. */
5261 ofpbuf_init(&odp_key, 0);
5262 error = odp_flow_key_from_string(arg1, &odp_key);
5264 unixctl_command_reply(conn, 501, "Bad flow syntax");
5268 /* Convert odp_key to flow. */
5269 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
5271 unixctl_command_reply(conn, 501, "Invalid flow");
5275 /* Generate a packet, if requested. */
5277 packet = ofpbuf_new(0);
5278 flow_compose(packet, &flow);
5280 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5281 /* ofproto/trace dpname priority tun_id in_port packet */
5286 priority = atoi(arg1);
5287 tun_id = htonll(strtoull(arg2, NULL, 0));
5288 in_port = ofp_port_to_odp_port(atoi(arg3));
5290 packet = ofpbuf_new(strlen(args) / 2);
5291 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5292 arg4 += strspn(arg4, " ");
5293 if (*arg4 != '\0') {
5294 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5297 if (packet->size < ETH_HEADER_LEN) {
5298 unixctl_command_reply(conn, 501,
5299 "Packet data too short for Ethernet");
5303 ds_put_cstr(&result, "Packet: ");
5304 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5305 ds_put_cstr(&result, s);
5308 flow_extract(packet, priority, tun_id, in_port, &flow);
5310 unixctl_command_reply(conn, 501, "Bad command syntax");
5314 ofproto = ofproto_dpif_lookup(dpname);
5316 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5321 ds_put_cstr(&result, "Flow: ");
5322 flow_format(&result, &flow);
5323 ds_put_char(&result, '\n');
5325 rule = rule_dpif_lookup(ofproto, &flow, 0);
5326 trace_format_rule(&result, 0, 0, rule);
5328 struct ofproto_trace trace;
5329 struct ofpbuf *odp_actions;
5331 trace.result = &result;
5333 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5334 trace.ctx.resubmit_hook = trace_resubmit;
5335 odp_actions = xlate_actions(&trace.ctx,
5336 rule->up.actions, rule->up.n_actions);
5338 ds_put_char(&result, '\n');
5339 trace_format_flow(&result, 0, "Final flow", &trace);
5340 ds_put_cstr(&result, "Datapath actions: ");
5341 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5342 ofpbuf_delete(odp_actions);
5344 if (!trace.ctx.may_set_up_flow) {
5346 ds_put_cstr(&result, "\nThis flow is not cachable.");
5348 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5349 "for complete actions, please supply a packet.");
5354 unixctl_command_reply(conn, 200, ds_cstr(&result));
5357 ds_destroy(&result);
5358 ofpbuf_delete(packet);
5359 ofpbuf_uninit(&odp_key);
5364 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5365 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5368 unixctl_command_reply(conn, 200, NULL);
5372 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5373 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5376 unixctl_command_reply(conn, 200, NULL);
5380 ofproto_dpif_unixctl_init(void)
5382 static bool registered;
5388 unixctl_command_register("ofproto/trace",
5389 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5390 ofproto_unixctl_trace, NULL);
5391 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5393 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5395 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5396 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5399 const struct ofproto_class ofproto_dpif_class = {
5426 port_is_lacp_current,
5427 NULL, /* rule_choose_table */
5434 rule_modify_actions,
5442 get_cfm_remote_mpids,
5446 get_stp_port_status,
5451 is_mirror_output_bundle,
5452 forward_bpdu_changed,