2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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.
23 #include "byte-order.h"
24 #include "connectivity.h"
29 #include "fail-open.h"
30 #include "guarded-list.h"
34 #include "mac-learning.h"
35 #include "mcast-snooping.h"
36 #include "multipath.h"
37 #include "netdev-vport.h"
42 #include "odp-execute.h"
43 #include "ofproto/ofproto-dpif.h"
44 #include "ofproto/ofproto-provider.h"
45 #include "ofproto-dpif-ipfix.h"
46 #include "ofproto-dpif-mirror.h"
47 #include "ofproto-dpif-monitor.h"
48 #include "ofproto-dpif-rid.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "ofproto-dpif-upcall.h"
51 #include "ofproto-dpif-xlate.h"
52 #include "openvswitch/ofp-actions.h"
53 #include "openvswitch/dynamic-string.h"
54 #include "openvswitch/meta-flow.h"
55 #include "openvswitch/ofp-parse.h"
56 #include "openvswitch/ofp-print.h"
57 #include "openvswitch/ofp-util.h"
58 #include "openvswitch/ofpbuf.h"
59 #include "openvswitch/vlog.h"
62 #include "ovs-router.h"
63 #include "poll-loop.h"
69 #include "unaligned.h"
72 #include "vlan-bitmap.h"
74 VLOG_DEFINE_THIS_MODULE(ofproto_dpif
);
76 COVERAGE_DEFINE(ofproto_dpif_expired
);
77 COVERAGE_DEFINE(packet_in_overflow
);
86 * - Do include packets and bytes from datapath flows which have not
87 * recently been processed by a revalidator. */
88 struct ovs_mutex stats_mutex
;
89 struct dpif_flow_stats stats OVS_GUARDED
;
91 /* In non-NULL, will point to a new rule (for which a reference is held) to
92 * which all the stats updates should be forwarded. This exists only
93 * transitionally when flows are replaced.
95 * Protected by stats_mutex. If both 'rule->stats_mutex' and
96 * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
98 struct rule_dpif
*new_rule OVS_GUARDED
;
100 /* If non-zero then the recirculation id that has
101 * been allocated for use with this rule.
102 * The recirculation id and associated internal flow should
103 * be freed when the rule is freed */
107 /* RULE_CAST() depends on this. */
108 BUILD_ASSERT_DECL(offsetof(struct rule_dpif
, up
) == 0);
110 static void rule_get_stats(struct rule
*, uint64_t *packets
, uint64_t *bytes
,
111 long long int *used
);
112 static struct rule_dpif
*rule_dpif_cast(const struct rule
*);
113 static void rule_expire(struct rule_dpif
*, long long now
);
120 * - Do include packets and bytes from datapath flows which have not
121 * recently been processed by a revalidator. */
122 struct ovs_mutex stats_mutex
;
123 uint64_t packet_count OVS_GUARDED
; /* Number of packets received. */
124 uint64_t byte_count OVS_GUARDED
; /* Number of bytes received. */
128 struct hmap_node hmap_node
; /* In struct ofproto's "bundles" hmap. */
129 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
130 void *aux
; /* Key supplied by ofproto's client. */
131 char *name
; /* Identifier for log messages. */
134 struct ovs_list ports
; /* Contains "struct ofport"s. */
135 enum port_vlan_mode vlan_mode
; /* VLAN mode */
136 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
137 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
138 * NULL if all VLANs are trunked. */
139 struct lacp
*lacp
; /* LACP if LACP is enabled, otherwise NULL. */
140 struct bond
*bond
; /* Nonnull iff more than one port. */
141 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
144 bool floodable
; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
147 static void bundle_remove(struct ofport
*);
148 static void bundle_update(struct ofbundle
*);
149 static void bundle_destroy(struct ofbundle
*);
150 static void bundle_del_port(struct ofport_dpif
*);
151 static void bundle_run(struct ofbundle
*);
152 static void bundle_wait(struct ofbundle
*);
153 static void bundle_flush_macs(struct ofbundle
*, bool);
154 static void bundle_move(struct ofbundle
*, struct ofbundle
*);
156 static void stp_run(struct ofproto_dpif
*ofproto
);
157 static void stp_wait(struct ofproto_dpif
*ofproto
);
158 static int set_stp_port(struct ofport
*,
159 const struct ofproto_port_stp_settings
*);
161 static void rstp_run(struct ofproto_dpif
*ofproto
);
162 static void set_rstp_port(struct ofport
*,
163 const struct ofproto_port_rstp_settings
*);
166 struct hmap_node odp_port_node
; /* In dpif_backer's "odp_to_ofport_map". */
170 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
171 struct ovs_list bundle_node
;/* In struct ofbundle's "ports" list. */
172 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
173 struct bfd
*bfd
; /* BFD, if any. */
174 struct lldp
*lldp
; /* lldp, if any. */
175 bool may_enable
; /* May be enabled in bonds. */
176 bool is_tunnel
; /* This port is a tunnel. */
177 bool is_layer3
; /* This is a layer 3 port. */
178 long long int carrier_seq
; /* Carrier status changes. */
179 struct ofport_dpif
*peer
; /* Peer if patch port. */
182 struct stp_port
*stp_port
; /* Spanning Tree Protocol, if any. */
183 enum stp_state stp_state
; /* Always STP_DISABLED if STP not in use. */
184 long long int stp_state_entered
;
186 /* Rapid Spanning Tree. */
187 struct rstp_port
*rstp_port
; /* Rapid Spanning Tree Protocol, if any. */
188 enum rstp_state rstp_state
; /* Always RSTP_DISABLED if RSTP not in use. */
190 /* Queue to DSCP mapping. */
191 struct ofproto_port_queue
*qdscp
;
195 static odp_port_t
ofp_port_to_odp_port(const struct ofproto_dpif
*,
198 static ofp_port_t
odp_port_to_ofp_port(const struct ofproto_dpif
*,
201 static struct ofport_dpif
*
202 ofport_dpif_cast(const struct ofport
*ofport
)
204 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
207 static void port_run(struct ofport_dpif
*);
208 static int set_bfd(struct ofport
*, const struct smap
*);
209 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
210 static int set_lldp(struct ofport
*ofport_
, const struct smap
*cfg
);
211 static void ofport_update_peer(struct ofport_dpif
*);
213 /* Reasons that we might need to revalidate every datapath flow, and
214 * corresponding coverage counters.
216 * A value of 0 means that there is no need to revalidate.
218 * It would be nice to have some cleaner way to integrate with coverage
219 * counters, but with only a few reasons I guess this is good enough for
221 enum revalidate_reason
{
222 REV_RECONFIGURE
= 1, /* Switch configuration changed. */
223 REV_STP
, /* Spanning tree protocol port status change. */
224 REV_RSTP
, /* RSTP port status change. */
225 REV_BOND
, /* Bonding changed. */
226 REV_PORT_TOGGLED
, /* Port enabled or disabled by CFM, LACP, ...*/
227 REV_FLOW_TABLE
, /* Flow table changed. */
228 REV_MAC_LEARNING
, /* Mac learning changed. */
229 REV_MCAST_SNOOPING
, /* Multicast snooping changed. */
231 COVERAGE_DEFINE(rev_reconfigure
);
232 COVERAGE_DEFINE(rev_stp
);
233 COVERAGE_DEFINE(rev_rstp
);
234 COVERAGE_DEFINE(rev_bond
);
235 COVERAGE_DEFINE(rev_port_toggled
);
236 COVERAGE_DEFINE(rev_flow_table
);
237 COVERAGE_DEFINE(rev_mac_learning
);
238 COVERAGE_DEFINE(rev_mcast_snooping
);
240 /* All datapaths of a given type share a single dpif backer instance. */
247 struct ovs_rwlock odp_to_ofport_lock
;
248 struct hmap odp_to_ofport_map OVS_GUARDED
; /* Contains "struct ofport"s. */
250 struct simap tnl_backers
; /* Set of dpif ports backing tunnels. */
252 enum revalidate_reason need_revalidate
; /* Revalidate all flows. */
254 bool recv_set_enable
; /* Enables or disables receiving packets. */
256 /* Version string of the datapath stored in OVSDB. */
257 char *dp_version_string
;
259 /* Datapath feature support. */
260 struct dpif_backer_support support
;
261 struct atomic_count tnl_count
;
264 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
265 static struct shash all_dpif_backers
= SHASH_INITIALIZER(&all_dpif_backers
);
267 struct ofproto_dpif
{
268 struct hmap_node all_ofproto_dpifs_node
; /* In 'all_ofproto_dpifs'. */
270 struct dpif_backer
*backer
;
272 /* Unique identifier for this instantiation of this bridge in this running
276 ATOMIC(ovs_version_t
) tables_version
; /* For classifier lookups. */
278 uint64_t dump_seq
; /* Last read of udpif_dump_seq(). */
280 /* Special OpenFlow rules. */
281 struct rule_dpif
*miss_rule
; /* Sends flow table misses to controller. */
282 struct rule_dpif
*no_packet_in_rule
; /* Drops flow table misses. */
283 struct rule_dpif
*drop_frags_rule
; /* Used in OFPUTIL_FRAG_DROP mode. */
286 struct netflow
*netflow
;
287 struct dpif_sflow
*sflow
;
288 struct dpif_ipfix
*ipfix
;
289 struct hmap bundles
; /* Contains "struct ofbundle"s. */
290 struct mac_learning
*ml
;
291 struct mcast_snooping
*ms
;
292 bool has_bonded_bundles
;
294 struct mbridge
*mbridge
;
296 struct ovs_mutex stats_mutex
;
297 struct netdev_stats stats OVS_GUARDED
; /* To account packets generated and
298 * consumed in userspace. */
302 long long int stp_last_tick
;
304 /* Rapid Spanning Tree. */
306 long long int rstp_last_tick
;
309 struct sset ports
; /* Set of standard port names. */
310 struct sset ghost_ports
; /* Ports with no datapath port. */
311 struct sset port_poll_set
; /* Queued names for port_poll() reply. */
312 int port_poll_errno
; /* Last errno for port_poll() reply. */
313 uint64_t change_seq
; /* Connectivity status changes. */
316 struct guarded_list ams
; /* Contains "struct ofproto_async_msgs"s. */
317 struct seq
*ams_seq
; /* For notifying 'ams' reception. */
321 /* All existing ofproto_dpif instances, indexed by ->up.name. */
322 static struct hmap all_ofproto_dpifs
= HMAP_INITIALIZER(&all_ofproto_dpifs
);
324 static bool ofproto_use_tnl_push_pop
= true;
325 static void ofproto_unixctl_init(void);
327 static inline struct ofproto_dpif
*
328 ofproto_dpif_cast(const struct ofproto
*ofproto
)
330 ovs_assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
331 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
335 ofproto_dpif_get_enable_ufid(const struct dpif_backer
*backer
)
337 return backer
->support
.ufid
;
340 struct dpif_backer_support
*
341 ofproto_dpif_get_support(const struct ofproto_dpif
*ofproto
)
343 return &ofproto
->backer
->support
;
346 static void ofproto_trace(struct ofproto_dpif
*, struct flow
*,
347 const struct dp_packet
*packet
,
348 const struct ofpact
[], size_t ofpacts_len
,
351 /* Global variables. */
352 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
354 /* Initial mappings of port to bridge mappings. */
355 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
357 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
360 ofproto_dpif_flow_mod(struct ofproto_dpif
*ofproto
,
361 const struct ofputil_flow_mod
*fm
)
363 struct ofproto_flow_mod ofm
;
365 /* Multiple threads may do this for the same 'fm' at the same time.
366 * Allocate ofproto_flow_mod with execution context from stack.
368 * Note: This copy could be avoided by making ofproto_flow_mod more
369 * complex, but that may not be desireable, and a learn action is not that
370 * fast to begin with. */
372 ofproto_flow_mod(&ofproto
->up
, &ofm
);
375 /* Appends 'am' to the queue of asynchronous messages to be sent to the
376 * controller. Takes ownership of 'am' and any data it points to. */
378 ofproto_dpif_send_async_msg(struct ofproto_dpif
*ofproto
,
379 struct ofproto_async_msg
*am
)
381 if (!guarded_list_push_back(&ofproto
->ams
, &am
->list_node
, 1024)) {
382 COVERAGE_INC(packet_in_overflow
);
383 ofproto_async_msg_free(am
);
386 /* Wakes up main thread for packet-in I/O. */
387 seq_change(ofproto
->ams_seq
);
390 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
391 * packet rather than to send the packet to the controller.
393 * This function returns false to indicate that a packet_in message
394 * for a "table-miss" should be sent to at least one controller.
395 * False otherwise. */
397 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif
*ofproto
)
399 return connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
);
402 /* Factory functions. */
405 init(const struct shash
*iface_hints
)
407 struct shash_node
*node
;
409 /* Make a local copy, since we don't own 'iface_hints' elements. */
410 SHASH_FOR_EACH(node
, iface_hints
) {
411 const struct iface_hint
*orig_hint
= node
->data
;
412 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
414 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
415 new_hint
->br_type
= xstrdup(orig_hint
->br_type
);
416 new_hint
->ofp_port
= orig_hint
->ofp_port
;
418 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
421 ofproto_unixctl_init();
426 enumerate_types(struct sset
*types
)
428 dp_enumerate_types(types
);
432 enumerate_names(const char *type
, struct sset
*names
)
434 struct ofproto_dpif
*ofproto
;
437 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
438 if (strcmp(type
, ofproto
->up
.type
)) {
441 sset_add(names
, ofproto
->up
.name
);
448 del(const char *type
, const char *name
)
453 error
= dpif_open(name
, type
, &dpif
);
455 error
= dpif_delete(dpif
);
462 port_open_type(const char *datapath_type
, const char *port_type
)
464 return dpif_port_open_type(datapath_type
, port_type
);
467 /* Type functions. */
469 static void process_dpif_port_changes(struct dpif_backer
*);
470 static void process_dpif_all_ports_changed(struct dpif_backer
*);
471 static void process_dpif_port_change(struct dpif_backer
*,
472 const char *devname
);
473 static void process_dpif_port_error(struct dpif_backer
*, int error
);
475 static struct ofproto_dpif
*
476 lookup_ofproto_dpif_by_port_name(const char *name
)
478 struct ofproto_dpif
*ofproto
;
480 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
481 if (sset_contains(&ofproto
->ports
, name
)) {
490 ofproto_dpif_backer_enabled(struct dpif_backer
* backer
)
492 return backer
->recv_set_enable
;
496 type_run(const char *type
)
498 struct dpif_backer
*backer
;
500 backer
= shash_find_data(&all_dpif_backers
, type
);
502 /* This is not necessarily a problem, since backers are only
503 * created on demand. */
507 /* This must be called before dpif_run() */
508 dpif_poll_threads_set(backer
->dpif
, pmd_cpu_mask
);
510 if (dpif_run(backer
->dpif
)) {
511 backer
->need_revalidate
= REV_RECONFIGURE
;
514 udpif_run(backer
->udpif
);
516 /* If vswitchd started with other_config:flow_restore_wait set as "true",
517 * and the configuration has now changed to "false", enable receiving
518 * packets from the datapath. */
519 if (!backer
->recv_set_enable
&& !ofproto_get_flow_restore_wait()) {
522 backer
->recv_set_enable
= true;
524 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
526 VLOG_ERR("Failed to enable receiving packets in dpif.");
529 dpif_flow_flush(backer
->dpif
);
530 backer
->need_revalidate
= REV_RECONFIGURE
;
533 if (backer
->recv_set_enable
) {
534 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
537 if (backer
->need_revalidate
) {
538 struct ofproto_dpif
*ofproto
;
539 struct simap_node
*node
;
540 struct simap tmp_backers
;
542 /* Handle tunnel garbage collection. */
543 simap_init(&tmp_backers
);
544 simap_swap(&backer
->tnl_backers
, &tmp_backers
);
546 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
547 struct ofport_dpif
*iter
;
549 if (backer
!= ofproto
->backer
) {
553 HMAP_FOR_EACH (iter
, up
.hmap_node
, &ofproto
->up
.ports
) {
554 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
557 if (!iter
->is_tunnel
) {
561 dp_port
= netdev_vport_get_dpif_port(iter
->up
.netdev
,
562 namebuf
, sizeof namebuf
);
563 node
= simap_find(&tmp_backers
, dp_port
);
565 simap_put(&backer
->tnl_backers
, dp_port
, node
->data
);
566 simap_delete(&tmp_backers
, node
);
567 node
= simap_find(&backer
->tnl_backers
, dp_port
);
569 node
= simap_find(&backer
->tnl_backers
, dp_port
);
571 odp_port_t odp_port
= ODPP_NONE
;
573 if (!dpif_port_add(backer
->dpif
, iter
->up
.netdev
,
575 simap_put(&backer
->tnl_backers
, dp_port
,
576 odp_to_u32(odp_port
));
577 node
= simap_find(&backer
->tnl_backers
, dp_port
);
582 iter
->odp_port
= node
? u32_to_odp(node
->data
) : ODPP_NONE
;
583 if (tnl_port_reconfigure(iter
, iter
->up
.netdev
,
585 ovs_native_tunneling_is_on(ofproto
), dp_port
)) {
586 backer
->need_revalidate
= REV_RECONFIGURE
;
591 SIMAP_FOR_EACH (node
, &tmp_backers
) {
592 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
));
594 simap_destroy(&tmp_backers
);
596 switch (backer
->need_revalidate
) {
597 case REV_RECONFIGURE
: COVERAGE_INC(rev_reconfigure
); break;
598 case REV_STP
: COVERAGE_INC(rev_stp
); break;
599 case REV_RSTP
: COVERAGE_INC(rev_rstp
); break;
600 case REV_BOND
: COVERAGE_INC(rev_bond
); break;
601 case REV_PORT_TOGGLED
: COVERAGE_INC(rev_port_toggled
); break;
602 case REV_FLOW_TABLE
: COVERAGE_INC(rev_flow_table
); break;
603 case REV_MAC_LEARNING
: COVERAGE_INC(rev_mac_learning
); break;
604 case REV_MCAST_SNOOPING
: COVERAGE_INC(rev_mcast_snooping
); break;
606 backer
->need_revalidate
= 0;
608 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
609 struct ofport_dpif
*ofport
;
610 struct ofbundle
*bundle
;
612 if (ofproto
->backer
!= backer
) {
617 xlate_ofproto_set(ofproto
, ofproto
->up
.name
,
618 ofproto
->backer
->dpif
, ofproto
->ml
,
619 ofproto
->stp
, ofproto
->rstp
, ofproto
->ms
,
620 ofproto
->mbridge
, ofproto
->sflow
, ofproto
->ipfix
,
622 ofproto
->up
.forward_bpdu
,
623 connmgr_has_in_band(ofproto
->up
.connmgr
),
624 &ofproto
->backer
->support
);
626 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
627 xlate_bundle_set(ofproto
, bundle
, bundle
->name
,
628 bundle
->vlan_mode
, bundle
->vlan
,
629 bundle
->trunks
, bundle
->use_priority_tags
,
630 bundle
->bond
, bundle
->lacp
,
634 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
635 int stp_port
= ofport
->stp_port
636 ? stp_port_no(ofport
->stp_port
)
638 xlate_ofport_set(ofproto
, ofport
->bundle
, ofport
,
639 ofport
->up
.ofp_port
, ofport
->odp_port
,
640 ofport
->up
.netdev
, ofport
->cfm
, ofport
->bfd
,
641 ofport
->lldp
, ofport
->peer
, stp_port
,
642 ofport
->rstp_port
, ofport
->qdscp
,
643 ofport
->n_qdscp
, ofport
->up
.pp
.config
,
644 ofport
->up
.pp
.state
, ofport
->is_tunnel
,
650 udpif_revalidate(backer
->udpif
);
653 process_dpif_port_changes(backer
);
658 /* Check for and handle port changes in 'backer''s dpif. */
660 process_dpif_port_changes(struct dpif_backer
*backer
)
666 error
= dpif_port_poll(backer
->dpif
, &devname
);
672 process_dpif_all_ports_changed(backer
);
676 process_dpif_port_change(backer
, devname
);
681 process_dpif_port_error(backer
, error
);
688 process_dpif_all_ports_changed(struct dpif_backer
*backer
)
690 struct ofproto_dpif
*ofproto
;
691 struct dpif_port dpif_port
;
692 struct dpif_port_dump dump
;
693 struct sset devnames
;
696 sset_init(&devnames
);
697 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
698 if (ofproto
->backer
== backer
) {
699 struct ofport
*ofport
;
701 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
702 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
706 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, backer
->dpif
) {
707 sset_add(&devnames
, dpif_port
.name
);
710 SSET_FOR_EACH (devname
, &devnames
) {
711 process_dpif_port_change(backer
, devname
);
713 sset_destroy(&devnames
);
717 process_dpif_port_change(struct dpif_backer
*backer
, const char *devname
)
719 struct ofproto_dpif
*ofproto
;
720 struct dpif_port port
;
722 /* Don't report on the datapath's device. */
723 if (!strcmp(devname
, dpif_base_name(backer
->dpif
))) {
727 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
,
728 &all_ofproto_dpifs
) {
729 if (simap_contains(&ofproto
->backer
->tnl_backers
, devname
)) {
734 ofproto
= lookup_ofproto_dpif_by_port_name(devname
);
735 if (dpif_port_query_by_name(backer
->dpif
, devname
, &port
)) {
736 /* The port was removed. If we know the datapath,
737 * report it through poll_set(). If we don't, it may be
738 * notifying us of a removal we initiated, so ignore it.
739 * If there's a pending ENOBUFS, let it stand, since
740 * everything will be reevaluated. */
741 if (ofproto
&& ofproto
->port_poll_errno
!= ENOBUFS
) {
742 sset_add(&ofproto
->port_poll_set
, devname
);
743 ofproto
->port_poll_errno
= 0;
745 } else if (!ofproto
) {
746 /* The port was added, but we don't know with which
747 * ofproto we should associate it. Delete it. */
748 dpif_port_del(backer
->dpif
, port
.port_no
);
750 struct ofport_dpif
*ofport
;
752 ofport
= ofport_dpif_cast(shash_find_data(
753 &ofproto
->up
.port_by_name
, devname
));
755 && ofport
->odp_port
!= port
.port_no
756 && !odp_port_to_ofport(backer
, port
.port_no
))
758 /* 'ofport''s datapath port number has changed from
759 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
760 * structures to match. */
761 ovs_rwlock_wrlock(&backer
->odp_to_ofport_lock
);
762 hmap_remove(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
);
763 ofport
->odp_port
= port
.port_no
;
764 hmap_insert(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
,
765 hash_odp_port(port
.port_no
));
766 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
767 backer
->need_revalidate
= REV_RECONFIGURE
;
770 dpif_port_destroy(&port
);
773 /* Propagate 'error' to all ofprotos based on 'backer'. */
775 process_dpif_port_error(struct dpif_backer
*backer
, int error
)
777 struct ofproto_dpif
*ofproto
;
779 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
780 if (ofproto
->backer
== backer
) {
781 sset_clear(&ofproto
->port_poll_set
);
782 ofproto
->port_poll_errno
= error
;
788 type_wait(const char *type
)
790 struct dpif_backer
*backer
;
792 backer
= shash_find_data(&all_dpif_backers
, type
);
794 /* This is not necessarily a problem, since backers are only
795 * created on demand. */
799 dpif_wait(backer
->dpif
);
802 /* Basic life-cycle. */
804 static int add_internal_flows(struct ofproto_dpif
*);
806 static struct ofproto
*
809 struct ofproto_dpif
*ofproto
= xzalloc(sizeof *ofproto
);
814 dealloc(struct ofproto
*ofproto_
)
816 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
821 close_dpif_backer(struct dpif_backer
*backer
)
823 ovs_assert(backer
->refcount
> 0);
825 if (--backer
->refcount
) {
829 udpif_destroy(backer
->udpif
);
831 simap_destroy(&backer
->tnl_backers
);
832 ovs_rwlock_destroy(&backer
->odp_to_ofport_lock
);
833 hmap_destroy(&backer
->odp_to_ofport_map
);
834 shash_find_and_delete(&all_dpif_backers
, backer
->type
);
836 free(backer
->dp_version_string
);
837 dpif_close(backer
->dpif
);
841 /* Datapath port slated for removal from datapath. */
843 struct ovs_list list_node
;
847 static bool check_variable_length_userdata(struct dpif_backer
*backer
);
848 static void check_support(struct dpif_backer
*backer
);
851 open_dpif_backer(const char *type
, struct dpif_backer
**backerp
)
853 struct dpif_backer
*backer
;
854 struct dpif_port_dump port_dump
;
855 struct dpif_port port
;
856 struct shash_node
*node
;
857 struct ovs_list garbage_list
;
858 struct odp_garbage
*garbage
;
865 backer
= shash_find_data(&all_dpif_backers
, type
);
872 backer_name
= xasprintf("ovs-%s", type
);
874 /* Remove any existing datapaths, since we assume we're the only
875 * userspace controlling the datapath. */
877 dp_enumerate_names(type
, &names
);
878 SSET_FOR_EACH(name
, &names
) {
879 struct dpif
*old_dpif
;
881 /* Don't remove our backer if it exists. */
882 if (!strcmp(name
, backer_name
)) {
886 if (dpif_open(name
, type
, &old_dpif
)) {
887 VLOG_WARN("couldn't open old datapath %s to remove it", name
);
889 dpif_delete(old_dpif
);
890 dpif_close(old_dpif
);
893 sset_destroy(&names
);
895 backer
= xmalloc(sizeof *backer
);
897 error
= dpif_create_and_open(backer_name
, type
, &backer
->dpif
);
900 VLOG_ERR("failed to open datapath of type %s: %s", type
,
901 ovs_strerror(error
));
905 backer
->udpif
= udpif_create(backer
, backer
->dpif
);
907 backer
->type
= xstrdup(type
);
908 backer
->refcount
= 1;
909 hmap_init(&backer
->odp_to_ofport_map
);
910 ovs_rwlock_init(&backer
->odp_to_ofport_lock
);
911 backer
->need_revalidate
= 0;
912 simap_init(&backer
->tnl_backers
);
913 backer
->recv_set_enable
= !ofproto_get_flow_restore_wait();
916 if (backer
->recv_set_enable
) {
917 dpif_flow_flush(backer
->dpif
);
920 /* Loop through the ports already on the datapath and remove any
921 * that we don't need anymore. */
922 ovs_list_init(&garbage_list
);
923 dpif_port_dump_start(&port_dump
, backer
->dpif
);
924 while (dpif_port_dump_next(&port_dump
, &port
)) {
925 node
= shash_find(&init_ofp_ports
, port
.name
);
926 if (!node
&& strcmp(port
.name
, dpif_base_name(backer
->dpif
))) {
927 garbage
= xmalloc(sizeof *garbage
);
928 garbage
->odp_port
= port
.port_no
;
929 ovs_list_push_front(&garbage_list
, &garbage
->list_node
);
932 dpif_port_dump_done(&port_dump
);
934 LIST_FOR_EACH_POP (garbage
, list_node
, &garbage_list
) {
935 dpif_port_del(backer
->dpif
, garbage
->odp_port
);
939 shash_add(&all_dpif_backers
, type
, backer
);
941 check_support(backer
);
942 atomic_count_init(&backer
->tnl_count
, 0);
944 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
946 VLOG_ERR("failed to listen on datapath of type %s: %s",
947 type
, ovs_strerror(error
));
948 close_dpif_backer(backer
);
952 if (backer
->recv_set_enable
) {
953 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
956 /* This check fails if performed before udpif threads have been set,
957 * as the kernel module checks that the 'pid' in userspace action
959 backer
->support
.variable_length_userdata
960 = check_variable_length_userdata(backer
);
961 backer
->dp_version_string
= dpif_get_dp_version(backer
->dpif
);
967 ovs_native_tunneling_is_on(struct ofproto_dpif
*ofproto
)
969 return ofproto_use_tnl_push_pop
&& ofproto
->backer
->support
.tnl_push_pop
&&
970 atomic_count_get(&ofproto
->backer
->tnl_count
);
973 /* Tests whether 'backer''s datapath supports recirculation. Only newer
974 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
975 * features on older datapaths that don't support this feature.
977 * Returns false if 'backer' definitely does not support recirculation, true if
978 * it seems to support recirculation or if at least the error we get is
981 check_recirc(struct dpif_backer
*backer
)
984 struct odputil_keybuf keybuf
;
987 struct odp_flow_key_parms odp_parms
= {
994 memset(&flow
, 0, sizeof flow
);
998 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
999 odp_flow_key_from_flow(&odp_parms
, &key
);
1000 enable_recirc
= dpif_probe_feature(backer
->dpif
, "recirculation", &key
,
1003 if (enable_recirc
) {
1004 VLOG_INFO("%s: Datapath supports recirculation",
1005 dpif_name(backer
->dpif
));
1007 VLOG_INFO("%s: Datapath does not support recirculation",
1008 dpif_name(backer
->dpif
));
1011 return enable_recirc
;
1014 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1015 * some flow attributes for datapaths that support this feature.
1017 * Returns true if 'dpif' supports UFID for flow operations.
1018 * Returns false if 'dpif' does not support UFID. */
1020 check_ufid(struct dpif_backer
*backer
)
1023 struct odputil_keybuf keybuf
;
1027 struct odp_flow_key_parms odp_parms
= {
1031 memset(&flow
, 0, sizeof flow
);
1032 flow
.dl_type
= htons(0x1234);
1034 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1035 odp_flow_key_from_flow(&odp_parms
, &key
);
1036 dpif_flow_hash(backer
->dpif
, key
.data
, key
.size
, &ufid
);
1038 enable_ufid
= dpif_probe_feature(backer
->dpif
, "UFID", &key
, &ufid
);
1041 VLOG_INFO("%s: Datapath supports unique flow ids",
1042 dpif_name(backer
->dpif
));
1044 VLOG_INFO("%s: Datapath does not support unique flow ids",
1045 dpif_name(backer
->dpif
));
1050 /* Tests whether 'backer''s datapath supports variable-length
1051 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1052 * to disable some features on older datapaths that don't support this
1055 * Returns false if 'backer' definitely does not support variable-length
1056 * userdata, true if it seems to support them or if at least the error we get
1059 check_variable_length_userdata(struct dpif_backer
*backer
)
1061 struct eth_header
*eth
;
1062 struct ofpbuf actions
;
1063 struct dpif_execute execute
;
1064 struct dp_packet packet
;
1069 /* Compose a userspace action that will cause an ERANGE error on older
1070 * datapaths that don't support variable-length userdata.
1072 * We really test for using userdata longer than 8 bytes, but older
1073 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1074 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1075 * test for that instead as a proxy for longer userdata support. */
1076 ofpbuf_init(&actions
, 64);
1077 start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_USERSPACE
);
1078 nl_msg_put_u32(&actions
, OVS_USERSPACE_ATTR_PID
,
1079 dpif_port_get_pid(backer
->dpif
, ODPP_NONE
, 0));
1080 nl_msg_put_unspec_zero(&actions
, OVS_USERSPACE_ATTR_USERDATA
, 4);
1081 nl_msg_end_nested(&actions
, start
);
1083 /* Compose a dummy ethernet packet. */
1084 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1085 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1086 eth
->eth_type
= htons(0x1234);
1088 flow_extract(&packet
, &flow
);
1090 /* Execute the actions. On older datapaths this fails with ERANGE, on
1091 * newer datapaths it succeeds. */
1092 execute
.actions
= actions
.data
;
1093 execute
.actions_len
= actions
.size
;
1094 execute
.packet
= &packet
;
1095 execute
.flow
= &flow
;
1096 execute
.needs_help
= false;
1097 execute
.probe
= true;
1100 error
= dpif_execute(backer
->dpif
, &execute
);
1102 dp_packet_uninit(&packet
);
1103 ofpbuf_uninit(&actions
);
1110 /* Variable-length userdata is not supported. */
1111 VLOG_WARN("%s: datapath does not support variable-length userdata "
1112 "feature (needs Linux 3.10+ or kernel module from OVS "
1113 "1..11+). The NXAST_SAMPLE action will be ignored.",
1114 dpif_name(backer
->dpif
));
1118 /* Something odd happened. We're not sure whether variable-length
1119 * userdata is supported. Default to "yes". */
1120 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1121 dpif_name(backer
->dpif
), ovs_strerror(error
));
1126 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1128 * Returns the number of elements in a struct flow's mpls_lse field
1129 * if the datapath supports at least that many entries in an
1131 * Otherwise returns the number of MPLS push actions supported by
1134 check_max_mpls_depth(struct dpif_backer
*backer
)
1139 for (n
= 0; n
< FLOW_MAX_MPLS_LABELS
; n
++) {
1140 struct odputil_keybuf keybuf
;
1142 struct odp_flow_key_parms odp_parms
= {
1146 memset(&flow
, 0, sizeof flow
);
1147 flow
.dl_type
= htons(ETH_TYPE_MPLS
);
1148 flow_set_mpls_bos(&flow
, n
, 1);
1150 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1151 odp_flow_key_from_flow(&odp_parms
, &key
);
1152 if (!dpif_probe_feature(backer
->dpif
, "MPLS", &key
, NULL
)) {
1157 VLOG_INFO("%s: MPLS label stack length probed as %d",
1158 dpif_name(backer
->dpif
), n
);
1162 /* Tests whether 'backer''s datapath supports masked data in
1163 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1164 * datapaths that don't support this feature. */
1166 check_masked_set_action(struct dpif_backer
*backer
)
1168 struct eth_header
*eth
;
1169 struct ofpbuf actions
;
1170 struct dpif_execute execute
;
1171 struct dp_packet packet
;
1174 struct ovs_key_ethernet key
, mask
;
1176 /* Compose a set action that will cause an EINVAL error on older
1177 * datapaths that don't support masked set actions.
1178 * Avoid using a full mask, as it could be translated to a non-masked
1179 * set action instead. */
1180 ofpbuf_init(&actions
, 64);
1181 memset(&key
, 0x53, sizeof key
);
1182 memset(&mask
, 0x7f, sizeof mask
);
1183 commit_masked_set_action(&actions
, OVS_KEY_ATTR_ETHERNET
, &key
, &mask
,
1186 /* Compose a dummy ethernet packet. */
1187 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1188 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1189 eth
->eth_type
= htons(0x1234);
1191 flow_extract(&packet
, &flow
);
1193 /* Execute the actions. On older datapaths this fails with EINVAL, on
1194 * newer datapaths it succeeds. */
1195 execute
.actions
= actions
.data
;
1196 execute
.actions_len
= actions
.size
;
1197 execute
.packet
= &packet
;
1198 execute
.flow
= &flow
;
1199 execute
.needs_help
= false;
1200 execute
.probe
= true;
1203 error
= dpif_execute(backer
->dpif
, &execute
);
1205 dp_packet_uninit(&packet
);
1206 ofpbuf_uninit(&actions
);
1209 /* Masked set action is not supported. */
1210 VLOG_INFO("%s: datapath does not support masked set action feature.",
1211 dpif_name(backer
->dpif
));
1216 /* Tests whether 'backer''s datapath supports truncation of a packet in
1217 * OVS_ACTION_ATTR_TRUNC. We need to disable some features on older
1218 * datapaths that don't support this feature. */
1220 check_trunc_action(struct dpif_backer
*backer
)
1222 struct eth_header
*eth
;
1223 struct ofpbuf actions
;
1224 struct dpif_execute execute
;
1225 struct dp_packet packet
;
1226 struct ovs_action_trunc
*trunc
;
1230 /* Compose an action with output(port:1,
1231 * max_len:OVS_ACTION_OUTPUT_MIN + 1).
1232 * This translates to one truncate action and one output action. */
1233 ofpbuf_init(&actions
, 64);
1234 trunc
= nl_msg_put_unspec_uninit(&actions
,
1235 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1237 trunc
->max_len
= ETH_HEADER_LEN
+ 1;
1238 nl_msg_put_odp_port(&actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
1240 /* Compose a dummy Ethernet packet. */
1241 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1242 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1243 eth
->eth_type
= htons(0x1234);
1245 flow_extract(&packet
, &flow
);
1247 /* Execute the actions. On older datapaths this fails with EINVAL, on
1248 * newer datapaths it succeeds. */
1249 execute
.actions
= actions
.data
;
1250 execute
.actions_len
= actions
.size
;
1251 execute
.packet
= &packet
;
1252 execute
.flow
= &flow
;
1253 execute
.needs_help
= false;
1254 execute
.probe
= true;
1257 error
= dpif_execute(backer
->dpif
, &execute
);
1259 dp_packet_uninit(&packet
);
1260 ofpbuf_uninit(&actions
);
1263 VLOG_INFO("%s: Datapath does not support truncate action",
1264 dpif_name(backer
->dpif
));
1266 VLOG_INFO("%s: Datapath supports truncate action",
1267 dpif_name(backer
->dpif
));
1273 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE) \
1275 check_##NAME(struct dpif_backer *backer) \
1278 struct odputil_keybuf keybuf; \
1279 struct ofpbuf key; \
1281 struct odp_flow_key_parms odp_parms = { \
1288 memset(&flow, 0, sizeof flow); \
1289 flow.FIELD = VALUE; \
1291 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1292 odp_flow_key_from_flow(&odp_parms, &key); \
1293 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL); \
1296 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1298 VLOG_INFO("%s: Datapath does not support "#NAME, \
1299 dpif_name(backer->dpif)); \
1304 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1)
1306 CHECK_FEATURE(ct_state
)
1307 CHECK_FEATURE(ct_zone
)
1308 CHECK_FEATURE(ct_mark
)
1309 CHECK_FEATURE__(ct_label
, ct_label
, ct_label
.u64
.lo
, 1)
1310 CHECK_FEATURE__(ct_state_nat
, ct_state
, ct_state
, CS_TRACKED
|CS_SRC_NAT
)
1312 #undef CHECK_FEATURE
1313 #undef CHECK_FEATURE__
1316 check_support(struct dpif_backer
*backer
)
1318 /* This feature needs to be tested after udpif threads are set. */
1319 backer
->support
.variable_length_userdata
= false;
1321 backer
->support
.odp
.recirc
= check_recirc(backer
);
1322 backer
->support
.odp
.max_mpls_depth
= check_max_mpls_depth(backer
);
1323 backer
->support
.masked_set_action
= check_masked_set_action(backer
);
1324 backer
->support
.trunc
= check_trunc_action(backer
);
1325 backer
->support
.ufid
= check_ufid(backer
);
1326 backer
->support
.tnl_push_pop
= dpif_supports_tnl_push_pop(backer
->dpif
);
1328 backer
->support
.odp
.ct_state
= check_ct_state(backer
);
1329 backer
->support
.odp
.ct_zone
= check_ct_zone(backer
);
1330 backer
->support
.odp
.ct_mark
= check_ct_mark(backer
);
1331 backer
->support
.odp
.ct_label
= check_ct_label(backer
);
1333 backer
->support
.odp
.ct_state_nat
= check_ct_state_nat(backer
);
1337 construct(struct ofproto
*ofproto_
)
1339 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1340 struct shash_node
*node
, *next
;
1343 /* Tunnel module can get used right after the udpif threads are running. */
1344 ofproto_tunnel_init();
1346 error
= open_dpif_backer(ofproto
->up
.type
, &ofproto
->backer
);
1351 uuid_generate(&ofproto
->uuid
);
1352 atomic_init(&ofproto
->tables_version
, OVS_VERSION_MIN
);
1353 ofproto
->netflow
= NULL
;
1354 ofproto
->sflow
= NULL
;
1355 ofproto
->ipfix
= NULL
;
1356 ofproto
->stp
= NULL
;
1357 ofproto
->rstp
= NULL
;
1358 ofproto
->dump_seq
= 0;
1359 hmap_init(&ofproto
->bundles
);
1360 ofproto
->ml
= mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME
);
1362 ofproto
->mbridge
= mbridge_create();
1363 ofproto
->has_bonded_bundles
= false;
1364 ofproto
->lacp_enabled
= false;
1365 ovs_mutex_init_adaptive(&ofproto
->stats_mutex
);
1367 guarded_list_init(&ofproto
->ams
);
1369 sset_init(&ofproto
->ports
);
1370 sset_init(&ofproto
->ghost_ports
);
1371 sset_init(&ofproto
->port_poll_set
);
1372 ofproto
->port_poll_errno
= 0;
1373 ofproto
->change_seq
= 0;
1374 ofproto
->ams_seq
= seq_create();
1375 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1378 SHASH_FOR_EACH_SAFE (node
, next
, &init_ofp_ports
) {
1379 struct iface_hint
*iface_hint
= node
->data
;
1381 if (!strcmp(iface_hint
->br_name
, ofproto
->up
.name
)) {
1382 /* Check if the datapath already has this port. */
1383 if (dpif_port_exists(ofproto
->backer
->dpif
, node
->name
)) {
1384 sset_add(&ofproto
->ports
, node
->name
);
1387 free(iface_hint
->br_name
);
1388 free(iface_hint
->br_type
);
1390 shash_delete(&init_ofp_ports
, node
);
1394 hmap_insert(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
,
1395 hash_string(ofproto
->up
.name
, 0));
1396 memset(&ofproto
->stats
, 0, sizeof ofproto
->stats
);
1398 ofproto_init_tables(ofproto_
, N_TABLES
);
1399 error
= add_internal_flows(ofproto
);
1401 ofproto
->up
.tables
[TBL_INTERNAL
].flags
= OFTABLE_HIDDEN
| OFTABLE_READONLY
;
1407 add_internal_miss_flow(struct ofproto_dpif
*ofproto
, int id
,
1408 const struct ofpbuf
*ofpacts
, struct rule_dpif
**rulep
)
1414 match_init_catchall(&match
);
1415 match_set_reg(&match
, 0, id
);
1417 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 0, 0, ofpacts
,
1419 *rulep
= error
? NULL
: rule_dpif_cast(rule
);
1425 add_internal_flows(struct ofproto_dpif
*ofproto
)
1427 struct ofpact_controller
*controller
;
1428 uint64_t ofpacts_stub
[128 / 8];
1429 struct ofpbuf ofpacts
;
1430 struct rule
*unused_rulep OVS_UNUSED
;
1435 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1438 controller
= ofpact_put_CONTROLLER(&ofpacts
);
1439 controller
->max_len
= UINT16_MAX
;
1440 controller
->controller_id
= 0;
1441 controller
->reason
= OFPR_IMPLICIT_MISS
;
1442 ofpact_finish_CONTROLLER(&ofpacts
, &controller
);
1444 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1445 &ofproto
->miss_rule
);
1450 ofpbuf_clear(&ofpacts
);
1451 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1452 &ofproto
->no_packet_in_rule
);
1457 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1458 &ofproto
->drop_frags_rule
);
1463 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1464 * zero when reaching this rule.
1466 * (priority=2), recirc_id=0, actions=drop
1468 ofpbuf_clear(&ofpacts
);
1469 match_init_catchall(&match
);
1470 match_set_recirc_id(&match
, 0);
1471 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 2, 0, &ofpacts
,
1477 destruct(struct ofproto
*ofproto_
)
1479 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1480 struct ofproto_async_msg
*am
;
1481 struct rule_dpif
*rule
;
1482 struct oftable
*table
;
1483 struct ovs_list ams
;
1485 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1487 xlate_remove_ofproto(ofproto
);
1490 /* Ensure that the upcall processing threads have no remaining references
1491 * to the ofproto or anything in it. */
1492 udpif_synchronize(ofproto
->backer
->udpif
);
1494 hmap_remove(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
);
1496 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1497 CLS_FOR_EACH (rule
, up
.cr
, &table
->cls
) {
1498 ofproto_rule_delete(&ofproto
->up
, &rule
->up
);
1501 ofproto_group_delete_all(&ofproto
->up
);
1503 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1504 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1505 ofproto_async_msg_free(am
);
1507 guarded_list_destroy(&ofproto
->ams
);
1509 recirc_free_ofproto(ofproto
, ofproto
->up
.name
);
1511 mbridge_unref(ofproto
->mbridge
);
1513 netflow_unref(ofproto
->netflow
);
1514 dpif_sflow_unref(ofproto
->sflow
);
1515 dpif_ipfix_unref(ofproto
->ipfix
);
1516 hmap_destroy(&ofproto
->bundles
);
1517 mac_learning_unref(ofproto
->ml
);
1518 mcast_snooping_unref(ofproto
->ms
);
1520 sset_destroy(&ofproto
->ports
);
1521 sset_destroy(&ofproto
->ghost_ports
);
1522 sset_destroy(&ofproto
->port_poll_set
);
1524 ovs_mutex_destroy(&ofproto
->stats_mutex
);
1526 seq_destroy(ofproto
->ams_seq
);
1528 close_dpif_backer(ofproto
->backer
);
1532 run(struct ofproto
*ofproto_
)
1534 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1535 uint64_t new_seq
, new_dump_seq
;
1537 if (mbridge_need_revalidate(ofproto
->mbridge
)) {
1538 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1539 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1540 mac_learning_flush(ofproto
->ml
);
1541 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1542 mcast_snooping_mdb_flush(ofproto
->ms
);
1545 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1546 * flow restore. Even though nothing is processed during flow restore,
1547 * all queued 'ams' will be handled immediately when flow restore
1549 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1551 /* Do not perform any periodic activity required by 'ofproto' while
1552 * waiting for flow restore to complete. */
1553 if (!ofproto_get_flow_restore_wait()) {
1554 struct ofproto_async_msg
*am
;
1555 struct ovs_list ams
;
1557 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1558 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1559 connmgr_send_async_msg(ofproto
->up
.connmgr
, am
);
1560 ofproto_async_msg_free(am
);
1564 if (ofproto
->netflow
) {
1565 netflow_run(ofproto
->netflow
);
1567 if (ofproto
->sflow
) {
1568 dpif_sflow_run(ofproto
->sflow
);
1570 if (ofproto
->ipfix
) {
1571 dpif_ipfix_run(ofproto
->ipfix
);
1574 new_seq
= seq_read(connectivity_seq_get());
1575 if (ofproto
->change_seq
!= new_seq
) {
1576 struct ofport_dpif
*ofport
;
1578 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1582 ofproto
->change_seq
= new_seq
;
1584 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1585 struct ofbundle
*bundle
;
1587 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1594 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1595 if (mac_learning_run(ofproto
->ml
)) {
1596 ofproto
->backer
->need_revalidate
= REV_MAC_LEARNING
;
1598 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1600 if (mcast_snooping_run(ofproto
->ms
)) {
1601 ofproto
->backer
->need_revalidate
= REV_MCAST_SNOOPING
;
1604 new_dump_seq
= seq_read(udpif_dump_seq(ofproto
->backer
->udpif
));
1605 if (ofproto
->dump_seq
!= new_dump_seq
) {
1606 struct rule
*rule
, *next_rule
;
1607 long long now
= time_msec();
1609 /* We know stats are relatively fresh, so now is a good time to do some
1611 ofproto
->dump_seq
= new_dump_seq
;
1613 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1615 ovs_mutex_lock(&ofproto_mutex
);
1616 LIST_FOR_EACH_SAFE (rule
, next_rule
, expirable
,
1617 &ofproto
->up
.expirable
) {
1618 rule_expire(rule_dpif_cast(rule
), now
);
1620 ovs_mutex_unlock(&ofproto_mutex
);
1622 /* All outstanding data in existing flows has been accounted, so it's a
1623 * good time to do bond rebalancing. */
1624 if (ofproto
->has_bonded_bundles
) {
1625 struct ofbundle
*bundle
;
1627 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1629 bond_rebalance(bundle
->bond
);
1638 ofproto_dpif_wait(struct ofproto
*ofproto_
)
1640 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1642 if (ofproto_get_flow_restore_wait()) {
1646 if (ofproto
->sflow
) {
1647 dpif_sflow_wait(ofproto
->sflow
);
1649 if (ofproto
->ipfix
) {
1650 dpif_ipfix_wait(ofproto
->ipfix
);
1652 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1653 struct ofbundle
*bundle
;
1655 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1656 bundle_wait(bundle
);
1659 if (ofproto
->netflow
) {
1660 netflow_wait(ofproto
->netflow
);
1662 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
1663 mac_learning_wait(ofproto
->ml
);
1664 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1665 mcast_snooping_wait(ofproto
->ms
);
1667 if (ofproto
->backer
->need_revalidate
) {
1668 poll_immediate_wake();
1671 seq_wait(udpif_dump_seq(ofproto
->backer
->udpif
), ofproto
->dump_seq
);
1672 seq_wait(ofproto
->ams_seq
, ofproto
->ams_seqno
);
1676 type_get_memory_usage(const char *type
, struct simap
*usage
)
1678 struct dpif_backer
*backer
;
1680 backer
= shash_find_data(&all_dpif_backers
, type
);
1682 udpif_get_memory_usage(backer
->udpif
, usage
);
1687 flush(struct ofproto
*ofproto_
)
1689 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1690 struct dpif_backer
*backer
= ofproto
->backer
;
1693 udpif_flush(backer
->udpif
);
1698 query_tables(struct ofproto
*ofproto
,
1699 struct ofputil_table_features
*features
,
1700 struct ofputil_table_stats
*stats
)
1702 strcpy(features
->name
, "classifier");
1707 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
1708 unsigned long missed
, matched
;
1710 atomic_read_relaxed(&ofproto
->tables
[i
].n_matched
, &matched
);
1711 atomic_read_relaxed(&ofproto
->tables
[i
].n_missed
, &missed
);
1713 stats
[i
].matched_count
= matched
;
1714 stats
[i
].lookup_count
= matched
+ missed
;
1720 set_tables_version(struct ofproto
*ofproto_
, ovs_version_t version
)
1722 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1724 atomic_store_relaxed(&ofproto
->tables_version
, version
);
1725 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
1728 static struct ofport
*
1731 struct ofport_dpif
*port
= xzalloc(sizeof *port
);
1736 port_dealloc(struct ofport
*port_
)
1738 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1743 port_construct(struct ofport
*port_
)
1745 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1746 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1747 const struct netdev
*netdev
= port
->up
.netdev
;
1748 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1749 const char *dp_port_name
;
1750 struct dpif_port dpif_port
;
1753 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1754 port
->bundle
= NULL
;
1758 port
->may_enable
= false;
1759 port
->stp_port
= NULL
;
1760 port
->stp_state
= STP_DISABLED
;
1761 port
->rstp_port
= NULL
;
1762 port
->rstp_state
= RSTP_DISABLED
;
1763 port
->is_tunnel
= false;
1767 port
->carrier_seq
= netdev_get_carrier_resets(netdev
);
1768 port
->is_layer3
= netdev_vport_is_layer3(netdev
);
1770 if (netdev_vport_is_patch(netdev
)) {
1771 /* By bailing out here, we don't submit the port to the sFlow module
1772 * to be considered for counter polling export. This is correct
1773 * because the patch port represents an interface that sFlow considers
1774 * to be "internal" to the switch as a whole, and therefore not a
1775 * candidate for counter polling. */
1776 port
->odp_port
= ODPP_NONE
;
1777 ofport_update_peer(port
);
1781 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1782 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
, dp_port_name
,
1788 port
->odp_port
= dpif_port
.port_no
;
1790 if (netdev_get_tunnel_config(netdev
)) {
1791 atomic_count_inc(&ofproto
->backer
->tnl_count
);
1792 error
= tnl_port_add(port
, port
->up
.netdev
, port
->odp_port
,
1793 ovs_native_tunneling_is_on(ofproto
), dp_port_name
);
1795 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1796 dpif_port_destroy(&dpif_port
);
1800 port
->is_tunnel
= true;
1801 if (ofproto
->ipfix
) {
1802 dpif_ipfix_add_tunnel_port(ofproto
->ipfix
, port_
, port
->odp_port
);
1805 /* Sanity-check that a mapping doesn't already exist. This
1806 * shouldn't happen for non-tunnel ports. */
1807 if (odp_port_to_ofp_port(ofproto
, port
->odp_port
) != OFPP_NONE
) {
1808 VLOG_ERR("port %s already has an OpenFlow port number",
1810 dpif_port_destroy(&dpif_port
);
1814 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1815 hmap_insert(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
,
1816 hash_odp_port(port
->odp_port
));
1817 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1819 dpif_port_destroy(&dpif_port
);
1821 if (ofproto
->sflow
) {
1822 dpif_sflow_add_port(ofproto
->sflow
, port_
, port
->odp_port
);
1829 port_destruct(struct ofport
*port_
, bool del
)
1831 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1832 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1833 const char *devname
= netdev_get_name(port
->up
.netdev
);
1834 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1835 const char *dp_port_name
;
1837 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1839 xlate_ofport_remove(port
);
1842 dp_port_name
= netdev_vport_get_dpif_port(port
->up
.netdev
, namebuf
,
1844 if (del
&& dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
1845 /* The underlying device is still there, so delete it. This
1846 * happens when the ofproto is being destroyed, since the caller
1847 * assumes that removal of attached ports will happen as part of
1849 if (!port
->is_tunnel
) {
1850 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
);
1855 port
->peer
->peer
= NULL
;
1859 if (port
->odp_port
!= ODPP_NONE
&& !port
->is_tunnel
) {
1860 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1861 hmap_remove(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
);
1862 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1865 if (port
->is_tunnel
) {
1866 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1869 if (port
->is_tunnel
&& ofproto
->ipfix
) {
1870 dpif_ipfix_del_tunnel_port(ofproto
->ipfix
, port
->odp_port
);
1874 sset_find_and_delete(&ofproto
->ports
, devname
);
1875 sset_find_and_delete(&ofproto
->ghost_ports
, devname
);
1876 bundle_remove(port_
);
1877 set_cfm(port_
, NULL
);
1878 set_bfd(port_
, NULL
);
1879 set_lldp(port_
, NULL
);
1880 if (port
->stp_port
) {
1881 stp_port_disable(port
->stp_port
);
1883 set_rstp_port(port_
, NULL
);
1884 if (ofproto
->sflow
) {
1885 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
1892 port_modified(struct ofport
*port_
)
1894 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1895 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1896 const char *dp_port_name
;
1897 struct netdev
*netdev
= port
->up
.netdev
;
1899 if (port
->bundle
&& port
->bundle
->bond
) {
1900 bond_slave_set_netdev(port
->bundle
->bond
, port
, netdev
);
1904 cfm_set_netdev(port
->cfm
, netdev
);
1908 bfd_set_netdev(port
->bfd
, netdev
);
1911 ofproto_dpif_monitor_port_update(port
, port
->bfd
, port
->cfm
,
1912 port
->lldp
, &port
->up
.pp
.hw_addr
);
1914 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1916 if (port
->is_tunnel
) {
1917 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1919 if (tnl_port_reconfigure(port
, netdev
, port
->odp_port
,
1920 ovs_native_tunneling_is_on(ofproto
),
1922 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1926 ofport_update_peer(port
);
1930 port_reconfigured(struct ofport
*port_
, enum ofputil_port_config old_config
)
1932 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1933 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1934 enum ofputil_port_config changed
= old_config
^ port
->up
.pp
.config
;
1936 if (changed
& (OFPUTIL_PC_NO_RECV
| OFPUTIL_PC_NO_RECV_STP
|
1937 OFPUTIL_PC_NO_FWD
| OFPUTIL_PC_NO_FLOOD
|
1938 OFPUTIL_PC_NO_PACKET_IN
)) {
1939 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1941 if (changed
& OFPUTIL_PC_NO_FLOOD
&& port
->bundle
) {
1942 bundle_update(port
->bundle
);
1948 set_sflow(struct ofproto
*ofproto_
,
1949 const struct ofproto_sflow_options
*sflow_options
)
1951 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1952 struct dpif_sflow
*ds
= ofproto
->sflow
;
1954 if (sflow_options
) {
1955 uint32_t old_probability
= ds
? dpif_sflow_get_probability(ds
) : 0;
1957 struct ofport_dpif
*ofport
;
1959 ds
= ofproto
->sflow
= dpif_sflow_create();
1960 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1961 dpif_sflow_add_port(ds
, &ofport
->up
, ofport
->odp_port
);
1964 dpif_sflow_set_options(ds
, sflow_options
);
1965 if (dpif_sflow_get_probability(ds
) != old_probability
) {
1966 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1970 dpif_sflow_unref(ds
);
1971 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1972 ofproto
->sflow
= NULL
;
1980 struct ofproto
*ofproto_
,
1981 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
1982 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
1983 size_t n_flow_exporters_options
)
1985 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1986 struct dpif_ipfix
*di
= ofproto
->ipfix
;
1987 bool has_options
= bridge_exporter_options
|| flow_exporters_options
;
1988 bool new_di
= false;
1990 if (has_options
&& !di
) {
1991 di
= ofproto
->ipfix
= dpif_ipfix_create();
1996 /* Call set_options in any case to cleanly flush the flow
1997 * caches in the last exporters that are to be destroyed. */
1998 dpif_ipfix_set_options(
1999 di
, bridge_exporter_options
, flow_exporters_options
,
2000 n_flow_exporters_options
);
2002 /* Add tunnel ports only when a new ipfix created */
2003 if (new_di
== true) {
2004 struct ofport_dpif
*ofport
;
2005 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2006 if (ofport
->is_tunnel
== true) {
2007 dpif_ipfix_add_tunnel_port(di
, &ofport
->up
, ofport
->odp_port
);
2013 dpif_ipfix_unref(di
);
2014 ofproto
->ipfix
= NULL
;
2022 get_ipfix_stats(const struct ofproto
*ofproto_
,
2024 struct ovs_list
*replies
)
2026 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2027 struct dpif_ipfix
*di
= ofproto
->ipfix
;
2030 return OFPERR_NXST_NOT_CONFIGURED
;
2033 return dpif_ipfix_get_stats(di
, bridge_ipfix
, replies
);
2037 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
2039 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2040 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2041 struct cfm
*old
= ofport
->cfm
;
2046 ofport
->cfm
= cfm_create(ofport
->up
.netdev
);
2049 if (cfm_configure(ofport
->cfm
, s
)) {
2056 cfm_unref(ofport
->cfm
);
2059 if (ofport
->cfm
!= old
) {
2060 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2062 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2063 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2068 cfm_status_changed(struct ofport
*ofport_
)
2070 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2072 return ofport
->cfm
? cfm_check_status_change(ofport
->cfm
) : true;
2076 get_cfm_status(const struct ofport
*ofport_
,
2077 struct cfm_status
*status
)
2079 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2083 cfm_get_status(ofport
->cfm
, status
);
2092 set_bfd(struct ofport
*ofport_
, const struct smap
*cfg
)
2094 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
2095 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2099 ofport
->bfd
= bfd_configure(old
, netdev_get_name(ofport
->up
.netdev
),
2100 cfg
, ofport
->up
.netdev
);
2101 if (ofport
->bfd
!= old
) {
2102 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2104 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2105 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2110 bfd_status_changed(struct ofport
*ofport_
)
2112 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2114 return ofport
->bfd
? bfd_check_status_change(ofport
->bfd
) : true;
2118 get_bfd_status(struct ofport
*ofport_
, struct smap
*smap
)
2120 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2124 bfd_get_status(ofport
->bfd
, smap
);
2133 set_lldp(struct ofport
*ofport_
,
2134 const struct smap
*cfg
)
2136 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2140 if (!ofport
->lldp
) {
2141 struct ofproto_dpif
*ofproto
;
2143 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2144 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2145 ofport
->lldp
= lldp_create(ofport
->up
.netdev
, ofport_
->mtu
, cfg
);
2148 if (!lldp_configure(ofport
->lldp
, cfg
)) {
2153 lldp_unref(ofport
->lldp
);
2154 ofport
->lldp
= NULL
;
2157 ofproto_dpif_monitor_port_update(ofport
,
2161 &ofport
->up
.pp
.hw_addr
);
2166 get_lldp_status(const struct ofport
*ofport_
,
2167 struct lldp_status
*status OVS_UNUSED
)
2169 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2171 return ofport
->lldp
? true : false;
2175 set_aa(struct ofproto
*ofproto OVS_UNUSED
,
2176 const struct aa_settings
*s
)
2178 return aa_configure(s
);
2182 aa_mapping_set(struct ofproto
*ofproto_ OVS_UNUSED
, void *aux
,
2183 const struct aa_mapping_settings
*s
)
2185 return aa_mapping_register(aux
, s
);
2189 aa_mapping_unset(struct ofproto
*ofproto OVS_UNUSED
, void *aux
)
2191 return aa_mapping_unregister(aux
);
2195 aa_vlan_get_queued(struct ofproto
*ofproto OVS_UNUSED
, struct ovs_list
*list
)
2197 return aa_get_vlan_queued(list
);
2201 aa_vlan_get_queue_size(struct ofproto
*ofproto OVS_UNUSED
)
2203 return aa_get_vlan_queue_size();
2207 /* Spanning Tree. */
2209 /* Called while rstp_mutex is held. */
2211 rstp_send_bpdu_cb(struct dp_packet
*pkt
, void *ofport_
, void *ofproto_
)
2213 struct ofproto_dpif
*ofproto
= ofproto_
;
2214 struct ofport_dpif
*ofport
= ofport_
;
2215 struct eth_header
*eth
= dp_packet_l2(pkt
);
2217 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2218 if (eth_addr_is_zero(eth
->eth_src
)) {
2219 VLOG_WARN_RL(&rl
, "%s port %d: cannot send RSTP BPDU on a port which "
2220 "does not have a configured source MAC address.",
2221 ofproto
->up
.name
, ofp_to_u16(ofport
->up
.ofp_port
));
2223 ofproto_dpif_send_packet(ofport
, false, pkt
);
2225 dp_packet_delete(pkt
);
2229 send_bpdu_cb(struct dp_packet
*pkt
, int port_num
, void *ofproto_
)
2231 struct ofproto_dpif
*ofproto
= ofproto_
;
2232 struct stp_port
*sp
= stp_get_port(ofproto
->stp
, port_num
);
2233 struct ofport_dpif
*ofport
;
2235 ofport
= stp_port_get_aux(sp
);
2237 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
2238 ofproto
->up
.name
, port_num
);
2240 struct eth_header
*eth
= dp_packet_l2(pkt
);
2242 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2243 if (eth_addr_is_zero(eth
->eth_src
)) {
2244 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d "
2245 "with unknown MAC", ofproto
->up
.name
, port_num
);
2247 ofproto_dpif_send_packet(ofport
, false, pkt
);
2250 dp_packet_delete(pkt
);
2253 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2255 set_rstp(struct ofproto
*ofproto_
, const struct ofproto_rstp_settings
*s
)
2257 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2259 /* Only revalidate flows if the configuration changed. */
2260 if (!s
!= !ofproto
->rstp
) {
2261 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2265 if (!ofproto
->rstp
) {
2266 ofproto
->rstp
= rstp_create(ofproto_
->name
, s
->address
,
2267 rstp_send_bpdu_cb
, ofproto
);
2268 ofproto
->rstp_last_tick
= time_msec();
2270 rstp_set_bridge_address(ofproto
->rstp
, s
->address
);
2271 rstp_set_bridge_priority(ofproto
->rstp
, s
->priority
);
2272 rstp_set_bridge_ageing_time(ofproto
->rstp
, s
->ageing_time
);
2273 rstp_set_bridge_force_protocol_version(ofproto
->rstp
,
2274 s
->force_protocol_version
);
2275 rstp_set_bridge_max_age(ofproto
->rstp
, s
->bridge_max_age
);
2276 rstp_set_bridge_forward_delay(ofproto
->rstp
, s
->bridge_forward_delay
);
2277 rstp_set_bridge_transmit_hold_count(ofproto
->rstp
,
2278 s
->transmit_hold_count
);
2280 struct ofport
*ofport
;
2281 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2282 set_rstp_port(ofport
, NULL
);
2284 rstp_unref(ofproto
->rstp
);
2285 ofproto
->rstp
= NULL
;
2290 get_rstp_status(struct ofproto
*ofproto_
, struct ofproto_rstp_status
*s
)
2292 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2294 if (ofproto
->rstp
) {
2296 s
->root_id
= rstp_get_root_id(ofproto
->rstp
);
2297 s
->bridge_id
= rstp_get_bridge_id(ofproto
->rstp
);
2298 s
->designated_id
= rstp_get_designated_id(ofproto
->rstp
);
2299 s
->root_path_cost
= rstp_get_root_path_cost(ofproto
->rstp
);
2300 s
->designated_port_id
= rstp_get_designated_port_id(ofproto
->rstp
);
2301 s
->bridge_port_id
= rstp_get_bridge_port_id(ofproto
->rstp
);
2308 update_rstp_port_state(struct ofport_dpif
*ofport
)
2310 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2311 enum rstp_state state
;
2313 /* Figure out new state. */
2314 state
= ofport
->rstp_port
? rstp_port_get_state(ofport
->rstp_port
)
2318 if (ofport
->rstp_state
!= state
) {
2319 enum ofputil_port_state of_state
;
2322 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2323 netdev_get_name(ofport
->up
.netdev
),
2324 rstp_state_name(ofport
->rstp_state
),
2325 rstp_state_name(state
));
2327 if (rstp_learn_in_state(ofport
->rstp_state
)
2328 != rstp_learn_in_state(state
)) {
2329 /* XXX: Learning action flows should also be flushed. */
2330 if (ofport
->bundle
) {
2331 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2332 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2333 bundle_flush_macs(ofport
->bundle
, false);
2337 fwd_change
= rstp_forward_in_state(ofport
->rstp_state
)
2338 != rstp_forward_in_state(state
);
2340 ofproto
->backer
->need_revalidate
= REV_RSTP
;
2341 ofport
->rstp_state
= state
;
2343 if (fwd_change
&& ofport
->bundle
) {
2344 bundle_update(ofport
->bundle
);
2347 /* Update the RSTP state bits in the OpenFlow port description. */
2348 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2349 of_state
|= (state
== RSTP_LEARNING
? OFPUTIL_PS_STP_LEARN
2350 : state
== RSTP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2351 : state
== RSTP_DISCARDING
? OFPUTIL_PS_STP_LISTEN
2353 ofproto_port_set_state(&ofport
->up
, of_state
);
2358 rstp_run(struct ofproto_dpif
*ofproto
)
2360 if (ofproto
->rstp
) {
2361 long long int now
= time_msec();
2362 long long int elapsed
= now
- ofproto
->rstp_last_tick
;
2363 struct rstp_port
*rp
;
2364 struct ofport_dpif
*ofport
;
2366 /* Every second, decrease the values of the timers. */
2367 if (elapsed
>= 1000) {
2368 rstp_tick_timers(ofproto
->rstp
);
2369 ofproto
->rstp_last_tick
= now
;
2372 while ((ofport
= rstp_get_next_changed_port_aux(ofproto
->rstp
, &rp
))) {
2373 update_rstp_port_state(ofport
);
2377 /* FIXME: This check should be done on-event (i.e., when setting
2378 * p->fdb_flush) and not periodically.
2380 while ((ofport
= rstp_check_and_reset_fdb_flush(ofproto
->rstp
, &rp
))) {
2381 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2382 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2383 bundle_flush_macs(ofport
->bundle
, false);
2387 if (rstp_shift_root_learned_address(ofproto
->rstp
)) {
2388 struct ofport_dpif
*old_root_aux
=
2389 (struct ofport_dpif
*)rstp_get_old_root_aux(ofproto
->rstp
);
2390 struct ofport_dpif
*new_root_aux
=
2391 (struct ofport_dpif
*)rstp_get_new_root_aux(ofproto
->rstp
);
2392 if (old_root_aux
!= NULL
&& new_root_aux
!= NULL
) {
2393 bundle_move(old_root_aux
->bundle
, new_root_aux
->bundle
);
2394 rstp_reset_root_changed(ofproto
->rstp
);
2400 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2402 set_stp(struct ofproto
*ofproto_
, const struct ofproto_stp_settings
*s
)
2404 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2406 /* Only revalidate flows if the configuration changed. */
2407 if (!s
!= !ofproto
->stp
) {
2408 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2412 if (!ofproto
->stp
) {
2413 ofproto
->stp
= stp_create(ofproto_
->name
, s
->system_id
,
2414 send_bpdu_cb
, ofproto
);
2415 ofproto
->stp_last_tick
= time_msec();
2418 stp_set_bridge_id(ofproto
->stp
, s
->system_id
);
2419 stp_set_bridge_priority(ofproto
->stp
, s
->priority
);
2420 stp_set_hello_time(ofproto
->stp
, s
->hello_time
);
2421 stp_set_max_age(ofproto
->stp
, s
->max_age
);
2422 stp_set_forward_delay(ofproto
->stp
, s
->fwd_delay
);
2424 struct ofport
*ofport
;
2426 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2427 set_stp_port(ofport
, NULL
);
2430 stp_unref(ofproto
->stp
);
2431 ofproto
->stp
= NULL
;
2438 get_stp_status(struct ofproto
*ofproto_
, struct ofproto_stp_status
*s
)
2440 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2444 s
->bridge_id
= stp_get_bridge_id(ofproto
->stp
);
2445 s
->designated_root
= stp_get_designated_root(ofproto
->stp
);
2446 s
->root_path_cost
= stp_get_root_path_cost(ofproto
->stp
);
2455 update_stp_port_state(struct ofport_dpif
*ofport
)
2457 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2458 enum stp_state state
;
2460 /* Figure out new state. */
2461 state
= ofport
->stp_port
? stp_port_get_state(ofport
->stp_port
)
2465 if (ofport
->stp_state
!= state
) {
2466 enum ofputil_port_state of_state
;
2469 VLOG_DBG("port %s: STP state changed from %s to %s",
2470 netdev_get_name(ofport
->up
.netdev
),
2471 stp_state_name(ofport
->stp_state
),
2472 stp_state_name(state
));
2473 if (stp_learn_in_state(ofport
->stp_state
)
2474 != stp_learn_in_state(state
)) {
2475 /* xxx Learning action flows should also be flushed. */
2476 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2477 mac_learning_flush(ofproto
->ml
);
2478 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2479 mcast_snooping_mdb_flush(ofproto
->ms
);
2481 fwd_change
= stp_forward_in_state(ofport
->stp_state
)
2482 != stp_forward_in_state(state
);
2484 ofproto
->backer
->need_revalidate
= REV_STP
;
2485 ofport
->stp_state
= state
;
2486 ofport
->stp_state_entered
= time_msec();
2488 if (fwd_change
&& ofport
->bundle
) {
2489 bundle_update(ofport
->bundle
);
2492 /* Update the STP state bits in the OpenFlow port description. */
2493 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2494 of_state
|= (state
== STP_LISTENING
? OFPUTIL_PS_STP_LISTEN
2495 : state
== STP_LEARNING
? OFPUTIL_PS_STP_LEARN
2496 : state
== STP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2497 : state
== STP_BLOCKING
? OFPUTIL_PS_STP_BLOCK
2499 ofproto_port_set_state(&ofport
->up
, of_state
);
2503 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2504 * caller is responsible for assigning STP port numbers and ensuring
2505 * there are no duplicates. */
2507 set_stp_port(struct ofport
*ofport_
,
2508 const struct ofproto_port_stp_settings
*s
)
2510 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2511 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2512 struct stp_port
*sp
= ofport
->stp_port
;
2514 if (!s
|| !s
->enable
) {
2516 ofport
->stp_port
= NULL
;
2517 stp_port_disable(sp
);
2518 update_stp_port_state(ofport
);
2521 } else if (sp
&& stp_port_no(sp
) != s
->port_num
2522 && ofport
== stp_port_get_aux(sp
)) {
2523 /* The port-id changed, so disable the old one if it's not
2524 * already in use by another port. */
2525 stp_port_disable(sp
);
2528 sp
= ofport
->stp_port
= stp_get_port(ofproto
->stp
, s
->port_num
);
2530 /* Set name before enabling the port so that debugging messages can print
2532 stp_port_set_name(sp
, netdev_get_name(ofport
->up
.netdev
));
2533 stp_port_enable(sp
);
2535 stp_port_set_aux(sp
, ofport
);
2536 stp_port_set_priority(sp
, s
->priority
);
2537 stp_port_set_path_cost(sp
, s
->path_cost
);
2539 update_stp_port_state(ofport
);
2545 get_stp_port_status(struct ofport
*ofport_
,
2546 struct ofproto_port_stp_status
*s
)
2548 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2549 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2550 struct stp_port
*sp
= ofport
->stp_port
;
2552 if (!ofproto
->stp
|| !sp
) {
2558 s
->port_id
= stp_port_get_id(sp
);
2559 s
->state
= stp_port_get_state(sp
);
2560 s
->sec_in_state
= (time_msec() - ofport
->stp_state_entered
) / 1000;
2561 s
->role
= stp_port_get_role(sp
);
2567 get_stp_port_stats(struct ofport
*ofport_
,
2568 struct ofproto_port_stp_stats
*s
)
2570 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2571 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2572 struct stp_port
*sp
= ofport
->stp_port
;
2574 if (!ofproto
->stp
|| !sp
) {
2580 stp_port_get_counts(sp
, &s
->tx_count
, &s
->rx_count
, &s
->error_count
);
2586 stp_run(struct ofproto_dpif
*ofproto
)
2589 long long int now
= time_msec();
2590 long long int elapsed
= now
- ofproto
->stp_last_tick
;
2591 struct stp_port
*sp
;
2594 stp_tick(ofproto
->stp
, MIN(INT_MAX
, elapsed
));
2595 ofproto
->stp_last_tick
= now
;
2597 while (stp_get_changed_port(ofproto
->stp
, &sp
)) {
2598 struct ofport_dpif
*ofport
= stp_port_get_aux(sp
);
2601 update_stp_port_state(ofport
);
2605 if (stp_check_and_reset_fdb_flush(ofproto
->stp
)) {
2606 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2607 mac_learning_flush(ofproto
->ml
);
2608 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2609 mcast_snooping_mdb_flush(ofproto
->ms
);
2615 stp_wait(struct ofproto_dpif
*ofproto
)
2618 poll_timer_wait(1000);
2622 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2623 * caller is responsible for assigning RSTP port numbers and ensuring
2624 * there are no duplicates. */
2626 set_rstp_port(struct ofport
*ofport_
,
2627 const struct ofproto_port_rstp_settings
*s
)
2629 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2630 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2631 struct rstp_port
*rp
= ofport
->rstp_port
;
2633 if (!s
|| !s
->enable
) {
2635 rstp_port_set_aux(rp
, NULL
);
2636 rstp_port_set_state(rp
, RSTP_DISABLED
);
2637 rstp_port_set_mac_operational(rp
, false);
2638 ofport
->rstp_port
= NULL
;
2639 rstp_port_unref(rp
);
2640 update_rstp_port_state(ofport
);
2645 /* Check if need to add a new port. */
2647 rp
= ofport
->rstp_port
= rstp_add_port(ofproto
->rstp
);
2650 rstp_port_set(rp
, s
->port_num
, s
->priority
, s
->path_cost
,
2651 s
->admin_edge_port
, s
->auto_edge
,
2652 s
->admin_p2p_mac_state
, s
->admin_port_state
, s
->mcheck
,
2654 update_rstp_port_state(ofport
);
2655 /* Synchronize operational status. */
2656 rstp_port_set_mac_operational(rp
, ofport
->may_enable
);
2660 get_rstp_port_status(struct ofport
*ofport_
,
2661 struct ofproto_port_rstp_status
*s
)
2663 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2664 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2665 struct rstp_port
*rp
= ofport
->rstp_port
;
2667 if (!ofproto
->rstp
|| !rp
) {
2673 rstp_port_get_status(rp
, &s
->port_id
, &s
->state
, &s
->role
,
2674 &s
->designated_bridge_id
, &s
->designated_port_id
,
2675 &s
->designated_path_cost
, &s
->tx_count
,
2676 &s
->rx_count
, &s
->error_count
, &s
->uptime
);
2681 set_queues(struct ofport
*ofport_
, const struct ofproto_port_queue
*qdscp
,
2684 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2685 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2687 if (ofport
->n_qdscp
!= n_qdscp
2688 || (n_qdscp
&& memcmp(ofport
->qdscp
, qdscp
,
2689 n_qdscp
* sizeof *qdscp
))) {
2690 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2691 free(ofport
->qdscp
);
2692 ofport
->qdscp
= n_qdscp
2693 ? xmemdup(qdscp
, n_qdscp
* sizeof *qdscp
)
2695 ofport
->n_qdscp
= n_qdscp
;
2703 /* Expires all MAC learning entries associated with 'bundle' and forces its
2704 * ofproto to revalidate every flow.
2706 * Normally MAC learning entries are removed only from the ofproto associated
2707 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2708 * are removed from every ofproto. When patch ports and SLB bonds are in use
2709 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2710 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2711 * with the host from which it migrated. */
2713 bundle_flush_macs(struct ofbundle
*bundle
, bool all_ofprotos
)
2715 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2716 struct mac_learning
*ml
= ofproto
->ml
;
2717 struct mac_entry
*mac
, *next_mac
;
2719 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2720 ovs_rwlock_wrlock(&ml
->rwlock
);
2721 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2722 if (mac_entry_get_port(ml
, mac
) == bundle
) {
2724 struct ofproto_dpif
*o
;
2726 HMAP_FOR_EACH (o
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
2728 struct mac_entry
*e
;
2730 ovs_rwlock_wrlock(&o
->ml
->rwlock
);
2731 e
= mac_learning_lookup(o
->ml
, mac
->mac
, mac
->vlan
);
2733 mac_learning_expire(o
->ml
, e
);
2735 ovs_rwlock_unlock(&o
->ml
->rwlock
);
2740 mac_learning_expire(ml
, mac
);
2743 ovs_rwlock_unlock(&ml
->rwlock
);
2747 bundle_move(struct ofbundle
*old
, struct ofbundle
*new)
2749 struct ofproto_dpif
*ofproto
= old
->ofproto
;
2750 struct mac_learning
*ml
= ofproto
->ml
;
2751 struct mac_entry
*mac
, *next_mac
;
2753 ovs_assert(new->ofproto
== old
->ofproto
);
2755 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2756 ovs_rwlock_wrlock(&ml
->rwlock
);
2757 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2758 if (mac_entry_get_port(ml
, mac
) == old
) {
2759 mac_entry_set_port(ml
, mac
, new);
2762 ovs_rwlock_unlock(&ml
->rwlock
);
2765 static struct ofbundle
*
2766 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
2768 struct ofbundle
*bundle
;
2770 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
2771 &ofproto
->bundles
) {
2772 if (bundle
->aux
== aux
) {
2780 bundle_update(struct ofbundle
*bundle
)
2782 struct ofport_dpif
*port
;
2784 bundle
->floodable
= true;
2785 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2786 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2788 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2789 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2790 bundle
->floodable
= false;
2797 bundle_del_port(struct ofport_dpif
*port
)
2799 struct ofbundle
*bundle
= port
->bundle
;
2801 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2803 ovs_list_remove(&port
->bundle_node
);
2804 port
->bundle
= NULL
;
2807 lacp_slave_unregister(bundle
->lacp
, port
);
2810 bond_slave_unregister(bundle
->bond
, port
);
2813 bundle_update(bundle
);
2817 bundle_add_port(struct ofbundle
*bundle
, ofp_port_t ofp_port
,
2818 struct lacp_slave_settings
*lacp
)
2820 struct ofport_dpif
*port
;
2822 port
= ofp_port_to_ofport(bundle
->ofproto
, ofp_port
);
2827 if (port
->bundle
!= bundle
) {
2828 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2830 bundle_remove(&port
->up
);
2833 port
->bundle
= bundle
;
2834 ovs_list_push_back(&bundle
->ports
, &port
->bundle_node
);
2835 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2837 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2838 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2839 bundle
->floodable
= false;
2843 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2844 lacp_slave_register(bundle
->lacp
, port
, lacp
);
2851 bundle_destroy(struct ofbundle
*bundle
)
2853 struct ofproto_dpif
*ofproto
;
2854 struct ofport_dpif
*port
, *next_port
;
2860 ofproto
= bundle
->ofproto
;
2861 mbridge_unregister_bundle(ofproto
->mbridge
, bundle
);
2864 xlate_bundle_remove(bundle
);
2867 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2868 bundle_del_port(port
);
2871 bundle_flush_macs(bundle
, true);
2872 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
2874 free(bundle
->trunks
);
2875 lacp_unref(bundle
->lacp
);
2876 bond_unref(bundle
->bond
);
2881 bundle_set(struct ofproto
*ofproto_
, void *aux
,
2882 const struct ofproto_bundle_settings
*s
)
2884 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2885 bool need_flush
= false;
2886 struct ofport_dpif
*port
;
2887 struct ofbundle
*bundle
;
2888 unsigned long *trunks
;
2894 bundle_destroy(bundle_lookup(ofproto
, aux
));
2898 ovs_assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
2899 ovs_assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
2901 bundle
= bundle_lookup(ofproto
, aux
);
2903 bundle
= xmalloc(sizeof *bundle
);
2905 bundle
->ofproto
= ofproto
;
2906 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
2907 hash_pointer(aux
, 0));
2909 bundle
->name
= NULL
;
2911 ovs_list_init(&bundle
->ports
);
2912 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
2914 bundle
->trunks
= NULL
;
2915 bundle
->use_priority_tags
= s
->use_priority_tags
;
2916 bundle
->lacp
= NULL
;
2917 bundle
->bond
= NULL
;
2919 bundle
->floodable
= true;
2920 mbridge_register_bundle(ofproto
->mbridge
, bundle
);
2923 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
2925 bundle
->name
= xstrdup(s
->name
);
2930 ofproto
->lacp_enabled
= true;
2931 if (!bundle
->lacp
) {
2932 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2933 bundle
->lacp
= lacp_create();
2935 lacp_configure(bundle
->lacp
, s
->lacp
);
2937 lacp_unref(bundle
->lacp
);
2938 bundle
->lacp
= NULL
;
2941 /* Update set of ports. */
2943 for (i
= 0; i
< s
->n_slaves
; i
++) {
2944 if (!bundle_add_port(bundle
, s
->slaves
[i
],
2945 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
)) {
2949 if (!ok
|| ovs_list_size(&bundle
->ports
) != s
->n_slaves
) {
2950 struct ofport_dpif
*next_port
;
2952 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2953 for (i
= 0; i
< s
->n_slaves
; i
++) {
2954 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
2959 bundle_del_port(port
);
2963 ovs_assert(ovs_list_size(&bundle
->ports
) <= s
->n_slaves
);
2965 if (ovs_list_is_empty(&bundle
->ports
)) {
2966 bundle_destroy(bundle
);
2970 /* Set VLAN tagging mode */
2971 if (s
->vlan_mode
!= bundle
->vlan_mode
2972 || s
->use_priority_tags
!= bundle
->use_priority_tags
) {
2973 bundle
->vlan_mode
= s
->vlan_mode
;
2974 bundle
->use_priority_tags
= s
->use_priority_tags
;
2979 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
2980 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
2982 if (vlan
!= bundle
->vlan
) {
2983 bundle
->vlan
= vlan
;
2987 /* Get trunked VLANs. */
2988 switch (s
->vlan_mode
) {
2989 case PORT_VLAN_ACCESS
:
2993 case PORT_VLAN_TRUNK
:
2994 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
2997 case PORT_VLAN_NATIVE_UNTAGGED
:
2998 case PORT_VLAN_NATIVE_TAGGED
:
2999 if (vlan
!= 0 && (!s
->trunks
3000 || !bitmap_is_set(s
->trunks
, vlan
)
3001 || bitmap_is_set(s
->trunks
, 0))) {
3002 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3004 trunks
= bitmap_clone(s
->trunks
, 4096);
3006 trunks
= bitmap_allocate1(4096);
3008 bitmap_set1(trunks
, vlan
);
3009 bitmap_set0(trunks
, 0);
3011 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
3018 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
3019 free(bundle
->trunks
);
3020 if (trunks
== s
->trunks
) {
3021 bundle
->trunks
= vlan_bitmap_clone(trunks
);
3023 bundle
->trunks
= trunks
;
3028 if (trunks
!= s
->trunks
) {
3033 if (!ovs_list_is_short(&bundle
->ports
)) {
3034 bundle
->ofproto
->has_bonded_bundles
= true;
3036 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
3037 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3040 bundle
->bond
= bond_create(s
->bond
, ofproto
);
3041 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3044 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3045 bond_slave_register(bundle
->bond
, port
,
3046 port
->up
.ofp_port
, port
->up
.netdev
);
3049 bond_unref(bundle
->bond
);
3050 bundle
->bond
= NULL
;
3053 /* If we changed something that would affect MAC learning, un-learn
3054 * everything on this port and force flow revalidation. */
3056 bundle_flush_macs(bundle
, false);
3063 bundle_remove(struct ofport
*port_
)
3065 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
3066 struct ofbundle
*bundle
= port
->bundle
;
3069 bundle_del_port(port
);
3070 if (ovs_list_is_empty(&bundle
->ports
)) {
3071 bundle_destroy(bundle
);
3072 } else if (ovs_list_is_short(&bundle
->ports
)) {
3073 bond_unref(bundle
->bond
);
3074 bundle
->bond
= NULL
;
3080 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
3082 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
3083 struct ofport_dpif
*port
= port_
;
3087 error
= netdev_get_etheraddr(port
->up
.netdev
, &ea
);
3089 struct dp_packet packet
;
3092 dp_packet_init(&packet
, 0);
3093 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
3095 memcpy(packet_pdu
, pdu
, pdu_size
);
3097 ofproto_dpif_send_packet(port
, false, &packet
);
3098 dp_packet_uninit(&packet
);
3100 VLOG_ERR_RL(&rl
, "port %s: cannot obtain Ethernet address of iface "
3101 "%s (%s)", port
->bundle
->name
,
3102 netdev_get_name(port
->up
.netdev
), ovs_strerror(error
));
3107 bundle_send_learning_packets(struct ofbundle
*bundle
)
3109 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
3110 int error
, n_packets
, n_errors
;
3111 struct mac_entry
*e
;
3113 struct ovs_list list_node
;
3114 struct ofport_dpif
*port
;
3115 struct dp_packet
*pkt
;
3117 struct ovs_list packets
;
3119 ovs_list_init(&packets
);
3120 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
3121 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
3122 if (mac_entry_get_port(ofproto
->ml
, e
) != bundle
) {
3123 pkt_node
= xmalloc(sizeof *pkt_node
);
3124 pkt_node
->pkt
= bond_compose_learning_packet(bundle
->bond
,
3126 (void **)&pkt_node
->port
);
3127 ovs_list_push_back(&packets
, &pkt_node
->list_node
);
3130 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3132 error
= n_packets
= n_errors
= 0;
3133 LIST_FOR_EACH_POP (pkt_node
, list_node
, &packets
) {
3136 ret
= ofproto_dpif_send_packet(pkt_node
->port
, false, pkt_node
->pkt
);
3137 dp_packet_delete(pkt_node
->pkt
);
3147 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3148 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
3149 "packets, last error was: %s",
3150 bundle
->name
, n_errors
, n_packets
, ovs_strerror(error
));
3152 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3153 bundle
->name
, n_packets
);
3158 bundle_run(struct ofbundle
*bundle
)
3161 lacp_run(bundle
->lacp
, send_pdu_cb
);
3164 struct ofport_dpif
*port
;
3166 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3167 bond_slave_set_may_enable(bundle
->bond
, port
, port
->may_enable
);
3170 if (bond_run(bundle
->bond
, lacp_status(bundle
->lacp
))) {
3171 bundle
->ofproto
->backer
->need_revalidate
= REV_BOND
;
3174 if (bond_should_send_learning_packets(bundle
->bond
)) {
3175 bundle_send_learning_packets(bundle
);
3181 bundle_wait(struct ofbundle
*bundle
)
3184 lacp_wait(bundle
->lacp
);
3187 bond_wait(bundle
->bond
);
3194 mirror_set__(struct ofproto
*ofproto_
, void *aux
,
3195 const struct ofproto_mirror_settings
*s
)
3197 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3198 struct ofbundle
**srcs
, **dsts
;
3203 mirror_destroy(ofproto
->mbridge
, aux
);
3207 srcs
= xmalloc(s
->n_srcs
* sizeof *srcs
);
3208 dsts
= xmalloc(s
->n_dsts
* sizeof *dsts
);
3210 for (i
= 0; i
< s
->n_srcs
; i
++) {
3211 srcs
[i
] = bundle_lookup(ofproto
, s
->srcs
[i
]);
3214 for (i
= 0; i
< s
->n_dsts
; i
++) {
3215 dsts
[i
] = bundle_lookup(ofproto
, s
->dsts
[i
]);
3218 error
= mirror_set(ofproto
->mbridge
, aux
, s
->name
, srcs
, s
->n_srcs
, dsts
,
3219 s
->n_dsts
, s
->src_vlans
,
3220 bundle_lookup(ofproto
, s
->out_bundle
),
3221 s
->snaplen
, s
->out_vlan
);
3228 mirror_get_stats__(struct ofproto
*ofproto
, void *aux
,
3229 uint64_t *packets
, uint64_t *bytes
)
3231 return mirror_get_stats(ofproto_dpif_cast(ofproto
)->mbridge
, aux
, packets
,
3236 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
3238 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3239 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3240 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
3241 mac_learning_flush(ofproto
->ml
);
3243 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3248 is_mirror_output_bundle(const struct ofproto
*ofproto_
, void *aux
)
3250 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3251 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3252 return bundle
&& mirror_bundle_out(ofproto
->mbridge
, bundle
) != 0;
3256 forward_bpdu_changed(struct ofproto
*ofproto_
)
3258 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3259 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3263 set_mac_table_config(struct ofproto
*ofproto_
, unsigned int idle_time
,
3266 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3267 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3268 mac_learning_set_idle_time(ofproto
->ml
, idle_time
);
3269 mac_learning_set_max_entries(ofproto
->ml
, max_entries
);
3270 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3273 /* Configures multicast snooping on 'ofport' using the settings
3274 * defined in 's'. */
3276 set_mcast_snooping(struct ofproto
*ofproto_
,
3277 const struct ofproto_mcast_snooping_settings
*s
)
3279 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3281 /* Only revalidate flows if the configuration changed. */
3282 if (!s
!= !ofproto
->ms
) {
3283 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3288 ofproto
->ms
= mcast_snooping_create();
3291 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3292 mcast_snooping_set_idle_time(ofproto
->ms
, s
->idle_time
);
3293 mcast_snooping_set_max_entries(ofproto
->ms
, s
->max_entries
);
3294 if (mcast_snooping_set_flood_unreg(ofproto
->ms
, s
->flood_unreg
)) {
3295 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3297 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3299 mcast_snooping_unref(ofproto
->ms
);
3306 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3308 set_mcast_snooping_port(struct ofproto
*ofproto_
, void *aux
,
3309 const struct ofproto_mcast_snooping_port_settings
*s
)
3311 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3312 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3314 if (ofproto
->ms
&& s
) {
3315 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3316 mcast_snooping_set_port_flood(ofproto
->ms
, bundle
, s
->flood
);
3317 mcast_snooping_set_port_flood_reports(ofproto
->ms
, bundle
,
3319 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3327 struct ofport_dpif
*
3328 ofp_port_to_ofport(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
3330 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
3331 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
3335 ofproto_port_from_dpif_port(struct ofproto_dpif
*ofproto
,
3336 struct ofproto_port
*ofproto_port
,
3337 struct dpif_port
*dpif_port
)
3339 ofproto_port
->name
= dpif_port
->name
;
3340 ofproto_port
->type
= dpif_port
->type
;
3341 ofproto_port
->ofp_port
= odp_port_to_ofp_port(ofproto
, dpif_port
->port_no
);
3345 ofport_update_peer(struct ofport_dpif
*ofport
)
3347 const struct ofproto_dpif
*ofproto
;
3348 struct dpif_backer
*backer
;
3351 if (!netdev_vport_is_patch(ofport
->up
.netdev
)) {
3355 backer
= ofproto_dpif_cast(ofport
->up
.ofproto
)->backer
;
3356 backer
->need_revalidate
= REV_RECONFIGURE
;
3359 ofport
->peer
->peer
= NULL
;
3360 ofport
->peer
= NULL
;
3363 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3368 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
3369 struct ofport
*peer_ofport
;
3370 struct ofport_dpif
*peer
;
3373 if (ofproto
->backer
!= backer
) {
3377 peer_ofport
= shash_find_data(&ofproto
->up
.port_by_name
, peer_name
);
3382 peer
= ofport_dpif_cast(peer_ofport
);
3383 peer_peer
= netdev_vport_patch_peer(peer
->up
.netdev
);
3384 if (peer_peer
&& !strcmp(netdev_get_name(ofport
->up
.netdev
),
3386 ofport
->peer
= peer
;
3387 ofport
->peer
->peer
= ofport
;
3397 port_run(struct ofport_dpif
*ofport
)
3399 long long int carrier_seq
= netdev_get_carrier_resets(ofport
->up
.netdev
);
3400 bool carrier_changed
= carrier_seq
!= ofport
->carrier_seq
;
3401 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
3402 bool cfm_enable
= false;
3403 bool bfd_enable
= false;
3405 ofport
->carrier_seq
= carrier_seq
;
3408 int cfm_opup
= cfm_get_opup(ofport
->cfm
);
3410 cfm_enable
= !cfm_get_fault(ofport
->cfm
);
3412 if (cfm_opup
>= 0) {
3413 cfm_enable
= cfm_enable
&& cfm_opup
;
3418 bfd_enable
= bfd_forwarding(ofport
->bfd
);
3421 if (ofport
->bfd
|| ofport
->cfm
) {
3422 enable
= enable
&& (cfm_enable
|| bfd_enable
);
3425 if (ofport
->bundle
) {
3426 enable
= enable
&& lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
);
3427 if (carrier_changed
) {
3428 lacp_slave_carrier_changed(ofport
->bundle
->lacp
, ofport
);
3432 if (ofport
->may_enable
!= enable
) {
3433 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3435 ofproto
->backer
->need_revalidate
= REV_PORT_TOGGLED
;
3437 if (ofport
->rstp_port
) {
3438 rstp_port_set_mac_operational(ofport
->rstp_port
, enable
);
3442 ofport
->may_enable
= enable
;
3446 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
3447 struct ofproto_port
*ofproto_port
)
3449 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3450 struct dpif_port dpif_port
;
3453 if (sset_contains(&ofproto
->ghost_ports
, devname
)) {
3454 const char *type
= netdev_get_type_from_name(devname
);
3456 /* We may be called before ofproto->up.port_by_name is populated with
3457 * the appropriate ofport. For this reason, we must get the name and
3458 * type from the netdev layer directly. */
3460 const struct ofport
*ofport
;
3462 ofport
= shash_find_data(&ofproto
->up
.port_by_name
, devname
);
3463 ofproto_port
->ofp_port
= ofport
? ofport
->ofp_port
: OFPP_NONE
;
3464 ofproto_port
->name
= xstrdup(devname
);
3465 ofproto_port
->type
= xstrdup(type
);
3471 if (!sset_contains(&ofproto
->ports
, devname
)) {
3474 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
,
3475 devname
, &dpif_port
);
3477 ofproto_port_from_dpif_port(ofproto
, ofproto_port
, &dpif_port
);
3483 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
)
3485 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3486 const char *devname
= netdev_get_name(netdev
);
3487 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
3488 const char *dp_port_name
;
3490 if (netdev_vport_is_patch(netdev
)) {
3491 sset_add(&ofproto
->ghost_ports
, netdev_get_name(netdev
));
3495 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
3496 if (!dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
3497 odp_port_t port_no
= ODPP_NONE
;
3500 error
= dpif_port_add(ofproto
->backer
->dpif
, netdev
, &port_no
);
3504 if (netdev_get_tunnel_config(netdev
)) {
3505 simap_put(&ofproto
->backer
->tnl_backers
,
3506 dp_port_name
, odp_to_u32(port_no
));
3510 if (netdev_get_tunnel_config(netdev
)) {
3511 sset_add(&ofproto
->ghost_ports
, devname
);
3513 sset_add(&ofproto
->ports
, devname
);
3519 port_del(struct ofproto
*ofproto_
, ofp_port_t ofp_port
)
3521 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3522 struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
3529 sset_find_and_delete(&ofproto
->ghost_ports
,
3530 netdev_get_name(ofport
->up
.netdev
));
3531 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3532 if (!ofport
->is_tunnel
&& !netdev_vport_is_patch(ofport
->up
.netdev
)) {
3533 error
= dpif_port_del(ofproto
->backer
->dpif
, ofport
->odp_port
);
3535 /* The caller is going to close ofport->up.netdev. If this is a
3536 * bonded port, then the bond is using that netdev, so remove it
3537 * from the bond. The client will need to reconfigure everything
3538 * after deleting ports, so then the slave will get re-added. */
3539 bundle_remove(&ofport
->up
);
3546 port_set_config(const struct ofport
*ofport_
, const struct smap
*cfg
)
3548 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3549 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3551 if (sset_contains(&ofproto
->ghost_ports
,
3552 netdev_get_name(ofport
->up
.netdev
))) {
3556 return dpif_port_set_config(ofproto
->backer
->dpif
, ofport
->odp_port
, cfg
);
3560 port_get_stats(const struct ofport
*ofport_
, struct netdev_stats
*stats
)
3562 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3565 error
= netdev_get_stats(ofport
->up
.netdev
, stats
);
3567 if (!error
&& ofport_
->ofp_port
== OFPP_LOCAL
) {
3568 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3570 ovs_mutex_lock(&ofproto
->stats_mutex
);
3571 /* ofproto->stats.tx_packets represents packets that we created
3572 * internally and sent to some port (e.g. packets sent with
3573 * ofproto_dpif_send_packet()). Account for them as if they had
3574 * come from OFPP_LOCAL and got forwarded. */
3576 if (stats
->rx_packets
!= UINT64_MAX
) {
3577 stats
->rx_packets
+= ofproto
->stats
.tx_packets
;
3580 if (stats
->rx_bytes
!= UINT64_MAX
) {
3581 stats
->rx_bytes
+= ofproto
->stats
.tx_bytes
;
3584 /* ofproto->stats.rx_packets represents packets that were received on
3585 * some port and we processed internally and dropped (e.g. STP).
3586 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3588 if (stats
->tx_packets
!= UINT64_MAX
) {
3589 stats
->tx_packets
+= ofproto
->stats
.rx_packets
;
3592 if (stats
->tx_bytes
!= UINT64_MAX
) {
3593 stats
->tx_bytes
+= ofproto
->stats
.rx_bytes
;
3595 ovs_mutex_unlock(&ofproto
->stats_mutex
);
3602 port_get_lacp_stats(const struct ofport
*ofport_
, struct lacp_slave_stats
*stats
)
3604 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3605 if (ofport
->bundle
&& ofport
->bundle
->lacp
) {
3606 if (lacp_get_slave_stats(ofport
->bundle
->lacp
, ofport
, stats
)) {
3613 struct port_dump_state
{
3614 struct sset_position pos
;
3617 struct ofproto_port port
;
3622 port_dump_start(const struct ofproto
*ofproto_ OVS_UNUSED
, void **statep
)
3624 *statep
= xzalloc(sizeof(struct port_dump_state
));
3629 port_dump_next(const struct ofproto
*ofproto_
, void *state_
,
3630 struct ofproto_port
*port
)
3632 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3633 struct port_dump_state
*state
= state_
;
3634 const struct sset
*sset
;
3635 struct sset_node
*node
;
3637 if (state
->has_port
) {
3638 ofproto_port_destroy(&state
->port
);
3639 state
->has_port
= false;
3641 sset
= state
->ghost
? &ofproto
->ghost_ports
: &ofproto
->ports
;
3642 while ((node
= sset_at_position(sset
, &state
->pos
))) {
3645 error
= port_query_by_name(ofproto_
, node
->name
, &state
->port
);
3647 *port
= state
->port
;
3648 state
->has_port
= true;
3650 } else if (error
!= ENODEV
) {
3655 if (!state
->ghost
) {
3656 state
->ghost
= true;
3657 memset(&state
->pos
, 0, sizeof state
->pos
);
3658 return port_dump_next(ofproto_
, state_
, port
);
3665 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
3667 struct port_dump_state
*state
= state_
;
3669 if (state
->has_port
) {
3670 ofproto_port_destroy(&state
->port
);
3677 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
3679 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3681 if (ofproto
->port_poll_errno
) {
3682 int error
= ofproto
->port_poll_errno
;
3683 ofproto
->port_poll_errno
= 0;
3687 if (sset_is_empty(&ofproto
->port_poll_set
)) {
3691 *devnamep
= sset_pop(&ofproto
->port_poll_set
);
3696 port_poll_wait(const struct ofproto
*ofproto_
)
3698 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3699 dpif_port_poll_wait(ofproto
->backer
->dpif
);
3703 port_is_lacp_current(const struct ofport
*ofport_
)
3705 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3706 return (ofport
->bundle
&& ofport
->bundle
->lacp
3707 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
3711 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3712 * then delete it entirely. */
3714 rule_expire(struct rule_dpif
*rule
, long long now
)
3715 OVS_REQUIRES(ofproto_mutex
)
3717 uint16_t hard_timeout
, idle_timeout
;
3720 hard_timeout
= rule
->up
.hard_timeout
;
3721 idle_timeout
= rule
->up
.idle_timeout
;
3723 /* Has 'rule' expired? */
3725 long long int modified
;
3727 ovs_mutex_lock(&rule
->up
.mutex
);
3728 modified
= rule
->up
.modified
;
3729 ovs_mutex_unlock(&rule
->up
.mutex
);
3731 if (now
> modified
+ hard_timeout
* 1000) {
3732 reason
= OFPRR_HARD_TIMEOUT
;
3736 if (reason
< 0 && idle_timeout
) {
3739 ovs_mutex_lock(&rule
->stats_mutex
);
3740 used
= rule
->stats
.used
;
3741 ovs_mutex_unlock(&rule
->stats_mutex
);
3743 if (now
> used
+ idle_timeout
* 1000) {
3744 reason
= OFPRR_IDLE_TIMEOUT
;
3749 COVERAGE_INC(ofproto_dpif_expired
);
3750 ofproto_rule_expire(&rule
->up
, reason
);
3755 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif
*ofproto
,
3756 ofp_port_t in_port
, struct dp_packet
*packet
)
3758 if (in_port
== OFPP_NONE
) {
3759 in_port
= OFPP_LOCAL
;
3761 packet
->md
.in_port
.odp_port
= ofp_port_to_odp_port(ofproto
, in_port
);
3765 ofproto_dpif_execute_actions__(struct ofproto_dpif
*ofproto
,
3766 const struct flow
*flow
,
3767 struct rule_dpif
*rule
,
3768 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3769 int indentation
, int depth
, int resubmits
,
3770 struct dp_packet
*packet
)
3772 struct dpif_flow_stats stats
;
3773 struct xlate_out xout
;
3774 struct xlate_in xin
;
3775 struct dpif_execute execute
;
3778 ovs_assert((rule
!= NULL
) != (ofpacts
!= NULL
));
3780 dpif_flow_stats_extract(flow
, packet
, time_msec(), &stats
);
3783 rule_dpif_credit_stats(rule
, &stats
);
3786 uint64_t odp_actions_stub
[1024 / 8];
3787 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
3788 xlate_in_init(&xin
, ofproto
, flow
, flow
->in_port
.ofp_port
, rule
,
3789 stats
.tcp_flags
, packet
, NULL
, &odp_actions
);
3790 xin
.ofpacts
= ofpacts
;
3791 xin
.ofpacts_len
= ofpacts_len
;
3792 xin
.resubmit_stats
= &stats
;
3793 xin
.indentation
= indentation
;
3795 xin
.resubmits
= resubmits
;
3796 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
3801 execute
.actions
= odp_actions
.data
;
3802 execute
.actions_len
= odp_actions
.size
;
3804 pkt_metadata_from_flow(&packet
->md
, flow
);
3805 execute
.packet
= packet
;
3806 execute
.flow
= flow
;
3807 execute
.needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
3808 execute
.probe
= false;
3811 /* Fix up in_port. */
3812 ofproto_dpif_set_packet_odp_port(ofproto
, flow
->in_port
.ofp_port
, packet
);
3814 error
= dpif_execute(ofproto
->backer
->dpif
, &execute
);
3816 xlate_out_uninit(&xout
);
3817 ofpbuf_uninit(&odp_actions
);
3822 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3823 * 'flow' must reflect the data in 'packet'. */
3825 ofproto_dpif_execute_actions(struct ofproto_dpif
*ofproto
,
3826 const struct flow
*flow
,
3827 struct rule_dpif
*rule
,
3828 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3829 struct dp_packet
*packet
)
3831 return ofproto_dpif_execute_actions__(ofproto
, flow
, rule
, ofpacts
,
3832 ofpacts_len
, 0, 0, 0, packet
);
3836 rule_dpif_credit_stats(struct rule_dpif
*rule
,
3837 const struct dpif_flow_stats
*stats
)
3839 ovs_mutex_lock(&rule
->stats_mutex
);
3840 if (OVS_UNLIKELY(rule
->new_rule
)) {
3841 rule_dpif_credit_stats(rule
->new_rule
, stats
);
3843 rule
->stats
.n_packets
+= stats
->n_packets
;
3844 rule
->stats
.n_bytes
+= stats
->n_bytes
;
3845 rule
->stats
.used
= MAX(rule
->stats
.used
, stats
->used
);
3847 ovs_mutex_unlock(&rule
->stats_mutex
);
3851 rule_dpif_get_flow_cookie(const struct rule_dpif
*rule
)
3852 OVS_REQUIRES(rule
->up
.mutex
)
3854 return rule
->up
.flow_cookie
;
3858 rule_dpif_reduce_timeouts(struct rule_dpif
*rule
, uint16_t idle_timeout
,
3859 uint16_t hard_timeout
)
3861 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
3864 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3865 * be read until the calling thread quiesces. */
3866 const struct rule_actions
*
3867 rule_dpif_get_actions(const struct rule_dpif
*rule
)
3869 return rule_get_actions(&rule
->up
);
3872 /* Sets 'rule''s recirculation id. */
3874 rule_dpif_set_recirc_id(struct rule_dpif
*rule
, uint32_t id
)
3875 OVS_REQUIRES(rule
->up
.mutex
)
3877 ovs_assert(!rule
->recirc_id
|| rule
->recirc_id
== id
);
3878 if (rule
->recirc_id
== id
) {
3879 /* Release the new reference to the same id. */
3882 rule
->recirc_id
= id
;
3886 /* Sets 'rule''s recirculation id. */
3888 rule_set_recirc_id(struct rule
*rule_
, uint32_t id
)
3890 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3892 ovs_mutex_lock(&rule
->up
.mutex
);
3893 rule_dpif_set_recirc_id(rule
, id
);
3894 ovs_mutex_unlock(&rule
->up
.mutex
);
3898 ofproto_dpif_get_tables_version(struct ofproto_dpif
*ofproto OVS_UNUSED
)
3900 ovs_version_t version
;
3902 atomic_read_relaxed(&ofproto
->tables_version
, &version
);
3907 /* The returned rule (if any) is valid at least until the next RCU quiescent
3908 * period. If the rule needs to stay around longer, the caller should take
3911 * 'flow' is non-const to allow for temporary modifications during the lookup.
3912 * Any changes are restored before returning. */
3913 static struct rule_dpif
*
3914 rule_dpif_lookup_in_table(struct ofproto_dpif
*ofproto
, ovs_version_t version
,
3915 uint8_t table_id
, struct flow
*flow
,
3916 struct flow_wildcards
*wc
)
3918 struct classifier
*cls
= &ofproto
->up
.tables
[table_id
].cls
;
3919 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls
, version
,
3923 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3924 * table '*table_id'. Returns the rule that was found, which may be one of the
3925 * special rules according to packet miss hadling. If 'may_packet_in' is
3926 * false, returning of the miss_rule (which issues packet ins for the
3927 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3928 * that were used during the lookup.
3930 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3931 * if none is found then the table miss configuration for that table is
3932 * honored, which can result in additional lookups in other OpenFlow tables.
3933 * In this case the function updates '*table_id' to reflect the final OpenFlow
3934 * table that was searched.
3936 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3939 * The rule is returned in '*rule', which is valid at least until the next
3940 * RCU quiescent period. If the '*rule' needs to stay around longer, the
3941 * caller must take a reference.
3943 * 'in_port' allows the lookup to take place as if the in port had the value
3944 * 'in_port'. This is needed for resubmit action support.
3946 * 'flow' is non-const to allow for temporary modifications during the lookup.
3947 * Any changes are restored before returning. */
3949 rule_dpif_lookup_from_table(struct ofproto_dpif
*ofproto
,
3950 ovs_version_t version
, struct flow
*flow
,
3951 struct flow_wildcards
*wc
,
3952 const struct dpif_flow_stats
*stats
,
3953 uint8_t *table_id
, ofp_port_t in_port
,
3954 bool may_packet_in
, bool honor_table_miss
)
3956 ovs_be16 old_tp_src
= flow
->tp_src
, old_tp_dst
= flow
->tp_dst
;
3957 ofp_port_t old_in_port
= flow
->in_port
.ofp_port
;
3958 enum ofputil_table_miss miss_config
;
3959 struct rule_dpif
*rule
;
3962 /* We always unwildcard nw_frag (for IP), so they
3963 * need not be unwildcarded here. */
3964 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
3965 && ofproto
->up
.frag_handling
!= OFPUTIL_FRAG_NX_MATCH
) {
3966 if (ofproto
->up
.frag_handling
== OFPUTIL_FRAG_NORMAL
) {
3967 /* We must pretend that transport ports are unavailable. */
3968 flow
->tp_src
= htons(0);
3969 flow
->tp_dst
= htons(0);
3971 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
3972 * Use the drop_frags_rule (which cannot disappear). */
3973 rule
= ofproto
->drop_frags_rule
;
3975 struct oftable
*tbl
= &ofproto
->up
.tables
[*table_id
];
3978 atomic_add_relaxed(&tbl
->n_matched
, stats
->n_packets
, &orig
);
3984 /* Look up a flow with 'in_port' as the input port. Then restore the
3985 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3986 * have surprising behavior). */
3987 flow
->in_port
.ofp_port
= in_port
;
3989 /* Our current implementation depends on n_tables == N_TABLES, and
3990 * TBL_INTERNAL being the last table. */
3991 BUILD_ASSERT_DECL(N_TABLES
== TBL_INTERNAL
+ 1);
3993 miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
3995 for (next_id
= *table_id
;
3996 next_id
< ofproto
->up
.n_tables
;
3997 next_id
++, next_id
+= (next_id
== TBL_INTERNAL
))
3999 *table_id
= next_id
;
4000 rule
= rule_dpif_lookup_in_table(ofproto
, version
, next_id
, flow
, wc
);
4002 struct oftable
*tbl
= &ofproto
->up
.tables
[next_id
];
4005 atomic_add_relaxed(rule
? &tbl
->n_matched
: &tbl
->n_missed
,
4006 stats
->n_packets
, &orig
);
4009 goto out
; /* Match. */
4011 if (honor_table_miss
) {
4012 miss_config
= ofproto_table_get_miss_config(&ofproto
->up
,
4014 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
4021 rule
= ofproto
->no_packet_in_rule
;
4022 if (may_packet_in
) {
4023 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
4024 || miss_config
== OFPUTIL_TABLE_MISS_CONTROLLER
) {
4025 struct ofport_dpif
*port
;
4027 port
= ofp_port_to_ofport(ofproto
, old_in_port
);
4029 VLOG_WARN_RL(&rl
, "packet-in on unknown OpenFlow port %"PRIu16
,
4031 } else if (!(port
->up
.pp
.config
& OFPUTIL_PC_NO_PACKET_IN
)) {
4032 rule
= ofproto
->miss_rule
;
4034 } else if (miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
&&
4035 connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
)) {
4036 rule
= ofproto
->miss_rule
;
4040 /* Restore port numbers, as they may have been modified above. */
4041 flow
->tp_src
= old_tp_src
;
4042 flow
->tp_dst
= old_tp_dst
;
4043 /* Restore the old in port. */
4044 flow
->in_port
.ofp_port
= old_in_port
;
4049 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
4051 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
4054 static struct rule
*
4057 struct rule_dpif
*rule
= xzalloc(sizeof *rule
);
4062 rule_dealloc(struct rule
*rule_
)
4064 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4069 check_mask(struct ofproto_dpif
*ofproto
, const struct miniflow
*flow
)
4071 const struct odp_support
*support
;
4072 uint16_t ct_state
, ct_zone
;
4076 support
= &ofproto_dpif_get_support(ofproto
)->odp
;
4077 ct_state
= MINIFLOW_GET_U16(flow
, ct_state
);
4078 if (support
->ct_state
&& support
->ct_zone
&& support
->ct_mark
4079 && support
->ct_label
&& support
->ct_state_nat
) {
4080 return ct_state
& CS_UNSUPPORTED_MASK
? OFPERR_OFPBMC_BAD_MASK
: 0;
4083 ct_zone
= MINIFLOW_GET_U16(flow
, ct_zone
);
4084 ct_mark
= MINIFLOW_GET_U32(flow
, ct_mark
);
4085 ct_label
= MINIFLOW_GET_U128(flow
, ct_label
);
4087 if ((ct_state
&& !support
->ct_state
)
4088 || (ct_state
& CS_UNSUPPORTED_MASK
)
4089 || ((ct_state
& (CS_SRC_NAT
| CS_DST_NAT
)) && !support
->ct_state_nat
)
4090 || (ct_zone
&& !support
->ct_zone
)
4091 || (ct_mark
&& !support
->ct_mark
)
4092 || (!ovs_u128_is_zero(ct_label
) && !support
->ct_label
)) {
4093 return OFPERR_OFPBMC_BAD_MASK
;
4100 check_actions(const struct ofproto_dpif
*ofproto
,
4101 const struct rule_actions
*const actions
)
4103 const struct ofpact
*ofpact
;
4105 OFPACT_FOR_EACH (ofpact
, actions
->ofpacts
, actions
->ofpacts_len
) {
4106 const struct odp_support
*support
;
4107 const struct ofpact_conntrack
*ct
;
4108 const struct ofpact
*a
;
4110 if (ofpact
->type
!= OFPACT_CT
) {
4114 ct
= CONTAINER_OF(ofpact
, struct ofpact_conntrack
, ofpact
);
4115 support
= &ofproto_dpif_get_support(ofproto
)->odp
;
4117 if (!support
->ct_state
) {
4118 return OFPERR_OFPBAC_BAD_TYPE
;
4120 if ((ct
->zone_imm
|| ct
->zone_src
.field
) && !support
->ct_zone
) {
4121 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4124 OFPACT_FOR_EACH(a
, ct
->actions
, ofpact_ct_get_action_len(ct
)) {
4125 const struct mf_field
*dst
= ofpact_get_mf_dst(a
);
4127 if (a
->type
== OFPACT_NAT
&& !support
->ct_state_nat
) {
4128 /* The backer doesn't seem to support the NAT bits in
4129 * 'ct_state': assume that it doesn't support the NAT
4131 return OFPERR_OFPBAC_BAD_TYPE
;
4133 if (dst
&& ((dst
->id
== MFF_CT_MARK
&& !support
->ct_mark
)
4134 || (dst
->id
== MFF_CT_LABEL
&& !support
->ct_label
))) {
4135 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
4144 rule_check(struct rule
*rule
)
4146 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->ofproto
);
4149 err
= check_mask(ofproto
, &rule
->cr
.match
.mask
->masks
);
4153 return check_actions(ofproto
, rule
->actions
);
4157 rule_construct(struct rule
*rule_
)
4158 OVS_NO_THREAD_SAFETY_ANALYSIS
4160 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4163 error
= rule_check(rule_
);
4168 ovs_mutex_init_adaptive(&rule
->stats_mutex
);
4169 rule
->stats
.n_packets
= 0;
4170 rule
->stats
.n_bytes
= 0;
4171 rule
->stats
.used
= rule
->up
.modified
;
4172 rule
->recirc_id
= 0;
4173 rule
->new_rule
= NULL
;
4179 rule_insert(struct rule
*rule_
, struct rule
*old_rule_
, bool forward_stats
)
4180 OVS_REQUIRES(ofproto_mutex
)
4182 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4184 if (old_rule_
&& forward_stats
) {
4185 struct rule_dpif
*old_rule
= rule_dpif_cast(old_rule_
);
4187 ovs_assert(!old_rule
->new_rule
);
4189 /* Take a reference to the new rule, and refer all stats updates from
4190 * the old rule to the new rule. */
4191 rule_dpif_ref(rule
);
4193 ovs_mutex_lock(&old_rule
->stats_mutex
);
4194 ovs_mutex_lock(&rule
->stats_mutex
);
4195 old_rule
->new_rule
= rule
; /* Forward future stats. */
4196 rule
->stats
= old_rule
->stats
; /* Transfer stats to the new rule. */
4197 ovs_mutex_unlock(&rule
->stats_mutex
);
4198 ovs_mutex_unlock(&old_rule
->stats_mutex
);
4203 rule_destruct(struct rule
*rule_
)
4204 OVS_NO_THREAD_SAFETY_ANALYSIS
4206 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4208 ovs_mutex_destroy(&rule
->stats_mutex
);
4209 /* Release reference to the new rule, if any. */
4210 if (rule
->new_rule
) {
4211 rule_dpif_unref(rule
->new_rule
);
4213 if (rule
->recirc_id
) {
4214 recirc_free_id(rule
->recirc_id
);
4219 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
,
4220 long long int *used
)
4222 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4224 ovs_mutex_lock(&rule
->stats_mutex
);
4225 if (OVS_UNLIKELY(rule
->new_rule
)) {
4226 rule_get_stats(&rule
->new_rule
->up
, packets
, bytes
, used
);
4228 *packets
= rule
->stats
.n_packets
;
4229 *bytes
= rule
->stats
.n_bytes
;
4230 *used
= rule
->stats
.used
;
4232 ovs_mutex_unlock(&rule
->stats_mutex
);
4236 rule_dpif_execute(struct rule_dpif
*rule
, const struct flow
*flow
,
4237 struct dp_packet
*packet
)
4239 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
4241 ofproto_dpif_execute_actions(ofproto
, flow
, rule
, NULL
, 0, packet
);
4245 rule_execute(struct rule
*rule
, const struct flow
*flow
,
4246 struct dp_packet
*packet
)
4248 rule_dpif_execute(rule_dpif_cast(rule
), flow
, packet
);
4249 dp_packet_delete(packet
);
4253 static struct group_dpif
*group_dpif_cast(const struct ofgroup
*group
)
4255 return group
? CONTAINER_OF(group
, struct group_dpif
, up
) : NULL
;
4258 static struct ofgroup
*
4261 struct group_dpif
*group
= xzalloc(sizeof *group
);
4266 group_dealloc(struct ofgroup
*group_
)
4268 struct group_dpif
*group
= group_dpif_cast(group_
);
4273 group_construct_stats(struct group_dpif
*group
)
4274 OVS_REQUIRES(group
->stats_mutex
)
4276 struct ofputil_bucket
*bucket
;
4277 const struct ovs_list
*buckets
;
4279 group
->packet_count
= 0;
4280 group
->byte_count
= 0;
4282 buckets
= group_dpif_get_buckets(group
);
4283 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4284 bucket
->stats
.packet_count
= 0;
4285 bucket
->stats
.byte_count
= 0;
4290 group_dpif_credit_stats(struct group_dpif
*group
,
4291 struct ofputil_bucket
*bucket
,
4292 const struct dpif_flow_stats
*stats
)
4294 ovs_mutex_lock(&group
->stats_mutex
);
4295 group
->packet_count
+= stats
->n_packets
;
4296 group
->byte_count
+= stats
->n_bytes
;
4298 bucket
->stats
.packet_count
+= stats
->n_packets
;
4299 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4300 } else { /* Credit to all buckets */
4301 const struct ovs_list
*buckets
;
4303 buckets
= group_dpif_get_buckets(group
);
4304 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4305 bucket
->stats
.packet_count
+= stats
->n_packets
;
4306 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4309 ovs_mutex_unlock(&group
->stats_mutex
);
4313 group_construct(struct ofgroup
*group_
)
4315 struct group_dpif
*group
= group_dpif_cast(group_
);
4317 ovs_mutex_init_adaptive(&group
->stats_mutex
);
4318 ovs_mutex_lock(&group
->stats_mutex
);
4319 group_construct_stats(group
);
4320 ovs_mutex_unlock(&group
->stats_mutex
);
4325 group_destruct(struct ofgroup
*group_
)
4327 struct group_dpif
*group
= group_dpif_cast(group_
);
4328 ovs_mutex_destroy(&group
->stats_mutex
);
4332 group_modify(struct ofgroup
*group_
)
4334 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(group_
->ofproto
);
4336 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
4342 group_get_stats(const struct ofgroup
*group_
, struct ofputil_group_stats
*ogs
)
4344 struct group_dpif
*group
= group_dpif_cast(group_
);
4345 struct ofputil_bucket
*bucket
;
4346 const struct ovs_list
*buckets
;
4347 struct bucket_counter
*bucket_stats
;
4349 ovs_mutex_lock(&group
->stats_mutex
);
4350 ogs
->packet_count
= group
->packet_count
;
4351 ogs
->byte_count
= group
->byte_count
;
4353 buckets
= group_dpif_get_buckets(group
);
4354 bucket_stats
= ogs
->bucket_stats
;
4355 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4356 bucket_stats
->packet_count
= bucket
->stats
.packet_count
;
4357 bucket_stats
->byte_count
= bucket
->stats
.byte_count
;
4360 ovs_mutex_unlock(&group
->stats_mutex
);
4365 /* If the group exists, this function increments the groups's reference count.
4367 * Make sure to call group_dpif_unref() after no longer needing to maintain
4368 * a reference to the group. */
4370 group_dpif_lookup(struct ofproto_dpif
*ofproto
, uint32_t group_id
,
4373 struct ofgroup
*ofgroup
= ofproto_group_lookup(&ofproto
->up
, group_id
,
4375 return ofgroup
? group_dpif_cast(ofgroup
) : NULL
;
4378 const struct ovs_list
*
4379 group_dpif_get_buckets(const struct group_dpif
*group
)
4381 return &group
->up
.buckets
;
4384 enum ofp11_group_type
4385 group_dpif_get_type(const struct group_dpif
*group
)
4387 return group
->up
.type
;
4391 group_dpif_get_selection_method(const struct group_dpif
*group
)
4393 return group
->up
.props
.selection_method
;
4396 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4397 * supports a notion of an OAM flag, sets it if 'oam' is true.
4398 * May modify 'packet'.
4399 * Returns 0 if successful, otherwise a positive errno value. */
4401 ofproto_dpif_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
4402 struct dp_packet
*packet
)
4404 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
4407 error
= xlate_send_packet(ofport
, oam
, packet
);
4409 ovs_mutex_lock(&ofproto
->stats_mutex
);
4410 ofproto
->stats
.tx_packets
++;
4411 ofproto
->stats
.tx_bytes
+= dp_packet_size(packet
);
4412 ovs_mutex_unlock(&ofproto
->stats_mutex
);
4417 group_dpif_get_selection_method_param(const struct group_dpif
*group
)
4419 return group
->up
.props
.selection_method_param
;
4422 const struct field_array
*
4423 group_dpif_get_fields(const struct group_dpif
*group
)
4425 return &group
->up
.props
.fields
;
4428 /* Return the version string of the datapath that backs up
4432 get_datapath_version(const struct ofproto
*ofproto_
)
4434 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4436 return ofproto
->backer
->dp_version_string
;
4440 set_frag_handling(struct ofproto
*ofproto_
,
4441 enum ofputil_frag_handling frag_handling
)
4443 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4444 if (frag_handling
!= OFPUTIL_FRAG_REASM
) {
4445 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4453 packet_out(struct ofproto
*ofproto_
, struct dp_packet
*packet
,
4454 const struct flow
*flow
,
4455 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
4457 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4459 ofproto_dpif_execute_actions(ofproto
, flow
, NULL
, ofpacts
,
4460 ofpacts_len
, packet
);
4465 nxt_resume(struct ofproto
*ofproto_
,
4466 const struct ofputil_packet_in_private
*pin
)
4468 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4470 /* Translate pin into datapath actions. */
4471 uint64_t odp_actions_stub
[1024 / 8];
4472 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
4473 enum slow_path_reason slow
;
4474 enum ofperr error
= xlate_resume(ofproto
, pin
, &odp_actions
, &slow
);
4476 /* Steal 'pin->packet' and put it into a dp_packet. */
4477 struct dp_packet packet
;
4478 dp_packet_init(&packet
, pin
->public.packet_len
);
4479 dp_packet_put(&packet
, pin
->public.packet
, pin
->public.packet_len
);
4481 pkt_metadata_from_flow(&packet
.md
, &pin
->public.flow_metadata
.flow
);
4483 /* Fix up in_port. */
4484 ofproto_dpif_set_packet_odp_port(ofproto
,
4485 pin
->public.flow_metadata
.flow
.in_port
.ofp_port
,
4488 struct flow headers
;
4489 flow_extract(&packet
, &headers
);
4491 /* Execute the datapath actions on the packet. */
4492 struct dpif_execute execute
= {
4493 .actions
= odp_actions
.data
,
4494 .actions_len
= odp_actions
.size
,
4495 .needs_help
= (slow
& SLOW_ACTION
) != 0,
4499 dpif_execute(ofproto
->backer
->dpif
, &execute
);
4502 ofpbuf_uninit(&odp_actions
);
4503 dp_packet_uninit(&packet
);
4511 set_netflow(struct ofproto
*ofproto_
,
4512 const struct netflow_options
*netflow_options
)
4514 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4516 if (netflow_options
) {
4517 if (!ofproto
->netflow
) {
4518 ofproto
->netflow
= netflow_create();
4519 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4521 return netflow_set_options(ofproto
->netflow
, netflow_options
);
4522 } else if (ofproto
->netflow
) {
4523 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4524 netflow_unref(ofproto
->netflow
);
4525 ofproto
->netflow
= NULL
;
4532 get_netflow_ids(const struct ofproto
*ofproto_
,
4533 uint8_t *engine_type
, uint8_t *engine_id
)
4535 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4537 dpif_get_netflow_ids(ofproto
->backer
->dpif
, engine_type
, engine_id
);
4540 static struct ofproto_dpif
*
4541 ofproto_dpif_lookup(const char *name
)
4543 struct ofproto_dpif
*ofproto
;
4545 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_node
,
4546 hash_string(name
, 0), &all_ofproto_dpifs
) {
4547 if (!strcmp(ofproto
->up
.name
, name
)) {
4555 ofproto_unixctl_fdb_flush(struct unixctl_conn
*conn
, int argc
,
4556 const char *argv
[], void *aux OVS_UNUSED
)
4558 struct ofproto_dpif
*ofproto
;
4561 ofproto
= ofproto_dpif_lookup(argv
[1]);
4563 unixctl_command_reply_error(conn
, "no such bridge");
4566 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4567 mac_learning_flush(ofproto
->ml
);
4568 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4570 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4571 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4572 mac_learning_flush(ofproto
->ml
);
4573 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4577 unixctl_command_reply(conn
, "table successfully flushed");
4581 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn
*conn
, int argc
,
4582 const char *argv
[], void *aux OVS_UNUSED
)
4584 struct ofproto_dpif
*ofproto
;
4587 ofproto
= ofproto_dpif_lookup(argv
[1]);
4589 unixctl_command_reply_error(conn
, "no such bridge");
4593 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4594 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4597 mcast_snooping_mdb_flush(ofproto
->ms
);
4599 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4600 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4603 mcast_snooping_mdb_flush(ofproto
->ms
);
4607 unixctl_command_reply(conn
, "table successfully flushed");
4610 static struct ofport_dpif
*
4611 ofbundle_get_a_port(const struct ofbundle
*bundle
)
4613 return CONTAINER_OF(ovs_list_front(&bundle
->ports
), struct ofport_dpif
,
4618 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4619 const char *argv
[], void *aux OVS_UNUSED
)
4621 struct ds ds
= DS_EMPTY_INITIALIZER
;
4622 const struct ofproto_dpif
*ofproto
;
4623 const struct mac_entry
*e
;
4625 ofproto
= ofproto_dpif_lookup(argv
[1]);
4627 unixctl_command_reply_error(conn
, "no such bridge");
4631 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
4632 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
4633 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
4634 struct ofbundle
*bundle
= mac_entry_get_port(ofproto
->ml
, e
);
4635 char name
[OFP_MAX_PORT_NAME_LEN
];
4637 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4639 ds_put_format(&ds
, "%5s %4d "ETH_ADDR_FMT
" %3d\n",
4640 name
, e
->vlan
, ETH_ADDR_ARGS(e
->mac
),
4641 mac_entry_age(ofproto
->ml
, e
));
4643 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4644 unixctl_command_reply(conn
, ds_cstr(&ds
));
4649 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn
*conn
,
4650 int argc OVS_UNUSED
,
4652 void *aux OVS_UNUSED
)
4654 struct ds ds
= DS_EMPTY_INITIALIZER
;
4655 const struct ofproto_dpif
*ofproto
;
4656 const struct ofbundle
*bundle
;
4657 const struct mcast_group
*grp
;
4658 struct mcast_group_bundle
*b
;
4659 struct mcast_mrouter_bundle
*mrouter
;
4661 ofproto
= ofproto_dpif_lookup(argv
[1]);
4663 unixctl_command_reply_error(conn
, "no such bridge");
4667 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4668 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4672 ds_put_cstr(&ds
, " port VLAN GROUP Age\n");
4673 ovs_rwlock_rdlock(&ofproto
->ms
->rwlock
);
4674 LIST_FOR_EACH (grp
, group_node
, &ofproto
->ms
->group_lru
) {
4675 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
4676 char name
[OFP_MAX_PORT_NAME_LEN
];
4679 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4681 ds_put_format(&ds
, "%5s %4d ", name
, grp
->vlan
);
4682 ipv6_format_mapped(&grp
->addr
, &ds
);
4683 ds_put_format(&ds
, " %3d\n",
4684 mcast_bundle_age(ofproto
->ms
, b
));
4688 /* ports connected to multicast routers */
4689 LIST_FOR_EACH(mrouter
, mrouter_node
, &ofproto
->ms
->mrouter_lru
) {
4690 char name
[OFP_MAX_PORT_NAME_LEN
];
4692 bundle
= mrouter
->port
;
4693 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4695 ds_put_format(&ds
, "%5s %4d querier %3d\n",
4696 name
, mrouter
->vlan
,
4697 mcast_mrouter_age(ofproto
->ms
, mrouter
));
4699 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
4700 unixctl_command_reply(conn
, ds_cstr(&ds
));
4705 struct xlate_out xout
;
4706 struct xlate_in xin
;
4707 const struct flow
*key
;
4710 struct flow_wildcards wc
;
4711 struct ofpbuf odp_actions
;
4715 trace_format_rule(struct ds
*result
, int level
, const struct rule_dpif
*rule
)
4717 const struct rule_actions
*actions
;
4720 ds_put_char_multiple(result
, '\t', level
);
4722 ds_put_cstr(result
, "No match\n");
4726 ovs_mutex_lock(&rule
->up
.mutex
);
4727 cookie
= rule
->up
.flow_cookie
;
4728 ovs_mutex_unlock(&rule
->up
.mutex
);
4730 ds_put_format(result
, "Rule: table=%"PRIu8
" cookie=%#"PRIx64
" ",
4731 rule
? rule
->up
.table_id
: 0, ntohll(cookie
));
4732 cls_rule_format(&rule
->up
.cr
, result
);
4733 ds_put_char(result
, '\n');
4735 actions
= rule_dpif_get_actions(rule
);
4737 ds_put_char_multiple(result
, '\t', level
);
4738 ds_put_cstr(result
, "OpenFlow actions=");
4739 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, result
);
4740 ds_put_char(result
, '\n');
4744 trace_format_flow(struct ds
*result
, int level
, const char *title
,
4745 struct trace_ctx
*trace
)
4747 ds_put_char_multiple(result
, '\t', level
);
4748 ds_put_format(result
, "%s: ", title
);
4749 /* Do not report unchanged flows for resubmits. */
4750 if ((level
> 0 && flow_equal(&trace
->xin
.flow
, &trace
->flow
))
4751 || (level
== 0 && flow_equal(&trace
->xin
.flow
, trace
->key
))) {
4752 ds_put_cstr(result
, "unchanged");
4754 flow_format(result
, &trace
->xin
.flow
);
4755 trace
->flow
= trace
->xin
.flow
;
4757 ds_put_char(result
, '\n');
4761 trace_format_regs(struct ds
*result
, int level
, const char *title
,
4762 struct trace_ctx
*trace
)
4766 ds_put_char_multiple(result
, '\t', level
);
4767 ds_put_format(result
, "%s:", title
);
4768 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
4769 ds_put_format(result
, " reg%"PRIuSIZE
"=0x%"PRIx32
, i
, trace
->flow
.regs
[i
]);
4771 ds_put_char(result
, '\n');
4775 trace_format_odp(struct ds
*result
, int level
, const char *title
,
4776 struct trace_ctx
*trace
)
4778 struct ofpbuf
*odp_actions
= &trace
->odp_actions
;
4780 ds_put_char_multiple(result
, '\t', level
);
4781 ds_put_format(result
, "%s: ", title
);
4782 format_odp_actions(result
, odp_actions
->data
, odp_actions
->size
);
4783 ds_put_char(result
, '\n');
4787 trace_format_megaflow(struct ds
*result
, int level
, const char *title
,
4788 struct trace_ctx
*trace
)
4792 ds_put_char_multiple(result
, '\t', level
);
4793 ds_put_format(result
, "%s: ", title
);
4794 match_init(&match
, trace
->key
, &trace
->wc
);
4795 match_format(&match
, result
, OFP_DEFAULT_PRIORITY
);
4796 ds_put_char(result
, '\n');
4799 static void trace_report(struct xlate_in
*, int indentation
,
4800 const char *format
, ...)
4801 OVS_PRINTF_FORMAT(3, 4);
4802 static void trace_report_valist(struct xlate_in
*, int indentation
,
4803 const char *format
, va_list args
)
4804 OVS_PRINTF_FORMAT(3, 0);
4807 trace_resubmit(struct xlate_in
*xin
, struct rule_dpif
*rule
, int indentation
)
4809 struct trace_ctx
*trace
= CONTAINER_OF(xin
, struct trace_ctx
, xin
);
4810 struct ds
*result
= trace
->result
;
4813 if (rule
== xin
->ofproto
->miss_rule
) {
4814 trace_report(xin
, indentation
,
4815 "No match, flow generates \"packet in\"s.");
4816 } else if (rule
== xin
->ofproto
->no_packet_in_rule
) {
4817 trace_report(xin
, indentation
, "No match, packets dropped because "
4818 "OFPPC_NO_PACKET_IN is set on in_port.");
4819 } else if (rule
== xin
->ofproto
->drop_frags_rule
) {
4820 trace_report(xin
, indentation
,
4821 "Packets dropped because they are IP fragments and "
4822 "the fragment handling mode is \"drop\".");
4826 ds_put_char(result
, '\n');
4828 trace_format_flow(result
, indentation
, "Resubmitted flow", trace
);
4829 trace_format_regs(result
, indentation
, "Resubmitted regs", trace
);
4830 trace_format_odp(result
, indentation
, "Resubmitted odp", trace
);
4831 trace_format_megaflow(result
, indentation
, "Resubmitted megaflow",
4834 trace_format_rule(result
, indentation
, rule
);
4838 trace_report_valist(struct xlate_in
*xin
, int indentation
,
4839 const char *format
, va_list args
)
4841 struct trace_ctx
*trace
= CONTAINER_OF(xin
, struct trace_ctx
, xin
);
4842 struct ds
*result
= trace
->result
;
4844 ds_put_char_multiple(result
, '\t', indentation
);
4845 ds_put_format_valist(result
, format
, args
);
4846 ds_put_char(result
, '\n');
4850 trace_report(struct xlate_in
*xin
, int indentation
, const char *format
, ...)
4854 va_start(args
, format
);
4855 trace_report_valist(xin
, indentation
, format
, args
);
4859 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4860 * forms are supported:
4862 * - [dpname] odp_flow [-generate | packet]
4863 * - bridge br_flow [-generate | packet]
4865 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4866 * returns a nonnull malloced error message. */
4867 static char * OVS_WARN_UNUSED_RESULT
4868 parse_flow_and_packet(int argc
, const char *argv
[],
4869 struct ofproto_dpif
**ofprotop
, struct flow
*flow
,
4870 struct dp_packet
**packetp
)
4872 const struct dpif_backer
*backer
= NULL
;
4873 const char *error
= NULL
;
4875 struct simap port_names
= SIMAP_INITIALIZER(&port_names
);
4876 struct dp_packet
*packet
;
4877 struct ofpbuf odp_key
;
4878 struct ofpbuf odp_mask
;
4880 ofpbuf_init(&odp_key
, 0);
4881 ofpbuf_init(&odp_mask
, 0);
4883 /* Handle "-generate" or a hex string as the last argument. */
4884 if (!strcmp(argv
[argc
- 1], "-generate")) {
4885 packet
= dp_packet_new(0);
4888 error
= eth_from_hex(argv
[argc
- 1], &packet
);
4891 } else if (argc
== 4) {
4892 /* The 3-argument form must end in "-generate' or a hex string. */
4898 /* odp_flow can have its in_port specified as a name instead of port no.
4899 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4900 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4901 * we need to create a simap of name to port no. */
4903 const char *dp_type
;
4904 if (!strncmp(argv
[1], "ovs-", 4)) {
4905 dp_type
= argv
[1] + 4;
4909 backer
= shash_find_data(&all_dpif_backers
, dp_type
);
4910 } else if (argc
== 2) {
4911 struct shash_node
*node
;
4912 if (shash_count(&all_dpif_backers
) == 1) {
4913 node
= shash_first(&all_dpif_backers
);
4914 backer
= node
->data
;
4917 error
= "Syntax error";
4920 if (backer
&& backer
->dpif
) {
4921 struct dpif_port dpif_port
;
4922 struct dpif_port_dump port_dump
;
4923 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, backer
->dpif
) {
4924 simap_put(&port_names
, dpif_port
.name
,
4925 odp_to_u32(dpif_port
.port_no
));
4929 /* Parse the flow and determine whether a datapath or
4930 * bridge is specified. If function odp_flow_key_from_string()
4931 * returns 0, the flow is a odp_flow. If function
4932 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4933 if (!odp_flow_from_string(argv
[argc
- 1], &port_names
,
4934 &odp_key
, &odp_mask
)) {
4936 error
= "Cannot find the datapath";
4940 if (odp_flow_key_to_flow(odp_key
.data
, odp_key
.size
, flow
) == ODP_FIT_ERROR
) {
4941 error
= "Failed to parse datapath flow key";
4945 *ofprotop
= xlate_lookup_ofproto(backer
, flow
,
4946 &flow
->in_port
.ofp_port
);
4947 if (*ofprotop
== NULL
) {
4948 error
= "Invalid datapath flow";
4952 char *err
= parse_ofp_exact_flow(flow
, NULL
, argv
[argc
- 1], NULL
);
4955 m_err
= xasprintf("Bad openflow flow syntax: %s", err
);
4960 error
= "Must specify bridge name";
4964 *ofprotop
= ofproto_dpif_lookup(argv
[1]);
4966 error
= "Unknown bridge name";
4972 /* Generate a packet, if requested. */
4974 if (!dp_packet_size(packet
)) {
4975 flow_compose(packet
, flow
);
4977 /* Use the metadata from the flow and the packet argument
4978 * to reconstruct the flow. */
4979 pkt_metadata_from_flow(&packet
->md
, flow
);
4980 flow_extract(packet
, flow
);
4985 if (error
&& !m_err
) {
4986 m_err
= xstrdup(error
);
4989 dp_packet_delete(packet
);
4993 ofpbuf_uninit(&odp_key
);
4994 ofpbuf_uninit(&odp_mask
);
4995 simap_destroy(&port_names
);
5000 ofproto_unixctl_trace(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
5001 void *aux OVS_UNUSED
)
5003 struct ofproto_dpif
*ofproto
;
5004 struct dp_packet
*packet
;
5008 error
= parse_flow_and_packet(argc
, argv
, &ofproto
, &flow
, &packet
);
5013 ofproto_trace(ofproto
, &flow
, packet
, NULL
, 0, &result
);
5014 unixctl_command_reply(conn
, ds_cstr(&result
));
5015 ds_destroy(&result
);
5016 dp_packet_delete(packet
);
5018 unixctl_command_reply_error(conn
, error
);
5024 ofproto_unixctl_trace_actions(struct unixctl_conn
*conn
, int argc
,
5025 const char *argv
[], void *aux OVS_UNUSED
)
5027 enum ofputil_protocol usable_protocols
;
5028 struct ofproto_dpif
*ofproto
;
5029 bool enforce_consistency
;
5030 struct ofpbuf ofpacts
;
5031 struct dp_packet
*packet
;
5036 /* Three kinds of error return values! */
5042 ofpbuf_init(&ofpacts
, 0);
5044 /* Parse actions. */
5045 error
= ofpacts_parse_actions(argv
[--argc
], &ofpacts
, &usable_protocols
);
5047 unixctl_command_reply_error(conn
, error
);
5052 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
5053 * consistency between the flow and the actions. With -consistent, we
5054 * enforce consistency even for a flow supported in OpenFlow 1.0. */
5055 if (!strcmp(argv
[1], "-consistent")) {
5056 enforce_consistency
= true;
5060 enforce_consistency
= false;
5063 error
= parse_flow_and_packet(argc
, argv
, &ofproto
, &flow
, &packet
);
5065 unixctl_command_reply_error(conn
, error
);
5070 /* Do the same checks as handle_packet_out() in ofproto.c.
5072 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
5073 * strictly correct because these actions aren't in any table, but it's OK
5074 * because it 'table_id' is used only to check goto_table instructions, but
5075 * packet-outs take a list of actions and therefore it can't include
5078 * We skip the "meter" check here because meter is an instruction, not an
5079 * action, and thus cannot appear in ofpacts. */
5080 in_port
= ofp_to_u16(flow
.in_port
.ofp_port
);
5081 if (in_port
>= ofproto
->up
.max_ports
&& in_port
< ofp_to_u16(OFPP_MAX
)) {
5082 unixctl_command_reply_error(conn
, "invalid in_port");
5085 if (enforce_consistency
) {
5086 retval
= ofpacts_check_consistency(ofpacts
.data
, ofpacts
.size
, &flow
,
5087 u16_to_ofp(ofproto
->up
.max_ports
),
5088 0, ofproto
->up
.n_tables
,
5091 retval
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &flow
,
5092 u16_to_ofp(ofproto
->up
.max_ports
), 0,
5093 ofproto
->up
.n_tables
, &usable_protocols
);
5096 retval
= ofproto_check_ofpacts(&ofproto
->up
, ofpacts
.data
,
5102 ds_put_format(&result
, "Bad actions: %s", ofperr_to_string(retval
));
5103 unixctl_command_reply_error(conn
, ds_cstr(&result
));
5107 ofproto_trace(ofproto
, &flow
, packet
,
5108 ofpacts
.data
, ofpacts
.size
, &result
);
5109 unixctl_command_reply(conn
, ds_cstr(&result
));
5112 ds_destroy(&result
);
5113 dp_packet_delete(packet
);
5114 ofpbuf_uninit(&ofpacts
);
5117 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
5118 * description of the results to 'ds'.
5120 * The trace follows a packet with the specified 'flow' through the flow
5121 * table. 'packet' may be nonnull to trace an actual packet, with consequent
5122 * side effects (if it is nonnull then its flow must be 'flow').
5124 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
5125 * trace, otherwise the actions are determined by a flow table lookup. */
5127 ofproto_trace(struct ofproto_dpif
*ofproto
, struct flow
*flow
,
5128 const struct dp_packet
*packet
,
5129 const struct ofpact ofpacts
[], size_t ofpacts_len
,
5132 struct trace_ctx trace
;
5133 enum xlate_error error
;
5135 ds_put_format(ds
, "Bridge: %s\n", ofproto
->up
.name
);
5136 ds_put_cstr(ds
, "Flow: ");
5137 flow_format(ds
, flow
);
5138 ds_put_char(ds
, '\n');
5140 ofpbuf_init(&trace
.odp_actions
, 0);
5143 trace
.key
= flow
; /* Original flow key, used for megaflow. */
5144 trace
.flow
= *flow
; /* May be modified by actions. */
5145 xlate_in_init(&trace
.xin
, ofproto
, flow
, flow
->in_port
.ofp_port
, NULL
,
5146 ntohs(flow
->tcp_flags
), packet
, &trace
.wc
,
5147 &trace
.odp_actions
);
5148 trace
.xin
.ofpacts
= ofpacts
;
5149 trace
.xin
.ofpacts_len
= ofpacts_len
;
5150 trace
.xin
.resubmit_hook
= trace_resubmit
;
5151 trace
.xin
.report_hook
= trace_report_valist
;
5153 error
= xlate_actions(&trace
.xin
, &trace
.xout
);
5154 ds_put_char(ds
, '\n');
5155 trace
.xin
.flow
.actset_output
= 0;
5156 trace_format_flow(ds
, 0, "Final flow", &trace
);
5157 trace_format_megaflow(ds
, 0, "Megaflow", &trace
);
5159 ds_put_cstr(ds
, "Datapath actions: ");
5160 format_odp_actions(ds
, trace
.odp_actions
.data
, trace
.odp_actions
.size
);
5162 if (error
!= XLATE_OK
) {
5163 ds_put_format(ds
, "\nTranslation failed (%s), packet is dropped.\n",
5164 xlate_strerror(error
));
5165 } else if (trace
.xout
.slow
) {
5166 enum slow_path_reason slow
;
5168 ds_put_cstr(ds
, "\nThis flow is handled by the userspace "
5169 "slow path because it:");
5171 slow
= trace
.xout
.slow
;
5173 enum slow_path_reason bit
= rightmost_1bit(slow
);
5175 ds_put_format(ds
, "\n\t- %s.",
5176 slow_path_reason_to_explanation(bit
));
5182 xlate_out_uninit(&trace
.xout
);
5183 ofpbuf_uninit(&trace
.odp_actions
);
5186 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5187 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5188 * to destroy 'ofproto_shash' and free the returned value. */
5189 static const struct shash_node
**
5190 get_ofprotos(struct shash
*ofproto_shash
)
5192 const struct ofproto_dpif
*ofproto
;
5194 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
5195 char *name
= xasprintf("%s@%s", ofproto
->up
.type
, ofproto
->up
.name
);
5196 shash_add_nocopy(ofproto_shash
, name
, ofproto
);
5199 return shash_sort(ofproto_shash
);
5203 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5204 const char *argv
[] OVS_UNUSED
,
5205 void *aux OVS_UNUSED
)
5207 struct ds ds
= DS_EMPTY_INITIALIZER
;
5208 struct shash ofproto_shash
;
5209 const struct shash_node
**sorted_ofprotos
;
5212 shash_init(&ofproto_shash
);
5213 sorted_ofprotos
= get_ofprotos(&ofproto_shash
);
5214 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
5215 const struct shash_node
*node
= sorted_ofprotos
[i
];
5216 ds_put_format(&ds
, "%s\n", node
->name
);
5219 shash_destroy(&ofproto_shash
);
5220 free(sorted_ofprotos
);
5222 unixctl_command_reply(conn
, ds_cstr(&ds
));
5227 dpif_show_backer(const struct dpif_backer
*backer
, struct ds
*ds
)
5229 const struct shash_node
**ofprotos
;
5230 struct dpif_dp_stats dp_stats
;
5231 struct shash ofproto_shash
;
5234 dpif_get_dp_stats(backer
->dpif
, &dp_stats
);
5236 ds_put_format(ds
, "%s: hit:%"PRIu64
" missed:%"PRIu64
"\n",
5237 dpif_name(backer
->dpif
), dp_stats
.n_hit
, dp_stats
.n_missed
);
5239 shash_init(&ofproto_shash
);
5240 ofprotos
= get_ofprotos(&ofproto_shash
);
5241 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
5242 struct ofproto_dpif
*ofproto
= ofprotos
[i
]->data
;
5243 const struct shash_node
**ports
;
5246 if (ofproto
->backer
!= backer
) {
5250 ds_put_format(ds
, "\t%s:\n", ofproto
->up
.name
);
5252 ports
= shash_sort(&ofproto
->up
.port_by_name
);
5253 for (j
= 0; j
< shash_count(&ofproto
->up
.port_by_name
); j
++) {
5254 const struct shash_node
*node
= ports
[j
];
5255 struct ofport
*ofport
= node
->data
;
5257 odp_port_t odp_port
;
5259 ds_put_format(ds
, "\t\t%s %u/", netdev_get_name(ofport
->netdev
),
5262 odp_port
= ofp_port_to_odp_port(ofproto
, ofport
->ofp_port
);
5263 if (odp_port
!= ODPP_NONE
) {
5264 ds_put_format(ds
, "%"PRIu32
":", odp_port
);
5266 ds_put_cstr(ds
, "none:");
5269 ds_put_format(ds
, " (%s", netdev_get_type(ofport
->netdev
));
5272 if (!netdev_get_config(ofport
->netdev
, &config
)) {
5273 const struct smap_node
**nodes
;
5276 nodes
= smap_sort(&config
);
5277 for (i
= 0; i
< smap_count(&config
); i
++) {
5278 const struct smap_node
*node
= nodes
[i
];
5279 ds_put_format(ds
, "%c %s=%s", i
? ',' : ':',
5280 node
->key
, node
->value
);
5284 smap_destroy(&config
);
5286 ds_put_char(ds
, ')');
5287 ds_put_char(ds
, '\n');
5291 shash_destroy(&ofproto_shash
);
5296 ofproto_unixctl_dpif_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5297 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
5299 struct ds ds
= DS_EMPTY_INITIALIZER
;
5300 const struct shash_node
**backers
;
5303 backers
= shash_sort(&all_dpif_backers
);
5304 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5305 dpif_show_backer(backers
[i
]->data
, &ds
);
5309 unixctl_command_reply(conn
, ds_cstr(&ds
));
5314 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn
*conn
,
5315 int argc OVS_UNUSED
, const char *argv
[],
5316 void *aux OVS_UNUSED
)
5318 const struct ofproto_dpif
*ofproto
;
5320 struct ds ds
= DS_EMPTY_INITIALIZER
;
5321 bool verbosity
= false;
5323 struct dpif_port dpif_port
;
5324 struct dpif_port_dump port_dump
;
5325 struct hmap portno_names
;
5327 struct dpif_flow_dump
*flow_dump
;
5328 struct dpif_flow_dump_thread
*flow_dump_thread
;
5332 ofproto
= ofproto_dpif_lookup(argv
[argc
- 1]);
5334 unixctl_command_reply_error(conn
, "no such bridge");
5338 if (argc
> 2 && !strcmp(argv
[1], "-m")) {
5342 hmap_init(&portno_names
);
5343 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, ofproto
->backer
->dpif
) {
5344 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
5348 flow_dump
= dpif_flow_dump_create(ofproto
->backer
->dpif
, false);
5349 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
5350 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
5353 if (odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
) == ODP_FIT_ERROR
5354 || xlate_lookup_ofproto(ofproto
->backer
, &flow
, NULL
) != ofproto
) {
5359 odp_format_ufid(&f
.ufid
, &ds
);
5360 ds_put_cstr(&ds
, " ");
5362 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
5363 &portno_names
, &ds
, verbosity
);
5364 ds_put_cstr(&ds
, ", ");
5365 dpif_flow_stats_format(&f
.stats
, &ds
);
5366 ds_put_cstr(&ds
, ", actions:");
5367 format_odp_actions(&ds
, f
.actions
, f
.actions_len
);
5368 ds_put_char(&ds
, '\n');
5370 dpif_flow_dump_thread_destroy(flow_dump_thread
);
5371 error
= dpif_flow_dump_destroy(flow_dump
);
5375 ds_put_format(&ds
, "dpif/dump_flows failed: %s", ovs_strerror(errno
));
5376 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
5378 unixctl_command_reply(conn
, ds_cstr(&ds
));
5380 odp_portno_names_destroy(&portno_names
);
5381 hmap_destroy(&portno_names
);
5386 ofproto_revalidate_all_backers(void)
5388 const struct shash_node
**backers
;
5391 backers
= shash_sort(&all_dpif_backers
);
5392 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5393 struct dpif_backer
*backer
= backers
[i
]->data
;
5394 backer
->need_revalidate
= REV_RECONFIGURE
;
5400 disable_tnl_push_pop(struct unixctl_conn
*conn OVS_UNUSED
, int argc OVS_UNUSED
,
5401 const char *argv
[], void *aux OVS_UNUSED
)
5403 if (!strcasecmp(argv
[1], "off")) {
5404 ofproto_use_tnl_push_pop
= false;
5405 unixctl_command_reply(conn
, "Tunnel push-pop off");
5406 ofproto_revalidate_all_backers();
5407 } else if (!strcasecmp(argv
[1], "on")) {
5408 ofproto_use_tnl_push_pop
= true;
5409 unixctl_command_reply(conn
, "Tunnel push-pop on");
5410 ofproto_revalidate_all_backers();
5412 unixctl_command_reply_error(conn
, "Invalid argument");
5417 disable_datapath_truncate(struct unixctl_conn
*conn OVS_UNUSED
,
5418 int argc OVS_UNUSED
,
5419 const char *argv
[] OVS_UNUSED
,
5420 void *aux OVS_UNUSED
)
5422 const struct shash_node
**backers
;
5425 backers
= shash_sort(&all_dpif_backers
);
5426 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5427 struct dpif_backer
*backer
= backers
[i
]->data
;
5428 backer
->support
.trunc
= false;
5431 unixctl_command_reply(conn
, "Datapath truncate action diabled");
5435 ofproto_unixctl_init(void)
5437 static bool registered
;
5443 unixctl_command_register(
5445 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5446 1, 3, ofproto_unixctl_trace
, NULL
);
5447 unixctl_command_register(
5448 "ofproto/trace-packet-out",
5449 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5450 2, 6, ofproto_unixctl_trace_actions
, NULL
);
5451 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5452 ofproto_unixctl_fdb_flush
, NULL
);
5453 unixctl_command_register("fdb/show", "bridge", 1, 1,
5454 ofproto_unixctl_fdb_show
, NULL
);
5455 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5456 ofproto_unixctl_mcast_snooping_flush
, NULL
);
5457 unixctl_command_register("mdb/show", "bridge", 1, 1,
5458 ofproto_unixctl_mcast_snooping_show
, NULL
);
5459 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5460 ofproto_unixctl_dpif_dump_dps
, NULL
);
5461 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show
,
5463 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5464 ofproto_unixctl_dpif_dump_flows
, NULL
);
5466 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5467 disable_tnl_push_pop
, NULL
);
5469 unixctl_command_register("dpif/disable-truncate", "", 0, 0,
5470 disable_datapath_truncate
, NULL
);
5473 /* Returns true if 'table' is the table used for internal rules,
5474 * false otherwise. */
5476 table_is_internal(uint8_t table_id
)
5478 return table_id
== TBL_INTERNAL
;
5483 ofp_port_to_odp_port(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
5485 const struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
5486 return ofport
? ofport
->odp_port
: ODPP_NONE
;
5489 struct ofport_dpif
*
5490 odp_port_to_ofport(const struct dpif_backer
*backer
, odp_port_t odp_port
)
5492 struct ofport_dpif
*port
;
5494 ovs_rwlock_rdlock(&backer
->odp_to_ofport_lock
);
5495 HMAP_FOR_EACH_IN_BUCKET (port
, odp_port_node
, hash_odp_port(odp_port
),
5496 &backer
->odp_to_ofport_map
) {
5497 if (port
->odp_port
== odp_port
) {
5498 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5503 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5508 odp_port_to_ofp_port(const struct ofproto_dpif
*ofproto
, odp_port_t odp_port
)
5510 struct ofport_dpif
*port
;
5512 port
= odp_port_to_ofport(ofproto
->backer
, odp_port
);
5513 if (port
&& &ofproto
->up
== port
->up
.ofproto
) {
5514 return port
->up
.ofp_port
;
5521 ofproto_dpif_add_internal_flow(struct ofproto_dpif
*ofproto
,
5522 const struct match
*match
, int priority
,
5523 uint16_t idle_timeout
,
5524 const struct ofpbuf
*ofpacts
,
5525 struct rule
**rulep
)
5527 struct ofproto_flow_mod ofm
;
5528 struct rule_dpif
*rule
;
5531 ofm
.fm
= (struct ofputil_flow_mod
) {
5533 .priority
= priority
,
5534 .table_id
= TBL_INTERNAL
,
5535 .command
= OFPFC_ADD
,
5536 .idle_timeout
= idle_timeout
,
5537 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
5538 .ofpacts
= ofpacts
->data
,
5539 .ofpacts_len
= ofpacts
->size
,
5540 .delete_reason
= OVS_OFPRR_NONE
,
5543 error
= ofproto_flow_mod(&ofproto
->up
, &ofm
);
5545 VLOG_ERR_RL(&rl
, "failed to add internal flow (%s)",
5546 ofperr_to_string(error
));
5551 rule
= rule_dpif_lookup_in_table(ofproto
,
5552 ofproto_dpif_get_tables_version(ofproto
),
5553 TBL_INTERNAL
, &ofm
.fm
.match
.flow
,
5564 ofproto_dpif_delete_internal_flow(struct ofproto_dpif
*ofproto
,
5565 struct match
*match
, int priority
)
5567 struct ofproto_flow_mod ofm
;
5570 ofm
.fm
= (struct ofputil_flow_mod
) {
5572 .priority
= priority
,
5573 .table_id
= TBL_INTERNAL
,
5574 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
5575 .command
= OFPFC_DELETE_STRICT
,
5578 error
= ofproto_flow_mod(&ofproto
->up
, &ofm
);
5580 VLOG_ERR_RL(&rl
, "failed to delete internal flow (%s)",
5581 ofperr_to_string(error
));
5589 ofproto_dpif_get_uuid(const struct ofproto_dpif
*ofproto
)
5591 return &ofproto
->uuid
;
5594 const struct ofproto_class ofproto_dpif_class
= {
5608 NULL
, /* get_memory_usage. */
5609 type_get_memory_usage
,
5629 port_is_lacp_current
,
5630 port_get_lacp_stats
,
5631 NULL
, /* rule_choose_table */
5635 NULL
, /* rule_delete */
5657 aa_vlan_get_queue_size
,
5664 get_stp_port_status
,
5669 get_rstp_port_status
,
5676 is_mirror_output_bundle
,
5677 forward_bpdu_changed
,
5678 set_mac_table_config
,
5680 set_mcast_snooping_port
,
5681 NULL
, /* meter_get_features */
5682 NULL
, /* meter_set */
5683 NULL
, /* meter_get */
5684 NULL
, /* meter_del */
5685 group_alloc
, /* group_alloc */
5686 group_construct
, /* group_construct */
5687 group_destruct
, /* group_destruct */
5688 group_dealloc
, /* group_dealloc */
5689 group_modify
, /* group_modify */
5690 group_get_stats
, /* group_get_stats */
5691 get_datapath_version
, /* get_datapath_version */