2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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.
19 #include "ofproto/ofproto-dpif.h"
20 #include "ofproto/ofproto-provider.h"
27 #include "byte-order.h"
28 #include "connectivity.h"
34 #include "dynamic-string.h"
35 #include "fail-open.h"
36 #include "guarded-list.h"
40 #include "mac-learning.h"
41 #include "mcast-snooping.h"
42 #include "meta-flow.h"
43 #include "multipath.h"
44 #include "netdev-vport.h"
49 #include "odp-execute.h"
52 #include "ofp-actions.h"
53 #include "ofp-parse.h"
54 #include "ofp-print.h"
55 #include "ofproto-dpif-ipfix.h"
56 #include "ofproto-dpif-mirror.h"
57 #include "ofproto-dpif-monitor.h"
58 #include "ofproto-dpif-rid.h"
59 #include "ofproto-dpif-sflow.h"
60 #include "ofproto-dpif-upcall.h"
61 #include "ofproto-dpif-xlate.h"
62 #include "poll-loop.h"
64 #include "ovs-router.h"
70 #include "unaligned.h"
72 #include "vlan-bitmap.h"
73 #include "openvswitch/vlog.h"
75 VLOG_DEFINE_THIS_MODULE(ofproto_dpif
);
77 COVERAGE_DEFINE(ofproto_dpif_expired
);
78 COVERAGE_DEFINE(packet_in_overflow
);
87 * - Do include packets and bytes from datapath flows which have not
88 * recently been processed by a revalidator. */
89 struct ovs_mutex stats_mutex
;
90 struct dpif_flow_stats stats OVS_GUARDED
;
92 /* If non-zero then the recirculation id that has
93 * been allocated for use with this rule.
94 * The recirculation id and associated internal flow should
95 * be freed when the rule is freed */
99 /* RULE_CAST() depends on this. */
100 BUILD_ASSERT_DECL(offsetof(struct rule_dpif
, up
) == 0);
102 static void rule_get_stats(struct rule
*, uint64_t *packets
, uint64_t *bytes
,
103 long long int *used
);
104 static struct rule_dpif
*rule_dpif_cast(const struct rule
*);
105 static void rule_expire(struct rule_dpif
*);
112 * - Do include packets and bytes from datapath flows which have not
113 * recently been processed by a revalidator. */
114 struct ovs_mutex stats_mutex
;
115 uint64_t packet_count OVS_GUARDED
; /* Number of packets received. */
116 uint64_t byte_count OVS_GUARDED
; /* Number of bytes received. */
120 struct hmap_node hmap_node
; /* In struct ofproto's "bundles" hmap. */
121 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
122 void *aux
; /* Key supplied by ofproto's client. */
123 char *name
; /* Identifier for log messages. */
126 struct ovs_list ports
; /* Contains "struct ofport"s. */
127 enum port_vlan_mode vlan_mode
; /* VLAN mode */
128 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
129 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
130 * NULL if all VLANs are trunked. */
131 struct lacp
*lacp
; /* LACP if LACP is enabled, otherwise NULL. */
132 struct bond
*bond
; /* Nonnull iff more than one port. */
133 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
136 bool floodable
; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
139 static void bundle_remove(struct ofport
*);
140 static void bundle_update(struct ofbundle
*);
141 static void bundle_destroy(struct ofbundle
*);
142 static void bundle_del_port(struct ofport_dpif
*);
143 static void bundle_run(struct ofbundle
*);
144 static void bundle_wait(struct ofbundle
*);
145 static void bundle_flush_macs(struct ofbundle
*, bool);
146 static void bundle_move(struct ofbundle
*, struct ofbundle
*);
148 static void stp_run(struct ofproto_dpif
*ofproto
);
149 static void stp_wait(struct ofproto_dpif
*ofproto
);
150 static int set_stp_port(struct ofport
*,
151 const struct ofproto_port_stp_settings
*);
153 static void rstp_run(struct ofproto_dpif
*ofproto
);
154 static void set_rstp_port(struct ofport
*,
155 const struct ofproto_port_rstp_settings
*);
158 struct hmap_node odp_port_node
; /* In dpif_backer's "odp_to_ofport_map". */
162 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
163 struct ovs_list bundle_node
;/* In struct ofbundle's "ports" list. */
164 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
165 struct bfd
*bfd
; /* BFD, if any. */
166 struct lldp
*lldp
; /* lldp, if any. */
167 bool may_enable
; /* May be enabled in bonds. */
168 bool is_tunnel
; /* This port is a tunnel. */
169 bool is_layer3
; /* This is a layer 3 port. */
170 long long int carrier_seq
; /* Carrier status changes. */
171 struct ofport_dpif
*peer
; /* Peer if patch port. */
174 struct stp_port
*stp_port
; /* Spanning Tree Protocol, if any. */
175 enum stp_state stp_state
; /* Always STP_DISABLED if STP not in use. */
176 long long int stp_state_entered
;
178 /* Rapid Spanning Tree. */
179 struct rstp_port
*rstp_port
; /* Rapid Spanning Tree Protocol, if any. */
180 enum rstp_state rstp_state
; /* Always RSTP_DISABLED if RSTP not in use. */
182 /* Queue to DSCP mapping. */
183 struct ofproto_port_queue
*qdscp
;
186 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
188 * This is deprecated. It is only for compatibility with broken device
189 * drivers in old versions of Linux that do not properly support VLANs when
190 * VLAN devices are not used. When broken device drivers are no longer in
191 * widespread use, we will delete these interfaces. */
192 ofp_port_t realdev_ofp_port
;
196 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
198 * This is deprecated. It is only for compatibility with broken device drivers
199 * in old versions of Linux that do not properly support VLANs when VLAN
200 * devices are not used. When broken device drivers are no longer in
201 * widespread use, we will delete these interfaces. */
202 struct vlan_splinter
{
203 struct hmap_node realdev_vid_node
;
204 struct hmap_node vlandev_node
;
205 ofp_port_t realdev_ofp_port
;
206 ofp_port_t vlandev_ofp_port
;
210 static void vsp_remove(struct ofport_dpif
*);
211 static void vsp_add(struct ofport_dpif
*, ofp_port_t realdev_ofp_port
, int vid
);
213 static odp_port_t
ofp_port_to_odp_port(const struct ofproto_dpif
*,
216 static ofp_port_t
odp_port_to_ofp_port(const struct ofproto_dpif
*,
219 static struct ofport_dpif
*
220 ofport_dpif_cast(const struct ofport
*ofport
)
222 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
225 static void port_run(struct ofport_dpif
*);
226 static int set_bfd(struct ofport
*, const struct smap
*);
227 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
228 static int set_lldp(struct ofport
*ofport_
, const struct smap
*cfg
);
229 static void ofport_update_peer(struct ofport_dpif
*);
231 /* Reasons that we might need to revalidate every datapath flow, and
232 * corresponding coverage counters.
234 * A value of 0 means that there is no need to revalidate.
236 * It would be nice to have some cleaner way to integrate with coverage
237 * counters, but with only a few reasons I guess this is good enough for
239 enum revalidate_reason
{
240 REV_RECONFIGURE
= 1, /* Switch configuration changed. */
241 REV_STP
, /* Spanning tree protocol port status change. */
242 REV_RSTP
, /* RSTP port status change. */
243 REV_BOND
, /* Bonding changed. */
244 REV_PORT_TOGGLED
, /* Port enabled or disabled by CFM, LACP, ...*/
245 REV_FLOW_TABLE
, /* Flow table changed. */
246 REV_MAC_LEARNING
, /* Mac learning changed. */
247 REV_MCAST_SNOOPING
, /* Multicast snooping changed. */
249 COVERAGE_DEFINE(rev_reconfigure
);
250 COVERAGE_DEFINE(rev_stp
);
251 COVERAGE_DEFINE(rev_rstp
);
252 COVERAGE_DEFINE(rev_bond
);
253 COVERAGE_DEFINE(rev_port_toggled
);
254 COVERAGE_DEFINE(rev_flow_table
);
255 COVERAGE_DEFINE(rev_mac_learning
);
256 COVERAGE_DEFINE(rev_mcast_snooping
);
258 /* All datapaths of a given type share a single dpif backer instance. */
265 struct ovs_rwlock odp_to_ofport_lock
;
266 struct hmap odp_to_ofport_map OVS_GUARDED
; /* Contains "struct ofport"s. */
268 struct simap tnl_backers
; /* Set of dpif ports backing tunnels. */
270 enum revalidate_reason need_revalidate
; /* Revalidate all flows. */
272 bool recv_set_enable
; /* Enables or disables receiving packets. */
274 /* Version string of the datapath stored in OVSDB. */
275 char *dp_version_string
;
277 /* Datapath feature support. */
278 struct dpif_backer_support support
;
279 struct atomic_count tnl_count
;
282 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
283 static struct shash all_dpif_backers
= SHASH_INITIALIZER(&all_dpif_backers
);
285 struct ofproto_dpif
{
286 struct hmap_node all_ofproto_dpifs_node
; /* In 'all_ofproto_dpifs'. */
288 struct dpif_backer
*backer
;
290 atomic_llong tables_version
; /* Version # to use in classifier lookups. */
292 uint64_t dump_seq
; /* Last read of udpif_dump_seq(). */
294 /* Special OpenFlow rules. */
295 struct rule_dpif
*miss_rule
; /* Sends flow table misses to controller. */
296 struct rule_dpif
*no_packet_in_rule
; /* Drops flow table misses. */
297 struct rule_dpif
*drop_frags_rule
; /* Used in OFPC_FRAG_DROP mode. */
300 struct netflow
*netflow
;
301 struct dpif_sflow
*sflow
;
302 struct dpif_ipfix
*ipfix
;
303 struct hmap bundles
; /* Contains "struct ofbundle"s. */
304 struct mac_learning
*ml
;
305 struct mcast_snooping
*ms
;
306 bool has_bonded_bundles
;
308 struct mbridge
*mbridge
;
310 struct ovs_mutex stats_mutex
;
311 struct netdev_stats stats OVS_GUARDED
; /* To account packets generated and
312 * consumed in userspace. */
316 long long int stp_last_tick
;
318 /* Rapid Spanning Tree. */
320 long long int rstp_last_tick
;
322 /* VLAN splinters. */
323 struct ovs_mutex vsp_mutex
;
324 struct hmap realdev_vid_map OVS_GUARDED
; /* (realdev,vid) -> vlandev. */
325 struct hmap vlandev_map OVS_GUARDED
; /* vlandev -> (realdev,vid). */
328 struct sset ports
; /* Set of standard port names. */
329 struct sset ghost_ports
; /* Ports with no datapath port. */
330 struct sset port_poll_set
; /* Queued names for port_poll() reply. */
331 int port_poll_errno
; /* Last errno for port_poll() reply. */
332 uint64_t change_seq
; /* Connectivity status changes. */
335 struct guarded_list pins
; /* Contains "struct ofputil_packet_in"s. */
336 struct seq
*pins_seq
; /* For notifying 'pins' reception. */
340 /* All existing ofproto_dpif instances, indexed by ->up.name. */
341 static struct hmap all_ofproto_dpifs
= HMAP_INITIALIZER(&all_ofproto_dpifs
);
343 static bool ofproto_use_tnl_push_pop
= true;
344 static void ofproto_unixctl_init(void);
346 static inline struct ofproto_dpif
*
347 ofproto_dpif_cast(const struct ofproto
*ofproto
)
349 ovs_assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
350 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
354 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif
*ofproto
)
356 return ofproto
->backer
->support
.max_mpls_depth
;
360 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif
*ofproto
)
362 return ofproto
->backer
->support
.recirc
;
366 ofproto_dpif_get_enable_ufid(struct dpif_backer
*backer
)
368 return backer
->support
.ufid
;
371 static void ofproto_trace(struct ofproto_dpif
*, struct flow
*,
372 const struct dp_packet
*packet
,
373 const struct ofpact
[], size_t ofpacts_len
,
376 /* Global variables. */
377 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
379 /* Initial mappings of port to bridge mappings. */
380 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
382 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
385 ofproto_dpif_flow_mod(struct ofproto_dpif
*ofproto
,
386 struct ofputil_flow_mod
*fm
)
388 ofproto_flow_mod(&ofproto
->up
, fm
);
391 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
392 * Takes ownership of 'pin' and pin->packet. */
394 ofproto_dpif_send_packet_in(struct ofproto_dpif
*ofproto
,
395 struct ofproto_packet_in
*pin
)
397 if (!guarded_list_push_back(&ofproto
->pins
, &pin
->list_node
, 1024)) {
398 COVERAGE_INC(packet_in_overflow
);
399 free(CONST_CAST(void *, pin
->up
.packet
));
403 /* Wakes up main thread for packet-in I/O. */
404 seq_change(ofproto
->pins_seq
);
407 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
408 * packet rather than to send the packet to the controller.
410 * This function returns false to indicate that a packet_in message
411 * for a "table-miss" should be sent to at least one controller.
412 * False otherwise. */
414 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif
*ofproto
)
416 return connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
);
419 /* Factory functions. */
422 init(const struct shash
*iface_hints
)
424 struct shash_node
*node
;
426 /* Make a local copy, since we don't own 'iface_hints' elements. */
427 SHASH_FOR_EACH(node
, iface_hints
) {
428 const struct iface_hint
*orig_hint
= node
->data
;
429 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
431 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
432 new_hint
->br_type
= xstrdup(orig_hint
->br_type
);
433 new_hint
->ofp_port
= orig_hint
->ofp_port
;
435 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
440 enumerate_types(struct sset
*types
)
442 dp_enumerate_types(types
);
446 enumerate_names(const char *type
, struct sset
*names
)
448 struct ofproto_dpif
*ofproto
;
451 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
452 if (strcmp(type
, ofproto
->up
.type
)) {
455 sset_add(names
, ofproto
->up
.name
);
462 del(const char *type
, const char *name
)
467 error
= dpif_open(name
, type
, &dpif
);
469 error
= dpif_delete(dpif
);
476 port_open_type(const char *datapath_type
, const char *port_type
)
478 return dpif_port_open_type(datapath_type
, port_type
);
481 /* Type functions. */
483 static void process_dpif_port_changes(struct dpif_backer
*);
484 static void process_dpif_all_ports_changed(struct dpif_backer
*);
485 static void process_dpif_port_change(struct dpif_backer
*,
486 const char *devname
);
487 static void process_dpif_port_error(struct dpif_backer
*, int error
);
489 static struct ofproto_dpif
*
490 lookup_ofproto_dpif_by_port_name(const char *name
)
492 struct ofproto_dpif
*ofproto
;
494 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
495 if (sset_contains(&ofproto
->ports
, name
)) {
504 type_run(const char *type
)
506 struct dpif_backer
*backer
;
508 backer
= shash_find_data(&all_dpif_backers
, type
);
510 /* This is not necessarily a problem, since backers are only
511 * created on demand. */
516 if (dpif_run(backer
->dpif
)) {
517 backer
->need_revalidate
= REV_RECONFIGURE
;
520 udpif_run(backer
->udpif
);
522 /* If vswitchd started with other_config:flow_restore_wait set as "true",
523 * and the configuration has now changed to "false", enable receiving
524 * packets from the datapath. */
525 if (!backer
->recv_set_enable
&& !ofproto_get_flow_restore_wait()) {
528 backer
->recv_set_enable
= true;
530 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
532 VLOG_ERR("Failed to enable receiving packets in dpif.");
535 dpif_flow_flush(backer
->dpif
);
536 backer
->need_revalidate
= REV_RECONFIGURE
;
539 if (backer
->recv_set_enable
) {
540 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
543 dpif_poll_threads_set(backer
->dpif
, n_dpdk_rxqs
, pmd_cpu_mask
);
545 if (backer
->need_revalidate
) {
546 struct ofproto_dpif
*ofproto
;
547 struct simap_node
*node
;
548 struct simap tmp_backers
;
550 /* Handle tunnel garbage collection. */
551 simap_init(&tmp_backers
);
552 simap_swap(&backer
->tnl_backers
, &tmp_backers
);
554 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
555 struct ofport_dpif
*iter
;
557 if (backer
!= ofproto
->backer
) {
561 HMAP_FOR_EACH (iter
, up
.hmap_node
, &ofproto
->up
.ports
) {
562 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
565 if (!iter
->is_tunnel
) {
569 dp_port
= netdev_vport_get_dpif_port(iter
->up
.netdev
,
570 namebuf
, sizeof namebuf
);
571 node
= simap_find(&tmp_backers
, dp_port
);
573 simap_put(&backer
->tnl_backers
, dp_port
, node
->data
);
574 simap_delete(&tmp_backers
, node
);
575 node
= simap_find(&backer
->tnl_backers
, dp_port
);
577 node
= simap_find(&backer
->tnl_backers
, dp_port
);
579 odp_port_t odp_port
= ODPP_NONE
;
581 if (!dpif_port_add(backer
->dpif
, iter
->up
.netdev
,
583 simap_put(&backer
->tnl_backers
, dp_port
,
584 odp_to_u32(odp_port
));
585 node
= simap_find(&backer
->tnl_backers
, dp_port
);
590 iter
->odp_port
= node
? u32_to_odp(node
->data
) : ODPP_NONE
;
591 if (tnl_port_reconfigure(iter
, iter
->up
.netdev
,
593 ovs_native_tunneling_is_on(ofproto
), dp_port
)) {
594 backer
->need_revalidate
= REV_RECONFIGURE
;
599 SIMAP_FOR_EACH (node
, &tmp_backers
) {
600 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
));
602 simap_destroy(&tmp_backers
);
604 switch (backer
->need_revalidate
) {
605 case REV_RECONFIGURE
: COVERAGE_INC(rev_reconfigure
); break;
606 case REV_STP
: COVERAGE_INC(rev_stp
); break;
607 case REV_RSTP
: COVERAGE_INC(rev_rstp
); break;
608 case REV_BOND
: COVERAGE_INC(rev_bond
); break;
609 case REV_PORT_TOGGLED
: COVERAGE_INC(rev_port_toggled
); break;
610 case REV_FLOW_TABLE
: COVERAGE_INC(rev_flow_table
); break;
611 case REV_MAC_LEARNING
: COVERAGE_INC(rev_mac_learning
); break;
612 case REV_MCAST_SNOOPING
: COVERAGE_INC(rev_mcast_snooping
); break;
614 backer
->need_revalidate
= 0;
616 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
617 struct ofport_dpif
*ofport
;
618 struct ofbundle
*bundle
;
620 if (ofproto
->backer
!= backer
) {
625 xlate_ofproto_set(ofproto
, ofproto
->up
.name
,
626 ofproto
->backer
->dpif
, ofproto
->ml
,
627 ofproto
->stp
, ofproto
->rstp
, ofproto
->ms
,
628 ofproto
->mbridge
, ofproto
->sflow
, ofproto
->ipfix
,
630 ofproto
->up
.forward_bpdu
,
631 connmgr_has_in_band(ofproto
->up
.connmgr
),
632 &ofproto
->backer
->support
);
634 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
635 xlate_bundle_set(ofproto
, bundle
, bundle
->name
,
636 bundle
->vlan_mode
, bundle
->vlan
,
637 bundle
->trunks
, bundle
->use_priority_tags
,
638 bundle
->bond
, bundle
->lacp
,
642 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
643 int stp_port
= ofport
->stp_port
644 ? stp_port_no(ofport
->stp_port
)
646 xlate_ofport_set(ofproto
, ofport
->bundle
, ofport
,
647 ofport
->up
.ofp_port
, ofport
->odp_port
,
648 ofport
->up
.netdev
, ofport
->cfm
, ofport
->bfd
,
649 ofport
->lldp
, ofport
->peer
, stp_port
,
650 ofport
->rstp_port
, ofport
->qdscp
,
651 ofport
->n_qdscp
, ofport
->up
.pp
.config
,
652 ofport
->up
.pp
.state
, ofport
->is_tunnel
,
658 udpif_revalidate(backer
->udpif
);
661 process_dpif_port_changes(backer
);
666 /* Check for and handle port changes in 'backer''s dpif. */
668 process_dpif_port_changes(struct dpif_backer
*backer
)
674 error
= dpif_port_poll(backer
->dpif
, &devname
);
680 process_dpif_all_ports_changed(backer
);
684 process_dpif_port_change(backer
, devname
);
689 process_dpif_port_error(backer
, error
);
696 process_dpif_all_ports_changed(struct dpif_backer
*backer
)
698 struct ofproto_dpif
*ofproto
;
699 struct dpif_port dpif_port
;
700 struct dpif_port_dump dump
;
701 struct sset devnames
;
704 sset_init(&devnames
);
705 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
706 if (ofproto
->backer
== backer
) {
707 struct ofport
*ofport
;
709 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
710 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
714 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, backer
->dpif
) {
715 sset_add(&devnames
, dpif_port
.name
);
718 SSET_FOR_EACH (devname
, &devnames
) {
719 process_dpif_port_change(backer
, devname
);
721 sset_destroy(&devnames
);
725 process_dpif_port_change(struct dpif_backer
*backer
, const char *devname
)
727 struct ofproto_dpif
*ofproto
;
728 struct dpif_port port
;
730 /* Don't report on the datapath's device. */
731 if (!strcmp(devname
, dpif_base_name(backer
->dpif
))) {
735 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
,
736 &all_ofproto_dpifs
) {
737 if (simap_contains(&ofproto
->backer
->tnl_backers
, devname
)) {
742 ofproto
= lookup_ofproto_dpif_by_port_name(devname
);
743 if (dpif_port_query_by_name(backer
->dpif
, devname
, &port
)) {
744 /* The port was removed. If we know the datapath,
745 * report it through poll_set(). If we don't, it may be
746 * notifying us of a removal we initiated, so ignore it.
747 * If there's a pending ENOBUFS, let it stand, since
748 * everything will be reevaluated. */
749 if (ofproto
&& ofproto
->port_poll_errno
!= ENOBUFS
) {
750 sset_add(&ofproto
->port_poll_set
, devname
);
751 ofproto
->port_poll_errno
= 0;
753 } else if (!ofproto
) {
754 /* The port was added, but we don't know with which
755 * ofproto we should associate it. Delete it. */
756 dpif_port_del(backer
->dpif
, port
.port_no
);
758 struct ofport_dpif
*ofport
;
760 ofport
= ofport_dpif_cast(shash_find_data(
761 &ofproto
->up
.port_by_name
, devname
));
763 && ofport
->odp_port
!= port
.port_no
764 && !odp_port_to_ofport(backer
, port
.port_no
))
766 /* 'ofport''s datapath port number has changed from
767 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
768 * structures to match. */
769 ovs_rwlock_wrlock(&backer
->odp_to_ofport_lock
);
770 hmap_remove(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
);
771 ofport
->odp_port
= port
.port_no
;
772 hmap_insert(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
,
773 hash_odp_port(port
.port_no
));
774 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
775 backer
->need_revalidate
= REV_RECONFIGURE
;
778 dpif_port_destroy(&port
);
781 /* Propagate 'error' to all ofprotos based on 'backer'. */
783 process_dpif_port_error(struct dpif_backer
*backer
, int error
)
785 struct ofproto_dpif
*ofproto
;
787 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
788 if (ofproto
->backer
== backer
) {
789 sset_clear(&ofproto
->port_poll_set
);
790 ofproto
->port_poll_errno
= error
;
796 type_wait(const char *type
)
798 struct dpif_backer
*backer
;
800 backer
= shash_find_data(&all_dpif_backers
, type
);
802 /* This is not necessarily a problem, since backers are only
803 * created on demand. */
807 dpif_wait(backer
->dpif
);
810 /* Basic life-cycle. */
812 static int add_internal_flows(struct ofproto_dpif
*);
814 static struct ofproto
*
817 struct ofproto_dpif
*ofproto
= xzalloc(sizeof *ofproto
);
822 dealloc(struct ofproto
*ofproto_
)
824 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
829 close_dpif_backer(struct dpif_backer
*backer
)
831 ovs_assert(backer
->refcount
> 0);
833 if (--backer
->refcount
) {
837 udpif_destroy(backer
->udpif
);
839 simap_destroy(&backer
->tnl_backers
);
840 ovs_rwlock_destroy(&backer
->odp_to_ofport_lock
);
841 hmap_destroy(&backer
->odp_to_ofport_map
);
842 shash_find_and_delete(&all_dpif_backers
, backer
->type
);
844 free(backer
->dp_version_string
);
845 dpif_close(backer
->dpif
);
849 /* Datapath port slated for removal from datapath. */
851 struct ovs_list list_node
;
855 static bool check_variable_length_userdata(struct dpif_backer
*backer
);
856 static void check_support(struct dpif_backer
*backer
);
859 open_dpif_backer(const char *type
, struct dpif_backer
**backerp
)
861 struct dpif_backer
*backer
;
862 struct dpif_port_dump port_dump
;
863 struct dpif_port port
;
864 struct shash_node
*node
;
865 struct ovs_list garbage_list
;
866 struct odp_garbage
*garbage
;
875 backer
= shash_find_data(&all_dpif_backers
, type
);
882 backer_name
= xasprintf("ovs-%s", type
);
884 /* Remove any existing datapaths, since we assume we're the only
885 * userspace controlling the datapath. */
887 dp_enumerate_names(type
, &names
);
888 SSET_FOR_EACH(name
, &names
) {
889 struct dpif
*old_dpif
;
891 /* Don't remove our backer if it exists. */
892 if (!strcmp(name
, backer_name
)) {
896 if (dpif_open(name
, type
, &old_dpif
)) {
897 VLOG_WARN("couldn't open old datapath %s to remove it", name
);
899 dpif_delete(old_dpif
);
900 dpif_close(old_dpif
);
903 sset_destroy(&names
);
905 backer
= xmalloc(sizeof *backer
);
907 error
= dpif_create_and_open(backer_name
, type
, &backer
->dpif
);
910 VLOG_ERR("failed to open datapath of type %s: %s", type
,
911 ovs_strerror(error
));
915 backer
->udpif
= udpif_create(backer
, backer
->dpif
);
917 backer
->type
= xstrdup(type
);
918 backer
->refcount
= 1;
919 hmap_init(&backer
->odp_to_ofport_map
);
920 ovs_rwlock_init(&backer
->odp_to_ofport_lock
);
921 backer
->need_revalidate
= 0;
922 simap_init(&backer
->tnl_backers
);
923 backer
->recv_set_enable
= !ofproto_get_flow_restore_wait();
926 if (backer
->recv_set_enable
) {
927 dpif_flow_flush(backer
->dpif
);
930 /* Loop through the ports already on the datapath and remove any
931 * that we don't need anymore. */
932 list_init(&garbage_list
);
933 dpif_port_dump_start(&port_dump
, backer
->dpif
);
934 while (dpif_port_dump_next(&port_dump
, &port
)) {
935 node
= shash_find(&init_ofp_ports
, port
.name
);
936 if (!node
&& strcmp(port
.name
, dpif_base_name(backer
->dpif
))) {
937 garbage
= xmalloc(sizeof *garbage
);
938 garbage
->odp_port
= port
.port_no
;
939 list_push_front(&garbage_list
, &garbage
->list_node
);
942 dpif_port_dump_done(&port_dump
);
944 LIST_FOR_EACH_POP (garbage
, list_node
, &garbage_list
) {
945 dpif_port_del(backer
->dpif
, garbage
->odp_port
);
949 shash_add(&all_dpif_backers
, type
, backer
);
951 check_support(backer
);
952 atomic_count_init(&backer
->tnl_count
, 0);
954 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
956 VLOG_ERR("failed to listen on datapath of type %s: %s",
957 type
, ovs_strerror(error
));
958 close_dpif_backer(backer
);
962 if (backer
->recv_set_enable
) {
963 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
966 /* This check fails if performed before udpif threads have been set,
967 * as the kernel module checks that the 'pid' in userspace action
969 backer
->support
.variable_length_userdata
970 = check_variable_length_userdata(backer
);
971 backer
->dp_version_string
= dpif_get_dp_version(backer
->dpif
);
977 ovs_native_tunneling_is_on(struct ofproto_dpif
*ofproto
)
979 return ofproto_use_tnl_push_pop
&& ofproto
->backer
->support
.tnl_push_pop
&&
980 atomic_count_get(&ofproto
->backer
->tnl_count
);
983 /* Tests whether 'backer''s datapath supports recirculation. Only newer
984 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
985 * features on older datapaths that don't support this feature.
987 * Returns false if 'backer' definitely does not support recirculation, true if
988 * it seems to support recirculation or if at least the error we get is
991 check_recirc(struct dpif_backer
*backer
)
994 struct odputil_keybuf keybuf
;
998 memset(&flow
, 0, sizeof flow
);
1002 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1003 odp_flow_key_from_flow(&key
, &flow
, NULL
, 0, true);
1004 enable_recirc
= dpif_probe_feature(backer
->dpif
, "recirculation", &key
,
1007 if (enable_recirc
) {
1008 VLOG_INFO("%s: Datapath supports recirculation",
1009 dpif_name(backer
->dpif
));
1011 VLOG_INFO("%s: Datapath does not support recirculation",
1012 dpif_name(backer
->dpif
));
1015 return enable_recirc
;
1018 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1019 * some flow attributes for datapaths that support this feature.
1021 * Returns true if 'dpif' supports UFID for flow operations.
1022 * Returns false if 'dpif' does not support UFID. */
1024 check_ufid(struct dpif_backer
*backer
)
1027 struct odputil_keybuf keybuf
;
1032 memset(&flow
, 0, sizeof flow
);
1033 flow
.dl_type
= htons(0x1234);
1035 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1036 odp_flow_key_from_flow(&key
, &flow
, NULL
, 0, true);
1037 dpif_flow_hash(backer
->dpif
, key
.data
, key
.size
, &ufid
);
1039 enable_ufid
= dpif_probe_feature(backer
->dpif
, "UFID", &key
, &ufid
);
1042 VLOG_INFO("%s: Datapath supports unique flow ids",
1043 dpif_name(backer
->dpif
));
1045 VLOG_INFO("%s: Datapath does not support unique flow ids",
1046 dpif_name(backer
->dpif
));
1051 /* Tests whether 'backer''s datapath supports variable-length
1052 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1053 * to disable some features on older datapaths that don't support this
1056 * Returns false if 'backer' definitely does not support variable-length
1057 * userdata, true if it seems to support them or if at least the error we get
1060 check_variable_length_userdata(struct dpif_backer
*backer
)
1062 struct eth_header
*eth
;
1063 struct ofpbuf actions
;
1064 struct dpif_execute execute
;
1065 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 /* Execute the actions. On older datapaths this fails with ERANGE, on
1089 * newer datapaths it succeeds. */
1090 execute
.actions
= actions
.data
;
1091 execute
.actions_len
= actions
.size
;
1092 execute
.packet
= &packet
;
1093 execute
.needs_help
= false;
1094 execute
.probe
= true;
1096 error
= dpif_execute(backer
->dpif
, &execute
);
1098 dp_packet_uninit(&packet
);
1099 ofpbuf_uninit(&actions
);
1106 /* Variable-length userdata is not supported. */
1107 VLOG_WARN("%s: datapath does not support variable-length userdata "
1108 "feature (needs Linux 3.10+ or kernel module from OVS "
1109 "1..11+). The NXAST_SAMPLE action will be ignored.",
1110 dpif_name(backer
->dpif
));
1114 /* Something odd happened. We're not sure whether variable-length
1115 * userdata is supported. Default to "yes". */
1116 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1117 dpif_name(backer
->dpif
), ovs_strerror(error
));
1122 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1124 * Returns the number of elements in a struct flow's mpls_lse field
1125 * if the datapath supports at least that many entries in an
1127 * Otherwise returns the number of MPLS push actions supported by
1130 check_max_mpls_depth(struct dpif_backer
*backer
)
1135 for (n
= 0; n
< FLOW_MAX_MPLS_LABELS
; n
++) {
1136 struct odputil_keybuf keybuf
;
1139 memset(&flow
, 0, sizeof flow
);
1140 flow
.dl_type
= htons(ETH_TYPE_MPLS
);
1141 flow_set_mpls_bos(&flow
, n
, 1);
1143 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1144 odp_flow_key_from_flow(&key
, &flow
, NULL
, 0, false);
1145 if (!dpif_probe_feature(backer
->dpif
, "MPLS", &key
, NULL
)) {
1150 VLOG_INFO("%s: MPLS label stack length probed as %d",
1151 dpif_name(backer
->dpif
), n
);
1155 /* Tests whether 'backer''s datapath supports masked data in
1156 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1157 * datapaths that don't support this feature. */
1159 check_masked_set_action(struct dpif_backer
*backer
)
1161 struct eth_header
*eth
;
1162 struct ofpbuf actions
;
1163 struct dpif_execute execute
;
1164 struct dp_packet packet
;
1166 struct ovs_key_ethernet key
, mask
;
1168 /* Compose a set action that will cause an EINVAL error on older
1169 * datapaths that don't support masked set actions.
1170 * Avoid using a full mask, as it could be translated to a non-masked
1171 * set action instead. */
1172 ofpbuf_init(&actions
, 64);
1173 memset(&key
, 0x53, sizeof key
);
1174 memset(&mask
, 0x7f, sizeof mask
);
1175 commit_masked_set_action(&actions
, OVS_KEY_ATTR_ETHERNET
, &key
, &mask
,
1178 /* Compose a dummy ethernet packet. */
1179 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1180 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1181 eth
->eth_type
= htons(0x1234);
1183 /* Execute the actions. On older datapaths this fails with EINVAL, on
1184 * newer datapaths it succeeds. */
1185 execute
.actions
= actions
.data
;
1186 execute
.actions_len
= actions
.size
;
1187 execute
.packet
= &packet
;
1188 execute
.needs_help
= false;
1189 execute
.probe
= true;
1191 error
= dpif_execute(backer
->dpif
, &execute
);
1193 dp_packet_uninit(&packet
);
1194 ofpbuf_uninit(&actions
);
1197 /* Masked set action is not supported. */
1198 VLOG_INFO("%s: datapath does not support masked set action feature.",
1199 dpif_name(backer
->dpif
));
1205 check_support(struct dpif_backer
*backer
)
1207 /* This feature needs to be tested after udpif threads are set. */
1208 backer
->support
.variable_length_userdata
= false;
1210 backer
->support
.recirc
= check_recirc(backer
);
1211 backer
->support
.max_mpls_depth
= check_max_mpls_depth(backer
);
1212 backer
->support
.masked_set_action
= check_masked_set_action(backer
);
1213 backer
->support
.ufid
= check_ufid(backer
);
1214 backer
->support
.tnl_push_pop
= dpif_supports_tnl_push_pop(backer
->dpif
);
1218 construct(struct ofproto
*ofproto_
)
1220 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1221 struct shash_node
*node
, *next
;
1224 /* Tunnel module can get used right after the udpif threads are running. */
1225 ofproto_tunnel_init();
1227 error
= open_dpif_backer(ofproto
->up
.type
, &ofproto
->backer
);
1232 atomic_init(&ofproto
->tables_version
, CLS_MIN_VERSION
);
1233 ofproto
->netflow
= NULL
;
1234 ofproto
->sflow
= NULL
;
1235 ofproto
->ipfix
= NULL
;
1236 ofproto
->stp
= NULL
;
1237 ofproto
->rstp
= NULL
;
1238 ofproto
->dump_seq
= 0;
1239 hmap_init(&ofproto
->bundles
);
1240 ofproto
->ml
= mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME
);
1242 ofproto
->mbridge
= mbridge_create();
1243 ofproto
->has_bonded_bundles
= false;
1244 ofproto
->lacp_enabled
= false;
1245 ovs_mutex_init_adaptive(&ofproto
->stats_mutex
);
1246 ovs_mutex_init(&ofproto
->vsp_mutex
);
1248 guarded_list_init(&ofproto
->pins
);
1250 ofproto_unixctl_init();
1252 hmap_init(&ofproto
->vlandev_map
);
1253 hmap_init(&ofproto
->realdev_vid_map
);
1255 sset_init(&ofproto
->ports
);
1256 sset_init(&ofproto
->ghost_ports
);
1257 sset_init(&ofproto
->port_poll_set
);
1258 ofproto
->port_poll_errno
= 0;
1259 ofproto
->change_seq
= 0;
1260 ofproto
->pins_seq
= seq_create();
1261 ofproto
->pins_seqno
= seq_read(ofproto
->pins_seq
);
1264 SHASH_FOR_EACH_SAFE (node
, next
, &init_ofp_ports
) {
1265 struct iface_hint
*iface_hint
= node
->data
;
1267 if (!strcmp(iface_hint
->br_name
, ofproto
->up
.name
)) {
1268 /* Check if the datapath already has this port. */
1269 if (dpif_port_exists(ofproto
->backer
->dpif
, node
->name
)) {
1270 sset_add(&ofproto
->ports
, node
->name
);
1273 free(iface_hint
->br_name
);
1274 free(iface_hint
->br_type
);
1276 shash_delete(&init_ofp_ports
, node
);
1280 hmap_insert(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
,
1281 hash_string(ofproto
->up
.name
, 0));
1282 memset(&ofproto
->stats
, 0, sizeof ofproto
->stats
);
1284 ofproto_init_tables(ofproto_
, N_TABLES
);
1285 error
= add_internal_flows(ofproto
);
1287 ofproto
->up
.tables
[TBL_INTERNAL
].flags
= OFTABLE_HIDDEN
| OFTABLE_READONLY
;
1293 add_internal_miss_flow(struct ofproto_dpif
*ofproto
, int id
,
1294 const struct ofpbuf
*ofpacts
, struct rule_dpif
**rulep
)
1300 match_init_catchall(&match
);
1301 match_set_reg(&match
, 0, id
);
1303 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 0, 0, ofpacts
,
1305 *rulep
= error
? NULL
: rule_dpif_cast(rule
);
1311 add_internal_flows(struct ofproto_dpif
*ofproto
)
1313 struct ofpact_controller
*controller
;
1314 uint64_t ofpacts_stub
[128 / 8];
1315 struct ofpbuf ofpacts
;
1316 struct rule
*unused_rulep OVS_UNUSED
;
1321 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1324 controller
= ofpact_put_CONTROLLER(&ofpacts
);
1325 controller
->max_len
= UINT16_MAX
;
1326 controller
->controller_id
= 0;
1327 controller
->reason
= OFPR_NO_MATCH
;
1328 ofpact_pad(&ofpacts
);
1330 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1331 &ofproto
->miss_rule
);
1336 ofpbuf_clear(&ofpacts
);
1337 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1338 &ofproto
->no_packet_in_rule
);
1343 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1344 &ofproto
->drop_frags_rule
);
1349 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1350 * zero when reaching this rule.
1352 * (priority=2), recirc_id=0, actions=drop
1354 ofpbuf_clear(&ofpacts
);
1355 match_init_catchall(&match
);
1356 match_set_recirc_id(&match
, 0);
1357 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 2, 0, &ofpacts
,
1363 destruct(struct ofproto
*ofproto_
)
1365 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1366 struct ofproto_packet_in
*pin
;
1367 struct rule_dpif
*rule
;
1368 struct oftable
*table
;
1369 struct ovs_list pins
;
1371 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1373 xlate_remove_ofproto(ofproto
);
1376 /* Ensure that the upcall processing threads have no remaining references
1377 * to the ofproto or anything in it. */
1378 udpif_synchronize(ofproto
->backer
->udpif
);
1380 hmap_remove(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
);
1382 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1383 CLS_FOR_EACH (rule
, up
.cr
, &table
->cls
) {
1384 ofproto_rule_delete(&ofproto
->up
, &rule
->up
);
1388 guarded_list_pop_all(&ofproto
->pins
, &pins
);
1389 LIST_FOR_EACH_POP (pin
, list_node
, &pins
) {
1390 free(CONST_CAST(void *, pin
->up
.packet
));
1393 guarded_list_destroy(&ofproto
->pins
);
1395 recirc_free_ofproto(ofproto
, ofproto
->up
.name
);
1397 mbridge_unref(ofproto
->mbridge
);
1399 netflow_unref(ofproto
->netflow
);
1400 dpif_sflow_unref(ofproto
->sflow
);
1401 dpif_ipfix_unref(ofproto
->ipfix
);
1402 hmap_destroy(&ofproto
->bundles
);
1403 mac_learning_unref(ofproto
->ml
);
1404 mcast_snooping_unref(ofproto
->ms
);
1406 hmap_destroy(&ofproto
->vlandev_map
);
1407 hmap_destroy(&ofproto
->realdev_vid_map
);
1409 sset_destroy(&ofproto
->ports
);
1410 sset_destroy(&ofproto
->ghost_ports
);
1411 sset_destroy(&ofproto
->port_poll_set
);
1413 ovs_mutex_destroy(&ofproto
->stats_mutex
);
1414 ovs_mutex_destroy(&ofproto
->vsp_mutex
);
1416 seq_destroy(ofproto
->pins_seq
);
1418 close_dpif_backer(ofproto
->backer
);
1422 run(struct ofproto
*ofproto_
)
1424 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1425 uint64_t new_seq
, new_dump_seq
;
1427 if (mbridge_need_revalidate(ofproto
->mbridge
)) {
1428 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1429 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1430 mac_learning_flush(ofproto
->ml
);
1431 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1432 mcast_snooping_mdb_flush(ofproto
->ms
);
1435 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1436 * flow restore. Even though nothing is processed during flow restore,
1437 * all queued 'pins' will be handled immediately when flow restore
1439 ofproto
->pins_seqno
= seq_read(ofproto
->pins_seq
);
1441 /* Do not perform any periodic activity required by 'ofproto' while
1442 * waiting for flow restore to complete. */
1443 if (!ofproto_get_flow_restore_wait()) {
1444 struct ofproto_packet_in
*pin
;
1445 struct ovs_list pins
;
1447 guarded_list_pop_all(&ofproto
->pins
, &pins
);
1448 LIST_FOR_EACH_POP (pin
, list_node
, &pins
) {
1449 connmgr_send_packet_in(ofproto
->up
.connmgr
, pin
);
1450 free(CONST_CAST(void *, pin
->up
.packet
));
1455 if (ofproto
->netflow
) {
1456 netflow_run(ofproto
->netflow
);
1458 if (ofproto
->sflow
) {
1459 dpif_sflow_run(ofproto
->sflow
);
1461 if (ofproto
->ipfix
) {
1462 dpif_ipfix_run(ofproto
->ipfix
);
1465 new_seq
= seq_read(connectivity_seq_get());
1466 if (ofproto
->change_seq
!= new_seq
) {
1467 struct ofport_dpif
*ofport
;
1469 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1473 ofproto
->change_seq
= new_seq
;
1475 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1476 struct ofbundle
*bundle
;
1478 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1485 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1486 if (mac_learning_run(ofproto
->ml
)) {
1487 ofproto
->backer
->need_revalidate
= REV_MAC_LEARNING
;
1489 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1491 if (mcast_snooping_run(ofproto
->ms
)) {
1492 ofproto
->backer
->need_revalidate
= REV_MCAST_SNOOPING
;
1495 new_dump_seq
= seq_read(udpif_dump_seq(ofproto
->backer
->udpif
));
1496 if (ofproto
->dump_seq
!= new_dump_seq
) {
1497 struct rule
*rule
, *next_rule
;
1499 /* We know stats are relatively fresh, so now is a good time to do some
1501 ofproto
->dump_seq
= new_dump_seq
;
1503 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1505 ovs_mutex_lock(&ofproto_mutex
);
1506 LIST_FOR_EACH_SAFE (rule
, next_rule
, expirable
,
1507 &ofproto
->up
.expirable
) {
1508 rule_expire(rule_dpif_cast(rule
));
1510 ovs_mutex_unlock(&ofproto_mutex
);
1512 /* All outstanding data in existing flows has been accounted, so it's a
1513 * good time to do bond rebalancing. */
1514 if (ofproto
->has_bonded_bundles
) {
1515 struct ofbundle
*bundle
;
1517 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1519 bond_rebalance(bundle
->bond
);
1528 wait(struct ofproto
*ofproto_
)
1530 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1532 if (ofproto_get_flow_restore_wait()) {
1536 if (ofproto
->sflow
) {
1537 dpif_sflow_wait(ofproto
->sflow
);
1539 if (ofproto
->ipfix
) {
1540 dpif_ipfix_wait(ofproto
->ipfix
);
1542 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1543 struct ofbundle
*bundle
;
1545 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1546 bundle_wait(bundle
);
1549 if (ofproto
->netflow
) {
1550 netflow_wait(ofproto
->netflow
);
1552 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
1553 mac_learning_wait(ofproto
->ml
);
1554 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1555 mcast_snooping_wait(ofproto
->ms
);
1557 if (ofproto
->backer
->need_revalidate
) {
1558 /* Shouldn't happen, but if it does just go around again. */
1559 VLOG_DBG_RL(&rl
, "need revalidate in ofproto_wait_cb()");
1560 poll_immediate_wake();
1563 seq_wait(udpif_dump_seq(ofproto
->backer
->udpif
), ofproto
->dump_seq
);
1564 seq_wait(ofproto
->pins_seq
, ofproto
->pins_seqno
);
1568 type_get_memory_usage(const char *type
, struct simap
*usage
)
1570 struct dpif_backer
*backer
;
1572 backer
= shash_find_data(&all_dpif_backers
, type
);
1574 udpif_get_memory_usage(backer
->udpif
, usage
);
1579 flush(struct ofproto
*ofproto_
)
1581 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1582 struct dpif_backer
*backer
= ofproto
->backer
;
1585 udpif_flush(backer
->udpif
);
1590 query_tables(struct ofproto
*ofproto
,
1591 struct ofputil_table_features
*features
,
1592 struct ofputil_table_stats
*stats
)
1594 strcpy(features
->name
, "classifier");
1599 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
1600 unsigned long missed
, matched
;
1602 atomic_read_relaxed(&ofproto
->tables
[i
].n_matched
, &matched
);
1603 atomic_read_relaxed(&ofproto
->tables
[i
].n_missed
, &missed
);
1605 stats
[i
].matched_count
= matched
;
1606 stats
[i
].lookup_count
= matched
+ missed
;
1612 set_tables_version(struct ofproto
*ofproto_
, long long version
)
1614 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1616 atomic_store_relaxed(&ofproto
->tables_version
, version
);
1620 static struct ofport
*
1623 struct ofport_dpif
*port
= xzalloc(sizeof *port
);
1628 port_dealloc(struct ofport
*port_
)
1630 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1635 port_construct(struct ofport
*port_
)
1637 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1638 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1639 const struct netdev
*netdev
= port
->up
.netdev
;
1640 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1641 const char *dp_port_name
;
1642 struct dpif_port dpif_port
;
1645 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1646 port
->bundle
= NULL
;
1650 port
->may_enable
= false;
1651 port
->stp_port
= NULL
;
1652 port
->stp_state
= STP_DISABLED
;
1653 port
->rstp_port
= NULL
;
1654 port
->rstp_state
= RSTP_DISABLED
;
1655 port
->is_tunnel
= false;
1659 port
->realdev_ofp_port
= 0;
1660 port
->vlandev_vid
= 0;
1661 port
->carrier_seq
= netdev_get_carrier_resets(netdev
);
1662 port
->is_layer3
= netdev_vport_is_layer3(netdev
);
1664 if (netdev_vport_is_patch(netdev
)) {
1665 /* By bailing out here, we don't submit the port to the sFlow module
1666 * to be considered for counter polling export. This is correct
1667 * because the patch port represents an interface that sFlow considers
1668 * to be "internal" to the switch as a whole, and therefore not a
1669 * candidate for counter polling. */
1670 port
->odp_port
= ODPP_NONE
;
1671 ofport_update_peer(port
);
1675 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1676 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
, dp_port_name
,
1682 port
->odp_port
= dpif_port
.port_no
;
1684 if (netdev_get_tunnel_config(netdev
)) {
1685 atomic_count_inc(&ofproto
->backer
->tnl_count
);
1686 error
= tnl_port_add(port
, port
->up
.netdev
, port
->odp_port
,
1687 ovs_native_tunneling_is_on(ofproto
), dp_port_name
);
1689 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1690 dpif_port_destroy(&dpif_port
);
1694 port
->is_tunnel
= true;
1695 if (ofproto
->ipfix
) {
1696 dpif_ipfix_add_tunnel_port(ofproto
->ipfix
, port_
, port
->odp_port
);
1699 /* Sanity-check that a mapping doesn't already exist. This
1700 * shouldn't happen for non-tunnel ports. */
1701 if (odp_port_to_ofp_port(ofproto
, port
->odp_port
) != OFPP_NONE
) {
1702 VLOG_ERR("port %s already has an OpenFlow port number",
1704 dpif_port_destroy(&dpif_port
);
1708 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1709 hmap_insert(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
,
1710 hash_odp_port(port
->odp_port
));
1711 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1713 dpif_port_destroy(&dpif_port
);
1715 if (ofproto
->sflow
) {
1716 dpif_sflow_add_port(ofproto
->sflow
, port_
, port
->odp_port
);
1723 port_destruct(struct ofport
*port_
)
1725 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1726 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1727 const char *devname
= netdev_get_name(port
->up
.netdev
);
1728 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1729 const char *dp_port_name
;
1731 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1733 xlate_ofport_remove(port
);
1736 dp_port_name
= netdev_vport_get_dpif_port(port
->up
.netdev
, namebuf
,
1738 if (dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
1739 /* The underlying device is still there, so delete it. This
1740 * happens when the ofproto is being destroyed, since the caller
1741 * assumes that removal of attached ports will happen as part of
1743 if (!port
->is_tunnel
) {
1744 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
);
1749 port
->peer
->peer
= NULL
;
1753 if (port
->odp_port
!= ODPP_NONE
&& !port
->is_tunnel
) {
1754 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1755 hmap_remove(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
);
1756 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1759 if (port
->is_tunnel
) {
1760 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1763 if (port
->is_tunnel
&& ofproto
->ipfix
) {
1764 dpif_ipfix_del_tunnel_port(ofproto
->ipfix
, port
->odp_port
);
1768 sset_find_and_delete(&ofproto
->ports
, devname
);
1769 sset_find_and_delete(&ofproto
->ghost_ports
, devname
);
1770 bundle_remove(port_
);
1771 set_cfm(port_
, NULL
);
1772 set_bfd(port_
, NULL
);
1773 set_lldp(port_
, NULL
);
1774 if (port
->stp_port
) {
1775 stp_port_disable(port
->stp_port
);
1777 set_rstp_port(port_
, NULL
);
1778 if (ofproto
->sflow
) {
1779 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
1786 port_modified(struct ofport
*port_
)
1788 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1789 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1790 const char *dp_port_name
;
1791 struct netdev
*netdev
= port
->up
.netdev
;
1793 if (port
->bundle
&& port
->bundle
->bond
) {
1794 bond_slave_set_netdev(port
->bundle
->bond
, port
, netdev
);
1798 cfm_set_netdev(port
->cfm
, netdev
);
1802 bfd_set_netdev(port
->bfd
, netdev
);
1805 ofproto_dpif_monitor_port_update(port
, port
->bfd
, port
->cfm
,
1806 port
->lldp
, port
->up
.pp
.hw_addr
);
1808 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1810 if (port
->is_tunnel
) {
1811 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1813 if (tnl_port_reconfigure(port
, netdev
, port
->odp_port
,
1814 ovs_native_tunneling_is_on(ofproto
),
1816 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1820 ofport_update_peer(port
);
1824 port_reconfigured(struct ofport
*port_
, enum ofputil_port_config old_config
)
1826 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1827 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1828 enum ofputil_port_config changed
= old_config
^ port
->up
.pp
.config
;
1830 if (changed
& (OFPUTIL_PC_NO_RECV
| OFPUTIL_PC_NO_RECV_STP
|
1831 OFPUTIL_PC_NO_FWD
| OFPUTIL_PC_NO_FLOOD
|
1832 OFPUTIL_PC_NO_PACKET_IN
)) {
1833 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1835 if (changed
& OFPUTIL_PC_NO_FLOOD
&& port
->bundle
) {
1836 bundle_update(port
->bundle
);
1842 set_sflow(struct ofproto
*ofproto_
,
1843 const struct ofproto_sflow_options
*sflow_options
)
1845 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1846 struct dpif_sflow
*ds
= ofproto
->sflow
;
1848 if (sflow_options
) {
1849 uint32_t old_probability
= ds
? dpif_sflow_get_probability(ds
) : 0;
1851 struct ofport_dpif
*ofport
;
1853 ds
= ofproto
->sflow
= dpif_sflow_create();
1854 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1855 dpif_sflow_add_port(ds
, &ofport
->up
, ofport
->odp_port
);
1858 dpif_sflow_set_options(ds
, sflow_options
);
1859 if (dpif_sflow_get_probability(ds
) != old_probability
) {
1860 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1864 dpif_sflow_unref(ds
);
1865 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1866 ofproto
->sflow
= NULL
;
1874 struct ofproto
*ofproto_
,
1875 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
1876 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
1877 size_t n_flow_exporters_options
)
1879 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1880 struct dpif_ipfix
*di
= ofproto
->ipfix
;
1881 bool has_options
= bridge_exporter_options
|| flow_exporters_options
;
1882 bool new_di
= false;
1884 if (has_options
&& !di
) {
1885 di
= ofproto
->ipfix
= dpif_ipfix_create();
1890 /* Call set_options in any case to cleanly flush the flow
1891 * caches in the last exporters that are to be destroyed. */
1892 dpif_ipfix_set_options(
1893 di
, bridge_exporter_options
, flow_exporters_options
,
1894 n_flow_exporters_options
);
1896 /* Add tunnel ports only when a new ipfix created */
1897 if (new_di
== true) {
1898 struct ofport_dpif
*ofport
;
1899 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1900 if (ofport
->is_tunnel
== true) {
1901 dpif_ipfix_add_tunnel_port(di
, &ofport
->up
, ofport
->odp_port
);
1907 dpif_ipfix_unref(di
);
1908 ofproto
->ipfix
= NULL
;
1916 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
1918 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1919 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
1920 struct cfm
*old
= ofport
->cfm
;
1925 ofport
->cfm
= cfm_create(ofport
->up
.netdev
);
1928 if (cfm_configure(ofport
->cfm
, s
)) {
1935 cfm_unref(ofport
->cfm
);
1938 if (ofport
->cfm
!= old
) {
1939 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1941 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
1942 ofport
->lldp
, ofport
->up
.pp
.hw_addr
);
1947 cfm_status_changed(struct ofport
*ofport_
)
1949 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1951 return ofport
->cfm
? cfm_check_status_change(ofport
->cfm
) : true;
1955 get_cfm_status(const struct ofport
*ofport_
,
1956 struct cfm_status
*status
)
1958 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1962 cfm_get_status(ofport
->cfm
, status
);
1971 set_bfd(struct ofport
*ofport_
, const struct smap
*cfg
)
1973 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
1974 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1978 ofport
->bfd
= bfd_configure(old
, netdev_get_name(ofport
->up
.netdev
),
1979 cfg
, ofport
->up
.netdev
);
1980 if (ofport
->bfd
!= old
) {
1981 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1983 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
1984 ofport
->lldp
, ofport
->up
.pp
.hw_addr
);
1989 bfd_status_changed(struct ofport
*ofport_
)
1991 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1993 return ofport
->bfd
? bfd_check_status_change(ofport
->bfd
) : true;
1997 get_bfd_status(struct ofport
*ofport_
, struct smap
*smap
)
1999 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2003 bfd_get_status(ofport
->bfd
, smap
);
2012 set_lldp(struct ofport
*ofport_
,
2013 const struct smap
*cfg
)
2015 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2019 if (!ofport
->lldp
) {
2020 struct ofproto_dpif
*ofproto
;
2022 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2023 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2024 ofport
->lldp
= lldp_create(ofport
->up
.netdev
, ofport_
->mtu
, cfg
);
2027 if (!lldp_configure(ofport
->lldp
, cfg
)) {
2032 lldp_unref(ofport
->lldp
);
2033 ofport
->lldp
= NULL
;
2036 ofproto_dpif_monitor_port_update(ofport
,
2040 ofport
->up
.pp
.hw_addr
);
2045 get_lldp_status(const struct ofport
*ofport_
,
2046 struct lldp_status
*status OVS_UNUSED
)
2048 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2050 return ofport
->lldp
? true : false;
2054 set_aa(struct ofproto
*ofproto OVS_UNUSED
,
2055 const struct aa_settings
*s
)
2057 return aa_configure(s
);
2061 aa_mapping_set(struct ofproto
*ofproto_ OVS_UNUSED
, void *aux
,
2062 const struct aa_mapping_settings
*s
)
2064 return aa_mapping_register(aux
, s
);
2068 aa_mapping_unset(struct ofproto
*ofproto OVS_UNUSED
, void *aux
)
2070 return aa_mapping_unregister(aux
);
2074 aa_vlan_get_queued(struct ofproto
*ofproto OVS_UNUSED
, struct ovs_list
*list
)
2076 return aa_get_vlan_queued(list
);
2080 aa_vlan_get_queue_size(struct ofproto
*ofproto OVS_UNUSED
)
2082 return aa_get_vlan_queue_size();
2086 /* Spanning Tree. */
2088 /* Called while rstp_mutex is held. */
2090 rstp_send_bpdu_cb(struct dp_packet
*pkt
, void *ofport_
, void *ofproto_
)
2092 struct ofproto_dpif
*ofproto
= ofproto_
;
2093 struct ofport_dpif
*ofport
= ofport_
;
2094 struct eth_header
*eth
= dp_packet_l2(pkt
);
2096 netdev_get_etheraddr(ofport
->up
.netdev
, eth
->eth_src
);
2097 if (eth_addr_is_zero(eth
->eth_src
)) {
2098 VLOG_WARN_RL(&rl
, "%s port %d: cannot send RSTP BPDU on a port which "
2099 "does not have a configured source MAC address.",
2100 ofproto
->up
.name
, ofp_to_u16(ofport
->up
.ofp_port
));
2102 ofproto_dpif_send_packet(ofport
, pkt
);
2104 dp_packet_delete(pkt
);
2108 send_bpdu_cb(struct dp_packet
*pkt
, int port_num
, void *ofproto_
)
2110 struct ofproto_dpif
*ofproto
= ofproto_
;
2111 struct stp_port
*sp
= stp_get_port(ofproto
->stp
, port_num
);
2112 struct ofport_dpif
*ofport
;
2114 ofport
= stp_port_get_aux(sp
);
2116 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
2117 ofproto
->up
.name
, port_num
);
2119 struct eth_header
*eth
= dp_packet_l2(pkt
);
2121 netdev_get_etheraddr(ofport
->up
.netdev
, eth
->eth_src
);
2122 if (eth_addr_is_zero(eth
->eth_src
)) {
2123 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d "
2124 "with unknown MAC", ofproto
->up
.name
, port_num
);
2126 ofproto_dpif_send_packet(ofport
, pkt
);
2129 dp_packet_delete(pkt
);
2132 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2134 set_rstp(struct ofproto
*ofproto_
, const struct ofproto_rstp_settings
*s
)
2136 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2138 /* Only revalidate flows if the configuration changed. */
2139 if (!s
!= !ofproto
->rstp
) {
2140 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2144 if (!ofproto
->rstp
) {
2145 ofproto
->rstp
= rstp_create(ofproto_
->name
, s
->address
,
2146 rstp_send_bpdu_cb
, ofproto
);
2147 ofproto
->rstp_last_tick
= time_msec();
2149 rstp_set_bridge_address(ofproto
->rstp
, s
->address
);
2150 rstp_set_bridge_priority(ofproto
->rstp
, s
->priority
);
2151 rstp_set_bridge_ageing_time(ofproto
->rstp
, s
->ageing_time
);
2152 rstp_set_bridge_force_protocol_version(ofproto
->rstp
,
2153 s
->force_protocol_version
);
2154 rstp_set_bridge_max_age(ofproto
->rstp
, s
->bridge_max_age
);
2155 rstp_set_bridge_forward_delay(ofproto
->rstp
, s
->bridge_forward_delay
);
2156 rstp_set_bridge_transmit_hold_count(ofproto
->rstp
,
2157 s
->transmit_hold_count
);
2159 struct ofport
*ofport
;
2160 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2161 set_rstp_port(ofport
, NULL
);
2163 rstp_unref(ofproto
->rstp
);
2164 ofproto
->rstp
= NULL
;
2169 get_rstp_status(struct ofproto
*ofproto_
, struct ofproto_rstp_status
*s
)
2171 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2173 if (ofproto
->rstp
) {
2175 s
->root_id
= rstp_get_root_id(ofproto
->rstp
);
2176 s
->bridge_id
= rstp_get_bridge_id(ofproto
->rstp
);
2177 s
->designated_id
= rstp_get_designated_id(ofproto
->rstp
);
2178 s
->root_path_cost
= rstp_get_root_path_cost(ofproto
->rstp
);
2179 s
->designated_port_id
= rstp_get_designated_port_id(ofproto
->rstp
);
2180 s
->bridge_port_id
= rstp_get_bridge_port_id(ofproto
->rstp
);
2187 update_rstp_port_state(struct ofport_dpif
*ofport
)
2189 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2190 enum rstp_state state
;
2192 /* Figure out new state. */
2193 state
= ofport
->rstp_port
? rstp_port_get_state(ofport
->rstp_port
)
2197 if (ofport
->rstp_state
!= state
) {
2198 enum ofputil_port_state of_state
;
2201 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2202 netdev_get_name(ofport
->up
.netdev
),
2203 rstp_state_name(ofport
->rstp_state
),
2204 rstp_state_name(state
));
2206 if (rstp_learn_in_state(ofport
->rstp_state
)
2207 != rstp_learn_in_state(state
)) {
2208 /* XXX: Learning action flows should also be flushed. */
2209 if (ofport
->bundle
) {
2210 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2211 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2212 bundle_flush_macs(ofport
->bundle
, false);
2216 fwd_change
= rstp_forward_in_state(ofport
->rstp_state
)
2217 != rstp_forward_in_state(state
);
2219 ofproto
->backer
->need_revalidate
= REV_RSTP
;
2220 ofport
->rstp_state
= state
;
2222 if (fwd_change
&& ofport
->bundle
) {
2223 bundle_update(ofport
->bundle
);
2226 /* Update the RSTP state bits in the OpenFlow port description. */
2227 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2228 of_state
|= (state
== RSTP_LEARNING
? OFPUTIL_PS_STP_LEARN
2229 : state
== RSTP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2230 : state
== RSTP_DISCARDING
? OFPUTIL_PS_STP_LISTEN
2232 ofproto_port_set_state(&ofport
->up
, of_state
);
2237 rstp_run(struct ofproto_dpif
*ofproto
)
2239 if (ofproto
->rstp
) {
2240 long long int now
= time_msec();
2241 long long int elapsed
= now
- ofproto
->rstp_last_tick
;
2242 struct rstp_port
*rp
;
2243 struct ofport_dpif
*ofport
;
2245 /* Every second, decrease the values of the timers. */
2246 if (elapsed
>= 1000) {
2247 rstp_tick_timers(ofproto
->rstp
);
2248 ofproto
->rstp_last_tick
= now
;
2251 while ((ofport
= rstp_get_next_changed_port_aux(ofproto
->rstp
, &rp
))) {
2252 update_rstp_port_state(ofport
);
2256 /* FIXME: This check should be done on-event (i.e., when setting
2257 * p->fdb_flush) and not periodically.
2259 while ((ofport
= rstp_check_and_reset_fdb_flush(ofproto
->rstp
, &rp
))) {
2260 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2261 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2262 bundle_flush_macs(ofport
->bundle
, false);
2266 if (rstp_shift_root_learned_address(ofproto
->rstp
)) {
2267 bundle_move(((struct ofport_dpif
*)rstp_get_old_root_aux(ofproto
->rstp
))->bundle
,
2268 ((struct ofport_dpif
*)rstp_get_new_root_aux(ofproto
->rstp
))->bundle
);
2269 rstp_reset_root_changed(ofproto
->rstp
);
2274 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2276 set_stp(struct ofproto
*ofproto_
, const struct ofproto_stp_settings
*s
)
2278 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2280 /* Only revalidate flows if the configuration changed. */
2281 if (!s
!= !ofproto
->stp
) {
2282 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2286 if (!ofproto
->stp
) {
2287 ofproto
->stp
= stp_create(ofproto_
->name
, s
->system_id
,
2288 send_bpdu_cb
, ofproto
);
2289 ofproto
->stp_last_tick
= time_msec();
2292 stp_set_bridge_id(ofproto
->stp
, s
->system_id
);
2293 stp_set_bridge_priority(ofproto
->stp
, s
->priority
);
2294 stp_set_hello_time(ofproto
->stp
, s
->hello_time
);
2295 stp_set_max_age(ofproto
->stp
, s
->max_age
);
2296 stp_set_forward_delay(ofproto
->stp
, s
->fwd_delay
);
2298 struct ofport
*ofport
;
2300 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2301 set_stp_port(ofport
, NULL
);
2304 stp_unref(ofproto
->stp
);
2305 ofproto
->stp
= NULL
;
2312 get_stp_status(struct ofproto
*ofproto_
, struct ofproto_stp_status
*s
)
2314 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2318 s
->bridge_id
= stp_get_bridge_id(ofproto
->stp
);
2319 s
->designated_root
= stp_get_designated_root(ofproto
->stp
);
2320 s
->root_path_cost
= stp_get_root_path_cost(ofproto
->stp
);
2329 update_stp_port_state(struct ofport_dpif
*ofport
)
2331 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2332 enum stp_state state
;
2334 /* Figure out new state. */
2335 state
= ofport
->stp_port
? stp_port_get_state(ofport
->stp_port
)
2339 if (ofport
->stp_state
!= state
) {
2340 enum ofputil_port_state of_state
;
2343 VLOG_DBG("port %s: STP state changed from %s to %s",
2344 netdev_get_name(ofport
->up
.netdev
),
2345 stp_state_name(ofport
->stp_state
),
2346 stp_state_name(state
));
2347 if (stp_learn_in_state(ofport
->stp_state
)
2348 != stp_learn_in_state(state
)) {
2349 /* xxx Learning action flows should also be flushed. */
2350 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2351 mac_learning_flush(ofproto
->ml
);
2352 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2353 mcast_snooping_mdb_flush(ofproto
->ms
);
2355 fwd_change
= stp_forward_in_state(ofport
->stp_state
)
2356 != stp_forward_in_state(state
);
2358 ofproto
->backer
->need_revalidate
= REV_STP
;
2359 ofport
->stp_state
= state
;
2360 ofport
->stp_state_entered
= time_msec();
2362 if (fwd_change
&& ofport
->bundle
) {
2363 bundle_update(ofport
->bundle
);
2366 /* Update the STP state bits in the OpenFlow port description. */
2367 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2368 of_state
|= (state
== STP_LISTENING
? OFPUTIL_PS_STP_LISTEN
2369 : state
== STP_LEARNING
? OFPUTIL_PS_STP_LEARN
2370 : state
== STP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2371 : state
== STP_BLOCKING
? OFPUTIL_PS_STP_BLOCK
2373 ofproto_port_set_state(&ofport
->up
, of_state
);
2377 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2378 * caller is responsible for assigning STP port numbers and ensuring
2379 * there are no duplicates. */
2381 set_stp_port(struct ofport
*ofport_
,
2382 const struct ofproto_port_stp_settings
*s
)
2384 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2385 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2386 struct stp_port
*sp
= ofport
->stp_port
;
2388 if (!s
|| !s
->enable
) {
2390 ofport
->stp_port
= NULL
;
2391 stp_port_disable(sp
);
2392 update_stp_port_state(ofport
);
2395 } else if (sp
&& stp_port_no(sp
) != s
->port_num
2396 && ofport
== stp_port_get_aux(sp
)) {
2397 /* The port-id changed, so disable the old one if it's not
2398 * already in use by another port. */
2399 stp_port_disable(sp
);
2402 sp
= ofport
->stp_port
= stp_get_port(ofproto
->stp
, s
->port_num
);
2404 /* Set name before enabling the port so that debugging messages can print
2406 stp_port_set_name(sp
, netdev_get_name(ofport
->up
.netdev
));
2407 stp_port_enable(sp
);
2409 stp_port_set_aux(sp
, ofport
);
2410 stp_port_set_priority(sp
, s
->priority
);
2411 stp_port_set_path_cost(sp
, s
->path_cost
);
2413 update_stp_port_state(ofport
);
2419 get_stp_port_status(struct ofport
*ofport_
,
2420 struct ofproto_port_stp_status
*s
)
2422 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2423 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2424 struct stp_port
*sp
= ofport
->stp_port
;
2426 if (!ofproto
->stp
|| !sp
) {
2432 s
->port_id
= stp_port_get_id(sp
);
2433 s
->state
= stp_port_get_state(sp
);
2434 s
->sec_in_state
= (time_msec() - ofport
->stp_state_entered
) / 1000;
2435 s
->role
= stp_port_get_role(sp
);
2441 get_stp_port_stats(struct ofport
*ofport_
,
2442 struct ofproto_port_stp_stats
*s
)
2444 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2445 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2446 struct stp_port
*sp
= ofport
->stp_port
;
2448 if (!ofproto
->stp
|| !sp
) {
2454 stp_port_get_counts(sp
, &s
->tx_count
, &s
->rx_count
, &s
->error_count
);
2460 stp_run(struct ofproto_dpif
*ofproto
)
2463 long long int now
= time_msec();
2464 long long int elapsed
= now
- ofproto
->stp_last_tick
;
2465 struct stp_port
*sp
;
2468 stp_tick(ofproto
->stp
, MIN(INT_MAX
, elapsed
));
2469 ofproto
->stp_last_tick
= now
;
2471 while (stp_get_changed_port(ofproto
->stp
, &sp
)) {
2472 struct ofport_dpif
*ofport
= stp_port_get_aux(sp
);
2475 update_stp_port_state(ofport
);
2479 if (stp_check_and_reset_fdb_flush(ofproto
->stp
)) {
2480 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2481 mac_learning_flush(ofproto
->ml
);
2482 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2483 mcast_snooping_mdb_flush(ofproto
->ms
);
2489 stp_wait(struct ofproto_dpif
*ofproto
)
2492 poll_timer_wait(1000);
2496 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2497 * caller is responsible for assigning RSTP port numbers and ensuring
2498 * there are no duplicates. */
2500 set_rstp_port(struct ofport
*ofport_
,
2501 const struct ofproto_port_rstp_settings
*s
)
2503 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2504 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2505 struct rstp_port
*rp
= ofport
->rstp_port
;
2507 if (!s
|| !s
->enable
) {
2509 rstp_port_unref(rp
);
2510 ofport
->rstp_port
= NULL
;
2511 update_rstp_port_state(ofport
);
2516 /* Check if need to add a new port. */
2518 rp
= ofport
->rstp_port
= rstp_add_port(ofproto
->rstp
);
2521 rstp_port_set(rp
, s
->port_num
, s
->priority
, s
->path_cost
,
2522 s
->admin_edge_port
, s
->auto_edge
,
2523 s
->admin_p2p_mac_state
, s
->admin_port_state
, s
->mcheck
,
2525 update_rstp_port_state(ofport
);
2526 /* Synchronize operational status. */
2527 rstp_port_set_mac_operational(rp
, ofport
->may_enable
);
2531 get_rstp_port_status(struct ofport
*ofport_
,
2532 struct ofproto_port_rstp_status
*s
)
2534 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2535 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2536 struct rstp_port
*rp
= ofport
->rstp_port
;
2538 if (!ofproto
->rstp
|| !rp
) {
2544 rstp_port_get_status(rp
, &s
->port_id
, &s
->state
, &s
->role
,
2545 &s
->designated_bridge_id
, &s
->designated_port_id
,
2546 &s
->designated_path_cost
, &s
->tx_count
,
2547 &s
->rx_count
, &s
->error_count
, &s
->uptime
);
2552 set_queues(struct ofport
*ofport_
, const struct ofproto_port_queue
*qdscp
,
2555 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2556 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2558 if (ofport
->n_qdscp
!= n_qdscp
2559 || (n_qdscp
&& memcmp(ofport
->qdscp
, qdscp
,
2560 n_qdscp
* sizeof *qdscp
))) {
2561 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2562 free(ofport
->qdscp
);
2563 ofport
->qdscp
= n_qdscp
2564 ? xmemdup(qdscp
, n_qdscp
* sizeof *qdscp
)
2566 ofport
->n_qdscp
= n_qdscp
;
2574 /* Expires all MAC learning entries associated with 'bundle' and forces its
2575 * ofproto to revalidate every flow.
2577 * Normally MAC learning entries are removed only from the ofproto associated
2578 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2579 * are removed from every ofproto. When patch ports and SLB bonds are in use
2580 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2581 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2582 * with the host from which it migrated. */
2584 bundle_flush_macs(struct ofbundle
*bundle
, bool all_ofprotos
)
2586 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2587 struct mac_learning
*ml
= ofproto
->ml
;
2588 struct mac_entry
*mac
, *next_mac
;
2590 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2591 ovs_rwlock_wrlock(&ml
->rwlock
);
2592 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2593 if (mac_entry_get_port(ml
, mac
) == bundle
) {
2595 struct ofproto_dpif
*o
;
2597 HMAP_FOR_EACH (o
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
2599 struct mac_entry
*e
;
2601 ovs_rwlock_wrlock(&o
->ml
->rwlock
);
2602 e
= mac_learning_lookup(o
->ml
, mac
->mac
, mac
->vlan
);
2604 mac_learning_expire(o
->ml
, e
);
2606 ovs_rwlock_unlock(&o
->ml
->rwlock
);
2611 mac_learning_expire(ml
, mac
);
2614 ovs_rwlock_unlock(&ml
->rwlock
);
2618 bundle_move(struct ofbundle
*old
, struct ofbundle
*new)
2620 struct ofproto_dpif
*ofproto
= old
->ofproto
;
2621 struct mac_learning
*ml
= ofproto
->ml
;
2622 struct mac_entry
*mac
, *next_mac
;
2624 ovs_assert(new->ofproto
== old
->ofproto
);
2626 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2627 ovs_rwlock_wrlock(&ml
->rwlock
);
2628 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2629 if (mac_entry_get_port(ml
, mac
) == old
) {
2630 mac_entry_set_port(ml
, mac
, new);
2633 ovs_rwlock_unlock(&ml
->rwlock
);
2636 static struct ofbundle
*
2637 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
2639 struct ofbundle
*bundle
;
2641 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
2642 &ofproto
->bundles
) {
2643 if (bundle
->aux
== aux
) {
2651 bundle_update(struct ofbundle
*bundle
)
2653 struct ofport_dpif
*port
;
2655 bundle
->floodable
= true;
2656 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2657 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2659 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2660 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2661 bundle
->floodable
= false;
2668 bundle_del_port(struct ofport_dpif
*port
)
2670 struct ofbundle
*bundle
= port
->bundle
;
2672 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2674 list_remove(&port
->bundle_node
);
2675 port
->bundle
= NULL
;
2678 lacp_slave_unregister(bundle
->lacp
, port
);
2681 bond_slave_unregister(bundle
->bond
, port
);
2684 bundle_update(bundle
);
2688 bundle_add_port(struct ofbundle
*bundle
, ofp_port_t ofp_port
,
2689 struct lacp_slave_settings
*lacp
)
2691 struct ofport_dpif
*port
;
2693 port
= ofp_port_to_ofport(bundle
->ofproto
, ofp_port
);
2698 if (port
->bundle
!= bundle
) {
2699 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2701 bundle_remove(&port
->up
);
2704 port
->bundle
= bundle
;
2705 list_push_back(&bundle
->ports
, &port
->bundle_node
);
2706 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2708 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2709 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2710 bundle
->floodable
= false;
2714 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2715 lacp_slave_register(bundle
->lacp
, port
, lacp
);
2722 bundle_destroy(struct ofbundle
*bundle
)
2724 struct ofproto_dpif
*ofproto
;
2725 struct ofport_dpif
*port
, *next_port
;
2731 ofproto
= bundle
->ofproto
;
2732 mbridge_unregister_bundle(ofproto
->mbridge
, bundle
);
2735 xlate_bundle_remove(bundle
);
2738 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2739 bundle_del_port(port
);
2742 bundle_flush_macs(bundle
, true);
2743 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
2745 free(bundle
->trunks
);
2746 lacp_unref(bundle
->lacp
);
2747 bond_unref(bundle
->bond
);
2752 bundle_set(struct ofproto
*ofproto_
, void *aux
,
2753 const struct ofproto_bundle_settings
*s
)
2755 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2756 bool need_flush
= false;
2757 struct ofport_dpif
*port
;
2758 struct ofbundle
*bundle
;
2759 unsigned long *trunks
;
2765 bundle_destroy(bundle_lookup(ofproto
, aux
));
2769 ovs_assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
2770 ovs_assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
2772 bundle
= bundle_lookup(ofproto
, aux
);
2774 bundle
= xmalloc(sizeof *bundle
);
2776 bundle
->ofproto
= ofproto
;
2777 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
2778 hash_pointer(aux
, 0));
2780 bundle
->name
= NULL
;
2782 list_init(&bundle
->ports
);
2783 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
2785 bundle
->trunks
= NULL
;
2786 bundle
->use_priority_tags
= s
->use_priority_tags
;
2787 bundle
->lacp
= NULL
;
2788 bundle
->bond
= NULL
;
2790 bundle
->floodable
= true;
2791 mbridge_register_bundle(ofproto
->mbridge
, bundle
);
2794 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
2796 bundle
->name
= xstrdup(s
->name
);
2801 ofproto
->lacp_enabled
= true;
2802 if (!bundle
->lacp
) {
2803 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2804 bundle
->lacp
= lacp_create();
2806 lacp_configure(bundle
->lacp
, s
->lacp
);
2808 lacp_unref(bundle
->lacp
);
2809 bundle
->lacp
= NULL
;
2812 /* Update set of ports. */
2814 for (i
= 0; i
< s
->n_slaves
; i
++) {
2815 if (!bundle_add_port(bundle
, s
->slaves
[i
],
2816 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
)) {
2820 if (!ok
|| list_size(&bundle
->ports
) != s
->n_slaves
) {
2821 struct ofport_dpif
*next_port
;
2823 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2824 for (i
= 0; i
< s
->n_slaves
; i
++) {
2825 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
2830 bundle_del_port(port
);
2834 ovs_assert(list_size(&bundle
->ports
) <= s
->n_slaves
);
2836 if (list_is_empty(&bundle
->ports
)) {
2837 bundle_destroy(bundle
);
2841 /* Set VLAN tagging mode */
2842 if (s
->vlan_mode
!= bundle
->vlan_mode
2843 || s
->use_priority_tags
!= bundle
->use_priority_tags
) {
2844 bundle
->vlan_mode
= s
->vlan_mode
;
2845 bundle
->use_priority_tags
= s
->use_priority_tags
;
2850 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
2851 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
2853 if (vlan
!= bundle
->vlan
) {
2854 bundle
->vlan
= vlan
;
2858 /* Get trunked VLANs. */
2859 switch (s
->vlan_mode
) {
2860 case PORT_VLAN_ACCESS
:
2864 case PORT_VLAN_TRUNK
:
2865 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
2868 case PORT_VLAN_NATIVE_UNTAGGED
:
2869 case PORT_VLAN_NATIVE_TAGGED
:
2870 if (vlan
!= 0 && (!s
->trunks
2871 || !bitmap_is_set(s
->trunks
, vlan
)
2872 || bitmap_is_set(s
->trunks
, 0))) {
2873 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2875 trunks
= bitmap_clone(s
->trunks
, 4096);
2877 trunks
= bitmap_allocate1(4096);
2879 bitmap_set1(trunks
, vlan
);
2880 bitmap_set0(trunks
, 0);
2882 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
2889 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
2890 free(bundle
->trunks
);
2891 if (trunks
== s
->trunks
) {
2892 bundle
->trunks
= vlan_bitmap_clone(trunks
);
2894 bundle
->trunks
= trunks
;
2899 if (trunks
!= s
->trunks
) {
2904 if (!list_is_short(&bundle
->ports
)) {
2905 bundle
->ofproto
->has_bonded_bundles
= true;
2907 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
2908 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2911 bundle
->bond
= bond_create(s
->bond
, ofproto
);
2912 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2915 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2916 bond_slave_register(bundle
->bond
, port
,
2917 port
->up
.ofp_port
, port
->up
.netdev
);
2920 bond_unref(bundle
->bond
);
2921 bundle
->bond
= NULL
;
2924 /* If we changed something that would affect MAC learning, un-learn
2925 * everything on this port and force flow revalidation. */
2927 bundle_flush_macs(bundle
, false);
2934 bundle_remove(struct ofport
*port_
)
2936 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
2937 struct ofbundle
*bundle
= port
->bundle
;
2940 bundle_del_port(port
);
2941 if (list_is_empty(&bundle
->ports
)) {
2942 bundle_destroy(bundle
);
2943 } else if (list_is_short(&bundle
->ports
)) {
2944 bond_unref(bundle
->bond
);
2945 bundle
->bond
= NULL
;
2951 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
2953 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
2954 struct ofport_dpif
*port
= port_
;
2955 uint8_t ea
[ETH_ADDR_LEN
];
2958 error
= netdev_get_etheraddr(port
->up
.netdev
, ea
);
2960 struct dp_packet packet
;
2963 dp_packet_init(&packet
, 0);
2964 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
2966 memcpy(packet_pdu
, pdu
, pdu_size
);
2968 ofproto_dpif_send_packet(port
, &packet
);
2969 dp_packet_uninit(&packet
);
2971 VLOG_ERR_RL(&rl
, "port %s: cannot obtain Ethernet address of iface "
2972 "%s (%s)", port
->bundle
->name
,
2973 netdev_get_name(port
->up
.netdev
), ovs_strerror(error
));
2978 bundle_send_learning_packets(struct ofbundle
*bundle
)
2980 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2981 int error
, n_packets
, n_errors
;
2982 struct mac_entry
*e
;
2984 struct ovs_list list_node
;
2985 struct ofport_dpif
*port
;
2986 struct dp_packet
*pkt
;
2988 struct ovs_list packets
;
2990 list_init(&packets
);
2991 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
2992 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
2993 if (mac_entry_get_port(ofproto
->ml
, e
) != bundle
) {
2994 pkt_node
= xmalloc(sizeof *pkt_node
);
2995 pkt_node
->pkt
= bond_compose_learning_packet(bundle
->bond
,
2997 (void **)&pkt_node
->port
);
2998 list_push_back(&packets
, &pkt_node
->list_node
);
3001 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3003 error
= n_packets
= n_errors
= 0;
3004 LIST_FOR_EACH_POP (pkt_node
, list_node
, &packets
) {
3007 ret
= ofproto_dpif_send_packet(pkt_node
->port
, pkt_node
->pkt
);
3008 dp_packet_delete(pkt_node
->pkt
);
3018 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3019 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
3020 "packets, last error was: %s",
3021 bundle
->name
, n_errors
, n_packets
, ovs_strerror(error
));
3023 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3024 bundle
->name
, n_packets
);
3029 bundle_run(struct ofbundle
*bundle
)
3032 lacp_run(bundle
->lacp
, send_pdu_cb
);
3035 struct ofport_dpif
*port
;
3037 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3038 bond_slave_set_may_enable(bundle
->bond
, port
, port
->may_enable
);
3041 if (bond_run(bundle
->bond
, lacp_status(bundle
->lacp
))) {
3042 bundle
->ofproto
->backer
->need_revalidate
= REV_BOND
;
3045 if (bond_should_send_learning_packets(bundle
->bond
)) {
3046 bundle_send_learning_packets(bundle
);
3052 bundle_wait(struct ofbundle
*bundle
)
3055 lacp_wait(bundle
->lacp
);
3058 bond_wait(bundle
->bond
);
3065 mirror_set__(struct ofproto
*ofproto_
, void *aux
,
3066 const struct ofproto_mirror_settings
*s
)
3068 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3069 struct ofbundle
**srcs
, **dsts
;
3074 mirror_destroy(ofproto
->mbridge
, aux
);
3078 srcs
= xmalloc(s
->n_srcs
* sizeof *srcs
);
3079 dsts
= xmalloc(s
->n_dsts
* sizeof *dsts
);
3081 for (i
= 0; i
< s
->n_srcs
; i
++) {
3082 srcs
[i
] = bundle_lookup(ofproto
, s
->srcs
[i
]);
3085 for (i
= 0; i
< s
->n_dsts
; i
++) {
3086 dsts
[i
] = bundle_lookup(ofproto
, s
->dsts
[i
]);
3089 error
= mirror_set(ofproto
->mbridge
, aux
, s
->name
, srcs
, s
->n_srcs
, dsts
,
3090 s
->n_dsts
, s
->src_vlans
,
3091 bundle_lookup(ofproto
, s
->out_bundle
), s
->out_vlan
);
3098 mirror_get_stats__(struct ofproto
*ofproto
, void *aux
,
3099 uint64_t *packets
, uint64_t *bytes
)
3101 return mirror_get_stats(ofproto_dpif_cast(ofproto
)->mbridge
, aux
, packets
,
3106 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
3108 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3109 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3110 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
3111 mac_learning_flush(ofproto
->ml
);
3113 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3118 is_mirror_output_bundle(const struct ofproto
*ofproto_
, void *aux
)
3120 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3121 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3122 return bundle
&& mirror_bundle_out(ofproto
->mbridge
, bundle
) != 0;
3126 forward_bpdu_changed(struct ofproto
*ofproto_
)
3128 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3129 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3133 set_mac_table_config(struct ofproto
*ofproto_
, unsigned int idle_time
,
3136 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3137 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3138 mac_learning_set_idle_time(ofproto
->ml
, idle_time
);
3139 mac_learning_set_max_entries(ofproto
->ml
, max_entries
);
3140 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3143 /* Configures multicast snooping on 'ofport' using the settings
3144 * defined in 's'. */
3146 set_mcast_snooping(struct ofproto
*ofproto_
,
3147 const struct ofproto_mcast_snooping_settings
*s
)
3149 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3151 /* Only revalidate flows if the configuration changed. */
3152 if (!s
!= !ofproto
->ms
) {
3153 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3158 ofproto
->ms
= mcast_snooping_create();
3161 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3162 mcast_snooping_set_idle_time(ofproto
->ms
, s
->idle_time
);
3163 mcast_snooping_set_max_entries(ofproto
->ms
, s
->max_entries
);
3164 if (mcast_snooping_set_flood_unreg(ofproto
->ms
, s
->flood_unreg
)) {
3165 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3167 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3169 mcast_snooping_unref(ofproto
->ms
);
3176 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3178 set_mcast_snooping_port(struct ofproto
*ofproto_
, void *aux
,
3179 const struct ofproto_mcast_snooping_port_settings
*s
)
3181 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3182 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3184 if (ofproto
->ms
&& s
) {
3185 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3186 mcast_snooping_set_port_flood(ofproto
->ms
, bundle
, s
->flood
);
3187 mcast_snooping_set_port_flood_reports(ofproto
->ms
, bundle
,
3189 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3197 struct ofport_dpif
*
3198 ofp_port_to_ofport(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
3200 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
3201 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
3205 ofproto_port_from_dpif_port(struct ofproto_dpif
*ofproto
,
3206 struct ofproto_port
*ofproto_port
,
3207 struct dpif_port
*dpif_port
)
3209 ofproto_port
->name
= dpif_port
->name
;
3210 ofproto_port
->type
= dpif_port
->type
;
3211 ofproto_port
->ofp_port
= odp_port_to_ofp_port(ofproto
, dpif_port
->port_no
);
3215 ofport_update_peer(struct ofport_dpif
*ofport
)
3217 const struct ofproto_dpif
*ofproto
;
3218 struct dpif_backer
*backer
;
3221 if (!netdev_vport_is_patch(ofport
->up
.netdev
)) {
3225 backer
= ofproto_dpif_cast(ofport
->up
.ofproto
)->backer
;
3226 backer
->need_revalidate
= REV_RECONFIGURE
;
3229 ofport
->peer
->peer
= NULL
;
3230 ofport
->peer
= NULL
;
3233 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3238 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
3239 struct ofport
*peer_ofport
;
3240 struct ofport_dpif
*peer
;
3243 if (ofproto
->backer
!= backer
) {
3247 peer_ofport
= shash_find_data(&ofproto
->up
.port_by_name
, peer_name
);
3252 peer
= ofport_dpif_cast(peer_ofport
);
3253 peer_peer
= netdev_vport_patch_peer(peer
->up
.netdev
);
3254 if (peer_peer
&& !strcmp(netdev_get_name(ofport
->up
.netdev
),
3256 ofport
->peer
= peer
;
3257 ofport
->peer
->peer
= ofport
;
3267 port_run(struct ofport_dpif
*ofport
)
3269 long long int carrier_seq
= netdev_get_carrier_resets(ofport
->up
.netdev
);
3270 bool carrier_changed
= carrier_seq
!= ofport
->carrier_seq
;
3271 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
3272 bool cfm_enable
= false;
3273 bool bfd_enable
= false;
3275 ofport
->carrier_seq
= carrier_seq
;
3278 int cfm_opup
= cfm_get_opup(ofport
->cfm
);
3280 cfm_enable
= !cfm_get_fault(ofport
->cfm
);
3282 if (cfm_opup
>= 0) {
3283 cfm_enable
= cfm_enable
&& cfm_opup
;
3288 bfd_enable
= bfd_forwarding(ofport
->bfd
);
3291 if (ofport
->bfd
|| ofport
->cfm
) {
3292 enable
= enable
&& (cfm_enable
|| bfd_enable
);
3295 if (ofport
->bundle
) {
3296 enable
= enable
&& lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
);
3297 if (carrier_changed
) {
3298 lacp_slave_carrier_changed(ofport
->bundle
->lacp
, ofport
);
3302 if (ofport
->may_enable
!= enable
) {
3303 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3305 ofproto
->backer
->need_revalidate
= REV_PORT_TOGGLED
;
3307 if (ofport
->rstp_port
) {
3308 rstp_port_set_mac_operational(ofport
->rstp_port
, enable
);
3312 ofport
->may_enable
= enable
;
3316 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
3317 struct ofproto_port
*ofproto_port
)
3319 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3320 struct dpif_port dpif_port
;
3323 if (sset_contains(&ofproto
->ghost_ports
, devname
)) {
3324 const char *type
= netdev_get_type_from_name(devname
);
3326 /* We may be called before ofproto->up.port_by_name is populated with
3327 * the appropriate ofport. For this reason, we must get the name and
3328 * type from the netdev layer directly. */
3330 const struct ofport
*ofport
;
3332 ofport
= shash_find_data(&ofproto
->up
.port_by_name
, devname
);
3333 ofproto_port
->ofp_port
= ofport
? ofport
->ofp_port
: OFPP_NONE
;
3334 ofproto_port
->name
= xstrdup(devname
);
3335 ofproto_port
->type
= xstrdup(type
);
3341 if (!sset_contains(&ofproto
->ports
, devname
)) {
3344 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
,
3345 devname
, &dpif_port
);
3347 ofproto_port_from_dpif_port(ofproto
, ofproto_port
, &dpif_port
);
3353 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
)
3355 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3356 const char *devname
= netdev_get_name(netdev
);
3357 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
3358 const char *dp_port_name
;
3360 if (netdev_vport_is_patch(netdev
)) {
3361 sset_add(&ofproto
->ghost_ports
, netdev_get_name(netdev
));
3365 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
3366 if (!dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
3367 odp_port_t port_no
= ODPP_NONE
;
3370 error
= dpif_port_add(ofproto
->backer
->dpif
, netdev
, &port_no
);
3374 if (netdev_get_tunnel_config(netdev
)) {
3375 simap_put(&ofproto
->backer
->tnl_backers
,
3376 dp_port_name
, odp_to_u32(port_no
));
3380 if (netdev_get_tunnel_config(netdev
)) {
3381 sset_add(&ofproto
->ghost_ports
, devname
);
3383 sset_add(&ofproto
->ports
, devname
);
3389 port_del(struct ofproto
*ofproto_
, ofp_port_t ofp_port
)
3391 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3392 struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
3399 sset_find_and_delete(&ofproto
->ghost_ports
,
3400 netdev_get_name(ofport
->up
.netdev
));
3401 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3402 if (!ofport
->is_tunnel
&& !netdev_vport_is_patch(ofport
->up
.netdev
)) {
3403 error
= dpif_port_del(ofproto
->backer
->dpif
, ofport
->odp_port
);
3405 /* The caller is going to close ofport->up.netdev. If this is a
3406 * bonded port, then the bond is using that netdev, so remove it
3407 * from the bond. The client will need to reconfigure everything
3408 * after deleting ports, so then the slave will get re-added. */
3409 bundle_remove(&ofport
->up
);
3416 port_get_stats(const struct ofport
*ofport_
, struct netdev_stats
*stats
)
3418 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3421 error
= netdev_get_stats(ofport
->up
.netdev
, stats
);
3423 if (!error
&& ofport_
->ofp_port
== OFPP_LOCAL
) {
3424 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3426 ovs_mutex_lock(&ofproto
->stats_mutex
);
3427 /* ofproto->stats.tx_packets represents packets that we created
3428 * internally and sent to some port (e.g. packets sent with
3429 * ofproto_dpif_send_packet()). Account for them as if they had
3430 * come from OFPP_LOCAL and got forwarded. */
3432 if (stats
->rx_packets
!= UINT64_MAX
) {
3433 stats
->rx_packets
+= ofproto
->stats
.tx_packets
;
3436 if (stats
->rx_bytes
!= UINT64_MAX
) {
3437 stats
->rx_bytes
+= ofproto
->stats
.tx_bytes
;
3440 /* ofproto->stats.rx_packets represents packets that were received on
3441 * some port and we processed internally and dropped (e.g. STP).
3442 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3444 if (stats
->tx_packets
!= UINT64_MAX
) {
3445 stats
->tx_packets
+= ofproto
->stats
.rx_packets
;
3448 if (stats
->tx_bytes
!= UINT64_MAX
) {
3449 stats
->tx_bytes
+= ofproto
->stats
.rx_bytes
;
3451 ovs_mutex_unlock(&ofproto
->stats_mutex
);
3458 port_get_lacp_stats(const struct ofport
*ofport_
, struct lacp_slave_stats
*stats
)
3460 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3461 if (ofport
->bundle
&& ofport
->bundle
->lacp
) {
3462 if (lacp_get_slave_stats(ofport
->bundle
->lacp
, ofport
, stats
)) {
3469 struct port_dump_state
{
3474 struct ofproto_port port
;
3479 port_dump_start(const struct ofproto
*ofproto_ OVS_UNUSED
, void **statep
)
3481 *statep
= xzalloc(sizeof(struct port_dump_state
));
3486 port_dump_next(const struct ofproto
*ofproto_
, void *state_
,
3487 struct ofproto_port
*port
)
3489 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3490 struct port_dump_state
*state
= state_
;
3491 const struct sset
*sset
;
3492 struct sset_node
*node
;
3494 if (state
->has_port
) {
3495 ofproto_port_destroy(&state
->port
);
3496 state
->has_port
= false;
3498 sset
= state
->ghost
? &ofproto
->ghost_ports
: &ofproto
->ports
;
3499 while ((node
= sset_at_position(sset
, &state
->bucket
, &state
->offset
))) {
3502 error
= port_query_by_name(ofproto_
, node
->name
, &state
->port
);
3504 *port
= state
->port
;
3505 state
->has_port
= true;
3507 } else if (error
!= ENODEV
) {
3512 if (!state
->ghost
) {
3513 state
->ghost
= true;
3516 return port_dump_next(ofproto_
, state_
, port
);
3523 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
3525 struct port_dump_state
*state
= state_
;
3527 if (state
->has_port
) {
3528 ofproto_port_destroy(&state
->port
);
3535 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
3537 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3539 if (ofproto
->port_poll_errno
) {
3540 int error
= ofproto
->port_poll_errno
;
3541 ofproto
->port_poll_errno
= 0;
3545 if (sset_is_empty(&ofproto
->port_poll_set
)) {
3549 *devnamep
= sset_pop(&ofproto
->port_poll_set
);
3554 port_poll_wait(const struct ofproto
*ofproto_
)
3556 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3557 dpif_port_poll_wait(ofproto
->backer
->dpif
);
3561 port_is_lacp_current(const struct ofport
*ofport_
)
3563 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3564 return (ofport
->bundle
&& ofport
->bundle
->lacp
3565 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
3569 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3570 * then delete it entirely. */
3572 rule_expire(struct rule_dpif
*rule
)
3573 OVS_REQUIRES(ofproto_mutex
)
3575 uint16_t hard_timeout
, idle_timeout
;
3576 long long int now
= time_msec();
3579 hard_timeout
= rule
->up
.hard_timeout
;
3580 idle_timeout
= rule
->up
.idle_timeout
;
3582 /* Has 'rule' expired? */
3584 long long int modified
;
3586 ovs_mutex_lock(&rule
->up
.mutex
);
3587 modified
= rule
->up
.modified
;
3588 ovs_mutex_unlock(&rule
->up
.mutex
);
3590 if (now
> modified
+ hard_timeout
* 1000) {
3591 reason
= OFPRR_HARD_TIMEOUT
;
3595 if (reason
< 0 && idle_timeout
) {
3598 ovs_mutex_lock(&rule
->stats_mutex
);
3599 used
= rule
->stats
.used
;
3600 ovs_mutex_unlock(&rule
->stats_mutex
);
3602 if (now
> used
+ idle_timeout
* 1000) {
3603 reason
= OFPRR_IDLE_TIMEOUT
;
3608 COVERAGE_INC(ofproto_dpif_expired
);
3609 ofproto_rule_expire(&rule
->up
, reason
);
3613 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3614 * 'flow' must reflect the data in 'packet'. */
3616 ofproto_dpif_execute_actions(struct ofproto_dpif
*ofproto
,
3617 const struct flow
*flow
,
3618 struct rule_dpif
*rule
,
3619 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3620 struct dp_packet
*packet
)
3622 struct dpif_flow_stats stats
;
3623 struct xlate_out xout
;
3624 struct xlate_in xin
;
3626 struct dpif_execute execute
;
3629 ovs_assert((rule
!= NULL
) != (ofpacts
!= NULL
));
3631 dpif_flow_stats_extract(flow
, packet
, time_msec(), &stats
);
3634 rule_dpif_credit_stats(rule
, &stats
);
3637 xlate_in_init(&xin
, ofproto
, flow
, flow
->in_port
.ofp_port
, rule
,
3638 stats
.tcp_flags
, packet
);
3639 xin
.ofpacts
= ofpacts
;
3640 xin
.ofpacts_len
= ofpacts_len
;
3641 xin
.resubmit_stats
= &stats
;
3642 xlate_actions(&xin
, &xout
);
3644 execute
.actions
= xout
.odp_actions
->data
;
3645 execute
.actions_len
= xout
.odp_actions
->size
;
3647 pkt_metadata_from_flow(&packet
->md
, flow
);
3648 execute
.packet
= packet
;
3649 execute
.needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
3650 execute
.probe
= false;
3652 /* Fix up in_port. */
3653 in_port
= flow
->in_port
.ofp_port
;
3654 if (in_port
== OFPP_NONE
) {
3655 in_port
= OFPP_LOCAL
;
3657 execute
.packet
->md
.in_port
.odp_port
= ofp_port_to_odp_port(ofproto
, in_port
);
3659 error
= dpif_execute(ofproto
->backer
->dpif
, &execute
);
3661 xlate_out_uninit(&xout
);
3667 rule_dpif_credit_stats(struct rule_dpif
*rule
,
3668 const struct dpif_flow_stats
*stats
)
3670 ovs_mutex_lock(&rule
->stats_mutex
);
3671 rule
->stats
.n_packets
+= stats
->n_packets
;
3672 rule
->stats
.n_bytes
+= stats
->n_bytes
;
3673 rule
->stats
.used
= MAX(rule
->stats
.used
, stats
->used
);
3674 ovs_mutex_unlock(&rule
->stats_mutex
);
3678 rule_dpif_get_flow_cookie(const struct rule_dpif
*rule
)
3679 OVS_REQUIRES(rule
->up
.mutex
)
3681 return rule
->up
.flow_cookie
;
3685 rule_dpif_reduce_timeouts(struct rule_dpif
*rule
, uint16_t idle_timeout
,
3686 uint16_t hard_timeout
)
3688 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
3691 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3692 * be read until the calling thread quiesces. */
3693 const struct rule_actions
*
3694 rule_dpif_get_actions(const struct rule_dpif
*rule
)
3696 return rule_get_actions(&rule
->up
);
3699 /* Sets 'rule''s recirculation id. */
3701 rule_dpif_set_recirc_id(struct rule_dpif
*rule
, uint32_t id
)
3702 OVS_REQUIRES(rule
->up
.mutex
)
3704 ovs_assert(!rule
->recirc_id
|| rule
->recirc_id
== id
);
3705 if (rule
->recirc_id
== id
) {
3706 /* Release the new reference to the same id. */
3709 rule
->recirc_id
= id
;
3713 /* Sets 'rule''s recirculation id. */
3715 rule_set_recirc_id(struct rule
*rule_
, uint32_t id
)
3717 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3719 ovs_mutex_lock(&rule
->up
.mutex
);
3720 rule_dpif_set_recirc_id(rule
, id
);
3721 ovs_mutex_unlock(&rule
->up
.mutex
);
3725 ofproto_dpif_get_tables_version(struct ofproto_dpif
*ofproto
)
3729 atomic_read_relaxed(&ofproto
->tables_version
, &version
);
3733 /* The returned rule (if any) is valid at least until the next RCU quiescent
3734 * period. If the rule needs to stay around longer, a non-zero 'take_ref'
3735 * must be passed in to cause a reference to be taken on it.
3737 * 'flow' is non-const to allow for temporary modifications during the lookup.
3738 * Any changes are restored before returning. */
3739 static struct rule_dpif
*
3740 rule_dpif_lookup_in_table(struct ofproto_dpif
*ofproto
, long long version
,
3741 uint8_t table_id
, struct flow
*flow
,
3742 struct flow_wildcards
*wc
, bool take_ref
)
3744 struct classifier
*cls
= &ofproto
->up
.tables
[table_id
].cls
;
3745 const struct cls_rule
*cls_rule
;
3746 struct rule_dpif
*rule
;
3749 cls_rule
= classifier_lookup(cls
, version
, flow
, wc
);
3751 rule
= rule_dpif_cast(rule_from_cls_rule(cls_rule
));
3753 /* Try again if the rule was released before we get the reference. */
3754 } while (rule
&& take_ref
&& !rule_dpif_try_ref(rule
));
3759 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3760 * Returns the rule that was found, which may be one of the special rules
3761 * according to packet miss hadling. If 'may_packet_in' is false, returning of
3762 * the miss_rule (which issues packet ins for the controller) is avoided.
3763 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3766 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3767 * if none is found then the table miss configuration for that table is
3768 * honored, which can result in additional lookups in other OpenFlow tables.
3769 * In this case the function updates '*table_id' to reflect the final OpenFlow
3770 * table that was searched.
3772 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3775 * The rule is returned in '*rule', which is valid at least until the next
3776 * RCU quiescent period. If the '*rule' needs to stay around longer,
3777 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3778 * on it before this returns.
3780 * 'in_port' allows the lookup to take place as if the in port had the value
3781 * 'in_port'. This is needed for resubmit action support.
3783 * 'flow' is non-const to allow for temporary modifications during the lookup.
3784 * Any changes are restored before returning. */
3786 rule_dpif_lookup_from_table(struct ofproto_dpif
*ofproto
, long long version
,
3787 struct flow
*flow
, struct flow_wildcards
*wc
,
3788 bool take_ref
, const struct dpif_flow_stats
*stats
,
3789 uint8_t *table_id
, ofp_port_t in_port
,
3790 bool may_packet_in
, bool honor_table_miss
)
3792 ovs_be16 old_tp_src
= flow
->tp_src
, old_tp_dst
= flow
->tp_dst
;
3793 ofp_port_t old_in_port
= flow
->in_port
.ofp_port
;
3794 enum ofputil_table_miss miss_config
;
3795 struct rule_dpif
*rule
;
3798 /* We always unwildcard nw_frag (for IP), so they
3799 * need not be unwildcarded here. */
3800 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
3801 && ofproto
->up
.frag_handling
!= OFPC_FRAG_NX_MATCH
) {
3802 if (ofproto
->up
.frag_handling
== OFPC_FRAG_NORMAL
) {
3803 /* We must pretend that transport ports are unavailable. */
3804 flow
->tp_src
= htons(0);
3805 flow
->tp_dst
= htons(0);
3807 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3808 * Use the drop_frags_rule (which cannot disappear). */
3809 rule
= ofproto
->drop_frags_rule
;
3811 rule_dpif_ref(rule
);
3814 struct oftable
*tbl
= &ofproto
->up
.tables
[*table_id
];
3817 atomic_add_relaxed(&tbl
->n_matched
, stats
->n_packets
, &orig
);
3823 /* Look up a flow with 'in_port' as the input port. Then restore the
3824 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3825 * have surprising behavior). */
3826 flow
->in_port
.ofp_port
= in_port
;
3828 /* Our current implementation depends on n_tables == N_TABLES, and
3829 * TBL_INTERNAL being the last table. */
3830 BUILD_ASSERT_DECL(N_TABLES
== TBL_INTERNAL
+ 1);
3832 miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
3834 for (next_id
= *table_id
;
3835 next_id
< ofproto
->up
.n_tables
;
3836 next_id
++, next_id
+= (next_id
== TBL_INTERNAL
))
3838 *table_id
= next_id
;
3839 rule
= rule_dpif_lookup_in_table(ofproto
, version
, next_id
, flow
, wc
,
3842 struct oftable
*tbl
= &ofproto
->up
.tables
[next_id
];
3845 atomic_add_relaxed(rule
? &tbl
->n_matched
: &tbl
->n_missed
,
3846 stats
->n_packets
, &orig
);
3849 goto out
; /* Match. */
3851 if (honor_table_miss
) {
3852 miss_config
= ofproto_table_get_miss_config(&ofproto
->up
,
3854 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
3861 rule
= ofproto
->no_packet_in_rule
;
3862 if (may_packet_in
) {
3863 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
3864 || miss_config
== OFPUTIL_TABLE_MISS_CONTROLLER
) {
3865 struct ofport_dpif
*port
;
3867 port
= ofp_port_to_ofport(ofproto
, old_in_port
);
3869 VLOG_WARN_RL(&rl
, "packet-in on unknown OpenFlow port %"PRIu16
,
3871 } else if (!(port
->up
.pp
.config
& OFPUTIL_PC_NO_PACKET_IN
)) {
3872 rule
= ofproto
->miss_rule
;
3874 } else if (miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
&&
3875 connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
)) {
3876 rule
= ofproto
->miss_rule
;
3880 rule_dpif_ref(rule
);
3883 /* Restore port numbers, as they may have been modified above. */
3884 flow
->tp_src
= old_tp_src
;
3885 flow
->tp_dst
= old_tp_dst
;
3886 /* Restore the old in port. */
3887 flow
->in_port
.ofp_port
= old_in_port
;
3893 complete_operation(struct rule_dpif
*rule
)
3894 OVS_REQUIRES(ofproto_mutex
)
3896 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
3898 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
3901 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
3903 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
3906 static struct rule
*
3909 struct rule_dpif
*rule
= xzalloc(sizeof *rule
);
3914 rule_dealloc(struct rule
*rule_
)
3916 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3921 rule_construct(struct rule
*rule_
)
3922 OVS_NO_THREAD_SAFETY_ANALYSIS
3924 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3925 ovs_mutex_init_adaptive(&rule
->stats_mutex
);
3926 rule
->stats
.n_packets
= 0;
3927 rule
->stats
.n_bytes
= 0;
3928 rule
->stats
.used
= rule
->up
.modified
;
3929 rule
->recirc_id
= 0;
3935 rule_insert(struct rule
*rule_
)
3936 OVS_REQUIRES(ofproto_mutex
)
3938 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3939 complete_operation(rule
);
3944 rule_delete(struct rule
*rule_
)
3945 OVS_REQUIRES(ofproto_mutex
)
3947 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3948 complete_operation(rule
);
3952 rule_destruct(struct rule
*rule_
)
3954 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3956 ovs_mutex_destroy(&rule
->stats_mutex
);
3957 if (rule
->recirc_id
) {
3958 recirc_free_id(rule
->recirc_id
);
3963 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
,
3964 long long int *used
)
3966 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3968 ovs_mutex_lock(&rule
->stats_mutex
);
3969 *packets
= rule
->stats
.n_packets
;
3970 *bytes
= rule
->stats
.n_bytes
;
3971 *used
= rule
->stats
.used
;
3972 ovs_mutex_unlock(&rule
->stats_mutex
);
3976 rule_dpif_execute(struct rule_dpif
*rule
, const struct flow
*flow
,
3977 struct dp_packet
*packet
)
3979 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
3981 ofproto_dpif_execute_actions(ofproto
, flow
, rule
, NULL
, 0, packet
);
3985 rule_execute(struct rule
*rule
, const struct flow
*flow
,
3986 struct dp_packet
*packet
)
3988 rule_dpif_execute(rule_dpif_cast(rule
), flow
, packet
);
3989 dp_packet_delete(packet
);
3994 rule_modify_actions(struct rule
*rule_
, bool reset_counters
)
3995 OVS_REQUIRES(ofproto_mutex
)
3997 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3999 if (reset_counters
) {
4000 ovs_mutex_lock(&rule
->stats_mutex
);
4001 rule
->stats
.n_packets
= 0;
4002 rule
->stats
.n_bytes
= 0;
4003 ovs_mutex_unlock(&rule
->stats_mutex
);
4006 complete_operation(rule
);
4009 static struct group_dpif
*group_dpif_cast(const struct ofgroup
*group
)
4011 return group
? CONTAINER_OF(group
, struct group_dpif
, up
) : NULL
;
4014 static struct ofgroup
*
4017 struct group_dpif
*group
= xzalloc(sizeof *group
);
4022 group_dealloc(struct ofgroup
*group_
)
4024 struct group_dpif
*group
= group_dpif_cast(group_
);
4029 group_construct_stats(struct group_dpif
*group
)
4030 OVS_REQUIRES(group
->stats_mutex
)
4032 struct ofputil_bucket
*bucket
;
4033 const struct ovs_list
*buckets
;
4035 group
->packet_count
= 0;
4036 group
->byte_count
= 0;
4038 group_dpif_get_buckets(group
, &buckets
);
4039 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4040 bucket
->stats
.packet_count
= 0;
4041 bucket
->stats
.byte_count
= 0;
4046 group_dpif_credit_stats(struct group_dpif
*group
,
4047 struct ofputil_bucket
*bucket
,
4048 const struct dpif_flow_stats
*stats
)
4050 ovs_mutex_lock(&group
->stats_mutex
);
4051 group
->packet_count
+= stats
->n_packets
;
4052 group
->byte_count
+= stats
->n_bytes
;
4054 bucket
->stats
.packet_count
+= stats
->n_packets
;
4055 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4056 } else { /* Credit to all buckets */
4057 const struct ovs_list
*buckets
;
4059 group_dpif_get_buckets(group
, &buckets
);
4060 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4061 bucket
->stats
.packet_count
+= stats
->n_packets
;
4062 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4065 ovs_mutex_unlock(&group
->stats_mutex
);
4069 group_construct(struct ofgroup
*group_
)
4071 struct group_dpif
*group
= group_dpif_cast(group_
);
4072 const struct ofputil_bucket
*bucket
;
4074 /* Prevent group chaining because our locking structure makes it hard to
4075 * implement deadlock-free. (See xlate_group_resource_check().) */
4076 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4077 const struct ofpact
*a
;
4079 OFPACT_FOR_EACH (a
, bucket
->ofpacts
, bucket
->ofpacts_len
) {
4080 if (a
->type
== OFPACT_GROUP
) {
4081 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED
;
4086 ovs_mutex_init_adaptive(&group
->stats_mutex
);
4087 ovs_mutex_lock(&group
->stats_mutex
);
4088 group_construct_stats(group
);
4089 ovs_mutex_unlock(&group
->stats_mutex
);
4094 group_destruct(struct ofgroup
*group_
)
4096 struct group_dpif
*group
= group_dpif_cast(group_
);
4097 ovs_mutex_destroy(&group
->stats_mutex
);
4101 group_modify(struct ofgroup
*group_
)
4103 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(group_
->ofproto
);
4105 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
4111 group_get_stats(const struct ofgroup
*group_
, struct ofputil_group_stats
*ogs
)
4113 struct group_dpif
*group
= group_dpif_cast(group_
);
4114 struct ofputil_bucket
*bucket
;
4115 const struct ovs_list
*buckets
;
4116 struct bucket_counter
*bucket_stats
;
4118 ovs_mutex_lock(&group
->stats_mutex
);
4119 ogs
->packet_count
= group
->packet_count
;
4120 ogs
->byte_count
= group
->byte_count
;
4122 group_dpif_get_buckets(group
, &buckets
);
4123 bucket_stats
= ogs
->bucket_stats
;
4124 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4125 bucket_stats
->packet_count
= bucket
->stats
.packet_count
;
4126 bucket_stats
->byte_count
= bucket
->stats
.byte_count
;
4129 ovs_mutex_unlock(&group
->stats_mutex
);
4134 /* If the group exists, this function increments the groups's reference count.
4136 * Make sure to call group_dpif_unref() after no longer needing to maintain
4137 * a reference to the group. */
4139 group_dpif_lookup(struct ofproto_dpif
*ofproto
, uint32_t group_id
,
4140 struct group_dpif
**group
)
4142 struct ofgroup
*ofgroup
;
4145 found
= ofproto_group_lookup(&ofproto
->up
, group_id
, &ofgroup
);
4146 *group
= found
? group_dpif_cast(ofgroup
) : NULL
;
4152 group_dpif_get_buckets(const struct group_dpif
*group
,
4153 const struct ovs_list
**buckets
)
4155 *buckets
= &group
->up
.buckets
;
4158 enum ofp11_group_type
4159 group_dpif_get_type(const struct group_dpif
*group
)
4161 return group
->up
.type
;
4165 group_dpif_get_selection_method(const struct group_dpif
*group
)
4167 return group
->up
.props
.selection_method
;
4170 /* Sends 'packet' out 'ofport'.
4171 * May modify 'packet'.
4172 * Returns 0 if successful, otherwise a positive errno value. */
4174 ofproto_dpif_send_packet(const struct ofport_dpif
*ofport
, struct dp_packet
*packet
)
4176 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
4179 error
= xlate_send_packet(ofport
, packet
);
4181 ovs_mutex_lock(&ofproto
->stats_mutex
);
4182 ofproto
->stats
.tx_packets
++;
4183 ofproto
->stats
.tx_bytes
+= dp_packet_size(packet
);
4184 ovs_mutex_unlock(&ofproto
->stats_mutex
);
4189 group_dpif_get_selection_method_param(const struct group_dpif
*group
)
4191 return group
->up
.props
.selection_method_param
;
4194 const struct field_array
*
4195 group_dpif_get_fields(const struct group_dpif
*group
)
4197 return &group
->up
.props
.fields
;
4200 /* Return the version string of the datapath that backs up
4204 get_datapath_version(const struct ofproto
*ofproto_
)
4206 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4208 return ofproto
->backer
->dp_version_string
;
4212 set_frag_handling(struct ofproto
*ofproto_
,
4213 enum ofp_config_flags frag_handling
)
4215 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4216 if (frag_handling
!= OFPC_FRAG_REASM
) {
4217 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4225 packet_out(struct ofproto
*ofproto_
, struct dp_packet
*packet
,
4226 const struct flow
*flow
,
4227 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
4229 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4231 ofproto_dpif_execute_actions(ofproto
, flow
, NULL
, ofpacts
,
4232 ofpacts_len
, packet
);
4239 set_netflow(struct ofproto
*ofproto_
,
4240 const struct netflow_options
*netflow_options
)
4242 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4244 if (netflow_options
) {
4245 if (!ofproto
->netflow
) {
4246 ofproto
->netflow
= netflow_create();
4247 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4249 return netflow_set_options(ofproto
->netflow
, netflow_options
);
4250 } else if (ofproto
->netflow
) {
4251 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4252 netflow_unref(ofproto
->netflow
);
4253 ofproto
->netflow
= NULL
;
4260 get_netflow_ids(const struct ofproto
*ofproto_
,
4261 uint8_t *engine_type
, uint8_t *engine_id
)
4263 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4265 dpif_get_netflow_ids(ofproto
->backer
->dpif
, engine_type
, engine_id
);
4268 static struct ofproto_dpif
*
4269 ofproto_dpif_lookup(const char *name
)
4271 struct ofproto_dpif
*ofproto
;
4273 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_node
,
4274 hash_string(name
, 0), &all_ofproto_dpifs
) {
4275 if (!strcmp(ofproto
->up
.name
, name
)) {
4283 ofproto_unixctl_fdb_flush(struct unixctl_conn
*conn
, int argc
,
4284 const char *argv
[], void *aux OVS_UNUSED
)
4286 struct ofproto_dpif
*ofproto
;
4289 ofproto
= ofproto_dpif_lookup(argv
[1]);
4291 unixctl_command_reply_error(conn
, "no such bridge");
4294 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4295 mac_learning_flush(ofproto
->ml
);
4296 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4298 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4299 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4300 mac_learning_flush(ofproto
->ml
);
4301 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4305 unixctl_command_reply(conn
, "table successfully flushed");
4309 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn
*conn
, int argc
,
4310 const char *argv
[], void *aux OVS_UNUSED
)
4312 struct ofproto_dpif
*ofproto
;
4315 ofproto
= ofproto_dpif_lookup(argv
[1]);
4317 unixctl_command_reply_error(conn
, "no such bridge");
4321 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4322 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4325 mcast_snooping_mdb_flush(ofproto
->ms
);
4327 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4328 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4331 mcast_snooping_mdb_flush(ofproto
->ms
);
4335 unixctl_command_reply(conn
, "table successfully flushed");
4338 static struct ofport_dpif
*
4339 ofbundle_get_a_port(const struct ofbundle
*bundle
)
4341 return CONTAINER_OF(list_front(&bundle
->ports
), struct ofport_dpif
,
4346 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4347 const char *argv
[], void *aux OVS_UNUSED
)
4349 struct ds ds
= DS_EMPTY_INITIALIZER
;
4350 const struct ofproto_dpif
*ofproto
;
4351 const struct mac_entry
*e
;
4353 ofproto
= ofproto_dpif_lookup(argv
[1]);
4355 unixctl_command_reply_error(conn
, "no such bridge");
4359 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
4360 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
4361 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
4362 struct ofbundle
*bundle
= mac_entry_get_port(ofproto
->ml
, e
);
4363 char name
[OFP_MAX_PORT_NAME_LEN
];
4365 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4367 ds_put_format(&ds
, "%5s %4d "ETH_ADDR_FMT
" %3d\n",
4368 name
, e
->vlan
, ETH_ADDR_ARGS(e
->mac
),
4369 mac_entry_age(ofproto
->ml
, e
));
4371 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4372 unixctl_command_reply(conn
, ds_cstr(&ds
));
4377 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn
*conn
,
4378 int argc OVS_UNUSED
,
4380 void *aux OVS_UNUSED
)
4382 struct ds ds
= DS_EMPTY_INITIALIZER
;
4383 const struct ofproto_dpif
*ofproto
;
4384 const struct ofbundle
*bundle
;
4385 const struct mcast_group
*grp
;
4386 struct mcast_group_bundle
*b
;
4387 struct mcast_mrouter_bundle
*mrouter
;
4389 ofproto
= ofproto_dpif_lookup(argv
[1]);
4391 unixctl_command_reply_error(conn
, "no such bridge");
4395 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4396 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4400 ds_put_cstr(&ds
, " port VLAN GROUP Age\n");
4401 ovs_rwlock_rdlock(&ofproto
->ms
->rwlock
);
4402 LIST_FOR_EACH (grp
, group_node
, &ofproto
->ms
->group_lru
) {
4403 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
4404 char name
[OFP_MAX_PORT_NAME_LEN
];
4407 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4409 ds_put_format(&ds
, "%5s %4d "IP_FMT
" %3d\n",
4410 name
, grp
->vlan
, IP_ARGS(grp
->ip4
),
4411 mcast_bundle_age(ofproto
->ms
, b
));
4415 /* ports connected to multicast routers */
4416 LIST_FOR_EACH(mrouter
, mrouter_node
, &ofproto
->ms
->mrouter_lru
) {
4417 char name
[OFP_MAX_PORT_NAME_LEN
];
4419 bundle
= mrouter
->port
;
4420 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4422 ds_put_format(&ds
, "%5s %4d querier %3d\n",
4423 name
, mrouter
->vlan
,
4424 mcast_mrouter_age(ofproto
->ms
, mrouter
));
4426 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
4427 unixctl_command_reply(conn
, ds_cstr(&ds
));
4432 struct xlate_out xout
;
4433 struct xlate_in xin
;
4434 const struct flow
*key
;
4436 struct flow_wildcards wc
;
4441 trace_format_rule(struct ds
*result
, int level
, const struct rule_dpif
*rule
)
4443 const struct rule_actions
*actions
;
4446 ds_put_char_multiple(result
, '\t', level
);
4448 ds_put_cstr(result
, "No match\n");
4452 ovs_mutex_lock(&rule
->up
.mutex
);
4453 cookie
= rule
->up
.flow_cookie
;
4454 ovs_mutex_unlock(&rule
->up
.mutex
);
4456 ds_put_format(result
, "Rule: table=%"PRIu8
" cookie=%#"PRIx64
" ",
4457 rule
? rule
->up
.table_id
: 0, ntohll(cookie
));
4458 cls_rule_format(&rule
->up
.cr
, result
);
4459 ds_put_char(result
, '\n');
4461 actions
= rule_dpif_get_actions(rule
);
4463 ds_put_char_multiple(result
, '\t', level
);
4464 ds_put_cstr(result
, "OpenFlow actions=");
4465 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, result
);
4466 ds_put_char(result
, '\n');
4470 trace_format_flow(struct ds
*result
, int level
, const char *title
,
4471 struct trace_ctx
*trace
)
4473 ds_put_char_multiple(result
, '\t', level
);
4474 ds_put_format(result
, "%s: ", title
);
4475 /* Do not report unchanged flows for resubmits. */
4476 if ((level
> 0 && flow_equal(&trace
->xin
.flow
, &trace
->flow
))
4477 || (level
== 0 && flow_equal(&trace
->xin
.flow
, trace
->key
))) {
4478 ds_put_cstr(result
, "unchanged");
4480 flow_format(result
, &trace
->xin
.flow
);
4481 trace
->flow
= trace
->xin
.flow
;
4483 ds_put_char(result
, '\n');
4487 trace_format_regs(struct ds
*result
, int level
, const char *title
,
4488 struct trace_ctx
*trace
)
4492 ds_put_char_multiple(result
, '\t', level
);
4493 ds_put_format(result
, "%s:", title
);
4494 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
4495 ds_put_format(result
, " reg%"PRIuSIZE
"=0x%"PRIx32
, i
, trace
->flow
.regs
[i
]);
4497 ds_put_char(result
, '\n');
4501 trace_format_odp(struct ds
*result
, int level
, const char *title
,
4502 struct trace_ctx
*trace
)
4504 struct ofpbuf
*odp_actions
= trace
->xout
.odp_actions
;
4506 ds_put_char_multiple(result
, '\t', level
);
4507 ds_put_format(result
, "%s: ", title
);
4508 format_odp_actions(result
, odp_actions
->data
, odp_actions
->size
);
4509 ds_put_char(result
, '\n');
4513 trace_format_megaflow(struct ds
*result
, int level
, const char *title
,
4514 struct trace_ctx
*trace
)
4518 ds_put_char_multiple(result
, '\t', level
);
4519 ds_put_format(result
, "%s: ", title
);
4520 flow_wildcards_or(&trace
->wc
, &trace
->xout
.wc
, &trace
->wc
);
4521 match_init(&match
, trace
->key
, &trace
->wc
);
4522 match_format(&match
, result
, OFP_DEFAULT_PRIORITY
);
4523 ds_put_char(result
, '\n');
4526 static void trace_report(struct xlate_in
*xin
, const char *s
, int recurse
);
4529 trace_resubmit(struct xlate_in
*xin
, struct rule_dpif
*rule
, int recurse
)
4531 struct trace_ctx
*trace
= CONTAINER_OF(xin
, struct trace_ctx
, xin
);
4532 struct ds
*result
= trace
->result
;
4535 if (rule
== xin
->ofproto
->miss_rule
) {
4536 trace_report(xin
, "No match, flow generates \"packet in\"s.",
4538 } else if (rule
== xin
->ofproto
->no_packet_in_rule
) {
4539 trace_report(xin
, "No match, packets dropped because "
4540 "OFPPC_NO_PACKET_IN is set on in_port.", recurse
);
4541 } else if (rule
== xin
->ofproto
->drop_frags_rule
) {
4542 trace_report(xin
, "Packets dropped because they are IP "
4543 "fragments and the fragment handling mode is "
4544 "\"drop\".", recurse
);
4548 ds_put_char(result
, '\n');
4550 trace_format_flow(result
, recurse
, "Resubmitted flow", trace
);
4551 trace_format_regs(result
, recurse
, "Resubmitted regs", trace
);
4552 trace_format_odp(result
, recurse
, "Resubmitted odp", trace
);
4553 trace_format_megaflow(result
, recurse
, "Resubmitted megaflow", trace
);
4555 trace_format_rule(result
, recurse
, rule
);
4559 trace_report(struct xlate_in
*xin
, const char *s
, int recurse
)
4561 struct trace_ctx
*trace
= CONTAINER_OF(xin
, struct trace_ctx
, xin
);
4562 struct ds
*result
= trace
->result
;
4564 ds_put_char_multiple(result
, '\t', recurse
);
4565 ds_put_cstr(result
, s
);
4566 ds_put_char(result
, '\n');
4569 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4570 * forms are supported:
4572 * - [dpname] odp_flow [-generate | packet]
4573 * - bridge br_flow [-generate | packet]
4575 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4576 * returns a nonnull malloced error message. */
4577 static char * OVS_WARN_UNUSED_RESULT
4578 parse_flow_and_packet(int argc
, const char *argv
[],
4579 struct ofproto_dpif
**ofprotop
, struct flow
*flow
,
4580 struct dp_packet
**packetp
)
4582 const struct dpif_backer
*backer
= NULL
;
4583 const char *error
= NULL
;
4585 struct simap port_names
= SIMAP_INITIALIZER(&port_names
);
4586 struct dp_packet
*packet
;
4587 struct ofpbuf odp_key
;
4588 struct ofpbuf odp_mask
;
4590 ofpbuf_init(&odp_key
, 0);
4591 ofpbuf_init(&odp_mask
, 0);
4593 /* Handle "-generate" or a hex string as the last argument. */
4594 if (!strcmp(argv
[argc
- 1], "-generate")) {
4595 packet
= dp_packet_new(0);
4598 error
= eth_from_hex(argv
[argc
- 1], &packet
);
4601 } else if (argc
== 4) {
4602 /* The 3-argument form must end in "-generate' or a hex string. */
4608 /* odp_flow can have its in_port specified as a name instead of port no.
4609 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4610 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4611 * we need to create a simap of name to port no. */
4613 const char *dp_type
;
4614 if (!strncmp(argv
[1], "ovs-", 4)) {
4615 dp_type
= argv
[1] + 4;
4619 backer
= shash_find_data(&all_dpif_backers
, dp_type
);
4620 } else if (argc
== 2) {
4621 struct shash_node
*node
;
4622 if (shash_count(&all_dpif_backers
) == 1) {
4623 node
= shash_first(&all_dpif_backers
);
4624 backer
= node
->data
;
4627 error
= "Syntax error";
4630 if (backer
&& backer
->dpif
) {
4631 struct dpif_port dpif_port
;
4632 struct dpif_port_dump port_dump
;
4633 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, backer
->dpif
) {
4634 simap_put(&port_names
, dpif_port
.name
,
4635 odp_to_u32(dpif_port
.port_no
));
4639 /* Parse the flow and determine whether a datapath or
4640 * bridge is specified. If function odp_flow_key_from_string()
4641 * returns 0, the flow is a odp_flow. If function
4642 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4643 if (!odp_flow_from_string(argv
[argc
- 1], &port_names
,
4644 &odp_key
, &odp_mask
)) {
4646 error
= "Cannot find the datapath";
4650 if (odp_flow_key_to_flow(odp_key
.data
, odp_key
.size
, flow
) == ODP_FIT_ERROR
) {
4651 error
= "Failed to parse datapath flow key";
4655 *ofprotop
= xlate_lookup_ofproto(backer
, flow
,
4656 &flow
->in_port
.ofp_port
);
4657 if (*ofprotop
== NULL
) {
4658 error
= "Invalid datapath flow";
4662 vsp_adjust_flow(*ofprotop
, flow
, NULL
);
4665 char *err
= parse_ofp_exact_flow(flow
, NULL
, argv
[argc
- 1], NULL
);
4668 m_err
= xasprintf("Bad openflow flow syntax: %s", err
);
4673 error
= "Must specify bridge name";
4677 *ofprotop
= ofproto_dpif_lookup(argv
[1]);
4679 error
= "Unknown bridge name";
4685 /* Generate a packet, if requested. */
4687 if (!dp_packet_size(packet
)) {
4688 flow_compose(packet
, flow
);
4690 /* Use the metadata from the flow and the packet argument
4691 * to reconstruct the flow. */
4692 pkt_metadata_from_flow(&packet
->md
, flow
);
4693 flow_extract(packet
, flow
);
4698 if (error
&& !m_err
) {
4699 m_err
= xstrdup(error
);
4702 dp_packet_delete(packet
);
4706 ofpbuf_uninit(&odp_key
);
4707 ofpbuf_uninit(&odp_mask
);
4708 simap_destroy(&port_names
);
4713 ofproto_unixctl_trace(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
4714 void *aux OVS_UNUSED
)
4716 struct ofproto_dpif
*ofproto
;
4717 struct dp_packet
*packet
;
4721 error
= parse_flow_and_packet(argc
, argv
, &ofproto
, &flow
, &packet
);
4726 ofproto_trace(ofproto
, &flow
, packet
, NULL
, 0, &result
);
4727 unixctl_command_reply(conn
, ds_cstr(&result
));
4728 ds_destroy(&result
);
4729 dp_packet_delete(packet
);
4731 unixctl_command_reply_error(conn
, error
);
4737 ofproto_unixctl_trace_actions(struct unixctl_conn
*conn
, int argc
,
4738 const char *argv
[], void *aux OVS_UNUSED
)
4740 enum ofputil_protocol usable_protocols
;
4741 struct ofproto_dpif
*ofproto
;
4742 bool enforce_consistency
;
4743 struct ofpbuf ofpacts
;
4744 struct dp_packet
*packet
;
4749 /* Three kinds of error return values! */
4755 ofpbuf_init(&ofpacts
, 0);
4757 /* Parse actions. */
4758 error
= ofpacts_parse_actions(argv
[--argc
], &ofpacts
, &usable_protocols
);
4760 unixctl_command_reply_error(conn
, error
);
4765 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4766 * consistency between the flow and the actions. With -consistent, we
4767 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4768 if (!strcmp(argv
[1], "-consistent")) {
4769 enforce_consistency
= true;
4773 enforce_consistency
= false;
4776 error
= parse_flow_and_packet(argc
, argv
, &ofproto
, &flow
, &packet
);
4778 unixctl_command_reply_error(conn
, error
);
4783 /* Do the same checks as handle_packet_out() in ofproto.c.
4785 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4786 * strictly correct because these actions aren't in any table, but it's OK
4787 * because it 'table_id' is used only to check goto_table instructions, but
4788 * packet-outs take a list of actions and therefore it can't include
4791 * We skip the "meter" check here because meter is an instruction, not an
4792 * action, and thus cannot appear in ofpacts. */
4793 in_port
= ofp_to_u16(flow
.in_port
.ofp_port
);
4794 if (in_port
>= ofproto
->up
.max_ports
&& in_port
< ofp_to_u16(OFPP_MAX
)) {
4795 unixctl_command_reply_error(conn
, "invalid in_port");
4798 if (enforce_consistency
) {
4799 retval
= ofpacts_check_consistency(ofpacts
.data
, ofpacts
.size
,
4800 &flow
, u16_to_ofp(ofproto
->up
.max_ports
),
4801 0, 0, usable_protocols
);
4803 retval
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &flow
,
4804 u16_to_ofp(ofproto
->up
.max_ports
), 0, 0,
4810 ds_put_format(&result
, "Bad actions: %s", ofperr_to_string(retval
));
4811 unixctl_command_reply_error(conn
, ds_cstr(&result
));
4815 ofproto_trace(ofproto
, &flow
, packet
,
4816 ofpacts
.data
, ofpacts
.size
, &result
);
4817 unixctl_command_reply(conn
, ds_cstr(&result
));
4820 ds_destroy(&result
);
4821 dp_packet_delete(packet
);
4822 ofpbuf_uninit(&ofpacts
);
4825 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4826 * description of the results to 'ds'.
4828 * The trace follows a packet with the specified 'flow' through the flow
4829 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4830 * side effects (if it is nonnull then its flow must be 'flow').
4832 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4833 * trace, otherwise the actions are determined by a flow table lookup. */
4835 ofproto_trace(struct ofproto_dpif
*ofproto
, struct flow
*flow
,
4836 const struct dp_packet
*packet
,
4837 const struct ofpact ofpacts
[], size_t ofpacts_len
,
4840 struct trace_ctx trace
;
4842 ds_put_format(ds
, "Bridge: %s\n", ofproto
->up
.name
);
4843 ds_put_cstr(ds
, "Flow: ");
4844 flow_format(ds
, flow
);
4845 ds_put_char(ds
, '\n');
4847 flow_wildcards_init_catchall(&trace
.wc
);
4850 trace
.key
= flow
; /* Original flow key, used for megaflow. */
4851 trace
.flow
= *flow
; /* May be modified by actions. */
4852 xlate_in_init(&trace
.xin
, ofproto
, flow
, flow
->in_port
.ofp_port
, NULL
,
4853 ntohs(flow
->tcp_flags
), packet
);
4854 trace
.xin
.ofpacts
= ofpacts
;
4855 trace
.xin
.ofpacts_len
= ofpacts_len
;
4856 trace
.xin
.resubmit_hook
= trace_resubmit
;
4857 trace
.xin
.report_hook
= trace_report
;
4859 xlate_actions(&trace
.xin
, &trace
.xout
);
4861 ds_put_char(ds
, '\n');
4862 trace_format_flow(ds
, 0, "Final flow", &trace
);
4863 trace_format_megaflow(ds
, 0, "Megaflow", &trace
);
4865 ds_put_cstr(ds
, "Datapath actions: ");
4866 format_odp_actions(ds
, trace
.xout
.odp_actions
->data
,
4867 trace
.xout
.odp_actions
->size
);
4869 if (trace
.xout
.slow
) {
4870 enum slow_path_reason slow
;
4872 ds_put_cstr(ds
, "\nThis flow is handled by the userspace "
4873 "slow path because it:");
4875 slow
= trace
.xout
.slow
;
4877 enum slow_path_reason bit
= rightmost_1bit(slow
);
4879 ds_put_format(ds
, "\n\t- %s.",
4880 slow_path_reason_to_explanation(bit
));
4886 xlate_out_uninit(&trace
.xout
);
4889 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4890 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4891 * to destroy 'ofproto_shash' and free the returned value. */
4892 static const struct shash_node
**
4893 get_ofprotos(struct shash
*ofproto_shash
)
4895 const struct ofproto_dpif
*ofproto
;
4897 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4898 char *name
= xasprintf("%s@%s", ofproto
->up
.type
, ofproto
->up
.name
);
4899 shash_add_nocopy(ofproto_shash
, name
, ofproto
);
4902 return shash_sort(ofproto_shash
);
4906 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4907 const char *argv
[] OVS_UNUSED
,
4908 void *aux OVS_UNUSED
)
4910 struct ds ds
= DS_EMPTY_INITIALIZER
;
4911 struct shash ofproto_shash
;
4912 const struct shash_node
**sorted_ofprotos
;
4915 shash_init(&ofproto_shash
);
4916 sorted_ofprotos
= get_ofprotos(&ofproto_shash
);
4917 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
4918 const struct shash_node
*node
= sorted_ofprotos
[i
];
4919 ds_put_format(&ds
, "%s\n", node
->name
);
4922 shash_destroy(&ofproto_shash
);
4923 free(sorted_ofprotos
);
4925 unixctl_command_reply(conn
, ds_cstr(&ds
));
4930 dpif_show_backer(const struct dpif_backer
*backer
, struct ds
*ds
)
4932 const struct shash_node
**ofprotos
;
4933 struct dpif_dp_stats dp_stats
;
4934 struct shash ofproto_shash
;
4937 dpif_get_dp_stats(backer
->dpif
, &dp_stats
);
4939 ds_put_format(ds
, "%s: hit:%"PRIu64
" missed:%"PRIu64
"\n",
4940 dpif_name(backer
->dpif
), dp_stats
.n_hit
, dp_stats
.n_missed
);
4942 shash_init(&ofproto_shash
);
4943 ofprotos
= get_ofprotos(&ofproto_shash
);
4944 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
4945 struct ofproto_dpif
*ofproto
= ofprotos
[i
]->data
;
4946 const struct shash_node
**ports
;
4949 if (ofproto
->backer
!= backer
) {
4953 ds_put_format(ds
, "\t%s:\n", ofproto
->up
.name
);
4955 ports
= shash_sort(&ofproto
->up
.port_by_name
);
4956 for (j
= 0; j
< shash_count(&ofproto
->up
.port_by_name
); j
++) {
4957 const struct shash_node
*node
= ports
[j
];
4958 struct ofport
*ofport
= node
->data
;
4960 odp_port_t odp_port
;
4962 ds_put_format(ds
, "\t\t%s %u/", netdev_get_name(ofport
->netdev
),
4965 odp_port
= ofp_port_to_odp_port(ofproto
, ofport
->ofp_port
);
4966 if (odp_port
!= ODPP_NONE
) {
4967 ds_put_format(ds
, "%"PRIu32
":", odp_port
);
4969 ds_put_cstr(ds
, "none:");
4972 ds_put_format(ds
, " (%s", netdev_get_type(ofport
->netdev
));
4975 if (!netdev_get_config(ofport
->netdev
, &config
)) {
4976 const struct smap_node
**nodes
;
4979 nodes
= smap_sort(&config
);
4980 for (i
= 0; i
< smap_count(&config
); i
++) {
4981 const struct smap_node
*node
= nodes
[i
];
4982 ds_put_format(ds
, "%c %s=%s", i
? ',' : ':',
4983 node
->key
, node
->value
);
4987 smap_destroy(&config
);
4989 ds_put_char(ds
, ')');
4990 ds_put_char(ds
, '\n');
4994 shash_destroy(&ofproto_shash
);
4999 ofproto_unixctl_dpif_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5000 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
5002 struct ds ds
= DS_EMPTY_INITIALIZER
;
5003 const struct shash_node
**backers
;
5006 backers
= shash_sort(&all_dpif_backers
);
5007 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5008 dpif_show_backer(backers
[i
]->data
, &ds
);
5012 unixctl_command_reply(conn
, ds_cstr(&ds
));
5017 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn
*conn
,
5018 int argc OVS_UNUSED
, const char *argv
[],
5019 void *aux OVS_UNUSED
)
5021 const struct ofproto_dpif
*ofproto
;
5023 struct ds ds
= DS_EMPTY_INITIALIZER
;
5024 bool verbosity
= false;
5026 struct dpif_port dpif_port
;
5027 struct dpif_port_dump port_dump
;
5028 struct hmap portno_names
;
5030 struct dpif_flow_dump
*flow_dump
;
5031 struct dpif_flow_dump_thread
*flow_dump_thread
;
5035 ofproto
= ofproto_dpif_lookup(argv
[argc
- 1]);
5037 unixctl_command_reply_error(conn
, "no such bridge");
5041 if (argc
> 2 && !strcmp(argv
[1], "-m")) {
5045 hmap_init(&portno_names
);
5046 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, ofproto
->backer
->dpif
) {
5047 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
5051 flow_dump
= dpif_flow_dump_create(ofproto
->backer
->dpif
, false);
5052 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
5053 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
5056 if (odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
) == ODP_FIT_ERROR
5057 || xlate_lookup_ofproto(ofproto
->backer
, &flow
, NULL
) != ofproto
) {
5062 odp_format_ufid(&f
.ufid
, &ds
);
5063 ds_put_cstr(&ds
, " ");
5065 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
5066 &portno_names
, &ds
, verbosity
);
5067 ds_put_cstr(&ds
, ", ");
5068 dpif_flow_stats_format(&f
.stats
, &ds
);
5069 ds_put_cstr(&ds
, ", actions:");
5070 format_odp_actions(&ds
, f
.actions
, f
.actions_len
);
5071 ds_put_char(&ds
, '\n');
5073 dpif_flow_dump_thread_destroy(flow_dump_thread
);
5074 error
= dpif_flow_dump_destroy(flow_dump
);
5078 ds_put_format(&ds
, "dpif/dump_flows failed: %s", ovs_strerror(errno
));
5079 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
5081 unixctl_command_reply(conn
, ds_cstr(&ds
));
5083 odp_portno_names_destroy(&portno_names
);
5084 hmap_destroy(&portno_names
);
5089 ofproto_revalidate_all_backers(void)
5091 const struct shash_node
**backers
;
5094 backers
= shash_sort(&all_dpif_backers
);
5095 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5096 struct dpif_backer
*backer
= backers
[i
]->data
;
5097 backer
->need_revalidate
= REV_RECONFIGURE
;
5103 disable_tnl_push_pop(struct unixctl_conn
*conn OVS_UNUSED
, int argc OVS_UNUSED
,
5104 const char *argv
[], void *aux OVS_UNUSED
)
5106 if (!strcasecmp(argv
[1], "off")) {
5107 ofproto_use_tnl_push_pop
= false;
5108 unixctl_command_reply(conn
, "Tunnel push-pop off");
5109 ofproto_revalidate_all_backers();
5110 } else if (!strcasecmp(argv
[1], "on")) {
5111 ofproto_use_tnl_push_pop
= true;
5112 unixctl_command_reply(conn
, "Tunnel push-pop on");
5113 ofproto_revalidate_all_backers();
5118 ofproto_unixctl_init(void)
5120 static bool registered
;
5126 unixctl_command_register(
5128 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5129 1, 3, ofproto_unixctl_trace
, NULL
);
5130 unixctl_command_register(
5131 "ofproto/trace-packet-out",
5132 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5133 2, 6, ofproto_unixctl_trace_actions
, NULL
);
5134 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5135 ofproto_unixctl_fdb_flush
, NULL
);
5136 unixctl_command_register("fdb/show", "bridge", 1, 1,
5137 ofproto_unixctl_fdb_show
, NULL
);
5138 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5139 ofproto_unixctl_mcast_snooping_flush
, NULL
);
5140 unixctl_command_register("mdb/show", "bridge", 1, 1,
5141 ofproto_unixctl_mcast_snooping_show
, NULL
);
5142 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5143 ofproto_unixctl_dpif_dump_dps
, NULL
);
5144 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show
,
5146 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5147 ofproto_unixctl_dpif_dump_flows
, NULL
);
5149 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5150 disable_tnl_push_pop
, NULL
);
5153 /* Returns true if 'table' is the table used for internal rules,
5154 * false otherwise. */
5156 table_is_internal(uint8_t table_id
)
5158 return table_id
== TBL_INTERNAL
;
5161 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5163 * This is deprecated. It is only for compatibility with broken device drivers
5164 * in old versions of Linux that do not properly support VLANs when VLAN
5165 * devices are not used. When broken device drivers are no longer in
5166 * widespread use, we will delete these interfaces. */
5169 set_realdev(struct ofport
*ofport_
, ofp_port_t realdev_ofp_port
, int vid
)
5171 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
5172 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
5174 if (realdev_ofp_port
== ofport
->realdev_ofp_port
5175 && vid
== ofport
->vlandev_vid
) {
5179 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5181 if (ofport
->realdev_ofp_port
) {
5184 if (realdev_ofp_port
&& ofport
->bundle
) {
5185 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5186 * themselves be part of a bundle. */
5187 bundle_set(ofport_
->ofproto
, ofport
->bundle
, NULL
);
5190 ofport
->realdev_ofp_port
= realdev_ofp_port
;
5191 ofport
->vlandev_vid
= vid
;
5193 if (realdev_ofp_port
) {
5194 vsp_add(ofport
, realdev_ofp_port
, vid
);
5201 hash_realdev_vid(ofp_port_t realdev_ofp_port
, int vid
)
5203 return hash_2words(ofp_to_u16(realdev_ofp_port
), vid
);
5207 ofproto_has_vlan_splinters(const struct ofproto_dpif
*ofproto
)
5208 OVS_EXCLUDED(ofproto
->vsp_mutex
)
5210 /* hmap_is_empty is thread safe. */
5211 return !hmap_is_empty(&ofproto
->realdev_vid_map
);
5216 vsp_realdev_to_vlandev__(const struct ofproto_dpif
*ofproto
,
5217 ofp_port_t realdev_ofp_port
, ovs_be16 vlan_tci
)
5218 OVS_REQUIRES(ofproto
->vsp_mutex
)
5220 if (!hmap_is_empty(&ofproto
->realdev_vid_map
)) {
5221 int vid
= vlan_tci_to_vid(vlan_tci
);
5222 const struct vlan_splinter
*vsp
;
5224 HMAP_FOR_EACH_WITH_HASH (vsp
, realdev_vid_node
,
5225 hash_realdev_vid(realdev_ofp_port
, vid
),
5226 &ofproto
->realdev_vid_map
) {
5227 if (vsp
->realdev_ofp_port
== realdev_ofp_port
5228 && vsp
->vid
== vid
) {
5229 return vsp
->vlandev_ofp_port
;
5233 return realdev_ofp_port
;
5236 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5237 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5238 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5239 * 'vlan_tci' 9, it would return the port number of eth0.9.
5241 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5242 * function just returns its 'realdev_ofp_port' argument. */
5244 vsp_realdev_to_vlandev(const struct ofproto_dpif
*ofproto
,
5245 ofp_port_t realdev_ofp_port
, ovs_be16 vlan_tci
)
5246 OVS_EXCLUDED(ofproto
->vsp_mutex
)
5250 /* hmap_is_empty is thread safe, see if we can return immediately. */
5251 if (hmap_is_empty(&ofproto
->realdev_vid_map
)) {
5252 return realdev_ofp_port
;
5254 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5255 ret
= vsp_realdev_to_vlandev__(ofproto
, realdev_ofp_port
, vlan_tci
);
5256 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5260 static struct vlan_splinter
*
5261 vlandev_find(const struct ofproto_dpif
*ofproto
, ofp_port_t vlandev_ofp_port
)
5263 struct vlan_splinter
*vsp
;
5265 HMAP_FOR_EACH_WITH_HASH (vsp
, vlandev_node
,
5266 hash_ofp_port(vlandev_ofp_port
),
5267 &ofproto
->vlandev_map
) {
5268 if (vsp
->vlandev_ofp_port
== vlandev_ofp_port
) {
5276 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5277 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5278 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5279 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5280 * eth0 and store 9 in '*vid'.
5282 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5283 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5286 vsp_vlandev_to_realdev(const struct ofproto_dpif
*ofproto
,
5287 ofp_port_t vlandev_ofp_port
, int *vid
)
5288 OVS_REQUIRES(ofproto
->vsp_mutex
)
5290 if (!hmap_is_empty(&ofproto
->vlandev_map
)) {
5291 const struct vlan_splinter
*vsp
;
5293 vsp
= vlandev_find(ofproto
, vlandev_ofp_port
);
5298 return vsp
->realdev_ofp_port
;
5304 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5305 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5306 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5307 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5308 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5309 * case unless VLAN splinters are enabled), returns false without making any
5312 vsp_adjust_flow(const struct ofproto_dpif
*ofproto
, struct flow
*flow
,
5313 struct dp_packet
*packet
)
5314 OVS_EXCLUDED(ofproto
->vsp_mutex
)
5319 /* hmap_is_empty is thread safe. */
5320 if (hmap_is_empty(&ofproto
->vlandev_map
)) {
5324 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5325 realdev
= vsp_vlandev_to_realdev(ofproto
, flow
->in_port
.ofp_port
, &vid
);
5326 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5331 /* Cause the flow to be processed as if it came in on the real device with
5332 * the VLAN device's VLAN ID. */
5333 flow
->in_port
.ofp_port
= realdev
;
5334 flow
->vlan_tci
= htons((vid
& VLAN_VID_MASK
) | VLAN_CFI
);
5337 /* Make the packet resemble the flow, so that it gets sent to an
5338 * OpenFlow controller properly, so that it looks correct for sFlow,
5339 * and so that flow_extract() will get the correct vlan_tci if it is
5340 * called on 'packet'. */
5341 eth_push_vlan(packet
, htons(ETH_TYPE_VLAN
), flow
->vlan_tci
);
5348 vsp_remove(struct ofport_dpif
*port
)
5350 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
5351 struct vlan_splinter
*vsp
;
5353 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5354 vsp
= vlandev_find(ofproto
, port
->up
.ofp_port
);
5356 hmap_remove(&ofproto
->vlandev_map
, &vsp
->vlandev_node
);
5357 hmap_remove(&ofproto
->realdev_vid_map
, &vsp
->realdev_vid_node
);
5360 port
->realdev_ofp_port
= 0;
5362 VLOG_ERR("missing vlan device record");
5364 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5368 vsp_add(struct ofport_dpif
*port
, ofp_port_t realdev_ofp_port
, int vid
)
5370 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
5372 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5373 if (!vsp_vlandev_to_realdev(ofproto
, port
->up
.ofp_port
, NULL
)
5374 && (vsp_realdev_to_vlandev__(ofproto
, realdev_ofp_port
, htons(vid
))
5375 == realdev_ofp_port
)) {
5376 struct vlan_splinter
*vsp
;
5378 vsp
= xmalloc(sizeof *vsp
);
5379 vsp
->realdev_ofp_port
= realdev_ofp_port
;
5380 vsp
->vlandev_ofp_port
= port
->up
.ofp_port
;
5383 port
->realdev_ofp_port
= realdev_ofp_port
;
5385 hmap_insert(&ofproto
->vlandev_map
, &vsp
->vlandev_node
,
5386 hash_ofp_port(port
->up
.ofp_port
));
5387 hmap_insert(&ofproto
->realdev_vid_map
, &vsp
->realdev_vid_node
,
5388 hash_realdev_vid(realdev_ofp_port
, vid
));
5390 VLOG_ERR("duplicate vlan device record");
5392 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5396 ofp_port_to_odp_port(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
5398 const struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
5399 return ofport
? ofport
->odp_port
: ODPP_NONE
;
5402 struct ofport_dpif
*
5403 odp_port_to_ofport(const struct dpif_backer
*backer
, odp_port_t odp_port
)
5405 struct ofport_dpif
*port
;
5407 ovs_rwlock_rdlock(&backer
->odp_to_ofport_lock
);
5408 HMAP_FOR_EACH_IN_BUCKET (port
, odp_port_node
, hash_odp_port(odp_port
),
5409 &backer
->odp_to_ofport_map
) {
5410 if (port
->odp_port
== odp_port
) {
5411 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5416 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5421 odp_port_to_ofp_port(const struct ofproto_dpif
*ofproto
, odp_port_t odp_port
)
5423 struct ofport_dpif
*port
;
5425 port
= odp_port_to_ofport(ofproto
->backer
, odp_port
);
5426 if (port
&& &ofproto
->up
== port
->up
.ofproto
) {
5427 return port
->up
.ofp_port
;
5434 ofproto_dpif_add_internal_flow(struct ofproto_dpif
*ofproto
,
5435 const struct match
*match
, int priority
,
5436 uint16_t idle_timeout
,
5437 const struct ofpbuf
*ofpacts
,
5438 struct rule
**rulep
)
5440 struct ofputil_flow_mod fm
;
5441 struct rule_dpif
*rule
;
5445 fm
.priority
= priority
;
5446 fm
.new_cookie
= htonll(0);
5447 fm
.cookie
= htonll(0);
5448 fm
.cookie_mask
= htonll(0);
5449 fm
.modify_cookie
= false;
5450 fm
.table_id
= TBL_INTERNAL
;
5451 fm
.command
= OFPFC_ADD
;
5452 fm
.idle_timeout
= idle_timeout
;
5453 fm
.hard_timeout
= 0;
5457 fm
.flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
;
5458 fm
.ofpacts
= ofpacts
->data
;
5459 fm
.ofpacts_len
= ofpacts
->size
;
5461 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
5463 VLOG_ERR_RL(&rl
, "failed to add internal flow (%s)",
5464 ofperr_to_string(error
));
5469 rule
= rule_dpif_lookup_in_table(ofproto
,
5470 ofproto_dpif_get_tables_version(ofproto
),
5471 TBL_INTERNAL
, &fm
.match
.flow
,
5472 &fm
.match
.wc
, false);
5482 ofproto_dpif_delete_internal_flow(struct ofproto_dpif
*ofproto
,
5483 struct match
*match
, int priority
)
5485 struct ofputil_flow_mod fm
;
5489 fm
.priority
= priority
;
5490 fm
.new_cookie
= htonll(0);
5491 fm
.cookie
= htonll(0);
5492 fm
.cookie_mask
= htonll(0);
5493 fm
.modify_cookie
= false;
5494 fm
.table_id
= TBL_INTERNAL
;
5495 fm
.flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
;
5496 fm
.command
= OFPFC_DELETE_STRICT
;
5498 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
5500 VLOG_ERR_RL(&rl
, "failed to delete internal flow (%s)",
5501 ofperr_to_string(error
));
5508 const struct ofproto_class ofproto_dpif_class
= {
5522 NULL
, /* get_memory_usage. */
5523 type_get_memory_usage
,
5542 port_is_lacp_current
,
5543 port_get_lacp_stats
,
5544 NULL
, /* rule_choose_table */
5553 NULL
, /* rule_premodify_actions */
5554 rule_modify_actions
,
5570 aa_vlan_get_queue_size
,
5577 get_stp_port_status
,
5582 get_rstp_port_status
,
5589 is_mirror_output_bundle
,
5590 forward_bpdu_changed
,
5591 set_mac_table_config
,
5593 set_mcast_snooping_port
,
5595 NULL
, /* meter_get_features */
5596 NULL
, /* meter_set */
5597 NULL
, /* meter_get */
5598 NULL
, /* meter_del */
5599 group_alloc
, /* group_alloc */
5600 group_construct
, /* group_construct */
5601 group_destruct
, /* group_destruct */
5602 group_dealloc
, /* group_dealloc */
5603 group_modify
, /* group_modify */
5604 group_get_stats
, /* group_get_stats */
5605 get_datapath_version
, /* get_datapath_version */