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 const struct flow *);
378 static void subfacet_destroy(struct ofproto_dpif *, struct subfacet *);
379 static void subfacet_destroy__(struct ofproto_dpif *, struct subfacet *);
380 static void subfacet_reset_dp_stats(struct subfacet *,
381 struct dpif_flow_stats *);
382 static void subfacet_update_time(struct ofproto_dpif *, struct subfacet *,
384 static void subfacet_update_stats(struct ofproto_dpif *, struct subfacet *,
385 const struct dpif_flow_stats *);
386 static void subfacet_make_actions(struct ofproto_dpif *, struct subfacet *,
387 const struct ofpbuf *packet);
388 static int subfacet_install(struct ofproto_dpif *, struct subfacet *,
389 const struct nlattr *actions, size_t actions_len,
390 struct dpif_flow_stats *);
391 static void subfacet_uninstall(struct ofproto_dpif *, struct subfacet *);
397 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
398 struct list bundle_node; /* In struct ofbundle's "ports" list. */
399 struct cfm *cfm; /* Connectivity Fault Management, if any. */
400 tag_type tag; /* Tag associated with this port. */
401 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
402 bool may_enable; /* May be enabled in bonds. */
405 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
406 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
407 long long int stp_state_entered;
409 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
411 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
413 * This is deprecated. It is only for compatibility with broken device
414 * drivers in old versions of Linux that do not properly support VLANs when
415 * VLAN devices are not used. When broken device drivers are no longer in
416 * widespread use, we will delete these interfaces. */
417 uint16_t realdev_ofp_port;
421 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
422 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
423 * traffic egressing the 'ofport' with that priority should be marked with. */
424 struct priority_to_dscp {
425 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
426 uint32_t priority; /* Priority of this queue (see struct flow). */
428 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
431 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
433 * This is deprecated. It is only for compatibility with broken device drivers
434 * in old versions of Linux that do not properly support VLANs when VLAN
435 * devices are not used. When broken device drivers are no longer in
436 * widespread use, we will delete these interfaces. */
437 struct vlan_splinter {
438 struct hmap_node realdev_vid_node;
439 struct hmap_node vlandev_node;
440 uint16_t realdev_ofp_port;
441 uint16_t vlandev_ofp_port;
445 static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
446 uint32_t realdev, ovs_be16 vlan_tci);
447 static uint16_t vsp_vlandev_to_realdev(const struct ofproto_dpif *,
448 uint16_t vlandev, int *vid);
449 static void vsp_remove(struct ofport_dpif *);
450 static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
452 static struct ofport_dpif *
453 ofport_dpif_cast(const struct ofport *ofport)
455 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
456 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
459 static void port_run(struct ofport_dpif *);
460 static void port_wait(struct ofport_dpif *);
461 static int set_cfm(struct ofport *, const struct cfm_settings *);
462 static void ofport_clear_priorities(struct ofport_dpif *);
464 struct dpif_completion {
465 struct list list_node;
466 struct ofoperation *op;
469 /* Extra information about a classifier table.
470 * Currently used just for optimized flow revalidation. */
472 /* If either of these is nonnull, then this table has a form that allows
473 * flows to be tagged to avoid revalidating most flows for the most common
474 * kinds of flow table changes. */
475 struct cls_table *catchall_table; /* Table that wildcards all fields. */
476 struct cls_table *other_table; /* Table with any other wildcard set. */
477 uint32_t basis; /* Keeps each table's tags separate. */
480 struct ofproto_dpif {
489 struct netflow *netflow;
490 struct dpif_sflow *sflow;
491 struct hmap bundles; /* Contains "struct ofbundle"s. */
492 struct mac_learning *ml;
493 struct ofmirror *mirrors[MAX_MIRRORS];
494 bool has_bonded_bundles;
497 struct timer next_expiration;
501 struct hmap subfacets;
504 struct table_dpif tables[N_TABLES];
505 bool need_revalidate;
506 struct tag_set revalidate_set;
508 /* Support for debugging async flow mods. */
509 struct list completions;
511 bool has_bundle_action; /* True when the first bundle action appears. */
515 long long int stp_last_tick;
517 /* VLAN splinters. */
518 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
519 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
522 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
523 * for debugging the asynchronous flow_mod implementation.) */
526 static void ofproto_dpif_unixctl_init(void);
528 static struct ofproto_dpif *
529 ofproto_dpif_cast(const struct ofproto *ofproto)
531 assert(ofproto->ofproto_class == &ofproto_dpif_class);
532 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
535 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
537 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
540 /* Packet processing. */
541 static void update_learning_table(struct ofproto_dpif *,
542 const struct flow *, int vlan,
545 #define FLOW_MISS_MAX_BATCH 50
546 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
548 /* Flow expiration. */
549 static int expire(struct ofproto_dpif *);
552 static void send_netflow_active_timeouts(struct ofproto_dpif *);
555 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
557 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
558 const struct flow *, uint32_t odp_port);
559 static void add_mirror_actions(struct action_xlate_ctx *ctx,
560 const struct flow *flow);
561 /* Global variables. */
562 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
564 /* Factory functions. */
567 enumerate_types(struct sset *types)
569 dp_enumerate_types(types);
573 enumerate_names(const char *type, struct sset *names)
575 return dp_enumerate_names(type, names);
579 del(const char *type, const char *name)
584 error = dpif_open(name, type, &dpif);
586 error = dpif_delete(dpif);
592 /* Basic life-cycle. */
594 static struct ofproto *
597 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
602 dealloc(struct ofproto *ofproto_)
604 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
609 construct(struct ofproto *ofproto_, int *n_tablesp)
611 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
612 const char *name = ofproto->up.name;
616 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
618 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
622 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
623 ofproto->n_matches = 0;
625 dpif_flow_flush(ofproto->dpif);
626 dpif_recv_purge(ofproto->dpif);
628 error = dpif_recv_set_mask(ofproto->dpif,
629 ((1u << DPIF_UC_MISS) |
630 (1u << DPIF_UC_ACTION)));
632 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
633 dpif_close(ofproto->dpif);
637 ofproto->netflow = NULL;
638 ofproto->sflow = NULL;
640 hmap_init(&ofproto->bundles);
641 ofproto->ml = mac_learning_create();
642 for (i = 0; i < MAX_MIRRORS; i++) {
643 ofproto->mirrors[i] = NULL;
645 ofproto->has_bonded_bundles = false;
647 timer_set_duration(&ofproto->next_expiration, 1000);
649 hmap_init(&ofproto->facets);
650 hmap_init(&ofproto->subfacets);
652 for (i = 0; i < N_TABLES; i++) {
653 struct table_dpif *table = &ofproto->tables[i];
655 table->catchall_table = NULL;
656 table->other_table = NULL;
657 table->basis = random_uint32();
659 ofproto->need_revalidate = false;
660 tag_set_init(&ofproto->revalidate_set);
662 list_init(&ofproto->completions);
664 ofproto_dpif_unixctl_init();
666 ofproto->has_bundle_action = false;
668 hmap_init(&ofproto->vlandev_map);
669 hmap_init(&ofproto->realdev_vid_map);
671 *n_tablesp = N_TABLES;
676 complete_operations(struct ofproto_dpif *ofproto)
678 struct dpif_completion *c, *next;
680 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
681 ofoperation_complete(c->op, 0);
682 list_remove(&c->list_node);
688 destruct(struct ofproto *ofproto_)
690 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
691 struct rule_dpif *rule, *next_rule;
692 struct classifier *table;
695 complete_operations(ofproto);
697 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
698 struct cls_cursor cursor;
700 cls_cursor_init(&cursor, table, NULL);
701 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
702 ofproto_rule_destroy(&rule->up);
706 for (i = 0; i < MAX_MIRRORS; i++) {
707 mirror_destroy(ofproto->mirrors[i]);
710 netflow_destroy(ofproto->netflow);
711 dpif_sflow_destroy(ofproto->sflow);
712 hmap_destroy(&ofproto->bundles);
713 mac_learning_destroy(ofproto->ml);
715 hmap_destroy(&ofproto->facets);
716 hmap_destroy(&ofproto->subfacets);
718 hmap_destroy(&ofproto->vlandev_map);
719 hmap_destroy(&ofproto->realdev_vid_map);
721 dpif_close(ofproto->dpif);
725 run_fast(struct ofproto *ofproto_)
727 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
730 /* Handle one or more batches of upcalls, until there's nothing left to do
731 * or until we do a fixed total amount of work.
733 * We do work in batches because it can be much cheaper to set up a number
734 * of flows and fire off their patches all at once. We do multiple batches
735 * because in some cases handling a packet can cause another packet to be
736 * queued almost immediately as part of the return flow. Both
737 * optimizations can make major improvements on some benchmarks and
738 * presumably for real traffic as well. */
740 while (work < FLOW_MISS_MAX_BATCH) {
741 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
751 run(struct ofproto *ofproto_)
753 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
754 struct ofport_dpif *ofport;
755 struct ofbundle *bundle;
759 complete_operations(ofproto);
761 dpif_run(ofproto->dpif);
763 error = run_fast(ofproto_);
768 if (timer_expired(&ofproto->next_expiration)) {
769 int delay = expire(ofproto);
770 timer_set_duration(&ofproto->next_expiration, delay);
773 if (ofproto->netflow) {
774 if (netflow_run(ofproto->netflow)) {
775 send_netflow_active_timeouts(ofproto);
778 if (ofproto->sflow) {
779 dpif_sflow_run(ofproto->sflow);
782 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
785 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
790 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
792 /* Now revalidate if there's anything to do. */
793 if (ofproto->need_revalidate
794 || !tag_set_is_empty(&ofproto->revalidate_set)) {
795 struct tag_set revalidate_set = ofproto->revalidate_set;
796 bool revalidate_all = ofproto->need_revalidate;
797 struct facet *facet, *next;
799 /* Clear the revalidation flags. */
800 tag_set_init(&ofproto->revalidate_set);
801 ofproto->need_revalidate = false;
803 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
805 || tag_set_intersects(&revalidate_set, facet->tags)) {
806 facet_revalidate(ofproto, facet);
815 wait(struct ofproto *ofproto_)
817 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
818 struct ofport_dpif *ofport;
819 struct ofbundle *bundle;
821 if (!clogged && !list_is_empty(&ofproto->completions)) {
822 poll_immediate_wake();
825 dpif_wait(ofproto->dpif);
826 dpif_recv_wait(ofproto->dpif);
827 if (ofproto->sflow) {
828 dpif_sflow_wait(ofproto->sflow);
830 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
831 poll_immediate_wake();
833 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
836 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
839 if (ofproto->netflow) {
840 netflow_wait(ofproto->netflow);
842 mac_learning_wait(ofproto->ml);
844 if (ofproto->need_revalidate) {
845 /* Shouldn't happen, but if it does just go around again. */
846 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
847 poll_immediate_wake();
849 timer_wait(&ofproto->next_expiration);
854 flush(struct ofproto *ofproto_)
856 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
857 struct facet *facet, *next_facet;
859 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
860 /* Mark the facet as not installed so that facet_remove() doesn't
861 * bother trying to uninstall it. There is no point in uninstalling it
862 * individually since we are about to blow away all the facets with
863 * dpif_flow_flush(). */
864 struct subfacet *subfacet;
866 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
867 subfacet->installed = false;
868 subfacet->dp_packet_count = 0;
869 subfacet->dp_byte_count = 0;
871 facet_remove(ofproto, facet);
873 dpif_flow_flush(ofproto->dpif);
877 get_features(struct ofproto *ofproto_ OVS_UNUSED,
878 bool *arp_match_ip, uint32_t *actions)
880 *arp_match_ip = true;
881 *actions = ((1u << OFPAT_OUTPUT) |
882 (1u << OFPAT_SET_VLAN_VID) |
883 (1u << OFPAT_SET_VLAN_PCP) |
884 (1u << OFPAT_STRIP_VLAN) |
885 (1u << OFPAT_SET_DL_SRC) |
886 (1u << OFPAT_SET_DL_DST) |
887 (1u << OFPAT_SET_NW_SRC) |
888 (1u << OFPAT_SET_NW_DST) |
889 (1u << OFPAT_SET_NW_TOS) |
890 (1u << OFPAT_SET_TP_SRC) |
891 (1u << OFPAT_SET_TP_DST) |
892 (1u << OFPAT_ENQUEUE));
896 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
898 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
899 struct dpif_dp_stats s;
901 strcpy(ots->name, "classifier");
903 dpif_get_dp_stats(ofproto->dpif, &s);
904 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
905 put_32aligned_be64(&ots->matched_count,
906 htonll(s.n_hit + ofproto->n_matches));
909 static struct ofport *
912 struct ofport_dpif *port = xmalloc(sizeof *port);
917 port_dealloc(struct ofport *port_)
919 struct ofport_dpif *port = ofport_dpif_cast(port_);
924 port_construct(struct ofport *port_)
926 struct ofport_dpif *port = ofport_dpif_cast(port_);
927 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
929 ofproto->need_revalidate = true;
930 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
933 port->tag = tag_create_random();
934 port->may_enable = true;
935 port->stp_port = NULL;
936 port->stp_state = STP_DISABLED;
937 hmap_init(&port->priorities);
938 port->realdev_ofp_port = 0;
939 port->vlandev_vid = 0;
941 if (ofproto->sflow) {
942 dpif_sflow_add_port(ofproto->sflow, port->odp_port,
943 netdev_get_name(port->up.netdev));
950 port_destruct(struct ofport *port_)
952 struct ofport_dpif *port = ofport_dpif_cast(port_);
953 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
955 ofproto->need_revalidate = true;
956 bundle_remove(port_);
957 set_cfm(port_, NULL);
958 if (ofproto->sflow) {
959 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
962 ofport_clear_priorities(port);
963 hmap_destroy(&port->priorities);
967 port_modified(struct ofport *port_)
969 struct ofport_dpif *port = ofport_dpif_cast(port_);
971 if (port->bundle && port->bundle->bond) {
972 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
977 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
979 struct ofport_dpif *port = ofport_dpif_cast(port_);
980 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
981 ovs_be32 changed = old_config ^ port->up.opp.config;
983 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
984 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
985 ofproto->need_revalidate = true;
987 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
988 bundle_update(port->bundle);
994 set_sflow(struct ofproto *ofproto_,
995 const struct ofproto_sflow_options *sflow_options)
997 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
998 struct dpif_sflow *ds = ofproto->sflow;
1000 if (sflow_options) {
1002 struct ofport_dpif *ofport;
1004 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
1005 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1006 dpif_sflow_add_port(ds, ofport->odp_port,
1007 netdev_get_name(ofport->up.netdev));
1009 ofproto->need_revalidate = true;
1011 dpif_sflow_set_options(ds, sflow_options);
1014 dpif_sflow_destroy(ds);
1015 ofproto->need_revalidate = true;
1016 ofproto->sflow = NULL;
1023 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1025 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1032 struct ofproto_dpif *ofproto;
1034 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1035 ofproto->need_revalidate = true;
1036 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
1039 if (cfm_configure(ofport->cfm, s)) {
1045 cfm_destroy(ofport->cfm);
1051 get_cfm_fault(const struct ofport *ofport_)
1053 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1055 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
1059 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1062 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1065 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1072 /* Spanning Tree. */
1075 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1077 struct ofproto_dpif *ofproto = ofproto_;
1078 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1079 struct ofport_dpif *ofport;
1081 ofport = stp_port_get_aux(sp);
1083 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1084 ofproto->up.name, port_num);
1086 struct eth_header *eth = pkt->l2;
1088 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1089 if (eth_addr_is_zero(eth->eth_src)) {
1090 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1091 "with unknown MAC", ofproto->up.name, port_num);
1093 send_packet(ofport, pkt);
1099 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1101 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1103 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1105 /* Only revalidate flows if the configuration changed. */
1106 if (!s != !ofproto->stp) {
1107 ofproto->need_revalidate = true;
1111 if (!ofproto->stp) {
1112 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1113 send_bpdu_cb, ofproto);
1114 ofproto->stp_last_tick = time_msec();
1117 stp_set_bridge_id(ofproto->stp, s->system_id);
1118 stp_set_bridge_priority(ofproto->stp, s->priority);
1119 stp_set_hello_time(ofproto->stp, s->hello_time);
1120 stp_set_max_age(ofproto->stp, s->max_age);
1121 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1123 stp_destroy(ofproto->stp);
1124 ofproto->stp = NULL;
1131 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1133 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1137 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1138 s->designated_root = stp_get_designated_root(ofproto->stp);
1139 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1148 update_stp_port_state(struct ofport_dpif *ofport)
1150 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1151 enum stp_state state;
1153 /* Figure out new state. */
1154 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1158 if (ofport->stp_state != state) {
1162 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1163 netdev_get_name(ofport->up.netdev),
1164 stp_state_name(ofport->stp_state),
1165 stp_state_name(state));
1166 if (stp_learn_in_state(ofport->stp_state)
1167 != stp_learn_in_state(state)) {
1168 /* xxx Learning action flows should also be flushed. */
1169 mac_learning_flush(ofproto->ml);
1171 fwd_change = stp_forward_in_state(ofport->stp_state)
1172 != stp_forward_in_state(state);
1174 ofproto->need_revalidate = true;
1175 ofport->stp_state = state;
1176 ofport->stp_state_entered = time_msec();
1178 if (fwd_change && ofport->bundle) {
1179 bundle_update(ofport->bundle);
1182 /* Update the STP state bits in the OpenFlow port description. */
1183 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1184 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1185 : state == STP_LEARNING ? OFPPS_STP_LEARN
1186 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1187 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1189 ofproto_port_set_state(&ofport->up, of_state);
1193 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1194 * caller is responsible for assigning STP port numbers and ensuring
1195 * there are no duplicates. */
1197 set_stp_port(struct ofport *ofport_,
1198 const struct ofproto_port_stp_settings *s)
1200 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1201 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1202 struct stp_port *sp = ofport->stp_port;
1204 if (!s || !s->enable) {
1206 ofport->stp_port = NULL;
1207 stp_port_disable(sp);
1208 update_stp_port_state(ofport);
1211 } else if (sp && stp_port_no(sp) != s->port_num
1212 && ofport == stp_port_get_aux(sp)) {
1213 /* The port-id changed, so disable the old one if it's not
1214 * already in use by another port. */
1215 stp_port_disable(sp);
1218 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1219 stp_port_enable(sp);
1221 stp_port_set_aux(sp, ofport);
1222 stp_port_set_priority(sp, s->priority);
1223 stp_port_set_path_cost(sp, s->path_cost);
1225 update_stp_port_state(ofport);
1231 get_stp_port_status(struct ofport *ofport_,
1232 struct ofproto_port_stp_status *s)
1234 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1235 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1236 struct stp_port *sp = ofport->stp_port;
1238 if (!ofproto->stp || !sp) {
1244 s->port_id = stp_port_get_id(sp);
1245 s->state = stp_port_get_state(sp);
1246 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1247 s->role = stp_port_get_role(sp);
1248 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1254 stp_run(struct ofproto_dpif *ofproto)
1257 long long int now = time_msec();
1258 long long int elapsed = now - ofproto->stp_last_tick;
1259 struct stp_port *sp;
1262 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1263 ofproto->stp_last_tick = now;
1265 while (stp_get_changed_port(ofproto->stp, &sp)) {
1266 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1269 update_stp_port_state(ofport);
1276 stp_wait(struct ofproto_dpif *ofproto)
1279 poll_timer_wait(1000);
1283 /* Returns true if STP should process 'flow'. */
1285 stp_should_process_flow(const struct flow *flow)
1287 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1291 stp_process_packet(const struct ofport_dpif *ofport,
1292 const struct ofpbuf *packet)
1294 struct ofpbuf payload = *packet;
1295 struct eth_header *eth = payload.data;
1296 struct stp_port *sp = ofport->stp_port;
1298 /* Sink packets on ports that have STP disabled when the bridge has
1300 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1304 /* Trim off padding on payload. */
1305 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1306 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1309 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1310 stp_received_bpdu(sp, payload.data, payload.size);
1314 static struct priority_to_dscp *
1315 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1317 struct priority_to_dscp *pdscp;
1320 hash = hash_int(priority, 0);
1321 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1322 if (pdscp->priority == priority) {
1330 ofport_clear_priorities(struct ofport_dpif *ofport)
1332 struct priority_to_dscp *pdscp, *next;
1334 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1335 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1341 set_queues(struct ofport *ofport_,
1342 const struct ofproto_port_queue *qdscp_list,
1345 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1346 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1347 struct hmap new = HMAP_INITIALIZER(&new);
1350 for (i = 0; i < n_qdscp; i++) {
1351 struct priority_to_dscp *pdscp;
1355 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1356 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1361 pdscp = get_priority(ofport, priority);
1363 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1365 pdscp = xmalloc(sizeof *pdscp);
1366 pdscp->priority = priority;
1368 ofproto->need_revalidate = true;
1371 if (pdscp->dscp != dscp) {
1373 ofproto->need_revalidate = true;
1376 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1379 if (!hmap_is_empty(&ofport->priorities)) {
1380 ofport_clear_priorities(ofport);
1381 ofproto->need_revalidate = true;
1384 hmap_swap(&new, &ofport->priorities);
1392 /* Expires all MAC learning entries associated with 'port' and forces ofproto
1393 * to revalidate every flow. */
1395 bundle_flush_macs(struct ofbundle *bundle)
1397 struct ofproto_dpif *ofproto = bundle->ofproto;
1398 struct mac_learning *ml = ofproto->ml;
1399 struct mac_entry *mac, *next_mac;
1401 ofproto->need_revalidate = true;
1402 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1403 if (mac->port.p == bundle) {
1404 mac_learning_expire(ml, mac);
1409 static struct ofbundle *
1410 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1412 struct ofbundle *bundle;
1414 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1415 &ofproto->bundles) {
1416 if (bundle->aux == aux) {
1423 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1424 * ones that are found to 'bundles'. */
1426 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1427 void **auxes, size_t n_auxes,
1428 struct hmapx *bundles)
1432 hmapx_init(bundles);
1433 for (i = 0; i < n_auxes; i++) {
1434 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1436 hmapx_add(bundles, bundle);
1442 bundle_update(struct ofbundle *bundle)
1444 struct ofport_dpif *port;
1446 bundle->floodable = true;
1447 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1448 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1449 bundle->floodable = false;
1456 bundle_del_port(struct ofport_dpif *port)
1458 struct ofbundle *bundle = port->bundle;
1460 bundle->ofproto->need_revalidate = true;
1462 list_remove(&port->bundle_node);
1463 port->bundle = NULL;
1466 lacp_slave_unregister(bundle->lacp, port);
1469 bond_slave_unregister(bundle->bond, port);
1472 bundle_update(bundle);
1476 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1477 struct lacp_slave_settings *lacp,
1478 uint32_t bond_stable_id)
1480 struct ofport_dpif *port;
1482 port = get_ofp_port(bundle->ofproto, ofp_port);
1487 if (port->bundle != bundle) {
1488 bundle->ofproto->need_revalidate = true;
1490 bundle_del_port(port);
1493 port->bundle = bundle;
1494 list_push_back(&bundle->ports, &port->bundle_node);
1495 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1496 bundle->floodable = false;
1500 port->bundle->ofproto->need_revalidate = true;
1501 lacp_slave_register(bundle->lacp, port, lacp);
1504 port->bond_stable_id = bond_stable_id;
1510 bundle_destroy(struct ofbundle *bundle)
1512 struct ofproto_dpif *ofproto;
1513 struct ofport_dpif *port, *next_port;
1520 ofproto = bundle->ofproto;
1521 for (i = 0; i < MAX_MIRRORS; i++) {
1522 struct ofmirror *m = ofproto->mirrors[i];
1524 if (m->out == bundle) {
1526 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1527 || hmapx_find_and_delete(&m->dsts, bundle)) {
1528 ofproto->need_revalidate = true;
1533 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1534 bundle_del_port(port);
1537 bundle_flush_macs(bundle);
1538 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1540 free(bundle->trunks);
1541 lacp_destroy(bundle->lacp);
1542 bond_destroy(bundle->bond);
1547 bundle_set(struct ofproto *ofproto_, void *aux,
1548 const struct ofproto_bundle_settings *s)
1550 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1551 bool need_flush = false;
1552 struct ofport_dpif *port;
1553 struct ofbundle *bundle;
1554 unsigned long *trunks;
1560 bundle_destroy(bundle_lookup(ofproto, aux));
1564 assert(s->n_slaves == 1 || s->bond != NULL);
1565 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1567 bundle = bundle_lookup(ofproto, aux);
1569 bundle = xmalloc(sizeof *bundle);
1571 bundle->ofproto = ofproto;
1572 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1573 hash_pointer(aux, 0));
1575 bundle->name = NULL;
1577 list_init(&bundle->ports);
1578 bundle->vlan_mode = PORT_VLAN_TRUNK;
1580 bundle->trunks = NULL;
1581 bundle->use_priority_tags = s->use_priority_tags;
1582 bundle->lacp = NULL;
1583 bundle->bond = NULL;
1585 bundle->floodable = true;
1587 bundle->src_mirrors = 0;
1588 bundle->dst_mirrors = 0;
1589 bundle->mirror_out = 0;
1592 if (!bundle->name || strcmp(s->name, bundle->name)) {
1594 bundle->name = xstrdup(s->name);
1599 if (!bundle->lacp) {
1600 ofproto->need_revalidate = true;
1601 bundle->lacp = lacp_create();
1603 lacp_configure(bundle->lacp, s->lacp);
1605 lacp_destroy(bundle->lacp);
1606 bundle->lacp = NULL;
1609 /* Update set of ports. */
1611 for (i = 0; i < s->n_slaves; i++) {
1612 if (!bundle_add_port(bundle, s->slaves[i],
1613 s->lacp ? &s->lacp_slaves[i] : NULL,
1614 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1618 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1619 struct ofport_dpif *next_port;
1621 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1622 for (i = 0; i < s->n_slaves; i++) {
1623 if (s->slaves[i] == port->up.ofp_port) {
1628 bundle_del_port(port);
1632 assert(list_size(&bundle->ports) <= s->n_slaves);
1634 if (list_is_empty(&bundle->ports)) {
1635 bundle_destroy(bundle);
1639 /* Set VLAN tagging mode */
1640 if (s->vlan_mode != bundle->vlan_mode
1641 || s->use_priority_tags != bundle->use_priority_tags) {
1642 bundle->vlan_mode = s->vlan_mode;
1643 bundle->use_priority_tags = s->use_priority_tags;
1648 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1649 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1651 if (vlan != bundle->vlan) {
1652 bundle->vlan = vlan;
1656 /* Get trunked VLANs. */
1657 switch (s->vlan_mode) {
1658 case PORT_VLAN_ACCESS:
1662 case PORT_VLAN_TRUNK:
1663 trunks = (unsigned long *) s->trunks;
1666 case PORT_VLAN_NATIVE_UNTAGGED:
1667 case PORT_VLAN_NATIVE_TAGGED:
1668 if (vlan != 0 && (!s->trunks
1669 || !bitmap_is_set(s->trunks, vlan)
1670 || bitmap_is_set(s->trunks, 0))) {
1671 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1673 trunks = bitmap_clone(s->trunks, 4096);
1675 trunks = bitmap_allocate1(4096);
1677 bitmap_set1(trunks, vlan);
1678 bitmap_set0(trunks, 0);
1680 trunks = (unsigned long *) s->trunks;
1687 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1688 free(bundle->trunks);
1689 if (trunks == s->trunks) {
1690 bundle->trunks = vlan_bitmap_clone(trunks);
1692 bundle->trunks = trunks;
1697 if (trunks != s->trunks) {
1702 if (!list_is_short(&bundle->ports)) {
1703 bundle->ofproto->has_bonded_bundles = true;
1705 if (bond_reconfigure(bundle->bond, s->bond)) {
1706 ofproto->need_revalidate = true;
1709 bundle->bond = bond_create(s->bond);
1710 ofproto->need_revalidate = true;
1713 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1714 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1718 bond_destroy(bundle->bond);
1719 bundle->bond = NULL;
1722 /* If we changed something that would affect MAC learning, un-learn
1723 * everything on this port and force flow revalidation. */
1725 bundle_flush_macs(bundle);
1732 bundle_remove(struct ofport *port_)
1734 struct ofport_dpif *port = ofport_dpif_cast(port_);
1735 struct ofbundle *bundle = port->bundle;
1738 bundle_del_port(port);
1739 if (list_is_empty(&bundle->ports)) {
1740 bundle_destroy(bundle);
1741 } else if (list_is_short(&bundle->ports)) {
1742 bond_destroy(bundle->bond);
1743 bundle->bond = NULL;
1749 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1751 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1752 struct ofport_dpif *port = port_;
1753 uint8_t ea[ETH_ADDR_LEN];
1756 error = netdev_get_etheraddr(port->up.netdev, ea);
1758 struct ofpbuf packet;
1761 ofpbuf_init(&packet, 0);
1762 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1764 memcpy(packet_pdu, pdu, pdu_size);
1766 send_packet(port, &packet);
1767 ofpbuf_uninit(&packet);
1769 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1770 "%s (%s)", port->bundle->name,
1771 netdev_get_name(port->up.netdev), strerror(error));
1776 bundle_send_learning_packets(struct ofbundle *bundle)
1778 struct ofproto_dpif *ofproto = bundle->ofproto;
1779 int error, n_packets, n_errors;
1780 struct mac_entry *e;
1782 error = n_packets = n_errors = 0;
1783 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1784 if (e->port.p != bundle) {
1785 struct ofpbuf *learning_packet;
1786 struct ofport_dpif *port;
1789 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1792 ret = send_packet(port, learning_packet);
1793 ofpbuf_delete(learning_packet);
1803 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1804 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1805 "packets, last error was: %s",
1806 bundle->name, n_errors, n_packets, strerror(error));
1808 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1809 bundle->name, n_packets);
1814 bundle_run(struct ofbundle *bundle)
1817 lacp_run(bundle->lacp, send_pdu_cb);
1820 struct ofport_dpif *port;
1822 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1823 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1826 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1827 lacp_negotiated(bundle->lacp));
1828 if (bond_should_send_learning_packets(bundle->bond)) {
1829 bundle_send_learning_packets(bundle);
1835 bundle_wait(struct ofbundle *bundle)
1838 lacp_wait(bundle->lacp);
1841 bond_wait(bundle->bond);
1848 mirror_scan(struct ofproto_dpif *ofproto)
1852 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1853 if (!ofproto->mirrors[idx]) {
1860 static struct ofmirror *
1861 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1865 for (i = 0; i < MAX_MIRRORS; i++) {
1866 struct ofmirror *mirror = ofproto->mirrors[i];
1867 if (mirror && mirror->aux == aux) {
1875 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1877 mirror_update_dups(struct ofproto_dpif *ofproto)
1881 for (i = 0; i < MAX_MIRRORS; i++) {
1882 struct ofmirror *m = ofproto->mirrors[i];
1885 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1889 for (i = 0; i < MAX_MIRRORS; i++) {
1890 struct ofmirror *m1 = ofproto->mirrors[i];
1897 for (j = i + 1; j < MAX_MIRRORS; j++) {
1898 struct ofmirror *m2 = ofproto->mirrors[j];
1900 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1901 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1902 m2->dup_mirrors |= m1->dup_mirrors;
1909 mirror_set(struct ofproto *ofproto_, void *aux,
1910 const struct ofproto_mirror_settings *s)
1912 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1913 mirror_mask_t mirror_bit;
1914 struct ofbundle *bundle;
1915 struct ofmirror *mirror;
1916 struct ofbundle *out;
1917 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1918 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1921 mirror = mirror_lookup(ofproto, aux);
1923 mirror_destroy(mirror);
1929 idx = mirror_scan(ofproto);
1931 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1933 ofproto->up.name, MAX_MIRRORS, s->name);
1937 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1938 mirror->ofproto = ofproto;
1941 mirror->out_vlan = -1;
1942 mirror->name = NULL;
1945 if (!mirror->name || strcmp(s->name, mirror->name)) {
1947 mirror->name = xstrdup(s->name);
1950 /* Get the new configuration. */
1951 if (s->out_bundle) {
1952 out = bundle_lookup(ofproto, s->out_bundle);
1954 mirror_destroy(mirror);
1960 out_vlan = s->out_vlan;
1962 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1963 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1965 /* If the configuration has not changed, do nothing. */
1966 if (hmapx_equals(&srcs, &mirror->srcs)
1967 && hmapx_equals(&dsts, &mirror->dsts)
1968 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1969 && mirror->out == out
1970 && mirror->out_vlan == out_vlan)
1972 hmapx_destroy(&srcs);
1973 hmapx_destroy(&dsts);
1977 hmapx_swap(&srcs, &mirror->srcs);
1978 hmapx_destroy(&srcs);
1980 hmapx_swap(&dsts, &mirror->dsts);
1981 hmapx_destroy(&dsts);
1983 free(mirror->vlans);
1984 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1987 mirror->out_vlan = out_vlan;
1989 /* Update bundles. */
1990 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1991 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1992 if (hmapx_contains(&mirror->srcs, bundle)) {
1993 bundle->src_mirrors |= mirror_bit;
1995 bundle->src_mirrors &= ~mirror_bit;
1998 if (hmapx_contains(&mirror->dsts, bundle)) {
1999 bundle->dst_mirrors |= mirror_bit;
2001 bundle->dst_mirrors &= ~mirror_bit;
2004 if (mirror->out == bundle) {
2005 bundle->mirror_out |= mirror_bit;
2007 bundle->mirror_out &= ~mirror_bit;
2011 ofproto->need_revalidate = true;
2012 mac_learning_flush(ofproto->ml);
2013 mirror_update_dups(ofproto);
2019 mirror_destroy(struct ofmirror *mirror)
2021 struct ofproto_dpif *ofproto;
2022 mirror_mask_t mirror_bit;
2023 struct ofbundle *bundle;
2029 ofproto = mirror->ofproto;
2030 ofproto->need_revalidate = true;
2031 mac_learning_flush(ofproto->ml);
2033 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2034 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2035 bundle->src_mirrors &= ~mirror_bit;
2036 bundle->dst_mirrors &= ~mirror_bit;
2037 bundle->mirror_out &= ~mirror_bit;
2040 hmapx_destroy(&mirror->srcs);
2041 hmapx_destroy(&mirror->dsts);
2042 free(mirror->vlans);
2044 ofproto->mirrors[mirror->idx] = NULL;
2048 mirror_update_dups(ofproto);
2052 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2053 uint64_t *packets, uint64_t *bytes)
2055 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2056 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2059 *packets = *bytes = UINT64_MAX;
2063 *packets = mirror->packet_count;
2064 *bytes = mirror->byte_count;
2070 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2072 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2073 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2074 ofproto->need_revalidate = true;
2075 mac_learning_flush(ofproto->ml);
2081 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2083 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2084 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2085 return bundle && bundle->mirror_out != 0;
2089 forward_bpdu_changed(struct ofproto *ofproto_)
2091 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2092 /* Revalidate cached flows whenever forward_bpdu option changes. */
2093 ofproto->need_revalidate = true;
2098 static struct ofport_dpif *
2099 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
2101 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2102 return ofport ? ofport_dpif_cast(ofport) : NULL;
2105 static struct ofport_dpif *
2106 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
2108 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2112 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2113 struct dpif_port *dpif_port)
2115 ofproto_port->name = dpif_port->name;
2116 ofproto_port->type = dpif_port->type;
2117 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2121 port_run(struct ofport_dpif *ofport)
2123 bool enable = netdev_get_carrier(ofport->up.netdev);
2126 cfm_run(ofport->cfm);
2128 if (cfm_should_send_ccm(ofport->cfm)) {
2129 struct ofpbuf packet;
2131 ofpbuf_init(&packet, 0);
2132 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
2133 send_packet(ofport, &packet);
2134 ofpbuf_uninit(&packet);
2137 enable = enable && !cfm_get_fault(ofport->cfm)
2138 && cfm_get_opup(ofport->cfm);
2141 if (ofport->bundle) {
2142 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2145 if (ofport->may_enable != enable) {
2146 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2148 if (ofproto->has_bundle_action) {
2149 ofproto->need_revalidate = true;
2153 ofport->may_enable = enable;
2157 port_wait(struct ofport_dpif *ofport)
2160 cfm_wait(ofport->cfm);
2165 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2166 struct ofproto_port *ofproto_port)
2168 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2169 struct dpif_port dpif_port;
2172 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2174 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2180 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2182 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2186 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2188 *ofp_portp = odp_port_to_ofp_port(odp_port);
2194 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2196 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2199 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2201 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2203 /* The caller is going to close ofport->up.netdev. If this is a
2204 * bonded port, then the bond is using that netdev, so remove it
2205 * from the bond. The client will need to reconfigure everything
2206 * after deleting ports, so then the slave will get re-added. */
2207 bundle_remove(&ofport->up);
2213 struct port_dump_state {
2214 struct dpif_port_dump dump;
2219 port_dump_start(const struct ofproto *ofproto_, void **statep)
2221 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2222 struct port_dump_state *state;
2224 *statep = state = xmalloc(sizeof *state);
2225 dpif_port_dump_start(&state->dump, ofproto->dpif);
2226 state->done = false;
2231 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2232 struct ofproto_port *port)
2234 struct port_dump_state *state = state_;
2235 struct dpif_port dpif_port;
2237 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2238 ofproto_port_from_dpif_port(port, &dpif_port);
2241 int error = dpif_port_dump_done(&state->dump);
2243 return error ? error : EOF;
2248 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2250 struct port_dump_state *state = state_;
2253 dpif_port_dump_done(&state->dump);
2260 port_poll(const struct ofproto *ofproto_, char **devnamep)
2262 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2263 return dpif_port_poll(ofproto->dpif, devnamep);
2267 port_poll_wait(const struct ofproto *ofproto_)
2269 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2270 dpif_port_poll_wait(ofproto->dpif);
2274 port_is_lacp_current(const struct ofport *ofport_)
2276 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2277 return (ofport->bundle && ofport->bundle->lacp
2278 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2282 /* Upcall handling. */
2284 /* Flow miss batching.
2286 * Some dpifs implement operations faster when you hand them off in a batch.
2287 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2288 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2289 * more packets, plus possibly installing the flow in the dpif.
2291 * So far we only batch the operations that affect flow setup time the most.
2292 * It's possible to batch more than that, but the benefit might be minimal. */
2294 struct hmap_node hmap_node;
2296 enum odp_key_fitness key_fitness;
2297 const struct nlattr *key;
2299 ovs_be16 initial_tci;
2300 struct list packets;
2303 struct flow_miss_op {
2304 union dpif_op dpif_op;
2305 struct subfacet *subfacet;
2308 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2309 * OpenFlow controller as necessary according to their individual
2312 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2313 * ownership is transferred to this function. */
2315 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2316 const struct flow *flow, bool clone)
2318 struct ofputil_packet_in pin;
2320 pin.packet = packet;
2321 pin.in_port = flow->in_port;
2322 pin.reason = OFPR_NO_MATCH;
2323 pin.buffer_id = 0; /* not yet known */
2324 pin.send_len = 0; /* not used for flow table misses */
2325 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2326 clone ? NULL : packet);
2329 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2330 * OpenFlow controller as necessary according to their individual
2333 * 'send_len' should be the number of bytes of 'packet' to send to the
2334 * controller, as specified in the action that caused the packet to be sent.
2336 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2337 * Otherwise, ownership is transferred to this function. */
2339 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2340 uint64_t userdata, const struct flow *flow, bool clone)
2342 struct ofputil_packet_in pin;
2343 struct user_action_cookie cookie;
2345 memcpy(&cookie, &userdata, sizeof(cookie));
2347 pin.packet = packet;
2348 pin.in_port = flow->in_port;
2349 pin.reason = OFPR_ACTION;
2350 pin.buffer_id = 0; /* not yet known */
2351 pin.send_len = cookie.data;
2352 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2353 clone ? NULL : packet);
2357 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2358 const struct ofpbuf *packet)
2360 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2366 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2368 cfm_process_heartbeat(ofport->cfm, packet);
2371 } else if (ofport->bundle && ofport->bundle->lacp
2372 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2374 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2377 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2379 stp_process_packet(ofport, packet);
2386 static struct flow_miss *
2387 flow_miss_create(struct hmap *todo, const struct flow *flow,
2388 enum odp_key_fitness key_fitness,
2389 const struct nlattr *key, size_t key_len,
2390 ovs_be16 initial_tci)
2392 uint32_t hash = flow_hash(flow, 0);
2393 struct flow_miss *miss;
2395 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2396 if (flow_equal(&miss->flow, flow)) {
2401 miss = xmalloc(sizeof *miss);
2402 hmap_insert(todo, &miss->hmap_node, hash);
2404 miss->key_fitness = key_fitness;
2406 miss->key_len = key_len;
2407 miss->initial_tci = initial_tci;
2408 list_init(&miss->packets);
2413 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2414 struct flow_miss_op *ops, size_t *n_ops)
2416 const struct flow *flow = &miss->flow;
2417 struct ofpbuf *packet, *next_packet;
2418 struct subfacet *subfacet;
2419 struct facet *facet;
2421 facet = facet_lookup_valid(ofproto, flow);
2423 struct rule_dpif *rule;
2425 rule = rule_dpif_lookup(ofproto, flow, 0);
2427 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2428 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2430 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2431 COVERAGE_INC(ofproto_dpif_no_packet_in);
2432 /* XXX install 'drop' flow entry */
2436 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2440 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2442 list_remove(&packet->list_node);
2443 send_packet_in_miss(ofproto, packet, flow, false);
2449 facet = facet_create(rule, flow);
2452 subfacet = subfacet_create(ofproto, facet,
2453 miss->key_fitness, miss->key, miss->key_len,
2456 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2457 list_remove(&packet->list_node);
2458 ofproto->n_matches++;
2460 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2462 * Extra-special case for fail-open mode.
2464 * We are in fail-open mode and the packet matched the fail-open
2465 * rule, but we are connected to a controller too. We should send
2466 * the packet up to the controller in the hope that it will try to
2467 * set up a flow and thereby allow us to exit fail-open.
2469 * See the top-level comment in fail-open.c for more information.
2471 send_packet_in_miss(ofproto, packet, flow, true);
2474 if (!facet->may_install || !subfacet->actions) {
2475 subfacet_make_actions(ofproto, subfacet, packet);
2477 if (!execute_controller_action(ofproto, &facet->flow,
2479 subfacet->actions_len, packet)) {
2480 struct flow_miss_op *op = &ops[(*n_ops)++];
2481 struct dpif_execute *execute = &op->dpif_op.execute;
2483 op->subfacet = subfacet;
2484 execute->type = DPIF_OP_EXECUTE;
2485 execute->key = miss->key;
2486 execute->key_len = miss->key_len;
2488 = (facet->may_install
2490 : xmemdup(subfacet->actions, subfacet->actions_len));
2491 execute->actions_len = subfacet->actions_len;
2492 execute->packet = packet;
2496 if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
2497 struct flow_miss_op *op = &ops[(*n_ops)++];
2498 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2500 op->subfacet = subfacet;
2501 put->type = DPIF_OP_FLOW_PUT;
2502 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2503 put->key = miss->key;
2504 put->key_len = miss->key_len;
2505 put->actions = subfacet->actions;
2506 put->actions_len = subfacet->actions_len;
2511 static enum odp_key_fitness
2512 ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
2513 const struct nlattr *key, size_t key_len,
2514 struct flow *flow, ovs_be16 *initial_tci)
2516 enum odp_key_fitness fitness;
2520 fitness = odp_flow_key_to_flow(key, key_len, flow);
2521 if (fitness == ODP_FIT_ERROR) {
2524 *initial_tci = flow->vlan_tci;
2526 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
2528 /* Cause the flow to be processed as if it came in on the real device
2529 * with the VLAN device's VLAN ID. */
2530 flow->in_port = realdev;
2531 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
2533 /* Let the caller know that we can't reproduce 'key' from 'flow'. */
2534 if (fitness == ODP_FIT_PERFECT) {
2535 fitness = ODP_FIT_TOO_MUCH;
2543 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2546 struct dpif_upcall *upcall;
2547 struct flow_miss *miss, *next_miss;
2548 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2549 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2558 /* Construct the to-do list.
2560 * This just amounts to extracting the flow from each packet and sticking
2561 * the packets that have the same flow in the same "flow_miss" structure so
2562 * that we can process them together. */
2564 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2565 enum odp_key_fitness fitness;
2566 struct flow_miss *miss;
2567 ovs_be16 initial_tci;
2570 /* Obtain metadata and check userspace/kernel agreement on flow match,
2571 * then set 'flow''s header pointers. */
2572 fitness = ofproto_dpif_extract_flow_key(ofproto,
2573 upcall->key, upcall->key_len,
2574 &flow, &initial_tci);
2575 if (fitness == ODP_FIT_ERROR) {
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) {
2655 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2656 if (ofproto->sflow) {
2657 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
2660 ofpbuf_delete(upcall->packet);
2661 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2662 COVERAGE_INC(ofproto_dpif_ctlr_action);
2663 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2666 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2671 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
2673 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
2677 assert (max_batch <= FLOW_MISS_MAX_BATCH);
2680 for (i = 0; i < max_batch; i++) {
2681 struct dpif_upcall *upcall = &misses[n_misses];
2684 error = dpif_recv(ofproto->dpif, upcall);
2689 switch (upcall->type) {
2690 case DPIF_UC_ACTION:
2691 handle_userspace_upcall(ofproto, upcall);
2695 /* Handle it later. */
2699 case DPIF_N_UC_TYPES:
2701 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
2707 handle_miss_upcalls(ofproto, misses, n_misses);
2712 /* Flow expiration. */
2714 static int subfacet_max_idle(const struct ofproto_dpif *);
2715 static void update_stats(struct ofproto_dpif *);
2716 static void rule_expire(struct rule_dpif *);
2717 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
2719 /* This function is called periodically by run(). Its job is to collect
2720 * updates for the flows that have been installed into the datapath, most
2721 * importantly when they last were used, and then use that information to
2722 * expire flows that have not been used recently.
2724 * Returns the number of milliseconds after which it should be called again. */
2726 expire(struct ofproto_dpif *ofproto)
2728 struct rule_dpif *rule, *next_rule;
2729 struct classifier *table;
2732 /* Update stats for each flow in the datapath. */
2733 update_stats(ofproto);
2735 /* Expire subfacets that have been idle too long. */
2736 dp_max_idle = subfacet_max_idle(ofproto);
2737 expire_subfacets(ofproto, dp_max_idle);
2739 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2740 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2741 struct cls_cursor cursor;
2743 cls_cursor_init(&cursor, table, NULL);
2744 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2749 /* All outstanding data in existing flows has been accounted, so it's a
2750 * good time to do bond rebalancing. */
2751 if (ofproto->has_bonded_bundles) {
2752 struct ofbundle *bundle;
2754 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2756 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2761 return MIN(dp_max_idle, 1000);
2764 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2766 * This function also pushes statistics updates to rules which each facet
2767 * resubmits into. Generally these statistics will be accurate. However, if a
2768 * facet changes the rule it resubmits into at some time in between
2769 * update_stats() runs, it is possible that statistics accrued to the
2770 * old rule will be incorrectly attributed to the new rule. This could be
2771 * avoided by calling update_stats() whenever rules are created or
2772 * deleted. However, the performance impact of making so many calls to the
2773 * datapath do not justify the benefit of having perfectly accurate statistics.
2776 update_stats(struct ofproto_dpif *p)
2778 const struct dpif_flow_stats *stats;
2779 struct dpif_flow_dump dump;
2780 const struct nlattr *key;
2783 dpif_flow_dump_start(&dump, p->dpif);
2784 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2785 enum odp_key_fitness fitness;
2786 struct subfacet *subfacet;
2789 fitness = odp_flow_key_to_flow(key, key_len, &flow);
2790 if (fitness == ODP_FIT_ERROR) {
2794 subfacet = subfacet_find(p, key, key_len, &flow);
2795 if (subfacet && subfacet->installed) {
2796 struct facet *facet = subfacet->facet;
2798 if (stats->n_packets >= subfacet->dp_packet_count) {
2799 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
2800 facet->packet_count += extra;
2802 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2805 if (stats->n_bytes >= subfacet->dp_byte_count) {
2806 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
2808 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2811 subfacet->dp_packet_count = stats->n_packets;
2812 subfacet->dp_byte_count = stats->n_bytes;
2814 subfacet_update_time(p, subfacet, stats->used);
2815 facet_account(p, facet);
2816 facet_push_stats(facet);
2818 /* There's a flow in the datapath that we know nothing about, or a
2819 * flow that shouldn't be installed but was anyway. Delete it. */
2820 COVERAGE_INC(facet_unexpected);
2821 dpif_flow_del(p->dpif, key, key_len, NULL);
2824 dpif_flow_dump_done(&dump);
2827 /* Calculates and returns the number of milliseconds of idle time after which
2828 * subfacets should expire from the datapath. When a subfacet expires, we fold
2829 * its statistics into its facet, and when a facet's last subfacet expires, we
2830 * fold its statistic into its rule. */
2832 subfacet_max_idle(const struct ofproto_dpif *ofproto)
2835 * Idle time histogram.
2837 * Most of the time a switch has a relatively small number of subfacets.
2838 * When this is the case we might as well keep statistics for all of them
2839 * in userspace and to cache them in the kernel datapath for performance as
2842 * As the number of subfacets increases, the memory required to maintain
2843 * statistics about them in userspace and in the kernel becomes
2844 * significant. However, with a large number of subfacets it is likely
2845 * that only a few of them are "heavy hitters" that consume a large amount
2846 * of bandwidth. At this point, only heavy hitters are worth caching in
2847 * the kernel and maintaining in userspaces; other subfacets we can
2850 * The technique used to compute the idle time is to build a histogram with
2851 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
2852 * that is installed in the kernel gets dropped in the appropriate bucket.
2853 * After the histogram has been built, we compute the cutoff so that only
2854 * the most-recently-used 1% of subfacets (but at least
2855 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
2856 * the most-recently-used bucket of subfacets is kept, so actually an
2857 * arbitrary number of subfacets can be kept in any given expiration run
2858 * (though the next run will delete most of those unless they receive
2861 * This requires a second pass through the subfacets, in addition to the
2862 * pass made by update_stats(), because the former function never looks at
2863 * uninstallable subfacets.
2865 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
2866 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
2867 int buckets[N_BUCKETS] = { 0 };
2868 int total, subtotal, bucket;
2869 struct subfacet *subfacet;
2873 total = hmap_count(&ofproto->subfacets);
2874 if (total <= ofproto->up.flow_eviction_threshold) {
2875 return N_BUCKETS * BUCKET_WIDTH;
2878 /* Build histogram. */
2880 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
2881 long long int idle = now - subfacet->used;
2882 int bucket = (idle <= 0 ? 0
2883 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
2884 : (unsigned int) idle / BUCKET_WIDTH);
2888 /* Find the first bucket whose flows should be expired. */
2889 subtotal = bucket = 0;
2891 subtotal += buckets[bucket++];
2892 } while (bucket < N_BUCKETS &&
2893 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
2895 if (VLOG_IS_DBG_ENABLED()) {
2899 ds_put_cstr(&s, "keep");
2900 for (i = 0; i < N_BUCKETS; i++) {
2902 ds_put_cstr(&s, ", drop");
2905 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
2908 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
2912 return bucket * BUCKET_WIDTH;
2916 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
2918 long long int cutoff = time_msec() - dp_max_idle;
2919 struct subfacet *subfacet, *next_subfacet;
2921 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
2922 &ofproto->subfacets) {
2923 if (subfacet->used < cutoff) {
2924 subfacet_destroy(ofproto, subfacet);
2929 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2930 * then delete it entirely. */
2932 rule_expire(struct rule_dpif *rule)
2934 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2935 struct facet *facet, *next_facet;
2939 /* Has 'rule' expired? */
2941 if (rule->up.hard_timeout
2942 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
2943 reason = OFPRR_HARD_TIMEOUT;
2944 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
2945 && now > rule->used + rule->up.idle_timeout * 1000) {
2946 reason = OFPRR_IDLE_TIMEOUT;
2951 COVERAGE_INC(ofproto_dpif_expired);
2953 /* Update stats. (This is a no-op if the rule expired due to an idle
2954 * timeout, because that only happens when the rule has no facets left.) */
2955 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2956 facet_remove(ofproto, facet);
2959 /* Get rid of the rule. */
2960 ofproto_rule_expire(&rule->up, reason);
2965 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
2967 * The caller must already have determined that no facet with an identical
2968 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2969 * the ofproto's classifier table.
2971 * The facet will initially have no subfacets. The caller should create (at
2972 * least) one subfacet with subfacet_create(). */
2973 static struct facet *
2974 facet_create(struct rule_dpif *rule, const struct flow *flow)
2976 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2977 struct facet *facet;
2979 facet = xzalloc(sizeof *facet);
2980 facet->used = time_msec();
2981 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2982 list_push_back(&rule->facets, &facet->list_node);
2984 facet->flow = *flow;
2985 list_init(&facet->subfacets);
2986 netflow_flow_init(&facet->nf_flow);
2987 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2993 facet_free(struct facet *facet)
2999 execute_controller_action(struct ofproto_dpif *ofproto,
3000 const struct flow *flow,
3001 const struct nlattr *odp_actions, size_t actions_len,
3002 struct ofpbuf *packet)
3005 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
3006 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
3007 /* As an optimization, avoid a round-trip from userspace to kernel to
3008 * userspace. This also avoids possibly filling up kernel packet
3009 * buffers along the way.
3011 * This optimization will not accidentally catch sFlow
3012 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
3013 * inside OVS_ACTION_ATTR_SAMPLE. */
3014 const struct nlattr *nla;
3016 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
3017 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
3025 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3026 * 'packet', which arrived on 'in_port'.
3028 * Takes ownership of 'packet'. */
3030 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3031 const struct nlattr *odp_actions, size_t actions_len,
3032 struct ofpbuf *packet)
3034 struct odputil_keybuf keybuf;
3038 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
3043 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3044 odp_flow_key_from_flow(&key, flow);
3046 error = dpif_execute(ofproto->dpif, key.data, key.size,
3047 odp_actions, actions_len, packet);
3049 ofpbuf_delete(packet);
3053 /* Remove 'facet' from 'ofproto' and free up the associated memory:
3055 * - If 'facet' was installed in the datapath, uninstalls it and updates its
3056 * rule's statistics, via subfacet_uninstall().
3058 * - Removes 'facet' from its rule and from ofproto->facets.
3061 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
3063 struct subfacet *subfacet, *next_subfacet;
3065 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3066 &facet->subfacets) {
3067 subfacet_destroy__(ofproto, subfacet);
3070 facet_flush_stats(ofproto, facet);
3071 hmap_remove(&ofproto->facets, &facet->hmap_node);
3072 list_remove(&facet->list_node);
3077 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
3080 struct subfacet *subfacet;
3081 const struct nlattr *a;
3085 if (facet->byte_count <= facet->accounted_bytes) {
3088 n_bytes = facet->byte_count - facet->accounted_bytes;
3089 facet->accounted_bytes = facet->byte_count;
3091 /* Feed information from the active flows back into the learning table to
3092 * ensure that table is always in sync with what is actually flowing
3093 * through the datapath. */
3094 if (facet->has_learn || facet->has_normal) {
3095 struct action_xlate_ctx ctx;
3097 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3098 facet->flow.vlan_tci, NULL);
3099 ctx.may_learn = true;
3100 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
3101 facet->rule->up.n_actions));
3104 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
3108 /* This loop feeds byte counters to bond_account() for rebalancing to use
3109 * as a basis. We also need to track the actual VLAN on which the packet
3110 * is going to be sent to ensure that it matches the one passed to
3111 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3114 * We use the actions from an arbitrary subfacet because they should all
3115 * be equally valid for our purpose. */
3116 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3117 struct subfacet, list_node);
3118 vlan_tci = facet->flow.vlan_tci;
3119 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3120 subfacet->actions, subfacet->actions_len) {
3121 const struct ovs_action_push_vlan *vlan;
3122 struct ofport_dpif *port;
3124 switch (nl_attr_type(a)) {
3125 case OVS_ACTION_ATTR_OUTPUT:
3126 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3127 if (port && port->bundle && port->bundle->bond) {
3128 bond_account(port->bundle->bond, &facet->flow,
3129 vlan_tci_to_vid(vlan_tci), n_bytes);
3133 case OVS_ACTION_ATTR_POP_VLAN:
3134 vlan_tci = htons(0);
3137 case OVS_ACTION_ATTR_PUSH_VLAN:
3138 vlan = nl_attr_get(a);
3139 vlan_tci = vlan->vlan_tci;
3145 /* Returns true if the only action for 'facet' is to send to the controller.
3146 * (We don't report NetFlow expiration messages for such facets because they
3147 * are just part of the control logic for the network, not real traffic). */
3149 facet_is_controller_flow(struct facet *facet)
3152 && facet->rule->up.n_actions == 1
3153 && action_outputs_to_port(&facet->rule->up.actions[0],
3154 htons(OFPP_CONTROLLER)));
3157 /* Folds all of 'facet''s statistics into its rule. Also updates the
3158 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3159 * 'facet''s statistics in the datapath should have been zeroed and folded into
3160 * its packet and byte counts before this function is called. */
3162 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3164 struct subfacet *subfacet;
3166 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3167 assert(!subfacet->dp_byte_count);
3168 assert(!subfacet->dp_packet_count);
3171 facet_push_stats(facet);
3172 facet_account(ofproto, facet);
3174 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3175 struct ofexpired expired;
3176 expired.flow = facet->flow;
3177 expired.packet_count = facet->packet_count;
3178 expired.byte_count = facet->byte_count;
3179 expired.used = facet->used;
3180 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3183 facet->rule->packet_count += facet->packet_count;
3184 facet->rule->byte_count += facet->byte_count;
3186 /* Reset counters to prevent double counting if 'facet' ever gets
3188 facet_reset_counters(facet);
3190 netflow_flow_clear(&facet->nf_flow);
3193 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3194 * Returns it if found, otherwise a null pointer.
3196 * The returned facet might need revalidation; use facet_lookup_valid()
3197 * instead if that is important. */
3198 static struct facet *
3199 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3201 struct facet *facet;
3203 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3205 if (flow_equal(flow, &facet->flow)) {
3213 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3214 * Returns it if found, otherwise a null pointer.
3216 * The returned facet is guaranteed to be valid. */
3217 static struct facet *
3218 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3220 struct facet *facet = facet_find(ofproto, flow);
3222 /* The facet we found might not be valid, since we could be in need of
3223 * revalidation. If it is not valid, don't return it. */
3225 && (ofproto->need_revalidate
3226 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3227 && !facet_revalidate(ofproto, facet)) {
3228 COVERAGE_INC(facet_invalidated);
3235 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3237 * - If the rule found is different from 'facet''s current rule, moves
3238 * 'facet' to the new rule and recompiles its actions.
3240 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3241 * where it is and recompiles its actions anyway.
3243 * - If there is none, destroys 'facet'.
3245 * Returns true if 'facet' still exists, false if it has been destroyed. */
3247 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3250 struct nlattr *odp_actions;
3253 struct actions *new_actions;
3255 struct action_xlate_ctx ctx;
3256 struct rule_dpif *new_rule;
3257 struct subfacet *subfacet;
3258 bool actions_changed;
3261 COVERAGE_INC(facet_revalidate);
3263 /* Determine the new rule. */
3264 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3266 /* No new rule, so delete the facet. */
3267 facet_remove(ofproto, facet);
3271 /* Calculate new datapath actions.
3273 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3274 * emit a NetFlow expiration and, if so, we need to have the old state
3275 * around to properly compose it. */
3277 /* If the datapath actions changed or the installability changed,
3278 * then we need to talk to the datapath. */
3281 memset(&ctx, 0, sizeof ctx);
3282 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3283 struct ofpbuf *odp_actions;
3284 bool should_install;
3286 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3287 subfacet->initial_tci, NULL);
3288 odp_actions = xlate_actions(&ctx, new_rule->up.actions,
3289 new_rule->up.n_actions);
3290 actions_changed = (subfacet->actions_len != odp_actions->size
3291 || memcmp(subfacet->actions, odp_actions->data,
3292 subfacet->actions_len));
3294 should_install = (ctx.may_set_up_flow
3295 && subfacet->key_fitness != ODP_FIT_TOO_LITTLE);
3296 if (actions_changed || should_install != subfacet->installed) {
3297 if (should_install) {
3298 struct dpif_flow_stats stats;
3300 subfacet_install(ofproto, subfacet,
3301 odp_actions->data, odp_actions->size, &stats);
3302 subfacet_update_stats(ofproto, subfacet, &stats);
3304 subfacet_uninstall(ofproto, subfacet);
3308 new_actions = xcalloc(list_size(&facet->subfacets),
3309 sizeof *new_actions);
3311 new_actions[i].odp_actions = xmemdup(odp_actions->data,
3313 new_actions[i].actions_len = odp_actions->size;
3316 ofpbuf_delete(odp_actions);
3320 facet_flush_stats(ofproto, facet);
3323 /* Update 'facet' now that we've taken care of all the old state. */
3324 facet->tags = ctx.tags;
3325 facet->nf_flow.output_iface = ctx.nf_output_iface;
3326 facet->may_install = ctx.may_set_up_flow;
3327 facet->has_learn = ctx.has_learn;
3328 facet->has_normal = ctx.has_normal;
3329 facet->mirrors = ctx.mirrors;
3332 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3333 if (new_actions[i].odp_actions) {
3334 free(subfacet->actions);
3335 subfacet->actions = new_actions[i].odp_actions;
3336 subfacet->actions_len = new_actions[i].actions_len;
3342 if (facet->rule != new_rule) {
3343 COVERAGE_INC(facet_changed_rule);
3344 list_remove(&facet->list_node);
3345 list_push_back(&new_rule->facets, &facet->list_node);
3346 facet->rule = new_rule;
3347 facet->used = new_rule->up.created;
3348 facet->prev_used = facet->used;
3354 /* Updates 'facet''s used time. Caller is responsible for calling
3355 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3357 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3360 if (used > facet->used) {
3362 if (used > facet->rule->used) {
3363 facet->rule->used = used;
3365 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3370 facet_reset_counters(struct facet *facet)
3372 facet->packet_count = 0;
3373 facet->byte_count = 0;
3374 facet->prev_packet_count = 0;
3375 facet->prev_byte_count = 0;
3376 facet->accounted_bytes = 0;
3380 facet_push_stats(struct facet *facet)
3382 uint64_t new_packets, new_bytes;
3384 assert(facet->packet_count >= facet->prev_packet_count);
3385 assert(facet->byte_count >= facet->prev_byte_count);
3386 assert(facet->used >= facet->prev_used);
3388 new_packets = facet->packet_count - facet->prev_packet_count;
3389 new_bytes = facet->byte_count - facet->prev_byte_count;
3391 if (new_packets || new_bytes || facet->used > facet->prev_used) {
3392 facet->prev_packet_count = facet->packet_count;
3393 facet->prev_byte_count = facet->byte_count;
3394 facet->prev_used = facet->used;
3396 flow_push_stats(facet->rule, &facet->flow,
3397 new_packets, new_bytes, facet->used);
3399 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
3400 facet->mirrors, new_packets, new_bytes);
3404 struct ofproto_push {
3405 struct action_xlate_ctx ctx;
3412 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3414 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3417 rule->packet_count += push->packets;
3418 rule->byte_count += push->bytes;
3419 rule->used = MAX(push->used, rule->used);
3423 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3424 * 'rule''s actions and mirrors. */
3426 flow_push_stats(const struct rule_dpif *rule,
3427 const struct flow *flow, uint64_t packets, uint64_t bytes,
3430 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3431 struct ofproto_push push;
3433 push.packets = packets;
3437 action_xlate_ctx_init(&push.ctx, ofproto, flow, flow->vlan_tci, NULL);
3438 push.ctx.resubmit_hook = push_resubmit;
3439 ofpbuf_delete(xlate_actions(&push.ctx,
3440 rule->up.actions, rule->up.n_actions));
3445 static struct subfacet *
3446 subfacet_find__(struct ofproto_dpif *ofproto,
3447 const struct nlattr *key, size_t key_len, uint32_t key_hash,
3448 const struct flow *flow)
3450 struct subfacet *subfacet;
3452 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
3453 &ofproto->subfacets) {
3455 ? (subfacet->key_len == key_len
3456 && !memcmp(key, subfacet->key, key_len))
3457 : flow_equal(flow, &subfacet->facet->flow)) {
3465 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
3466 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
3467 * there is one, otherwise creates and returns a new subfacet.
3469 * If the returned subfacet is new, then subfacet->actions will be NULL, in
3470 * which case the caller must populate the actions with
3471 * subfacet_make_actions(). */
3472 static struct subfacet *
3473 subfacet_create(struct ofproto_dpif *ofproto, struct facet *facet,
3474 enum odp_key_fitness key_fitness,
3475 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
3477 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3478 struct subfacet *subfacet;
3480 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
3482 if (subfacet->facet == facet) {
3486 /* This shouldn't happen. */
3487 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3488 subfacet_destroy(ofproto, subfacet);
3491 subfacet = xzalloc(sizeof *subfacet);
3492 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
3493 list_push_back(&facet->subfacets, &subfacet->list_node);
3494 subfacet->facet = facet;
3495 subfacet->used = time_msec();
3496 subfacet->key_fitness = key_fitness;
3497 if (key_fitness != ODP_FIT_PERFECT) {
3498 subfacet->key = xmemdup(key, key_len);
3499 subfacet->key_len = key_len;
3501 subfacet->installed = false;
3502 subfacet->initial_tci = initial_tci;
3507 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
3508 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
3509 static struct subfacet *
3510 subfacet_find(struct ofproto_dpif *ofproto,
3511 const struct nlattr *key, size_t key_len,
3512 const struct flow *flow)
3514 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3516 return subfacet_find__(ofproto, key, key_len, key_hash, flow);
3519 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
3520 * its facet within 'ofproto', and frees it. */
3522 subfacet_destroy__(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3524 subfacet_uninstall(ofproto, subfacet);
3525 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
3526 list_remove(&subfacet->list_node);
3527 free(subfacet->key);
3528 free(subfacet->actions);
3532 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
3533 * last remaining subfacet in its facet destroys the facet too. */
3535 subfacet_destroy(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3537 struct facet *facet = subfacet->facet;
3539 subfacet_destroy__(ofproto, subfacet);
3540 if (list_is_empty(&facet->subfacets)) {
3541 facet_remove(ofproto, facet);
3545 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
3546 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
3547 * for use as temporary storage. */
3549 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
3552 if (!subfacet->key) {
3553 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
3554 odp_flow_key_from_flow(key, &subfacet->facet->flow);
3556 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
3560 /* Composes the datapath actions for 'subfacet' based on its rule's actions. */
3562 subfacet_make_actions(struct ofproto_dpif *p, struct subfacet *subfacet,
3563 const struct ofpbuf *packet)
3565 struct facet *facet = subfacet->facet;
3566 const struct rule_dpif *rule = facet->rule;
3567 struct ofpbuf *odp_actions;
3568 struct action_xlate_ctx ctx;
3570 action_xlate_ctx_init(&ctx, p, &facet->flow, subfacet->initial_tci,
3572 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3573 facet->tags = ctx.tags;
3574 facet->may_install = ctx.may_set_up_flow;
3575 facet->has_learn = ctx.has_learn;
3576 facet->has_normal = ctx.has_normal;
3577 facet->nf_flow.output_iface = ctx.nf_output_iface;
3578 facet->mirrors = ctx.mirrors;
3580 if (subfacet->actions_len != odp_actions->size
3581 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
3582 free(subfacet->actions);
3583 subfacet->actions_len = odp_actions->size;
3584 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
3587 ofpbuf_delete(odp_actions);
3590 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
3591 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
3592 * in the datapath will be zeroed and 'stats' will be updated with traffic new
3593 * since 'subfacet' was last updated.
3595 * Returns 0 if successful, otherwise a positive errno value. */
3597 subfacet_install(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3598 const struct nlattr *actions, size_t actions_len,
3599 struct dpif_flow_stats *stats)
3601 struct odputil_keybuf keybuf;
3602 enum dpif_flow_put_flags flags;
3606 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3608 flags |= DPIF_FP_ZERO_STATS;
3611 subfacet_get_key(subfacet, &keybuf, &key);
3612 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
3613 actions, actions_len, stats);
3616 subfacet_reset_dp_stats(subfacet, stats);
3622 /* If 'subfacet' is installed in the datapath, uninstalls it. */
3624 subfacet_uninstall(struct ofproto_dpif *p, struct subfacet *subfacet)
3626 if (subfacet->installed) {
3627 struct odputil_keybuf keybuf;
3628 struct dpif_flow_stats stats;
3632 subfacet_get_key(subfacet, &keybuf, &key);
3633 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3634 subfacet_reset_dp_stats(subfacet, &stats);
3636 subfacet_update_stats(p, subfacet, &stats);
3638 subfacet->installed = false;
3640 assert(subfacet->dp_packet_count == 0);
3641 assert(subfacet->dp_byte_count == 0);
3645 /* Resets 'subfacet''s datapath statistics counters. This should be called
3646 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
3647 * non-null, it should contain the statistics returned by dpif when 'subfacet'
3648 * was reset in the datapath. 'stats' will be modified to include only
3649 * statistics new since 'subfacet' was last updated. */
3651 subfacet_reset_dp_stats(struct subfacet *subfacet,
3652 struct dpif_flow_stats *stats)
3655 && subfacet->dp_packet_count <= stats->n_packets
3656 && subfacet->dp_byte_count <= stats->n_bytes) {
3657 stats->n_packets -= subfacet->dp_packet_count;
3658 stats->n_bytes -= subfacet->dp_byte_count;
3661 subfacet->dp_packet_count = 0;
3662 subfacet->dp_byte_count = 0;
3665 /* Updates 'subfacet''s used time. The caller is responsible for calling
3666 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
3668 subfacet_update_time(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3671 if (used > subfacet->used) {
3672 subfacet->used = used;
3673 facet_update_time(ofproto, subfacet->facet, used);
3677 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
3679 * Because of the meaning of a subfacet's counters, it only makes sense to do
3680 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
3681 * represents a packet that was sent by hand or if it represents statistics
3682 * that have been cleared out of the datapath. */
3684 subfacet_update_stats(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3685 const struct dpif_flow_stats *stats)
3687 if (stats->n_packets || stats->used > subfacet->used) {
3688 struct facet *facet = subfacet->facet;
3690 subfacet_update_time(ofproto, subfacet, stats->used);
3691 facet->packet_count += stats->n_packets;
3692 facet->byte_count += stats->n_bytes;
3693 facet_push_stats(facet);
3694 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3700 static struct rule_dpif *
3701 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3704 struct cls_rule *cls_rule;
3705 struct classifier *cls;
3707 if (table_id >= N_TABLES) {
3711 cls = &ofproto->up.tables[table_id];
3712 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3713 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3714 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3715 * are unavailable. */
3716 struct flow ofpc_normal_flow = *flow;
3717 ofpc_normal_flow.tp_src = htons(0);
3718 ofpc_normal_flow.tp_dst = htons(0);
3719 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3721 cls_rule = classifier_lookup(cls, flow);
3723 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3727 complete_operation(struct rule_dpif *rule)
3729 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3731 rule_invalidate(rule);
3733 struct dpif_completion *c = xmalloc(sizeof *c);
3734 c->op = rule->up.pending;
3735 list_push_back(&ofproto->completions, &c->list_node);
3737 ofoperation_complete(rule->up.pending, 0);
3741 static struct rule *
3744 struct rule_dpif *rule = xmalloc(sizeof *rule);
3749 rule_dealloc(struct rule *rule_)
3751 struct rule_dpif *rule = rule_dpif_cast(rule_);
3756 rule_construct(struct rule *rule_)
3758 struct rule_dpif *rule = rule_dpif_cast(rule_);
3759 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3760 struct rule_dpif *victim;
3764 error = validate_actions(rule->up.actions, rule->up.n_actions,
3765 &rule->up.cr.flow, ofproto->max_ports);
3770 rule->used = rule->up.created;
3771 rule->packet_count = 0;
3772 rule->byte_count = 0;
3774 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3775 if (victim && !list_is_empty(&victim->facets)) {
3776 struct facet *facet;
3778 rule->facets = victim->facets;
3779 list_moved(&rule->facets);
3780 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3781 /* XXX: We're only clearing our local counters here. It's possible
3782 * that quite a few packets are unaccounted for in the datapath
3783 * statistics. These will be accounted to the new rule instead of
3784 * cleared as required. This could be fixed by clearing out the
3785 * datapath statistics for this facet, but currently it doesn't
3787 facet_reset_counters(facet);
3791 /* Must avoid list_moved() in this case. */
3792 list_init(&rule->facets);
3795 table_id = rule->up.table_id;
3796 rule->tag = (victim ? victim->tag
3798 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3799 ofproto->tables[table_id].basis));
3801 complete_operation(rule);
3806 rule_destruct(struct rule *rule_)
3808 struct rule_dpif *rule = rule_dpif_cast(rule_);
3809 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3810 struct facet *facet, *next_facet;
3812 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3813 facet_revalidate(ofproto, facet);
3816 complete_operation(rule);
3820 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3822 struct rule_dpif *rule = rule_dpif_cast(rule_);
3823 struct facet *facet;
3825 /* Start from historical data for 'rule' itself that are no longer tracked
3826 * in facets. This counts, for example, facets that have expired. */
3827 *packets = rule->packet_count;
3828 *bytes = rule->byte_count;
3830 /* Add any statistics that are tracked by facets. This includes
3831 * statistical data recently updated by ofproto_update_stats() as well as
3832 * stats for packets that were executed "by hand" via dpif_execute(). */
3833 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3834 *packets += facet->packet_count;
3835 *bytes += facet->byte_count;
3840 rule_execute(struct rule *rule_, const struct flow *flow,
3841 struct ofpbuf *packet)
3843 struct rule_dpif *rule = rule_dpif_cast(rule_);
3844 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3845 struct action_xlate_ctx ctx;
3846 struct ofpbuf *odp_actions;
3849 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
3850 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3851 size = packet->size;
3852 if (execute_odp_actions(ofproto, flow, odp_actions->data,
3853 odp_actions->size, packet)) {
3854 rule->used = time_msec();
3855 rule->packet_count++;
3856 rule->byte_count += size;
3857 flow_push_stats(rule, flow, 1, size, rule->used);
3859 ofpbuf_delete(odp_actions);
3865 rule_modify_actions(struct rule *rule_)
3867 struct rule_dpif *rule = rule_dpif_cast(rule_);
3868 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3871 error = validate_actions(rule->up.actions, rule->up.n_actions,
3872 &rule->up.cr.flow, ofproto->max_ports);
3874 ofoperation_complete(rule->up.pending, error);
3878 complete_operation(rule);
3881 /* Sends 'packet' out 'ofport'.
3882 * May modify 'packet'.
3883 * Returns 0 if successful, otherwise a positive errno value. */
3885 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3887 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3888 struct ofpbuf key, odp_actions;
3889 struct odputil_keybuf keybuf;
3894 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
3895 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
3897 if (odp_port != ofport->odp_port) {
3898 eth_pop_vlan(packet);
3899 flow.vlan_tci = htons(0);
3902 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3903 odp_flow_key_from_flow(&key, &flow);
3905 ofpbuf_init(&odp_actions, 32);
3906 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
3908 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
3909 error = dpif_execute(ofproto->dpif,
3911 odp_actions.data, odp_actions.size,
3913 ofpbuf_uninit(&odp_actions);
3916 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
3917 ofproto->up.name, odp_port, strerror(error));
3922 /* OpenFlow to datapath action translation. */
3924 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3925 struct action_xlate_ctx *ctx);
3926 static void xlate_normal(struct action_xlate_ctx *);
3929 put_userspace_action(const struct ofproto_dpif *ofproto,
3930 struct ofpbuf *odp_actions,
3931 const struct flow *flow,
3932 const struct user_action_cookie *cookie)
3936 pid = dpif_port_get_pid(ofproto->dpif,
3937 ofp_port_to_odp_port(flow->in_port));
3939 return odp_put_userspace_action(pid, cookie, odp_actions);
3942 /* Compose SAMPLE action for sFlow. */
3944 compose_sflow_action(const struct ofproto_dpif *ofproto,
3945 struct ofpbuf *odp_actions,
3946 const struct flow *flow,
3949 uint32_t port_ifindex;
3950 uint32_t probability;
3951 struct user_action_cookie cookie;
3952 size_t sample_offset, actions_offset;
3953 int cookie_offset, n_output;
3955 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
3959 if (odp_port == OVSP_NONE) {
3963 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
3967 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
3969 /* Number of packets out of UINT_MAX to sample. */
3970 probability = dpif_sflow_get_probability(ofproto->sflow);
3971 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
3973 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
3975 cookie.type = USER_ACTION_COOKIE_SFLOW;
3976 cookie.data = port_ifindex;
3977 cookie.n_output = n_output;
3978 cookie.vlan_tci = 0;
3979 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
3981 nl_msg_end_nested(odp_actions, actions_offset);
3982 nl_msg_end_nested(odp_actions, sample_offset);
3983 return cookie_offset;
3986 /* SAMPLE action must be first action in any given list of actions.
3987 * At this point we do not have all information required to build it. So try to
3988 * build sample action as complete as possible. */
3990 add_sflow_action(struct action_xlate_ctx *ctx)
3992 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
3994 &ctx->flow, OVSP_NONE);
3995 ctx->sflow_odp_port = 0;
3996 ctx->sflow_n_outputs = 0;
3999 /* Fix SAMPLE action according to data collected while composing ODP actions.
4000 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
4001 * USERSPACE action's user-cookie which is required for sflow. */
4003 fix_sflow_action(struct action_xlate_ctx *ctx)
4005 const struct flow *base = &ctx->base_flow;
4006 struct user_action_cookie *cookie;
4008 if (!ctx->user_cookie_offset) {
4012 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
4014 assert(cookie != NULL);
4015 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
4017 if (ctx->sflow_n_outputs) {
4018 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
4019 ctx->sflow_odp_port);
4021 if (ctx->sflow_n_outputs >= 255) {
4022 cookie->n_output = 255;
4024 cookie->n_output = ctx->sflow_n_outputs;
4026 cookie->vlan_tci = base->vlan_tci;
4030 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4033 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
4034 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
4035 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
4036 uint8_t flow_nw_tos = ctx->flow.nw_tos;
4040 struct priority_to_dscp *pdscp;
4042 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
4043 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4047 pdscp = get_priority(ofport, ctx->flow.priority);
4049 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4050 ctx->flow.nw_tos |= pdscp->dscp;
4053 /* We may not have an ofport record for this port, but it doesn't hurt
4054 * to allow forwarding to it anyhow. Maybe such a port will appear
4055 * later and we're pre-populating the flow table. */
4058 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
4059 ctx->flow.vlan_tci);
4060 if (out_port != odp_port) {
4061 ctx->flow.vlan_tci = htons(0);
4063 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4064 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
4066 ctx->sflow_odp_port = odp_port;
4067 ctx->sflow_n_outputs++;
4068 ctx->nf_output_iface = ofp_port;
4069 ctx->flow.vlan_tci = flow_vlan_tci;
4070 ctx->flow.nw_tos = flow_nw_tos;
4074 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
4076 compose_output_action__(ctx, ofp_port, true);
4080 xlate_table_action(struct action_xlate_ctx *ctx,
4081 uint16_t in_port, uint8_t table_id)
4083 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
4084 struct ofproto_dpif *ofproto = ctx->ofproto;
4085 struct rule_dpif *rule;
4086 uint16_t old_in_port;
4087 uint8_t old_table_id;
4089 old_table_id = ctx->table_id;
4090 ctx->table_id = table_id;
4092 /* Look up a flow with 'in_port' as the input port. */
4093 old_in_port = ctx->flow.in_port;
4094 ctx->flow.in_port = in_port;
4095 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
4098 if (table_id > 0 && table_id < N_TABLES) {
4099 struct table_dpif *table = &ofproto->tables[table_id];
4100 if (table->other_table) {
4103 : rule_calculate_tag(&ctx->flow,
4104 &table->other_table->wc,
4109 /* Restore the original input port. Otherwise OFPP_NORMAL and
4110 * OFPP_IN_PORT will have surprising behavior. */
4111 ctx->flow.in_port = old_in_port;
4113 if (ctx->resubmit_hook) {
4114 ctx->resubmit_hook(ctx, rule);
4119 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4123 ctx->table_id = old_table_id;
4125 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4127 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
4128 MAX_RESUBMIT_RECURSION);
4133 xlate_resubmit_table(struct action_xlate_ctx *ctx,
4134 const struct nx_action_resubmit *nar)
4139 in_port = (nar->in_port == htons(OFPP_IN_PORT)
4141 : ntohs(nar->in_port));
4142 table_id = nar->table == 255 ? ctx->table_id : nar->table;
4144 xlate_table_action(ctx, in_port, table_id);
4148 flood_packets(struct action_xlate_ctx *ctx, bool all)
4150 struct ofport_dpif *ofport;
4152 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
4153 uint16_t ofp_port = ofport->up.ofp_port;
4155 if (ofp_port == ctx->flow.in_port) {
4160 compose_output_action__(ctx, ofp_port, false);
4161 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
4162 compose_output_action(ctx, ofp_port);
4166 ctx->nf_output_iface = NF_OUT_FLOOD;
4170 compose_controller_action(struct action_xlate_ctx *ctx, int len)
4172 struct user_action_cookie cookie;
4174 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4175 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4177 cookie.n_output = 0;
4178 cookie.vlan_tci = 0;
4179 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
4183 xlate_output_action__(struct action_xlate_ctx *ctx,
4184 uint16_t port, uint16_t max_len)
4186 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4188 ctx->nf_output_iface = NF_OUT_DROP;
4192 compose_output_action(ctx, ctx->flow.in_port);
4195 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
4201 flood_packets(ctx, false);
4204 flood_packets(ctx, true);
4206 case OFPP_CONTROLLER:
4207 compose_controller_action(ctx, max_len);
4210 compose_output_action(ctx, OFPP_LOCAL);
4215 if (port != ctx->flow.in_port) {
4216 compose_output_action(ctx, port);
4221 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4222 ctx->nf_output_iface = NF_OUT_FLOOD;
4223 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4224 ctx->nf_output_iface = prev_nf_output_iface;
4225 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4226 ctx->nf_output_iface != NF_OUT_FLOOD) {
4227 ctx->nf_output_iface = NF_OUT_MULTI;
4232 xlate_output_reg_action(struct action_xlate_ctx *ctx,
4233 const struct nx_action_output_reg *naor)
4237 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4239 if (ofp_port <= UINT16_MAX) {
4240 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4245 xlate_output_action(struct action_xlate_ctx *ctx,
4246 const struct ofp_action_output *oao)
4248 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4252 xlate_enqueue_action(struct action_xlate_ctx *ctx,
4253 const struct ofp_action_enqueue *oae)
4256 uint32_t flow_priority, priority;
4259 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4262 /* Fall back to ordinary output action. */
4263 xlate_output_action__(ctx, ntohs(oae->port), 0);
4267 /* Figure out datapath output port. */
4268 ofp_port = ntohs(oae->port);
4269 if (ofp_port == OFPP_IN_PORT) {
4270 ofp_port = ctx->flow.in_port;
4271 } else if (ofp_port == ctx->flow.in_port) {
4275 /* Add datapath actions. */
4276 flow_priority = ctx->flow.priority;
4277 ctx->flow.priority = priority;
4278 compose_output_action(ctx, ofp_port);
4279 ctx->flow.priority = flow_priority;
4281 /* Update NetFlow output port. */
4282 if (ctx->nf_output_iface == NF_OUT_DROP) {
4283 ctx->nf_output_iface = ofp_port;
4284 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4285 ctx->nf_output_iface = NF_OUT_MULTI;
4290 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4291 const struct nx_action_set_queue *nasq)
4296 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4299 /* Couldn't translate queue to a priority, so ignore. A warning
4300 * has already been logged. */
4304 ctx->flow.priority = priority;
4307 struct xlate_reg_state {
4313 xlate_autopath(struct action_xlate_ctx *ctx,
4314 const struct nx_action_autopath *naa)
4316 uint16_t ofp_port = ntohl(naa->id);
4317 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4319 if (!port || !port->bundle) {
4320 ofp_port = OFPP_NONE;
4321 } else if (port->bundle->bond) {
4322 /* Autopath does not support VLAN hashing. */
4323 struct ofport_dpif *slave = bond_choose_output_slave(
4324 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4326 ofp_port = slave->up.ofp_port;
4329 autopath_execute(naa, &ctx->flow, ofp_port);
4333 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4335 struct ofproto_dpif *ofproto = ofproto_;
4336 struct ofport_dpif *port;
4346 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4349 port = get_ofp_port(ofproto, ofp_port);
4350 return port ? port->may_enable : false;
4355 xlate_learn_action(struct action_xlate_ctx *ctx,
4356 const struct nx_action_learn *learn)
4358 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4359 struct ofputil_flow_mod fm;
4362 learn_execute(learn, &ctx->flow, &fm);
4364 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4365 if (error && !VLOG_DROP_WARN(&rl)) {
4366 char *msg = ofputil_error_to_string(error);
4367 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4375 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4377 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4378 ? htonl(OFPPC_NO_RECV_STP)
4379 : htonl(OFPPC_NO_RECV))) {
4383 /* Only drop packets here if both forwarding and learning are
4384 * disabled. If just learning is enabled, we need to have
4385 * OFPP_NORMAL and the learning action have a look at the packet
4386 * before we can drop it. */
4387 if (!stp_forward_in_state(port->stp_state)
4388 && !stp_learn_in_state(port->stp_state)) {
4396 do_xlate_actions(const union ofp_action *in, size_t n_in,
4397 struct action_xlate_ctx *ctx)
4399 const struct ofport_dpif *port;
4400 const union ofp_action *ia;
4403 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4404 if (port && !may_receive(port, ctx)) {
4405 /* Drop this flow. */
4409 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4410 const struct ofp_action_dl_addr *oada;
4411 const struct nx_action_resubmit *nar;
4412 const struct nx_action_set_tunnel *nast;
4413 const struct nx_action_set_queue *nasq;
4414 const struct nx_action_multipath *nam;
4415 const struct nx_action_autopath *naa;
4416 const struct nx_action_bundle *nab;
4417 const struct nx_action_output_reg *naor;
4418 enum ofputil_action_code code;
4425 code = ofputil_decode_action_unsafe(ia);
4427 case OFPUTIL_OFPAT_OUTPUT:
4428 xlate_output_action(ctx, &ia->output);
4431 case OFPUTIL_OFPAT_SET_VLAN_VID:
4432 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4433 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4436 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4437 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4438 ctx->flow.vlan_tci |= htons(
4439 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4442 case OFPUTIL_OFPAT_STRIP_VLAN:
4443 ctx->flow.vlan_tci = htons(0);
4446 case OFPUTIL_OFPAT_SET_DL_SRC:
4447 oada = ((struct ofp_action_dl_addr *) ia);
4448 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4451 case OFPUTIL_OFPAT_SET_DL_DST:
4452 oada = ((struct ofp_action_dl_addr *) ia);
4453 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4456 case OFPUTIL_OFPAT_SET_NW_SRC:
4457 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4460 case OFPUTIL_OFPAT_SET_NW_DST:
4461 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4464 case OFPUTIL_OFPAT_SET_NW_TOS:
4465 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4466 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4469 case OFPUTIL_OFPAT_SET_TP_SRC:
4470 ctx->flow.tp_src = ia->tp_port.tp_port;
4473 case OFPUTIL_OFPAT_SET_TP_DST:
4474 ctx->flow.tp_dst = ia->tp_port.tp_port;
4477 case OFPUTIL_OFPAT_ENQUEUE:
4478 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4481 case OFPUTIL_NXAST_RESUBMIT:
4482 nar = (const struct nx_action_resubmit *) ia;
4483 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4486 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4487 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4490 case OFPUTIL_NXAST_SET_TUNNEL:
4491 nast = (const struct nx_action_set_tunnel *) ia;
4492 tun_id = htonll(ntohl(nast->tun_id));
4493 ctx->flow.tun_id = tun_id;
4496 case OFPUTIL_NXAST_SET_QUEUE:
4497 nasq = (const struct nx_action_set_queue *) ia;
4498 xlate_set_queue_action(ctx, nasq);
4501 case OFPUTIL_NXAST_POP_QUEUE:
4502 ctx->flow.priority = ctx->original_priority;
4505 case OFPUTIL_NXAST_REG_MOVE:
4506 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4510 case OFPUTIL_NXAST_REG_LOAD:
4511 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4515 case OFPUTIL_NXAST_NOTE:
4516 /* Nothing to do. */
4519 case OFPUTIL_NXAST_SET_TUNNEL64:
4520 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4521 ctx->flow.tun_id = tun_id;
4524 case OFPUTIL_NXAST_MULTIPATH:
4525 nam = (const struct nx_action_multipath *) ia;
4526 multipath_execute(nam, &ctx->flow);
4529 case OFPUTIL_NXAST_AUTOPATH:
4530 naa = (const struct nx_action_autopath *) ia;
4531 xlate_autopath(ctx, naa);
4534 case OFPUTIL_NXAST_BUNDLE:
4535 ctx->ofproto->has_bundle_action = true;
4536 nab = (const struct nx_action_bundle *) ia;
4537 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4542 case OFPUTIL_NXAST_BUNDLE_LOAD:
4543 ctx->ofproto->has_bundle_action = true;
4544 nab = (const struct nx_action_bundle *) ia;
4545 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4549 case OFPUTIL_NXAST_OUTPUT_REG:
4550 naor = (const struct nx_action_output_reg *) ia;
4551 xlate_output_reg_action(ctx, naor);
4554 case OFPUTIL_NXAST_LEARN:
4555 ctx->has_learn = true;
4556 if (ctx->may_learn) {
4557 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4561 case OFPUTIL_NXAST_EXIT:
4567 /* We've let OFPP_NORMAL and the learning action look at the packet,
4568 * so drop it now if forwarding is disabled. */
4569 if (port && !stp_forward_in_state(port->stp_state)) {
4570 ofpbuf_clear(ctx->odp_actions);
4571 add_sflow_action(ctx);
4576 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4577 struct ofproto_dpif *ofproto, const struct flow *flow,
4578 ovs_be16 initial_tci, const struct ofpbuf *packet)
4580 ctx->ofproto = ofproto;
4582 ctx->base_flow = ctx->flow;
4583 ctx->base_flow.tun_id = 0;
4584 ctx->base_flow.vlan_tci = initial_tci;
4585 ctx->packet = packet;
4586 ctx->may_learn = packet != NULL;
4587 ctx->resubmit_hook = NULL;
4590 static struct ofpbuf *
4591 xlate_actions(struct action_xlate_ctx *ctx,
4592 const union ofp_action *in, size_t n_in)
4594 struct flow orig_flow = ctx->flow;
4596 COVERAGE_INC(ofproto_dpif_xlate);
4598 ctx->odp_actions = ofpbuf_new(512);
4599 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4601 ctx->may_set_up_flow = true;
4602 ctx->has_learn = false;
4603 ctx->has_normal = false;
4604 ctx->nf_output_iface = NF_OUT_DROP;
4607 ctx->original_priority = ctx->flow.priority;
4611 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4612 switch (ctx->ofproto->up.frag_handling) {
4613 case OFPC_FRAG_NORMAL:
4614 /* We must pretend that transport ports are unavailable. */
4615 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4616 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4619 case OFPC_FRAG_DROP:
4620 return ctx->odp_actions;
4622 case OFPC_FRAG_REASM:
4625 case OFPC_FRAG_NX_MATCH:
4626 /* Nothing to do. */
4631 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4632 ctx->may_set_up_flow = false;
4633 return ctx->odp_actions;
4635 add_sflow_action(ctx);
4636 do_xlate_actions(in, n_in, ctx);
4638 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4639 ctx->odp_actions->data,
4640 ctx->odp_actions->size)) {
4641 ctx->may_set_up_flow = false;
4643 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4645 compose_output_action(ctx, OFPP_LOCAL);
4648 add_mirror_actions(ctx, &orig_flow);
4649 fix_sflow_action(ctx);
4652 return ctx->odp_actions;
4655 /* OFPP_NORMAL implementation. */
4657 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4659 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4660 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4661 * the bundle on which the packet was received, returns the VLAN to which the
4664 * Both 'vid' and the return value are in the range 0...4095. */
4666 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4668 switch (in_bundle->vlan_mode) {
4669 case PORT_VLAN_ACCESS:
4670 return in_bundle->vlan;
4673 case PORT_VLAN_TRUNK:
4676 case PORT_VLAN_NATIVE_UNTAGGED:
4677 case PORT_VLAN_NATIVE_TAGGED:
4678 return vid ? vid : in_bundle->vlan;
4685 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4686 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4689 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4690 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4693 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4695 switch (in_bundle->vlan_mode) {
4696 case PORT_VLAN_ACCESS:
4699 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4700 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4701 "packet received on port %s configured as VLAN "
4702 "%"PRIu16" access port",
4703 in_bundle->ofproto->up.name, vid,
4704 in_bundle->name, in_bundle->vlan);
4710 case PORT_VLAN_NATIVE_UNTAGGED:
4711 case PORT_VLAN_NATIVE_TAGGED:
4713 /* Port must always carry its native VLAN. */
4717 case PORT_VLAN_TRUNK:
4718 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4720 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4721 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4722 "received on port %s not configured for trunking "
4724 in_bundle->ofproto->up.name, vid,
4725 in_bundle->name, vid);
4737 /* Given 'vlan', the VLAN that a packet belongs to, and
4738 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4739 * that should be included in the 802.1Q header. (If the return value is 0,
4740 * then the 802.1Q header should only be included in the packet if there is a
4743 * Both 'vlan' and the return value are in the range 0...4095. */
4745 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4747 switch (out_bundle->vlan_mode) {
4748 case PORT_VLAN_ACCESS:
4751 case PORT_VLAN_TRUNK:
4752 case PORT_VLAN_NATIVE_TAGGED:
4755 case PORT_VLAN_NATIVE_UNTAGGED:
4756 return vlan == out_bundle->vlan ? 0 : vlan;
4764 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4767 struct ofport_dpif *port;
4769 ovs_be16 tci, old_tci;
4771 vid = output_vlan_to_vid(out_bundle, vlan);
4772 if (!out_bundle->bond) {
4773 port = ofbundle_get_a_port(out_bundle);
4775 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4778 /* No slaves enabled, so drop packet. */
4783 old_tci = ctx->flow.vlan_tci;
4785 if (tci || out_bundle->use_priority_tags) {
4786 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4788 tci |= htons(VLAN_CFI);
4791 ctx->flow.vlan_tci = tci;
4793 compose_output_action(ctx, port->up.ofp_port);
4794 ctx->flow.vlan_tci = old_tci;
4798 mirror_mask_ffs(mirror_mask_t mask)
4800 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4805 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4807 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4808 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4812 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4814 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4817 /* Returns an arbitrary interface within 'bundle'. */
4818 static struct ofport_dpif *
4819 ofbundle_get_a_port(const struct ofbundle *bundle)
4821 return CONTAINER_OF(list_front(&bundle->ports),
4822 struct ofport_dpif, bundle_node);
4826 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4828 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4831 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4832 * to a VLAN. In general most packets may be mirrored but we want to drop
4833 * protocols that may confuse switches. */
4835 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4837 /* If you change this function's behavior, please update corresponding
4838 * documentation in vswitch.xml at the same time. */
4839 if (dst[0] != 0x01) {
4840 /* All the currently banned MACs happen to start with 01 currently, so
4841 * this is a quick way to eliminate most of the good ones. */
4843 if (eth_addr_is_reserved(dst)) {
4844 /* Drop STP, IEEE pause frames, and other reserved protocols
4845 * (01-80-c2-00-00-0x). */
4849 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
4851 if ((dst[3] & 0xfe) == 0xcc &&
4852 (dst[4] & 0xfe) == 0xcc &&
4853 (dst[5] & 0xfe) == 0xcc) {
4854 /* Drop the following protocols plus others following the same
4857 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
4858 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
4859 STP Uplink Fast (01-00-0c-cd-cd-cd) */
4863 if (!(dst[3] | dst[4] | dst[5])) {
4864 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
4873 add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
4875 struct ofproto_dpif *ofproto = ctx->ofproto;
4876 mirror_mask_t mirrors;
4877 struct ofport_dpif *in_port;
4878 struct ofbundle *in_bundle;
4881 const struct nlattr *a;
4884 /* Obtain in_port from orig_flow.in_port.
4886 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
4887 in_port = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
4888 ctx->packet != NULL);
4892 in_bundle = in_port->bundle;
4893 mirrors = in_bundle->src_mirrors;
4895 /* Drop frames on bundles reserved for mirroring. */
4896 if (in_bundle->mirror_out) {
4897 if (ctx->packet != NULL) {
4898 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4899 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
4900 "%s, which is reserved exclusively for mirroring",
4901 ctx->ofproto->up.name, in_bundle->name);
4907 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
4908 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
4911 vlan = input_vid_to_vlan(in_bundle, vid);
4913 /* Look at the output ports to check for destination selections. */
4915 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
4916 ctx->odp_actions->size) {
4917 enum ovs_action_attr type = nl_attr_type(a);
4918 struct ofport_dpif *ofport;
4920 if (type != OVS_ACTION_ATTR_OUTPUT) {
4924 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
4925 mirrors |= ofport ? ofport->bundle->dst_mirrors : 0;
4932 /* Restore the original packet before adding the mirror actions. */
4933 ctx->flow = *orig_flow;
4938 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4940 if (!vlan_is_mirrored(m, vlan)) {
4941 mirrors &= mirrors - 1;
4945 mirrors &= ~m->dup_mirrors;
4946 ctx->mirrors |= m->dup_mirrors;
4948 output_normal(ctx, m->out, vlan);
4949 } else if (eth_dst_may_rspan(orig_flow->dl_dst)
4950 && vlan != m->out_vlan) {
4951 struct ofbundle *bundle;
4953 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4954 if (ofbundle_includes_vlan(bundle, m->out_vlan)
4955 && !bundle->mirror_out) {
4956 output_normal(ctx, bundle, m->out_vlan);
4964 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
4965 uint64_t packets, uint64_t bytes)
4971 for (; mirrors; mirrors &= mirrors - 1) {
4974 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
4977 /* In normal circumstances 'm' will not be NULL. However,
4978 * if mirrors are reconfigured, we can temporarily get out
4979 * of sync in facet_revalidate(). We could "correct" the
4980 * mirror list before reaching here, but doing that would
4981 * not properly account the traffic stats we've currently
4982 * accumulated for previous mirror configuration. */
4986 m->packet_count += packets;
4987 m->byte_count += bytes;
4991 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
4992 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
4993 * indicate this; newer upstream kernels use gratuitous ARP requests. */
4995 is_gratuitous_arp(const struct flow *flow)
4997 return (flow->dl_type == htons(ETH_TYPE_ARP)
4998 && eth_addr_is_broadcast(flow->dl_dst)
4999 && (flow->nw_proto == ARP_OP_REPLY
5000 || (flow->nw_proto == ARP_OP_REQUEST
5001 && flow->nw_src == flow->nw_dst)));
5005 update_learning_table(struct ofproto_dpif *ofproto,
5006 const struct flow *flow, int vlan,
5007 struct ofbundle *in_bundle)
5009 struct mac_entry *mac;
5011 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
5015 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
5016 if (is_gratuitous_arp(flow)) {
5017 /* We don't want to learn from gratuitous ARP packets that are
5018 * reflected back over bond slaves so we lock the learning table. */
5019 if (!in_bundle->bond) {
5020 mac_entry_set_grat_arp_lock(mac);
5021 } else if (mac_entry_is_grat_arp_locked(mac)) {
5026 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
5027 /* The log messages here could actually be useful in debugging,
5028 * so keep the rate limit relatively high. */
5029 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
5030 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
5031 "on port %s in VLAN %d",
5032 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
5033 in_bundle->name, vlan);
5035 mac->port.p = in_bundle;
5036 tag_set_add(&ofproto->revalidate_set,
5037 mac_learning_changed(ofproto->ml, mac));
5041 static struct ofport_dpif *
5042 lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
5044 struct ofport_dpif *ofport;
5046 /* Find the port and bundle for the received packet. */
5047 ofport = get_ofp_port(ofproto, in_port);
5048 if (ofport && ofport->bundle) {
5052 /* Odd. A few possible reasons here:
5054 * - We deleted a port but there are still a few packets queued up
5057 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
5058 * we don't know about.
5060 * - The ofproto client didn't configure the port as part of a bundle.
5063 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5065 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
5066 "port %"PRIu16, ofproto->up.name, in_port);
5071 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
5072 * dropped. Returns true if they may be forwarded, false if they should be
5075 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
5076 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
5078 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
5079 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
5080 * checked by input_vid_is_valid().
5082 * May also add tags to '*tags', although the current implementation only does
5083 * so in one special case.
5086 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
5087 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
5089 struct ofbundle *in_bundle = in_port->bundle;
5091 /* Drop frames for reserved multicast addresses
5092 * only if forward_bpdu option is absent. */
5093 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
5097 if (in_bundle->bond) {
5098 struct mac_entry *mac;
5100 switch (bond_check_admissibility(in_bundle->bond, in_port,
5101 flow->dl_dst, tags)) {
5108 case BV_DROP_IF_MOVED:
5109 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
5110 if (mac && mac->port.p != in_bundle &&
5111 (!is_gratuitous_arp(flow)
5112 || mac_entry_is_grat_arp_locked(mac))) {
5123 xlate_normal(struct action_xlate_ctx *ctx)
5125 struct ofport_dpif *in_port;
5126 struct ofbundle *in_bundle;
5127 struct mac_entry *mac;
5131 ctx->has_normal = true;
5133 /* Obtain in_port from ctx->flow.in_port.
5135 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5136 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
5137 ctx->packet != NULL);
5141 in_bundle = in_port->bundle;
5143 /* Drop malformed frames. */
5144 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
5145 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
5146 if (ctx->packet != NULL) {
5147 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5148 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
5149 "VLAN tag received on port %s",
5150 ctx->ofproto->up.name, in_bundle->name);
5155 /* Drop frames on bundles reserved for mirroring. */
5156 if (in_bundle->mirror_out) {
5157 if (ctx->packet != NULL) {
5158 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5159 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5160 "%s, which is reserved exclusively for mirroring",
5161 ctx->ofproto->up.name, in_bundle->name);
5167 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
5168 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5171 vlan = input_vid_to_vlan(in_bundle, vid);
5173 /* Check other admissibility requirements. */
5174 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
5178 /* Learn source MAC. */
5179 if (ctx->may_learn) {
5180 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
5183 /* Determine output bundle. */
5184 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
5187 if (mac->port.p != in_bundle) {
5188 output_normal(ctx, mac->port.p, vlan);
5190 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
5191 /* If we are revalidating but don't have a learning entry then eject
5192 * the flow. Installing a flow that floods packets opens up a window
5193 * of time where we could learn from a packet reflected on a bond and
5194 * blackhole packets before the learning table is updated to reflect
5195 * the correct port. */
5196 ctx->may_set_up_flow = false;
5199 struct ofbundle *bundle;
5201 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
5202 if (bundle != in_bundle
5203 && ofbundle_includes_vlan(bundle, vlan)
5204 && bundle->floodable
5205 && !bundle->mirror_out) {
5206 output_normal(ctx, bundle, vlan);
5209 ctx->nf_output_iface = NF_OUT_FLOOD;
5213 /* Optimized flow revalidation.
5215 * It's a difficult problem, in general, to tell which facets need to have
5216 * their actions recalculated whenever the OpenFlow flow table changes. We
5217 * don't try to solve that general problem: for most kinds of OpenFlow flow
5218 * table changes, we recalculate the actions for every facet. This is
5219 * relatively expensive, but it's good enough if the OpenFlow flow table
5220 * doesn't change very often.
5222 * However, we can expect one particular kind of OpenFlow flow table change to
5223 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5224 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5225 * table, we add a special case that applies to flow tables in which every rule
5226 * has the same form (that is, the same wildcards), except that the table is
5227 * also allowed to have a single "catch-all" flow that matches all packets. We
5228 * optimize this case by tagging all of the facets that resubmit into the table
5229 * and invalidating the same tag whenever a flow changes in that table. The
5230 * end result is that we revalidate just the facets that need it (and sometimes
5231 * a few more, but not all of the facets or even all of the facets that
5232 * resubmit to the table modified by MAC learning). */
5234 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5235 * into an OpenFlow table with the given 'basis'. */
5237 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5240 if (flow_wildcards_is_catchall(wc)) {
5243 struct flow tag_flow = *flow;
5244 flow_zero_wildcards(&tag_flow, wc);
5245 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5249 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5250 * taggability of that table.
5252 * This function must be called after *each* change to a flow table. If you
5253 * skip calling it on some changes then the pointer comparisons at the end can
5254 * be invalid if you get unlucky. For example, if a flow removal causes a
5255 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5256 * different wildcards to be created with the same address, then this function
5257 * will incorrectly skip revalidation. */
5259 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5261 struct table_dpif *table = &ofproto->tables[table_id];
5262 const struct classifier *cls = &ofproto->up.tables[table_id];
5263 struct cls_table *catchall, *other;
5264 struct cls_table *t;
5266 catchall = other = NULL;
5268 switch (hmap_count(&cls->tables)) {
5270 /* We could tag this OpenFlow table but it would make the logic a
5271 * little harder and it's a corner case that doesn't seem worth it
5277 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5278 if (cls_table_is_catchall(t)) {
5280 } else if (!other) {
5283 /* Indicate that we can't tag this by setting both tables to
5284 * NULL. (We know that 'catchall' is already NULL.) */
5291 /* Can't tag this table. */
5295 if (table->catchall_table != catchall || table->other_table != other) {
5296 table->catchall_table = catchall;
5297 table->other_table = other;
5298 ofproto->need_revalidate = true;
5302 /* Given 'rule' that has changed in some way (either it is a rule being
5303 * inserted, a rule being deleted, or a rule whose actions are being
5304 * modified), marks facets for revalidation to ensure that packets will be
5305 * forwarded correctly according to the new state of the flow table.
5307 * This function must be called after *each* change to a flow table. See
5308 * the comment on table_update_taggable() for more information. */
5310 rule_invalidate(const struct rule_dpif *rule)
5312 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5314 table_update_taggable(ofproto, rule->up.table_id);
5316 if (!ofproto->need_revalidate) {
5317 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5319 if (table->other_table && rule->tag) {
5320 tag_set_add(&ofproto->revalidate_set, rule->tag);
5322 ofproto->need_revalidate = true;
5328 set_frag_handling(struct ofproto *ofproto_,
5329 enum ofp_config_flags frag_handling)
5331 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5333 if (frag_handling != OFPC_FRAG_REASM) {
5334 ofproto->need_revalidate = true;
5342 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5343 const struct flow *flow,
5344 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5346 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5349 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5350 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5353 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5354 ofproto->max_ports);
5356 struct odputil_keybuf keybuf;
5357 struct action_xlate_ctx ctx;
5358 struct ofpbuf *odp_actions;
5361 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5362 odp_flow_key_from_flow(&key, flow);
5364 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
5365 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5366 dpif_execute(ofproto->dpif, key.data, key.size,
5367 odp_actions->data, odp_actions->size, packet);
5368 ofpbuf_delete(odp_actions);
5376 set_netflow(struct ofproto *ofproto_,
5377 const struct netflow_options *netflow_options)
5379 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5381 if (netflow_options) {
5382 if (!ofproto->netflow) {
5383 ofproto->netflow = netflow_create();
5385 return netflow_set_options(ofproto->netflow, netflow_options);
5387 netflow_destroy(ofproto->netflow);
5388 ofproto->netflow = NULL;
5394 get_netflow_ids(const struct ofproto *ofproto_,
5395 uint8_t *engine_type, uint8_t *engine_id)
5397 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5399 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5403 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5405 if (!facet_is_controller_flow(facet) &&
5406 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5407 struct subfacet *subfacet;
5408 struct ofexpired expired;
5410 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5411 if (subfacet->installed) {
5412 struct dpif_flow_stats stats;
5414 subfacet_install(ofproto, subfacet, subfacet->actions,
5415 subfacet->actions_len, &stats);
5416 subfacet_update_stats(ofproto, subfacet, &stats);
5420 expired.flow = facet->flow;
5421 expired.packet_count = facet->packet_count;
5422 expired.byte_count = facet->byte_count;
5423 expired.used = facet->used;
5424 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5429 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5431 struct facet *facet;
5433 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5434 send_active_timeout(ofproto, facet);
5438 static struct ofproto_dpif *
5439 ofproto_dpif_lookup(const char *name)
5441 struct ofproto *ofproto = ofproto_lookup(name);
5442 return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
5443 ? ofproto_dpif_cast(ofproto)
5448 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn,
5449 const char *args, void *aux OVS_UNUSED)
5451 const struct ofproto_dpif *ofproto;
5453 ofproto = ofproto_dpif_lookup(args);
5455 unixctl_command_reply(conn, 501, "no such bridge");
5458 mac_learning_flush(ofproto->ml);
5460 unixctl_command_reply(conn, 200, "table successfully flushed");
5464 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5465 const char *args, void *aux OVS_UNUSED)
5467 struct ds ds = DS_EMPTY_INITIALIZER;
5468 const struct ofproto_dpif *ofproto;
5469 const struct mac_entry *e;
5471 ofproto = ofproto_dpif_lookup(args);
5473 unixctl_command_reply(conn, 501, "no such bridge");
5477 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5478 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5479 struct ofbundle *bundle = e->port.p;
5480 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5481 ofbundle_get_a_port(bundle)->odp_port,
5482 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5484 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5488 struct ofproto_trace {
5489 struct action_xlate_ctx ctx;
5495 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5496 const struct rule_dpif *rule)
5498 ds_put_char_multiple(result, '\t', level);
5500 ds_put_cstr(result, "No match\n");
5504 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5505 table_id, ntohll(rule->up.flow_cookie));
5506 cls_rule_format(&rule->up.cr, result);
5507 ds_put_char(result, '\n');
5509 ds_put_char_multiple(result, '\t', level);
5510 ds_put_cstr(result, "OpenFlow ");
5511 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5512 ds_put_char(result, '\n');
5516 trace_format_flow(struct ds *result, int level, const char *title,
5517 struct ofproto_trace *trace)
5519 ds_put_char_multiple(result, '\t', level);
5520 ds_put_format(result, "%s: ", title);
5521 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5522 ds_put_cstr(result, "unchanged");
5524 flow_format(result, &trace->ctx.flow);
5525 trace->flow = trace->ctx.flow;
5527 ds_put_char(result, '\n');
5531 trace_format_regs(struct ds *result, int level, const char *title,
5532 struct ofproto_trace *trace)
5536 ds_put_char_multiple(result, '\t', level);
5537 ds_put_format(result, "%s:", title);
5538 for (i = 0; i < FLOW_N_REGS; i++) {
5539 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5541 ds_put_char(result, '\n');
5545 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5547 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5548 struct ds *result = trace->result;
5550 ds_put_char(result, '\n');
5551 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5552 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5553 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5557 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5558 void *aux OVS_UNUSED)
5560 char *dpname, *arg1, *arg2, *arg3, *arg4;
5561 char *args = xstrdup(args_);
5562 char *save_ptr = NULL;
5563 struct ofproto_dpif *ofproto;
5564 struct ofpbuf odp_key;
5565 struct ofpbuf *packet;
5566 struct rule_dpif *rule;
5567 ovs_be16 initial_tci;
5573 ofpbuf_init(&odp_key, 0);
5576 dpname = strtok_r(args, " ", &save_ptr);
5578 unixctl_command_reply(conn, 501, "Bad command syntax");
5582 ofproto = ofproto_dpif_lookup(dpname);
5584 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5588 arg1 = strtok_r(NULL, " ", &save_ptr);
5589 arg2 = strtok_r(NULL, " ", &save_ptr);
5590 arg3 = strtok_r(NULL, " ", &save_ptr);
5591 arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5592 if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) {
5593 /* ofproto/trace dpname flow [-generate] */
5596 /* Convert string to datapath key. */
5597 ofpbuf_init(&odp_key, 0);
5598 error = odp_flow_key_from_string(arg1, NULL, &odp_key);
5600 unixctl_command_reply(conn, 501, "Bad flow syntax");
5604 /* Convert odp_key to flow. */
5605 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
5606 odp_key.size, &flow,
5608 if (error == ODP_FIT_ERROR) {
5609 unixctl_command_reply(conn, 501, "Invalid flow");
5613 /* Generate a packet, if requested. */
5615 packet = ofpbuf_new(0);
5616 flow_compose(packet, &flow);
5618 } else if (dpname && arg1 && arg2 && arg3 && arg4) {
5619 /* ofproto/trace dpname priority tun_id in_port packet */
5624 priority = atoi(arg1);
5625 tun_id = htonll(strtoull(arg2, NULL, 0));
5626 in_port = ofp_port_to_odp_port(atoi(arg3));
5628 packet = ofpbuf_new(strlen(args) / 2);
5629 arg4 = ofpbuf_put_hex(packet, arg4, NULL);
5630 arg4 += strspn(arg4, " ");
5631 if (*arg4 != '\0') {
5632 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5635 if (packet->size < ETH_HEADER_LEN) {
5636 unixctl_command_reply(conn, 501,
5637 "Packet data too short for Ethernet");
5641 ds_put_cstr(&result, "Packet: ");
5642 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5643 ds_put_cstr(&result, s);
5646 flow_extract(packet, priority, tun_id, in_port, &flow);
5647 initial_tci = flow.vlan_tci;
5649 unixctl_command_reply(conn, 501, "Bad command syntax");
5653 ds_put_cstr(&result, "Flow: ");
5654 flow_format(&result, &flow);
5655 ds_put_char(&result, '\n');
5657 rule = rule_dpif_lookup(ofproto, &flow, 0);
5658 trace_format_rule(&result, 0, 0, rule);
5660 struct ofproto_trace trace;
5661 struct ofpbuf *odp_actions;
5663 trace.result = &result;
5665 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, initial_tci, packet);
5666 trace.ctx.resubmit_hook = trace_resubmit;
5667 odp_actions = xlate_actions(&trace.ctx,
5668 rule->up.actions, rule->up.n_actions);
5670 ds_put_char(&result, '\n');
5671 trace_format_flow(&result, 0, "Final flow", &trace);
5672 ds_put_cstr(&result, "Datapath actions: ");
5673 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5674 ofpbuf_delete(odp_actions);
5676 if (!trace.ctx.may_set_up_flow) {
5678 ds_put_cstr(&result, "\nThis flow is not cachable.");
5680 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5681 "for complete actions, please supply a packet.");
5686 unixctl_command_reply(conn, 200, ds_cstr(&result));
5689 ds_destroy(&result);
5690 ofpbuf_delete(packet);
5691 ofpbuf_uninit(&odp_key);
5696 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED,
5697 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5700 unixctl_command_reply(conn, 200, NULL);
5704 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED,
5705 const char *args_ OVS_UNUSED, void *aux OVS_UNUSED)
5708 unixctl_command_reply(conn, 200, NULL);
5712 ofproto_dpif_unixctl_init(void)
5714 static bool registered;
5720 unixctl_command_register("ofproto/trace",
5721 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5722 ofproto_unixctl_trace, NULL);
5723 unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush,
5725 unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show,
5727 unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL);
5728 unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL);
5731 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5733 * This is deprecated. It is only for compatibility with broken device drivers
5734 * in old versions of Linux that do not properly support VLANs when VLAN
5735 * devices are not used. When broken device drivers are no longer in
5736 * widespread use, we will delete these interfaces. */
5739 set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
5741 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5742 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5744 if (realdev_ofp_port == ofport->realdev_ofp_port
5745 && vid == ofport->vlandev_vid) {
5749 ofproto->need_revalidate = true;
5751 if (ofport->realdev_ofp_port) {
5754 if (realdev_ofp_port && ofport->bundle) {
5755 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5756 * themselves be part of a bundle. */
5757 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
5760 ofport->realdev_ofp_port = realdev_ofp_port;
5761 ofport->vlandev_vid = vid;
5763 if (realdev_ofp_port) {
5764 vsp_add(ofport, realdev_ofp_port, vid);
5771 hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
5773 return hash_2words(realdev_ofp_port, vid);
5777 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5778 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
5780 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5781 uint16_t realdev_ofp_port = odp_port_to_ofp_port(realdev_odp_port);
5782 int vid = vlan_tci_to_vid(vlan_tci);
5783 const struct vlan_splinter *vsp;
5785 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5786 hash_realdev_vid(realdev_ofp_port, vid),
5787 &ofproto->realdev_vid_map) {
5788 if (vsp->realdev_ofp_port == realdev_ofp_port
5789 && vsp->vid == vid) {
5790 return ofp_port_to_odp_port(vsp->vlandev_ofp_port);
5794 return realdev_odp_port;
5797 static struct vlan_splinter *
5798 vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
5800 struct vlan_splinter *vsp;
5802 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
5803 &ofproto->vlandev_map) {
5804 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5813 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5814 uint16_t vlandev_ofp_port, int *vid)
5816 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5817 const struct vlan_splinter *vsp;
5819 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5824 return vsp->realdev_ofp_port;
5831 vsp_remove(struct ofport_dpif *port)
5833 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5834 struct vlan_splinter *vsp;
5836 vsp = vlandev_find(ofproto, port->up.ofp_port);
5838 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5839 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5842 port->realdev_ofp_port = 0;
5844 VLOG_ERR("missing vlan device record");
5849 vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
5851 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5853 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5854 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
5855 == realdev_ofp_port)) {
5856 struct vlan_splinter *vsp;
5858 vsp = xmalloc(sizeof *vsp);
5859 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5860 hash_int(port->up.ofp_port, 0));
5861 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5862 hash_realdev_vid(realdev_ofp_port, vid));
5863 vsp->realdev_ofp_port = realdev_ofp_port;
5864 vsp->vlandev_ofp_port = port->up.ofp_port;
5867 port->realdev_ofp_port = realdev_ofp_port;
5869 VLOG_ERR("duplicate vlan device record");
5873 const struct ofproto_class ofproto_dpif_class = {
5901 port_is_lacp_current,
5902 NULL, /* rule_choose_table */
5909 rule_modify_actions,
5917 get_cfm_remote_mpids,
5921 get_stp_port_status,
5928 is_mirror_output_bundle,
5929 forward_bpdu_changed,