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 subfacet.
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 static void flow_push_stats(const struct rule_dpif *, const struct flow *,
110 uint64_t packets, uint64_t bytes,
113 static uint32_t rule_calculate_tag(const struct flow *,
114 const struct flow_wildcards *,
116 static void rule_invalidate(const struct rule_dpif *);
118 #define MAX_MIRRORS 32
119 typedef uint32_t mirror_mask_t;
120 #define MIRROR_MASK_C(X) UINT32_C(X)
121 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
123 struct ofproto_dpif *ofproto; /* Owning ofproto. */
124 size_t idx; /* In ofproto's "mirrors" array. */
125 void *aux; /* Key supplied by ofproto's client. */
126 char *name; /* Identifier for log messages. */
128 /* Selection criteria. */
129 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
130 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
131 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
133 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
134 struct ofbundle *out; /* Output port or NULL. */
135 int out_vlan; /* Output VLAN or -1. */
136 mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */
139 int64_t packet_count; /* Number of packets sent. */
140 int64_t byte_count; /* Number of bytes sent. */
143 static void mirror_destroy(struct ofmirror *);
144 static void update_mirror_stats(struct ofproto_dpif *ofproto,
145 mirror_mask_t mirrors,
146 uint64_t packets, uint64_t bytes);
149 struct ofproto_dpif *ofproto; /* Owning ofproto. */
150 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
151 void *aux; /* Key supplied by ofproto's client. */
152 char *name; /* Identifier for log messages. */
155 struct list ports; /* Contains "struct ofport"s. */
156 enum port_vlan_mode vlan_mode; /* VLAN mode */
157 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
158 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
159 * NULL if all VLANs are trunked. */
160 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
161 struct bond *bond; /* Nonnull iff more than one port. */
162 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
165 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
167 /* Port mirroring info. */
168 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
169 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
170 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
173 static void bundle_remove(struct ofport *);
174 static void bundle_update(struct ofbundle *);
175 static void bundle_destroy(struct ofbundle *);
176 static void bundle_del_port(struct ofport_dpif *);
177 static void bundle_run(struct ofbundle *);
178 static void bundle_wait(struct ofbundle *);
179 static struct ofport_dpif *lookup_input_bundle(struct ofproto_dpif *,
180 uint16_t in_port, bool warn);
182 static void stp_run(struct ofproto_dpif *ofproto);
183 static void stp_wait(struct ofproto_dpif *ofproto);
185 static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan);
187 struct action_xlate_ctx {
188 /* action_xlate_ctx_init() initializes these members. */
191 struct ofproto_dpif *ofproto;
193 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
194 * this flow when actions change header fields. */
197 /* The packet corresponding to 'flow', or a null pointer if we are
198 * revalidating without a packet to refer to. */
199 const struct ofpbuf *packet;
201 /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We
202 * want to execute them if we are actually processing a packet, or if we
203 * are accounting for packets that the datapath has processed, but not if
204 * we are just revalidating. */
207 /* If nonnull, called just before executing a resubmit action.
209 * This is normally null so the client has to set it manually after
210 * calling action_xlate_ctx_init(). */
211 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
213 /* xlate_actions() initializes and uses these members. The client might want
214 * to look at them after it returns. */
216 struct ofpbuf *odp_actions; /* Datapath actions. */
217 tag_type tags; /* Tags associated with actions. */
218 bool may_set_up_flow; /* True ordinarily; false if the actions must
219 * be reassessed for every packet. */
220 bool has_learn; /* Actions include NXAST_LEARN? */
221 bool has_normal; /* Actions output to OFPP_NORMAL? */
222 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
223 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
225 /* xlate_actions() initializes and uses these members, but the client has no
226 * reason to look at them. */
228 int recurse; /* Recursion level, via xlate_table_action. */
229 struct flow base_flow; /* Flow at the last commit. */
230 uint32_t original_priority; /* Priority when packet arrived. */
231 uint8_t table_id; /* OpenFlow table ID where flow was found. */
232 uint32_t sflow_n_outputs; /* Number of output ports. */
233 uint16_t sflow_odp_port; /* Output port for composing sFlow action. */
234 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
235 bool exit; /* No further actions should be processed. */
238 static void action_xlate_ctx_init(struct action_xlate_ctx *,
239 struct ofproto_dpif *, const struct flow *,
240 ovs_be16 initial_tci, const struct ofpbuf *);
241 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
242 const union ofp_action *in, size_t n_in);
244 /* An exact-match instantiation of an OpenFlow flow.
246 * A facet associates a "struct flow", which represents the Open vSwitch
247 * userspace idea of an exact-match flow, with one or more subfacets. Each
248 * subfacet tracks the datapath's idea of the exact-match flow equivalent to
249 * the facet. When the kernel module (or other dpif implementation) and Open
250 * vSwitch userspace agree on the definition of a flow key, there is exactly
251 * one subfacet per facet. If the dpif implementation supports more-specific
252 * flow matching than userspace, however, a facet can have more than one
253 * subfacet, each of which corresponds to some distinction in flow that
254 * userspace simply doesn't understand.
256 * Flow expiration works in terms of subfacets, so a facet must have at least
257 * one subfacet or it will never expire, leaking memory. */
260 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
261 struct list list_node; /* In owning rule's 'facets' list. */
262 struct rule_dpif *rule; /* Owning rule. */
265 struct list subfacets;
266 long long int used; /* Time last used; time created if not used. */
273 * - Do include packets and bytes sent "by hand", e.g. with
276 * - Do include packets and bytes that were obtained from the datapath
277 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
278 * DPIF_FP_ZERO_STATS).
280 * - Do not include packets or bytes that can be obtained from the
281 * datapath for any existing subfacet.
283 uint64_t packet_count; /* Number of packets received. */
284 uint64_t byte_count; /* Number of bytes received. */
286 /* Resubmit statistics. */
287 uint64_t prev_packet_count; /* Number of packets from last stats push. */
288 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
289 long long int prev_used; /* Used time from last stats push. */
292 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
293 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
295 /* Properties of datapath actions.
297 * Every subfacet has its own actions because actions can differ slightly
298 * between splintered and non-splintered subfacets due to the VLAN tag
299 * being initially different (present vs. absent). All of them have these
300 * properties in common so we just store one copy of them here. */
301 bool may_install; /* Reassess actions for every packet? */
302 bool has_learn; /* Actions include NXAST_LEARN? */
303 bool has_normal; /* Actions output to OFPP_NORMAL? */
304 tag_type tags; /* Tags that would require revalidation. */
305 mirror_mask_t mirrors; /* Bitmap of dependent mirrors. */
308 static struct facet *facet_create(struct rule_dpif *, const struct flow *);
309 static void facet_remove(struct ofproto_dpif *, struct facet *);
310 static void facet_free(struct facet *);
312 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
313 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
314 const struct flow *);
315 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
317 static bool execute_controller_action(struct ofproto_dpif *,
319 const struct nlattr *odp_actions,
321 struct ofpbuf *packet);
323 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
325 static void facet_update_time(struct ofproto_dpif *, struct facet *,
327 static void facet_reset_counters(struct facet *);
328 static void facet_push_stats(struct facet *);
329 static void facet_account(struct ofproto_dpif *, struct facet *);
331 static bool facet_is_controller_flow(struct facet *);
333 /* A dpif flow and actions associated with a facet.
335 * See also the large comment on struct facet. */
338 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
339 struct list list_node; /* In struct facet's 'facets' list. */
340 struct facet *facet; /* Owning facet. */
344 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
345 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
346 * regenerate the ODP flow key from ->facet->flow. */
347 enum odp_key_fitness key_fitness;
351 long long int used; /* Time last used; time created if not used. */
353 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
354 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
358 * These should be essentially identical for every subfacet in a facet, but
359 * may differ in trivial ways due to VLAN splinters. */
360 size_t actions_len; /* Number of bytes in actions[]. */
361 struct nlattr *actions; /* Datapath actions. */
363 bool installed; /* Installed in datapath? */
365 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
366 * splinters can cause it to differ. This value should be removed when
367 * the VLAN splinters feature is no longer needed. */
368 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
371 static struct subfacet *subfacet_create(struct ofproto_dpif *, struct facet *,
372 enum odp_key_fitness,
373 const struct nlattr *key,
374 size_t key_len, ovs_be16 initial_tci);
375 static struct subfacet *subfacet_find(struct ofproto_dpif *,
376 const struct nlattr *key, size_t key_len);
377 static void subfacet_destroy(struct ofproto_dpif *, struct subfacet *);
378 static void subfacet_destroy__(struct ofproto_dpif *, struct subfacet *);
379 static void subfacet_reset_dp_stats(struct subfacet *,
380 struct dpif_flow_stats *);
381 static void subfacet_update_time(struct ofproto_dpif *, struct subfacet *,
383 static void subfacet_update_stats(struct ofproto_dpif *, struct subfacet *,
384 const struct dpif_flow_stats *);
385 static void subfacet_make_actions(struct ofproto_dpif *, struct subfacet *,
386 const struct ofpbuf *packet);
387 static int subfacet_install(struct ofproto_dpif *, struct subfacet *,
388 const struct nlattr *actions, size_t actions_len,
389 struct dpif_flow_stats *);
390 static void subfacet_uninstall(struct ofproto_dpif *, struct subfacet *);
396 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
397 struct list bundle_node; /* In struct ofbundle's "ports" list. */
398 struct cfm *cfm; /* Connectivity Fault Management, if any. */
399 tag_type tag; /* Tag associated with this port. */
400 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
401 bool may_enable; /* May be enabled in bonds. */
404 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
405 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
406 long long int stp_state_entered;
408 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
410 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
412 * This is deprecated. It is only for compatibility with broken device
413 * drivers in old versions of Linux that do not properly support VLANs when
414 * VLAN devices are not used. When broken device drivers are no longer in
415 * widespread use, we will delete these interfaces. */
416 uint16_t realdev_ofp_port;
420 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
421 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
422 * traffic egressing the 'ofport' with that priority should be marked with. */
423 struct priority_to_dscp {
424 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
425 uint32_t priority; /* Priority of this queue (see struct flow). */
427 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
430 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
432 * This is deprecated. It is only for compatibility with broken device drivers
433 * in old versions of Linux that do not properly support VLANs when VLAN
434 * devices are not used. When broken device drivers are no longer in
435 * widespread use, we will delete these interfaces. */
436 struct vlan_splinter {
437 struct hmap_node realdev_vid_node;
438 struct hmap_node vlandev_node;
439 uint16_t realdev_ofp_port;
440 uint16_t vlandev_ofp_port;
444 static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
445 uint32_t realdev, ovs_be16 vlan_tci);
446 static uint16_t vsp_vlandev_to_realdev(const struct ofproto_dpif *,
447 uint16_t vlandev, int *vid);
448 static void vsp_remove(struct ofport_dpif *);
449 static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
451 static struct ofport_dpif *
452 ofport_dpif_cast(const struct ofport *ofport)
454 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
455 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
458 static void port_run(struct ofport_dpif *);
459 static void port_wait(struct ofport_dpif *);
460 static int set_cfm(struct ofport *, const struct cfm_settings *);
461 static void ofport_clear_priorities(struct ofport_dpif *);
463 struct dpif_completion {
464 struct list list_node;
465 struct ofoperation *op;
468 /* Extra information about a classifier table.
469 * Currently used just for optimized flow revalidation. */
471 /* If either of these is nonnull, then this table has a form that allows
472 * flows to be tagged to avoid revalidating most flows for the most common
473 * kinds of flow table changes. */
474 struct cls_table *catchall_table; /* Table that wildcards all fields. */
475 struct cls_table *other_table; /* Table with any other wildcard set. */
476 uint32_t basis; /* Keeps each table's tags separate. */
479 struct ofproto_dpif {
488 struct netflow *netflow;
489 struct dpif_sflow *sflow;
490 struct hmap bundles; /* Contains "struct ofbundle"s. */
491 struct mac_learning *ml;
492 struct ofmirror *mirrors[MAX_MIRRORS];
493 bool has_bonded_bundles;
496 struct timer next_expiration;
500 struct hmap subfacets;
503 struct table_dpif tables[N_TABLES];
504 bool need_revalidate;
505 struct tag_set revalidate_set;
507 /* Support for debugging async flow mods. */
508 struct list completions;
510 bool has_bundle_action; /* True when the first bundle action appears. */
514 long long int stp_last_tick;
516 /* VLAN splinters. */
517 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
518 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
521 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
522 * for debugging the asynchronous flow_mod implementation.) */
525 static void ofproto_dpif_unixctl_init(void);
527 static struct ofproto_dpif *
528 ofproto_dpif_cast(const struct ofproto *ofproto)
530 assert(ofproto->ofproto_class == &ofproto_dpif_class);
531 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
534 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
536 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
539 /* Packet processing. */
540 static void update_learning_table(struct ofproto_dpif *,
541 const struct flow *, int vlan,
544 #define FLOW_MISS_MAX_BATCH 50
545 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
547 /* Flow expiration. */
548 static int expire(struct ofproto_dpif *);
551 static void send_netflow_active_timeouts(struct ofproto_dpif *);
554 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
556 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
557 const struct flow *, uint32_t odp_port);
558 static void add_mirror_actions(struct action_xlate_ctx *ctx,
559 const struct flow *flow);
560 /* Global variables. */
561 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
563 /* Factory functions. */
566 enumerate_types(struct sset *types)
568 dp_enumerate_types(types);
572 enumerate_names(const char *type, struct sset *names)
574 return dp_enumerate_names(type, names);
578 del(const char *type, const char *name)
583 error = dpif_open(name, type, &dpif);
585 error = dpif_delete(dpif);
591 /* Basic life-cycle. */
593 static struct ofproto *
596 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
601 dealloc(struct ofproto *ofproto_)
603 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
608 construct(struct ofproto *ofproto_, int *n_tablesp)
610 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
611 const char *name = ofproto->up.name;
615 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
617 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
621 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
622 ofproto->n_matches = 0;
624 dpif_flow_flush(ofproto->dpif);
625 dpif_recv_purge(ofproto->dpif);
627 error = dpif_recv_set_mask(ofproto->dpif,
628 ((1u << DPIF_UC_MISS) |
629 (1u << DPIF_UC_ACTION)));
631 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
632 dpif_close(ofproto->dpif);
636 ofproto->netflow = NULL;
637 ofproto->sflow = NULL;
639 hmap_init(&ofproto->bundles);
640 ofproto->ml = mac_learning_create();
641 for (i = 0; i < MAX_MIRRORS; i++) {
642 ofproto->mirrors[i] = NULL;
644 ofproto->has_bonded_bundles = false;
646 timer_set_duration(&ofproto->next_expiration, 1000);
648 hmap_init(&ofproto->facets);
649 hmap_init(&ofproto->subfacets);
651 for (i = 0; i < N_TABLES; i++) {
652 struct table_dpif *table = &ofproto->tables[i];
654 table->catchall_table = NULL;
655 table->other_table = NULL;
656 table->basis = random_uint32();
658 ofproto->need_revalidate = false;
659 tag_set_init(&ofproto->revalidate_set);
661 list_init(&ofproto->completions);
663 ofproto_dpif_unixctl_init();
665 ofproto->has_bundle_action = false;
667 hmap_init(&ofproto->vlandev_map);
668 hmap_init(&ofproto->realdev_vid_map);
670 *n_tablesp = N_TABLES;
675 complete_operations(struct ofproto_dpif *ofproto)
677 struct dpif_completion *c, *next;
679 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
680 ofoperation_complete(c->op, 0);
681 list_remove(&c->list_node);
687 destruct(struct ofproto *ofproto_)
689 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
690 struct rule_dpif *rule, *next_rule;
691 struct classifier *table;
694 complete_operations(ofproto);
696 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
697 struct cls_cursor cursor;
699 cls_cursor_init(&cursor, table, NULL);
700 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
701 ofproto_rule_destroy(&rule->up);
705 for (i = 0; i < MAX_MIRRORS; i++) {
706 mirror_destroy(ofproto->mirrors[i]);
709 netflow_destroy(ofproto->netflow);
710 dpif_sflow_destroy(ofproto->sflow);
711 hmap_destroy(&ofproto->bundles);
712 mac_learning_destroy(ofproto->ml);
714 hmap_destroy(&ofproto->facets);
715 hmap_destroy(&ofproto->subfacets);
717 hmap_destroy(&ofproto->vlandev_map);
718 hmap_destroy(&ofproto->realdev_vid_map);
720 dpif_close(ofproto->dpif);
724 run_fast(struct ofproto *ofproto_)
726 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
729 /* Handle one or more batches of upcalls, until there's nothing left to do
730 * or until we do a fixed total amount of work.
732 * We do work in batches because it can be much cheaper to set up a number
733 * of flows and fire off their patches all at once. We do multiple batches
734 * because in some cases handling a packet can cause another packet to be
735 * queued almost immediately as part of the return flow. Both
736 * optimizations can make major improvements on some benchmarks and
737 * presumably for real traffic as well. */
739 while (work < FLOW_MISS_MAX_BATCH) {
740 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
750 run(struct ofproto *ofproto_)
752 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
753 struct ofport_dpif *ofport;
754 struct ofbundle *bundle;
758 complete_operations(ofproto);
760 dpif_run(ofproto->dpif);
762 error = run_fast(ofproto_);
767 if (timer_expired(&ofproto->next_expiration)) {
768 int delay = expire(ofproto);
769 timer_set_duration(&ofproto->next_expiration, delay);
772 if (ofproto->netflow) {
773 if (netflow_run(ofproto->netflow)) {
774 send_netflow_active_timeouts(ofproto);
777 if (ofproto->sflow) {
778 dpif_sflow_run(ofproto->sflow);
781 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
784 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
789 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
791 /* Now revalidate if there's anything to do. */
792 if (ofproto->need_revalidate
793 || !tag_set_is_empty(&ofproto->revalidate_set)) {
794 struct tag_set revalidate_set = ofproto->revalidate_set;
795 bool revalidate_all = ofproto->need_revalidate;
796 struct facet *facet, *next;
798 /* Clear the revalidation flags. */
799 tag_set_init(&ofproto->revalidate_set);
800 ofproto->need_revalidate = false;
802 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
804 || tag_set_intersects(&revalidate_set, facet->tags)) {
805 facet_revalidate(ofproto, facet);
814 wait(struct ofproto *ofproto_)
816 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
817 struct ofport_dpif *ofport;
818 struct ofbundle *bundle;
820 if (!clogged && !list_is_empty(&ofproto->completions)) {
821 poll_immediate_wake();
824 dpif_wait(ofproto->dpif);
825 dpif_recv_wait(ofproto->dpif);
826 if (ofproto->sflow) {
827 dpif_sflow_wait(ofproto->sflow);
829 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
830 poll_immediate_wake();
832 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
835 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
838 if (ofproto->netflow) {
839 netflow_wait(ofproto->netflow);
841 mac_learning_wait(ofproto->ml);
843 if (ofproto->need_revalidate) {
844 /* Shouldn't happen, but if it does just go around again. */
845 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
846 poll_immediate_wake();
848 timer_wait(&ofproto->next_expiration);
853 flush(struct ofproto *ofproto_)
855 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
856 struct facet *facet, *next_facet;
858 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
859 /* Mark the facet as not installed so that facet_remove() doesn't
860 * bother trying to uninstall it. There is no point in uninstalling it
861 * individually since we are about to blow away all the facets with
862 * dpif_flow_flush(). */
863 struct subfacet *subfacet;
865 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
866 subfacet->installed = false;
867 subfacet->dp_packet_count = 0;
868 subfacet->dp_byte_count = 0;
870 facet_remove(ofproto, facet);
872 dpif_flow_flush(ofproto->dpif);
876 get_features(struct ofproto *ofproto_ OVS_UNUSED,
877 bool *arp_match_ip, uint32_t *actions)
879 *arp_match_ip = true;
880 *actions = ((1u << OFPAT_OUTPUT) |
881 (1u << OFPAT_SET_VLAN_VID) |
882 (1u << OFPAT_SET_VLAN_PCP) |
883 (1u << OFPAT_STRIP_VLAN) |
884 (1u << OFPAT_SET_DL_SRC) |
885 (1u << OFPAT_SET_DL_DST) |
886 (1u << OFPAT_SET_NW_SRC) |
887 (1u << OFPAT_SET_NW_DST) |
888 (1u << OFPAT_SET_NW_TOS) |
889 (1u << OFPAT_SET_TP_SRC) |
890 (1u << OFPAT_SET_TP_DST) |
891 (1u << OFPAT_ENQUEUE));
895 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
897 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
898 struct dpif_dp_stats s;
900 strcpy(ots->name, "classifier");
902 dpif_get_dp_stats(ofproto->dpif, &s);
903 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
904 put_32aligned_be64(&ots->matched_count,
905 htonll(s.n_hit + ofproto->n_matches));
908 static struct ofport *
911 struct ofport_dpif *port = xmalloc(sizeof *port);
916 port_dealloc(struct ofport *port_)
918 struct ofport_dpif *port = ofport_dpif_cast(port_);
923 port_construct(struct ofport *port_)
925 struct ofport_dpif *port = ofport_dpif_cast(port_);
926 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
928 ofproto->need_revalidate = true;
929 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
932 port->tag = tag_create_random();
933 port->may_enable = true;
934 port->stp_port = NULL;
935 port->stp_state = STP_DISABLED;
936 hmap_init(&port->priorities);
937 port->realdev_ofp_port = 0;
938 port->vlandev_vid = 0;
940 if (ofproto->sflow) {
941 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
942 netdev_get_name(port->up.netdev));
949 port_destruct(struct ofport *port_)
951 struct ofport_dpif *port = ofport_dpif_cast(port_);
952 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
954 ofproto->need_revalidate = true;
955 bundle_remove(port_);
956 set_cfm(port_, NULL);
957 if (ofproto->sflow) {
958 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
961 ofport_clear_priorities(port);
962 hmap_destroy(&port->priorities);
966 port_modified(struct ofport *port_)
968 struct ofport_dpif *port = ofport_dpif_cast(port_);
970 if (port->bundle && port->bundle->bond) {
971 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
976 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
978 struct ofport_dpif *port = ofport_dpif_cast(port_);
979 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
980 ovs_be32 changed = old_config ^ port->up.opp.config;
982 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
983 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
984 ofproto->need_revalidate = true;
986 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
987 bundle_update(port->bundle);
993 set_sflow(struct ofproto *ofproto_,
994 const struct ofproto_sflow_options *sflow_options)
996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
997 struct dpif_sflow *ds = ofproto->sflow;
1001 struct ofport_dpif *ofport;
1003 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
1004 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1005 dpif_sflow_add_port(ds, ofport->odp_port,
1006 netdev_get_name(ofport->up.netdev));
1008 ofproto->need_revalidate = true;
1010 dpif_sflow_set_options(ds, sflow_options);
1013 dpif_sflow_destroy(ds);
1014 ofproto->need_revalidate = true;
1015 ofproto->sflow = NULL;
1022 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1024 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1031 struct ofproto_dpif *ofproto;
1033 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1034 ofproto->need_revalidate = true;
1035 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
1038 if (cfm_configure(ofport->cfm, s)) {
1044 cfm_destroy(ofport->cfm);
1050 get_cfm_fault(const struct ofport *ofport_)
1052 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1054 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
1058 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1061 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1064 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1071 /* Spanning Tree. */
1074 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1076 struct ofproto_dpif *ofproto = ofproto_;
1077 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1078 struct ofport_dpif *ofport;
1080 ofport = stp_port_get_aux(sp);
1082 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1083 ofproto->up.name, port_num);
1085 struct eth_header *eth = pkt->l2;
1087 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1088 if (eth_addr_is_zero(eth->eth_src)) {
1089 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1090 "with unknown MAC", ofproto->up.name, port_num);
1092 send_packet(ofport, pkt);
1098 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1100 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1102 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1104 /* Only revalidate flows if the configuration changed. */
1105 if (!s != !ofproto->stp) {
1106 ofproto->need_revalidate = true;
1110 if (!ofproto->stp) {
1111 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1112 send_bpdu_cb, ofproto);
1113 ofproto->stp_last_tick = time_msec();
1116 stp_set_bridge_id(ofproto->stp, s->system_id);
1117 stp_set_bridge_priority(ofproto->stp, s->priority);
1118 stp_set_hello_time(ofproto->stp, s->hello_time);
1119 stp_set_max_age(ofproto->stp, s->max_age);
1120 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1122 stp_destroy(ofproto->stp);
1123 ofproto->stp = NULL;
1130 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1132 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1136 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1137 s->designated_root = stp_get_designated_root(ofproto->stp);
1138 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1147 update_stp_port_state(struct ofport_dpif *ofport)
1149 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1150 enum stp_state state;
1152 /* Figure out new state. */
1153 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1157 if (ofport->stp_state != state) {
1161 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1162 netdev_get_name(ofport->up.netdev),
1163 stp_state_name(ofport->stp_state),
1164 stp_state_name(state));
1165 if (stp_learn_in_state(ofport->stp_state)
1166 != stp_learn_in_state(state)) {
1167 /* xxx Learning action flows should also be flushed. */
1168 mac_learning_flush(ofproto->ml);
1170 fwd_change = stp_forward_in_state(ofport->stp_state)
1171 != stp_forward_in_state(state);
1173 ofproto->need_revalidate = true;
1174 ofport->stp_state = state;
1175 ofport->stp_state_entered = time_msec();
1177 if (fwd_change && ofport->bundle) {
1178 bundle_update(ofport->bundle);
1181 /* Update the STP state bits in the OpenFlow port description. */
1182 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1183 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1184 : state == STP_LEARNING ? OFPPS_STP_LEARN
1185 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1186 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1188 ofproto_port_set_state(&ofport->up, of_state);
1192 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1193 * caller is responsible for assigning STP port numbers and ensuring
1194 * there are no duplicates. */
1196 set_stp_port(struct ofport *ofport_,
1197 const struct ofproto_port_stp_settings *s)
1199 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1200 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1201 struct stp_port *sp = ofport->stp_port;
1203 if (!s || !s->enable) {
1205 ofport->stp_port = NULL;
1206 stp_port_disable(sp);
1207 update_stp_port_state(ofport);
1210 } else if (sp && stp_port_no(sp) != s->port_num
1211 && ofport == stp_port_get_aux(sp)) {
1212 /* The port-id changed, so disable the old one if it's not
1213 * already in use by another port. */
1214 stp_port_disable(sp);
1217 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1218 stp_port_enable(sp);
1220 stp_port_set_aux(sp, ofport);
1221 stp_port_set_priority(sp, s->priority);
1222 stp_port_set_path_cost(sp, s->path_cost);
1224 update_stp_port_state(ofport);
1230 get_stp_port_status(struct ofport *ofport_,
1231 struct ofproto_port_stp_status *s)
1233 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1234 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1235 struct stp_port *sp = ofport->stp_port;
1237 if (!ofproto->stp || !sp) {
1243 s->port_id = stp_port_get_id(sp);
1244 s->state = stp_port_get_state(sp);
1245 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1246 s->role = stp_port_get_role(sp);
1247 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1253 stp_run(struct ofproto_dpif *ofproto)
1256 long long int now = time_msec();
1257 long long int elapsed = now - ofproto->stp_last_tick;
1258 struct stp_port *sp;
1261 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1262 ofproto->stp_last_tick = now;
1264 while (stp_get_changed_port(ofproto->stp, &sp)) {
1265 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1268 update_stp_port_state(ofport);
1275 stp_wait(struct ofproto_dpif *ofproto)
1278 poll_timer_wait(1000);
1282 /* Returns true if STP should process 'flow'. */
1284 stp_should_process_flow(const struct flow *flow)
1286 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1290 stp_process_packet(const struct ofport_dpif *ofport,
1291 const struct ofpbuf *packet)
1293 struct ofpbuf payload = *packet;
1294 struct eth_header *eth = payload.data;
1295 struct stp_port *sp = ofport->stp_port;
1297 /* Sink packets on ports that have STP disabled when the bridge has
1299 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1303 /* Trim off padding on payload. */
1304 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1305 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1308 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1309 stp_received_bpdu(sp, payload.data, payload.size);
1313 static struct priority_to_dscp *
1314 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1316 struct priority_to_dscp *pdscp;
1319 hash = hash_int(priority, 0);
1320 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1321 if (pdscp->priority == priority) {
1329 ofport_clear_priorities(struct ofport_dpif *ofport)
1331 struct priority_to_dscp *pdscp, *next;
1333 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1334 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1340 set_queues(struct ofport *ofport_,
1341 const struct ofproto_port_queue *qdscp_list,
1344 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1345 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1346 struct hmap new = HMAP_INITIALIZER(&new);
1349 for (i = 0; i < n_qdscp; i++) {
1350 struct priority_to_dscp *pdscp;
1354 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1355 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1360 pdscp = get_priority(ofport, priority);
1362 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1364 pdscp = xmalloc(sizeof *pdscp);
1365 pdscp->priority = priority;
1367 ofproto->need_revalidate = true;
1370 if (pdscp->dscp != dscp) {
1372 ofproto->need_revalidate = true;
1375 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1378 if (!hmap_is_empty(&ofport->priorities)) {
1379 ofport_clear_priorities(ofport);
1380 ofproto->need_revalidate = true;
1383 hmap_swap(&new, &ofport->priorities);
1391 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1392 * to revalidate every flow. */
1394 bundle_flush_macs(struct ofbundle *bundle)
1396 struct ofproto_dpif *ofproto = bundle->ofproto;
1397 struct mac_learning *ml = ofproto->ml;
1398 struct mac_entry *mac, *next_mac;
1400 ofproto->need_revalidate = true;
1401 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1402 if (mac->port.p == bundle) {
1403 mac_learning_expire(ml, mac);
1408 static struct ofbundle *
1409 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1411 struct ofbundle *bundle;
1413 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1414 &ofproto->bundles) {
1415 if (bundle->aux == aux) {
1422 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1423 * ones that are found to 'bundles'. */
1425 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1426 void **auxes, size_t n_auxes,
1427 struct hmapx *bundles)
1431 hmapx_init(bundles);
1432 for (i = 0; i < n_auxes; i++) {
1433 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1435 hmapx_add(bundles, bundle);
1441 bundle_update(struct ofbundle *bundle)
1443 struct ofport_dpif *port;
1445 bundle->floodable = true;
1446 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1447 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1448 bundle->floodable = false;
1455 bundle_del_port(struct ofport_dpif *port)
1457 struct ofbundle *bundle = port->bundle;
1459 bundle->ofproto->need_revalidate = true;
1461 list_remove(&port->bundle_node);
1462 port->bundle = NULL;
1465 lacp_slave_unregister(bundle->lacp, port);
1468 bond_slave_unregister(bundle->bond, port);
1471 bundle_update(bundle);
1475 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1476 struct lacp_slave_settings *lacp,
1477 uint32_t bond_stable_id)
1479 struct ofport_dpif *port;
1481 port = get_ofp_port(bundle->ofproto, ofp_port);
1486 if (port->bundle != bundle) {
1487 bundle->ofproto->need_revalidate = true;
1489 bundle_del_port(port);
1492 port->bundle = bundle;
1493 list_push_back(&bundle->ports, &port->bundle_node);
1494 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1495 bundle->floodable = false;
1499 port->bundle->ofproto->need_revalidate = true;
1500 lacp_slave_register(bundle->lacp, port, lacp);
1503 port->bond_stable_id = bond_stable_id;
1509 bundle_destroy(struct ofbundle *bundle)
1511 struct ofproto_dpif *ofproto;
1512 struct ofport_dpif *port, *next_port;
1519 ofproto = bundle->ofproto;
1520 for (i = 0; i < MAX_MIRRORS; i++) {
1521 struct ofmirror *m = ofproto->mirrors[i];
1523 if (m->out == bundle) {
1525 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1526 || hmapx_find_and_delete(&m->dsts, bundle)) {
1527 ofproto->need_revalidate = true;
1532 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1533 bundle_del_port(port);
1536 bundle_flush_macs(bundle);
1537 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1539 free(bundle->trunks);
1540 lacp_destroy(bundle->lacp);
1541 bond_destroy(bundle->bond);
1546 bundle_set(struct ofproto *ofproto_, void *aux,
1547 const struct ofproto_bundle_settings *s)
1549 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1550 bool need_flush = false;
1551 struct ofport_dpif *port;
1552 struct ofbundle *bundle;
1553 unsigned long *trunks;
1559 bundle_destroy(bundle_lookup(ofproto, aux));
1563 assert(s->n_slaves == 1 || s->bond != NULL);
1564 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1566 bundle = bundle_lookup(ofproto, aux);
1568 bundle = xmalloc(sizeof *bundle);
1570 bundle->ofproto = ofproto;
1571 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1572 hash_pointer(aux, 0));
1574 bundle->name = NULL;
1576 list_init(&bundle->ports);
1577 bundle->vlan_mode = PORT_VLAN_TRUNK;
1579 bundle->trunks = NULL;
1580 bundle->use_priority_tags = s->use_priority_tags;
1581 bundle->lacp = NULL;
1582 bundle->bond = NULL;
1584 bundle->floodable = true;
1586 bundle->src_mirrors = 0;
1587 bundle->dst_mirrors = 0;
1588 bundle->mirror_out = 0;
1591 if (!bundle->name || strcmp(s->name, bundle->name)) {
1593 bundle->name = xstrdup(s->name);
1598 if (!bundle->lacp) {
1599 ofproto->need_revalidate = true;
1600 bundle->lacp = lacp_create();
1602 lacp_configure(bundle->lacp, s->lacp);
1604 lacp_destroy(bundle->lacp);
1605 bundle->lacp = NULL;
1608 /* Update set of ports. */
1610 for (i = 0; i < s->n_slaves; i++) {
1611 if (!bundle_add_port(bundle, s->slaves[i],
1612 s->lacp ? &s->lacp_slaves[i] : NULL,
1613 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1617 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1618 struct ofport_dpif *next_port;
1620 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1621 for (i = 0; i < s->n_slaves; i++) {
1622 if (s->slaves[i] == port->up.ofp_port) {
1627 bundle_del_port(port);
1631 assert(list_size(&bundle->ports) <= s->n_slaves);
1633 if (list_is_empty(&bundle->ports)) {
1634 bundle_destroy(bundle);
1638 /* Set VLAN tagging mode */
1639 if (s->vlan_mode != bundle->vlan_mode
1640 || s->use_priority_tags != bundle->use_priority_tags) {
1641 bundle->vlan_mode = s->vlan_mode;
1642 bundle->use_priority_tags = s->use_priority_tags;
1647 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1648 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1650 if (vlan != bundle->vlan) {
1651 bundle->vlan = vlan;
1655 /* Get trunked VLANs. */
1656 switch (s->vlan_mode) {
1657 case PORT_VLAN_ACCESS:
1661 case PORT_VLAN_TRUNK:
1662 trunks = (unsigned long *) s->trunks;
1665 case PORT_VLAN_NATIVE_UNTAGGED:
1666 case PORT_VLAN_NATIVE_TAGGED:
1667 if (vlan != 0 && (!s->trunks
1668 || !bitmap_is_set(s->trunks, vlan)
1669 || bitmap_is_set(s->trunks, 0))) {
1670 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1672 trunks = bitmap_clone(s->trunks, 4096);
1674 trunks = bitmap_allocate1(4096);
1676 bitmap_set1(trunks, vlan);
1677 bitmap_set0(trunks, 0);
1679 trunks = (unsigned long *) s->trunks;
1686 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1687 free(bundle->trunks);
1688 if (trunks == s->trunks) {
1689 bundle->trunks = vlan_bitmap_clone(trunks);
1691 bundle->trunks = trunks;
1696 if (trunks != s->trunks) {
1701 if (!list_is_short(&bundle->ports)) {
1702 bundle->ofproto->has_bonded_bundles = true;
1704 if (bond_reconfigure(bundle->bond, s->bond)) {
1705 ofproto->need_revalidate = true;
1708 bundle->bond = bond_create(s->bond);
1709 ofproto->need_revalidate = true;
1712 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1713 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1717 bond_destroy(bundle->bond);
1718 bundle->bond = NULL;
1721 /* If we changed something that would affect MAC learning, un-learn
1722 * everything on this port and force flow revalidation. */
1724 bundle_flush_macs(bundle);
1731 bundle_remove(struct ofport *port_)
1733 struct ofport_dpif *port = ofport_dpif_cast(port_);
1734 struct ofbundle *bundle = port->bundle;
1737 bundle_del_port(port);
1738 if (list_is_empty(&bundle->ports)) {
1739 bundle_destroy(bundle);
1740 } else if (list_is_short(&bundle->ports)) {
1741 bond_destroy(bundle->bond);
1742 bundle->bond = NULL;
1748 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1750 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1751 struct ofport_dpif *port = port_;
1752 uint8_t ea[ETH_ADDR_LEN];
1755 error = netdev_get_etheraddr(port->up.netdev, ea);
1757 struct ofpbuf packet;
1760 ofpbuf_init(&packet, 0);
1761 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1763 memcpy(packet_pdu, pdu, pdu_size);
1765 send_packet(port, &packet);
1766 ofpbuf_uninit(&packet);
1768 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1769 "%s (%s)", port->bundle->name,
1770 netdev_get_name(port->up.netdev), strerror(error));
1775 bundle_send_learning_packets(struct ofbundle *bundle)
1777 struct ofproto_dpif *ofproto = bundle->ofproto;
1778 int error, n_packets, n_errors;
1779 struct mac_entry *e;
1781 error = n_packets = n_errors = 0;
1782 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1783 if (e->port.p != bundle) {
1784 struct ofpbuf *learning_packet;
1785 struct ofport_dpif *port;
1788 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1791 ret = send_packet(port, learning_packet);
1792 ofpbuf_delete(learning_packet);
1802 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1803 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1804 "packets, last error was: %s",
1805 bundle->name, n_errors, n_packets, strerror(error));
1807 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1808 bundle->name, n_packets);
1813 bundle_run(struct ofbundle *bundle)
1816 lacp_run(bundle->lacp, send_pdu_cb);
1819 struct ofport_dpif *port;
1821 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1822 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1825 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1826 lacp_negotiated(bundle->lacp));
1827 if (bond_should_send_learning_packets(bundle->bond)) {
1828 bundle_send_learning_packets(bundle);
1834 bundle_wait(struct ofbundle *bundle)
1837 lacp_wait(bundle->lacp);
1840 bond_wait(bundle->bond);
1847 mirror_scan(struct ofproto_dpif *ofproto)
1851 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1852 if (!ofproto->mirrors[idx]) {
1859 static struct ofmirror *
1860 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1864 for (i = 0; i < MAX_MIRRORS; i++) {
1865 struct ofmirror *mirror = ofproto->mirrors[i];
1866 if (mirror && mirror->aux == aux) {
1874 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1876 mirror_update_dups(struct ofproto_dpif *ofproto)
1880 for (i = 0; i < MAX_MIRRORS; i++) {
1881 struct ofmirror *m = ofproto->mirrors[i];
1884 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1888 for (i = 0; i < MAX_MIRRORS; i++) {
1889 struct ofmirror *m1 = ofproto->mirrors[i];
1896 for (j = i + 1; j < MAX_MIRRORS; j++) {
1897 struct ofmirror *m2 = ofproto->mirrors[j];
1899 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1900 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1901 m2->dup_mirrors |= m1->dup_mirrors;
1908 mirror_set(struct ofproto *ofproto_, void *aux,
1909 const struct ofproto_mirror_settings *s)
1911 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1912 mirror_mask_t mirror_bit;
1913 struct ofbundle *bundle;
1914 struct ofmirror *mirror;
1915 struct ofbundle *out;
1916 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1917 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1920 mirror = mirror_lookup(ofproto, aux);
1922 mirror_destroy(mirror);
1928 idx = mirror_scan(ofproto);
1930 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1932 ofproto->up.name, MAX_MIRRORS, s->name);
1936 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1937 mirror->ofproto = ofproto;
1940 mirror->out_vlan = -1;
1941 mirror->name = NULL;
1944 if (!mirror->name || strcmp(s->name, mirror->name)) {
1946 mirror->name = xstrdup(s->name);
1949 /* Get the new configuration. */
1950 if (s->out_bundle) {
1951 out = bundle_lookup(ofproto, s->out_bundle);
1953 mirror_destroy(mirror);
1959 out_vlan = s->out_vlan;
1961 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1962 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1964 /* If the configuration has not changed, do nothing. */
1965 if (hmapx_equals(&srcs, &mirror->srcs)
1966 && hmapx_equals(&dsts, &mirror->dsts)
1967 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1968 && mirror->out == out
1969 && mirror->out_vlan == out_vlan)
1971 hmapx_destroy(&srcs);
1972 hmapx_destroy(&dsts);
1976 hmapx_swap(&srcs, &mirror->srcs);
1977 hmapx_destroy(&srcs);
1979 hmapx_swap(&dsts, &mirror->dsts);
1980 hmapx_destroy(&dsts);
1982 free(mirror->vlans);
1983 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1986 mirror->out_vlan = out_vlan;
1988 /* Update bundles. */
1989 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1990 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1991 if (hmapx_contains(&mirror->srcs, bundle)) {
1992 bundle->src_mirrors |= mirror_bit;
1994 bundle->src_mirrors &= ~mirror_bit;
1997 if (hmapx_contains(&mirror->dsts, bundle)) {
1998 bundle->dst_mirrors |= mirror_bit;
2000 bundle->dst_mirrors &= ~mirror_bit;
2003 if (mirror->out == bundle) {
2004 bundle->mirror_out |= mirror_bit;
2006 bundle->mirror_out &= ~mirror_bit;
2010 ofproto->need_revalidate = true;
2011 mac_learning_flush(ofproto->ml);
2012 mirror_update_dups(ofproto);
2018 mirror_destroy(struct ofmirror *mirror)
2020 struct ofproto_dpif *ofproto;
2021 mirror_mask_t mirror_bit;
2022 struct ofbundle *bundle;
2028 ofproto = mirror->ofproto;
2029 ofproto->need_revalidate = true;
2030 mac_learning_flush(ofproto->ml);
2032 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2033 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2034 bundle->src_mirrors &= ~mirror_bit;
2035 bundle->dst_mirrors &= ~mirror_bit;
2036 bundle->mirror_out &= ~mirror_bit;
2039 hmapx_destroy(&mirror->srcs);
2040 hmapx_destroy(&mirror->dsts);
2041 free(mirror->vlans);
2043 ofproto->mirrors[mirror->idx] = NULL;
2047 mirror_update_dups(ofproto);
2051 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2052 uint64_t *packets, uint64_t *bytes)
2054 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2055 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2058 *packets = *bytes = UINT64_MAX;
2062 *packets = mirror->packet_count;
2063 *bytes = mirror->byte_count;
2069 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2071 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2072 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2073 ofproto->need_revalidate = true;
2074 mac_learning_flush(ofproto->ml);
2080 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2082 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2083 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2084 return bundle && bundle->mirror_out != 0;
2088 forward_bpdu_changed(struct ofproto *ofproto_)
2090 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2091 /* Revalidate cached flows whenever forward_bpdu option changes. */
2092 ofproto->need_revalidate = true;
2097 static struct ofport_dpif *
2098 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
2100 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2101 return ofport ? ofport_dpif_cast(ofport) : NULL;
2104 static struct ofport_dpif *
2105 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
2107 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2111 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2112 struct dpif_port *dpif_port)
2114 ofproto_port->name = dpif_port->name;
2115 ofproto_port->type = dpif_port->type;
2116 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2120 port_run(struct ofport_dpif *ofport)
2122 bool enable = netdev_get_carrier(ofport->up.netdev);
2125 cfm_run(ofport->cfm);
2127 if (cfm_should_send_ccm(ofport->cfm)) {
2128 struct ofpbuf packet;
2130 ofpbuf_init(&packet, 0);
2131 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
2132 send_packet(ofport, &packet);
2133 ofpbuf_uninit(&packet);
2136 enable = enable && !cfm_get_fault(ofport->cfm)
2137 && cfm_get_opup(ofport->cfm);
2140 if (ofport->bundle) {
2141 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2144 if (ofport->may_enable != enable) {
2145 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2147 if (ofproto->has_bundle_action) {
2148 ofproto->need_revalidate = true;
2152 ofport->may_enable = enable;
2156 port_wait(struct ofport_dpif *ofport)
2159 cfm_wait(ofport->cfm);
2164 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2165 struct ofproto_port *ofproto_port)
2167 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2168 struct dpif_port dpif_port;
2171 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2173 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2179 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2181 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2185 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2187 *ofp_portp = odp_port_to_ofp_port(odp_port);
2193 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2195 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2198 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2200 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2202 /* The caller is going to close ofport->up.netdev. If this is a
2203 * bonded port, then the bond is using that netdev, so remove it
2204 * from the bond. The client will need to reconfigure everything
2205 * after deleting ports, so then the slave will get re-added. */
2206 bundle_remove(&ofport->up);
2212 struct port_dump_state {
2213 struct dpif_port_dump dump;
2218 port_dump_start(const struct ofproto *ofproto_, void **statep)
2220 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2221 struct port_dump_state *state;
2223 *statep = state = xmalloc(sizeof *state);
2224 dpif_port_dump_start(&state->dump, ofproto->dpif);
2225 state->done = false;
2230 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2231 struct ofproto_port *port)
2233 struct port_dump_state *state = state_;
2234 struct dpif_port dpif_port;
2236 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2237 ofproto_port_from_dpif_port(port, &dpif_port);
2240 int error = dpif_port_dump_done(&state->dump);
2242 return error ? error : EOF;
2247 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2249 struct port_dump_state *state = state_;
2252 dpif_port_dump_done(&state->dump);
2259 port_poll(const struct ofproto *ofproto_, char **devnamep)
2261 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2262 return dpif_port_poll(ofproto->dpif, devnamep);
2266 port_poll_wait(const struct ofproto *ofproto_)
2268 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2269 dpif_port_poll_wait(ofproto->dpif);
2273 port_is_lacp_current(const struct ofport *ofport_)
2275 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2276 return (ofport->bundle && ofport->bundle->lacp
2277 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2281 /* Upcall handling. */
2283 /* Flow miss batching.
2285 * Some dpifs implement operations faster when you hand them off in a batch.
2286 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2287 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2288 * more packets, plus possibly installing the flow in the dpif.
2290 * So far we only batch the operations that affect flow setup time the most.
2291 * It's possible to batch more than that, but the benefit might be minimal. */
2293 struct hmap_node hmap_node;
2295 enum odp_key_fitness key_fitness;
2296 const struct nlattr *key;
2298 ovs_be16 initial_tci;
2299 struct list packets;
2302 struct flow_miss_op {
2303 union dpif_op dpif_op;
2304 struct subfacet *subfacet;
2307 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2308 * OpenFlow controller as necessary according to their individual
2311 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2312 * ownership is transferred to this function. */
2314 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2315 const struct flow *flow, bool clone)
2317 struct ofputil_packet_in pin;
2319 pin.packet = packet;
2320 pin.in_port = flow->in_port;
2321 pin.reason = OFPR_NO_MATCH;
2322 pin.buffer_id = 0; /* not yet known */
2323 pin.send_len = 0; /* not used for flow table misses */
2324 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2325 clone ? NULL : packet);
2328 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2329 * OpenFlow controller as necessary according to their individual
2332 * 'send_len' should be the number of bytes of 'packet' to send to the
2333 * controller, as specified in the action that caused the packet to be sent.
2335 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2336 * Otherwise, ownership is transferred to this function. */
2338 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2339 uint64_t userdata, const struct flow *flow, bool clone)
2341 struct ofputil_packet_in pin;
2342 struct user_action_cookie cookie;
2344 memcpy(&cookie, &userdata, sizeof(cookie));
2346 pin.packet = packet;
2347 pin.in_port = flow->in_port;
2348 pin.reason = OFPR_ACTION;
2349 pin.buffer_id = 0; /* not yet known */
2350 pin.send_len = cookie.data;
2351 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2352 clone ? NULL : packet);
2356 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2357 const struct ofpbuf *packet)
2359 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2365 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2367 cfm_process_heartbeat(ofport->cfm, packet);
2370 } else if (ofport->bundle && ofport->bundle->lacp
2371 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2373 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2376 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2378 stp_process_packet(ofport, packet);
2385 static struct flow_miss *
2386 flow_miss_create(struct hmap *todo, const struct flow *flow,
2387 enum odp_key_fitness key_fitness,
2388 const struct nlattr *key, size_t key_len,
2389 ovs_be16 initial_tci)
2391 uint32_t hash = flow_hash(flow, 0);
2392 struct flow_miss *miss;
2394 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2395 if (flow_equal(&miss->flow, flow)) {
2400 miss = xmalloc(sizeof *miss);
2401 hmap_insert(todo, &miss->hmap_node, hash);
2403 miss->key_fitness = key_fitness;
2405 miss->key_len = key_len;
2406 miss->initial_tci = initial_tci;
2407 list_init(&miss->packets);
2412 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2413 struct flow_miss_op *ops, size_t *n_ops)
2415 const struct flow *flow = &miss->flow;
2416 struct ofpbuf *packet, *next_packet;
2417 struct subfacet *subfacet;
2418 struct facet *facet;
2420 facet = facet_lookup_valid(ofproto, flow);
2422 struct rule_dpif *rule;
2424 rule = rule_dpif_lookup(ofproto, flow, 0);
2426 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2427 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2429 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2430 COVERAGE_INC(ofproto_dpif_no_packet_in);
2431 /* XXX install 'drop' flow entry */
2435 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2439 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2441 list_remove(&packet->list_node);
2442 send_packet_in_miss(ofproto, packet, flow, false);
2448 facet = facet_create(rule, flow);
2451 subfacet = subfacet_create(ofproto, facet,
2452 miss->key_fitness, miss->key, miss->key_len,
2455 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2456 list_remove(&packet->list_node);
2457 ofproto->n_matches++;
2459 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2461 * Extra-special case for fail-open mode.
2463 * We are in fail-open mode and the packet matched the fail-open
2464 * rule, but we are connected to a controller too. We should send
2465 * the packet up to the controller in the hope that it will try to
2466 * set up a flow and thereby allow us to exit fail-open.
2468 * See the top-level comment in fail-open.c for more information.
2470 send_packet_in_miss(ofproto, packet, flow, true);
2473 if (!facet->may_install || !subfacet->actions) {
2474 subfacet_make_actions(ofproto, subfacet, packet);
2476 if (!execute_controller_action(ofproto, &facet->flow,
2478 subfacet->actions_len, packet)) {
2479 struct flow_miss_op *op = &ops[(*n_ops)++];
2480 struct dpif_execute *execute = &op->dpif_op.execute;
2482 op->subfacet = subfacet;
2483 execute->type = DPIF_OP_EXECUTE;
2484 execute->key = miss->key;
2485 execute->key_len = miss->key_len;
2487 = (facet->may_install
2489 : xmemdup(subfacet->actions, subfacet->actions_len));
2490 execute->actions_len = subfacet->actions_len;
2491 execute->packet = packet;
2495 if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
2496 struct flow_miss_op *op = &ops[(*n_ops)++];
2497 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2499 op->subfacet = subfacet;
2500 put->type = DPIF_OP_FLOW_PUT;
2501 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2502 put->key = miss->key;
2503 put->key_len = miss->key_len;
2504 put->actions = subfacet->actions;
2505 put->actions_len = subfacet->actions_len;
2510 static enum odp_key_fitness
2511 ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
2512 const struct nlattr *key, size_t key_len,
2513 struct flow *flow, ovs_be16 *initial_tci)
2515 enum odp_key_fitness fitness;
2519 fitness = odp_flow_key_to_flow(key, key_len, flow);
2520 if (fitness == ODP_FIT_ERROR) {
2523 *initial_tci = flow->vlan_tci;
2525 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
2527 /* Cause the flow to be processed as if it came in on the real device
2528 * with the VLAN device's VLAN ID. */
2529 flow->in_port = realdev;
2530 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
2532 /* Let the caller know that we can't reproduce 'key' from 'flow'. */
2533 if (fitness == ODP_FIT_PERFECT) {
2534 fitness = ODP_FIT_TOO_MUCH;
2542 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2545 struct dpif_upcall *upcall;
2546 struct flow_miss *miss, *next_miss;
2547 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2548 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2557 /* Construct the to-do list.
2559 * This just amounts to extracting the flow from each packet and sticking
2560 * the packets that have the same flow in the same "flow_miss" structure so
2561 * that we can process them together. */
2563 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2564 enum odp_key_fitness fitness;
2565 struct flow_miss *miss;
2566 ovs_be16 initial_tci;
2569 /* Obtain metadata and check userspace/kernel agreement on flow match,
2570 * then set 'flow''s header pointers. */
2571 fitness = ofproto_dpif_extract_flow_key(ofproto,
2572 upcall->key, upcall->key_len,
2573 &flow, &initial_tci);
2574 if (fitness == ODP_FIT_ERROR) {
2575 ofpbuf_delete(upcall->packet);
2578 flow_extract(upcall->packet, flow.priority, flow.tun_id,
2579 flow.in_port, &flow);
2581 /* Handle 802.1ag, LACP, and STP specially. */
2582 if (process_special(ofproto, &flow, upcall->packet)) {
2583 ofpbuf_delete(upcall->packet);
2584 ofproto->n_matches++;
2588 /* Add other packets to a to-do list. */
2589 miss = flow_miss_create(&todo, &flow, fitness,
2590 upcall->key, upcall->key_len, initial_tci);
2591 list_push_back(&miss->packets, &upcall->packet->list_node);
2594 /* Process each element in the to-do list, constructing the set of
2595 * operations to batch. */
2597 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2598 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2599 ofpbuf_list_delete(&miss->packets);
2600 hmap_remove(&todo, &miss->hmap_node);
2603 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2604 hmap_destroy(&todo);
2606 /* Execute batch. */
2607 for (i = 0; i < n_ops; i++) {
2608 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2610 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2612 /* Free memory and update facets. */
2613 for (i = 0; i < n_ops; i++) {
2614 struct flow_miss_op *op = &flow_miss_ops[i];
2615 struct dpif_execute *execute;
2616 struct dpif_flow_put *put;
2618 switch (op->dpif_op.type) {
2619 case DPIF_OP_EXECUTE:
2620 execute = &op->dpif_op.execute;
2621 if (op->subfacet->actions != execute->actions) {
2622 free((struct nlattr *) execute->actions);
2624 ofpbuf_delete((struct ofpbuf *) execute->packet);
2627 case DPIF_OP_FLOW_PUT:
2628 put = &op->dpif_op.flow_put;
2630 op->subfacet->installed = true;
2638 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2639 struct dpif_upcall *upcall)
2641 struct user_action_cookie cookie;
2642 enum odp_key_fitness fitness;
2643 ovs_be16 initial_tci;
2646 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2648 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
2649 upcall->key_len, &flow,
2651 if (fitness == ODP_FIT_ERROR) {
2652 ofpbuf_delete(upcall->packet);
2656 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2657 if (ofproto->sflow) {
2658 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
2661 ofpbuf_delete(upcall->packet);
2662 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2663 COVERAGE_INC(ofproto_dpif_ctlr_action);
2664 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2667 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2668 ofpbuf_delete(upcall->packet);
2673 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
2675 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
2679 assert (max_batch <= FLOW_MISS_MAX_BATCH);
2682 for (i = 0; i < max_batch; i++) {
2683 struct dpif_upcall *upcall = &misses[n_misses];
2686 error = dpif_recv(ofproto->dpif, upcall);
2691 switch (upcall->type) {
2692 case DPIF_UC_ACTION:
2693 handle_userspace_upcall(ofproto, upcall);
2697 /* Handle it later. */
2701 case DPIF_N_UC_TYPES:
2703 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
2709 handle_miss_upcalls(ofproto, misses, n_misses);
2714 /* Flow expiration. */
2716 static int subfacet_max_idle(const struct ofproto_dpif *);
2717 static void update_stats(struct ofproto_dpif *);
2718 static void rule_expire(struct rule_dpif *);
2719 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
2721 /* This function is called periodically by run(). Its job is to collect
2722 * updates for the flows that have been installed into the datapath, most
2723 * importantly when they last were used, and then use that information to
2724 * expire flows that have not been used recently.
2726 * Returns the number of milliseconds after which it should be called again. */
2728 expire(struct ofproto_dpif *ofproto)
2730 struct rule_dpif *rule, *next_rule;
2731 struct classifier *table;
2734 /* Update stats for each flow in the datapath. */
2735 update_stats(ofproto);
2737 /* Expire subfacets that have been idle too long. */
2738 dp_max_idle = subfacet_max_idle(ofproto);
2739 expire_subfacets(ofproto, dp_max_idle);
2741 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2742 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2743 struct cls_cursor cursor;
2745 cls_cursor_init(&cursor, table, NULL);
2746 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2751 /* All outstanding data in existing flows has been accounted, so it's a
2752 * good time to do bond rebalancing. */
2753 if (ofproto->has_bonded_bundles) {
2754 struct ofbundle *bundle;
2756 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2758 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2763 return MIN(dp_max_idle, 1000);
2766 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2768 * This function also pushes statistics updates to rules which each facet
2769 * resubmits into. Generally these statistics will be accurate. However, if a
2770 * facet changes the rule it resubmits into at some time in between
2771 * update_stats() runs, it is possible that statistics accrued to the
2772 * old rule will be incorrectly attributed to the new rule. This could be
2773 * avoided by calling update_stats() whenever rules are created or
2774 * deleted. However, the performance impact of making so many calls to the
2775 * datapath do not justify the benefit of having perfectly accurate statistics.
2778 update_stats(struct ofproto_dpif *p)
2780 const struct dpif_flow_stats *stats;
2781 struct dpif_flow_dump dump;
2782 const struct nlattr *key;
2785 dpif_flow_dump_start(&dump, p->dpif);
2786 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2787 struct subfacet *subfacet;
2789 subfacet = subfacet_find(p, key, key_len);
2790 if (subfacet && subfacet->installed) {
2791 struct facet *facet = subfacet->facet;
2793 if (stats->n_packets >= subfacet->dp_packet_count) {
2794 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
2795 facet->packet_count += extra;
2797 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2800 if (stats->n_bytes >= subfacet->dp_byte_count) {
2801 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
2803 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2806 subfacet->dp_packet_count = stats->n_packets;
2807 subfacet->dp_byte_count = stats->n_bytes;
2809 subfacet_update_time(p, subfacet, stats->used);
2810 facet_account(p, facet);
2811 facet_push_stats(facet);
2813 if (!VLOG_DROP_WARN(&rl)) {
2817 odp_flow_key_format(key, key_len, &s);
2818 VLOG_WARN("unexpected flow from datapath %s", ds_cstr(&s));
2822 COVERAGE_INC(facet_unexpected);
2823 /* There's a flow in the datapath that we know nothing about, or a
2824 * flow that shouldn't be installed but was anyway. Delete it. */
2825 dpif_flow_del(p->dpif, key, key_len, NULL);
2828 dpif_flow_dump_done(&dump);
2831 /* Calculates and returns the number of milliseconds of idle time after which
2832 * subfacets should expire from the datapath. When a subfacet expires, we fold
2833 * its statistics into its facet, and when a facet's last subfacet expires, we
2834 * fold its statistic into its rule. */
2836 subfacet_max_idle(const struct ofproto_dpif *ofproto)
2839 * Idle time histogram.
2841 * Most of the time a switch has a relatively small number of subfacets.
2842 * When this is the case we might as well keep statistics for all of them
2843 * in userspace and to cache them in the kernel datapath for performance as
2846 * As the number of subfacets increases, the memory required to maintain
2847 * statistics about them in userspace and in the kernel becomes
2848 * significant. However, with a large number of subfacets it is likely
2849 * that only a few of them are "heavy hitters" that consume a large amount
2850 * of bandwidth. At this point, only heavy hitters are worth caching in
2851 * the kernel and maintaining in userspaces; other subfacets we can
2854 * The technique used to compute the idle time is to build a histogram with
2855 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
2856 * that is installed in the kernel gets dropped in the appropriate bucket.
2857 * After the histogram has been built, we compute the cutoff so that only
2858 * the most-recently-used 1% of subfacets (but at least
2859 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2860 * the most-recently-used bucket of subfacets is kept, so actually an
2861 * arbitrary number of subfacets can be kept in any given expiration run
2862 * (though the next run will delete most of those unless they receive
2865 * This requires a second pass through the subfacets, in addition to the
2866 * pass made by update_stats(), because the former function never looks at
2867 * uninstallable subfacets.
2869 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2870 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2871 int buckets[N_BUCKETS] = { 0 };
2872 int total, subtotal, bucket;
2873 struct subfacet *subfacet;
2877 total = hmap_count(&ofproto->subfacets);
2878 if (total <= ofproto->up.flow_eviction_threshold) {
2879 return N_BUCKETS * BUCKET_WIDTH;
2882 /* Build histogram. */
2884 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
2885 long long int idle = now - subfacet->used;
2886 int bucket = (idle <= 0 ? 0
2887 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2888 : (unsigned int) idle / BUCKET_WIDTH);
2892 /* Find the first bucket whose flows should be expired. */
2893 subtotal = bucket = 0;
2895 subtotal += buckets[bucket++];
2896 } while (bucket < N_BUCKETS &&
2897 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2899 if (VLOG_IS_DBG_ENABLED()) {
2903 ds_put_cstr(&s, "keep");
2904 for (i = 0; i < N_BUCKETS; i++) {
2906 ds_put_cstr(&s, ", drop");
2909 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2912 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2916 return bucket * BUCKET_WIDTH;
2920 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
2922 long long int cutoff = time_msec() - dp_max_idle;
2923 struct subfacet *subfacet, *next_subfacet;
2925 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
2926 &ofproto->subfacets) {
2927 if (subfacet->used < cutoff) {
2928 subfacet_destroy(ofproto, subfacet);
2933 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2934 * then delete it entirely. */
2936 rule_expire(struct rule_dpif *rule)
2938 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2939 struct facet *facet, *next_facet;
2943 /* Has 'rule' expired? */
2945 if (rule->up.hard_timeout
2946 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2947 reason = OFPRR_HARD_TIMEOUT;
2948 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2949 && now > rule->used + rule->up.idle_timeout * 1000) {
2950 reason = OFPRR_IDLE_TIMEOUT;
2955 COVERAGE_INC(ofproto_dpif_expired);
2957 /* Update stats. (This is a no-op if the rule expired due to an idle
2958 * timeout, because that only happens when the rule has no facets left.) */
2959 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2960 facet_remove(ofproto, facet);
2963 /* Get rid of the rule. */
2964 ofproto_rule_expire(&rule->up, reason);
2969 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2971 * The caller must already have determined that no facet with an identical
2972 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2973 * the ofproto's classifier table.
2975 * The facet will initially have no subfacets. The caller should create (at
2976 * least) one subfacet with subfacet_create(). */
2977 static struct facet *
2978 facet_create(struct rule_dpif *rule, const struct flow *flow)
2980 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2981 struct facet *facet;
2983 facet = xzalloc(sizeof *facet);
2984 facet->used = time_msec();
2985 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2986 list_push_back(&rule->facets, &facet->list_node);
2988 facet->flow = *flow;
2989 list_init(&facet->subfacets);
2990 netflow_flow_init(&facet->nf_flow);
2991 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2997 facet_free(struct facet *facet)
3003 execute_controller_action(struct ofproto_dpif *ofproto,
3004 const struct flow *flow,
3005 const struct nlattr *odp_actions, size_t actions_len,
3006 struct ofpbuf *packet)
3009 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
3010 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
3011 /* As an optimization, avoid a round-trip from userspace to kernel to
3012 * userspace. This also avoids possibly filling up kernel packet
3013 * buffers along the way.
3015 * This optimization will not accidentally catch sFlow
3016 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
3017 * inside OVS_ACTION_ATTR_SAMPLE. */
3018 const struct nlattr *nla;
3020 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
3021 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
3029 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3030 * 'packet', which arrived on 'in_port'.
3032 * Takes ownership of 'packet'. */
3034 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3035 const struct nlattr *odp_actions, size_t actions_len,
3036 struct ofpbuf *packet)
3038 struct odputil_keybuf keybuf;
3042 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
3047 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3048 odp_flow_key_from_flow(&key, flow);
3050 error = dpif_execute(ofproto->dpif, key.data, key.size,
3051 odp_actions, actions_len, packet);
3053 ofpbuf_delete(packet);
3057 /* Remove 'facet' from 'ofproto' and free up the associated memory:
3059 * - If 'facet' was installed in the datapath, uninstalls it and updates its
3060 * rule's statistics, via subfacet_uninstall().
3062 * - Removes 'facet' from its rule and from ofproto->facets.
3065 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
3067 struct subfacet *subfacet, *next_subfacet;
3069 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3070 &facet->subfacets) {
3071 subfacet_destroy__(ofproto, subfacet);
3074 facet_flush_stats(ofproto, facet);
3075 hmap_remove(&ofproto->facets, &facet->hmap_node);
3076 list_remove(&facet->list_node);
3081 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
3084 struct subfacet *subfacet;
3085 const struct nlattr *a;
3089 if (facet->byte_count <= facet->accounted_bytes) {
3092 n_bytes = facet->byte_count - facet->accounted_bytes;
3093 facet->accounted_bytes = facet->byte_count;
3095 /* Feed information from the active flows back into the learning table to
3096 * ensure that table is always in sync with what is actually flowing
3097 * through the datapath. */
3098 if (facet->has_learn || facet->has_normal) {
3099 struct action_xlate_ctx ctx;
3101 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3102 facet->flow.vlan_tci, NULL);
3103 ctx.may_learn = true;
3104 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
3105 facet->rule->up.n_actions));
3108 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
3112 /* This loop feeds byte counters to bond_account() for rebalancing to use
3113 * as a basis. We also need to track the actual VLAN on which the packet
3114 * is going to be sent to ensure that it matches the one passed to
3115 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3118 * We use the actions from an arbitrary subfacet because they should all
3119 * be equally valid for our purpose. */
3120 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3121 struct subfacet, list_node);
3122 vlan_tci = facet->flow.vlan_tci;
3123 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3124 subfacet->actions, subfacet->actions_len) {
3125 const struct ovs_action_push_vlan *vlan;
3126 struct ofport_dpif *port;
3128 switch (nl_attr_type(a)) {
3129 case OVS_ACTION_ATTR_OUTPUT:
3130 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3131 if (port && port->bundle && port->bundle->bond) {
3132 bond_account(port->bundle->bond, &facet->flow,
3133 vlan_tci_to_vid(vlan_tci), n_bytes);
3137 case OVS_ACTION_ATTR_POP_VLAN:
3138 vlan_tci = htons(0);
3141 case OVS_ACTION_ATTR_PUSH_VLAN:
3142 vlan = nl_attr_get(a);
3143 vlan_tci = vlan->vlan_tci;
3149 /* Returns true if the only action for 'facet' is to send to the controller.
3150 * (We don't report NetFlow expiration messages for such facets because they
3151 * are just part of the control logic for the network, not real traffic). */
3153 facet_is_controller_flow(struct facet *facet)
3156 && facet->rule->up.n_actions == 1
3157 && action_outputs_to_port(&facet->rule->up.actions[0],
3158 htons(OFPP_CONTROLLER)));
3161 /* Folds all of 'facet''s statistics into its rule. Also updates the
3162 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3163 * 'facet''s statistics in the datapath should have been zeroed and folded into
3164 * its packet and byte counts before this function is called. */
3166 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3168 struct subfacet *subfacet;
3170 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3171 assert(!subfacet->dp_byte_count);
3172 assert(!subfacet->dp_packet_count);
3175 facet_push_stats(facet);
3176 facet_account(ofproto, facet);
3178 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3179 struct ofexpired expired;
3180 expired.flow = facet->flow;
3181 expired.packet_count = facet->packet_count;
3182 expired.byte_count = facet->byte_count;
3183 expired.used = facet->used;
3184 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3187 facet->rule->packet_count += facet->packet_count;
3188 facet->rule->byte_count += facet->byte_count;
3190 /* Reset counters to prevent double counting if 'facet' ever gets
3192 facet_reset_counters(facet);
3194 netflow_flow_clear(&facet->nf_flow);
3197 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3198 * Returns it if found, otherwise a null pointer.
3200 * The returned facet might need revalidation; use facet_lookup_valid()
3201 * instead if that is important. */
3202 static struct facet *
3203 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3205 struct facet *facet;
3207 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3209 if (flow_equal(flow, &facet->flow)) {
3217 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3218 * Returns it if found, otherwise a null pointer.
3220 * The returned facet is guaranteed to be valid. */
3221 static struct facet *
3222 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3224 struct facet *facet = facet_find(ofproto, flow);
3226 /* The facet we found might not be valid, since we could be in need of
3227 * revalidation. If it is not valid, don't return it. */
3229 && (ofproto->need_revalidate
3230 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3231 && !facet_revalidate(ofproto, facet)) {
3232 COVERAGE_INC(facet_invalidated);
3239 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3241 * - If the rule found is different from 'facet''s current rule, moves
3242 * 'facet' to the new rule and recompiles its actions.
3244 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3245 * where it is and recompiles its actions anyway.
3247 * - If there is none, destroys 'facet'.
3249 * Returns true if 'facet' still exists, false if it has been destroyed. */
3251 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3254 struct nlattr *odp_actions;
3257 struct actions *new_actions;
3259 struct action_xlate_ctx ctx;
3260 struct rule_dpif *new_rule;
3261 struct subfacet *subfacet;
3262 bool actions_changed;
3265 COVERAGE_INC(facet_revalidate);
3267 /* Determine the new rule. */
3268 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3270 /* No new rule, so delete the facet. */
3271 facet_remove(ofproto, facet);
3275 /* Calculate new datapath actions.
3277 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3278 * emit a NetFlow expiration and, if so, we need to have the old state
3279 * around to properly compose it. */
3281 /* If the datapath actions changed or the installability changed,
3282 * then we need to talk to the datapath. */
3285 memset(&ctx, 0, sizeof ctx);
3286 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3287 struct ofpbuf *odp_actions;
3288 bool should_install;
3290 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3291 subfacet->initial_tci, NULL);
3292 odp_actions = xlate_actions(&ctx, new_rule->up.actions,
3293 new_rule->up.n_actions);
3294 actions_changed = (subfacet->actions_len != odp_actions->size
3295 || memcmp(subfacet->actions, odp_actions->data,
3296 subfacet->actions_len));
3298 should_install = (ctx.may_set_up_flow
3299 && subfacet->key_fitness != ODP_FIT_TOO_LITTLE);
3300 if (actions_changed || should_install != subfacet->installed) {
3301 if (should_install) {
3302 struct dpif_flow_stats stats;
3304 subfacet_install(ofproto, subfacet,
3305 odp_actions->data, odp_actions->size, &stats);
3306 subfacet_update_stats(ofproto, subfacet, &stats);
3308 subfacet_uninstall(ofproto, subfacet);
3312 new_actions = xcalloc(list_size(&facet->subfacets),
3313 sizeof *new_actions);
3315 new_actions[i].odp_actions = xmemdup(odp_actions->data,
3317 new_actions[i].actions_len = odp_actions->size;
3320 ofpbuf_delete(odp_actions);
3324 facet_flush_stats(ofproto, facet);
3327 /* Update 'facet' now that we've taken care of all the old state. */
3328 facet->tags = ctx.tags;
3329 facet->nf_flow.output_iface = ctx.nf_output_iface;
3330 facet->may_install = ctx.may_set_up_flow;
3331 facet->has_learn = ctx.has_learn;
3332 facet->has_normal = ctx.has_normal;
3333 facet->mirrors = ctx.mirrors;
3336 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3337 if (new_actions[i].odp_actions) {
3338 free(subfacet->actions);
3339 subfacet->actions = new_actions[i].odp_actions;
3340 subfacet->actions_len = new_actions[i].actions_len;
3346 if (facet->rule != new_rule) {
3347 COVERAGE_INC(facet_changed_rule);
3348 list_remove(&facet->list_node);
3349 list_push_back(&new_rule->facets, &facet->list_node);
3350 facet->rule = new_rule;
3351 facet->used = new_rule->up.created;
3352 facet->prev_used = facet->used;
3358 /* Updates 'facet''s used time. Caller is responsible for calling
3359 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3361 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3364 if (used > facet->used) {
3366 if (used > facet->rule->used) {
3367 facet->rule->used = used;
3369 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3374 facet_reset_counters(struct facet *facet)
3376 facet->packet_count = 0;
3377 facet->byte_count = 0;
3378 facet->prev_packet_count = 0;
3379 facet->prev_byte_count = 0;
3380 facet->accounted_bytes = 0;
3384 facet_push_stats(struct facet *facet)
3386 uint64_t new_packets, new_bytes;
3388 assert(facet->packet_count >= facet->prev_packet_count);
3389 assert(facet->byte_count >= facet->prev_byte_count);
3390 assert(facet->used >= facet->prev_used);
3392 new_packets = facet->packet_count - facet->prev_packet_count;
3393 new_bytes = facet->byte_count - facet->prev_byte_count;
3395 if (new_packets || new_bytes || facet->used > facet->prev_used) {
3396 facet->prev_packet_count = facet->packet_count;
3397 facet->prev_byte_count = facet->byte_count;
3398 facet->prev_used = facet->used;
3400 flow_push_stats(facet->rule, &facet->flow,
3401 new_packets, new_bytes, facet->used);
3403 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
3404 facet->mirrors, new_packets, new_bytes);
3408 struct ofproto_push {
3409 struct action_xlate_ctx ctx;
3416 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3418 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3421 rule->packet_count += push->packets;
3422 rule->byte_count += push->bytes;
3423 rule->used = MAX(push->used, rule->used);
3427 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3428 * 'rule''s actions and mirrors. */
3430 flow_push_stats(const struct rule_dpif *rule,
3431 const struct flow *flow, uint64_t packets, uint64_t bytes,
3434 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3435 struct ofproto_push push;
3437 push.packets = packets;
3441 action_xlate_ctx_init(&push.ctx, ofproto, flow, flow->vlan_tci, NULL);
3442 push.ctx.resubmit_hook = push_resubmit;
3443 ofpbuf_delete(xlate_actions(&push.ctx,
3444 rule->up.actions, rule->up.n_actions));
3449 static struct subfacet *
3450 subfacet_find__(struct ofproto_dpif *ofproto,
3451 const struct nlattr *key, size_t key_len, uint32_t key_hash,
3452 const struct flow *flow)
3454 struct subfacet *subfacet;
3456 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
3457 &ofproto->subfacets) {
3459 ? (subfacet->key_len == key_len
3460 && !memcmp(key, subfacet->key, key_len))
3461 : flow_equal(flow, &subfacet->facet->flow)) {
3469 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
3470 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
3471 * there is one, otherwise creates and returns a new subfacet.
3473 * If the returned subfacet is new, then subfacet->actions will be NULL, in
3474 * which case the caller must populate the actions with
3475 * subfacet_make_actions(). */
3476 static struct subfacet *
3477 subfacet_create(struct ofproto_dpif *ofproto, struct facet *facet,
3478 enum odp_key_fitness key_fitness,
3479 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
3481 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3482 struct subfacet *subfacet;
3484 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
3486 if (subfacet->facet == facet) {
3490 /* This shouldn't happen. */
3491 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3492 subfacet_destroy(ofproto, subfacet);
3495 subfacet = xzalloc(sizeof *subfacet);
3496 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
3497 list_push_back(&facet->subfacets, &subfacet->list_node);
3498 subfacet->facet = facet;
3499 subfacet->used = time_msec();
3500 subfacet->key_fitness = key_fitness;
3501 if (key_fitness != ODP_FIT_PERFECT) {
3502 subfacet->key = xmemdup(key, key_len);
3503 subfacet->key_len = key_len;
3505 subfacet->installed = false;
3506 subfacet->initial_tci = initial_tci;
3511 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
3512 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
3513 static struct subfacet *
3514 subfacet_find(struct ofproto_dpif *ofproto,
3515 const struct nlattr *key, size_t key_len)
3517 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3518 enum odp_key_fitness fitness;
3521 fitness = odp_flow_key_to_flow(key, key_len, &flow);
3522 if (fitness == ODP_FIT_ERROR) {
3526 return subfacet_find__(ofproto, key, key_len, key_hash, &flow);
3529 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
3530 * its facet within 'ofproto', and frees it. */
3532 subfacet_destroy__(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3534 subfacet_uninstall(ofproto, subfacet);
3535 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
3536 list_remove(&subfacet->list_node);
3537 free(subfacet->key);
3538 free(subfacet->actions);
3542 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
3543 * last remaining subfacet in its facet destroys the facet too. */
3545 subfacet_destroy(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3547 struct facet *facet = subfacet->facet;
3549 subfacet_destroy__(ofproto, subfacet);
3550 if (list_is_empty(&facet->subfacets)) {
3551 facet_remove(ofproto, facet);
3555 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
3556 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
3557 * for use as temporary storage. */
3559 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
3562 if (!subfacet->key) {
3563 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
3564 odp_flow_key_from_flow(key, &subfacet->facet->flow);
3566 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
3570 /* Composes the datapath actions for 'subfacet' based on its rule's actions. */
3572 subfacet_make_actions(struct ofproto_dpif *p, struct subfacet *subfacet,
3573 const struct ofpbuf *packet)
3575 struct facet *facet = subfacet->facet;
3576 const struct rule_dpif *rule = facet->rule;
3577 struct ofpbuf *odp_actions;
3578 struct action_xlate_ctx ctx;
3580 action_xlate_ctx_init(&ctx, p, &facet->flow, subfacet->initial_tci,
3582 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3583 facet->tags = ctx.tags;
3584 facet->may_install = ctx.may_set_up_flow;
3585 facet->has_learn = ctx.has_learn;
3586 facet->has_normal = ctx.has_normal;
3587 facet->nf_flow.output_iface = ctx.nf_output_iface;
3588 facet->mirrors = ctx.mirrors;
3590 if (subfacet->actions_len != odp_actions->size
3591 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
3592 free(subfacet->actions);
3593 subfacet->actions_len = odp_actions->size;
3594 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
3597 ofpbuf_delete(odp_actions);
3600 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
3601 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
3602 * in the datapath will be zeroed and 'stats' will be updated with traffic new
3603 * since 'subfacet' was last updated.
3605 * Returns 0 if successful, otherwise a positive errno value. */
3607 subfacet_install(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3608 const struct nlattr *actions, size_t actions_len,
3609 struct dpif_flow_stats *stats)
3611 struct odputil_keybuf keybuf;
3612 enum dpif_flow_put_flags flags;
3616 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3618 flags |= DPIF_FP_ZERO_STATS;
3621 subfacet_get_key(subfacet, &keybuf, &key);
3622 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
3623 actions, actions_len, stats);
3626 subfacet_reset_dp_stats(subfacet, stats);
3632 /* If 'subfacet' is installed in the datapath, uninstalls it. */
3634 subfacet_uninstall(struct ofproto_dpif *p, struct subfacet *subfacet)
3636 if (subfacet->installed) {
3637 struct odputil_keybuf keybuf;
3638 struct dpif_flow_stats stats;
3642 subfacet_get_key(subfacet, &keybuf, &key);
3643 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3644 subfacet_reset_dp_stats(subfacet, &stats);
3646 subfacet_update_stats(p, subfacet, &stats);
3648 subfacet->installed = false;
3650 assert(subfacet->dp_packet_count == 0);
3651 assert(subfacet->dp_byte_count == 0);
3655 /* Resets 'subfacet''s datapath statistics counters. This should be called
3656 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
3657 * non-null, it should contain the statistics returned by dpif when 'subfacet'
3658 * was reset in the datapath. 'stats' will be modified to include only
3659 * statistics new since 'subfacet' was last updated. */
3661 subfacet_reset_dp_stats(struct subfacet *subfacet,
3662 struct dpif_flow_stats *stats)
3665 && subfacet->dp_packet_count <= stats->n_packets
3666 && subfacet->dp_byte_count <= stats->n_bytes) {
3667 stats->n_packets -= subfacet->dp_packet_count;
3668 stats->n_bytes -= subfacet->dp_byte_count;
3671 subfacet->dp_packet_count = 0;
3672 subfacet->dp_byte_count = 0;
3675 /* Updates 'subfacet''s used time. The caller is responsible for calling
3676 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
3678 subfacet_update_time(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3681 if (used > subfacet->used) {
3682 subfacet->used = used;
3683 facet_update_time(ofproto, subfacet->facet, used);
3687 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
3689 * Because of the meaning of a subfacet's counters, it only makes sense to do
3690 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
3691 * represents a packet that was sent by hand or if it represents statistics
3692 * that have been cleared out of the datapath. */
3694 subfacet_update_stats(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3695 const struct dpif_flow_stats *stats)
3697 if (stats->n_packets || stats->used > subfacet->used) {
3698 struct facet *facet = subfacet->facet;
3700 subfacet_update_time(ofproto, subfacet, stats->used);
3701 facet->packet_count += stats->n_packets;
3702 facet->byte_count += stats->n_bytes;
3703 facet_push_stats(facet);
3704 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3710 static struct rule_dpif *
3711 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3714 struct cls_rule *cls_rule;
3715 struct classifier *cls;
3717 if (table_id >= N_TABLES) {
3721 cls = &ofproto->up.tables[table_id];
3722 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3723 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3724 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3725 * are unavailable. */
3726 struct flow ofpc_normal_flow = *flow;
3727 ofpc_normal_flow.tp_src = htons(0);
3728 ofpc_normal_flow.tp_dst = htons(0);
3729 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3731 cls_rule = classifier_lookup(cls, flow);
3733 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3737 complete_operation(struct rule_dpif *rule)
3739 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3741 rule_invalidate(rule);
3743 struct dpif_completion *c = xmalloc(sizeof *c);
3744 c->op = rule->up.pending;
3745 list_push_back(&ofproto->completions, &c->list_node);
3747 ofoperation_complete(rule->up.pending, 0);
3751 static struct rule *
3754 struct rule_dpif *rule = xmalloc(sizeof *rule);
3759 rule_dealloc(struct rule *rule_)
3761 struct rule_dpif *rule = rule_dpif_cast(rule_);
3766 rule_construct(struct rule *rule_)
3768 struct rule_dpif *rule = rule_dpif_cast(rule_);
3769 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3770 struct rule_dpif *victim;
3774 error = validate_actions(rule->up.actions, rule->up.n_actions,
3775 &rule->up.cr.flow, ofproto->max_ports);
3780 rule->used = rule->up.created;
3781 rule->packet_count = 0;
3782 rule->byte_count = 0;
3784 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3785 if (victim && !list_is_empty(&victim->facets)) {
3786 struct facet *facet;
3788 rule->facets = victim->facets;
3789 list_moved(&rule->facets);
3790 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3791 /* XXX: We're only clearing our local counters here. It's possible
3792 * that quite a few packets are unaccounted for in the datapath
3793 * statistics. These will be accounted to the new rule instead of
3794 * cleared as required. This could be fixed by clearing out the
3795 * datapath statistics for this facet, but currently it doesn't
3797 facet_reset_counters(facet);
3801 /* Must avoid list_moved() in this case. */
3802 list_init(&rule->facets);
3805 table_id = rule->up.table_id;
3806 rule->tag = (victim ? victim->tag
3808 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3809 ofproto->tables[table_id].basis));
3811 complete_operation(rule);
3816 rule_destruct(struct rule *rule_)
3818 struct rule_dpif *rule = rule_dpif_cast(rule_);
3819 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3820 struct facet *facet, *next_facet;
3822 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3823 facet_revalidate(ofproto, facet);
3826 complete_operation(rule);
3830 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3832 struct rule_dpif *rule = rule_dpif_cast(rule_);
3833 struct facet *facet;
3835 /* Start from historical data for 'rule' itself that are no longer tracked
3836 * in facets. This counts, for example, facets that have expired. */
3837 *packets = rule->packet_count;
3838 *bytes = rule->byte_count;
3840 /* Add any statistics that are tracked by facets. This includes
3841 * statistical data recently updated by ofproto_update_stats() as well as
3842 * stats for packets that were executed "by hand" via dpif_execute(). */
3843 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3844 *packets += facet->packet_count;
3845 *bytes += facet->byte_count;
3850 rule_execute(struct rule *rule_, const struct flow *flow,
3851 struct ofpbuf *packet)
3853 struct rule_dpif *rule = rule_dpif_cast(rule_);
3854 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3855 struct action_xlate_ctx ctx;
3856 struct ofpbuf *odp_actions;
3859 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
3860 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3861 size = packet->size;
3862 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3863 odp_actions->size, packet)) {
3864 rule->used = time_msec();
3865 rule->packet_count++;
3866 rule->byte_count += size;
3867 flow_push_stats(rule, flow, 1, size, rule->used);
3869 ofpbuf_delete(odp_actions);
3875 rule_modify_actions(struct rule *rule_)
3877 struct rule_dpif *rule = rule_dpif_cast(rule_);
3878 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3881 error = validate_actions(rule->up.actions, rule->up.n_actions,
3882 &rule->up.cr.flow, ofproto->max_ports);
3884 ofoperation_complete(rule->up.pending, error);
3888 complete_operation(rule);
3891 /* Sends 'packet' out 'ofport'.
3892 * May modify 'packet'.
3893 * Returns 0 if successful, otherwise a positive errno value. */
3895 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3897 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3898 struct ofpbuf key, odp_actions;
3899 struct odputil_keybuf keybuf;
3904 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3905 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
3907 if (odp_port != ofport->odp_port) {
3908 eth_pop_vlan(packet);
3909 flow.vlan_tci = htons(0);
3912 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3913 odp_flow_key_from_flow(&key, &flow);
3915 ofpbuf_init(&odp_actions, 32);
3916 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3918 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3919 error = dpif_execute(ofproto->dpif,
3921 odp_actions.data, odp_actions.size,
3923 ofpbuf_uninit(&odp_actions);
3926 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3927 ofproto->up.name, odp_port, strerror(error));
3932 /* OpenFlow to datapath action translation. */
3934 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3935 struct action_xlate_ctx *ctx);
3936 static void xlate_normal(struct action_xlate_ctx *);
3939 put_userspace_action(const struct ofproto_dpif *ofproto,
3940 struct ofpbuf *odp_actions,
3941 const struct flow *flow,
3942 const struct user_action_cookie *cookie)
3946 pid = dpif_port_get_pid(ofproto->dpif,
3947 ofp_port_to_odp_port(flow->in_port));
3949 return odp_put_userspace_action(pid, cookie, odp_actions);
3952 /* Compose SAMPLE action for sFlow. */
3954 compose_sflow_action(const struct ofproto_dpif *ofproto,
3955 struct ofpbuf *odp_actions,
3956 const struct flow *flow,
3959 uint32_t port_ifindex;
3960 uint32_t probability;
3961 struct user_action_cookie cookie;
3962 size_t sample_offset, actions_offset;
3963 int cookie_offset, n_output;
3965 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3969 if (odp_port == OVSP_NONE) {
3973 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3977 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3979 /* Number of packets out of UINT_MAX to sample. */
3980 probability = dpif_sflow_get_probability(ofproto->sflow);
3981 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3983 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3985 cookie.type = USER_ACTION_COOKIE_SFLOW;
3986 cookie.data = port_ifindex;
3987 cookie.n_output = n_output;
3988 cookie.vlan_tci = 0;
3989 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3991 nl_msg_end_nested(odp_actions, actions_offset);
3992 nl_msg_end_nested(odp_actions, sample_offset);
3993 return cookie_offset;
3996 /* SAMPLE action must be first action in any given list of actions.
3997 * At this point we do not have all information required to build it. So try to
3998 * build sample action as complete as possible. */
4000 add_sflow_action(struct action_xlate_ctx *ctx)
4002 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
4004 &ctx->flow, OVSP_NONE);
4005 ctx->sflow_odp_port = 0;
4006 ctx->sflow_n_outputs = 0;
4009 /* Fix SAMPLE action according to data collected while composing ODP actions.
4010 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
4011 * USERSPACE action's user-cookie which is required for sflow. */
4013 fix_sflow_action(struct action_xlate_ctx *ctx)
4015 const struct flow *base = &ctx->base_flow;
4016 struct user_action_cookie *cookie;
4018 if (!ctx->user_cookie_offset) {
4022 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
4024 assert(cookie != NULL);
4025 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
4027 if (ctx->sflow_n_outputs) {
4028 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
4029 ctx->sflow_odp_port);
4031 if (ctx->sflow_n_outputs >= 255) {
4032 cookie->n_output = 255;
4034 cookie->n_output = ctx->sflow_n_outputs;
4036 cookie->vlan_tci = base->vlan_tci;
4040 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4043 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
4044 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
4045 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
4046 uint8_t flow_nw_tos = ctx->flow.nw_tos;
4050 struct priority_to_dscp *pdscp;
4052 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
4053 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4057 pdscp = get_priority(ofport, ctx->flow.priority);
4059 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4060 ctx->flow.nw_tos |= pdscp->dscp;
4063 /* We may not have an ofport record for this port, but it doesn't hurt
4064 * to allow forwarding to it anyhow. Maybe such a port will appear
4065 * later and we're pre-populating the flow table. */
4068 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
4069 ctx->flow.vlan_tci);
4070 if (out_port != odp_port) {
4071 ctx->flow.vlan_tci = htons(0);
4073 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4074 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
4076 ctx->sflow_odp_port = odp_port;
4077 ctx->sflow_n_outputs++;
4078 ctx->nf_output_iface = ofp_port;
4079 ctx->flow.vlan_tci = flow_vlan_tci;
4080 ctx->flow.nw_tos = flow_nw_tos;
4084 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
4086 compose_output_action__(ctx, ofp_port, true);
4090 xlate_table_action(struct action_xlate_ctx *ctx,
4091 uint16_t in_port, uint8_t table_id)
4093 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
4094 struct ofproto_dpif *ofproto = ctx->ofproto;
4095 struct rule_dpif *rule;
4096 uint16_t old_in_port;
4097 uint8_t old_table_id;
4099 old_table_id = ctx->table_id;
4100 ctx->table_id = table_id;
4102 /* Look up a flow with 'in_port' as the input port. */
4103 old_in_port = ctx->flow.in_port;
4104 ctx->flow.in_port = in_port;
4105 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
4108 if (table_id > 0 && table_id < N_TABLES) {
4109 struct table_dpif *table = &ofproto->tables[table_id];
4110 if (table->other_table) {
4113 : rule_calculate_tag(&ctx->flow,
4114 &table->other_table->wc,
4119 /* Restore the original input port. Otherwise OFPP_NORMAL and
4120 * OFPP_IN_PORT will have surprising behavior. */
4121 ctx->flow.in_port = old_in_port;
4123 if (ctx->resubmit_hook) {
4124 ctx->resubmit_hook(ctx, rule);
4129 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4133 ctx->table_id = old_table_id;
4135 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4137 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
4138 MAX_RESUBMIT_RECURSION);
4143 xlate_resubmit_table(struct action_xlate_ctx *ctx,
4144 const struct nx_action_resubmit *nar)
4149 in_port = (nar->in_port == htons(OFPP_IN_PORT)
4151 : ntohs(nar->in_port));
4152 table_id = nar->table == 255 ? ctx->table_id : nar->table;
4154 xlate_table_action(ctx, in_port, table_id);
4158 flood_packets(struct action_xlate_ctx *ctx, bool all)
4160 struct ofport_dpif *ofport;
4162 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
4163 uint16_t ofp_port = ofport->up.ofp_port;
4165 if (ofp_port == ctx->flow.in_port) {
4170 compose_output_action__(ctx, ofp_port, false);
4171 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
4172 compose_output_action(ctx, ofp_port);
4176 ctx->nf_output_iface = NF_OUT_FLOOD;
4180 compose_controller_action(struct action_xlate_ctx *ctx, int len)
4182 struct user_action_cookie cookie;
4184 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4185 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4187 cookie.n_output = 0;
4188 cookie.vlan_tci = 0;
4189 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
4193 xlate_output_action__(struct action_xlate_ctx *ctx,
4194 uint16_t port, uint16_t max_len)
4196 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4198 ctx->nf_output_iface = NF_OUT_DROP;
4202 compose_output_action(ctx, ctx->flow.in_port);
4205 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
4211 flood_packets(ctx, false);
4214 flood_packets(ctx, true);
4216 case OFPP_CONTROLLER:
4217 compose_controller_action(ctx, max_len);
4220 compose_output_action(ctx, OFPP_LOCAL);
4225 if (port != ctx->flow.in_port) {
4226 compose_output_action(ctx, port);
4231 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4232 ctx->nf_output_iface = NF_OUT_FLOOD;
4233 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4234 ctx->nf_output_iface = prev_nf_output_iface;
4235 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4236 ctx->nf_output_iface != NF_OUT_FLOOD) {
4237 ctx->nf_output_iface = NF_OUT_MULTI;
4242 xlate_output_reg_action(struct action_xlate_ctx *ctx,
4243 const struct nx_action_output_reg *naor)
4247 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4249 if (ofp_port <= UINT16_MAX) {
4250 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4255 xlate_output_action(struct action_xlate_ctx *ctx,
4256 const struct ofp_action_output *oao)
4258 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4262 xlate_enqueue_action(struct action_xlate_ctx *ctx,
4263 const struct ofp_action_enqueue *oae)
4266 uint32_t flow_priority, priority;
4269 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4272 /* Fall back to ordinary output action. */
4273 xlate_output_action__(ctx, ntohs(oae->port), 0);
4277 /* Figure out datapath output port. */
4278 ofp_port = ntohs(oae->port);
4279 if (ofp_port == OFPP_IN_PORT) {
4280 ofp_port = ctx->flow.in_port;
4281 } else if (ofp_port == ctx->flow.in_port) {
4285 /* Add datapath actions. */
4286 flow_priority = ctx->flow.priority;
4287 ctx->flow.priority = priority;
4288 compose_output_action(ctx, ofp_port);
4289 ctx->flow.priority = flow_priority;
4291 /* Update NetFlow output port. */
4292 if (ctx->nf_output_iface == NF_OUT_DROP) {
4293 ctx->nf_output_iface = ofp_port;
4294 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4295 ctx->nf_output_iface = NF_OUT_MULTI;
4300 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4301 const struct nx_action_set_queue *nasq)
4306 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4309 /* Couldn't translate queue to a priority, so ignore. A warning
4310 * has already been logged. */
4314 ctx->flow.priority = priority;
4317 struct xlate_reg_state {
4323 xlate_autopath(struct action_xlate_ctx *ctx,
4324 const struct nx_action_autopath *naa)
4326 uint16_t ofp_port = ntohl(naa->id);
4327 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4329 if (!port || !port->bundle) {
4330 ofp_port = OFPP_NONE;
4331 } else if (port->bundle->bond) {
4332 /* Autopath does not support VLAN hashing. */
4333 struct ofport_dpif *slave = bond_choose_output_slave(
4334 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4336 ofp_port = slave->up.ofp_port;
4339 autopath_execute(naa, &ctx->flow, ofp_port);
4343 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4345 struct ofproto_dpif *ofproto = ofproto_;
4346 struct ofport_dpif *port;
4356 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4359 port = get_ofp_port(ofproto, ofp_port);
4360 return port ? port->may_enable : false;
4365 xlate_learn_action(struct action_xlate_ctx *ctx,
4366 const struct nx_action_learn *learn)
4368 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4369 struct ofputil_flow_mod fm;
4372 learn_execute(learn, &ctx->flow, &fm);
4374 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4375 if (error && !VLOG_DROP_WARN(&rl)) {
4376 char *msg = ofputil_error_to_string(error);
4377 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4385 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4387 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4388 ? htonl(OFPPC_NO_RECV_STP)
4389 : htonl(OFPPC_NO_RECV))) {
4393 /* Only drop packets here if both forwarding and learning are
4394 * disabled. If just learning is enabled, we need to have
4395 * OFPP_NORMAL and the learning action have a look at the packet
4396 * before we can drop it. */
4397 if (!stp_forward_in_state(port->stp_state)
4398 && !stp_learn_in_state(port->stp_state)) {
4406 do_xlate_actions(const union ofp_action *in, size_t n_in,
4407 struct action_xlate_ctx *ctx)
4409 const struct ofport_dpif *port;
4410 const union ofp_action *ia;
4413 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4414 if (port && !may_receive(port, ctx)) {
4415 /* Drop this flow. */
4419 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4420 const struct ofp_action_dl_addr *oada;
4421 const struct nx_action_resubmit *nar;
4422 const struct nx_action_set_tunnel *nast;
4423 const struct nx_action_set_queue *nasq;
4424 const struct nx_action_multipath *nam;
4425 const struct nx_action_autopath *naa;
4426 const struct nx_action_bundle *nab;
4427 const struct nx_action_output_reg *naor;
4428 enum ofputil_action_code code;
4435 code = ofputil_decode_action_unsafe(ia);
4437 case OFPUTIL_OFPAT_OUTPUT:
4438 xlate_output_action(ctx, &ia->output);
4441 case OFPUTIL_OFPAT_SET_VLAN_VID:
4442 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4443 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4446 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4447 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4448 ctx->flow.vlan_tci |= htons(
4449 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4452 case OFPUTIL_OFPAT_STRIP_VLAN:
4453 ctx->flow.vlan_tci = htons(0);
4456 case OFPUTIL_OFPAT_SET_DL_SRC:
4457 oada = ((struct ofp_action_dl_addr *) ia);
4458 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4461 case OFPUTIL_OFPAT_SET_DL_DST:
4462 oada = ((struct ofp_action_dl_addr *) ia);
4463 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4466 case OFPUTIL_OFPAT_SET_NW_SRC:
4467 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4470 case OFPUTIL_OFPAT_SET_NW_DST:
4471 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4474 case OFPUTIL_OFPAT_SET_NW_TOS:
4475 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4476 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4479 case OFPUTIL_OFPAT_SET_TP_SRC:
4480 ctx->flow.tp_src = ia->tp_port.tp_port;
4483 case OFPUTIL_OFPAT_SET_TP_DST:
4484 ctx->flow.tp_dst = ia->tp_port.tp_port;
4487 case OFPUTIL_OFPAT_ENQUEUE:
4488 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4491 case OFPUTIL_NXAST_RESUBMIT:
4492 nar = (const struct nx_action_resubmit *) ia;
4493 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4496 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4497 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4500 case OFPUTIL_NXAST_SET_TUNNEL:
4501 nast = (const struct nx_action_set_tunnel *) ia;
4502 tun_id = htonll(ntohl(nast->tun_id));
4503 ctx->flow.tun_id = tun_id;
4506 case OFPUTIL_NXAST_SET_QUEUE:
4507 nasq = (const struct nx_action_set_queue *) ia;
4508 xlate_set_queue_action(ctx, nasq);
4511 case OFPUTIL_NXAST_POP_QUEUE:
4512 ctx->flow.priority = ctx->original_priority;
4515 case OFPUTIL_NXAST_REG_MOVE:
4516 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4520 case OFPUTIL_NXAST_REG_LOAD:
4521 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4525 case OFPUTIL_NXAST_NOTE:
4526 /* Nothing to do. */
4529 case OFPUTIL_NXAST_SET_TUNNEL64:
4530 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4531 ctx->flow.tun_id = tun_id;
4534 case OFPUTIL_NXAST_MULTIPATH:
4535 nam = (const struct nx_action_multipath *) ia;
4536 multipath_execute(nam, &ctx->flow);
4539 case OFPUTIL_NXAST_AUTOPATH:
4540 naa = (const struct nx_action_autopath *) ia;
4541 xlate_autopath(ctx, naa);
4544 case OFPUTIL_NXAST_BUNDLE:
4545 ctx->ofproto->has_bundle_action = true;
4546 nab = (const struct nx_action_bundle *) ia;
4547 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4552 case OFPUTIL_NXAST_BUNDLE_LOAD:
4553 ctx->ofproto->has_bundle_action = true;
4554 nab = (const struct nx_action_bundle *) ia;
4555 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4559 case OFPUTIL_NXAST_OUTPUT_REG:
4560 naor = (const struct nx_action_output_reg *) ia;
4561 xlate_output_reg_action(ctx, naor);
4564 case OFPUTIL_NXAST_LEARN:
4565 ctx->has_learn = true;
4566 if (ctx->may_learn) {
4567 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4571 case OFPUTIL_NXAST_EXIT:
4577 /* We've let OFPP_NORMAL and the learning action look at the packet,
4578 * so drop it now if forwarding is disabled. */
4579 if (port && !stp_forward_in_state(port->stp_state)) {
4580 ofpbuf_clear(ctx->odp_actions);
4581 add_sflow_action(ctx);
4586 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4587 struct ofproto_dpif *ofproto, const struct flow *flow,
4588 ovs_be16 initial_tci, const struct ofpbuf *packet)
4590 ctx->ofproto = ofproto;
4592 ctx->base_flow = ctx->flow;
4593 ctx->base_flow.tun_id = 0;
4594 ctx->base_flow.vlan_tci = initial_tci;
4595 ctx->packet = packet;
4596 ctx->may_learn = packet != NULL;
4597 ctx->resubmit_hook = NULL;
4600 static struct ofpbuf *
4601 xlate_actions(struct action_xlate_ctx *ctx,
4602 const union ofp_action *in, size_t n_in)
4604 struct flow orig_flow = ctx->flow;
4606 COVERAGE_INC(ofproto_dpif_xlate);
4608 ctx->odp_actions = ofpbuf_new(512);
4609 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4611 ctx->may_set_up_flow = true;
4612 ctx->has_learn = false;
4613 ctx->has_normal = false;
4614 ctx->nf_output_iface = NF_OUT_DROP;
4617 ctx->original_priority = ctx->flow.priority;
4621 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4622 switch (ctx->ofproto->up.frag_handling) {
4623 case OFPC_FRAG_NORMAL:
4624 /* We must pretend that transport ports are unavailable. */
4625 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4626 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4629 case OFPC_FRAG_DROP:
4630 return ctx->odp_actions;
4632 case OFPC_FRAG_REASM:
4635 case OFPC_FRAG_NX_MATCH:
4636 /* Nothing to do. */
4641 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4642 ctx->may_set_up_flow = false;
4643 return ctx->odp_actions;
4645 add_sflow_action(ctx);
4646 do_xlate_actions(in, n_in, ctx);
4648 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4649 ctx->odp_actions->data,
4650 ctx->odp_actions->size)) {
4651 ctx->may_set_up_flow = false;
4653 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4655 compose_output_action(ctx, OFPP_LOCAL);
4658 add_mirror_actions(ctx, &orig_flow);
4659 fix_sflow_action(ctx);
4662 return ctx->odp_actions;
4665 /* OFPP_NORMAL implementation. */
4667 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4669 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4670 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4671 * the bundle on which the packet was received, returns the VLAN to which the
4674 * Both 'vid' and the return value are in the range 0...4095. */
4676 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4678 switch (in_bundle->vlan_mode) {
4679 case PORT_VLAN_ACCESS:
4680 return in_bundle->vlan;
4683 case PORT_VLAN_TRUNK:
4686 case PORT_VLAN_NATIVE_UNTAGGED:
4687 case PORT_VLAN_NATIVE_TAGGED:
4688 return vid ? vid : in_bundle->vlan;
4695 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4696 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4699 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4700 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4703 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4705 switch (in_bundle->vlan_mode) {
4706 case PORT_VLAN_ACCESS:
4709 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4710 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4711 "packet received on port %s configured as VLAN "
4712 "%"PRIu16" access port",
4713 in_bundle->ofproto->up.name, vid,
4714 in_bundle->name, in_bundle->vlan);
4720 case PORT_VLAN_NATIVE_UNTAGGED:
4721 case PORT_VLAN_NATIVE_TAGGED:
4723 /* Port must always carry its native VLAN. */
4727 case PORT_VLAN_TRUNK:
4728 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4730 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4731 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4732 "received on port %s not configured for trunking "
4734 in_bundle->ofproto->up.name, vid,
4735 in_bundle->name, vid);
4747 /* Given 'vlan', the VLAN that a packet belongs to, and
4748 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4749 * that should be included in the 802.1Q header. (If the return value is 0,
4750 * then the 802.1Q header should only be included in the packet if there is a
4753 * Both 'vlan' and the return value are in the range 0...4095. */
4755 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4757 switch (out_bundle->vlan_mode) {
4758 case PORT_VLAN_ACCESS:
4761 case PORT_VLAN_TRUNK:
4762 case PORT_VLAN_NATIVE_TAGGED:
4765 case PORT_VLAN_NATIVE_UNTAGGED:
4766 return vlan == out_bundle->vlan ? 0 : vlan;
4774 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4777 struct ofport_dpif *port;
4779 ovs_be16 tci, old_tci;
4781 vid = output_vlan_to_vid(out_bundle, vlan);
4782 if (!out_bundle->bond) {
4783 port = ofbundle_get_a_port(out_bundle);
4785 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4788 /* No slaves enabled, so drop packet. */
4793 old_tci = ctx->flow.vlan_tci;
4795 if (tci || out_bundle->use_priority_tags) {
4796 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4798 tci |= htons(VLAN_CFI);
4801 ctx->flow.vlan_tci = tci;
4803 compose_output_action(ctx, port->up.ofp_port);
4804 ctx->flow.vlan_tci = old_tci;
4808 mirror_mask_ffs(mirror_mask_t mask)
4810 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4815 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4817 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4818 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4822 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4824 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4827 /* Returns an arbitrary interface within 'bundle'. */
4828 static struct ofport_dpif *
4829 ofbundle_get_a_port(const struct ofbundle *bundle)
4831 return CONTAINER_OF(list_front(&bundle->ports),
4832 struct ofport_dpif, bundle_node);
4836 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4838 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4841 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4842 * to a VLAN. In general most packets may be mirrored but we want to drop
4843 * protocols that may confuse switches. */
4845 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4847 /* If you change this function's behavior, please update corresponding
4848 * documentation in vswitch.xml at the same time. */
4849 if (dst[0] != 0x01) {
4850 /* All the currently banned MACs happen to start with 01 currently, so
4851 * this is a quick way to eliminate most of the good ones. */
4853 if (eth_addr_is_reserved(dst)) {
4854 /* Drop STP, IEEE pause frames, and other reserved protocols
4855 * (01-80-c2-00-00-0x). */
4859 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4861 if ((dst[3] & 0xfe) == 0xcc &&
4862 (dst[4] & 0xfe) == 0xcc &&
4863 (dst[5] & 0xfe) == 0xcc) {
4864 /* Drop the following protocols plus others following the same
4867 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4868 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4869 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4873 if (!(dst[3] | dst[4] | dst[5])) {
4874 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4883 add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
4885 struct ofproto_dpif *ofproto = ctx->ofproto;
4886 mirror_mask_t mirrors;
4887 struct ofport_dpif *in_port;
4888 struct ofbundle *in_bundle;
4891 const struct nlattr *a;
4894 /* Obtain in_port from orig_flow.in_port.
4896 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
4897 in_port = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
4898 ctx->packet != NULL);
4902 in_bundle = in_port->bundle;
4903 mirrors = in_bundle->src_mirrors;
4905 /* Drop frames on bundles reserved for mirroring. */
4906 if (in_bundle->mirror_out) {
4907 if (ctx->packet != NULL) {
4908 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4909 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4910 "%s, which is reserved exclusively for mirroring",
4911 ctx->ofproto->up.name, in_bundle->name);
4917 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
4918 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
4921 vlan = input_vid_to_vlan(in_bundle, vid);
4923 /* Look at the output ports to check for destination selections. */
4925 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
4926 ctx->odp_actions->size) {
4927 enum ovs_action_attr type = nl_attr_type(a);
4928 struct ofport_dpif *ofport;
4930 if (type != OVS_ACTION_ATTR_OUTPUT) {
4934 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
4935 mirrors |= ofport ? ofport->bundle->dst_mirrors : 0;
4942 /* Restore the original packet before adding the mirror actions. */
4943 ctx->flow = *orig_flow;
4948 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4950 if (!vlan_is_mirrored(m, vlan)) {
4951 mirrors &= mirrors - 1;
4955 mirrors &= ~m->dup_mirrors;
4956 ctx->mirrors |= m->dup_mirrors;
4958 output_normal(ctx, m->out, vlan);
4959 } else if (eth_dst_may_rspan(orig_flow->dl_dst)
4960 && vlan != m->out_vlan) {
4961 struct ofbundle *bundle;
4963 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4964 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4965 && !bundle->mirror_out) {
4966 output_normal(ctx, bundle, m->out_vlan);
4974 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
4975 uint64_t packets, uint64_t bytes)
4981 for (; mirrors; mirrors &= mirrors - 1) {
4984 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4987 /* In normal circumstances 'm' will not be NULL. However,
4988 * if mirrors are reconfigured, we can temporarily get out
4989 * of sync in facet_revalidate(). We could "correct" the
4990 * mirror list before reaching here, but doing that would
4991 * not properly account the traffic stats we've currently
4992 * accumulated for previous mirror configuration. */
4996 m->packet_count += packets;
4997 m->byte_count += bytes;
5001 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
5002 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
5003 * indicate this; newer upstream kernels use gratuitous ARP requests. */
5005 is_gratuitous_arp(const struct flow *flow)
5007 return (flow->dl_type == htons(ETH_TYPE_ARP)
5008 && eth_addr_is_broadcast(flow->dl_dst)
5009 && (flow->nw_proto == ARP_OP_REPLY
5010 || (flow->nw_proto == ARP_OP_REQUEST
5011 && flow->nw_src == flow->nw_dst)));
5015 update_learning_table(struct ofproto_dpif *ofproto,
5016 const struct flow *flow, int vlan,
5017 struct ofbundle *in_bundle)
5019 struct mac_entry *mac;
5021 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
5025 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
5026 if (is_gratuitous_arp(flow)) {
5027 /* We don't want to learn from gratuitous ARP packets that are
5028 * reflected back over bond slaves so we lock the learning table. */
5029 if (!in_bundle->bond) {
5030 mac_entry_set_grat_arp_lock(mac);
5031 } else if (mac_entry_is_grat_arp_locked(mac)) {
5036 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
5037 /* The log messages here could actually be useful in debugging,
5038 * so keep the rate limit relatively high. */
5039 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
5040 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
5041 "on port %s in VLAN %d",
5042 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
5043 in_bundle->name, vlan);
5045 mac->port.p = in_bundle;
5046 tag_set_add(&ofproto->revalidate_set,
5047 mac_learning_changed(ofproto->ml, mac));
5051 static struct ofport_dpif *
5052 lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
5054 struct ofport_dpif *ofport;
5056 /* Find the port and bundle for the received packet. */
5057 ofport = get_ofp_port(ofproto, in_port);
5058 if (ofport && ofport->bundle) {
5062 /* Odd. A few possible reasons here:
5064 * - We deleted a port but there are still a few packets queued up
5067 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
5068 * we don't know about.
5070 * - The ofproto client didn't configure the port as part of a bundle.
5073 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5075 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
5076 "port %"PRIu16, ofproto->up.name, in_port);
5081 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
5082 * dropped. Returns true if they may be forwarded, false if they should be
5085 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
5086 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
5088 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
5089 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
5090 * checked by input_vid_is_valid().
5092 * May also add tags to '*tags', although the current implementation only does
5093 * so in one special case.
5096 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
5097 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
5099 struct ofbundle *in_bundle = in_port->bundle;
5101 /* Drop frames for reserved multicast addresses
5102 * only if forward_bpdu option is absent. */
5103 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
5107 if (in_bundle->bond) {
5108 struct mac_entry *mac;
5110 switch (bond_check_admissibility(in_bundle->bond, in_port,
5111 flow->dl_dst, tags)) {
5118 case BV_DROP_IF_MOVED:
5119 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
5120 if (mac && mac->port.p != in_bundle &&
5121 (!is_gratuitous_arp(flow)
5122 || mac_entry_is_grat_arp_locked(mac))) {
5133 xlate_normal(struct action_xlate_ctx *ctx)
5135 struct ofport_dpif *in_port;
5136 struct ofbundle *in_bundle;
5137 struct mac_entry *mac;
5141 ctx->has_normal = true;
5143 /* Obtain in_port from ctx->flow.in_port.
5145 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5146 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
5147 ctx->packet != NULL);
5151 in_bundle = in_port->bundle;
5153 /* Drop malformed frames. */
5154 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
5155 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
5156 if (ctx->packet != NULL) {
5157 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5158 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
5159 "VLAN tag received on port %s",
5160 ctx->ofproto->up.name, in_bundle->name);
5165 /* Drop frames on bundles reserved for mirroring. */
5166 if (in_bundle->mirror_out) {
5167 if (ctx->packet != NULL) {
5168 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5169 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5170 "%s, which is reserved exclusively for mirroring",
5171 ctx->ofproto->up.name, in_bundle->name);
5177 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
5178 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5181 vlan = input_vid_to_vlan(in_bundle, vid);
5183 /* Check other admissibility requirements. */
5184 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
5188 /* Learn source MAC. */
5189 if (ctx->may_learn) {
5190 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
5193 /* Determine output bundle. */
5194 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
5197 if (mac->port.p != in_bundle) {
5198 output_normal(ctx, mac->port.p, vlan);
5200 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
5201 /* If we are revalidating but don't have a learning entry then eject
5202 * the flow. Installing a flow that floods packets opens up a window
5203 * of time where we could learn from a packet reflected on a bond and
5204 * blackhole packets before the learning table is updated to reflect
5205 * the correct port. */
5206 ctx->may_set_up_flow = false;
5209 struct ofbundle *bundle;
5211 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
5212 if (bundle != in_bundle
5213 && ofbundle_includes_vlan(bundle, vlan)
5214 && bundle->floodable
5215 && !bundle->mirror_out) {
5216 output_normal(ctx, bundle, vlan);
5219 ctx->nf_output_iface = NF_OUT_FLOOD;
5223 /* Optimized flow revalidation.
5225 * It's a difficult problem, in general, to tell which facets need to have
5226 * their actions recalculated whenever the OpenFlow flow table changes. We
5227 * don't try to solve that general problem: for most kinds of OpenFlow flow
5228 * table changes, we recalculate the actions for every facet. This is
5229 * relatively expensive, but it's good enough if the OpenFlow flow table
5230 * doesn't change very often.
5232 * However, we can expect one particular kind of OpenFlow flow table change to
5233 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5234 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5235 * table, we add a special case that applies to flow tables in which every rule
5236 * has the same form (that is, the same wildcards), except that the table is
5237 * also allowed to have a single "catch-all" flow that matches all packets. We
5238 * optimize this case by tagging all of the facets that resubmit into the table
5239 * and invalidating the same tag whenever a flow changes in that table. The
5240 * end result is that we revalidate just the facets that need it (and sometimes
5241 * a few more, but not all of the facets or even all of the facets that
5242 * resubmit to the table modified by MAC learning). */
5244 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5245 * into an OpenFlow table with the given 'basis'. */
5247 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5250 if (flow_wildcards_is_catchall(wc)) {
5253 struct flow tag_flow = *flow;
5254 flow_zero_wildcards(&tag_flow, wc);
5255 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5259 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5260 * taggability of that table.
5262 * This function must be called after *each* change to a flow table. If you
5263 * skip calling it on some changes then the pointer comparisons at the end can
5264 * be invalid if you get unlucky. For example, if a flow removal causes a
5265 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5266 * different wildcards to be created with the same address, then this function
5267 * will incorrectly skip revalidation. */
5269 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5271 struct table_dpif *table = &ofproto->tables[table_id];
5272 const struct classifier *cls = &ofproto->up.tables[table_id];
5273 struct cls_table *catchall, *other;
5274 struct cls_table *t;
5276 catchall = other = NULL;
5278 switch (hmap_count(&cls->tables)) {
5280 /* We could tag this OpenFlow table but it would make the logic a
5281 * little harder and it's a corner case that doesn't seem worth it
5287 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5288 if (cls_table_is_catchall(t)) {
5290 } else if (!other) {
5293 /* Indicate that we can't tag this by setting both tables to
5294 * NULL. (We know that 'catchall' is already NULL.) */
5301 /* Can't tag this table. */
5305 if (table->catchall_table != catchall || table->other_table != other) {
5306 table->catchall_table = catchall;
5307 table->other_table = other;
5308 ofproto->need_revalidate = true;
5312 /* Given 'rule' that has changed in some way (either it is a rule being
5313 * inserted, a rule being deleted, or a rule whose actions are being
5314 * modified), marks facets for revalidation to ensure that packets will be
5315 * forwarded correctly according to the new state of the flow table.
5317 * This function must be called after *each* change to a flow table. See
5318 * the comment on table_update_taggable() for more information. */
5320 rule_invalidate(const struct rule_dpif *rule)
5322 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5324 table_update_taggable(ofproto, rule->up.table_id);
5326 if (!ofproto->need_revalidate) {
5327 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5329 if (table->other_table && rule->tag) {
5330 tag_set_add(&ofproto->revalidate_set, rule->tag);
5332 ofproto->need_revalidate = true;
5338 set_frag_handling(struct ofproto *ofproto_,
5339 enum ofp_config_flags frag_handling)
5341 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5343 if (frag_handling != OFPC_FRAG_REASM) {
5344 ofproto->need_revalidate = true;
5352 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5353 const struct flow *flow,
5354 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5356 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5359 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5360 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5363 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5364 ofproto->max_ports);
5366 struct odputil_keybuf keybuf;
5367 struct action_xlate_ctx ctx;
5368 struct ofpbuf *odp_actions;
5371 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5372 odp_flow_key_from_flow(&key, flow);
5374 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
5375 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5376 dpif_execute(ofproto->dpif, key.data, key.size,
5377 odp_actions->data, odp_actions->size, packet);
5378 ofpbuf_delete(odp_actions);
5386 set_netflow(struct ofproto *ofproto_,
5387 const struct netflow_options *netflow_options)
5389 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5391 if (netflow_options) {
5392 if (!ofproto->netflow) {
5393 ofproto->netflow = netflow_create();
5395 return netflow_set_options(ofproto->netflow, netflow_options);
5397 netflow_destroy(ofproto->netflow);
5398 ofproto->netflow = NULL;
5404 get_netflow_ids(const struct ofproto *ofproto_,
5405 uint8_t *engine_type, uint8_t *engine_id)
5407 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5409 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5413 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5415 if (!facet_is_controller_flow(facet) &&
5416 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5417 struct subfacet *subfacet;
5418 struct ofexpired expired;
5420 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5421 if (subfacet->installed) {
5422 struct dpif_flow_stats stats;
5424 subfacet_install(ofproto, subfacet, subfacet->actions,
5425 subfacet->actions_len, &stats);
5426 subfacet_update_stats(ofproto, subfacet, &stats);
5430 expired.flow = facet->flow;
5431 expired.packet_count = facet->packet_count;
5432 expired.byte_count = facet->byte_count;
5433 expired.used = facet->used;
5434 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5439 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5441 struct facet *facet;
5443 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5444 send_active_timeout(ofproto, facet);
5448 static struct ofproto_dpif *
5449 ofproto_dpif_lookup(const char *name)
5451 struct ofproto *ofproto = ofproto_lookup(name);
5452 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5453 ? ofproto_dpif_cast(ofproto)
5458 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5459 const char *args, void *aux OVS_UNUSED)
5461 const struct ofproto_dpif *ofproto;
5463 ofproto = ofproto_dpif_lookup(args);
5465 unixctl_command_reply(conn, 501, "no such bridge");
5468 mac_learning_flush(ofproto->ml);
5470 unixctl_command_reply(conn, 200, "table successfully flushed");
5474 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5475 const char *args, void *aux OVS_UNUSED)
5477 struct ds ds = DS_EMPTY_INITIALIZER;
5478 const struct ofproto_dpif *ofproto;
5479 const struct mac_entry *e;
5481 ofproto = ofproto_dpif_lookup(args);
5483 unixctl_command_reply(conn, 501, "no such bridge");
5487 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5488 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5489 struct ofbundle *bundle = e->port.p;
5490 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5491 ofbundle_get_a_port(bundle)->odp_port,
5492 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5494 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5498 struct ofproto_trace {
5499 struct action_xlate_ctx ctx;
5505 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5506 const struct rule_dpif *rule)
5508 ds_put_char_multiple(result, '\t', level);
5510 ds_put_cstr(result, "No match\n");
5514 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5515 table_id, ntohll(rule->up.flow_cookie));
5516 cls_rule_format(&rule->up.cr, result);
5517 ds_put_char(result, '\n');
5519 ds_put_char_multiple(result, '\t', level);
5520 ds_put_cstr(result, "OpenFlow ");
5521 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5522 ds_put_char(result, '\n');
5526 trace_format_flow(struct ds *result, int level, const char *title,
5527 struct ofproto_trace *trace)
5529 ds_put_char_multiple(result, '\t', level);
5530 ds_put_format(result, "%s: ", title);
5531 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5532 ds_put_cstr(result, "unchanged");
5534 flow_format(result, &trace->ctx.flow);
5535 trace->flow = trace->ctx.flow;
5537 ds_put_char(result, '\n');
5541 trace_format_regs(struct ds *result, int level, const char *title,
5542 struct ofproto_trace *trace)
5546 ds_put_char_multiple(result, '\t', level);
5547 ds_put_format(result, "%s:", title);
5548 for (i = 0; i < FLOW_N_REGS; i++) {
5549 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5551 ds_put_char(result, '\n');
5555 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5557 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5558 struct ds *result = trace->result;
5560 ds_put_char(result, '\n');
5561 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5562 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5563 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5567 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5568 void *aux OVS_UNUSED)
5570 char *dpname, *arg1, *arg2, *arg3, *arg4;
5571 char *args = xstrdup(args_);
5572 char *save_ptr = NULL;
5573 struct ofproto_dpif *ofproto;
5574 struct ofpbuf odp_key;
5575 struct ofpbuf *packet;
5576 struct rule_dpif *rule;
5577 ovs_be16 initial_tci;
5583 ofpbuf_init(&odp_key, 0);
5586 dpname = strtok_r(args, " ", &save_ptr);
5588 unixctl_command_reply(conn, 501, "Bad command syntax");
5592 ofproto = ofproto_dpif_lookup(dpname);
5594 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5598 arg1 = strtok_r(NULL, " ", &save_ptr);
5599 arg2 = strtok_r(NULL, " ", &save_ptr);
5600 arg3 = strtok_r(NULL, " ", &save_ptr);
5601 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5602 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5603 /* ofproto/trace dpname flow [-generate] */
5606 /* Convert string to datapath key. */
5607 ofpbuf_init(&odp_key, 0);
5608 error = odp_flow_key_from_string(arg1, NULL, &odp_key);
5610 unixctl_command_reply(conn, 501, "Bad flow syntax");
5614 /* Convert odp_key to flow. */
5615 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
5616 odp_key.size, &flow,
5618 if (error == ODP_FIT_ERROR) {
5619 unixctl_command_reply(conn, 501, "Invalid flow");
5623 /* Generate a packet, if requested. */
5625 packet = ofpbuf_new(0);
5626 flow_compose(packet, &flow);
5628 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5629 /* ofproto/trace dpname priority tun_id in_port packet */
5634 priority = atoi(arg1);
5635 tun_id = htonll(strtoull(arg2, NULL, 0));
5636 in_port = ofp_port_to_odp_port(atoi(arg3));
5638 packet = ofpbuf_new(strlen(args) / 2);
5639 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5640 arg4 += strspn(arg4, " ");
5641 if (*arg4 != '\0') {
5642 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5645 if (packet->size < ETH_HEADER_LEN) {
5646 unixctl_command_reply(conn, 501,
5647 "Packet data too short for Ethernet");
5651 ds_put_cstr(&result, "Packet: ");
5652 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5653 ds_put_cstr(&result, s);
5656 flow_extract(packet, priority, tun_id, in_port, &flow);
5657 initial_tci = flow.vlan_tci;
5659 unixctl_command_reply(conn, 501, "Bad command syntax");
5663 ds_put_cstr(&result, "Flow: ");
5664 flow_format(&result, &flow);
5665 ds_put_char(&result, '\n');
5667 rule = rule_dpif_lookup(ofproto, &flow, 0);
5668 trace_format_rule(&result, 0, 0, rule);
5670 struct ofproto_trace trace;
5671 struct ofpbuf *odp_actions;
5673 trace.result = &result;
5675 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, initial_tci, packet);
5676 trace.ctx.resubmit_hook = trace_resubmit;
5677 odp_actions = xlate_actions(&trace.ctx,
5678 rule->up.actions, rule->up.n_actions);
5680 ds_put_char(&result, '\n');
5681 trace_format_flow(&result, 0, "Final flow", &trace);
5682 ds_put_cstr(&result, "Datapath actions: ");
5683 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5684 ofpbuf_delete(odp_actions);
5686 if (!trace.ctx.may_set_up_flow) {
5688 ds_put_cstr(&result, "\nThis flow is not cachable.");
5690 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5691 "for complete actions, please supply a packet.");
5696 unixctl_command_reply(conn, 200, ds_cstr(&result));
5699 ds_destroy(&result);
5700 ofpbuf_delete(packet);
5701 ofpbuf_uninit(&odp_key);
5706 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5707 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5710 unixctl_command_reply(conn, 200, NULL);
5714 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5715 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5718 unixctl_command_reply(conn, 200, NULL);
5722 ofproto_dpif_unixctl_init(void)
5724 static bool registered;
5730 unixctl_command_register("ofproto/trace",
5731 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5732 ofproto_unixctl_trace, NULL);
5733 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5735 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5737 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5738 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5741 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5743 * This is deprecated. It is only for compatibility with broken device drivers
5744 * in old versions of Linux that do not properly support VLANs when VLAN
5745 * devices are not used. When broken device drivers are no longer in
5746 * widespread use, we will delete these interfaces. */
5749 set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
5751 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5752 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5754 if (realdev_ofp_port == ofport->realdev_ofp_port
5755 && vid == ofport->vlandev_vid) {
5759 ofproto->need_revalidate = true;
5761 if (ofport->realdev_ofp_port) {
5764 if (realdev_ofp_port && ofport->bundle) {
5765 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5766 * themselves be part of a bundle. */
5767 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
5770 ofport->realdev_ofp_port = realdev_ofp_port;
5771 ofport->vlandev_vid = vid;
5773 if (realdev_ofp_port) {
5774 vsp_add(ofport, realdev_ofp_port, vid);
5781 hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
5783 return hash_2words(realdev_ofp_port, vid);
5787 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5788 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
5790 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5791 uint16_t realdev_ofp_port = odp_port_to_ofp_port(realdev_odp_port);
5792 int vid = vlan_tci_to_vid(vlan_tci);
5793 const struct vlan_splinter *vsp;
5795 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5796 hash_realdev_vid(realdev_ofp_port, vid),
5797 &ofproto->realdev_vid_map) {
5798 if (vsp->realdev_ofp_port == realdev_ofp_port
5799 && vsp->vid == vid) {
5800 return ofp_port_to_odp_port(vsp->vlandev_ofp_port);
5804 return realdev_odp_port;
5807 static struct vlan_splinter *
5808 vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
5810 struct vlan_splinter *vsp;
5812 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
5813 &ofproto->vlandev_map) {
5814 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5823 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5824 uint16_t vlandev_ofp_port, int *vid)
5826 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5827 const struct vlan_splinter *vsp;
5829 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5834 return vsp->realdev_ofp_port;
5841 vsp_remove(struct ofport_dpif *port)
5843 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5844 struct vlan_splinter *vsp;
5846 vsp = vlandev_find(ofproto, port->up.ofp_port);
5848 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5849 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5852 port->realdev_ofp_port = 0;
5854 VLOG_ERR("missing vlan device record");
5859 vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
5861 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5863 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5864 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
5865 == realdev_ofp_port)) {
5866 struct vlan_splinter *vsp;
5868 vsp = xmalloc(sizeof *vsp);
5869 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5870 hash_int(port->up.ofp_port, 0));
5871 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5872 hash_realdev_vid(realdev_ofp_port, vid));
5873 vsp->realdev_ofp_port = realdev_ofp_port;
5874 vsp->vlandev_ofp_port = port->up.ofp_port;
5877 port->realdev_ofp_port = realdev_ofp_port;
5879 VLOG_ERR("duplicate vlan device record");
5883 const struct ofproto_class ofproto_dpif_class = {
5911 port_is_lacp_current,
5912 NULL, /* rule_choose_table */
5919 rule_modify_actions,
5927 get_cfm_remote_mpids,
5931 get_stp_port_status,
5938 is_mirror_output_bundle,
5939 forward_bpdu_changed,