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 orig_skb_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, bool clone);
323 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
325 static void facet_update_time(struct ofproto_dpif *, struct facet *,
327 static void facet_reset_counters(struct facet *);
328 static void facet_push_stats(struct facet *);
329 static void facet_account(struct ofproto_dpif *, struct facet *);
331 static bool facet_is_controller_flow(struct facet *);
333 /* A dpif flow and actions associated with a facet.
335 * See also the large comment on struct facet. */
338 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
339 struct list list_node; /* In struct facet's 'facets' list. */
340 struct facet *facet; /* Owning facet. */
344 * To save memory in the common case, 'key' is NULL if 'key_fitness' is
345 * ODP_FIT_PERFECT, that is, odp_flow_key_from_flow() can accurately
346 * regenerate the ODP flow key from ->facet->flow. */
347 enum odp_key_fitness key_fitness;
351 long long int used; /* Time last used; time created if not used. */
353 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
354 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
358 * These should be essentially identical for every subfacet in a facet, but
359 * may differ in trivial ways due to VLAN splinters. */
360 size_t actions_len; /* Number of bytes in actions[]. */
361 struct nlattr *actions; /* Datapath actions. */
363 bool installed; /* Installed in datapath? */
365 /* This value is normally the same as ->facet->flow.vlan_tci. Only VLAN
366 * splinters can cause it to differ. This value should be removed when
367 * the VLAN splinters feature is no longer needed. */
368 ovs_be16 initial_tci; /* Initial VLAN TCI value. */
371 static struct subfacet *subfacet_create(struct ofproto_dpif *, struct facet *,
372 enum odp_key_fitness,
373 const struct nlattr *key,
374 size_t key_len, ovs_be16 initial_tci);
375 static struct subfacet *subfacet_find(struct ofproto_dpif *,
376 const struct nlattr *key, size_t key_len);
377 static void subfacet_destroy(struct ofproto_dpif *, struct subfacet *);
378 static void subfacet_destroy__(struct ofproto_dpif *, struct subfacet *);
379 static void subfacet_reset_dp_stats(struct subfacet *,
380 struct dpif_flow_stats *);
381 static void subfacet_update_time(struct ofproto_dpif *, struct subfacet *,
383 static void subfacet_update_stats(struct ofproto_dpif *, struct subfacet *,
384 const struct dpif_flow_stats *);
385 static void subfacet_make_actions(struct ofproto_dpif *, struct subfacet *,
386 const struct ofpbuf *packet);
387 static int subfacet_install(struct ofproto_dpif *, struct subfacet *,
388 const struct nlattr *actions, size_t actions_len,
389 struct dpif_flow_stats *);
390 static void subfacet_uninstall(struct ofproto_dpif *, struct subfacet *);
396 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
397 struct list bundle_node; /* In struct ofbundle's "ports" list. */
398 struct cfm *cfm; /* Connectivity Fault Management, if any. */
399 tag_type tag; /* Tag associated with this port. */
400 uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */
401 bool may_enable; /* May be enabled in bonds. */
404 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
405 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
406 long long int stp_state_entered;
408 struct hmap priorities; /* Map of attached 'priority_to_dscp's. */
410 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
412 * This is deprecated. It is only for compatibility with broken device
413 * drivers in old versions of Linux that do not properly support VLANs when
414 * VLAN devices are not used. When broken device drivers are no longer in
415 * widespread use, we will delete these interfaces. */
416 uint16_t realdev_ofp_port;
420 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
421 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
422 * traffic egressing the 'ofport' with that priority should be marked with. */
423 struct priority_to_dscp {
424 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
425 uint32_t priority; /* Priority of this queue (see struct flow). */
427 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
430 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
432 * This is deprecated. It is only for compatibility with broken device drivers
433 * in old versions of Linux that do not properly support VLANs when VLAN
434 * devices are not used. When broken device drivers are no longer in
435 * widespread use, we will delete these interfaces. */
436 struct vlan_splinter {
437 struct hmap_node realdev_vid_node;
438 struct hmap_node vlandev_node;
439 uint16_t realdev_ofp_port;
440 uint16_t vlandev_ofp_port;
444 static uint32_t vsp_realdev_to_vlandev(const struct ofproto_dpif *,
445 uint32_t realdev, ovs_be16 vlan_tci);
446 static uint16_t vsp_vlandev_to_realdev(const struct ofproto_dpif *,
447 uint16_t vlandev, int *vid);
448 static void vsp_remove(struct ofport_dpif *);
449 static void vsp_add(struct ofport_dpif *, uint16_t realdev_ofp_port, int vid);
451 static struct ofport_dpif *
452 ofport_dpif_cast(const struct ofport *ofport)
454 assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
455 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
458 static void port_run(struct ofport_dpif *);
459 static void port_wait(struct ofport_dpif *);
460 static int set_cfm(struct ofport *, const struct cfm_settings *);
461 static void ofport_clear_priorities(struct ofport_dpif *);
463 struct dpif_completion {
464 struct list list_node;
465 struct ofoperation *op;
468 /* Extra information about a classifier table.
469 * Currently used just for optimized flow revalidation. */
471 /* If either of these is nonnull, then this table has a form that allows
472 * flows to be tagged to avoid revalidating most flows for the most common
473 * kinds of flow table changes. */
474 struct cls_table *catchall_table; /* Table that wildcards all fields. */
475 struct cls_table *other_table; /* Table with any other wildcard set. */
476 uint32_t basis; /* Keeps each table's tags separate. */
479 struct ofproto_dpif {
480 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
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. */
512 struct netdev_stats stats; /* To account packets generated and consumed in
517 long long int stp_last_tick;
519 /* VLAN splinters. */
520 struct hmap realdev_vid_map; /* (realdev,vid) -> vlandev. */
521 struct hmap vlandev_map; /* vlandev -> (realdev,vid). */
524 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
525 * for debugging the asynchronous flow_mod implementation.) */
528 /* All existing ofproto_dpif instances, indexed by ->up.name. */
529 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
531 static void ofproto_dpif_unixctl_init(void);
533 static struct ofproto_dpif *
534 ofproto_dpif_cast(const struct ofproto *ofproto)
536 assert(ofproto->ofproto_class == &ofproto_dpif_class);
537 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
540 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
542 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
545 /* Packet processing. */
546 static void update_learning_table(struct ofproto_dpif *,
547 const struct flow *, int vlan,
550 #define FLOW_MISS_MAX_BATCH 50
551 static int handle_upcalls(struct ofproto_dpif *, unsigned int max_batch);
553 /* Flow expiration. */
554 static int expire(struct ofproto_dpif *);
557 static void send_netflow_active_timeouts(struct ofproto_dpif *);
560 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
562 compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions,
563 const struct flow *, uint32_t odp_port);
564 static void add_mirror_actions(struct action_xlate_ctx *ctx,
565 const struct flow *flow);
566 /* Global variables. */
567 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
569 /* Factory functions. */
572 enumerate_types(struct sset *types)
574 dp_enumerate_types(types);
578 enumerate_names(const char *type, struct sset *names)
580 return dp_enumerate_names(type, names);
584 del(const char *type, const char *name)
589 error = dpif_open(name, type, &dpif);
591 error = dpif_delete(dpif);
597 /* Basic life-cycle. */
599 static struct ofproto *
602 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
607 dealloc(struct ofproto *ofproto_)
609 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
614 construct(struct ofproto *ofproto_, int *n_tablesp)
616 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
617 const char *name = ofproto->up.name;
621 error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
623 VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
627 ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
628 ofproto->n_matches = 0;
630 dpif_flow_flush(ofproto->dpif);
631 dpif_recv_purge(ofproto->dpif);
633 error = dpif_recv_set_mask(ofproto->dpif,
634 ((1u << DPIF_UC_MISS) |
635 (1u << DPIF_UC_ACTION)));
637 VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
638 dpif_close(ofproto->dpif);
642 ofproto->netflow = NULL;
643 ofproto->sflow = NULL;
645 hmap_init(&ofproto->bundles);
646 ofproto->ml = mac_learning_create();
647 for (i = 0; i < MAX_MIRRORS; i++) {
648 ofproto->mirrors[i] = NULL;
650 ofproto->has_bonded_bundles = false;
652 timer_set_duration(&ofproto->next_expiration, 1000);
654 hmap_init(&ofproto->facets);
655 hmap_init(&ofproto->subfacets);
657 for (i = 0; i < N_TABLES; i++) {
658 struct table_dpif *table = &ofproto->tables[i];
660 table->catchall_table = NULL;
661 table->other_table = NULL;
662 table->basis = random_uint32();
664 ofproto->need_revalidate = false;
665 tag_set_init(&ofproto->revalidate_set);
667 list_init(&ofproto->completions);
669 ofproto_dpif_unixctl_init();
671 ofproto->has_bundle_action = false;
673 hmap_init(&ofproto->vlandev_map);
674 hmap_init(&ofproto->realdev_vid_map);
676 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
677 hash_string(ofproto->up.name, 0));
679 *n_tablesp = N_TABLES;
680 memset(&ofproto->stats, 0, sizeof ofproto->stats);
685 complete_operations(struct ofproto_dpif *ofproto)
687 struct dpif_completion *c, *next;
689 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
690 ofoperation_complete(c->op, 0);
691 list_remove(&c->list_node);
697 destruct(struct ofproto *ofproto_)
699 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
700 struct rule_dpif *rule, *next_rule;
701 struct classifier *table;
704 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
705 complete_operations(ofproto);
707 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
708 struct cls_cursor cursor;
710 cls_cursor_init(&cursor, table, NULL);
711 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
712 ofproto_rule_destroy(&rule->up);
716 for (i = 0; i < MAX_MIRRORS; i++) {
717 mirror_destroy(ofproto->mirrors[i]);
720 netflow_destroy(ofproto->netflow);
721 dpif_sflow_destroy(ofproto->sflow);
722 hmap_destroy(&ofproto->bundles);
723 mac_learning_destroy(ofproto->ml);
725 hmap_destroy(&ofproto->facets);
726 hmap_destroy(&ofproto->subfacets);
728 hmap_destroy(&ofproto->vlandev_map);
729 hmap_destroy(&ofproto->realdev_vid_map);
731 dpif_close(ofproto->dpif);
735 run_fast(struct ofproto *ofproto_)
737 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
740 /* Handle one or more batches of upcalls, until there's nothing left to do
741 * or until we do a fixed total amount of work.
743 * We do work in batches because it can be much cheaper to set up a number
744 * of flows and fire off their patches all at once. We do multiple batches
745 * because in some cases handling a packet can cause another packet to be
746 * queued almost immediately as part of the return flow. Both
747 * optimizations can make major improvements on some benchmarks and
748 * presumably for real traffic as well. */
750 while (work < FLOW_MISS_MAX_BATCH) {
751 int retval = handle_upcalls(ofproto, FLOW_MISS_MAX_BATCH - work);
761 run(struct ofproto *ofproto_)
763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
764 struct ofport_dpif *ofport;
765 struct ofbundle *bundle;
769 complete_operations(ofproto);
771 dpif_run(ofproto->dpif);
773 error = run_fast(ofproto_);
778 if (timer_expired(&ofproto->next_expiration)) {
779 int delay = expire(ofproto);
780 timer_set_duration(&ofproto->next_expiration, delay);
783 if (ofproto->netflow) {
784 if (netflow_run(ofproto->netflow)) {
785 send_netflow_active_timeouts(ofproto);
788 if (ofproto->sflow) {
789 dpif_sflow_run(ofproto->sflow);
792 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
795 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
800 mac_learning_run(ofproto->ml, &ofproto->revalidate_set);
802 /* Now revalidate if there's anything to do. */
803 if (ofproto->need_revalidate
804 || !tag_set_is_empty(&ofproto->revalidate_set)) {
805 struct tag_set revalidate_set = ofproto->revalidate_set;
806 bool revalidate_all = ofproto->need_revalidate;
807 struct facet *facet, *next;
809 /* Clear the revalidation flags. */
810 tag_set_init(&ofproto->revalidate_set);
811 ofproto->need_revalidate = false;
813 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
815 || tag_set_intersects(&revalidate_set, facet->tags)) {
816 facet_revalidate(ofproto, facet);
825 wait(struct ofproto *ofproto_)
827 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
828 struct ofport_dpif *ofport;
829 struct ofbundle *bundle;
831 if (!clogged && !list_is_empty(&ofproto->completions)) {
832 poll_immediate_wake();
835 dpif_wait(ofproto->dpif);
836 dpif_recv_wait(ofproto->dpif);
837 if (ofproto->sflow) {
838 dpif_sflow_wait(ofproto->sflow);
840 if (!tag_set_is_empty(&ofproto->revalidate_set)) {
841 poll_immediate_wake();
843 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
846 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
849 if (ofproto->netflow) {
850 netflow_wait(ofproto->netflow);
852 mac_learning_wait(ofproto->ml);
854 if (ofproto->need_revalidate) {
855 /* Shouldn't happen, but if it does just go around again. */
856 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
857 poll_immediate_wake();
859 timer_wait(&ofproto->next_expiration);
864 flush(struct ofproto *ofproto_)
866 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
867 struct facet *facet, *next_facet;
869 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
870 /* Mark the facet as not installed so that facet_remove() doesn't
871 * bother trying to uninstall it. There is no point in uninstalling it
872 * individually since we are about to blow away all the facets with
873 * dpif_flow_flush(). */
874 struct subfacet *subfacet;
876 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
877 subfacet->installed = false;
878 subfacet->dp_packet_count = 0;
879 subfacet->dp_byte_count = 0;
881 facet_remove(ofproto, facet);
883 dpif_flow_flush(ofproto->dpif);
887 get_features(struct ofproto *ofproto_ OVS_UNUSED,
888 bool *arp_match_ip, uint32_t *actions)
890 *arp_match_ip = true;
891 *actions = ((1u << OFPAT_OUTPUT) |
892 (1u << OFPAT_SET_VLAN_VID) |
893 (1u << OFPAT_SET_VLAN_PCP) |
894 (1u << OFPAT_STRIP_VLAN) |
895 (1u << OFPAT_SET_DL_SRC) |
896 (1u << OFPAT_SET_DL_DST) |
897 (1u << OFPAT_SET_NW_SRC) |
898 (1u << OFPAT_SET_NW_DST) |
899 (1u << OFPAT_SET_NW_TOS) |
900 (1u << OFPAT_SET_TP_SRC) |
901 (1u << OFPAT_SET_TP_DST) |
902 (1u << OFPAT_ENQUEUE));
906 get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots)
908 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
909 struct dpif_dp_stats s;
911 strcpy(ots->name, "classifier");
913 dpif_get_dp_stats(ofproto->dpif, &s);
914 put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed));
915 put_32aligned_be64(&ots->matched_count,
916 htonll(s.n_hit + ofproto->n_matches));
919 static struct ofport *
922 struct ofport_dpif *port = xmalloc(sizeof *port);
927 port_dealloc(struct ofport *port_)
929 struct ofport_dpif *port = ofport_dpif_cast(port_);
934 port_construct(struct ofport *port_)
936 struct ofport_dpif *port = ofport_dpif_cast(port_);
937 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
939 ofproto->need_revalidate = true;
940 port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
943 port->tag = tag_create_random();
944 port->may_enable = true;
945 port->stp_port = NULL;
946 port->stp_state = STP_DISABLED;
947 hmap_init(&port->priorities);
948 port->realdev_ofp_port = 0;
949 port->vlandev_vid = 0;
951 if (ofproto->sflow) {
952 dpif_sflow_add_port(ofproto->sflow, port_);
959 port_destruct(struct ofport *port_)
961 struct ofport_dpif *port = ofport_dpif_cast(port_);
962 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
964 ofproto->need_revalidate = true;
965 bundle_remove(port_);
966 set_cfm(port_, NULL);
967 if (ofproto->sflow) {
968 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
971 ofport_clear_priorities(port);
972 hmap_destroy(&port->priorities);
976 port_modified(struct ofport *port_)
978 struct ofport_dpif *port = ofport_dpif_cast(port_);
980 if (port->bundle && port->bundle->bond) {
981 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
986 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
988 struct ofport_dpif *port = ofport_dpif_cast(port_);
989 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
990 ovs_be32 changed = old_config ^ port->up.opp.config;
992 if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
993 OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
994 ofproto->need_revalidate = true;
996 if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) {
997 bundle_update(port->bundle);
1003 set_sflow(struct ofproto *ofproto_,
1004 const struct ofproto_sflow_options *sflow_options)
1006 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1007 struct dpif_sflow *ds = ofproto->sflow;
1009 if (sflow_options) {
1011 struct ofport_dpif *ofport;
1013 ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif);
1014 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1015 dpif_sflow_add_port(ds, &ofport->up);
1017 ofproto->need_revalidate = true;
1019 dpif_sflow_set_options(ds, sflow_options);
1022 dpif_sflow_destroy(ds);
1023 ofproto->need_revalidate = true;
1024 ofproto->sflow = NULL;
1031 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1033 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1040 struct ofproto_dpif *ofproto;
1042 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1043 ofproto->need_revalidate = true;
1044 ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev));
1047 if (cfm_configure(ofport->cfm, s)) {
1053 cfm_destroy(ofport->cfm);
1059 get_cfm_fault(const struct ofport *ofport_)
1061 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1063 return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1;
1067 get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps,
1070 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1073 cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps);
1080 /* Spanning Tree. */
1083 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1085 struct ofproto_dpif *ofproto = ofproto_;
1086 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1087 struct ofport_dpif *ofport;
1089 ofport = stp_port_get_aux(sp);
1091 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1092 ofproto->up.name, port_num);
1094 struct eth_header *eth = pkt->l2;
1096 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1097 if (eth_addr_is_zero(eth->eth_src)) {
1098 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1099 "with unknown MAC", ofproto->up.name, port_num);
1101 send_packet(ofport, pkt);
1107 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1109 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1111 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1113 /* Only revalidate flows if the configuration changed. */
1114 if (!s != !ofproto->stp) {
1115 ofproto->need_revalidate = true;
1119 if (!ofproto->stp) {
1120 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1121 send_bpdu_cb, ofproto);
1122 ofproto->stp_last_tick = time_msec();
1125 stp_set_bridge_id(ofproto->stp, s->system_id);
1126 stp_set_bridge_priority(ofproto->stp, s->priority);
1127 stp_set_hello_time(ofproto->stp, s->hello_time);
1128 stp_set_max_age(ofproto->stp, s->max_age);
1129 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1131 stp_destroy(ofproto->stp);
1132 ofproto->stp = NULL;
1139 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1141 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1145 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1146 s->designated_root = stp_get_designated_root(ofproto->stp);
1147 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1156 update_stp_port_state(struct ofport_dpif *ofport)
1158 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1159 enum stp_state state;
1161 /* Figure out new state. */
1162 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1166 if (ofport->stp_state != state) {
1170 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1171 netdev_get_name(ofport->up.netdev),
1172 stp_state_name(ofport->stp_state),
1173 stp_state_name(state));
1174 if (stp_learn_in_state(ofport->stp_state)
1175 != stp_learn_in_state(state)) {
1176 /* xxx Learning action flows should also be flushed. */
1177 mac_learning_flush(ofproto->ml);
1179 fwd_change = stp_forward_in_state(ofport->stp_state)
1180 != stp_forward_in_state(state);
1182 ofproto->need_revalidate = true;
1183 ofport->stp_state = state;
1184 ofport->stp_state_entered = time_msec();
1186 if (fwd_change && ofport->bundle) {
1187 bundle_update(ofport->bundle);
1190 /* Update the STP state bits in the OpenFlow port description. */
1191 of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK))
1192 | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN
1193 : state == STP_LEARNING ? OFPPS_STP_LEARN
1194 : state == STP_FORWARDING ? OFPPS_STP_FORWARD
1195 : state == STP_BLOCKING ? OFPPS_STP_BLOCK
1197 ofproto_port_set_state(&ofport->up, of_state);
1201 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1202 * caller is responsible for assigning STP port numbers and ensuring
1203 * there are no duplicates. */
1205 set_stp_port(struct ofport *ofport_,
1206 const struct ofproto_port_stp_settings *s)
1208 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1209 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1210 struct stp_port *sp = ofport->stp_port;
1212 if (!s || !s->enable) {
1214 ofport->stp_port = NULL;
1215 stp_port_disable(sp);
1216 update_stp_port_state(ofport);
1219 } else if (sp && stp_port_no(sp) != s->port_num
1220 && ofport == stp_port_get_aux(sp)) {
1221 /* The port-id changed, so disable the old one if it's not
1222 * already in use by another port. */
1223 stp_port_disable(sp);
1226 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1227 stp_port_enable(sp);
1229 stp_port_set_aux(sp, ofport);
1230 stp_port_set_priority(sp, s->priority);
1231 stp_port_set_path_cost(sp, s->path_cost);
1233 update_stp_port_state(ofport);
1239 get_stp_port_status(struct ofport *ofport_,
1240 struct ofproto_port_stp_status *s)
1242 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1243 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1244 struct stp_port *sp = ofport->stp_port;
1246 if (!ofproto->stp || !sp) {
1252 s->port_id = stp_port_get_id(sp);
1253 s->state = stp_port_get_state(sp);
1254 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1255 s->role = stp_port_get_role(sp);
1256 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1262 stp_run(struct ofproto_dpif *ofproto)
1265 long long int now = time_msec();
1266 long long int elapsed = now - ofproto->stp_last_tick;
1267 struct stp_port *sp;
1270 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1271 ofproto->stp_last_tick = now;
1273 while (stp_get_changed_port(ofproto->stp, &sp)) {
1274 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1277 update_stp_port_state(ofport);
1284 stp_wait(struct ofproto_dpif *ofproto)
1287 poll_timer_wait(1000);
1291 /* Returns true if STP should process 'flow'. */
1293 stp_should_process_flow(const struct flow *flow)
1295 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1299 stp_process_packet(const struct ofport_dpif *ofport,
1300 const struct ofpbuf *packet)
1302 struct ofpbuf payload = *packet;
1303 struct eth_header *eth = payload.data;
1304 struct stp_port *sp = ofport->stp_port;
1306 /* Sink packets on ports that have STP disabled when the bridge has
1308 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1312 /* Trim off padding on payload. */
1313 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1314 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1317 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1318 stp_received_bpdu(sp, payload.data, payload.size);
1322 static struct priority_to_dscp *
1323 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1325 struct priority_to_dscp *pdscp;
1328 hash = hash_int(priority, 0);
1329 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1330 if (pdscp->priority == priority) {
1338 ofport_clear_priorities(struct ofport_dpif *ofport)
1340 struct priority_to_dscp *pdscp, *next;
1342 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
1343 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1349 set_queues(struct ofport *ofport_,
1350 const struct ofproto_port_queue *qdscp_list,
1353 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1354 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1355 struct hmap new = HMAP_INITIALIZER(&new);
1358 for (i = 0; i < n_qdscp; i++) {
1359 struct priority_to_dscp *pdscp;
1363 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1364 if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue,
1369 pdscp = get_priority(ofport, priority);
1371 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
1373 pdscp = xmalloc(sizeof *pdscp);
1374 pdscp->priority = priority;
1376 ofproto->need_revalidate = true;
1379 if (pdscp->dscp != dscp) {
1381 ofproto->need_revalidate = true;
1384 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
1387 if (!hmap_is_empty(&ofport->priorities)) {
1388 ofport_clear_priorities(ofport);
1389 ofproto->need_revalidate = true;
1392 hmap_swap(&new, &ofport->priorities);
1400 /* Expires all MAC learning entries associated with 'bundle' and forces its
1401 * ofproto to revalidate every flow.
1403 * Normally MAC learning entries are removed only from the ofproto associated
1404 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1405 * are removed from every ofproto. When patch ports and SLB bonds are in use
1406 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1407 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1408 * with the host from which it migrated. */
1410 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
1412 struct ofproto_dpif *ofproto = bundle->ofproto;
1413 struct mac_learning *ml = ofproto->ml;
1414 struct mac_entry *mac, *next_mac;
1416 ofproto->need_revalidate = true;
1417 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1418 if (mac->port.p == bundle) {
1420 struct ofproto_dpif *o;
1422 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1424 struct mac_entry *e;
1426 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan,
1429 tag_set_add(&o->revalidate_set, e->tag);
1430 mac_learning_expire(o->ml, e);
1436 mac_learning_expire(ml, mac);
1441 static struct ofbundle *
1442 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1444 struct ofbundle *bundle;
1446 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1447 &ofproto->bundles) {
1448 if (bundle->aux == aux) {
1455 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
1456 * ones that are found to 'bundles'. */
1458 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
1459 void **auxes, size_t n_auxes,
1460 struct hmapx *bundles)
1464 hmapx_init(bundles);
1465 for (i = 0; i < n_auxes; i++) {
1466 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
1468 hmapx_add(bundles, bundle);
1474 bundle_update(struct ofbundle *bundle)
1476 struct ofport_dpif *port;
1478 bundle->floodable = true;
1479 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1480 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1481 bundle->floodable = false;
1488 bundle_del_port(struct ofport_dpif *port)
1490 struct ofbundle *bundle = port->bundle;
1492 bundle->ofproto->need_revalidate = true;
1494 list_remove(&port->bundle_node);
1495 port->bundle = NULL;
1498 lacp_slave_unregister(bundle->lacp, port);
1501 bond_slave_unregister(bundle->bond, port);
1504 bundle_update(bundle);
1508 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
1509 struct lacp_slave_settings *lacp,
1510 uint32_t bond_stable_id)
1512 struct ofport_dpif *port;
1514 port = get_ofp_port(bundle->ofproto, ofp_port);
1519 if (port->bundle != bundle) {
1520 bundle->ofproto->need_revalidate = true;
1522 bundle_del_port(port);
1525 port->bundle = bundle;
1526 list_push_back(&bundle->ports, &port->bundle_node);
1527 if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
1528 bundle->floodable = false;
1532 port->bundle->ofproto->need_revalidate = true;
1533 lacp_slave_register(bundle->lacp, port, lacp);
1536 port->bond_stable_id = bond_stable_id;
1542 bundle_destroy(struct ofbundle *bundle)
1544 struct ofproto_dpif *ofproto;
1545 struct ofport_dpif *port, *next_port;
1552 ofproto = bundle->ofproto;
1553 for (i = 0; i < MAX_MIRRORS; i++) {
1554 struct ofmirror *m = ofproto->mirrors[i];
1556 if (m->out == bundle) {
1558 } else if (hmapx_find_and_delete(&m->srcs, bundle)
1559 || hmapx_find_and_delete(&m->dsts, bundle)) {
1560 ofproto->need_revalidate = true;
1565 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1566 bundle_del_port(port);
1569 bundle_flush_macs(bundle, true);
1570 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
1572 free(bundle->trunks);
1573 lacp_destroy(bundle->lacp);
1574 bond_destroy(bundle->bond);
1579 bundle_set(struct ofproto *ofproto_, void *aux,
1580 const struct ofproto_bundle_settings *s)
1582 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1583 bool need_flush = false;
1584 struct ofport_dpif *port;
1585 struct ofbundle *bundle;
1586 unsigned long *trunks;
1592 bundle_destroy(bundle_lookup(ofproto, aux));
1596 assert(s->n_slaves == 1 || s->bond != NULL);
1597 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
1599 bundle = bundle_lookup(ofproto, aux);
1601 bundle = xmalloc(sizeof *bundle);
1603 bundle->ofproto = ofproto;
1604 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
1605 hash_pointer(aux, 0));
1607 bundle->name = NULL;
1609 list_init(&bundle->ports);
1610 bundle->vlan_mode = PORT_VLAN_TRUNK;
1612 bundle->trunks = NULL;
1613 bundle->use_priority_tags = s->use_priority_tags;
1614 bundle->lacp = NULL;
1615 bundle->bond = NULL;
1617 bundle->floodable = true;
1619 bundle->src_mirrors = 0;
1620 bundle->dst_mirrors = 0;
1621 bundle->mirror_out = 0;
1624 if (!bundle->name || strcmp(s->name, bundle->name)) {
1626 bundle->name = xstrdup(s->name);
1631 if (!bundle->lacp) {
1632 ofproto->need_revalidate = true;
1633 bundle->lacp = lacp_create();
1635 lacp_configure(bundle->lacp, s->lacp);
1637 lacp_destroy(bundle->lacp);
1638 bundle->lacp = NULL;
1641 /* Update set of ports. */
1643 for (i = 0; i < s->n_slaves; i++) {
1644 if (!bundle_add_port(bundle, s->slaves[i],
1645 s->lacp ? &s->lacp_slaves[i] : NULL,
1646 s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) {
1650 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
1651 struct ofport_dpif *next_port;
1653 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
1654 for (i = 0; i < s->n_slaves; i++) {
1655 if (s->slaves[i] == port->up.ofp_port) {
1660 bundle_del_port(port);
1664 assert(list_size(&bundle->ports) <= s->n_slaves);
1666 if (list_is_empty(&bundle->ports)) {
1667 bundle_destroy(bundle);
1671 /* Set VLAN tagging mode */
1672 if (s->vlan_mode != bundle->vlan_mode
1673 || s->use_priority_tags != bundle->use_priority_tags) {
1674 bundle->vlan_mode = s->vlan_mode;
1675 bundle->use_priority_tags = s->use_priority_tags;
1680 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
1681 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
1683 if (vlan != bundle->vlan) {
1684 bundle->vlan = vlan;
1688 /* Get trunked VLANs. */
1689 switch (s->vlan_mode) {
1690 case PORT_VLAN_ACCESS:
1694 case PORT_VLAN_TRUNK:
1695 trunks = (unsigned long *) s->trunks;
1698 case PORT_VLAN_NATIVE_UNTAGGED:
1699 case PORT_VLAN_NATIVE_TAGGED:
1700 if (vlan != 0 && (!s->trunks
1701 || !bitmap_is_set(s->trunks, vlan)
1702 || bitmap_is_set(s->trunks, 0))) {
1703 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
1705 trunks = bitmap_clone(s->trunks, 4096);
1707 trunks = bitmap_allocate1(4096);
1709 bitmap_set1(trunks, vlan);
1710 bitmap_set0(trunks, 0);
1712 trunks = (unsigned long *) s->trunks;
1719 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
1720 free(bundle->trunks);
1721 if (trunks == s->trunks) {
1722 bundle->trunks = vlan_bitmap_clone(trunks);
1724 bundle->trunks = trunks;
1729 if (trunks != s->trunks) {
1734 if (!list_is_short(&bundle->ports)) {
1735 bundle->ofproto->has_bonded_bundles = true;
1737 if (bond_reconfigure(bundle->bond, s->bond)) {
1738 ofproto->need_revalidate = true;
1741 bundle->bond = bond_create(s->bond);
1742 ofproto->need_revalidate = true;
1745 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1746 bond_slave_register(bundle->bond, port, port->bond_stable_id,
1750 bond_destroy(bundle->bond);
1751 bundle->bond = NULL;
1754 /* If we changed something that would affect MAC learning, un-learn
1755 * everything on this port and force flow revalidation. */
1757 bundle_flush_macs(bundle, false);
1764 bundle_remove(struct ofport *port_)
1766 struct ofport_dpif *port = ofport_dpif_cast(port_);
1767 struct ofbundle *bundle = port->bundle;
1770 bundle_del_port(port);
1771 if (list_is_empty(&bundle->ports)) {
1772 bundle_destroy(bundle);
1773 } else if (list_is_short(&bundle->ports)) {
1774 bond_destroy(bundle->bond);
1775 bundle->bond = NULL;
1781 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
1783 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1784 struct ofport_dpif *port = port_;
1785 uint8_t ea[ETH_ADDR_LEN];
1788 error = netdev_get_etheraddr(port->up.netdev, ea);
1790 struct ofpbuf packet;
1793 ofpbuf_init(&packet, 0);
1794 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1796 memcpy(packet_pdu, pdu, pdu_size);
1798 send_packet(port, &packet);
1799 ofpbuf_uninit(&packet);
1801 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1802 "%s (%s)", port->bundle->name,
1803 netdev_get_name(port->up.netdev), strerror(error));
1808 bundle_send_learning_packets(struct ofbundle *bundle)
1810 struct ofproto_dpif *ofproto = bundle->ofproto;
1811 int error, n_packets, n_errors;
1812 struct mac_entry *e;
1814 error = n_packets = n_errors = 0;
1815 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1816 if (e->port.p != bundle) {
1817 struct ofpbuf *learning_packet;
1818 struct ofport_dpif *port;
1821 learning_packet = bond_compose_learning_packet(bundle->bond, e->mac,
1824 ret = send_packet(port, learning_packet);
1825 ofpbuf_delete(learning_packet);
1835 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1836 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1837 "packets, last error was: %s",
1838 bundle->name, n_errors, n_packets, strerror(error));
1840 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1841 bundle->name, n_packets);
1846 bundle_run(struct ofbundle *bundle)
1849 lacp_run(bundle->lacp, send_pdu_cb);
1852 struct ofport_dpif *port;
1854 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1855 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
1858 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1859 lacp_negotiated(bundle->lacp));
1860 if (bond_should_send_learning_packets(bundle->bond)) {
1861 bundle_send_learning_packets(bundle);
1867 bundle_wait(struct ofbundle *bundle)
1870 lacp_wait(bundle->lacp);
1873 bond_wait(bundle->bond);
1880 mirror_scan(struct ofproto_dpif *ofproto)
1884 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1885 if (!ofproto->mirrors[idx]) {
1892 static struct ofmirror *
1893 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1897 for (i = 0; i < MAX_MIRRORS; i++) {
1898 struct ofmirror *mirror = ofproto->mirrors[i];
1899 if (mirror && mirror->aux == aux) {
1907 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
1909 mirror_update_dups(struct ofproto_dpif *ofproto)
1913 for (i = 0; i < MAX_MIRRORS; i++) {
1914 struct ofmirror *m = ofproto->mirrors[i];
1917 m->dup_mirrors = MIRROR_MASK_C(1) << i;
1921 for (i = 0; i < MAX_MIRRORS; i++) {
1922 struct ofmirror *m1 = ofproto->mirrors[i];
1929 for (j = i + 1; j < MAX_MIRRORS; j++) {
1930 struct ofmirror *m2 = ofproto->mirrors[j];
1932 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
1933 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
1934 m2->dup_mirrors |= m1->dup_mirrors;
1941 mirror_set(struct ofproto *ofproto_, void *aux,
1942 const struct ofproto_mirror_settings *s)
1944 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1945 mirror_mask_t mirror_bit;
1946 struct ofbundle *bundle;
1947 struct ofmirror *mirror;
1948 struct ofbundle *out;
1949 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1950 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1953 mirror = mirror_lookup(ofproto, aux);
1955 mirror_destroy(mirror);
1961 idx = mirror_scan(ofproto);
1963 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1965 ofproto->up.name, MAX_MIRRORS, s->name);
1969 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1970 mirror->ofproto = ofproto;
1973 mirror->out_vlan = -1;
1974 mirror->name = NULL;
1977 if (!mirror->name || strcmp(s->name, mirror->name)) {
1979 mirror->name = xstrdup(s->name);
1982 /* Get the new configuration. */
1983 if (s->out_bundle) {
1984 out = bundle_lookup(ofproto, s->out_bundle);
1986 mirror_destroy(mirror);
1992 out_vlan = s->out_vlan;
1994 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1995 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1997 /* If the configuration has not changed, do nothing. */
1998 if (hmapx_equals(&srcs, &mirror->srcs)
1999 && hmapx_equals(&dsts, &mirror->dsts)
2000 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
2001 && mirror->out == out
2002 && mirror->out_vlan == out_vlan)
2004 hmapx_destroy(&srcs);
2005 hmapx_destroy(&dsts);
2009 hmapx_swap(&srcs, &mirror->srcs);
2010 hmapx_destroy(&srcs);
2012 hmapx_swap(&dsts, &mirror->dsts);
2013 hmapx_destroy(&dsts);
2015 free(mirror->vlans);
2016 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
2019 mirror->out_vlan = out_vlan;
2021 /* Update bundles. */
2022 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2023 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
2024 if (hmapx_contains(&mirror->srcs, bundle)) {
2025 bundle->src_mirrors |= mirror_bit;
2027 bundle->src_mirrors &= ~mirror_bit;
2030 if (hmapx_contains(&mirror->dsts, bundle)) {
2031 bundle->dst_mirrors |= mirror_bit;
2033 bundle->dst_mirrors &= ~mirror_bit;
2036 if (mirror->out == bundle) {
2037 bundle->mirror_out |= mirror_bit;
2039 bundle->mirror_out &= ~mirror_bit;
2043 ofproto->need_revalidate = true;
2044 mac_learning_flush(ofproto->ml);
2045 mirror_update_dups(ofproto);
2051 mirror_destroy(struct ofmirror *mirror)
2053 struct ofproto_dpif *ofproto;
2054 mirror_mask_t mirror_bit;
2055 struct ofbundle *bundle;
2061 ofproto = mirror->ofproto;
2062 ofproto->need_revalidate = true;
2063 mac_learning_flush(ofproto->ml);
2065 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2066 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2067 bundle->src_mirrors &= ~mirror_bit;
2068 bundle->dst_mirrors &= ~mirror_bit;
2069 bundle->mirror_out &= ~mirror_bit;
2072 hmapx_destroy(&mirror->srcs);
2073 hmapx_destroy(&mirror->dsts);
2074 free(mirror->vlans);
2076 ofproto->mirrors[mirror->idx] = NULL;
2080 mirror_update_dups(ofproto);
2084 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2085 uint64_t *packets, uint64_t *bytes)
2087 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2088 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2091 *packets = *bytes = UINT64_MAX;
2095 *packets = mirror->packet_count;
2096 *bytes = mirror->byte_count;
2102 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2104 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2105 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2106 ofproto->need_revalidate = true;
2107 mac_learning_flush(ofproto->ml);
2113 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2115 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2116 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2117 return bundle && bundle->mirror_out != 0;
2121 forward_bpdu_changed(struct ofproto *ofproto_)
2123 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2124 /* Revalidate cached flows whenever forward_bpdu option changes. */
2125 ofproto->need_revalidate = true;
2130 static struct ofport_dpif *
2131 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
2133 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2134 return ofport ? ofport_dpif_cast(ofport) : NULL;
2137 static struct ofport_dpif *
2138 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
2140 return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
2144 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
2145 struct dpif_port *dpif_port)
2147 ofproto_port->name = dpif_port->name;
2148 ofproto_port->type = dpif_port->type;
2149 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
2153 port_run(struct ofport_dpif *ofport)
2155 bool enable = netdev_get_carrier(ofport->up.netdev);
2158 cfm_run(ofport->cfm);
2160 if (cfm_should_send_ccm(ofport->cfm)) {
2161 struct ofpbuf packet;
2163 ofpbuf_init(&packet, 0);
2164 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr);
2165 send_packet(ofport, &packet);
2166 ofpbuf_uninit(&packet);
2169 enable = enable && !cfm_get_fault(ofport->cfm)
2170 && cfm_get_opup(ofport->cfm);
2173 if (ofport->bundle) {
2174 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2177 if (ofport->may_enable != enable) {
2178 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2180 if (ofproto->has_bundle_action) {
2181 ofproto->need_revalidate = true;
2185 ofport->may_enable = enable;
2189 port_wait(struct ofport_dpif *ofport)
2192 cfm_wait(ofport->cfm);
2197 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2198 struct ofproto_port *ofproto_port)
2200 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2201 struct dpif_port dpif_port;
2204 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
2206 ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
2212 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
2214 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2218 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
2220 *ofp_portp = odp_port_to_ofp_port(odp_port);
2226 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
2228 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2231 error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
2233 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2235 /* The caller is going to close ofport->up.netdev. If this is a
2236 * bonded port, then the bond is using that netdev, so remove it
2237 * from the bond. The client will need to reconfigure everything
2238 * after deleting ports, so then the slave will get re-added. */
2239 bundle_remove(&ofport->up);
2246 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2248 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2251 error = netdev_get_stats(ofport->up.netdev, stats);
2253 if (!error && ofport->odp_port == OVSP_LOCAL) {
2254 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2256 /* ofproto->stats.tx_packets represents packets that we created
2257 * internally and sent to some port (e.g. packets sent with
2258 * send_packet()). Account for them as if they had come from
2259 * OFPP_LOCAL and got forwarded. */
2261 if (stats->rx_packets != UINT64_MAX) {
2262 stats->rx_packets += ofproto->stats.tx_packets;
2265 if (stats->rx_bytes != UINT64_MAX) {
2266 stats->rx_bytes += ofproto->stats.tx_bytes;
2269 /* ofproto->stats.rx_packets represents packets that were received on
2270 * some port and we processed internally and dropped (e.g. STP).
2271 * Account fro them as if they had been forwarded to OFPP_LOCAL. */
2273 if (stats->tx_packets != UINT64_MAX) {
2274 stats->tx_packets += ofproto->stats.rx_packets;
2277 if (stats->tx_bytes != UINT64_MAX) {
2278 stats->tx_bytes += ofproto->stats.rx_bytes;
2285 /* Account packets for LOCAL port. */
2287 ofproto_update_local_port_stats(const struct ofproto *ofproto_,
2288 size_t tx_size, size_t rx_size)
2290 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2293 ofproto->stats.rx_packets++;
2294 ofproto->stats.rx_bytes += rx_size;
2297 ofproto->stats.tx_packets++;
2298 ofproto->stats.tx_bytes += tx_size;
2302 struct port_dump_state {
2303 struct dpif_port_dump dump;
2308 port_dump_start(const struct ofproto *ofproto_, void **statep)
2310 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2311 struct port_dump_state *state;
2313 *statep = state = xmalloc(sizeof *state);
2314 dpif_port_dump_start(&state->dump, ofproto->dpif);
2315 state->done = false;
2320 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
2321 struct ofproto_port *port)
2323 struct port_dump_state *state = state_;
2324 struct dpif_port dpif_port;
2326 if (dpif_port_dump_next(&state->dump, &dpif_port)) {
2327 ofproto_port_from_dpif_port(port, &dpif_port);
2330 int error = dpif_port_dump_done(&state->dump);
2332 return error ? error : EOF;
2337 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2339 struct port_dump_state *state = state_;
2342 dpif_port_dump_done(&state->dump);
2349 port_poll(const struct ofproto *ofproto_, char **devnamep)
2351 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2352 return dpif_port_poll(ofproto->dpif, devnamep);
2356 port_poll_wait(const struct ofproto *ofproto_)
2358 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2359 dpif_port_poll_wait(ofproto->dpif);
2363 port_is_lacp_current(const struct ofport *ofport_)
2365 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2366 return (ofport->bundle && ofport->bundle->lacp
2367 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2371 /* Upcall handling. */
2373 /* Flow miss batching.
2375 * Some dpifs implement operations faster when you hand them off in a batch.
2376 * To allow batching, "struct flow_miss" queues the dpif-related work needed
2377 * for a given flow. Each "struct flow_miss" corresponds to sending one or
2378 * more packets, plus possibly installing the flow in the dpif.
2380 * So far we only batch the operations that affect flow setup time the most.
2381 * It's possible to batch more than that, but the benefit might be minimal. */
2383 struct hmap_node hmap_node;
2385 enum odp_key_fitness key_fitness;
2386 const struct nlattr *key;
2388 ovs_be16 initial_tci;
2389 struct list packets;
2392 struct flow_miss_op {
2393 union dpif_op dpif_op;
2394 struct subfacet *subfacet;
2397 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
2398 * OpenFlow controller as necessary according to their individual
2401 * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise,
2402 * ownership is transferred to this function. */
2404 send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2405 const struct flow *flow, bool clone)
2407 struct ofputil_packet_in pin;
2409 pin.packet = packet;
2410 pin.in_port = flow->in_port;
2411 pin.reason = OFPR_NO_MATCH;
2412 pin.buffer_id = 0; /* not yet known */
2413 pin.send_len = 0; /* not used for flow table misses */
2414 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2415 clone ? NULL : packet);
2418 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each
2419 * OpenFlow controller as necessary according to their individual
2422 * 'send_len' should be the number of bytes of 'packet' to send to the
2423 * controller, as specified in the action that caused the packet to be sent.
2425 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
2426 * Otherwise, ownership is transferred to this function. */
2428 send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet,
2429 uint64_t userdata, const struct flow *flow, bool clone)
2431 struct ofputil_packet_in pin;
2432 struct user_action_cookie cookie;
2434 memcpy(&cookie, &userdata, sizeof(cookie));
2436 pin.packet = packet;
2437 pin.in_port = flow->in_port;
2438 pin.reason = OFPR_ACTION;
2439 pin.buffer_id = 0; /* not yet known */
2440 pin.send_len = cookie.data;
2441 connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
2442 clone ? NULL : packet);
2446 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
2447 const struct ofpbuf *packet)
2449 struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
2455 if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) {
2457 cfm_process_heartbeat(ofport->cfm, packet);
2460 } else if (ofport->bundle && ofport->bundle->lacp
2461 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2463 lacp_process_packet(ofport->bundle->lacp, ofport, packet);
2466 } else if (ofproto->stp && stp_should_process_flow(flow)) {
2468 stp_process_packet(ofport, packet);
2475 static struct flow_miss *
2476 flow_miss_create(struct hmap *todo, const struct flow *flow,
2477 enum odp_key_fitness key_fitness,
2478 const struct nlattr *key, size_t key_len,
2479 ovs_be16 initial_tci)
2481 uint32_t hash = flow_hash(flow, 0);
2482 struct flow_miss *miss;
2484 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
2485 if (flow_equal(&miss->flow, flow)) {
2490 miss = xmalloc(sizeof *miss);
2491 hmap_insert(todo, &miss->hmap_node, hash);
2493 miss->key_fitness = key_fitness;
2495 miss->key_len = key_len;
2496 miss->initial_tci = initial_tci;
2497 list_init(&miss->packets);
2502 handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss,
2503 struct flow_miss_op *ops, size_t *n_ops)
2505 const struct flow *flow = &miss->flow;
2506 struct ofpbuf *packet, *next_packet;
2507 struct subfacet *subfacet;
2508 struct facet *facet;
2510 facet = facet_lookup_valid(ofproto, flow);
2512 struct rule_dpif *rule;
2514 rule = rule_dpif_lookup(ofproto, flow, 0);
2516 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
2517 struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
2519 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
2520 COVERAGE_INC(ofproto_dpif_no_packet_in);
2521 /* XXX install 'drop' flow entry */
2525 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
2529 LIST_FOR_EACH_SAFE (packet, next_packet, list_node,
2531 list_remove(&packet->list_node);
2532 send_packet_in_miss(ofproto, packet, flow, false);
2538 facet = facet_create(rule, flow);
2541 subfacet = subfacet_create(ofproto, facet,
2542 miss->key_fitness, miss->key, miss->key_len,
2545 LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
2546 struct dpif_flow_stats stats;
2548 list_remove(&packet->list_node);
2549 ofproto->n_matches++;
2551 if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
2553 * Extra-special case for fail-open mode.
2555 * We are in fail-open mode and the packet matched the fail-open
2556 * rule, but we are connected to a controller too. We should send
2557 * the packet up to the controller in the hope that it will try to
2558 * set up a flow and thereby allow us to exit fail-open.
2560 * See the top-level comment in fail-open.c for more information.
2562 send_packet_in_miss(ofproto, packet, flow, true);
2565 if (!facet->may_install || !subfacet->actions) {
2566 subfacet_make_actions(ofproto, subfacet, packet);
2569 /* Credit statistics to subfacet for this packet. We must do this now
2570 * because execute_controller_action() below may destroy 'packet'. */
2571 dpif_flow_stats_extract(&facet->flow, packet, &stats);
2572 subfacet_update_stats(ofproto, subfacet, &stats);
2574 if (!execute_controller_action(ofproto, &facet->flow,
2576 subfacet->actions_len, packet, true)
2577 && subfacet->actions_len > 0) {
2578 struct flow_miss_op *op = &ops[(*n_ops)++];
2579 struct dpif_execute *execute = &op->dpif_op.execute;
2581 if (flow->vlan_tci != subfacet->initial_tci) {
2582 /* This packet was received on a VLAN splinter port. We added
2583 * a VLAN to the packet to make the packet resemble the flow,
2584 * but the actions were composed assuming that the packet
2585 * contained no VLAN. So, we must remove the VLAN header from
2586 * the packet before trying to execute the actions. */
2587 eth_pop_vlan(packet);
2590 op->subfacet = subfacet;
2591 execute->type = DPIF_OP_EXECUTE;
2592 execute->key = miss->key;
2593 execute->key_len = miss->key_len;
2595 = (facet->may_install
2597 : xmemdup(subfacet->actions, subfacet->actions_len));
2598 execute->actions_len = subfacet->actions_len;
2599 execute->packet = packet;
2603 if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
2604 struct flow_miss_op *op = &ops[(*n_ops)++];
2605 struct dpif_flow_put *put = &op->dpif_op.flow_put;
2607 op->subfacet = subfacet;
2608 put->type = DPIF_OP_FLOW_PUT;
2609 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2610 put->key = miss->key;
2611 put->key_len = miss->key_len;
2612 put->actions = subfacet->actions;
2613 put->actions_len = subfacet->actions_len;
2618 /* Like odp_flow_key_to_flow(), this function converts the 'key_len' bytes of
2619 * OVS_KEY_ATTR_* attributes in 'key' to a flow structure in 'flow' and returns
2620 * an ODP_FIT_* value that indicates how well 'key' fits our expectations for
2621 * what a flow key should contain.
2623 * This function also includes some logic to help make VLAN splinters
2624 * transparent to the rest of the upcall processing logic. In particular, if
2625 * the extracted in_port is a VLAN splinter port, it replaces flow->in_port by
2626 * the "real" port, sets flow->vlan_tci correctly for the VLAN of the VLAN
2627 * splinter port, and pushes a VLAN header onto 'packet' (if it is nonnull).
2629 * Sets '*initial_tci' to the VLAN TCI with which the packet was really
2630 * received, that is, the actual VLAN TCI extracted by odp_flow_key_to_flow().
2631 * (This differs from the value returned in flow->vlan_tci only for packets
2632 * received on VLAN splinters.)
2634 static enum odp_key_fitness
2635 ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
2636 const struct nlattr *key, size_t key_len,
2637 struct flow *flow, ovs_be16 *initial_tci,
2638 struct ofpbuf *packet)
2640 enum odp_key_fitness fitness;
2644 fitness = odp_flow_key_to_flow(key, key_len, flow);
2645 if (fitness == ODP_FIT_ERROR) {
2648 *initial_tci = flow->vlan_tci;
2650 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
2652 /* Cause the flow to be processed as if it came in on the real device
2653 * with the VLAN device's VLAN ID. */
2654 flow->in_port = realdev;
2655 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
2657 /* Make the packet resemble the flow, so that it gets sent to an
2658 * OpenFlow controller properly, so that it looks correct for
2659 * sFlow, and so that flow_extract() will get the correct vlan_tci
2660 * if it is called on 'packet'.
2662 * The allocated space inside 'packet' probably also contains
2663 * 'key', that is, both 'packet' and 'key' are probably part of a
2664 * struct dpif_upcall (see the large comment on that structure
2665 * definition), so pushing data on 'packet' is in general not a
2666 * good idea since it could overwrite 'key' or free it as a side
2667 * effect. However, it's OK in this special case because we know
2668 * that 'packet' is inside a Netlink attribute: pushing 4 bytes
2669 * will just overwrite the 4-byte "struct nlattr", which is fine
2670 * since we don't need that header anymore. */
2671 eth_push_vlan(packet, flow->vlan_tci);
2674 /* Let the caller know that we can't reproduce 'key' from 'flow'. */
2675 if (fitness == ODP_FIT_PERFECT) {
2676 fitness = ODP_FIT_TOO_MUCH;
2684 handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
2687 struct dpif_upcall *upcall;
2688 struct flow_miss *miss, *next_miss;
2689 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
2690 union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
2699 /* Construct the to-do list.
2701 * This just amounts to extracting the flow from each packet and sticking
2702 * the packets that have the same flow in the same "flow_miss" structure so
2703 * that we can process them together. */
2705 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
2706 enum odp_key_fitness fitness;
2707 struct flow_miss *miss;
2708 ovs_be16 initial_tci;
2711 /* Obtain metadata and check userspace/kernel agreement on flow match,
2712 * then set 'flow''s header pointers. */
2713 fitness = ofproto_dpif_extract_flow_key(ofproto,
2714 upcall->key, upcall->key_len,
2715 &flow, &initial_tci,
2717 if (fitness == ODP_FIT_ERROR) {
2718 ofpbuf_delete(upcall->packet);
2721 flow_extract(upcall->packet, flow.skb_priority, flow.tun_id,
2722 flow.in_port, &flow);
2724 /* Handle 802.1ag, LACP, and STP specially. */
2725 if (process_special(ofproto, &flow, upcall->packet)) {
2726 ofproto_update_local_port_stats(&ofproto->up,
2727 0, upcall->packet->size);
2728 ofpbuf_delete(upcall->packet);
2729 ofproto->n_matches++;
2733 /* Add other packets to a to-do list. */
2734 miss = flow_miss_create(&todo, &flow, fitness,
2735 upcall->key, upcall->key_len, initial_tci);
2736 list_push_back(&miss->packets, &upcall->packet->list_node);
2739 /* Process each element in the to-do list, constructing the set of
2740 * operations to batch. */
2742 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) {
2743 handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);
2744 ofpbuf_list_delete(&miss->packets);
2745 hmap_remove(&todo, &miss->hmap_node);
2748 assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
2749 hmap_destroy(&todo);
2751 /* Execute batch. */
2752 for (i = 0; i < n_ops; i++) {
2753 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
2755 dpif_operate(ofproto->dpif, dpif_ops, n_ops);
2757 /* Free memory and update facets. */
2758 for (i = 0; i < n_ops; i++) {
2759 struct flow_miss_op *op = &flow_miss_ops[i];
2760 struct dpif_execute *execute;
2761 struct dpif_flow_put *put;
2763 switch (op->dpif_op.type) {
2764 case DPIF_OP_EXECUTE:
2765 execute = &op->dpif_op.execute;
2766 if (op->subfacet->actions != execute->actions) {
2767 free((struct nlattr *) execute->actions);
2769 ofpbuf_delete((struct ofpbuf *) execute->packet);
2772 case DPIF_OP_FLOW_PUT:
2773 put = &op->dpif_op.flow_put;
2775 op->subfacet->installed = true;
2783 handle_userspace_upcall(struct ofproto_dpif *ofproto,
2784 struct dpif_upcall *upcall)
2786 struct user_action_cookie cookie;
2787 enum odp_key_fitness fitness;
2788 ovs_be16 initial_tci;
2791 memcpy(&cookie, &upcall->userdata, sizeof(cookie));
2793 fitness = ofproto_dpif_extract_flow_key(ofproto, upcall->key,
2794 upcall->key_len, &flow,
2795 &initial_tci, upcall->packet);
2796 if (fitness == ODP_FIT_ERROR) {
2797 ofpbuf_delete(upcall->packet);
2801 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
2802 if (ofproto->sflow) {
2803 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
2806 ofpbuf_delete(upcall->packet);
2807 } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
2808 COVERAGE_INC(ofproto_dpif_ctlr_action);
2809 send_packet_in_action(ofproto, upcall->packet, upcall->userdata,
2812 VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata);
2813 ofpbuf_delete(upcall->packet);
2818 handle_upcalls(struct ofproto_dpif *ofproto, unsigned int max_batch)
2820 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
2824 assert (max_batch <= FLOW_MISS_MAX_BATCH);
2827 for (i = 0; i < max_batch; i++) {
2828 struct dpif_upcall *upcall = &misses[n_misses];
2831 error = dpif_recv(ofproto->dpif, upcall);
2836 switch (upcall->type) {
2837 case DPIF_UC_ACTION:
2838 handle_userspace_upcall(ofproto, upcall);
2842 /* Handle it later. */
2846 case DPIF_N_UC_TYPES:
2848 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
2854 handle_miss_upcalls(ofproto, misses, n_misses);
2859 /* Flow expiration. */
2861 static int subfacet_max_idle(const struct ofproto_dpif *);
2862 static void update_stats(struct ofproto_dpif *);
2863 static void rule_expire(struct rule_dpif *);
2864 static void expire_subfacets(struct ofproto_dpif *, int dp_max_idle);
2866 /* This function is called periodically by run(). Its job is to collect
2867 * updates for the flows that have been installed into the datapath, most
2868 * importantly when they last were used, and then use that information to
2869 * expire flows that have not been used recently.
2871 * Returns the number of milliseconds after which it should be called again. */
2873 expire(struct ofproto_dpif *ofproto)
2875 struct rule_dpif *rule, *next_rule;
2876 struct classifier *table;
2879 /* Update stats for each flow in the datapath. */
2880 update_stats(ofproto);
2882 /* Expire subfacets that have been idle too long. */
2883 dp_max_idle = subfacet_max_idle(ofproto);
2884 expire_subfacets(ofproto, dp_max_idle);
2886 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
2887 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
2888 struct cls_cursor cursor;
2890 cls_cursor_init(&cursor, table, NULL);
2891 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
2896 /* All outstanding data in existing flows has been accounted, so it's a
2897 * good time to do bond rebalancing. */
2898 if (ofproto->has_bonded_bundles) {
2899 struct ofbundle *bundle;
2901 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2903 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
2908 return MIN(dp_max_idle, 1000);
2911 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
2913 * This function also pushes statistics updates to rules which each facet
2914 * resubmits into. Generally these statistics will be accurate. However, if a
2915 * facet changes the rule it resubmits into at some time in between
2916 * update_stats() runs, it is possible that statistics accrued to the
2917 * old rule will be incorrectly attributed to the new rule. This could be
2918 * avoided by calling update_stats() whenever rules are created or
2919 * deleted. However, the performance impact of making so many calls to the
2920 * datapath do not justify the benefit of having perfectly accurate statistics.
2923 update_stats(struct ofproto_dpif *p)
2925 const struct dpif_flow_stats *stats;
2926 struct dpif_flow_dump dump;
2927 const struct nlattr *key;
2930 dpif_flow_dump_start(&dump, p->dpif);
2931 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
2932 struct subfacet *subfacet;
2934 subfacet = subfacet_find(p, key, key_len);
2935 if (subfacet && subfacet->installed) {
2936 struct facet *facet = subfacet->facet;
2938 if (stats->n_packets >= subfacet->dp_packet_count) {
2939 uint64_t extra = stats->n_packets - subfacet->dp_packet_count;
2940 facet->packet_count += extra;
2942 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
2945 if (stats->n_bytes >= subfacet->dp_byte_count) {
2946 facet->byte_count += stats->n_bytes - subfacet->dp_byte_count;
2948 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
2951 subfacet->dp_packet_count = stats->n_packets;
2952 subfacet->dp_byte_count = stats->n_bytes;
2954 subfacet_update_time(p, subfacet, stats->used);
2955 facet_account(p, facet);
2956 facet_push_stats(facet);
2958 if (!VLOG_DROP_WARN(&rl)) {
2962 odp_flow_key_format(key, key_len, &s);
2963 VLOG_WARN("unexpected flow from datapath %s", ds_cstr(&s));
2967 COVERAGE_INC(facet_unexpected);
2968 /* There's a flow in the datapath that we know nothing about, or a
2969 * flow that shouldn't be installed but was anyway. Delete it. */
2970 dpif_flow_del(p->dpif, key, key_len, NULL);
2973 dpif_flow_dump_done(&dump);
2976 /* Calculates and returns the number of milliseconds of idle time after which
2977 * subfacets should expire from the datapath. When a subfacet expires, we fold
2978 * its statistics into its facet, and when a facet's last subfacet expires, we
2979 * fold its statistic into its rule. */
2981 subfacet_max_idle(const struct ofproto_dpif *ofproto)
2984 * Idle time histogram.
2986 * Most of the time a switch has a relatively small number of subfacets.
2987 * When this is the case we might as well keep statistics for all of them
2988 * in userspace and to cache them in the kernel datapath for performance as
2991 * As the number of subfacets increases, the memory required to maintain
2992 * statistics about them in userspace and in the kernel becomes
2993 * significant. However, with a large number of subfacets it is likely
2994 * that only a few of them are "heavy hitters" that consume a large amount
2995 * of bandwidth. At this point, only heavy hitters are worth caching in
2996 * the kernel and maintaining in userspaces; other subfacets we can
2999 * The technique used to compute the idle time is to build a histogram with
3000 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
3001 * that is installed in the kernel gets dropped in the appropriate bucket.
3002 * After the histogram has been built, we compute the cutoff so that only
3003 * the most-recently-used 1% of subfacets (but at least
3004 * ofproto->up.flow_eviction_threshold flows) are kept cached. At least
3005 * the most-recently-used bucket of subfacets is kept, so actually an
3006 * arbitrary number of subfacets can be kept in any given expiration run
3007 * (though the next run will delete most of those unless they receive
3010 * This requires a second pass through the subfacets, in addition to the
3011 * pass made by update_stats(), because the former function never looks at
3012 * uninstallable subfacets.
3014 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
3015 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3016 int buckets[N_BUCKETS] = { 0 };
3017 int total, subtotal, bucket;
3018 struct subfacet *subfacet;
3022 total = hmap_count(&ofproto->subfacets);
3023 if (total <= ofproto->up.flow_eviction_threshold) {
3024 return N_BUCKETS * BUCKET_WIDTH;
3027 /* Build histogram. */
3029 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->subfacets) {
3030 long long int idle = now - subfacet->used;
3031 int bucket = (idle <= 0 ? 0
3032 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3033 : (unsigned int) idle / BUCKET_WIDTH);
3037 /* Find the first bucket whose flows should be expired. */
3038 subtotal = bucket = 0;
3040 subtotal += buckets[bucket++];
3041 } while (bucket < N_BUCKETS &&
3042 subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100));
3044 if (VLOG_IS_DBG_ENABLED()) {
3048 ds_put_cstr(&s, "keep");
3049 for (i = 0; i < N_BUCKETS; i++) {
3051 ds_put_cstr(&s, ", drop");
3054 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
3057 VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
3061 return bucket * BUCKET_WIDTH;
3065 expire_subfacets(struct ofproto_dpif *ofproto, int dp_max_idle)
3067 long long int cutoff = time_msec() - dp_max_idle;
3068 struct subfacet *subfacet, *next_subfacet;
3070 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
3071 &ofproto->subfacets) {
3072 if (subfacet->used < cutoff) {
3073 subfacet_destroy(ofproto, subfacet);
3078 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3079 * then delete it entirely. */
3081 rule_expire(struct rule_dpif *rule)
3083 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3084 struct facet *facet, *next_facet;
3088 /* Has 'rule' expired? */
3090 if (rule->up.hard_timeout
3091 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
3092 reason = OFPRR_HARD_TIMEOUT;
3093 } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
3094 && now > rule->used + rule->up.idle_timeout * 1000) {
3095 reason = OFPRR_IDLE_TIMEOUT;
3100 COVERAGE_INC(ofproto_dpif_expired);
3102 /* Update stats. (This is a no-op if the rule expired due to an idle
3103 * timeout, because that only happens when the rule has no facets left.) */
3104 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3105 facet_remove(ofproto, facet);
3108 /* Get rid of the rule. */
3109 ofproto_rule_expire(&rule->up, reason);
3114 /* Creates and returns a new facet owned by 'rule', given a 'flow'.
3116 * The caller must already have determined that no facet with an identical
3117 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
3118 * the ofproto's classifier table.
3120 * The facet will initially have no subfacets. The caller should create (at
3121 * least) one subfacet with subfacet_create(). */
3122 static struct facet *
3123 facet_create(struct rule_dpif *rule, const struct flow *flow)
3125 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3126 struct facet *facet;
3128 facet = xzalloc(sizeof *facet);
3129 facet->used = time_msec();
3130 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
3131 list_push_back(&rule->facets, &facet->list_node);
3133 facet->flow = *flow;
3134 list_init(&facet->subfacets);
3135 netflow_flow_init(&facet->nf_flow);
3136 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
3142 facet_free(struct facet *facet)
3147 /* If the 'actions_len' bytes of actions in 'odp_actions' are just a single
3148 * OVS_ACTION_ATTR_USERSPACE action, executes it internally and returns true.
3149 * Otherwise, returns false without doing anything.
3151 * If 'clone' is true, the caller always retains ownership of 'packet'.
3152 * Otherwise, ownership is transferred to this function if it returns true. */
3154 execute_controller_action(struct ofproto_dpif *ofproto,
3155 const struct flow *flow,
3156 const struct nlattr *odp_actions, size_t actions_len,
3157 struct ofpbuf *packet, bool clone)
3160 && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE
3161 && NLA_ALIGN(odp_actions->nla_len) == actions_len) {
3162 /* As an optimization, avoid a round-trip from userspace to kernel to
3163 * userspace. This also avoids possibly filling up kernel packet
3164 * buffers along the way.
3166 * This optimization will not accidentally catch sFlow
3167 * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated
3168 * inside OVS_ACTION_ATTR_SAMPLE. */
3169 const struct nlattr *nla;
3171 nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA);
3172 send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow,
3180 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3181 * 'packet', which arrived on 'in_port'.
3183 * Takes ownership of 'packet'. */
3185 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3186 const struct nlattr *odp_actions, size_t actions_len,
3187 struct ofpbuf *packet)
3189 struct odputil_keybuf keybuf;
3193 if (execute_controller_action(ofproto, flow, odp_actions, actions_len,
3198 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3199 odp_flow_key_from_flow(&key, flow);
3201 error = dpif_execute(ofproto->dpif, key.data, key.size,
3202 odp_actions, actions_len, packet);
3204 ofpbuf_delete(packet);
3208 /* Remove 'facet' from 'ofproto' and free up the associated memory:
3210 * - If 'facet' was installed in the datapath, uninstalls it and updates its
3211 * rule's statistics, via subfacet_uninstall().
3213 * - Removes 'facet' from its rule and from ofproto->facets.
3216 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
3218 struct subfacet *subfacet, *next_subfacet;
3220 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
3221 &facet->subfacets) {
3222 subfacet_destroy__(ofproto, subfacet);
3225 facet_flush_stats(ofproto, facet);
3226 hmap_remove(&ofproto->facets, &facet->hmap_node);
3227 list_remove(&facet->list_node);
3232 facet_account(struct ofproto_dpif *ofproto, struct facet *facet)
3235 struct subfacet *subfacet;
3236 const struct nlattr *a;
3240 if (facet->byte_count <= facet->accounted_bytes) {
3243 n_bytes = facet->byte_count - facet->accounted_bytes;
3244 facet->accounted_bytes = facet->byte_count;
3246 /* Feed information from the active flows back into the learning table to
3247 * ensure that table is always in sync with what is actually flowing
3248 * through the datapath. */
3249 if (facet->has_learn || facet->has_normal) {
3250 struct action_xlate_ctx ctx;
3252 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3253 facet->flow.vlan_tci, NULL);
3254 ctx.may_learn = true;
3255 ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions,
3256 facet->rule->up.n_actions));
3259 if (!facet->has_normal || !ofproto->has_bonded_bundles) {
3263 /* This loop feeds byte counters to bond_account() for rebalancing to use
3264 * as a basis. We also need to track the actual VLAN on which the packet
3265 * is going to be sent to ensure that it matches the one passed to
3266 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
3269 * We use the actions from an arbitrary subfacet because they should all
3270 * be equally valid for our purpose. */
3271 subfacet = CONTAINER_OF(list_front(&facet->subfacets),
3272 struct subfacet, list_node);
3273 vlan_tci = facet->flow.vlan_tci;
3274 NL_ATTR_FOR_EACH_UNSAFE (a, left,
3275 subfacet->actions, subfacet->actions_len) {
3276 const struct ovs_action_push_vlan *vlan;
3277 struct ofport_dpif *port;
3279 switch (nl_attr_type(a)) {
3280 case OVS_ACTION_ATTR_OUTPUT:
3281 port = get_odp_port(ofproto, nl_attr_get_u32(a));
3282 if (port && port->bundle && port->bundle->bond) {
3283 bond_account(port->bundle->bond, &facet->flow,
3284 vlan_tci_to_vid(vlan_tci), n_bytes);
3288 case OVS_ACTION_ATTR_POP_VLAN:
3289 vlan_tci = htons(0);
3292 case OVS_ACTION_ATTR_PUSH_VLAN:
3293 vlan = nl_attr_get(a);
3294 vlan_tci = vlan->vlan_tci;
3300 /* Returns true if the only action for 'facet' is to send to the controller.
3301 * (We don't report NetFlow expiration messages for such facets because they
3302 * are just part of the control logic for the network, not real traffic). */
3304 facet_is_controller_flow(struct facet *facet)
3307 && facet->rule->up.n_actions == 1
3308 && action_outputs_to_port(&facet->rule->up.actions[0],
3309 htons(OFPP_CONTROLLER)));
3312 /* Folds all of 'facet''s statistics into its rule. Also updates the
3313 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
3314 * 'facet''s statistics in the datapath should have been zeroed and folded into
3315 * its packet and byte counts before this function is called. */
3317 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
3319 struct subfacet *subfacet;
3321 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3322 assert(!subfacet->dp_byte_count);
3323 assert(!subfacet->dp_packet_count);
3326 facet_push_stats(facet);
3327 facet_account(ofproto, facet);
3329 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
3330 struct ofexpired expired;
3331 expired.flow = facet->flow;
3332 expired.packet_count = facet->packet_count;
3333 expired.byte_count = facet->byte_count;
3334 expired.used = facet->used;
3335 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
3338 facet->rule->packet_count += facet->packet_count;
3339 facet->rule->byte_count += facet->byte_count;
3341 /* Reset counters to prevent double counting if 'facet' ever gets
3343 facet_reset_counters(facet);
3345 netflow_flow_clear(&facet->nf_flow);
3348 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3349 * Returns it if found, otherwise a null pointer.
3351 * The returned facet might need revalidation; use facet_lookup_valid()
3352 * instead if that is important. */
3353 static struct facet *
3354 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
3356 struct facet *facet;
3358 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
3360 if (flow_equal(flow, &facet->flow)) {
3368 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
3369 * Returns it if found, otherwise a null pointer.
3371 * The returned facet is guaranteed to be valid. */
3372 static struct facet *
3373 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
3375 struct facet *facet = facet_find(ofproto, flow);
3377 /* The facet we found might not be valid, since we could be in need of
3378 * revalidation. If it is not valid, don't return it. */
3380 && (ofproto->need_revalidate
3381 || tag_set_intersects(&ofproto->revalidate_set, facet->tags))
3382 && !facet_revalidate(ofproto, facet)) {
3383 COVERAGE_INC(facet_invalidated);
3390 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
3392 * - If the rule found is different from 'facet''s current rule, moves
3393 * 'facet' to the new rule and recompiles its actions.
3395 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
3396 * where it is and recompiles its actions anyway.
3398 * - If there is none, destroys 'facet'.
3400 * Returns true if 'facet' still exists, false if it has been destroyed. */
3402 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
3405 struct nlattr *odp_actions;
3408 struct actions *new_actions;
3410 struct action_xlate_ctx ctx;
3411 struct rule_dpif *new_rule;
3412 struct subfacet *subfacet;
3413 bool actions_changed;
3416 COVERAGE_INC(facet_revalidate);
3418 /* Determine the new rule. */
3419 new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0);
3421 /* No new rule, so delete the facet. */
3422 facet_remove(ofproto, facet);
3426 /* Calculate new datapath actions.
3428 * We do not modify any 'facet' state yet, because we might need to, e.g.,
3429 * emit a NetFlow expiration and, if so, we need to have the old state
3430 * around to properly compose it. */
3432 /* If the datapath actions changed or the installability changed,
3433 * then we need to talk to the datapath. */
3436 memset(&ctx, 0, sizeof ctx);
3437 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3438 struct ofpbuf *odp_actions;
3439 bool should_install;
3441 action_xlate_ctx_init(&ctx, ofproto, &facet->flow,
3442 subfacet->initial_tci, NULL);
3443 odp_actions = xlate_actions(&ctx, new_rule->up.actions,
3444 new_rule->up.n_actions);
3445 actions_changed = (subfacet->actions_len != odp_actions->size
3446 || memcmp(subfacet->actions, odp_actions->data,
3447 subfacet->actions_len));
3449 should_install = (ctx.may_set_up_flow
3450 && subfacet->key_fitness != ODP_FIT_TOO_LITTLE);
3451 if (actions_changed || should_install != subfacet->installed) {
3452 if (should_install) {
3453 struct dpif_flow_stats stats;
3455 subfacet_install(ofproto, subfacet,
3456 odp_actions->data, odp_actions->size, &stats);
3457 subfacet_update_stats(ofproto, subfacet, &stats);
3459 subfacet_uninstall(ofproto, subfacet);
3463 new_actions = xcalloc(list_size(&facet->subfacets),
3464 sizeof *new_actions);
3466 new_actions[i].odp_actions = xmemdup(odp_actions->data,
3468 new_actions[i].actions_len = odp_actions->size;
3471 ofpbuf_delete(odp_actions);
3475 facet_flush_stats(ofproto, facet);
3478 /* Update 'facet' now that we've taken care of all the old state. */
3479 facet->tags = ctx.tags;
3480 facet->nf_flow.output_iface = ctx.nf_output_iface;
3481 facet->may_install = ctx.may_set_up_flow;
3482 facet->has_learn = ctx.has_learn;
3483 facet->has_normal = ctx.has_normal;
3484 facet->mirrors = ctx.mirrors;
3487 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
3488 if (new_actions[i].odp_actions) {
3489 free(subfacet->actions);
3490 subfacet->actions = new_actions[i].odp_actions;
3491 subfacet->actions_len = new_actions[i].actions_len;
3497 if (facet->rule != new_rule) {
3498 COVERAGE_INC(facet_changed_rule);
3499 list_remove(&facet->list_node);
3500 list_push_back(&new_rule->facets, &facet->list_node);
3501 facet->rule = new_rule;
3502 facet->used = new_rule->up.created;
3503 facet->prev_used = facet->used;
3509 /* Updates 'facet''s used time. Caller is responsible for calling
3510 * facet_push_stats() to update the flows which 'facet' resubmits into. */
3512 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
3515 if (used > facet->used) {
3517 if (used > facet->rule->used) {
3518 facet->rule->used = used;
3520 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
3525 facet_reset_counters(struct facet *facet)
3527 facet->packet_count = 0;
3528 facet->byte_count = 0;
3529 facet->prev_packet_count = 0;
3530 facet->prev_byte_count = 0;
3531 facet->accounted_bytes = 0;
3535 facet_push_stats(struct facet *facet)
3537 uint64_t new_packets, new_bytes;
3539 assert(facet->packet_count >= facet->prev_packet_count);
3540 assert(facet->byte_count >= facet->prev_byte_count);
3541 assert(facet->used >= facet->prev_used);
3543 new_packets = facet->packet_count - facet->prev_packet_count;
3544 new_bytes = facet->byte_count - facet->prev_byte_count;
3546 if (new_packets || new_bytes || facet->used > facet->prev_used) {
3547 facet->prev_packet_count = facet->packet_count;
3548 facet->prev_byte_count = facet->byte_count;
3549 facet->prev_used = facet->used;
3551 flow_push_stats(facet->rule, &facet->flow,
3552 new_packets, new_bytes, facet->used);
3554 update_mirror_stats(ofproto_dpif_cast(facet->rule->up.ofproto),
3555 facet->mirrors, new_packets, new_bytes);
3559 struct ofproto_push {
3560 struct action_xlate_ctx ctx;
3567 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3569 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
3572 rule->packet_count += push->packets;
3573 rule->byte_count += push->bytes;
3574 rule->used = MAX(push->used, rule->used);
3578 /* Pushes flow statistics to the rules which 'flow' resubmits into given
3579 * 'rule''s actions and mirrors. */
3581 flow_push_stats(const struct rule_dpif *rule,
3582 const struct flow *flow, uint64_t packets, uint64_t bytes,
3585 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3586 struct ofproto_push push;
3588 push.packets = packets;
3592 action_xlate_ctx_init(&push.ctx, ofproto, flow, flow->vlan_tci, NULL);
3593 push.ctx.resubmit_hook = push_resubmit;
3594 ofpbuf_delete(xlate_actions(&push.ctx,
3595 rule->up.actions, rule->up.n_actions));
3600 static struct subfacet *
3601 subfacet_find__(struct ofproto_dpif *ofproto,
3602 const struct nlattr *key, size_t key_len, uint32_t key_hash,
3603 const struct flow *flow)
3605 struct subfacet *subfacet;
3607 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
3608 &ofproto->subfacets) {
3610 ? (subfacet->key_len == key_len
3611 && !memcmp(key, subfacet->key, key_len))
3612 : flow_equal(flow, &subfacet->facet->flow)) {
3620 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
3621 * 'key_fitness', 'key', and 'key_len'. Returns the existing subfacet if
3622 * there is one, otherwise creates and returns a new subfacet.
3624 * If the returned subfacet is new, then subfacet->actions will be NULL, in
3625 * which case the caller must populate the actions with
3626 * subfacet_make_actions(). */
3627 static struct subfacet *
3628 subfacet_create(struct ofproto_dpif *ofproto, struct facet *facet,
3629 enum odp_key_fitness key_fitness,
3630 const struct nlattr *key, size_t key_len, ovs_be16 initial_tci)
3632 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3633 struct subfacet *subfacet;
3635 subfacet = subfacet_find__(ofproto, key, key_len, key_hash, &facet->flow);
3637 if (subfacet->facet == facet) {
3641 /* This shouldn't happen. */
3642 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
3643 subfacet_destroy(ofproto, subfacet);
3646 subfacet = xzalloc(sizeof *subfacet);
3647 hmap_insert(&ofproto->subfacets, &subfacet->hmap_node, key_hash);
3648 list_push_back(&facet->subfacets, &subfacet->list_node);
3649 subfacet->facet = facet;
3650 subfacet->used = time_msec();
3651 subfacet->key_fitness = key_fitness;
3652 if (key_fitness != ODP_FIT_PERFECT) {
3653 subfacet->key = xmemdup(key, key_len);
3654 subfacet->key_len = key_len;
3656 subfacet->installed = false;
3657 subfacet->initial_tci = initial_tci;
3662 /* Searches 'ofproto' for a subfacet with the given 'key', 'key_len', and
3663 * 'flow'. Returns the subfacet if one exists, otherwise NULL. */
3664 static struct subfacet *
3665 subfacet_find(struct ofproto_dpif *ofproto,
3666 const struct nlattr *key, size_t key_len)
3668 uint32_t key_hash = odp_flow_key_hash(key, key_len);
3669 enum odp_key_fitness fitness;
3672 fitness = odp_flow_key_to_flow(key, key_len, &flow);
3673 if (fitness == ODP_FIT_ERROR) {
3677 return subfacet_find__(ofproto, key, key_len, key_hash, &flow);
3680 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
3681 * its facet within 'ofproto', and frees it. */
3683 subfacet_destroy__(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3685 subfacet_uninstall(ofproto, subfacet);
3686 hmap_remove(&ofproto->subfacets, &subfacet->hmap_node);
3687 list_remove(&subfacet->list_node);
3688 free(subfacet->key);
3689 free(subfacet->actions);
3693 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
3694 * last remaining subfacet in its facet destroys the facet too. */
3696 subfacet_destroy(struct ofproto_dpif *ofproto, struct subfacet *subfacet)
3698 struct facet *facet = subfacet->facet;
3700 subfacet_destroy__(ofproto, subfacet);
3701 if (list_is_empty(&facet->subfacets)) {
3702 facet_remove(ofproto, facet);
3706 /* Initializes 'key' with the sequence of OVS_KEY_ATTR_* Netlink attributes
3707 * that can be used to refer to 'subfacet'. The caller must provide 'keybuf'
3708 * for use as temporary storage. */
3710 subfacet_get_key(struct subfacet *subfacet, struct odputil_keybuf *keybuf,
3713 if (!subfacet->key) {
3714 ofpbuf_use_stack(key, keybuf, sizeof *keybuf);
3715 odp_flow_key_from_flow(key, &subfacet->facet->flow);
3717 ofpbuf_use_const(key, subfacet->key, subfacet->key_len);
3721 /* Composes the datapath actions for 'subfacet' based on its rule's actions. */
3723 subfacet_make_actions(struct ofproto_dpif *p, struct subfacet *subfacet,
3724 const struct ofpbuf *packet)
3726 struct facet *facet = subfacet->facet;
3727 const struct rule_dpif *rule = facet->rule;
3728 struct ofpbuf *odp_actions;
3729 struct action_xlate_ctx ctx;
3731 action_xlate_ctx_init(&ctx, p, &facet->flow, subfacet->initial_tci,
3733 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
3734 facet->tags = ctx.tags;
3735 facet->may_install = ctx.may_set_up_flow;
3736 facet->has_learn = ctx.has_learn;
3737 facet->has_normal = ctx.has_normal;
3738 facet->nf_flow.output_iface = ctx.nf_output_iface;
3739 facet->mirrors = ctx.mirrors;
3741 if (subfacet->actions_len != odp_actions->size
3742 || memcmp(subfacet->actions, odp_actions->data, odp_actions->size)) {
3743 free(subfacet->actions);
3744 subfacet->actions_len = odp_actions->size;
3745 subfacet->actions = xmemdup(odp_actions->data, odp_actions->size);
3748 ofpbuf_delete(odp_actions);
3751 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
3752 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
3753 * in the datapath will be zeroed and 'stats' will be updated with traffic new
3754 * since 'subfacet' was last updated.
3756 * Returns 0 if successful, otherwise a positive errno value. */
3758 subfacet_install(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3759 const struct nlattr *actions, size_t actions_len,
3760 struct dpif_flow_stats *stats)
3762 struct odputil_keybuf keybuf;
3763 enum dpif_flow_put_flags flags;
3767 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3769 flags |= DPIF_FP_ZERO_STATS;
3772 subfacet_get_key(subfacet, &keybuf, &key);
3773 ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
3774 actions, actions_len, stats);
3777 subfacet_reset_dp_stats(subfacet, stats);
3783 /* If 'subfacet' is installed in the datapath, uninstalls it. */
3785 subfacet_uninstall(struct ofproto_dpif *p, struct subfacet *subfacet)
3787 if (subfacet->installed) {
3788 struct odputil_keybuf keybuf;
3789 struct dpif_flow_stats stats;
3793 subfacet_get_key(subfacet, &keybuf, &key);
3794 error = dpif_flow_del(p->dpif, key.data, key.size, &stats);
3795 subfacet_reset_dp_stats(subfacet, &stats);
3797 subfacet_update_stats(p, subfacet, &stats);
3799 subfacet->installed = false;
3801 assert(subfacet->dp_packet_count == 0);
3802 assert(subfacet->dp_byte_count == 0);
3806 /* Resets 'subfacet''s datapath statistics counters. This should be called
3807 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
3808 * non-null, it should contain the statistics returned by dpif when 'subfacet'
3809 * was reset in the datapath. 'stats' will be modified to include only
3810 * statistics new since 'subfacet' was last updated. */
3812 subfacet_reset_dp_stats(struct subfacet *subfacet,
3813 struct dpif_flow_stats *stats)
3816 && subfacet->dp_packet_count <= stats->n_packets
3817 && subfacet->dp_byte_count <= stats->n_bytes) {
3818 stats->n_packets -= subfacet->dp_packet_count;
3819 stats->n_bytes -= subfacet->dp_byte_count;
3822 subfacet->dp_packet_count = 0;
3823 subfacet->dp_byte_count = 0;
3826 /* Updates 'subfacet''s used time. The caller is responsible for calling
3827 * facet_push_stats() to update the flows which 'subfacet' resubmits into. */
3829 subfacet_update_time(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3832 if (used > subfacet->used) {
3833 subfacet->used = used;
3834 facet_update_time(ofproto, subfacet->facet, used);
3838 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
3840 * Because of the meaning of a subfacet's counters, it only makes sense to do
3841 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
3842 * represents a packet that was sent by hand or if it represents statistics
3843 * that have been cleared out of the datapath. */
3845 subfacet_update_stats(struct ofproto_dpif *ofproto, struct subfacet *subfacet,
3846 const struct dpif_flow_stats *stats)
3848 if (stats->n_packets || stats->used > subfacet->used) {
3849 struct facet *facet = subfacet->facet;
3851 subfacet_update_time(ofproto, subfacet, stats->used);
3852 facet->packet_count += stats->n_packets;
3853 facet->byte_count += stats->n_bytes;
3854 facet_push_stats(facet);
3855 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
3861 static struct rule_dpif *
3862 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3865 struct cls_rule *cls_rule;
3866 struct classifier *cls;
3868 if (table_id >= N_TABLES) {
3872 cls = &ofproto->up.tables[table_id];
3873 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3874 && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3875 /* For OFPC_NORMAL frag_handling, we must pretend that transport ports
3876 * are unavailable. */
3877 struct flow ofpc_normal_flow = *flow;
3878 ofpc_normal_flow.tp_src = htons(0);
3879 ofpc_normal_flow.tp_dst = htons(0);
3880 cls_rule = classifier_lookup(cls, &ofpc_normal_flow);
3882 cls_rule = classifier_lookup(cls, flow);
3884 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
3888 complete_operation(struct rule_dpif *rule)
3890 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3892 rule_invalidate(rule);
3894 struct dpif_completion *c = xmalloc(sizeof *c);
3895 c->op = rule->up.pending;
3896 list_push_back(&ofproto->completions, &c->list_node);
3898 ofoperation_complete(rule->up.pending, 0);
3902 static struct rule *
3905 struct rule_dpif *rule = xmalloc(sizeof *rule);
3910 rule_dealloc(struct rule *rule_)
3912 struct rule_dpif *rule = rule_dpif_cast(rule_);
3917 rule_construct(struct rule *rule_)
3919 struct rule_dpif *rule = rule_dpif_cast(rule_);
3920 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3921 struct rule_dpif *victim;
3925 error = validate_actions(rule->up.actions, rule->up.n_actions,
3926 &rule->up.cr.flow, ofproto->max_ports);
3931 rule->used = rule->up.created;
3932 rule->packet_count = 0;
3933 rule->byte_count = 0;
3935 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
3936 if (victim && !list_is_empty(&victim->facets)) {
3937 struct facet *facet;
3939 rule->facets = victim->facets;
3940 list_moved(&rule->facets);
3941 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3942 /* XXX: We're only clearing our local counters here. It's possible
3943 * that quite a few packets are unaccounted for in the datapath
3944 * statistics. These will be accounted to the new rule instead of
3945 * cleared as required. This could be fixed by clearing out the
3946 * datapath statistics for this facet, but currently it doesn't
3948 facet_reset_counters(facet);
3952 /* Must avoid list_moved() in this case. */
3953 list_init(&rule->facets);
3956 table_id = rule->up.table_id;
3957 rule->tag = (victim ? victim->tag
3959 : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc,
3960 ofproto->tables[table_id].basis));
3962 complete_operation(rule);
3967 rule_destruct(struct rule *rule_)
3969 struct rule_dpif *rule = rule_dpif_cast(rule_);
3970 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3971 struct facet *facet, *next_facet;
3973 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
3974 facet_revalidate(ofproto, facet);
3977 complete_operation(rule);
3981 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3983 struct rule_dpif *rule = rule_dpif_cast(rule_);
3984 struct facet *facet;
3986 /* Start from historical data for 'rule' itself that are no longer tracked
3987 * in facets. This counts, for example, facets that have expired. */
3988 *packets = rule->packet_count;
3989 *bytes = rule->byte_count;
3991 /* Add any statistics that are tracked by facets. This includes
3992 * statistical data recently updated by ofproto_update_stats() as well as
3993 * stats for packets that were executed "by hand" via dpif_execute(). */
3994 LIST_FOR_EACH (facet, list_node, &rule->facets) {
3995 *packets += facet->packet_count;
3996 *bytes += facet->byte_count;
4001 rule_execute(struct rule *rule_, const struct flow *flow,
4002 struct ofpbuf *packet)
4004 struct rule_dpif *rule = rule_dpif_cast(rule_);
4005 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4006 struct action_xlate_ctx ctx;
4007 struct ofpbuf *odp_actions;
4010 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
4011 odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
4012 size = packet->size;
4013 if (execute_odp_actions(ofproto, flow, odp_actions->data,
4014 odp_actions->size, packet)) {
4015 rule->used = time_msec();
4016 rule->packet_count++;
4017 rule->byte_count += size;
4018 flow_push_stats(rule, flow, 1, size, rule->used);
4020 ofpbuf_delete(odp_actions);
4026 rule_modify_actions(struct rule *rule_)
4028 struct rule_dpif *rule = rule_dpif_cast(rule_);
4029 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4032 error = validate_actions(rule->up.actions, rule->up.n_actions,
4033 &rule->up.cr.flow, ofproto->max_ports);
4035 ofoperation_complete(rule->up.pending, error);
4039 complete_operation(rule);
4042 /* Sends 'packet' out 'ofport'.
4043 * May modify 'packet'.
4044 * Returns 0 if successful, otherwise a positive errno value. */
4046 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4048 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4049 struct ofpbuf key, odp_actions;
4050 struct odputil_keybuf keybuf;
4055 flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow);
4056 odp_port = vsp_realdev_to_vlandev(ofproto, ofport->odp_port,
4058 if (odp_port != ofport->odp_port) {
4059 eth_pop_vlan(packet);
4060 flow.vlan_tci = htons(0);
4063 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4064 odp_flow_key_from_flow(&key, &flow);
4066 ofpbuf_init(&odp_actions, 32);
4067 compose_sflow_action(ofproto, &odp_actions, &flow, odp_port);
4069 nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port);
4070 error = dpif_execute(ofproto->dpif,
4072 odp_actions.data, odp_actions.size,
4074 ofpbuf_uninit(&odp_actions);
4077 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
4078 ofproto->up.name, odp_port, strerror(error));
4080 ofproto_update_local_port_stats(ofport->up.ofproto, packet->size, 0);
4084 /* OpenFlow to datapath action translation. */
4086 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
4087 struct action_xlate_ctx *ctx);
4088 static void xlate_normal(struct action_xlate_ctx *);
4091 put_userspace_action(const struct ofproto_dpif *ofproto,
4092 struct ofpbuf *odp_actions,
4093 const struct flow *flow,
4094 const struct user_action_cookie *cookie)
4098 pid = dpif_port_get_pid(ofproto->dpif,
4099 ofp_port_to_odp_port(flow->in_port));
4101 return odp_put_userspace_action(pid, cookie, odp_actions);
4104 /* Compose SAMPLE action for sFlow. */
4106 compose_sflow_action(const struct ofproto_dpif *ofproto,
4107 struct ofpbuf *odp_actions,
4108 const struct flow *flow,
4111 uint32_t port_ifindex;
4112 uint32_t probability;
4113 struct user_action_cookie cookie;
4114 size_t sample_offset, actions_offset;
4115 int cookie_offset, n_output;
4117 if (!ofproto->sflow || flow->in_port == OFPP_NONE) {
4121 if (odp_port == OVSP_NONE) {
4125 port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port);
4129 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
4131 /* Number of packets out of UINT_MAX to sample. */
4132 probability = dpif_sflow_get_probability(ofproto->sflow);
4133 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
4135 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
4137 cookie.type = USER_ACTION_COOKIE_SFLOW;
4138 cookie.data = port_ifindex;
4139 cookie.n_output = n_output;
4140 cookie.vlan_tci = 0;
4141 cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie);
4143 nl_msg_end_nested(odp_actions, actions_offset);
4144 nl_msg_end_nested(odp_actions, sample_offset);
4145 return cookie_offset;
4148 /* SAMPLE action must be first action in any given list of actions.
4149 * At this point we do not have all information required to build it. So try to
4150 * build sample action as complete as possible. */
4152 add_sflow_action(struct action_xlate_ctx *ctx)
4154 ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto,
4156 &ctx->flow, OVSP_NONE);
4157 ctx->sflow_odp_port = 0;
4158 ctx->sflow_n_outputs = 0;
4161 /* Fix SAMPLE action according to data collected while composing ODP actions.
4162 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
4163 * USERSPACE action's user-cookie which is required for sflow. */
4165 fix_sflow_action(struct action_xlate_ctx *ctx)
4167 const struct flow *base = &ctx->base_flow;
4168 struct user_action_cookie *cookie;
4170 if (!ctx->user_cookie_offset) {
4174 cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset,
4176 assert(cookie != NULL);
4177 assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
4179 if (ctx->sflow_n_outputs) {
4180 cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow,
4181 ctx->sflow_odp_port);
4183 if (ctx->sflow_n_outputs >= 255) {
4184 cookie->n_output = 255;
4186 cookie->n_output = ctx->sflow_n_outputs;
4188 cookie->vlan_tci = base->vlan_tci;
4192 compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port,
4195 const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
4196 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
4197 ovs_be16 flow_vlan_tci = ctx->flow.vlan_tci;
4198 uint8_t flow_nw_tos = ctx->flow.nw_tos;
4202 struct priority_to_dscp *pdscp;
4204 if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)
4205 || (check_stp && !stp_forward_in_state(ofport->stp_state))) {
4209 pdscp = get_priority(ofport, ctx->flow.skb_priority);
4211 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4212 ctx->flow.nw_tos |= pdscp->dscp;
4215 /* We may not have an ofport record for this port, but it doesn't hurt
4216 * to allow forwarding to it anyhow. Maybe such a port will appear
4217 * later and we're pre-populating the flow table. */
4220 out_port = vsp_realdev_to_vlandev(ctx->ofproto, odp_port,
4221 ctx->flow.vlan_tci);
4222 if (out_port != odp_port) {
4223 ctx->flow.vlan_tci = htons(0);
4225 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4226 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, out_port);
4228 ctx->sflow_odp_port = odp_port;
4229 ctx->sflow_n_outputs++;
4230 ctx->nf_output_iface = ofp_port;
4231 ctx->flow.vlan_tci = flow_vlan_tci;
4232 ctx->flow.nw_tos = flow_nw_tos;
4236 compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
4238 compose_output_action__(ctx, ofp_port, true);
4242 xlate_table_action(struct action_xlate_ctx *ctx,
4243 uint16_t in_port, uint8_t table_id)
4245 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
4246 struct ofproto_dpif *ofproto = ctx->ofproto;
4247 struct rule_dpif *rule;
4248 uint16_t old_in_port;
4249 uint8_t old_table_id;
4251 old_table_id = ctx->table_id;
4252 ctx->table_id = table_id;
4254 /* Look up a flow with 'in_port' as the input port. */
4255 old_in_port = ctx->flow.in_port;
4256 ctx->flow.in_port = in_port;
4257 rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id);
4260 if (table_id > 0 && table_id < N_TABLES) {
4261 struct table_dpif *table = &ofproto->tables[table_id];
4262 if (table->other_table) {
4265 : rule_calculate_tag(&ctx->flow,
4266 &table->other_table->wc,
4271 /* Restore the original input port. Otherwise OFPP_NORMAL and
4272 * OFPP_IN_PORT will have surprising behavior. */
4273 ctx->flow.in_port = old_in_port;
4275 if (ctx->resubmit_hook) {
4276 ctx->resubmit_hook(ctx, rule);
4281 do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
4285 ctx->table_id = old_table_id;
4287 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
4289 VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times",
4290 MAX_RESUBMIT_RECURSION);
4295 xlate_resubmit_table(struct action_xlate_ctx *ctx,
4296 const struct nx_action_resubmit *nar)
4301 in_port = (nar->in_port == htons(OFPP_IN_PORT)
4303 : ntohs(nar->in_port));
4304 table_id = nar->table == 255 ? ctx->table_id : nar->table;
4306 xlate_table_action(ctx, in_port, table_id);
4310 flood_packets(struct action_xlate_ctx *ctx, bool all)
4312 struct ofport_dpif *ofport;
4314 HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) {
4315 uint16_t ofp_port = ofport->up.ofp_port;
4317 if (ofp_port == ctx->flow.in_port) {
4322 compose_output_action__(ctx, ofp_port, false);
4323 } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) {
4324 compose_output_action(ctx, ofp_port);
4328 ctx->nf_output_iface = NF_OUT_FLOOD;
4332 compose_controller_action(struct action_xlate_ctx *ctx, int len)
4334 struct user_action_cookie cookie;
4336 commit_odp_actions(&ctx->flow, &ctx->base_flow, ctx->odp_actions);
4337 cookie.type = USER_ACTION_COOKIE_CONTROLLER;
4339 cookie.n_output = 0;
4340 cookie.vlan_tci = 0;
4341 put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie);
4345 xlate_output_action__(struct action_xlate_ctx *ctx,
4346 uint16_t port, uint16_t max_len)
4348 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
4350 ctx->nf_output_iface = NF_OUT_DROP;
4354 compose_output_action(ctx, ctx->flow.in_port);
4357 xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id);
4363 flood_packets(ctx, false);
4366 flood_packets(ctx, true);
4368 case OFPP_CONTROLLER:
4369 compose_controller_action(ctx, max_len);
4372 compose_output_action(ctx, OFPP_LOCAL);
4377 if (port != ctx->flow.in_port) {
4378 compose_output_action(ctx, port);
4383 if (prev_nf_output_iface == NF_OUT_FLOOD) {
4384 ctx->nf_output_iface = NF_OUT_FLOOD;
4385 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4386 ctx->nf_output_iface = prev_nf_output_iface;
4387 } else if (prev_nf_output_iface != NF_OUT_DROP &&
4388 ctx->nf_output_iface != NF_OUT_FLOOD) {
4389 ctx->nf_output_iface = NF_OUT_MULTI;
4394 xlate_output_reg_action(struct action_xlate_ctx *ctx,
4395 const struct nx_action_output_reg *naor)
4399 ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow);
4401 if (ofp_port <= UINT16_MAX) {
4402 xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len));
4407 xlate_output_action(struct action_xlate_ctx *ctx,
4408 const struct ofp_action_output *oao)
4410 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
4414 xlate_enqueue_action(struct action_xlate_ctx *ctx,
4415 const struct ofp_action_enqueue *oae)
4418 uint32_t flow_priority, priority;
4421 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
4424 /* Fall back to ordinary output action. */
4425 xlate_output_action__(ctx, ntohs(oae->port), 0);
4429 /* Figure out datapath output port. */
4430 ofp_port = ntohs(oae->port);
4431 if (ofp_port == OFPP_IN_PORT) {
4432 ofp_port = ctx->flow.in_port;
4433 } else if (ofp_port == ctx->flow.in_port) {
4437 /* Add datapath actions. */
4438 flow_priority = ctx->flow.skb_priority;
4439 ctx->flow.skb_priority = priority;
4440 compose_output_action(ctx, ofp_port);
4441 ctx->flow.skb_priority = flow_priority;
4443 /* Update NetFlow output port. */
4444 if (ctx->nf_output_iface == NF_OUT_DROP) {
4445 ctx->nf_output_iface = ofp_port;
4446 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4447 ctx->nf_output_iface = NF_OUT_MULTI;
4452 xlate_set_queue_action(struct action_xlate_ctx *ctx,
4453 const struct nx_action_set_queue *nasq)
4458 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
4461 /* Couldn't translate queue to a priority, so ignore. A warning
4462 * has already been logged. */
4466 ctx->flow.skb_priority = priority;
4469 struct xlate_reg_state {
4475 xlate_autopath(struct action_xlate_ctx *ctx,
4476 const struct nx_action_autopath *naa)
4478 uint16_t ofp_port = ntohl(naa->id);
4479 struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
4481 if (!port || !port->bundle) {
4482 ofp_port = OFPP_NONE;
4483 } else if (port->bundle->bond) {
4484 /* Autopath does not support VLAN hashing. */
4485 struct ofport_dpif *slave = bond_choose_output_slave(
4486 port->bundle->bond, &ctx->flow, 0, &ctx->tags);
4488 ofp_port = slave->up.ofp_port;
4491 autopath_execute(naa, &ctx->flow, ofp_port);
4495 slave_enabled_cb(uint16_t ofp_port, void *ofproto_)
4497 struct ofproto_dpif *ofproto = ofproto_;
4498 struct ofport_dpif *port;
4508 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4511 port = get_ofp_port(ofproto, ofp_port);
4512 return port ? port->may_enable : false;
4517 xlate_learn_action(struct action_xlate_ctx *ctx,
4518 const struct nx_action_learn *learn)
4520 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
4521 struct ofputil_flow_mod fm;
4524 learn_execute(learn, &ctx->flow, &fm);
4526 error = ofproto_flow_mod(&ctx->ofproto->up, &fm);
4527 if (error && !VLOG_DROP_WARN(&rl)) {
4528 char *msg = ofputil_error_to_string(error);
4529 VLOG_WARN("learning action failed to modify flow table (%s)", msg);
4537 may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx)
4539 if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
4540 ? htonl(OFPPC_NO_RECV_STP)
4541 : htonl(OFPPC_NO_RECV))) {
4545 /* Only drop packets here if both forwarding and learning are
4546 * disabled. If just learning is enabled, we need to have
4547 * OFPP_NORMAL and the learning action have a look at the packet
4548 * before we can drop it. */
4549 if (!stp_forward_in_state(port->stp_state)
4550 && !stp_learn_in_state(port->stp_state)) {
4558 do_xlate_actions(const union ofp_action *in, size_t n_in,
4559 struct action_xlate_ctx *ctx)
4561 const struct ofport_dpif *port;
4562 const union ofp_action *ia;
4565 port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
4566 if (port && !may_receive(port, ctx)) {
4567 /* Drop this flow. */
4571 OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) {
4572 const struct ofp_action_dl_addr *oada;
4573 const struct nx_action_resubmit *nar;
4574 const struct nx_action_set_tunnel *nast;
4575 const struct nx_action_set_queue *nasq;
4576 const struct nx_action_multipath *nam;
4577 const struct nx_action_autopath *naa;
4578 const struct nx_action_bundle *nab;
4579 const struct nx_action_output_reg *naor;
4580 enum ofputil_action_code code;
4587 code = ofputil_decode_action_unsafe(ia);
4589 case OFPUTIL_OFPAT_OUTPUT:
4590 xlate_output_action(ctx, &ia->output);
4593 case OFPUTIL_OFPAT_SET_VLAN_VID:
4594 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
4595 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
4598 case OFPUTIL_OFPAT_SET_VLAN_PCP:
4599 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
4600 ctx->flow.vlan_tci |= htons(
4601 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
4604 case OFPUTIL_OFPAT_STRIP_VLAN:
4605 ctx->flow.vlan_tci = htons(0);
4608 case OFPUTIL_OFPAT_SET_DL_SRC:
4609 oada = ((struct ofp_action_dl_addr *) ia);
4610 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
4613 case OFPUTIL_OFPAT_SET_DL_DST:
4614 oada = ((struct ofp_action_dl_addr *) ia);
4615 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
4618 case OFPUTIL_OFPAT_SET_NW_SRC:
4619 ctx->flow.nw_src = ia->nw_addr.nw_addr;
4622 case OFPUTIL_OFPAT_SET_NW_DST:
4623 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
4626 case OFPUTIL_OFPAT_SET_NW_TOS:
4627 ctx->flow.nw_tos &= ~IP_DSCP_MASK;
4628 ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK;
4631 case OFPUTIL_OFPAT_SET_TP_SRC:
4632 ctx->flow.tp_src = ia->tp_port.tp_port;
4635 case OFPUTIL_OFPAT_SET_TP_DST:
4636 ctx->flow.tp_dst = ia->tp_port.tp_port;
4639 case OFPUTIL_OFPAT_ENQUEUE:
4640 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
4643 case OFPUTIL_NXAST_RESUBMIT:
4644 nar = (const struct nx_action_resubmit *) ia;
4645 xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id);
4648 case OFPUTIL_NXAST_RESUBMIT_TABLE:
4649 xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia);
4652 case OFPUTIL_NXAST_SET_TUNNEL:
4653 nast = (const struct nx_action_set_tunnel *) ia;
4654 tun_id = htonll(ntohl(nast->tun_id));
4655 ctx->flow.tun_id = tun_id;
4658 case OFPUTIL_NXAST_SET_QUEUE:
4659 nasq = (const struct nx_action_set_queue *) ia;
4660 xlate_set_queue_action(ctx, nasq);
4663 case OFPUTIL_NXAST_POP_QUEUE:
4664 ctx->flow.skb_priority = ctx->orig_skb_priority;
4667 case OFPUTIL_NXAST_REG_MOVE:
4668 nxm_execute_reg_move((const struct nx_action_reg_move *) ia,
4672 case OFPUTIL_NXAST_REG_LOAD:
4673 nxm_execute_reg_load((const struct nx_action_reg_load *) ia,
4677 case OFPUTIL_NXAST_NOTE:
4678 /* Nothing to do. */
4681 case OFPUTIL_NXAST_SET_TUNNEL64:
4682 tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id;
4683 ctx->flow.tun_id = tun_id;
4686 case OFPUTIL_NXAST_MULTIPATH:
4687 nam = (const struct nx_action_multipath *) ia;
4688 multipath_execute(nam, &ctx->flow);
4691 case OFPUTIL_NXAST_AUTOPATH:
4692 naa = (const struct nx_action_autopath *) ia;
4693 xlate_autopath(ctx, naa);
4696 case OFPUTIL_NXAST_BUNDLE:
4697 ctx->ofproto->has_bundle_action = true;
4698 nab = (const struct nx_action_bundle *) ia;
4699 xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow,
4704 case OFPUTIL_NXAST_BUNDLE_LOAD:
4705 ctx->ofproto->has_bundle_action = true;
4706 nab = (const struct nx_action_bundle *) ia;
4707 bundle_execute_load(nab, &ctx->flow, slave_enabled_cb,
4711 case OFPUTIL_NXAST_OUTPUT_REG:
4712 naor = (const struct nx_action_output_reg *) ia;
4713 xlate_output_reg_action(ctx, naor);
4716 case OFPUTIL_NXAST_LEARN:
4717 ctx->has_learn = true;
4718 if (ctx->may_learn) {
4719 xlate_learn_action(ctx, (const struct nx_action_learn *) ia);
4723 case OFPUTIL_NXAST_EXIT:
4729 /* We've let OFPP_NORMAL and the learning action look at the packet,
4730 * so drop it now if forwarding is disabled. */
4731 if (port && !stp_forward_in_state(port->stp_state)) {
4732 ofpbuf_clear(ctx->odp_actions);
4733 add_sflow_action(ctx);
4738 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
4739 struct ofproto_dpif *ofproto, const struct flow *flow,
4740 ovs_be16 initial_tci, const struct ofpbuf *packet)
4742 ctx->ofproto = ofproto;
4744 ctx->base_flow = ctx->flow;
4745 ctx->base_flow.tun_id = 0;
4746 ctx->base_flow.vlan_tci = initial_tci;
4747 ctx->packet = packet;
4748 ctx->may_learn = packet != NULL;
4749 ctx->resubmit_hook = NULL;
4752 static struct ofpbuf *
4753 xlate_actions(struct action_xlate_ctx *ctx,
4754 const union ofp_action *in, size_t n_in)
4756 struct flow orig_flow = ctx->flow;
4758 COVERAGE_INC(ofproto_dpif_xlate);
4760 ctx->odp_actions = ofpbuf_new(512);
4761 ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE);
4763 ctx->may_set_up_flow = true;
4764 ctx->has_learn = false;
4765 ctx->has_normal = false;
4766 ctx->nf_output_iface = NF_OUT_DROP;
4769 ctx->orig_skb_priority = ctx->flow.skb_priority;
4773 if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) {
4774 switch (ctx->ofproto->up.frag_handling) {
4775 case OFPC_FRAG_NORMAL:
4776 /* We must pretend that transport ports are unavailable. */
4777 ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0);
4778 ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0);
4781 case OFPC_FRAG_DROP:
4782 return ctx->odp_actions;
4784 case OFPC_FRAG_REASM:
4787 case OFPC_FRAG_NX_MATCH:
4788 /* Nothing to do. */
4793 if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
4794 ctx->may_set_up_flow = false;
4795 return ctx->odp_actions;
4797 add_sflow_action(ctx);
4798 do_xlate_actions(in, n_in, ctx);
4800 if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
4801 ctx->odp_actions->data,
4802 ctx->odp_actions->size)) {
4803 ctx->may_set_up_flow = false;
4805 && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow,
4807 compose_output_action(ctx, OFPP_LOCAL);
4810 add_mirror_actions(ctx, &orig_flow);
4811 fix_sflow_action(ctx);
4814 return ctx->odp_actions;
4817 /* OFPP_NORMAL implementation. */
4819 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
4821 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
4822 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle',
4823 * the bundle on which the packet was received, returns the VLAN to which the
4826 * Both 'vid' and the return value are in the range 0...4095. */
4828 input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid)
4830 switch (in_bundle->vlan_mode) {
4831 case PORT_VLAN_ACCESS:
4832 return in_bundle->vlan;
4835 case PORT_VLAN_TRUNK:
4838 case PORT_VLAN_NATIVE_UNTAGGED:
4839 case PORT_VLAN_NATIVE_TAGGED:
4840 return vid ? vid : in_bundle->vlan;
4847 /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'.
4848 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
4851 * 'vid' should be the VID obtained from the 802.1Q header that was received as
4852 * part of a packet (specify 0 if there was no 802.1Q header), in the range
4855 input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn)
4857 switch (in_bundle->vlan_mode) {
4858 case PORT_VLAN_ACCESS:
4861 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4862 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged "
4863 "packet received on port %s configured as VLAN "
4864 "%"PRIu16" access port",
4865 in_bundle->ofproto->up.name, vid,
4866 in_bundle->name, in_bundle->vlan);
4872 case PORT_VLAN_NATIVE_UNTAGGED:
4873 case PORT_VLAN_NATIVE_TAGGED:
4875 /* Port must always carry its native VLAN. */
4879 case PORT_VLAN_TRUNK:
4880 if (!ofbundle_includes_vlan(in_bundle, vid)) {
4882 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4883 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet "
4884 "received on port %s not configured for trunking "
4886 in_bundle->ofproto->up.name, vid,
4887 in_bundle->name, vid);
4899 /* Given 'vlan', the VLAN that a packet belongs to, and
4900 * 'out_bundle', a bundle on which the packet is to be output, returns the VID
4901 * that should be included in the 802.1Q header. (If the return value is 0,
4902 * then the 802.1Q header should only be included in the packet if there is a
4905 * Both 'vlan' and the return value are in the range 0...4095. */
4907 output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan)
4909 switch (out_bundle->vlan_mode) {
4910 case PORT_VLAN_ACCESS:
4913 case PORT_VLAN_TRUNK:
4914 case PORT_VLAN_NATIVE_TAGGED:
4917 case PORT_VLAN_NATIVE_UNTAGGED:
4918 return vlan == out_bundle->vlan ? 0 : vlan;
4926 output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle,
4929 struct ofport_dpif *port;
4931 ovs_be16 tci, old_tci;
4933 vid = output_vlan_to_vid(out_bundle, vlan);
4934 if (!out_bundle->bond) {
4935 port = ofbundle_get_a_port(out_bundle);
4937 port = bond_choose_output_slave(out_bundle->bond, &ctx->flow,
4940 /* No slaves enabled, so drop packet. */
4945 old_tci = ctx->flow.vlan_tci;
4947 if (tci || out_bundle->use_priority_tags) {
4948 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
4950 tci |= htons(VLAN_CFI);
4953 ctx->flow.vlan_tci = tci;
4955 compose_output_action(ctx, port->up.ofp_port);
4956 ctx->flow.vlan_tci = old_tci;
4960 mirror_mask_ffs(mirror_mask_t mask)
4962 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
4967 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
4969 return (bundle->vlan_mode != PORT_VLAN_ACCESS
4970 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
4974 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
4976 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
4979 /* Returns an arbitrary interface within 'bundle'. */
4980 static struct ofport_dpif *
4981 ofbundle_get_a_port(const struct ofbundle *bundle)
4983 return CONTAINER_OF(list_front(&bundle->ports),
4984 struct ofport_dpif, bundle_node);
4988 vlan_is_mirrored(const struct ofmirror *m, int vlan)
4990 return !m->vlans || bitmap_is_set(m->vlans, vlan);
4993 /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored
4994 * to a VLAN. In general most packets may be mirrored but we want to drop
4995 * protocols that may confuse switches. */
4997 eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN])
4999 /* If you change this function's behavior, please update corresponding
5000 * documentation in vswitch.xml at the same time. */
5001 if (dst[0] != 0x01) {
5002 /* All the currently banned MACs happen to start with 01 currently, so
5003 * this is a quick way to eliminate most of the good ones. */
5005 if (eth_addr_is_reserved(dst)) {
5006 /* Drop STP, IEEE pause frames, and other reserved protocols
5007 * (01-80-c2-00-00-0x). */
5011 if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) {
5013 if ((dst[3] & 0xfe) == 0xcc &&
5014 (dst[4] & 0xfe) == 0xcc &&
5015 (dst[5] & 0xfe) == 0xcc) {
5016 /* Drop the following protocols plus others following the same
5019 CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc)
5020 Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd)
5021 STP Uplink Fast (01-00-0c-cd-cd-cd) */
5025 if (!(dst[3] | dst[4] | dst[5])) {
5026 /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */
5035 add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
5037 struct ofproto_dpif *ofproto = ctx->ofproto;
5038 mirror_mask_t mirrors;
5039 struct ofport_dpif *in_port;
5040 struct ofbundle *in_bundle;
5043 const struct nlattr *a;
5046 /* Obtain in_port from orig_flow.in_port.
5048 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5049 in_port = lookup_input_bundle(ctx->ofproto, orig_flow->in_port,
5050 ctx->packet != NULL);
5054 in_bundle = in_port->bundle;
5055 mirrors = in_bundle->src_mirrors;
5057 /* Drop frames on bundles reserved for mirroring. */
5058 if (in_bundle->mirror_out) {
5059 if (ctx->packet != NULL) {
5060 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5061 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5062 "%s, which is reserved exclusively for mirroring",
5063 ctx->ofproto->up.name, in_bundle->name);
5069 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
5070 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5073 vlan = input_vid_to_vlan(in_bundle, vid);
5075 /* Look at the output ports to check for destination selections. */
5077 NL_ATTR_FOR_EACH (a, left, ctx->odp_actions->data,
5078 ctx->odp_actions->size) {
5079 enum ovs_action_attr type = nl_attr_type(a);
5080 struct ofport_dpif *ofport;
5082 if (type != OVS_ACTION_ATTR_OUTPUT) {
5086 ofport = get_odp_port(ofproto, nl_attr_get_u32(a));
5087 if (ofport && ofport->bundle) {
5088 mirrors |= ofport->bundle->dst_mirrors;
5096 /* Restore the original packet before adding the mirror actions. */
5097 ctx->flow = *orig_flow;
5102 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5104 if (!vlan_is_mirrored(m, vlan)) {
5105 mirrors &= mirrors - 1;
5109 mirrors &= ~m->dup_mirrors;
5110 ctx->mirrors |= m->dup_mirrors;
5112 output_normal(ctx, m->out, vlan);
5113 } else if (eth_dst_may_rspan(orig_flow->dl_dst)
5114 && vlan != m->out_vlan) {
5115 struct ofbundle *bundle;
5117 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
5118 if (ofbundle_includes_vlan(bundle, m->out_vlan)
5119 && !bundle->mirror_out) {
5120 output_normal(ctx, bundle, m->out_vlan);
5128 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
5129 uint64_t packets, uint64_t bytes)
5135 for (; mirrors; mirrors &= mirrors - 1) {
5138 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5141 /* In normal circumstances 'm' will not be NULL. However,
5142 * if mirrors are reconfigured, we can temporarily get out
5143 * of sync in facet_revalidate(). We could "correct" the
5144 * mirror list before reaching here, but doing that would
5145 * not properly account the traffic stats we've currently
5146 * accumulated for previous mirror configuration. */
5150 m->packet_count += packets;
5151 m->byte_count += bytes;
5155 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
5156 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
5157 * indicate this; newer upstream kernels use gratuitous ARP requests. */
5159 is_gratuitous_arp(const struct flow *flow)
5161 return (flow->dl_type == htons(ETH_TYPE_ARP)
5162 && eth_addr_is_broadcast(flow->dl_dst)
5163 && (flow->nw_proto == ARP_OP_REPLY
5164 || (flow->nw_proto == ARP_OP_REQUEST
5165 && flow->nw_src == flow->nw_dst)));
5169 update_learning_table(struct ofproto_dpif *ofproto,
5170 const struct flow *flow, int vlan,
5171 struct ofbundle *in_bundle)
5173 struct mac_entry *mac;
5175 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
5179 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
5180 if (is_gratuitous_arp(flow)) {
5181 /* We don't want to learn from gratuitous ARP packets that are
5182 * reflected back over bond slaves so we lock the learning table. */
5183 if (!in_bundle->bond) {
5184 mac_entry_set_grat_arp_lock(mac);
5185 } else if (mac_entry_is_grat_arp_locked(mac)) {
5190 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
5191 /* The log messages here could actually be useful in debugging,
5192 * so keep the rate limit relatively high. */
5193 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
5194 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
5195 "on port %s in VLAN %d",
5196 ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
5197 in_bundle->name, vlan);
5199 mac->port.p = in_bundle;
5200 tag_set_add(&ofproto->revalidate_set,
5201 mac_learning_changed(ofproto->ml, mac));
5205 static struct ofport_dpif *
5206 lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn)
5208 struct ofport_dpif *ofport;
5210 /* Find the port and bundle for the received packet. */
5211 ofport = get_ofp_port(ofproto, in_port);
5212 if (ofport && ofport->bundle) {
5216 /* Odd. A few possible reasons here:
5218 * - We deleted a port but there are still a few packets queued up
5221 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
5222 * we don't know about.
5224 * - The ofproto client didn't configure the port as part of a bundle.
5227 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5229 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
5230 "port %"PRIu16, ofproto->up.name, in_port);
5235 /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or
5236 * dropped. Returns true if they may be forwarded, false if they should be
5239 * 'in_port' must be the ofport_dpif that corresponds to flow->in_port.
5240 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
5242 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
5243 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
5244 * checked by input_vid_is_valid().
5246 * May also add tags to '*tags', although the current implementation only does
5247 * so in one special case.
5250 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
5251 struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags)
5253 struct ofbundle *in_bundle = in_port->bundle;
5255 /* Drop frames for reserved multicast addresses
5256 * only if forward_bpdu option is absent. */
5257 if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) {
5261 if (in_bundle->bond) {
5262 struct mac_entry *mac;
5264 switch (bond_check_admissibility(in_bundle->bond, in_port,
5265 flow->dl_dst, tags)) {
5272 case BV_DROP_IF_MOVED:
5273 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
5274 if (mac && mac->port.p != in_bundle &&
5275 (!is_gratuitous_arp(flow)
5276 || mac_entry_is_grat_arp_locked(mac))) {
5287 xlate_normal(struct action_xlate_ctx *ctx)
5289 struct ofport_dpif *in_port;
5290 struct ofbundle *in_bundle;
5291 struct mac_entry *mac;
5295 ctx->has_normal = true;
5297 /* Obtain in_port from ctx->flow.in_port.
5299 * lookup_input_bundle() also ensures that in_port belongs to a bundle. */
5300 in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port,
5301 ctx->packet != NULL);
5305 in_bundle = in_port->bundle;
5307 /* Drop malformed frames. */
5308 if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) &&
5309 !(ctx->flow.vlan_tci & htons(VLAN_CFI))) {
5310 if (ctx->packet != NULL) {
5311 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5312 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
5313 "VLAN tag received on port %s",
5314 ctx->ofproto->up.name, in_bundle->name);
5319 /* Drop frames on bundles reserved for mirroring. */
5320 if (in_bundle->mirror_out) {
5321 if (ctx->packet != NULL) {
5322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5323 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5324 "%s, which is reserved exclusively for mirroring",
5325 ctx->ofproto->up.name, in_bundle->name);
5331 vid = vlan_tci_to_vid(ctx->flow.vlan_tci);
5332 if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) {
5335 vlan = input_vid_to_vlan(in_bundle, vid);
5337 /* Check other admissibility requirements. */
5338 if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) {
5342 /* Learn source MAC. */
5343 if (ctx->may_learn) {
5344 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
5347 /* Determine output bundle. */
5348 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
5351 if (mac->port.p != in_bundle) {
5352 output_normal(ctx, mac->port.p, vlan);
5354 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
5355 /* If we are revalidating but don't have a learning entry then eject
5356 * the flow. Installing a flow that floods packets opens up a window
5357 * of time where we could learn from a packet reflected on a bond and
5358 * blackhole packets before the learning table is updated to reflect
5359 * the correct port. */
5360 ctx->may_set_up_flow = false;
5363 struct ofbundle *bundle;
5365 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
5366 if (bundle != in_bundle
5367 && ofbundle_includes_vlan(bundle, vlan)
5368 && bundle->floodable
5369 && !bundle->mirror_out) {
5370 output_normal(ctx, bundle, vlan);
5373 ctx->nf_output_iface = NF_OUT_FLOOD;
5377 /* Optimized flow revalidation.
5379 * It's a difficult problem, in general, to tell which facets need to have
5380 * their actions recalculated whenever the OpenFlow flow table changes. We
5381 * don't try to solve that general problem: for most kinds of OpenFlow flow
5382 * table changes, we recalculate the actions for every facet. This is
5383 * relatively expensive, but it's good enough if the OpenFlow flow table
5384 * doesn't change very often.
5386 * However, we can expect one particular kind of OpenFlow flow table change to
5387 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5388 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5389 * table, we add a special case that applies to flow tables in which every rule
5390 * has the same form (that is, the same wildcards), except that the table is
5391 * also allowed to have a single "catch-all" flow that matches all packets. We
5392 * optimize this case by tagging all of the facets that resubmit into the table
5393 * and invalidating the same tag whenever a flow changes in that table. The
5394 * end result is that we revalidate just the facets that need it (and sometimes
5395 * a few more, but not all of the facets or even all of the facets that
5396 * resubmit to the table modified by MAC learning). */
5398 /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted
5399 * into an OpenFlow table with the given 'basis'. */
5401 rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc,
5404 if (flow_wildcards_is_catchall(wc)) {
5407 struct flow tag_flow = *flow;
5408 flow_zero_wildcards(&tag_flow, wc);
5409 return tag_create_deterministic(flow_hash(&tag_flow, secret));
5413 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5414 * taggability of that table.
5416 * This function must be called after *each* change to a flow table. If you
5417 * skip calling it on some changes then the pointer comparisons at the end can
5418 * be invalid if you get unlucky. For example, if a flow removal causes a
5419 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5420 * different wildcards to be created with the same address, then this function
5421 * will incorrectly skip revalidation. */
5423 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5425 struct table_dpif *table = &ofproto->tables[table_id];
5426 const struct classifier *cls = &ofproto->up.tables[table_id];
5427 struct cls_table *catchall, *other;
5428 struct cls_table *t;
5430 catchall = other = NULL;
5432 switch (hmap_count(&cls->tables)) {
5434 /* We could tag this OpenFlow table but it would make the logic a
5435 * little harder and it's a corner case that doesn't seem worth it
5441 HMAP_FOR_EACH (t, hmap_node, &cls->tables) {
5442 if (cls_table_is_catchall(t)) {
5444 } else if (!other) {
5447 /* Indicate that we can't tag this by setting both tables to
5448 * NULL. (We know that 'catchall' is already NULL.) */
5455 /* Can't tag this table. */
5459 if (table->catchall_table != catchall || table->other_table != other) {
5460 table->catchall_table = catchall;
5461 table->other_table = other;
5462 ofproto->need_revalidate = true;
5466 /* Given 'rule' that has changed in some way (either it is a rule being
5467 * inserted, a rule being deleted, or a rule whose actions are being
5468 * modified), marks facets for revalidation to ensure that packets will be
5469 * forwarded correctly according to the new state of the flow table.
5471 * This function must be called after *each* change to a flow table. See
5472 * the comment on table_update_taggable() for more information. */
5474 rule_invalidate(const struct rule_dpif *rule)
5476 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5478 table_update_taggable(ofproto, rule->up.table_id);
5480 if (!ofproto->need_revalidate) {
5481 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5483 if (table->other_table && rule->tag) {
5484 tag_set_add(&ofproto->revalidate_set, rule->tag);
5486 ofproto->need_revalidate = true;
5492 set_frag_handling(struct ofproto *ofproto_,
5493 enum ofp_config_flags frag_handling)
5495 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5497 if (frag_handling != OFPC_FRAG_REASM) {
5498 ofproto->need_revalidate = true;
5506 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5507 const struct flow *flow,
5508 const union ofp_action *ofp_actions, size_t n_ofp_actions)
5510 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5513 if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) {
5514 return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT);
5517 error = validate_actions(ofp_actions, n_ofp_actions, flow,
5518 ofproto->max_ports);
5520 struct odputil_keybuf keybuf;
5521 struct action_xlate_ctx ctx;
5522 struct ofpbuf *odp_actions;
5525 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5526 odp_flow_key_from_flow(&key, flow);
5528 action_xlate_ctx_init(&ctx, ofproto, flow, flow->vlan_tci, packet);
5529 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
5530 dpif_execute(ofproto->dpif, key.data, key.size,
5531 odp_actions->data, odp_actions->size, packet);
5532 ofpbuf_delete(odp_actions);
5540 set_netflow(struct ofproto *ofproto_,
5541 const struct netflow_options *netflow_options)
5543 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5545 if (netflow_options) {
5546 if (!ofproto->netflow) {
5547 ofproto->netflow = netflow_create();
5549 return netflow_set_options(ofproto->netflow, netflow_options);
5551 netflow_destroy(ofproto->netflow);
5552 ofproto->netflow = NULL;
5558 get_netflow_ids(const struct ofproto *ofproto_,
5559 uint8_t *engine_type, uint8_t *engine_id)
5561 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5563 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
5567 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5569 if (!facet_is_controller_flow(facet) &&
5570 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5571 struct subfacet *subfacet;
5572 struct ofexpired expired;
5574 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5575 if (subfacet->installed) {
5576 struct dpif_flow_stats stats;
5578 subfacet_install(ofproto, subfacet, subfacet->actions,
5579 subfacet->actions_len, &stats);
5580 subfacet_update_stats(ofproto, subfacet, &stats);
5584 expired.flow = facet->flow;
5585 expired.packet_count = facet->packet_count;
5586 expired.byte_count = facet->byte_count;
5587 expired.used = facet->used;
5588 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5593 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5595 struct facet *facet;
5597 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5598 send_active_timeout(ofproto, facet);
5602 static struct ofproto_dpif *
5603 ofproto_dpif_lookup(const char *name)
5605 struct ofproto_dpif *ofproto;
5607 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
5608 hash_string(name, 0), &all_ofproto_dpifs) {
5609 if (!strcmp(ofproto->up.name, name)) {
5617 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
5618 const char *argv[], void *aux OVS_UNUSED)
5620 const struct ofproto_dpif *ofproto;
5622 ofproto = ofproto_dpif_lookup(argv[1]);
5624 unixctl_command_reply(conn, 501, "no such bridge");
5627 mac_learning_flush(ofproto->ml);
5629 unixctl_command_reply(conn, 200, "table successfully flushed");
5633 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5634 const char *argv[], void *aux OVS_UNUSED)
5636 struct ds ds = DS_EMPTY_INITIALIZER;
5637 const struct ofproto_dpif *ofproto;
5638 const struct mac_entry *e;
5640 ofproto = ofproto_dpif_lookup(argv[1]);
5642 unixctl_command_reply(conn, 501, "no such bridge");
5646 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5647 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5648 struct ofbundle *bundle = e->port.p;
5649 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5650 ofbundle_get_a_port(bundle)->odp_port,
5651 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5653 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5657 struct ofproto_trace {
5658 struct action_xlate_ctx ctx;
5664 trace_format_rule(struct ds *result, uint8_t table_id, int level,
5665 const struct rule_dpif *rule)
5667 ds_put_char_multiple(result, '\t', level);
5669 ds_put_cstr(result, "No match\n");
5673 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5674 table_id, ntohll(rule->up.flow_cookie));
5675 cls_rule_format(&rule->up.cr, result);
5676 ds_put_char(result, '\n');
5678 ds_put_char_multiple(result, '\t', level);
5679 ds_put_cstr(result, "OpenFlow ");
5680 ofp_print_actions(result, rule->up.actions, rule->up.n_actions);
5681 ds_put_char(result, '\n');
5685 trace_format_flow(struct ds *result, int level, const char *title,
5686 struct ofproto_trace *trace)
5688 ds_put_char_multiple(result, '\t', level);
5689 ds_put_format(result, "%s: ", title);
5690 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5691 ds_put_cstr(result, "unchanged");
5693 flow_format(result, &trace->ctx.flow);
5694 trace->flow = trace->ctx.flow;
5696 ds_put_char(result, '\n');
5700 trace_format_regs(struct ds *result, int level, const char *title,
5701 struct ofproto_trace *trace)
5705 ds_put_char_multiple(result, '\t', level);
5706 ds_put_format(result, "%s:", title);
5707 for (i = 0; i < FLOW_N_REGS; i++) {
5708 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5710 ds_put_char(result, '\n');
5714 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
5716 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5717 struct ds *result = trace->result;
5719 ds_put_char(result, '\n');
5720 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5721 trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace);
5722 trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule);
5726 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
5727 void *aux OVS_UNUSED)
5729 const char *dpname = argv[1];
5730 struct ofproto_dpif *ofproto;
5731 struct ofpbuf odp_key;
5732 struct ofpbuf *packet;
5733 struct rule_dpif *rule;
5734 ovs_be16 initial_tci;
5740 ofpbuf_init(&odp_key, 0);
5743 ofproto = ofproto_dpif_lookup(dpname);
5745 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5749 if (argc == 3 || (argc == 4 && !strcmp(argv[3], "-generate"))) {
5750 /* ofproto/trace dpname flow [-generate] */
5751 const char *flow_s = argv[2];
5752 const char *generate_s = argv[3];
5755 /* Convert string to datapath key. */
5756 ofpbuf_init(&odp_key, 0);
5757 error = odp_flow_key_from_string(flow_s, NULL, &odp_key);
5759 unixctl_command_reply(conn, 501, "Bad flow syntax");
5763 /* Convert odp_key to flow. */
5764 error = ofproto_dpif_extract_flow_key(ofproto, odp_key.data,
5765 odp_key.size, &flow,
5766 &initial_tci, NULL);
5767 if (error == ODP_FIT_ERROR) {
5768 unixctl_command_reply(conn, 501, "Invalid flow");
5772 /* Generate a packet, if requested. */
5774 packet = ofpbuf_new(0);
5775 flow_compose(packet, &flow);
5777 } else if (argc == 6) {
5778 /* ofproto/trace dpname priority tun_id in_port packet */
5779 const char *priority_s = argv[2];
5780 const char *tun_id_s = argv[3];
5781 const char *in_port_s = argv[4];
5782 const char *packet_s = argv[5];
5783 uint16_t in_port = ofp_port_to_odp_port(atoi(in_port_s));
5784 ovs_be64 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
5785 uint32_t priority = atoi(priority_s);
5788 msg = eth_from_hex(packet_s, &packet);
5790 unixctl_command_reply(conn, 501, msg);
5794 ds_put_cstr(&result, "Packet: ");
5795 s = ofp_packet_to_string(packet->data, packet->size, packet->size);
5796 ds_put_cstr(&result, s);
5799 flow_extract(packet, priority, tun_id, in_port, &flow);
5800 initial_tci = flow.vlan_tci;
5802 unixctl_command_reply(conn, 501, "Bad command syntax");
5806 ds_put_cstr(&result, "Flow: ");
5807 flow_format(&result, &flow);
5808 ds_put_char(&result, '\n');
5810 rule = rule_dpif_lookup(ofproto, &flow, 0);
5811 trace_format_rule(&result, 0, 0, rule);
5813 struct ofproto_trace trace;
5814 struct ofpbuf *odp_actions;
5816 trace.result = &result;
5818 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, initial_tci, packet);
5819 trace.ctx.resubmit_hook = trace_resubmit;
5820 odp_actions = xlate_actions(&trace.ctx,
5821 rule->up.actions, rule->up.n_actions);
5823 ds_put_char(&result, '\n');
5824 trace_format_flow(&result, 0, "Final flow", &trace);
5825 ds_put_cstr(&result, "Datapath actions: ");
5826 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5827 ofpbuf_delete(odp_actions);
5829 if (!trace.ctx.may_set_up_flow) {
5831 ds_put_cstr(&result, "\nThis flow is not cachable.");
5833 ds_put_cstr(&result, "\nThe datapath actions are incomplete--"
5834 "for complete actions, please supply a packet.");
5839 unixctl_command_reply(conn, 200, ds_cstr(&result));
5842 ds_destroy(&result);
5843 ofpbuf_delete(packet);
5844 ofpbuf_uninit(&odp_key);
5848 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5849 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5852 unixctl_command_reply(conn, 200, NULL);
5856 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5857 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5860 unixctl_command_reply(conn, 200, NULL);
5864 ofproto_dpif_unixctl_init(void)
5866 static bool registered;
5872 unixctl_command_register(
5874 "bridge {tun_id in_port packet | odp_flow [-generate]}",
5875 2, 4, ofproto_unixctl_trace, NULL);
5876 unixctl_command_register("fdb/flush", "bridge", 1, 1,
5877 ofproto_unixctl_fdb_flush, NULL);
5878 unixctl_command_register("fdb/show", "bridge", 1, 1,
5879 ofproto_unixctl_fdb_show, NULL);
5880 unixctl_command_register("ofproto/clog", "", 0, 0,
5881 ofproto_dpif_clog, NULL);
5882 unixctl_command_register("ofproto/unclog", "", 0, 0,
5883 ofproto_dpif_unclog, NULL);
5886 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5888 * This is deprecated. It is only for compatibility with broken device drivers
5889 * in old versions of Linux that do not properly support VLANs when VLAN
5890 * devices are not used. When broken device drivers are no longer in
5891 * widespread use, we will delete these interfaces. */
5894 set_realdev(struct ofport *ofport_, uint16_t realdev_ofp_port, int vid)
5896 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5897 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5899 if (realdev_ofp_port == ofport->realdev_ofp_port
5900 && vid == ofport->vlandev_vid) {
5904 ofproto->need_revalidate = true;
5906 if (ofport->realdev_ofp_port) {
5909 if (realdev_ofp_port && ofport->bundle) {
5910 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5911 * themselves be part of a bundle. */
5912 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
5915 ofport->realdev_ofp_port = realdev_ofp_port;
5916 ofport->vlandev_vid = vid;
5918 if (realdev_ofp_port) {
5919 vsp_add(ofport, realdev_ofp_port, vid);
5926 hash_realdev_vid(uint16_t realdev_ofp_port, int vid)
5928 return hash_2words(realdev_ofp_port, vid);
5932 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5933 uint32_t realdev_odp_port, ovs_be16 vlan_tci)
5935 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5936 uint16_t realdev_ofp_port = odp_port_to_ofp_port(realdev_odp_port);
5937 int vid = vlan_tci_to_vid(vlan_tci);
5938 const struct vlan_splinter *vsp;
5940 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5941 hash_realdev_vid(realdev_ofp_port, vid),
5942 &ofproto->realdev_vid_map) {
5943 if (vsp->realdev_ofp_port == realdev_ofp_port
5944 && vsp->vid == vid) {
5945 return ofp_port_to_odp_port(vsp->vlandev_ofp_port);
5949 return realdev_odp_port;
5952 static struct vlan_splinter *
5953 vlandev_find(const struct ofproto_dpif *ofproto, uint16_t vlandev_ofp_port)
5955 struct vlan_splinter *vsp;
5957 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node, hash_int(vlandev_ofp_port, 0),
5958 &ofproto->vlandev_map) {
5959 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5968 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5969 uint16_t vlandev_ofp_port, int *vid)
5971 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5972 const struct vlan_splinter *vsp;
5974 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5979 return vsp->realdev_ofp_port;
5986 vsp_remove(struct ofport_dpif *port)
5988 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5989 struct vlan_splinter *vsp;
5991 vsp = vlandev_find(ofproto, port->up.ofp_port);
5993 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5994 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5997 port->realdev_ofp_port = 0;
5999 VLOG_ERR("missing vlan device record");
6004 vsp_add(struct ofport_dpif *port, uint16_t realdev_ofp_port, int vid)
6006 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6008 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
6009 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
6010 == realdev_ofp_port)) {
6011 struct vlan_splinter *vsp;
6013 vsp = xmalloc(sizeof *vsp);
6014 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
6015 hash_int(port->up.ofp_port, 0));
6016 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
6017 hash_realdev_vid(realdev_ofp_port, vid));
6018 vsp->realdev_ofp_port = realdev_ofp_port;
6019 vsp->vlandev_ofp_port = port->up.ofp_port;
6022 port->realdev_ofp_port = realdev_ofp_port;
6024 VLOG_ERR("duplicate vlan device record");
6028 const struct ofproto_class ofproto_dpif_class = {
6057 port_is_lacp_current,
6058 NULL, /* rule_choose_table */
6065 rule_modify_actions,
6073 get_cfm_remote_mpids,
6077 get_stp_port_status,
6084 is_mirror_output_bundle,
6085 forward_bpdu_changed,