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 (exactly one of out == NULL and out_vlan == -1 is true). */
125 struct ofbundle *out; /* Output port or NULL. */
126 int out_vlan; /* Output VLAN or -1. */
127 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
130 static void mirror_destroy(struct ofmirror *);
132 /* A group of one or more OpenFlow ports. */
133 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
135 struct ofproto_dpif *ofproto; /* Owning ofproto. */
136 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
137 void *aux; /* Key supplied by ofproto's client. */
138 char *name; /* Identifier for log messages. */
141 struct list ports; /* Contains "struct ofport"s. */
142 enum port_vlan_mode vlan_mode; /* VLAN mode */
143 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
144 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
145 * NULL if all VLANs are trunked. */
146 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
147 struct bond *bond; /* Nonnull iff more than one port. */
150 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
152 /* Port mirroring info. */
153 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
154 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
155 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
158 static void bundle_remove(struct ofport *);
159 static void bundle_update(struct ofbundle *);
160 static void bundle_destroy(struct ofbundle *);
161 static void bundle_del_port(struct ofport_dpif *);
162 static void bundle_run(struct ofbundle *);
163 static void bundle_wait(struct ofbundle *);
165 static void stp_run(struct ofproto_dpif *ofproto);
166 static void stp_wait(struct ofproto_dpif *ofproto);
168 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
170 struct action_xlate_ctx {
171 /* action_xlate_ctx_init() initializes these members. */
174 struct ofproto_dpif *ofproto;
176 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
177 * this flow when actions change header fields. */
180 /* The packet corresponding to 'flow', or a null pointer if we are
181 * revalidating without a packet to refer to. */
182 const struct ofpbuf *packet;
184 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
185 * want to execute them if we are actually processing a packet, or if we
186 * are accounting for packets that the datapath has processed, but not if
187 * we are just revalidating. */
190 /* If nonnull, called just before executing a resubmit action.
192 * This is normally null so the client has to set it manually after
193 * calling action_xlate_ctx_init(). */
194 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
196 /* xlate_actions() initializes and uses these members. The client might want
197 * to look at them after it returns. */
199 struct ofpbuf *odp_actions; /* Datapath actions. */
200 tag_type tags; /* Tags associated with actions. */
201 bool may_set_up_flow; /* True ordinarily; false if the actions must
202 * be reassessed for every packet. */
203 bool has_learn; /* Actions include NXAST_LEARN? */
204 bool has_normal; /* Actions output to OFPP_NORMAL? */
205 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
207 /* xlate_actions() initializes and uses these members, but the client has no
208 * reason to look at them. */
210 int recurse; /* Recursion level, via xlate_table_action. */
211 struct flow base_flow; /* Flow at the last commit. */
212 uint32_t original_priority; /* Priority when packet arrived. */
213 uint8_t table_id; /* OpenFlow table ID where flow was found. */
214 uint32_t sflow_n_outputs; /* Number of output ports. */
215 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
216 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
217 bool exit; /* No further actions should be processed. */
220 static void action_xlate_ctx_init(struct action_xlate_ctx *,
221 struct ofproto_dpif *, const struct flow *,
222 const struct ofpbuf *);
223 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
224 const union ofp_action *in, size_t n_in);
226 /* An exact-match instantiation of an OpenFlow flow. */
228 long long int used; /* Time last used; time created if not used. */
232 * - Do include packets and bytes sent "by hand", e.g. with
235 * - Do include packets and bytes that were obtained from the datapath
236 * when its statistics were reset (e.g. dpif_flow_put() with
237 * DPIF_FP_ZERO_STATS).
239 uint64_t packet_count; /* Number of packets received. */
240 uint64_t byte_count; /* Number of bytes received. */
242 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
243 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
245 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
246 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
247 long long int rs_used; /* Used time pushed to resubmit children. */
249 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
251 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
252 struct list list_node; /* In owning rule's 'facets' list. */
253 struct rule_dpif *rule; /* Owning rule. */
254 struct flow flow; /* Exact-match flow. */
255 bool installed; /* Installed in datapath? */
256 bool may_install; /* True ordinarily; false if actions must
257 * be reassessed for every packet. */
258 bool has_learn; /* Actions include NXAST_LEARN? */
259 bool has_normal; /* Actions output to OFPP_NORMAL? */
260 size_t actions_len; /* Number of bytes in actions[]. */
261 struct nlattr *actions; /* Datapath actions. */
262 tag_type tags; /* Tags. */
263 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
266 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
267 static void facet_remove(struct ofproto_dpif *, struct facet *);
268 static void facet_free(struct facet *);
270 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
271 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
272 const struct flow *);
273 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
275 static bool execute_controller_action(struct ofproto_dpif *,
277 const struct nlattr *odp_actions,
279 struct ofpbuf *packet);
280 static void facet_execute(struct ofproto_dpif *, struct facet *,
281 struct ofpbuf *packet);
283 static int facet_put__(struct ofproto_dpif *, struct facet *,
284 const struct nlattr *actions, size_t actions_len,
285 struct dpif_flow_stats *);
286 static void facet_install(struct ofproto_dpif *, struct facet *,
288 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
289 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
291 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
292 const struct ofpbuf *packet);
293 static void facet_update_time(struct ofproto_dpif *, struct facet *,
295 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
296 const struct dpif_flow_stats *);
297 static void facet_reset_counters(struct facet *);
298 static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *);
299 static void facet_push_stats(struct facet *);
300 static void facet_account(struct ofproto_dpif *, struct facet *);
302 static bool facet_is_controller_flow(struct facet *);
304 static void flow_push_stats(const struct rule_dpif *,
305 struct flow *, uint64_t packets, uint64_t bytes,
308 static uint32_t rule_calculate_tag(const struct flow *,
309 const struct flow_wildcards *,
311 static void rule_invalidate(const struct rule_dpif *);
317 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
318 struct list bundle_node; /* In struct ofbundle's "ports" list. */
319 struct cfm *cfm; /* Connectivity Fault Management, if any. */
320 tag_type tag; /* Tag associated with this port. */
321 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
322 bool may_enable; /* May be enabled in bonds. */
324 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
325 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
326 long long int stp_state_entered;
329 static struct ofport_dpif *
330 ofport_dpif_cast(const struct ofport *ofport)
332 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
333 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
336 static void port_run(struct ofport_dpif *);
337 static void port_wait(struct ofport_dpif *);
338 static int set_cfm(struct ofport *, const struct cfm_settings *);
340 struct dpif_completion {
341 struct list list_node;
342 struct ofoperation *op;
345 /* Extra information about a classifier table.
346 * Currently used just for optimized flow revalidation. */
348 /* If either of these is nonnull, then this table has a form that allows
349 * flows to be tagged to avoid revalidating most flows for the most common
350 * kinds of flow table changes. */
351 struct cls_table *catchall_table; /* Table that wildcards all fields. */
352 struct cls_table *other_table; /* Table with any other wildcard set. */
353 uint32_t basis; /* Keeps each table's tags separate. */
356 struct ofproto_dpif {
365 struct netflow *netflow;
366 struct dpif_sflow *sflow;
367 struct hmap bundles; /* Contains "struct ofbundle"s. */
368 struct mac_learning *ml;
369 struct ofmirror *mirrors[MAX_MIRRORS];
370 bool has_bonded_bundles;
373 struct timer next_expiration;
379 struct table_dpif tables[N_TABLES];
380 bool need_revalidate;
381 struct tag_set revalidate_set;
383 /* Support for debugging async flow mods. */
384 struct list completions;
386 bool has_bundle_action; /* True when the first bundle action appears. */
390 long long int stp_last_tick;
393 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
394 * for debugging the asynchronous flow_mod implementation.) */
397 static void ofproto_dpif_unixctl_init(void);
399 static struct ofproto_dpif *
400 ofproto_dpif_cast(const struct ofproto *ofproto)
402 assert(ofproto->ofproto_class == &ofproto_dpif_class);
403 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
406 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
408 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
411 /* Packet processing. */
412 static void update_learning_table(struct ofproto_dpif *,
413 const struct flow *, int vlan,
415 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
416 bool have_packet, tag_type *, int *vlanp,
417 struct ofbundle **in_bundlep);
420 #define FLOW_MISS_MAX_BATCH 50
421 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
422 static void handle_miss_upcalls(struct ofproto_dpif *,
423 struct dpif_upcall *, size_t n);
425 /* Flow expiration. */
426 static int expire(struct ofproto_dpif *);
429 static int send_packet(struct ofproto_dpif *, uint32_t odp_port,
430 const struct ofpbuf *packet);
432 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
433 const struct flow *, uint32_t odp_port);
434 /* Global variables. */
435 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
437 /* Factory functions. */
440 enumerate_types(struct sset *types)
442 dp_enumerate_types(types);
446 enumerate_names(const char *type, struct sset *names)
448 return dp_enumerate_names(type, names);
452 del(const char *type, const char *name)
457 error = dpif_open(name, type, &dpif);
459 error = dpif_delete(dpif);
465 /* Basic life-cycle. */
467 static struct ofproto *
470 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
475 dealloc(struct ofproto *ofproto_)
477 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
482 construct(struct ofproto *ofproto_, int *n_tablesp)
484 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
485 const char *name = ofproto->up.name;
489 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
491 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
495 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
496 ofproto->n_matches = 0;
498 dpif_flow_flush(ofproto->dpif);
499 dpif_recv_purge(ofproto->dpif);
501 error = dpif_recv_set_mask(ofproto->dpif,
502 ((1u << DPIF_UC_MISS) |
503 (1u << DPIF_UC_ACTION)));
505 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
506 dpif_close(ofproto->dpif);
510 ofproto->netflow = NULL;
511 ofproto->sflow = NULL;
513 hmap_init(&ofproto->bundles);
514 ofproto->ml = mac_learning_create();
515 for (i = 0; i < MAX_MIRRORS; i++) {
516 ofproto->mirrors[i] = NULL;
518 ofproto->has_bonded_bundles = false;
520 timer_set_duration(&ofproto->next_expiration, 1000);
522 hmap_init(&ofproto->facets);
524 for (i = 0; i < N_TABLES; i++) {
525 struct table_dpif *table = &ofproto->tables[i];
527 table->catchall_table = NULL;
528 table->other_table = NULL;
529 table->basis = random_uint32();
531 ofproto->need_revalidate = false;
532 tag_set_init(&ofproto->revalidate_set);
534 list_init(&ofproto->completions);
536 ofproto_dpif_unixctl_init();
538 ofproto->has_bundle_action = false;
540 *n_tablesp = N_TABLES;
545 complete_operations(struct ofproto_dpif *ofproto)
547 struct dpif_completion *c, *next;
549 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
550 ofoperation_complete(c->op, 0);
551 list_remove(&c->list_node);
557 destruct(struct ofproto *ofproto_)
559 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
560 struct rule_dpif *rule, *next_rule;
561 struct classifier *table;
564 complete_operations(ofproto);
566 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
567 struct cls_cursor cursor;
569 cls_cursor_init(&cursor, table, NULL);
570 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
571 ofproto_rule_destroy(&rule->up);
575 for (i = 0; i < MAX_MIRRORS; i++) {
576 mirror_destroy(ofproto->mirrors[i]);
579 netflow_destroy(ofproto->netflow);
580 dpif_sflow_destroy(ofproto->sflow);
581 hmap_destroy(&ofproto->bundles);
582 mac_learning_destroy(ofproto->ml);
584 hmap_destroy(&ofproto->facets);
586 dpif_close(ofproto->dpif);
590 run(struct ofproto *ofproto_)
592 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
593 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
594 struct ofport_dpif *ofport;
595 struct ofbundle *bundle;
600 complete_operations(ofproto);
602 dpif_run(ofproto->dpif);
605 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
606 struct dpif_upcall *upcall = &misses[n_misses];
609 error = dpif_recv(ofproto->dpif, upcall);
611 if (error == ENODEV && n_misses == 0) {
617 if (upcall->type == DPIF_UC_MISS) {
618 /* Handle it later. */
621 handle_upcall(ofproto, upcall);
625 handle_miss_upcalls(ofproto, misses, n_misses);
627 if (timer_expired(&ofproto->next_expiration)) {
628 int delay = expire(ofproto);
629 timer_set_duration(&ofproto->next_expiration, delay);
632 if (ofproto->netflow) {
633 netflow_run(ofproto->netflow);
635 if (ofproto->sflow) {
636 dpif_sflow_run(ofproto->sflow);
639 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
642 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
647 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
649 /* Now revalidate if there's anything to do. */
650 if (ofproto->need_revalidate
651 || !tag_set_is_empty(&ofproto->revalidate_set)) {
652 struct tag_set revalidate_set = ofproto->revalidate_set;
653 bool revalidate_all = ofproto->need_revalidate;
654 struct facet *facet, *next;
656 /* Clear the revalidation flags. */
657 tag_set_init(&ofproto->revalidate_set);
658 ofproto->need_revalidate = false;
660 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
662 || tag_set_intersects(&revalidate_set, facet->tags)) {
663 facet_revalidate(ofproto, facet);
672 wait(struct ofproto *ofproto_)
674 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
675 struct ofport_dpif *ofport;
676 struct ofbundle *bundle;
678 if (!clogged && !list_is_empty(&ofproto->completions)) {
679 poll_immediate_wake();
682 dpif_wait(ofproto->dpif);
683 dpif_recv_wait(ofproto->dpif);
684 if (ofproto->sflow) {
685 dpif_sflow_wait(ofproto->sflow);
687 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
688 poll_immediate_wake();
690 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
693 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
696 mac_learning_wait(ofproto->ml);
698 if (ofproto->need_revalidate) {
699 /* Shouldn't happen, but if it does just go around again. */
700 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
701 poll_immediate_wake();
703 timer_wait(&ofproto->next_expiration);
708 flush(struct ofproto *ofproto_)
710 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
711 struct facet *facet, *next_facet;
713 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
714 /* Mark the facet as not installed so that facet_remove() doesn't
715 * bother trying to uninstall it. There is no point in uninstalling it
716 * individually since we are about to blow away all the facets with
717 * dpif_flow_flush(). */
718 facet->installed = false;
719 facet->dp_packet_count = 0;
720 facet->dp_byte_count = 0;
721 facet_remove(ofproto, facet);
723 dpif_flow_flush(ofproto->dpif);
727 get_features(struct ofproto *ofproto_ OVS_UNUSED,
728 bool *arp_match_ip, uint32_t *actions)
730 *arp_match_ip = true;
731 *actions = ((1u << OFPAT_OUTPUT) |
732 (1u << OFPAT_SET_VLAN_VID) |
733 (1u << OFPAT_SET_VLAN_PCP) |
734 (1u << OFPAT_STRIP_VLAN) |
735 (1u << OFPAT_SET_DL_SRC) |
736 (1u << OFPAT_SET_DL_DST) |
737 (1u << OFPAT_SET_NW_SRC) |
738 (1u << OFPAT_SET_NW_DST) |
739 (1u << OFPAT_SET_NW_TOS) |
740 (1u << OFPAT_SET_TP_SRC) |
741 (1u << OFPAT_SET_TP_DST) |
742 (1u << OFPAT_ENQUEUE));
746 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
748 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
749 struct dpif_dp_stats s;
751 strcpy(ots->name, "classifier");
753 dpif_get_dp_stats(ofproto->dpif, &s);
754 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
755 put_32aligned_be64(&ots->matched_count,
756 htonll(s.n_hit + ofproto->n_matches));
760 set_netflow(struct ofproto *ofproto_,
761 const struct netflow_options *netflow_options)
763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
765 if (netflow_options) {
766 if (!ofproto->netflow) {
767 ofproto->netflow = netflow_create();
769 return netflow_set_options(ofproto->netflow, netflow_options);
771 netflow_destroy(ofproto->netflow);
772 ofproto->netflow = NULL;
777 static struct ofport *
780 struct ofport_dpif *port = xmalloc(sizeof *port);
785 port_dealloc(struct ofport *port_)
787 struct ofport_dpif *port = ofport_dpif_cast(port_);
792 port_construct(struct ofport *port_)
794 struct ofport_dpif *port = ofport_dpif_cast(port_);
795 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
797 ofproto->need_revalidate = true;
798 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
801 port->tag = tag_create_random();
802 port->may_enable = true;
803 port->stp_port = NULL;
804 port->stp_state = STP_DISABLED;
806 if (ofproto->sflow) {
807 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
808 netdev_get_name(port->up.netdev));
815 port_destruct(struct ofport *port_)
817 struct ofport_dpif *port = ofport_dpif_cast(port_);
818 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
820 ofproto->need_revalidate = true;
821 bundle_remove(port_);
822 set_cfm(port_, NULL);
823 if (ofproto->sflow) {
824 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
829 port_modified(struct ofport *port_)
831 struct ofport_dpif *port = ofport_dpif_cast(port_);
833 if (port->bundle && port->bundle->bond) {
834 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
839 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
841 struct ofport_dpif *port = ofport_dpif_cast(port_);
842 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
843 ovs_be32 changed = old_config ^ port->up.opp.config;
845 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
846 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
847 ofproto->need_revalidate = true;
849 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
850 bundle_update(port->bundle);
856 set_sflow(struct ofproto *ofproto_,
857 const struct ofproto_sflow_options *sflow_options)
859 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
860 struct dpif_sflow *ds = ofproto->sflow;
864 struct ofport_dpif *ofport;
866 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
867 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
868 dpif_sflow_add_port(ds, ofport->odp_port,
869 netdev_get_name(ofport->up.netdev));
871 ofproto->need_revalidate = true;
873 dpif_sflow_set_options(ds, sflow_options);
876 dpif_sflow_destroy(ds);
877 ofproto->need_revalidate = true;
878 ofproto->sflow = NULL;
885 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
887 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
894 struct ofproto_dpif *ofproto;
896 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
897 ofproto->need_revalidate = true;
898 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
901 if (cfm_configure(ofport->cfm, s)) {
907 cfm_destroy(ofport->cfm);
913 get_cfm_fault(const struct ofport *ofport_)
915 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
917 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
921 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
924 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
927 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
937 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
939 struct ofproto_dpif *ofproto = ofproto_;
940 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
941 struct ofport_dpif *ofport;
943 ofport = stp_port_get_aux(sp);
945 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
946 ofproto->up.name, port_num);
948 struct eth_header *eth = pkt->l2;
950 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
951 if (eth_addr_is_zero(eth->eth_src)) {
952 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
953 "with unknown MAC", ofproto->up.name, port_num);
955 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
956 ofport->odp_port, pkt);
962 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
964 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
966 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
968 /* Only revalidate flows if the configuration changed. */
969 if (!s != !ofproto->stp) {
970 ofproto->need_revalidate = true;
975 ofproto->stp = stp_create(ofproto_->name, s->system_id,
976 send_bpdu_cb, ofproto);
977 ofproto->stp_last_tick = time_msec();
980 stp_set_bridge_id(ofproto->stp, s->system_id);
981 stp_set_bridge_priority(ofproto->stp, s->priority);
982 stp_set_hello_time(ofproto->stp, s->hello_time);
983 stp_set_max_age(ofproto->stp, s->max_age);
984 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
986 stp_destroy(ofproto->stp);
994 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1000 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1001 s->designated_root = stp_get_designated_root(ofproto->stp);
1002 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1011 update_stp_port_state(struct ofport_dpif *ofport)
1013 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1014 enum stp_state state;
1016 /* Figure out new state. */
1017 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1021 if (ofport->stp_state != state) {
1025 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1026 netdev_get_name(ofport->up.netdev),
1027 stp_state_name(ofport->stp_state),
1028 stp_state_name(state));
1029 if (stp_learn_in_state(ofport->stp_state)
1030 != stp_learn_in_state(state)) {
1031 /* xxx Learning action flows should also be flushed. */
1032 mac_learning_flush(ofproto->ml);
1034 fwd_change = stp_forward_in_state(ofport->stp_state)
1035 != stp_forward_in_state(state);
1037 ofproto->need_revalidate = true;
1038 ofport->stp_state = state;
1039 ofport->stp_state_entered = time_msec();
1041 if (fwd_change && ofport->bundle) {
1042 bundle_update(ofport->bundle);
1045 /* Update the STP state bits in the OpenFlow port description. */
1046 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1047 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1048 : state == STP_LEARNING ? OFPPS_STP_LEARN
1049 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1050 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1052 ofproto_port_set_state(&ofport->up, of_state);
1056 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1057 * caller is responsible for assigning STP port numbers and ensuring
1058 * there are no duplicates. */
1060 set_stp_port(struct ofport *ofport_,
1061 const struct ofproto_port_stp_settings *s)
1063 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1064 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1065 struct stp_port *sp = ofport->stp_port;
1067 if (!s || !s->enable) {
1069 ofport->stp_port = NULL;
1070 stp_port_disable(sp);
1071 update_stp_port_state(ofport);
1074 } else if (sp && stp_port_no(sp) != s->port_num
1075 && ofport == stp_port_get_aux(sp)) {
1076 /* The port-id changed, so disable the old one if it's not
1077 * already in use by another port. */
1078 stp_port_disable(sp);
1081 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1082 stp_port_enable(sp);
1084 stp_port_set_aux(sp, ofport);
1085 stp_port_set_priority(sp, s->priority);
1086 stp_port_set_path_cost(sp, s->path_cost);
1088 update_stp_port_state(ofport);
1094 get_stp_port_status(struct ofport *ofport_,
1095 struct ofproto_port_stp_status *s)
1097 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1098 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1099 struct stp_port *sp = ofport->stp_port;
1101 if (!ofproto->stp || !sp) {
1107 s->port_id = stp_port_get_id(sp);
1108 s->state = stp_port_get_state(sp);
1109 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1110 s->role = stp_port_get_role(sp);
1111 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1117 stp_run(struct ofproto_dpif *ofproto)
1120 long long int now = time_msec();
1121 long long int elapsed = now - ofproto->stp_last_tick;
1122 struct stp_port *sp;
1125 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1126 ofproto->stp_last_tick = now;
1128 while (stp_get_changed_port(ofproto->stp, &sp)) {
1129 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1132 update_stp_port_state(ofport);
1139 stp_wait(struct ofproto_dpif *ofproto)
1142 poll_timer_wait(1000);
1146 /* Returns true if STP should process 'flow'. */
1148 stp_should_process_flow(const struct flow *flow)
1150 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1154 stp_process_packet(const struct ofport_dpif *ofport,
1155 const struct ofpbuf *packet)
1157 struct ofpbuf payload = *packet;
1158 struct eth_header *eth = payload.data;
1159 struct stp_port *sp = ofport->stp_port;
1161 /* Sink packets on ports that have STP disabled when the bridge has
1163 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1167 /* Trim off padding on payload. */
1168 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1169 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1172 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1173 stp_received_bpdu(sp, payload.data, payload.size);
1179 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1180 * to revalidate every flow. */
1182 bundle_flush_macs(struct ofbundle *bundle)
1184 struct ofproto_dpif *ofproto = bundle->ofproto;
1185 struct mac_learning *ml = ofproto->ml;
1186 struct mac_entry *mac, *next_mac;
1188 ofproto->need_revalidate = true;
1189 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1190 if (mac->port.p == bundle) {
1191 mac_learning_expire(ml, mac);
1196 static struct ofbundle *
1197 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1199 struct ofbundle *bundle;
1201 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1202 &ofproto->bundles) {
1203 if (bundle->aux == aux) {
1210 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1211 * ones that are found to 'bundles'. */
1213 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1214 void **auxes, size_t n_auxes,
1215 struct hmapx *bundles)
1219 hmapx_init(bundles);
1220 for (i = 0; i < n_auxes; i++) {
1221 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1223 hmapx_add(bundles, bundle);
1229 bundle_update(struct ofbundle *bundle)
1231 struct ofport_dpif *port;
1233 bundle->floodable = true;
1234 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1235 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1236 || !stp_forward_in_state(port->stp_state)) {
1237 bundle->floodable = false;
1244 bundle_del_port(struct ofport_dpif *port)
1246 struct ofbundle *bundle = port->bundle;
1248 bundle->ofproto->need_revalidate = true;
1250 list_remove(&port->bundle_node);
1251 port->bundle = NULL;
1254 lacp_slave_unregister(bundle->lacp, port);
1257 bond_slave_unregister(bundle->bond, port);
1260 bundle_update(bundle);
1264 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1265 struct lacp_slave_settings *lacp,
1266 uint32_t bond_stable_id)
1268 struct ofport_dpif *port;
1270 port = get_ofp_port(bundle->ofproto, ofp_port);
1275 if (port->bundle != bundle) {
1276 bundle->ofproto->need_revalidate = true;
1278 bundle_del_port(port);
1281 port->bundle = bundle;
1282 list_push_back(&bundle->ports, &port->bundle_node);
1283 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)
1284 || !stp_forward_in_state(port->stp_state)) {
1285 bundle->floodable = false;
1289 port->bundle->ofproto->need_revalidate = true;
1290 lacp_slave_register(bundle->lacp, port, lacp);
1293 port->bond_stable_id = bond_stable_id;
1299 bundle_destroy(struct ofbundle *bundle)
1301 struct ofproto_dpif *ofproto;
1302 struct ofport_dpif *port, *next_port;
1309 ofproto = bundle->ofproto;
1310 for (i = 0; i < MAX_MIRRORS; i++) {
1311 struct ofmirror *m = ofproto->mirrors[i];
1313 if (m->out == bundle) {
1315 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1316 || hmapx_find_and_delete(&m->dsts, bundle)) {
1317 ofproto->need_revalidate = true;
1322 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1323 bundle_del_port(port);
1326 bundle_flush_macs(bundle);
1327 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1329 free(bundle->trunks);
1330 lacp_destroy(bundle->lacp);
1331 bond_destroy(bundle->bond);
1336 bundle_set(struct ofproto *ofproto_, void *aux,
1337 const struct ofproto_bundle_settings *s)
1339 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1340 bool need_flush = false;
1341 struct ofport_dpif *port;
1342 struct ofbundle *bundle;
1343 unsigned long *trunks;
1349 bundle_destroy(bundle_lookup(ofproto, aux));
1353 assert(s->n_slaves == 1 || s->bond != NULL);
1354 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1356 bundle = bundle_lookup(ofproto, aux);
1358 bundle = xmalloc(sizeof *bundle);
1360 bundle->ofproto = ofproto;
1361 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1362 hash_pointer(aux, 0));
1364 bundle->name = NULL;
1366 list_init(&bundle->ports);
1367 bundle->vlan_mode = PORT_VLAN_TRUNK;
1369 bundle->trunks = NULL;
1370 bundle->lacp = NULL;
1371 bundle->bond = NULL;
1373 bundle->floodable = true;
1375 bundle->src_mirrors = 0;
1376 bundle->dst_mirrors = 0;
1377 bundle->mirror_out = 0;
1380 if (!bundle->name || strcmp(s->name, bundle->name)) {
1382 bundle->name = xstrdup(s->name);
1387 if (!bundle->lacp) {
1388 ofproto->need_revalidate = true;
1389 bundle->lacp = lacp_create();
1391 lacp_configure(bundle->lacp, s->lacp);
1393 lacp_destroy(bundle->lacp);
1394 bundle->lacp = NULL;
1397 /* Update set of ports. */
1399 for (i = 0; i < s->n_slaves; i++) {
1400 if (!bundle_add_port(bundle, s->slaves[i],
1401 s->lacp ? &s->lacp_slaves[i] : NULL,
1402 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1406 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1407 struct ofport_dpif *next_port;
1409 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1410 for (i = 0; i < s->n_slaves; i++) {
1411 if (s->slaves[i] == port->up.ofp_port) {
1416 bundle_del_port(port);
1420 assert(list_size(&bundle->ports) <= s->n_slaves);
1422 if (list_is_empty(&bundle->ports)) {
1423 bundle_destroy(bundle);
1427 /* Set VLAN tagging mode */
1428 if (s->vlan_mode != bundle->vlan_mode) {
1429 bundle->vlan_mode = s->vlan_mode;
1434 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1435 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1437 if (vlan != bundle->vlan) {
1438 bundle->vlan = vlan;
1442 /* Get trunked VLANs. */
1443 switch (s->vlan_mode) {
1444 case PORT_VLAN_ACCESS:
1448 case PORT_VLAN_TRUNK:
1449 trunks = (unsigned long *) s->trunks;
1452 case PORT_VLAN_NATIVE_UNTAGGED:
1453 case PORT_VLAN_NATIVE_TAGGED:
1454 if (vlan != 0 && (!s->trunks
1455 || !bitmap_is_set(s->trunks, vlan)
1456 || bitmap_is_set(s->trunks, 0))) {
1457 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1459 trunks = bitmap_clone(s->trunks, 4096);
1461 trunks = bitmap_allocate1(4096);
1463 bitmap_set1(trunks, vlan);
1464 bitmap_set0(trunks, 0);
1466 trunks = (unsigned long *) s->trunks;
1473 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1474 free(bundle->trunks);
1475 if (trunks == s->trunks) {
1476 bundle->trunks = vlan_bitmap_clone(trunks);
1478 bundle->trunks = trunks;
1483 if (trunks != s->trunks) {
1488 if (!list_is_short(&bundle->ports)) {
1489 bundle->ofproto->has_bonded_bundles = true;
1491 if (bond_reconfigure(bundle->bond, s->bond)) {
1492 ofproto->need_revalidate = true;
1495 bundle->bond = bond_create(s->bond);
1496 ofproto->need_revalidate = true;
1499 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1500 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1504 bond_destroy(bundle->bond);
1505 bundle->bond = NULL;
1508 /* If we changed something that would affect MAC learning, un-learn
1509 * everything on this port and force flow revalidation. */
1511 bundle_flush_macs(bundle);
1518 bundle_remove(struct ofport *port_)
1520 struct ofport_dpif *port = ofport_dpif_cast(port_);
1521 struct ofbundle *bundle = port->bundle;
1524 bundle_del_port(port);
1525 if (list_is_empty(&bundle->ports)) {
1526 bundle_destroy(bundle);
1527 } else if (list_is_short(&bundle->ports)) {
1528 bond_destroy(bundle->bond);
1529 bundle->bond = NULL;
1535 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1537 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1538 struct ofport_dpif *port = port_;
1539 uint8_t ea[ETH_ADDR_LEN];
1542 error = netdev_get_etheraddr(port->up.netdev, ea);
1544 struct ofpbuf packet;
1547 ofpbuf_init(&packet, 0);
1548 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1550 memcpy(packet_pdu, pdu, pdu_size);
1552 send_packet(ofproto_dpif_cast(port->up.ofproto), port->odp_port,
1554 ofpbuf_uninit(&packet);
1556 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1557 "%s (%s)", port->bundle->name,
1558 netdev_get_name(port->up.netdev), strerror(error));
1563 bundle_send_learning_packets(struct ofbundle *bundle)
1565 struct ofproto_dpif *ofproto = bundle->ofproto;
1566 int error, n_packets, n_errors;
1567 struct mac_entry *e;
1569 error = n_packets = n_errors = 0;
1570 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1571 if (e->port.p != bundle) {
1572 struct ofpbuf *learning_packet;
1573 struct ofport_dpif *port;
1576 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1579 ret = send_packet(ofproto_dpif_cast(port->up.ofproto),
1580 port->odp_port, learning_packet);
1581 ofpbuf_delete(learning_packet);
1591 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1592 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1593 "packets, last error was: %s",
1594 bundle->name, n_errors, n_packets, strerror(error));
1596 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1597 bundle->name, n_packets);
1602 bundle_run(struct ofbundle *bundle)
1605 lacp_run(bundle->lacp, send_pdu_cb);
1608 struct ofport_dpif *port;
1610 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1611 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1614 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1615 lacp_negotiated(bundle->lacp));
1616 if (bond_should_send_learning_packets(bundle->bond)) {
1617 bundle_send_learning_packets(bundle);
1623 bundle_wait(struct ofbundle *bundle)
1626 lacp_wait(bundle->lacp);
1629 bond_wait(bundle->bond);
1636 mirror_scan(struct ofproto_dpif *ofproto)
1640 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1641 if (!ofproto->mirrors[idx]) {
1648 static struct ofmirror *
1649 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1653 for (i = 0; i < MAX_MIRRORS; i++) {
1654 struct ofmirror *mirror = ofproto->mirrors[i];
1655 if (mirror && mirror->aux == aux) {
1663 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1665 mirror_update_dups(struct ofproto_dpif *ofproto)
1669 for (i = 0; i < MAX_MIRRORS; i++) {
1670 struct ofmirror *m = ofproto->mirrors[i];
1673 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1677 for (i = 0; i < MAX_MIRRORS; i++) {
1678 struct ofmirror *m1 = ofproto->mirrors[i];
1685 for (j = i + 1; j < MAX_MIRRORS; j++) {
1686 struct ofmirror *m2 = ofproto->mirrors[j];
1688 if (m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1689 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1690 m2->dup_mirrors |= m1->dup_mirrors;
1697 mirror_set(struct ofproto *ofproto_, void *aux,
1698 const struct ofproto_mirror_settings *s)
1700 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1701 mirror_mask_t mirror_bit;
1702 struct ofbundle *bundle;
1703 struct ofmirror *mirror;
1704 struct ofbundle *out;
1705 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1706 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1709 mirror = mirror_lookup(ofproto, aux);
1711 mirror_destroy(mirror);
1717 idx = mirror_scan(ofproto);
1719 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1721 ofproto->up.name, MAX_MIRRORS, s->name);
1725 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1726 mirror->ofproto = ofproto;
1729 mirror->out_vlan = -1;
1730 mirror->name = NULL;
1733 if (!mirror->name || strcmp(s->name, mirror->name)) {
1735 mirror->name = xstrdup(s->name);
1738 /* Get the new configuration. */
1739 if (s->out_bundle) {
1740 out = bundle_lookup(ofproto, s->out_bundle);
1742 mirror_destroy(mirror);
1748 out_vlan = s->out_vlan;
1750 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1751 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1753 /* If the configuration has not changed, do nothing. */
1754 if (hmapx_equals(&srcs, &mirror->srcs)
1755 && hmapx_equals(&dsts, &mirror->dsts)
1756 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1757 && mirror->out == out
1758 && mirror->out_vlan == out_vlan)
1760 hmapx_destroy(&srcs);
1761 hmapx_destroy(&dsts);
1765 hmapx_swap(&srcs, &mirror->srcs);
1766 hmapx_destroy(&srcs);
1768 hmapx_swap(&dsts, &mirror->dsts);
1769 hmapx_destroy(&dsts);
1771 free(mirror->vlans);
1772 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1775 mirror->out_vlan = out_vlan;
1777 /* Update bundles. */
1778 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1779 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1780 if (hmapx_contains(&mirror->srcs, bundle)) {
1781 bundle->src_mirrors |= mirror_bit;
1783 bundle->src_mirrors &= ~mirror_bit;
1786 if (hmapx_contains(&mirror->dsts, bundle)) {
1787 bundle->dst_mirrors |= mirror_bit;
1789 bundle->dst_mirrors &= ~mirror_bit;
1792 if (mirror->out == bundle) {
1793 bundle->mirror_out |= mirror_bit;
1795 bundle->mirror_out &= ~mirror_bit;
1799 ofproto->need_revalidate = true;
1800 mac_learning_flush(ofproto->ml);
1801 mirror_update_dups(ofproto);
1807 mirror_destroy(struct ofmirror *mirror)
1809 struct ofproto_dpif *ofproto;
1810 mirror_mask_t mirror_bit;
1811 struct ofbundle *bundle;
1817 ofproto = mirror->ofproto;
1818 ofproto->need_revalidate = true;
1819 mac_learning_flush(ofproto->ml);
1821 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1822 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1823 bundle->src_mirrors &= ~mirror_bit;
1824 bundle->dst_mirrors &= ~mirror_bit;
1825 bundle->mirror_out &= ~mirror_bit;
1828 hmapx_destroy(&mirror->srcs);
1829 hmapx_destroy(&mirror->dsts);
1830 free(mirror->vlans);
1832 ofproto->mirrors[mirror->idx] = NULL;
1836 mirror_update_dups(ofproto);
1840 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1842 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1843 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1844 ofproto->need_revalidate = true;
1845 mac_learning_flush(ofproto->ml);
1851 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
1853 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1854 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1855 return bundle && bundle->mirror_out != 0;
1859 forward_bpdu_changed(struct ofproto *ofproto_)
1861 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1862 /* Revalidate cached flows whenever forward_bpdu option changes. */
1863 ofproto->need_revalidate = true;
1868 static struct ofport_dpif *
1869 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1871 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
1872 return ofport ? ofport_dpif_cast(ofport) : NULL;
1875 static struct ofport_dpif *
1876 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1878 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1882 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1883 struct dpif_port *dpif_port)
1885 ofproto_port->name = dpif_port->name;
1886 ofproto_port->type = dpif_port->type;
1887 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1891 port_run(struct ofport_dpif *ofport)
1893 bool enable = netdev_get_carrier(ofport->up.netdev);
1896 cfm_run(ofport->cfm);
1898 if (cfm_should_send_ccm(ofport->cfm)) {
1899 struct ofpbuf packet;
1901 ofpbuf_init(&packet, 0);
1902 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
1903 send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1904 ofport->odp_port, &packet);
1905 ofpbuf_uninit(&packet);
1908 enable = enable && !cfm_get_fault(ofport->cfm)
1909 && cfm_get_opup(ofport->cfm);
1912 if (ofport->bundle) {
1913 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
1916 if (ofport->may_enable != enable) {
1917 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1919 if (ofproto->has_bundle_action) {
1920 ofproto->need_revalidate = true;
1924 ofport->may_enable = enable;
1928 port_wait(struct ofport_dpif *ofport)
1931 cfm_wait(ofport->cfm);
1936 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1937 struct ofproto_port *ofproto_port)
1939 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1940 struct dpif_port dpif_port;
1943 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1945 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1951 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1953 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1957 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1959 *ofp_portp = odp_port_to_ofp_port(odp_port);
1965 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1967 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1970 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1972 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1974 /* The caller is going to close ofport->up.netdev. If this is a
1975 * bonded port, then the bond is using that netdev, so remove it
1976 * from the bond. The client will need to reconfigure everything
1977 * after deleting ports, so then the slave will get re-added. */
1978 bundle_remove(&ofport->up);
1984 struct port_dump_state {
1985 struct dpif_port_dump dump;
1990 port_dump_start(const struct ofproto *ofproto_, void **statep)
1992 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1993 struct port_dump_state *state;
1995 *statep = state = xmalloc(sizeof *state);
1996 dpif_port_dump_start(&state->dump, ofproto->dpif);
1997 state->done = false;
2002 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2003 struct ofproto_port *port)
2005 struct port_dump_state *state = state_;
2006 struct dpif_port dpif_port;
2008 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2009 ofproto_port_from_dpif_port(port, &dpif_port);
2012 int error = dpif_port_dump_done(&state->dump);
2014 return error ? error : EOF;
2019 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2021 struct port_dump_state *state = state_;
2024 dpif_port_dump_done(&state->dump);
2031 port_poll(const struct ofproto *ofproto_, char **devnamep)
2033 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2034 return dpif_port_poll(ofproto->dpif, devnamep);
2038 port_poll_wait(const struct ofproto *ofproto_)
2040 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2041 dpif_port_poll_wait(ofproto->dpif);
2045 port_is_lacp_current(const struct ofport *ofport_)
2047 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2048 return (ofport->bundle && ofport->bundle->lacp
2049 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2053 /* Upcall handling. */
2055 /* Flow miss batching.
2057 * Some dpifs implement operations faster when you hand them off in a batch.
2058 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2059 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2060 * more packets, plus possibly installing the flow in the dpif.
2062 * So far we only batch the operations that affect flow setup time the most.
2063 * It's possible to batch more than that, but the benefit might be minimal. */
2065 struct hmap_node hmap_node;
2067 const struct nlattr *key;
2069 struct list packets;
2072 struct flow_miss_op {
2073 union dpif_op dpif_op;
2074 struct facet *facet;
2077 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2078 * OpenFlow controller as necessary according to their individual
2081 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2082 * ownership is transferred to this function. */
2084 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2085 const struct flow *flow, bool clone)
2087 struct ofputil_packet_in pin;
2089 pin.packet = packet;
2090 pin.in_port = flow->in_port;
2091 pin.reason = OFPR_NO_MATCH;
2092 pin.buffer_id = 0; /* not yet known */
2093 pin.send_len = 0; /* not used for flow table misses */
2094 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2095 clone ? NULL : packet);
2098 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2099 * OpenFlow controller as necessary according to their individual
2102 * 'send_len' should be the number of bytes of 'packet' to send to the
2103 * controller, as specified in the action that caused the packet to be sent.
2105 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2106 * Otherwise, ownership is transferred to this function. */
2108 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2109 uint64_t userdata, const struct flow *flow, bool clone)
2111 struct ofputil_packet_in pin;
2112 struct user_action_cookie cookie;
2114 memcpy(&cookie, &userdata, sizeof(cookie));
2116 pin.packet = packet;
2117 pin.in_port = flow->in_port;
2118 pin.reason = OFPR_ACTION;
2119 pin.buffer_id = 0; /* not yet known */
2120 pin.send_len = cookie.data;
2121 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2122 clone ? NULL : packet);
2126 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2127 const struct ofpbuf *packet)
2129 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2135 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2137 cfm_process_heartbeat(ofport->cfm, packet);
2140 } else if (ofport->bundle && ofport->bundle->lacp
2141 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2143 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2146 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2148 stp_process_packet(ofport, packet);
2155 static struct flow_miss *
2156 flow_miss_create(struct hmap *todo, const struct flow *flow,
2157 const struct nlattr *key, size_t key_len)
2159 uint32_t hash = flow_hash(flow, 0);
2160 struct flow_miss *miss;
2162 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2163 if (flow_equal(&miss->flow, flow)) {
2168 miss = xmalloc(sizeof *miss);
2169 hmap_insert(todo, &miss->hmap_node, hash);
2172 miss->key_len = key_len;
2173 list_init(&miss->packets);
2178 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2179 struct flow_miss_op *ops, size_t *n_ops)
2181 const struct flow *flow = &miss->flow;
2182 struct ofpbuf *packet, *next_packet;
2183 struct facet *facet;
2185 facet = facet_lookup_valid(ofproto, flow);
2187 struct rule_dpif *rule;
2189 rule = rule_dpif_lookup(ofproto, flow, 0);
2191 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2192 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2194 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2195 COVERAGE_INC(ofproto_dpif_no_packet_in);
2196 /* XXX install 'drop' flow entry */
2200 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2204 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2206 list_remove(&packet->list_node);
2207 send_packet_in_miss(ofproto, packet, flow, false);
2213 facet = facet_create(rule, flow);
2216 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2217 list_remove(&packet->list_node);
2218 ofproto->n_matches++;
2220 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2222 * Extra-special case for fail-open mode.
2224 * We are in fail-open mode and the packet matched the fail-open
2225 * rule, but we are connected to a controller too. We should send
2226 * the packet up to the controller in the hope that it will try to
2227 * set up a flow and thereby allow us to exit fail-open.
2229 * See the top-level comment in fail-open.c for more information.
2231 send_packet_in_miss(ofproto, packet, flow, true);
2234 if (!facet->may_install) {
2235 facet_make_actions(ofproto, facet, packet);
2237 if (!execute_controller_action(ofproto, &facet->flow,
2238 facet->actions, facet->actions_len,
2240 struct flow_miss_op *op = &ops[(*n_ops)++];
2241 struct dpif_execute *execute = &op->dpif_op.execute;
2244 execute->type = DPIF_OP_EXECUTE;
2245 execute->key = miss->key;
2246 execute->key_len = miss->key_len;
2248 = (facet->may_install
2250 : xmemdup(facet->actions, facet->actions_len));
2251 execute->actions_len = facet->actions_len;
2252 execute->packet = packet;
2256 if (facet->may_install) {
2257 struct flow_miss_op *op = &ops[(*n_ops)++];
2258 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2261 put->type = DPIF_OP_FLOW_PUT;
2262 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2263 put->key = miss->key;
2264 put->key_len = miss->key_len;
2265 put->actions = facet->actions;
2266 put->actions_len = facet->actions_len;
2272 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2275 struct dpif_upcall *upcall;
2276 struct flow_miss *miss, *next_miss;
2277 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2278 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2287 /* Construct the to-do list.
2289 * This just amounts to extracting the flow from each packet and sticking
2290 * the packets that have the same flow in the same "flow_miss" structure so
2291 * that we can process them together. */
2293 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2294 struct flow_miss *miss;
2297 /* Obtain in_port and tun_id, at least, then set 'flow''s header
2299 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2300 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2301 flow.in_port, &flow);
2303 /* Handle 802.1ag, LACP, and STP specially. */
2304 if (process_special(ofproto, &flow, upcall->packet)) {
2305 ofpbuf_delete(upcall->packet);
2306 ofproto->n_matches++;
2310 /* Add other packets to a to-do list. */
2311 miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len);
2312 list_push_back(&miss->packets, &upcall->packet->list_node);
2315 /* Process each element in the to-do list, constructing the set of
2316 * operations to batch. */
2318 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2319 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2320 ofpbuf_list_delete(&miss->packets);
2321 hmap_remove(&todo, &miss->hmap_node);
2324 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2325 hmap_destroy(&todo);
2327 /* Execute batch. */
2328 for (i = 0; i < n_ops; i++) {
2329 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2331 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2333 /* Free memory and update facets. */
2334 for (i = 0; i < n_ops; i++) {
2335 struct flow_miss_op *op = &flow_miss_ops[i];
2336 struct dpif_execute *execute;
2337 struct dpif_flow_put *put;
2339 switch (op->dpif_op.type) {
2340 case DPIF_OP_EXECUTE:
2341 execute = &op->dpif_op.execute;
2342 if (op->facet->actions != execute->actions) {
2343 free((struct nlattr *) execute->actions);
2345 ofpbuf_delete((struct ofpbuf *) execute->packet);
2348 case DPIF_OP_FLOW_PUT:
2349 put = &op->dpif_op.flow_put;
2351 op->facet->installed = true;
2359 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2360 struct dpif_upcall *upcall)
2363 struct user_action_cookie cookie;
2365 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2367 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2368 if (ofproto->sflow) {
2369 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2370 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie);
2372 ofpbuf_delete(upcall->packet);
2374 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2375 COVERAGE_INC(ofproto_dpif_ctlr_action);
2376 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
2377 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2380 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2385 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
2387 switch (upcall->type) {
2388 case DPIF_UC_ACTION:
2389 handle_userspace_upcall(ofproto, upcall);
2393 /* The caller handles these. */
2396 case DPIF_N_UC_TYPES:
2398 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
2403 /* Flow expiration. */
2405 static int facet_max_idle(const struct ofproto_dpif *);
2406 static void update_stats(struct ofproto_dpif *);
2407 static void rule_expire(struct rule_dpif *);
2408 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
2410 /* This function is called periodically by run(). Its job is to collect
2411 * updates for the flows that have been installed into the datapath, most
2412 * importantly when they last were used, and then use that information to
2413 * expire flows that have not been used recently.
2415 * Returns the number of milliseconds after which it should be called again. */
2417 expire(struct ofproto_dpif *ofproto)
2419 struct rule_dpif *rule, *next_rule;
2420 struct classifier *table;
2423 /* Update stats for each flow in the datapath. */
2424 update_stats(ofproto);
2426 /* Expire facets that have been idle too long. */
2427 dp_max_idle = facet_max_idle(ofproto);
2428 expire_facets(ofproto, dp_max_idle);
2430 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2431 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2432 struct cls_cursor cursor;
2434 cls_cursor_init(&cursor, table, NULL);
2435 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2440 /* All outstanding data in existing flows has been accounted, so it's a
2441 * good time to do bond rebalancing. */
2442 if (ofproto->has_bonded_bundles) {
2443 struct ofbundle *bundle;
2445 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2447 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2452 return MIN(dp_max_idle, 1000);
2455 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2457 * This function also pushes statistics updates to rules which each facet
2458 * resubmits into. Generally these statistics will be accurate. However, if a
2459 * facet changes the rule it resubmits into at some time in between
2460 * update_stats() runs, it is possible that statistics accrued to the
2461 * old rule will be incorrectly attributed to the new rule. This could be
2462 * avoided by calling update_stats() whenever rules are created or
2463 * deleted. However, the performance impact of making so many calls to the
2464 * datapath do not justify the benefit of having perfectly accurate statistics.
2467 update_stats(struct ofproto_dpif *p)
2469 const struct dpif_flow_stats *stats;
2470 struct dpif_flow_dump dump;
2471 const struct nlattr *key;
2474 dpif_flow_dump_start(&dump, p->dpif);
2475 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2476 struct facet *facet;
2479 if (odp_flow_key_to_flow(key, key_len, &flow)) {
2483 odp_flow_key_format(key, key_len, &s);
2484 VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s",
2490 facet = facet_find(p, &flow);
2492 if (facet && facet->installed) {
2494 if (stats->n_packets >= facet->dp_packet_count) {
2495 uint64_t extra = stats->n_packets - facet->dp_packet_count;
2496 facet->packet_count += extra;
2498 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2501 if (stats->n_bytes >= facet->dp_byte_count) {
2502 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
2504 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2507 facet->dp_packet_count = stats->n_packets;
2508 facet->dp_byte_count = stats->n_bytes;
2510 facet_update_time(p, facet, stats->used);
2511 facet_account(p, facet);
2512 facet_push_stats(facet);
2514 /* There's a flow in the datapath that we know nothing about.
2516 COVERAGE_INC(facet_unexpected);
2517 dpif_flow_del(p->dpif, key, key_len, NULL);
2520 dpif_flow_dump_done(&dump);
2523 /* Calculates and returns the number of milliseconds of idle time after which
2524 * facets should expire from the datapath and we should fold their statistics
2525 * into their parent rules in userspace. */
2527 facet_max_idle(const struct ofproto_dpif *ofproto)
2530 * Idle time histogram.
2532 * Most of the time a switch has a relatively small number of facets. When
2533 * this is the case we might as well keep statistics for all of them in
2534 * userspace and to cache them in the kernel datapath for performance as
2537 * As the number of facets increases, the memory required to maintain
2538 * statistics about them in userspace and in the kernel becomes
2539 * significant. However, with a large number of facets it is likely that
2540 * only a few of them are "heavy hitters" that consume a large amount of
2541 * bandwidth. At this point, only heavy hitters are worth caching in the
2542 * kernel and maintaining in userspaces; other facets we can discard.
2544 * The technique used to compute the idle time is to build a histogram with
2545 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
2546 * that is installed in the kernel gets dropped in the appropriate bucket.
2547 * After the histogram has been built, we compute the cutoff so that only
2548 * the most-recently-used 1% of facets (but at least
2549 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2550 * the most-recently-used bucket of facets is kept, so actually an
2551 * arbitrary number of facets can be kept in any given expiration run
2552 * (though the next run will delete most of those unless they receive
2555 * This requires a second pass through the facets, in addition to the pass
2556 * made by update_stats(), because the former function never looks
2557 * at uninstallable facets.
2559 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2560 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2561 int buckets[N_BUCKETS] = { 0 };
2562 int total, subtotal, bucket;
2563 struct facet *facet;
2567 total = hmap_count(&ofproto->facets);
2568 if (total <= ofproto->up.flow_eviction_threshold) {
2569 return N_BUCKETS * BUCKET_WIDTH;
2572 /* Build histogram. */
2574 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
2575 long long int idle = now - facet->used;
2576 int bucket = (idle <= 0 ? 0
2577 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2578 : (unsigned int) idle / BUCKET_WIDTH);
2582 /* Find the first bucket whose flows should be expired. */
2583 subtotal = bucket = 0;
2585 subtotal += buckets[bucket++];
2586 } while (bucket < N_BUCKETS &&
2587 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2589 if (VLOG_IS_DBG_ENABLED()) {
2593 ds_put_cstr(&s, "keep");
2594 for (i = 0; i < N_BUCKETS; i++) {
2596 ds_put_cstr(&s, ", drop");
2599 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2602 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2606 return bucket * BUCKET_WIDTH;
2610 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
2612 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
2613 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
2614 struct ofexpired expired;
2616 if (facet->installed) {
2617 struct dpif_flow_stats stats;
2619 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
2621 facet_update_stats(ofproto, facet, &stats);
2624 expired.flow = facet->flow;
2625 expired.packet_count = facet->packet_count;
2626 expired.byte_count = facet->byte_count;
2627 expired.used = facet->used;
2628 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2633 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
2635 long long int cutoff = time_msec() - dp_max_idle;
2636 struct facet *facet, *next_facet;
2638 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
2639 facet_active_timeout(ofproto, facet);
2640 if (facet->used < cutoff) {
2641 facet_remove(ofproto, facet);
2646 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2647 * then delete it entirely. */
2649 rule_expire(struct rule_dpif *rule)
2651 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2652 struct facet *facet, *next_facet;
2656 /* Has 'rule' expired? */
2658 if (rule->up.hard_timeout
2659 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2660 reason = OFPRR_HARD_TIMEOUT;
2661 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2662 && now > rule->used + rule->up.idle_timeout * 1000) {
2663 reason = OFPRR_IDLE_TIMEOUT;
2668 COVERAGE_INC(ofproto_dpif_expired);
2670 /* Update stats. (This is a no-op if the rule expired due to an idle
2671 * timeout, because that only happens when the rule has no facets left.) */
2672 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2673 facet_remove(ofproto, facet);
2676 /* Get rid of the rule. */
2677 ofproto_rule_expire(&rule->up, reason);
2682 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2684 * The caller must already have determined that no facet with an identical
2685 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2686 * the ofproto's classifier table.
2688 * The facet will initially have no ODP actions. The caller should fix that
2689 * by calling facet_make_actions(). */
2690 static struct facet *
2691 facet_create(struct rule_dpif *rule, const struct flow *flow)
2693 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2694 struct facet *facet;
2696 facet = xzalloc(sizeof *facet);
2697 facet->used = time_msec();
2698 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2699 list_push_back(&rule->facets, &facet->list_node);
2701 facet->flow = *flow;
2702 netflow_flow_init(&facet->nf_flow);
2703 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2709 facet_free(struct facet *facet)
2711 free(facet->actions);
2716 execute_controller_action(struct ofproto_dpif *ofproto,
2717 const struct flow *flow,
2718 const struct nlattr *odp_actions, size_t actions_len,
2719 struct ofpbuf *packet)
2722 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
2723 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
2724 /* As an optimization, avoid a round-trip from userspace to kernel to
2725 * userspace. This also avoids possibly filling up kernel packet
2726 * buffers along the way.
2728 * This optimization will not accidentally catch sFlow
2729 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
2730 * inside OVS_ACTION_ATTR_SAMPLE. */
2731 const struct nlattr *nla;
2733 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
2734 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
2742 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2743 * 'packet', which arrived on 'in_port'.
2745 * Takes ownership of 'packet'. */
2747 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
2748 const struct nlattr *odp_actions, size_t actions_len,
2749 struct ofpbuf *packet)
2751 struct odputil_keybuf keybuf;
2755 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
2760 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2761 odp_flow_key_from_flow(&key, flow);
2763 error = dpif_execute(ofproto->dpif, key.data, key.size,
2764 odp_actions, actions_len, packet);
2766 ofpbuf_delete(packet);
2770 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2771 * statistics appropriately. 'packet' must have at least sizeof(struct
2772 * ofp_packet_in) bytes of headroom.
2774 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2775 * applying flow_extract() to 'packet' would yield the same flow as
2778 * 'facet' must have accurately composed datapath actions; that is, it must
2779 * not be in need of revalidation.
2781 * Takes ownership of 'packet'. */
2783 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
2784 struct ofpbuf *packet)
2786 struct dpif_flow_stats stats;
2788 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2790 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2791 stats.used = time_msec();
2792 if (execute_odp_actions(ofproto, &facet->flow,
2793 facet->actions, facet->actions_len, packet)) {
2794 facet_update_stats(ofproto, facet, &stats);
2798 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2800 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2801 * rule's statistics, via facet_uninstall().
2803 * - Removes 'facet' from its rule and from ofproto->facets.
2806 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2808 facet_uninstall(ofproto, facet);
2809 facet_flush_stats(ofproto, facet);
2810 hmap_remove(&ofproto->facets, &facet->hmap_node);
2811 list_remove(&facet->list_node);
2815 /* Composes the datapath actions for 'facet' based on its rule's actions. */
2817 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2818 const struct ofpbuf *packet)
2820 const struct rule_dpif *rule = facet->rule;
2821 struct ofpbuf *odp_actions;
2822 struct action_xlate_ctx ctx;
2824 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2825 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2826 facet->tags = ctx.tags;
2827 facet->may_install = ctx.may_set_up_flow;
2828 facet->has_learn = ctx.has_learn;
2829 facet->has_normal = ctx.has_normal;
2830 facet->nf_flow.output_iface = ctx.nf_output_iface;
2832 if (facet->actions_len != odp_actions->size
2833 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2834 free(facet->actions);
2835 facet->actions_len = odp_actions->size;
2836 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2839 ofpbuf_delete(odp_actions);
2842 /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len'
2843 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
2844 * in the datapath will be zeroed and 'stats' will be updated with traffic new
2845 * since 'facet' was last updated.
2847 * Returns 0 if successful, otherwise a positive errno value.*/
2849 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2850 const struct nlattr *actions, size_t actions_len,
2851 struct dpif_flow_stats *stats)
2853 struct odputil_keybuf keybuf;
2854 enum dpif_flow_put_flags flags;
2858 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2860 flags |= DPIF_FP_ZERO_STATS;
2863 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2864 odp_flow_key_from_flow(&key, &facet->flow);
2866 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2867 actions, actions_len, stats);
2870 facet_reset_dp_stats(facet, stats);
2876 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2877 * 'zero_stats' is true, clears any existing statistics from the datapath for
2880 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2882 struct dpif_flow_stats stats;
2884 if (facet->may_install
2885 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2886 zero_stats ? &stats : NULL)) {
2887 facet->installed = true;
2892 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
2895 const struct nlattr *a;
2899 if (facet->byte_count <= facet->accounted_bytes) {
2902 n_bytes = facet->byte_count - facet->accounted_bytes;
2903 facet->accounted_bytes = facet->byte_count;
2905 /* Feed information from the active flows back into the learning table to
2906 * ensure that table is always in sync with what is actually flowing
2907 * through the datapath. */
2908 if (facet->has_learn || facet->has_normal) {
2909 struct action_xlate_ctx ctx;
2911 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2912 ctx.may_learn = true;
2913 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
2914 facet->rule->up.n_actions));
2917 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
2921 /* This loop feeds byte counters to bond_account() for rebalancing to use
2922 * as a basis. We also need to track the actual VLAN on which the packet
2923 * is going to be sent to ensure that it matches the one passed to
2924 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
2926 vlan_tci = facet->flow.vlan_tci;
2927 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2928 const struct ovs_action_push_vlan *vlan;
2929 struct ofport_dpif *port;
2931 switch (nl_attr_type(a)) {
2932 case OVS_ACTION_ATTR_OUTPUT:
2933 port = get_odp_port(ofproto, nl_attr_get_u32(a));
2934 if (port && port->bundle && port->bundle->bond) {
2935 bond_account(port->bundle->bond, &facet->flow,
2936 vlan_tci_to_vid(vlan_tci), n_bytes);
2940 case OVS_ACTION_ATTR_POP_VLAN:
2941 vlan_tci = htons(0);
2944 case OVS_ACTION_ATTR_PUSH_VLAN:
2945 vlan = nl_attr_get(a);
2946 vlan_tci = vlan->vlan_tci;
2952 /* If 'rule' is installed in the datapath, uninstalls it. */
2954 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2956 if (facet->installed) {
2957 struct odputil_keybuf keybuf;
2958 struct dpif_flow_stats stats;
2962 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2963 odp_flow_key_from_flow(&key, &facet->flow);
2965 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
2966 facet_reset_dp_stats(facet, &stats);
2968 facet_update_stats(p, facet, &stats);
2970 facet->installed = false;
2972 assert(facet->dp_packet_count == 0);
2973 assert(facet->dp_byte_count == 0);
2977 /* Returns true if the only action for 'facet' is to send to the controller.
2978 * (We don't report NetFlow expiration messages for such facets because they
2979 * are just part of the control logic for the network, not real traffic). */
2981 facet_is_controller_flow(struct facet *facet)
2984 && facet->rule->up.n_actions == 1
2985 && action_outputs_to_port(&facet->rule->up.actions[0],
2986 htons(OFPP_CONTROLLER)));
2989 /* Resets 'facet''s datapath statistics counters. This should be called when
2990 * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null,
2991 * it should contain the statistics returned by dpif when 'facet' was reset in
2992 * the datapath. 'stats' will be modified to only included statistics new
2993 * since 'facet' was last updated. */
2995 facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats)
2997 if (stats && facet->dp_packet_count <= stats->n_packets
2998 && facet->dp_byte_count <= stats->n_bytes) {
2999 stats->n_packets -= facet->dp_packet_count;
3000 stats->n_bytes -= facet->dp_byte_count;
3003 facet->dp_packet_count = 0;
3004 facet->dp_byte_count = 0;
3007 /* Folds all of 'facet''s statistics into its rule. Also updates the
3008 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3009 * 'facet''s statistics in the datapath should have been zeroed and folded into
3010 * its packet and byte counts before this function is called. */
3012 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3014 assert(!facet->dp_byte_count);
3015 assert(!facet->dp_packet_count);
3017 facet_push_stats(facet);
3018 facet_account(ofproto, facet);
3020 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3021 struct ofexpired expired;
3022 expired.flow = facet->flow;
3023 expired.packet_count = facet->packet_count;
3024 expired.byte_count = facet->byte_count;
3025 expired.used = facet->used;
3026 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3029 facet->rule->packet_count += facet->packet_count;
3030 facet->rule->byte_count += facet->byte_count;
3032 /* Reset counters to prevent double counting if 'facet' ever gets
3034 facet_reset_counters(facet);
3036 netflow_flow_clear(&facet->nf_flow);
3039 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3040 * Returns it if found, otherwise a null pointer.
3042 * The returned facet might need revalidation; use facet_lookup_valid()
3043 * instead if that is important. */
3044 static struct facet *
3045 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3047 struct facet *facet;
3049 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3051 if (flow_equal(flow, &facet->flow)) {
3059 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3060 * Returns it if found, otherwise a null pointer.
3062 * The returned facet is guaranteed to be valid. */
3063 static struct facet *
3064 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3066 struct facet *facet = facet_find(ofproto, flow);
3068 /* The facet we found might not be valid, since we could be in need of
3069 * revalidation. If it is not valid, don't return it. */
3071 && (ofproto->need_revalidate
3072 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3073 && !facet_revalidate(ofproto, facet)) {
3074 COVERAGE_INC(facet_invalidated);
3081 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3083 * - If the rule found is different from 'facet''s current rule, moves
3084 * 'facet' to the new rule and recompiles its actions.
3086 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3087 * where it is and recompiles its actions anyway.
3089 * - If there is none, destroys 'facet'.
3091 * Returns true if 'facet' still exists, false if it has been destroyed. */
3093 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3095 struct action_xlate_ctx ctx;
3096 struct ofpbuf *odp_actions;
3097 struct rule_dpif *new_rule;
3098 bool actions_changed;
3100 COVERAGE_INC(facet_revalidate);
3102 /* Determine the new rule. */
3103 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3105 /* No new rule, so delete the facet. */
3106 facet_remove(ofproto, facet);
3110 /* Calculate new datapath actions.
3112 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3113 * emit a NetFlow expiration and, if so, we need to have the old state
3114 * around to properly compose it. */
3115 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
3116 odp_actions = xlate_actions(&ctx,
3117 new_rule->up.actions, new_rule->up.n_actions);
3118 actions_changed = (facet->actions_len != odp_actions->size
3119 || memcmp(facet->actions, odp_actions->data,
3120 facet->actions_len));
3122 /* If the datapath actions changed or the installability changed,
3123 * then we need to talk to the datapath. */
3124 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
3125 if (ctx.may_set_up_flow) {
3126 struct dpif_flow_stats stats;
3128 facet_put__(ofproto, facet,
3129 odp_actions->data, odp_actions->size, &stats);
3130 facet_update_stats(ofproto, facet, &stats);
3132 facet_uninstall(ofproto, facet);
3135 /* The datapath flow is gone or has zeroed stats, so push stats out of
3136 * 'facet' into 'rule'. */
3137 facet_flush_stats(ofproto, facet);
3140 /* Update 'facet' now that we've taken care of all the old state. */
3141 facet->tags = ctx.tags;
3142 facet->nf_flow.output_iface = ctx.nf_output_iface;
3143 facet->may_install = ctx.may_set_up_flow;
3144 facet->has_learn = ctx.has_learn;
3145 facet->has_normal = ctx.has_normal;
3146 if (actions_changed) {
3147 free(facet->actions);
3148 facet->actions_len = odp_actions->size;
3149 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
3151 if (facet->rule != new_rule) {
3152 COVERAGE_INC(facet_changed_rule);
3153 list_remove(&facet->list_node);
3154 list_push_back(&new_rule->facets, &facet->list_node);
3155 facet->rule = new_rule;
3156 facet->used = new_rule->up.created;
3157 facet->rs_used = facet->used;
3160 ofpbuf_delete(odp_actions);
3165 /* Updates 'facet''s used time. Caller is responsible for calling
3166 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3168 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3171 if (used > facet->used) {
3173 if (used > facet->rule->used) {
3174 facet->rule->used = used;
3176 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3180 /* Folds the statistics from 'stats' into the counters in 'facet'.
3182 * Because of the meaning of a facet's counters, it only makes sense to do this
3183 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
3184 * packet that was sent by hand or if it represents statistics that have been
3185 * cleared out of the datapath. */
3187 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
3188 const struct dpif_flow_stats *stats)
3190 if (stats->n_packets || stats->used > facet->used) {
3191 facet_update_time(ofproto, facet, stats->used);
3192 facet->packet_count += stats->n_packets;
3193 facet->byte_count += stats->n_bytes;
3194 facet_push_stats(facet);
3195 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3200 facet_reset_counters(struct facet *facet)
3202 facet->packet_count = 0;
3203 facet->byte_count = 0;
3204 facet->rs_packet_count = 0;
3205 facet->rs_byte_count = 0;
3206 facet->accounted_bytes = 0;
3210 facet_push_stats(struct facet *facet)
3212 uint64_t rs_packets, rs_bytes;
3214 assert(facet->packet_count >= facet->rs_packet_count);
3215 assert(facet->byte_count >= facet->rs_byte_count);
3216 assert(facet->used >= facet->rs_used);
3218 rs_packets = facet->packet_count - facet->rs_packet_count;
3219 rs_bytes = facet->byte_count - facet->rs_byte_count;
3221 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
3222 facet->rs_packet_count = facet->packet_count;
3223 facet->rs_byte_count = facet->byte_count;
3224 facet->rs_used = facet->used;
3226 flow_push_stats(facet->rule, &facet->flow,
3227 rs_packets, rs_bytes, facet->used);
3231 struct ofproto_push {
3232 struct action_xlate_ctx ctx;
3239 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3241 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3244 rule->packet_count += push->packets;
3245 rule->byte_count += push->bytes;
3246 rule->used = MAX(push->used, rule->used);
3250 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3251 * 'rule''s actions. */
3253 flow_push_stats(const struct rule_dpif *rule,
3254 struct flow *flow, uint64_t packets, uint64_t bytes,
3257 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3258 struct ofproto_push push;
3260 push.packets = packets;
3264 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
3265 push.ctx.resubmit_hook = push_resubmit;
3266 ofpbuf_delete(xlate_actions(&push.ctx,
3267 rule->up.actions, rule->up.n_actions));
3272 static struct rule_dpif *
3273 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3276 struct cls_rule *cls_rule;
3277 struct classifier *cls;
3279 if (table_id >= N_TABLES) {
3283 cls = &ofproto->up.tables[table_id];
3284 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3285 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3286 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3287 * are unavailable. */
3288 struct flow ofpc_normal_flow = *flow;
3289 ofpc_normal_flow.tp_src = htons(0);
3290 ofpc_normal_flow.tp_dst = htons(0);
3291 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3293 cls_rule = classifier_lookup(cls, flow);
3295 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3299 complete_operation(struct rule_dpif *rule)
3301 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3303 rule_invalidate(rule);
3305 struct dpif_completion *c = xmalloc(sizeof *c);
3306 c->op = rule->up.pending;
3307 list_push_back(&ofproto->completions, &c->list_node);
3309 ofoperation_complete(rule->up.pending, 0);
3313 static struct rule *
3316 struct rule_dpif *rule = xmalloc(sizeof *rule);
3321 rule_dealloc(struct rule *rule_)
3323 struct rule_dpif *rule = rule_dpif_cast(rule_);
3328 rule_construct(struct rule *rule_)
3330 struct rule_dpif *rule = rule_dpif_cast(rule_);
3331 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3332 struct rule_dpif *victim;
3336 error = validate_actions(rule->up.actions, rule->up.n_actions,
3337 &rule->up.cr.flow, ofproto->max_ports);
3342 rule->used = rule->up.created;
3343 rule->packet_count = 0;
3344 rule->byte_count = 0;
3346 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3347 if (victim && !list_is_empty(&victim->facets)) {
3348 struct facet *facet;
3350 rule->facets = victim->facets;
3351 list_moved(&rule->facets);
3352 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3353 /* XXX: We're only clearing our local counters here. It's possible
3354 * that quite a few packets are unaccounted for in the datapath
3355 * statistics. These will be accounted to the new rule instead of
3356 * cleared as required. This could be fixed by clearing out the
3357 * datapath statistics for this facet, but currently it doesn't
3359 facet_reset_counters(facet);
3363 /* Must avoid list_moved() in this case. */
3364 list_init(&rule->facets);
3367 table_id = rule->up.table_id;
3368 rule->tag = (victim ? victim->tag
3370 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3371 ofproto->tables[table_id].basis));
3373 complete_operation(rule);
3378 rule_destruct(struct rule *rule_)
3380 struct rule_dpif *rule = rule_dpif_cast(rule_);
3381 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3382 struct facet *facet, *next_facet;
3384 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3385 facet_revalidate(ofproto, facet);
3388 complete_operation(rule);
3392 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3394 struct rule_dpif *rule = rule_dpif_cast(rule_);
3395 struct facet *facet;
3397 /* Start from historical data for 'rule' itself that are no longer tracked
3398 * in facets. This counts, for example, facets that have expired. */
3399 *packets = rule->packet_count;
3400 *bytes = rule->byte_count;
3402 /* Add any statistics that are tracked by facets. This includes
3403 * statistical data recently updated by ofproto_update_stats() as well as
3404 * stats for packets that were executed "by hand" via dpif_execute(). */
3405 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3406 *packets += facet->packet_count;
3407 *bytes += facet->byte_count;
3412 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
3414 struct rule_dpif *rule = rule_dpif_cast(rule_);
3415 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3416 struct action_xlate_ctx ctx;
3417 struct ofpbuf *odp_actions;
3418 struct facet *facet;
3421 /* First look for a related facet. If we find one, account it to that. */
3422 facet = facet_lookup_valid(ofproto, flow);
3423 if (facet && facet->rule == rule) {
3424 if (!facet->may_install) {
3425 facet_make_actions(ofproto, facet, packet);
3427 facet_execute(ofproto, facet, packet);
3431 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
3432 * create a new facet for it and use that. */
3433 if (rule_dpif_lookup(ofproto, flow, 0) == rule) {
3434 facet = facet_create(rule, flow);
3435 facet_make_actions(ofproto, facet, packet);
3436 facet_execute(ofproto, facet, packet);
3437 facet_install(ofproto, facet, true);
3441 /* We can't account anything to a facet. If we were to try, then that
3442 * facet would have a non-matching rule, busting our invariants. */
3443 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3444 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3445 size = packet->size;
3446 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3447 odp_actions->size, packet)) {
3448 rule->used = time_msec();
3449 rule->packet_count++;
3450 rule->byte_count += size;
3451 flow_push_stats(rule, flow, 1, size, rule->used);
3453 ofpbuf_delete(odp_actions);
3459 rule_modify_actions(struct rule *rule_)
3461 struct rule_dpif *rule = rule_dpif_cast(rule_);
3462 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3465 error = validate_actions(rule->up.actions, rule->up.n_actions,
3466 &rule->up.cr.flow, ofproto->max_ports);
3468 ofoperation_complete(rule->up.pending, error);
3472 complete_operation(rule);
3475 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.
3476 * Returns 0 if successful, otherwise a positive errno value. */
3478 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port,
3479 const struct ofpbuf *packet)
3481 struct ofpbuf key, odp_actions;
3482 struct odputil_keybuf keybuf;
3486 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3487 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3488 odp_flow_key_from_flow(&key, &flow);
3490 ofpbuf_init(&odp_actions, 32);
3491 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3493 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3494 error = dpif_execute(ofproto->dpif,
3496 odp_actions.data, odp_actions.size,
3498 ofpbuf_uninit(&odp_actions);
3501 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3502 ofproto->up.name, odp_port, strerror(error));
3507 /* OpenFlow to datapath action translation. */
3509 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3510 struct action_xlate_ctx *ctx);
3511 static void xlate_normal(struct action_xlate_ctx *);
3514 put_userspace_action(const struct ofproto_dpif *ofproto,
3515 struct ofpbuf *odp_actions,
3516 const struct flow *flow,
3517 const struct user_action_cookie *cookie)
3522 pid = dpif_port_get_pid(ofproto->dpif,
3523 ofp_port_to_odp_port(flow->in_port));
3525 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3526 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3527 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3528 cookie, sizeof *cookie);
3529 nl_msg_end_nested(odp_actions, offset);
3531 return odp_actions->size - NLA_ALIGN(sizeof *cookie);
3534 /* Compose SAMPLE action for sFlow. */
3536 compose_sflow_action(const struct ofproto_dpif *ofproto,
3537 struct ofpbuf *odp_actions,
3538 const struct flow *flow,
3541 uint32_t port_ifindex;
3542 uint32_t probability;
3543 struct user_action_cookie cookie;
3544 size_t sample_offset, actions_offset;
3545 int cookie_offset, n_output;
3547 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3551 if (odp_port == OVSP_NONE) {
3555 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3559 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3561 /* Number of packets out of UINT_MAX to sample. */
3562 probability = dpif_sflow_get_probability(ofproto->sflow);
3563 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3565 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3567 cookie.type = USER_ACTION_COOKIE_SFLOW;
3568 cookie.data = port_ifindex;
3569 cookie.n_output = n_output;
3570 cookie.vlan_tci = 0;
3571 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3573 nl_msg_end_nested(odp_actions, actions_offset);
3574 nl_msg_end_nested(odp_actions, sample_offset);
3575 return cookie_offset;
3578 /* SAMPLE action must be first action in any given list of actions.
3579 * At this point we do not have all information required to build it. So try to
3580 * build sample action as complete as possible. */
3582 add_sflow_action(struct action_xlate_ctx *ctx)
3584 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3586 &ctx->flow, OVSP_NONE);
3587 ctx->sflow_odp_port = 0;
3588 ctx->sflow_n_outputs = 0;
3591 /* Fix SAMPLE action according to data collected while composing ODP actions.
3592 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
3593 * USERSPACE action's user-cookie which is required for sflow. */
3595 fix_sflow_action(struct action_xlate_ctx *ctx)
3597 const struct flow *base = &ctx->base_flow;
3598 struct user_action_cookie *cookie;
3600 if (!ctx->user_cookie_offset) {
3604 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
3606 assert(cookie != NULL);
3607 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
3609 if (ctx->sflow_n_outputs) {
3610 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
3611 ctx->sflow_odp_port);
3613 if (ctx->sflow_n_outputs >= 255) {
3614 cookie->n_output = 255;
3616 cookie->n_output = ctx->sflow_n_outputs;
3618 cookie->vlan_tci = base->vlan_tci;
3622 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3623 const void *key, size_t key_size)
3625 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3626 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3627 nl_msg_end_nested(odp_actions, offset);
3631 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
3632 struct ofpbuf *odp_actions)
3634 if (base->tun_id == flow->tun_id) {
3637 base->tun_id = flow->tun_id;
3639 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
3640 &base->tun_id, sizeof(base->tun_id));
3644 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3645 struct ofpbuf *odp_actions)
3647 struct ovs_key_ethernet eth_key;
3649 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3650 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3654 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3655 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3657 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3658 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3660 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3661 ð_key, sizeof(eth_key));
3665 commit_vlan_action(struct action_xlate_ctx *ctx, ovs_be16 new_tci)
3667 struct flow *base = &ctx->base_flow;
3669 if (base->vlan_tci == new_tci) {
3673 if (base->vlan_tci & htons(VLAN_CFI)) {
3674 nl_msg_put_flag(ctx->odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3677 if (new_tci & htons(VLAN_CFI)) {
3678 struct ovs_action_push_vlan vlan;
3680 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3681 vlan.vlan_tci = new_tci;
3682 nl_msg_put_unspec(ctx->odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3683 &vlan, sizeof vlan);
3685 base->vlan_tci = new_tci;
3689 commit_set_nw_action(const struct flow *flow, struct flow *base,
3690 struct ofpbuf *odp_actions)
3692 struct ovs_key_ipv4 ipv4_key;
3694 if (base->dl_type != htons(ETH_TYPE_IP) ||
3695 !base->nw_src || !base->nw_dst) {
3699 if (base->nw_src == flow->nw_src &&
3700 base->nw_dst == flow->nw_dst &&
3701 base->nw_tos == flow->nw_tos &&
3702 base->nw_ttl == flow->nw_ttl &&
3703 base->nw_frag == flow->nw_frag) {
3707 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3708 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3709 ipv4_key.ipv4_proto = base->nw_proto;
3710 ipv4_key.ipv4_tos = flow->nw_tos;
3711 ipv4_key.ipv4_ttl = flow->nw_ttl;
3712 ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE
3713 : base->nw_frag == FLOW_NW_FRAG_ANY
3714 ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER);
3716 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3717 &ipv4_key, sizeof(ipv4_key));
3721 commit_set_port_action(const struct flow *flow, struct flow *base,
3722 struct ofpbuf *odp_actions)
3724 if (!base->tp_src || !base->tp_dst) {
3728 if (base->tp_src == flow->tp_src &&
3729 base->tp_dst == flow->tp_dst) {
3733 if (flow->nw_proto == IPPROTO_TCP) {
3734 struct ovs_key_tcp port_key;
3736 port_key.tcp_src = base->tp_src = flow->tp_src;
3737 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3739 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3740 &port_key, sizeof(port_key));
3742 } else if (flow->nw_proto == IPPROTO_UDP) {
3743 struct ovs_key_udp port_key;
3745 port_key.udp_src = base->tp_src = flow->tp_src;
3746 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3748 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3749 &port_key, sizeof(port_key));
3754 commit_set_priority_action(const struct flow *flow, struct flow *base,
3755 struct ofpbuf *odp_actions)
3757 if (base->priority == flow->priority) {
3760 base->priority = flow->priority;
3762 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3763 &base->priority, sizeof(base->priority));
3767 commit_odp_actions(struct action_xlate_ctx *ctx)
3769 const struct flow *flow = &ctx->flow;
3770 struct flow *base = &ctx->base_flow;
3771 struct ofpbuf *odp_actions = ctx->odp_actions;
3773 commit_set_tun_id_action(flow, base, odp_actions);
3774 commit_set_ether_addr_action(flow, base, odp_actions);
3775 commit_vlan_action(ctx, flow->vlan_tci);
3776 commit_set_nw_action(flow, base, odp_actions);
3777 commit_set_port_action(flow, base, odp_actions);
3778 commit_set_priority_action(flow, base, odp_actions);
3782 compose_output_action(struct action_xlate_ctx *ctx, uint16_t odp_port)
3784 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3785 ctx->sflow_odp_port = odp_port;
3786 ctx->sflow_n_outputs++;
3790 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
3792 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
3793 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
3796 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
3797 || !stp_forward_in_state(ofport->stp_state)) {
3798 /* Forwarding disabled on port. */
3803 * We don't have an ofport record for this port, but it doesn't hurt to
3804 * allow forwarding to it anyhow. Maybe such a port will appear later
3805 * and we're pre-populating the flow table.
3809 commit_odp_actions(ctx);
3810 compose_output_action(ctx, odp_port);
3811 ctx->nf_output_iface = ofp_port;
3815 xlate_table_action(struct action_xlate_ctx *ctx,
3816 uint16_t in_port, uint8_t table_id)
3818 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3819 struct ofproto_dpif *ofproto = ctx->ofproto;
3820 struct rule_dpif *rule;
3821 uint16_t old_in_port;
3822 uint8_t old_table_id;
3824 old_table_id = ctx->table_id;
3825 ctx->table_id = table_id;
3827 /* Look up a flow with 'in_port' as the input port. */
3828 old_in_port = ctx->flow.in_port;
3829 ctx->flow.in_port = in_port;
3830 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
3833 if (table_id > 0 && table_id < N_TABLES) {
3834 struct table_dpif *table = &ofproto->tables[table_id];
3835 if (table->other_table) {
3838 : rule_calculate_tag(&ctx->flow,
3839 &table->other_table->wc,
3844 /* Restore the original input port. Otherwise OFPP_NORMAL and
3845 * OFPP_IN_PORT will have surprising behavior. */
3846 ctx->flow.in_port = old_in_port;
3848 if (ctx->resubmit_hook) {
3849 ctx->resubmit_hook(ctx, rule);
3854 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
3858 ctx->table_id = old_table_id;
3860 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3862 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
3863 MAX_RESUBMIT_RECURSION);
3868 xlate_resubmit_table(struct action_xlate_ctx *ctx,
3869 const struct nx_action_resubmit *nar)
3874 in_port = (nar->in_port == htons(OFPP_IN_PORT)
3876 : ntohs(nar->in_port));
3877 table_id = nar->table == 255 ? ctx->table_id : nar->table;
3879 xlate_table_action(ctx, in_port, table_id);
3883 flood_packets(struct action_xlate_ctx *ctx, ovs_be32 mask)
3885 struct ofport_dpif *ofport;
3887 commit_odp_actions(ctx);
3888 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
3889 uint16_t ofp_port = ofport->up.ofp_port;
3890 if (ofp_port != ctx->flow.in_port
3891 && !(ofport->up.opp.config & mask)
3892 && stp_forward_in_state(ofport->stp_state)) {
3893 compose_output_action(ctx, ofport->odp_port);
3897 ctx->nf_output_iface = NF_OUT_FLOOD;
3901 compose_controller_action(struct action_xlate_ctx *ctx, int len)
3903 struct user_action_cookie cookie;
3905 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
3907 cookie.n_output = 0;
3908 cookie.vlan_tci = 0;
3909 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
3913 xlate_output_action__(struct action_xlate_ctx *ctx,
3914 uint16_t port, uint16_t max_len)
3916 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3918 ctx->nf_output_iface = NF_OUT_DROP;
3922 add_output_action(ctx, ctx->flow.in_port);
3925 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
3931 flood_packets(ctx, htonl(OFPPC_NO_FLOOD));
3934 flood_packets(ctx, htonl(0));
3936 case OFPP_CONTROLLER:
3937 commit_odp_actions(ctx);
3938 compose_controller_action(ctx, max_len);
3941 add_output_action(ctx, OFPP_LOCAL);
3946 if (port != ctx->flow.in_port) {
3947 add_output_action(ctx, port);
3952 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3953 ctx->nf_output_iface = NF_OUT_FLOOD;
3954 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3955 ctx->nf_output_iface = prev_nf_output_iface;
3956 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3957 ctx->nf_output_iface != NF_OUT_FLOOD) {
3958 ctx->nf_output_iface = NF_OUT_MULTI;
3963 xlate_output_reg_action(struct action_xlate_ctx *ctx,
3964 const struct nx_action_output_reg *naor)
3968 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
3970 if (ofp_port <= UINT16_MAX) {
3971 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
3976 xlate_output_action(struct action_xlate_ctx *ctx,
3977 const struct ofp_action_output *oao)
3979 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3983 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3984 const struct ofp_action_enqueue *oae)
3986 uint16_t ofp_port, odp_port;
3987 uint32_t flow_priority, priority;
3990 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3993 /* Fall back to ordinary output action. */
3994 xlate_output_action__(ctx, ntohs(oae->port), 0);
3998 /* Figure out datapath output port. */
3999 ofp_port = ntohs(oae->port);
4000 if (ofp_port == OFPP_IN_PORT) {
4001 ofp_port = ctx->flow.in_port;
4002 } else if (ofp_port == ctx->flow.in_port) {
4005 odp_port = ofp_port_to_odp_port(ofp_port);
4007 /* Add datapath actions. */
4008 flow_priority = ctx->flow.priority;
4009 ctx->flow.priority = priority;
4010 add_output_action(ctx, odp_port);
4011 ctx->flow.priority = flow_priority;
4013 /* Update NetFlow output port. */
4014 if (ctx->nf_output_iface == NF_OUT_DROP) {
4015 ctx->nf_output_iface = odp_port;
4016 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4017 ctx->nf_output_iface = NF_OUT_MULTI;
4022 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4023 const struct nx_action_set_queue *nasq)
4028 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4031 /* Couldn't translate queue to a priority, so ignore. A warning
4032 * has already been logged. */
4036 ctx->flow.priority = priority;
4039 struct xlate_reg_state {
4045 xlate_autopath(struct action_xlate_ctx *ctx,
4046 const struct nx_action_autopath *naa)
4048 uint16_t ofp_port = ntohl(naa->id);
4049 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4051 if (!port || !port->bundle) {
4052 ofp_port = OFPP_NONE;
4053 } else if (port->bundle->bond) {
4054 /* Autopath does not support VLAN hashing. */
4055 struct ofport_dpif *slave = bond_choose_output_slave(
4056 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4058 ofp_port = slave->up.ofp_port;
4061 autopath_execute(naa, &ctx->flow, ofp_port);
4065 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4067 struct ofproto_dpif *ofproto = ofproto_;
4068 struct ofport_dpif *port;
4078 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4081 port = get_ofp_port(ofproto, ofp_port);
4082 return port ? port->may_enable : false;
4087 xlate_learn_action(struct action_xlate_ctx *ctx,
4088 const struct nx_action_learn *learn)
4090 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4091 struct ofputil_flow_mod fm;
4094 learn_execute(learn, &ctx->flow, &fm);
4096 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4097 if (error && !VLOG_DROP_WARN(&rl)) {
4098 char *msg = ofputil_error_to_string(error);
4099 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4107 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4109 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4110 ? htonl(OFPPC_NO_RECV_STP)
4111 : htonl(OFPPC_NO_RECV))) {
4115 /* Only drop packets here if both forwarding and learning are
4116 * disabled. If just learning is enabled, we need to have
4117 * OFPP_NORMAL and the learning action have a look at the packet
4118 * before we can drop it. */
4119 if (!stp_forward_in_state(port->stp_state)
4120 && !stp_learn_in_state(port->stp_state)) {
4128 do_xlate_actions(const union ofp_action *in, size_t n_in,
4129 struct action_xlate_ctx *ctx)
4131 const struct ofport_dpif *port;
4132 const union ofp_action *ia;
4135 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4136 if (port && !may_receive(port, ctx)) {
4137 /* Drop this flow. */
4141 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4142 const struct ofp_action_dl_addr *oada;
4143 const struct nx_action_resubmit *nar;
4144 const struct nx_action_set_tunnel *nast;
4145 const struct nx_action_set_queue *nasq;
4146 const struct nx_action_multipath *nam;
4147 const struct nx_action_autopath *naa;
4148 const struct nx_action_bundle *nab;
4149 const struct nx_action_output_reg *naor;
4150 enum ofputil_action_code code;
4157 code = ofputil_decode_action_unsafe(ia);
4159 case OFPUTIL_OFPAT_OUTPUT:
4160 xlate_output_action(ctx, &ia->output);
4163 case OFPUTIL_OFPAT_SET_VLAN_VID:
4164 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4165 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4168 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4169 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4170 ctx->flow.vlan_tci |= htons(
4171 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4174 case OFPUTIL_OFPAT_STRIP_VLAN:
4175 ctx->flow.vlan_tci = htons(0);
4178 case OFPUTIL_OFPAT_SET_DL_SRC:
4179 oada = ((struct ofp_action_dl_addr *) ia);
4180 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4183 case OFPUTIL_OFPAT_SET_DL_DST:
4184 oada = ((struct ofp_action_dl_addr *) ia);
4185 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4188 case OFPUTIL_OFPAT_SET_NW_SRC:
4189 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4192 case OFPUTIL_OFPAT_SET_NW_DST:
4193 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4196 case OFPUTIL_OFPAT_SET_NW_TOS:
4197 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4198 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4201 case OFPUTIL_OFPAT_SET_TP_SRC:
4202 ctx->flow.tp_src = ia->tp_port.tp_port;
4205 case OFPUTIL_OFPAT_SET_TP_DST:
4206 ctx->flow.tp_dst = ia->tp_port.tp_port;
4209 case OFPUTIL_OFPAT_ENQUEUE:
4210 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4213 case OFPUTIL_NXAST_RESUBMIT:
4214 nar = (const struct nx_action_resubmit *) ia;
4215 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4218 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4219 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4222 case OFPUTIL_NXAST_SET_TUNNEL:
4223 nast = (const struct nx_action_set_tunnel *) ia;
4224 tun_id = htonll(ntohl(nast->tun_id));
4225 ctx->flow.tun_id = tun_id;
4228 case OFPUTIL_NXAST_SET_QUEUE:
4229 nasq = (const struct nx_action_set_queue *) ia;
4230 xlate_set_queue_action(ctx, nasq);
4233 case OFPUTIL_NXAST_POP_QUEUE:
4234 ctx->flow.priority = ctx->original_priority;
4237 case OFPUTIL_NXAST_REG_MOVE:
4238 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4242 case OFPUTIL_NXAST_REG_LOAD:
4243 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4247 case OFPUTIL_NXAST_NOTE:
4248 /* Nothing to do. */
4251 case OFPUTIL_NXAST_SET_TUNNEL64:
4252 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4253 ctx->flow.tun_id = tun_id;
4256 case OFPUTIL_NXAST_MULTIPATH:
4257 nam = (const struct nx_action_multipath *) ia;
4258 multipath_execute(nam, &ctx->flow);
4261 case OFPUTIL_NXAST_AUTOPATH:
4262 naa = (const struct nx_action_autopath *) ia;
4263 xlate_autopath(ctx, naa);
4266 case OFPUTIL_NXAST_BUNDLE:
4267 ctx->ofproto->has_bundle_action = true;
4268 nab = (const struct nx_action_bundle *) ia;
4269 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4274 case OFPUTIL_NXAST_BUNDLE_LOAD:
4275 ctx->ofproto->has_bundle_action = true;
4276 nab = (const struct nx_action_bundle *) ia;
4277 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4281 case OFPUTIL_NXAST_OUTPUT_REG:
4282 naor = (const struct nx_action_output_reg *) ia;
4283 xlate_output_reg_action(ctx, naor);
4286 case OFPUTIL_NXAST_LEARN:
4287 ctx->has_learn = true;
4288 if (ctx->may_learn) {
4289 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4293 case OFPUTIL_NXAST_EXIT:
4299 /* We've let OFPP_NORMAL and the learning action look at the packet,
4300 * so drop it now if forwarding is disabled. */
4301 if (port && !stp_forward_in_state(port->stp_state)) {
4302 ofpbuf_clear(ctx->odp_actions);
4303 add_sflow_action(ctx);
4308 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4309 struct ofproto_dpif *ofproto, const struct flow *flow,
4310 const struct ofpbuf *packet)
4312 ctx->ofproto = ofproto;
4314 ctx->packet = packet;
4315 ctx->may_learn = packet != NULL;
4316 ctx->resubmit_hook = NULL;
4319 static struct ofpbuf *
4320 xlate_actions(struct action_xlate_ctx *ctx,
4321 const union ofp_action *in, size_t n_in)
4323 COVERAGE_INC(ofproto_dpif_xlate);
4325 ctx->odp_actions = ofpbuf_new(512);
4326 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4328 ctx->may_set_up_flow = true;
4329 ctx->has_learn = false;
4330 ctx->has_normal = false;
4331 ctx->nf_output_iface = NF_OUT_DROP;
4333 ctx->original_priority = ctx->flow.priority;
4334 ctx->base_flow = ctx->flow;
4335 ctx->base_flow.tun_id = 0;
4339 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4340 switch (ctx->ofproto->up.frag_handling) {
4341 case OFPC_FRAG_NORMAL:
4342 /* We must pretend that transport ports are unavailable. */
4343 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4344 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4347 case OFPC_FRAG_DROP:
4348 return ctx->odp_actions;
4350 case OFPC_FRAG_REASM:
4353 case OFPC_FRAG_NX_MATCH:
4354 /* Nothing to do. */
4359 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4360 ctx->may_set_up_flow = false;
4361 return ctx->odp_actions;
4363 add_sflow_action(ctx);
4364 do_xlate_actions(in, n_in, ctx);
4366 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4367 ctx->odp_actions->data,
4368 ctx->odp_actions->size)) {
4369 ctx->may_set_up_flow = false;
4371 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4373 compose_output_action(ctx, OVSP_LOCAL);
4376 fix_sflow_action(ctx);
4379 return ctx->odp_actions;
4382 /* OFPP_NORMAL implementation. */
4385 struct ofport_dpif *port;
4390 struct dst builtin[32];
4392 size_t n, allocated;
4395 static void dst_set_init(struct dst_set *);
4396 static void dst_set_add(struct dst_set *, const struct dst *);
4397 static void dst_set_free(struct dst_set *);
4399 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4401 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4402 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4403 * the bundle on which the packet was received, returns the VLAN to which the
4406 * Both 'vid' and the return value are in the range 0...4095. */
4408 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4410 switch (in_bundle->vlan_mode) {
4411 case PORT_VLAN_ACCESS:
4412 return in_bundle->vlan;
4415 case PORT_VLAN_TRUNK:
4418 case PORT_VLAN_NATIVE_UNTAGGED:
4419 case PORT_VLAN_NATIVE_TAGGED:
4420 return vid ? vid : in_bundle->vlan;
4427 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4428 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4431 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4432 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4435 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4437 switch (in_bundle->vlan_mode) {
4438 case PORT_VLAN_ACCESS:
4441 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4442 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4443 "packet received on port %s configured as VLAN "
4444 "%"PRIu16" access port",
4445 in_bundle->ofproto->up.name, vid,
4446 in_bundle->name, in_bundle->vlan);
4452 case PORT_VLAN_NATIVE_UNTAGGED:
4453 case PORT_VLAN_NATIVE_TAGGED:
4455 /* Port must always carry its native VLAN. */
4459 case PORT_VLAN_TRUNK:
4460 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4462 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4463 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4464 "received on port %s not configured for trunking "
4466 in_bundle->ofproto->up.name, vid,
4467 in_bundle->name, vid);
4479 /* Given 'vlan', the VLAN that a packet belongs to, and
4480 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4481 * that should be included in the 802.1Q header. (If the return value is 0,
4482 * then the 802.1Q header should only be included in the packet if there is a
4485 * Both 'vlan' and the return value are in the range 0...4095. */
4487 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4489 switch (out_bundle->vlan_mode) {
4490 case PORT_VLAN_ACCESS:
4493 case PORT_VLAN_TRUNK:
4494 case PORT_VLAN_NATIVE_TAGGED:
4497 case PORT_VLAN_NATIVE_UNTAGGED:
4498 return vlan == out_bundle->vlan ? 0 : vlan;
4506 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
4507 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
4511 vlan = input_vid_to_vlan(in_bundle, vlan_tci_to_vid(ctx->flow.vlan_tci));
4512 dst->vid = output_vlan_to_vid(out_bundle, vlan);
4514 dst->port = (!out_bundle->bond
4515 ? ofbundle_get_a_port(out_bundle)
4516 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4517 dst->vid, &ctx->tags));
4518 return dst->port != NULL;
4522 mirror_mask_ffs(mirror_mask_t mask)
4524 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4529 dst_set_init(struct dst_set *set)
4531 set->dsts = set->builtin;
4533 set->allocated = ARRAY_SIZE(set->builtin);
4537 dst_set_add(struct dst_set *set, const struct dst *dst)
4539 if (set->n >= set->allocated) {
4540 size_t new_allocated;
4541 struct dst *new_dsts;
4543 new_allocated = set->allocated * 2;
4544 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
4545 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
4549 set->dsts = new_dsts;
4550 set->allocated = new_allocated;
4552 set->dsts[set->n++] = *dst;
4556 dst_set_free(struct dst_set *set)
4558 if (set->dsts != set->builtin) {
4564 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4566 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4567 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4571 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4573 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4576 /* Returns an arbitrary interface within 'bundle'. */
4577 static struct ofport_dpif *
4578 ofbundle_get_a_port(const struct ofbundle *bundle)
4580 return CONTAINER_OF(list_front(&bundle->ports),
4581 struct ofport_dpif, bundle_node);
4585 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
4586 const struct ofbundle *in_bundle,
4587 const struct ofbundle *out_bundle, struct dst_set *set)
4591 if (out_bundle == OFBUNDLE_FLOOD) {
4592 struct ofbundle *bundle;
4594 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
4595 if (bundle != in_bundle
4596 && ofbundle_includes_vlan(bundle, vlan)
4597 && bundle->floodable
4598 && !bundle->mirror_out
4599 && set_dst(ctx, &dst, in_bundle, bundle)) {
4600 dst_set_add(set, &dst);
4603 ctx->nf_output_iface = NF_OUT_FLOOD;
4604 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
4605 dst_set_add(set, &dst);
4606 ctx->nf_output_iface = dst.port->odp_port;
4611 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4613 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4616 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4617 * to a VLAN. In general most packets may be mirrored but we want to drop
4618 * protocols that may confuse switches. */
4620 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4622 /* If you change this function's behavior, please update corresponding
4623 * documentation in vswitch.xml at the same time. */
4624 if (dst[0] != 0x01) {
4625 /* All the currently banned MACs happen to start with 01 currently, so
4626 * this is a quick way to eliminate most of the good ones. */
4628 if (eth_addr_is_reserved(dst)) {
4629 /* Drop STP, IEEE pause frames, and other reserved protocols
4630 * (01-80-c2-00-00-0x). */
4634 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4636 if ((dst[3] & 0xfe) == 0xcc &&
4637 (dst[4] & 0xfe) == 0xcc &&
4638 (dst[5] & 0xfe) == 0xcc) {
4639 /* Drop the following protocols plus others following the same
4642 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4643 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4644 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4648 if (!(dst[3] | dst[4] | dst[5])) {
4649 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4658 compose_mirror_dsts(struct action_xlate_ctx *ctx,
4659 uint16_t vlan, const struct ofbundle *in_bundle,
4660 struct dst_set *set)
4662 struct ofproto_dpif *ofproto = ctx->ofproto;
4663 mirror_mask_t mirrors;
4667 mirrors = in_bundle->src_mirrors;
4668 for (i = 0; i < set->n; i++) {
4669 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
4676 flow_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4681 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4683 if (!vlan_is_mirrored(m, vlan)) {
4684 mirrors &= mirrors - 1;
4688 mirrors &= ~m->dup_mirrors;
4690 if (set_dst(ctx, &dst, in_bundle, m->out)) {
4691 dst_set_add(set, &dst);
4693 } else if (eth_dst_may_rspan(ctx->flow.dl_dst)
4694 && vlan != m->out_vlan) {
4695 struct ofbundle *bundle;
4697 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4698 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4699 && !bundle->mirror_out
4700 && set_dst(ctx, &dst, in_bundle, bundle))
4702 /* set_dst() got dst->vid from the input packet's VLAN,
4703 * not from m->out_vlan, so recompute it. */
4704 dst.vid = output_vlan_to_vid(bundle, m->out_vlan);
4706 if (bundle == in_bundle && dst.vid == flow_vid) {
4707 /* Don't send out input port on same VLAN. */
4710 dst_set_add(set, &dst);
4718 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
4719 const struct ofbundle *in_bundle,
4720 const struct ofbundle *out_bundle)
4722 uint16_t initial_vid, cur_vid;
4723 const struct dst *dst;
4727 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
4728 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
4734 /* Output all the packets we can without having to change the VLAN. */
4735 commit_odp_actions(ctx);
4736 initial_vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
4737 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4738 if (dst->vid != initial_vid) {
4741 compose_output_action(ctx, dst->port->odp_port);
4744 /* Then output the rest. */
4745 cur_vid = initial_vid;
4746 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
4747 if (dst->vid == initial_vid) {
4750 if (dst->vid != cur_vid) {
4753 tci = htons(dst->vid);
4754 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4756 tci |= htons(VLAN_CFI);
4758 commit_vlan_action(ctx, tci);
4762 compose_output_action(ctx, dst->port->odp_port);
4768 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4769 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4770 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4772 is_gratuitous_arp(const struct flow *flow)
4774 return (flow->dl_type == htons(ETH_TYPE_ARP)
4775 && eth_addr_is_broadcast(flow->dl_dst)
4776 && (flow->nw_proto == ARP_OP_REPLY
4777 || (flow->nw_proto == ARP_OP_REQUEST
4778 && flow->nw_src == flow->nw_dst)));
4782 update_learning_table(struct ofproto_dpif *ofproto,
4783 const struct flow *flow, int vlan,
4784 struct ofbundle *in_bundle)
4786 struct mac_entry *mac;
4788 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
4792 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
4793 if (is_gratuitous_arp(flow)) {
4794 /* We don't want to learn from gratuitous ARP packets that are
4795 * reflected back over bond slaves so we lock the learning table. */
4796 if (!in_bundle->bond) {
4797 mac_entry_set_grat_arp_lock(mac);
4798 } else if (mac_entry_is_grat_arp_locked(mac)) {
4803 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
4804 /* The log messages here could actually be useful in debugging,
4805 * so keep the rate limit relatively high. */
4806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
4807 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
4808 "on port %s in VLAN %d",
4809 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
4810 in_bundle->name, vlan);
4812 mac->port.p = in_bundle;
4813 tag_set_add(&ofproto->revalidate_set,
4814 mac_learning_changed(ofproto->ml, mac));
4818 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
4819 * dropped. Returns true if they may be forwarded, false if they should be
4822 * If 'have_packet' is true, it indicates that the caller is processing a
4823 * received packet. If 'have_packet' is false, then the caller is just
4824 * revalidating an existing flow because configuration has changed. Either
4825 * way, 'have_packet' only affects logging (there is no point in logging errors
4826 * during revalidation).
4828 * Sets '*in_bundlep' to the input bundle. This will be a null pointer if
4829 * flow->in_port does not designate a known input port (in which case
4830 * is_admissible() returns false).
4832 * When returning true, sets '*vlanp' to the effective VLAN of the input
4833 * packet, as returned by input_vid_to_vlan().
4835 * May also add tags to '*tags', although the current implementation only does
4836 * so in one special case.
4839 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
4841 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
4843 struct ofport_dpif *in_port;
4844 struct ofbundle *in_bundle;
4850 /* Find the port and bundle for the received packet. */
4851 in_port = get_ofp_port(ofproto, flow->in_port);
4852 *in_bundlep = in_bundle = in_port ? in_port->bundle : NULL;
4853 if (!in_port || !in_bundle) {
4854 /* No interface? Something fishy... */
4856 /* Odd. A few possible reasons here:
4858 * - We deleted a port but there are still a few packets queued up
4861 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
4862 * we don't know about.
4864 * - Packet arrived on the local port but the local port is not
4867 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4869 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
4871 ofproto->up.name, flow->in_port);
4876 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
4877 !(flow->vlan_tci & htons(VLAN_CFI))) {
4878 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4879 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
4880 "VLAN tag received on port %s",
4881 ofproto->up.name, in_bundle->name);
4885 vid = vlan_tci_to_vid(flow->vlan_tci);
4886 if (!input_vid_is_valid(vid, in_bundle, have_packet)) {
4889 *vlanp = vlan = input_vid_to_vlan(in_bundle, vid);
4891 /* Drop frames for reserved multicast addresses only if forward_bpdu
4892 * option is absent. */
4893 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
4897 /* Drop frames on bundles reserved for mirroring. */
4898 if (in_bundle->mirror_out) {
4900 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4901 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4902 "%s, which is reserved exclusively for mirroring",
4903 ofproto->up.name, in_bundle->name);
4908 if (in_bundle->bond) {
4909 struct mac_entry *mac;
4911 switch (bond_check_admissibility(in_bundle->bond, in_port,
4912 flow->dl_dst, tags)) {
4919 case BV_DROP_IF_MOVED:
4920 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
4921 if (mac && mac->port.p != in_bundle &&
4922 (!is_gratuitous_arp(flow)
4923 || mac_entry_is_grat_arp_locked(mac))) {
4934 xlate_normal(struct action_xlate_ctx *ctx)
4936 struct ofbundle *in_bundle;
4937 struct ofbundle *out_bundle;
4938 struct mac_entry *mac;
4941 ctx->has_normal = true;
4943 /* Check whether we should drop packets in this flow. */
4944 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
4945 &ctx->tags, &vlan, &in_bundle)) {
4950 /* Learn source MAC. */
4951 if (ctx->may_learn) {
4952 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
4955 /* Determine output bundle. */
4956 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
4959 out_bundle = mac->port.p;
4960 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
4961 /* If we are revalidating but don't have a learning entry then eject
4962 * the flow. Installing a flow that floods packets opens up a window
4963 * of time where we could learn from a packet reflected on a bond and
4964 * blackhole packets before the learning table is updated to reflect
4965 * the correct port. */
4966 ctx->may_set_up_flow = false;
4969 out_bundle = OFBUNDLE_FLOOD;
4972 /* Don't send packets out their input bundles. */
4973 if (in_bundle == out_bundle) {
4979 compose_actions(ctx, vlan, in_bundle, out_bundle);
4983 /* Optimized flow revalidation.
4985 * It's a difficult problem, in general, to tell which facets need to have
4986 * their actions recalculated whenever the OpenFlow flow table changes. We
4987 * don't try to solve that general problem: for most kinds of OpenFlow flow
4988 * table changes, we recalculate the actions for every facet. This is
4989 * relatively expensive, but it's good enough if the OpenFlow flow table
4990 * doesn't change very often.
4992 * However, we can expect one particular kind of OpenFlow flow table change to
4993 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
4994 * of CPU on revalidating every facet whenever MAC learning modifies the flow
4995 * table, we add a special case that applies to flow tables in which every rule
4996 * has the same form (that is, the same wildcards), except that the table is
4997 * also allowed to have a single "catch-all" flow that matches all packets. We
4998 * optimize this case by tagging all of the facets that resubmit into the table
4999 * and invalidating the same tag whenever a flow changes in that table. The
5000 * end result is that we revalidate just the facets that need it (and sometimes
5001 * a few more, but not all of the facets or even all of the facets that
5002 * resubmit to the table modified by MAC learning). */
5004 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5005 * into an OpenFlow table with the given 'basis'. */
5007 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5010 if (flow_wildcards_is_catchall(wc)) {
5013 struct flow tag_flow = *flow;
5014 flow_zero_wildcards(&tag_flow, wc);
5015 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5019 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5020 * taggability of that table.
5022 * This function must be called after *each* change to a flow table. If you
5023 * skip calling it on some changes then the pointer comparisons at the end can
5024 * be invalid if you get unlucky. For example, if a flow removal causes a
5025 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5026 * different wildcards to be created with the same address, then this function
5027 * will incorrectly skip revalidation. */
5029 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5031 struct table_dpif *table = &ofproto->tables[table_id];
5032 const struct classifier *cls = &ofproto->up.tables[table_id];
5033 struct cls_table *catchall, *other;
5034 struct cls_table *t;
5036 catchall = other = NULL;
5038 switch (hmap_count(&cls->tables)) {
5040 /* We could tag this OpenFlow table but it would make the logic a
5041 * little harder and it's a corner case that doesn't seem worth it
5047 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5048 if (cls_table_is_catchall(t)) {
5050 } else if (!other) {
5053 /* Indicate that we can't tag this by setting both tables to
5054 * NULL. (We know that 'catchall' is already NULL.) */
5061 /* Can't tag this table. */
5065 if (table->catchall_table != catchall || table->other_table != other) {
5066 table->catchall_table = catchall;
5067 table->other_table = other;
5068 ofproto->need_revalidate = true;
5072 /* Given 'rule' that has changed in some way (either it is a rule being
5073 * inserted, a rule being deleted, or a rule whose actions are being
5074 * modified), marks facets for revalidation to ensure that packets will be
5075 * forwarded correctly according to the new state of the flow table.
5077 * This function must be called after *each* change to a flow table. See
5078 * the comment on table_update_taggable() for more information. */
5080 rule_invalidate(const struct rule_dpif *rule)
5082 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5084 table_update_taggable(ofproto, rule->up.table_id);
5086 if (!ofproto->need_revalidate) {
5087 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5089 if (table->other_table && rule->tag) {
5090 tag_set_add(&ofproto->revalidate_set, rule->tag);
5092 ofproto->need_revalidate = true;
5098 set_frag_handling(struct ofproto *ofproto_,
5099 enum ofp_config_flags frag_handling)
5101 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5103 if (frag_handling != OFPC_FRAG_REASM) {
5104 ofproto->need_revalidate = true;
5112 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5113 const struct flow *flow,
5114 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5116 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5119 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5120 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5123 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5124 ofproto->max_ports);
5126 struct odputil_keybuf keybuf;
5127 struct action_xlate_ctx ctx;
5128 struct ofpbuf *odp_actions;
5131 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5132 odp_flow_key_from_flow(&key, flow);
5134 action_xlate_ctx_init(&ctx, ofproto, flow, packet);
5135 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5136 dpif_execute(ofproto->dpif, key.data, key.size,
5137 odp_actions->data, odp_actions->size, packet);
5138 ofpbuf_delete(odp_actions);
5144 get_netflow_ids(const struct ofproto *ofproto_,
5145 uint8_t *engine_type, uint8_t *engine_id)
5147 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5149 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5152 static struct ofproto_dpif *
5153 ofproto_dpif_lookup(const char *name)
5155 struct ofproto *ofproto = ofproto_lookup(name);
5156 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5157 ? ofproto_dpif_cast(ofproto)
5162 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5163 const char *args, void *aux OVS_UNUSED)
5165 const struct ofproto_dpif *ofproto;
5167 ofproto = ofproto_dpif_lookup(args);
5169 unixctl_command_reply(conn, 501, "no such bridge");
5172 mac_learning_flush(ofproto->ml);
5174 unixctl_command_reply(conn, 200, "table successfully flushed");
5178 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5179 const char *args, void *aux OVS_UNUSED)
5181 struct ds ds = DS_EMPTY_INITIALIZER;
5182 const struct ofproto_dpif *ofproto;
5183 const struct mac_entry *e;
5185 ofproto = ofproto_dpif_lookup(args);
5187 unixctl_command_reply(conn, 501, "no such bridge");
5191 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5192 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5193 struct ofbundle *bundle = e->port.p;
5194 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5195 ofbundle_get_a_port(bundle)->odp_port,
5196 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5198 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5202 struct ofproto_trace {
5203 struct action_xlate_ctx ctx;
5209 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5210 const struct rule_dpif *rule)
5212 ds_put_char_multiple(result, '\t', level);
5214 ds_put_cstr(result, "No match\n");
5218 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5219 table_id, ntohll(rule->up.flow_cookie));
5220 cls_rule_format(&rule->up.cr, result);
5221 ds_put_char(result, '\n');
5223 ds_put_char_multiple(result, '\t', level);
5224 ds_put_cstr(result, "OpenFlow ");
5225 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5226 ds_put_char(result, '\n');
5230 trace_format_flow(struct ds *result, int level, const char *title,
5231 struct ofproto_trace *trace)
5233 ds_put_char_multiple(result, '\t', level);
5234 ds_put_format(result, "%s: ", title);
5235 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5236 ds_put_cstr(result, "unchanged");
5238 flow_format(result, &trace->ctx.flow);
5239 trace->flow = trace->ctx.flow;
5241 ds_put_char(result, '\n');
5245 trace_format_regs(struct ds *result, int level, const char *title,
5246 struct ofproto_trace *trace)
5250 ds_put_char_multiple(result, '\t', level);
5251 ds_put_format(result, "%s:", title);
5252 for (i = 0; i < FLOW_N_REGS; i++) {
5253 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5255 ds_put_char(result, '\n');
5259 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5261 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5262 struct ds *result = trace->result;
5264 ds_put_char(result, '\n');
5265 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5266 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5267 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5271 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5272 void *aux OVS_UNUSED)
5274 char *dpname, *arg1, *arg2, *arg3, *arg4;
5275 char *args = xstrdup(args_);
5276 char *save_ptr = NULL;
5277 struct ofproto_dpif *ofproto;
5278 struct ofpbuf odp_key;
5279 struct ofpbuf *packet;
5280 struct rule_dpif *rule;
5286 ofpbuf_init(&odp_key, 0);
5289 dpname = strtok_r(args, " ", &save_ptr);
5290 arg1 = strtok_r(NULL, " ", &save_ptr);
5291 arg2 = strtok_r(NULL, " ", &save_ptr);
5292 arg3 = strtok_r(NULL, " ", &save_ptr);
5293 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5294 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5295 /* ofproto/trace dpname flow [-generate] */
5298 /* Convert string to datapath key. */
5299 ofpbuf_init(&odp_key, 0);
5300 error = odp_flow_key_from_string(arg1, &odp_key);
5302 unixctl_command_reply(conn, 501, "Bad flow syntax");
5306 /* Convert odp_key to flow. */
5307 error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
5309 unixctl_command_reply(conn, 501, "Invalid flow");
5313 /* Generate a packet, if requested. */
5315 packet = ofpbuf_new(0);
5316 flow_compose(packet, &flow);
5318 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5319 /* ofproto/trace dpname priority tun_id in_port packet */
5324 priority = atoi(arg1);
5325 tun_id = htonll(strtoull(arg2, NULL, 0));
5326 in_port = ofp_port_to_odp_port(atoi(arg3));
5328 packet = ofpbuf_new(strlen(args) / 2);
5329 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5330 arg4 += strspn(arg4, " ");
5331 if (*arg4 != '\0') {
5332 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5335 if (packet->size < ETH_HEADER_LEN) {
5336 unixctl_command_reply(conn, 501,
5337 "Packet data too short for Ethernet");
5341 ds_put_cstr(&result, "Packet: ");
5342 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5343 ds_put_cstr(&result, s);
5346 flow_extract(packet, priority, tun_id, in_port, &flow);
5348 unixctl_command_reply(conn, 501, "Bad command syntax");
5352 ofproto = ofproto_dpif_lookup(dpname);
5354 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5359 ds_put_cstr(&result, "Flow: ");
5360 flow_format(&result, &flow);
5361 ds_put_char(&result, '\n');
5363 rule = rule_dpif_lookup(ofproto, &flow, 0);
5364 trace_format_rule(&result, 0, 0, rule);
5366 struct ofproto_trace trace;
5367 struct ofpbuf *odp_actions;
5369 trace.result = &result;
5371 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet);
5372 trace.ctx.resubmit_hook = trace_resubmit;
5373 odp_actions = xlate_actions(&trace.ctx,
5374 rule->up.actions, rule->up.n_actions);
5376 ds_put_char(&result, '\n');
5377 trace_format_flow(&result, 0, "Final flow", &trace);
5378 ds_put_cstr(&result, "Datapath actions: ");
5379 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5380 ofpbuf_delete(odp_actions);
5382 if (!trace.ctx.may_set_up_flow) {
5384 ds_put_cstr(&result, "\nThis flow is not cachable.");
5386 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5387 "for complete actions, please supply a packet.");
5392 unixctl_command_reply(conn, 200, ds_cstr(&result));
5395 ds_destroy(&result);
5396 ofpbuf_delete(packet);
5397 ofpbuf_uninit(&odp_key);
5402 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5403 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5406 unixctl_command_reply(conn, 200, NULL);
5410 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5411 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5414 unixctl_command_reply(conn, 200, NULL);
5418 ofproto_dpif_unixctl_init(void)
5420 static bool registered;
5426 unixctl_command_register("ofproto/trace",
5427 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5428 ofproto_unixctl_trace, NULL);
5429 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5431 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5433 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5434 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5437 const struct ofproto_class ofproto_dpif_class = {
5464 port_is_lacp_current,
5465 NULL, /* rule_choose_table */
5472 rule_modify_actions,
5480 get_cfm_remote_mpids,
5484 get_stp_port_status,
5489 is_mirror_output_bundle,
5490 forward_bpdu_changed,