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 /* In non-NULL, will point to a new rule (for which a reference is held) to
93 * which all the stats updates should be forwarded. This exists only
94 * transitionally when flows are replaced.
96 * Protected by stats_mutex. If both 'rule->stats_mutex' and
97 * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
99 struct rule_dpif
*new_rule OVS_GUARDED
;
101 /* If non-zero then the recirculation id that has
102 * been allocated for use with this rule.
103 * The recirculation id and associated internal flow should
104 * be freed when the rule is freed */
108 /* RULE_CAST() depends on this. */
109 BUILD_ASSERT_DECL(offsetof(struct rule_dpif
, up
) == 0);
111 static void rule_get_stats(struct rule
*, uint64_t *packets
, uint64_t *bytes
,
112 long long int *used
);
113 static struct rule_dpif
*rule_dpif_cast(const struct rule
*);
114 static void rule_expire(struct rule_dpif
*);
121 * - Do include packets and bytes from datapath flows which have not
122 * recently been processed by a revalidator. */
123 struct ovs_mutex stats_mutex
;
124 uint64_t packet_count OVS_GUARDED
; /* Number of packets received. */
125 uint64_t byte_count OVS_GUARDED
; /* Number of bytes received. */
129 struct hmap_node hmap_node
; /* In struct ofproto's "bundles" hmap. */
130 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
131 void *aux
; /* Key supplied by ofproto's client. */
132 char *name
; /* Identifier for log messages. */
135 struct ovs_list ports
; /* Contains "struct ofport"s. */
136 enum port_vlan_mode vlan_mode
; /* VLAN mode */
137 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
138 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
139 * NULL if all VLANs are trunked. */
140 struct lacp
*lacp
; /* LACP if LACP is enabled, otherwise NULL. */
141 struct bond
*bond
; /* Nonnull iff more than one port. */
142 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
145 bool floodable
; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
148 static void bundle_remove(struct ofport
*);
149 static void bundle_update(struct ofbundle
*);
150 static void bundle_destroy(struct ofbundle
*);
151 static void bundle_del_port(struct ofport_dpif
*);
152 static void bundle_run(struct ofbundle
*);
153 static void bundle_wait(struct ofbundle
*);
154 static void bundle_flush_macs(struct ofbundle
*, bool);
155 static void bundle_move(struct ofbundle
*, struct ofbundle
*);
157 static void stp_run(struct ofproto_dpif
*ofproto
);
158 static void stp_wait(struct ofproto_dpif
*ofproto
);
159 static int set_stp_port(struct ofport
*,
160 const struct ofproto_port_stp_settings
*);
162 static void rstp_run(struct ofproto_dpif
*ofproto
);
163 static void set_rstp_port(struct ofport
*,
164 const struct ofproto_port_rstp_settings
*);
167 struct hmap_node odp_port_node
; /* In dpif_backer's "odp_to_ofport_map". */
171 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
172 struct ovs_list bundle_node
;/* In struct ofbundle's "ports" list. */
173 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
174 struct bfd
*bfd
; /* BFD, if any. */
175 struct lldp
*lldp
; /* lldp, if any. */
176 bool may_enable
; /* May be enabled in bonds. */
177 bool is_tunnel
; /* This port is a tunnel. */
178 bool is_layer3
; /* This is a layer 3 port. */
179 long long int carrier_seq
; /* Carrier status changes. */
180 struct ofport_dpif
*peer
; /* Peer if patch port. */
183 struct stp_port
*stp_port
; /* Spanning Tree Protocol, if any. */
184 enum stp_state stp_state
; /* Always STP_DISABLED if STP not in use. */
185 long long int stp_state_entered
;
187 /* Rapid Spanning Tree. */
188 struct rstp_port
*rstp_port
; /* Rapid Spanning Tree Protocol, if any. */
189 enum rstp_state rstp_state
; /* Always RSTP_DISABLED if RSTP not in use. */
191 /* Queue to DSCP mapping. */
192 struct ofproto_port_queue
*qdscp
;
195 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
197 * This is deprecated. It is only for compatibility with broken device
198 * drivers in old versions of Linux that do not properly support VLANs when
199 * VLAN devices are not used. When broken device drivers are no longer in
200 * widespread use, we will delete these interfaces. */
201 ofp_port_t realdev_ofp_port
;
205 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
207 * This is deprecated. It is only for compatibility with broken device drivers
208 * in old versions of Linux that do not properly support VLANs when VLAN
209 * devices are not used. When broken device drivers are no longer in
210 * widespread use, we will delete these interfaces. */
211 struct vlan_splinter
{
212 struct hmap_node realdev_vid_node
;
213 struct hmap_node vlandev_node
;
214 ofp_port_t realdev_ofp_port
;
215 ofp_port_t vlandev_ofp_port
;
219 static void vsp_remove(struct ofport_dpif
*);
220 static void vsp_add(struct ofport_dpif
*, ofp_port_t realdev_ofp_port
, int vid
);
222 static odp_port_t
ofp_port_to_odp_port(const struct ofproto_dpif
*,
225 static ofp_port_t
odp_port_to_ofp_port(const struct ofproto_dpif
*,
228 static struct ofport_dpif
*
229 ofport_dpif_cast(const struct ofport
*ofport
)
231 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
234 static void port_run(struct ofport_dpif
*);
235 static int set_bfd(struct ofport
*, const struct smap
*);
236 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
237 static int set_lldp(struct ofport
*ofport_
, const struct smap
*cfg
);
238 static void ofport_update_peer(struct ofport_dpif
*);
240 /* Reasons that we might need to revalidate every datapath flow, and
241 * corresponding coverage counters.
243 * A value of 0 means that there is no need to revalidate.
245 * It would be nice to have some cleaner way to integrate with coverage
246 * counters, but with only a few reasons I guess this is good enough for
248 enum revalidate_reason
{
249 REV_RECONFIGURE
= 1, /* Switch configuration changed. */
250 REV_STP
, /* Spanning tree protocol port status change. */
251 REV_RSTP
, /* RSTP port status change. */
252 REV_BOND
, /* Bonding changed. */
253 REV_PORT_TOGGLED
, /* Port enabled or disabled by CFM, LACP, ...*/
254 REV_FLOW_TABLE
, /* Flow table changed. */
255 REV_MAC_LEARNING
, /* Mac learning changed. */
256 REV_MCAST_SNOOPING
, /* Multicast snooping changed. */
258 COVERAGE_DEFINE(rev_reconfigure
);
259 COVERAGE_DEFINE(rev_stp
);
260 COVERAGE_DEFINE(rev_rstp
);
261 COVERAGE_DEFINE(rev_bond
);
262 COVERAGE_DEFINE(rev_port_toggled
);
263 COVERAGE_DEFINE(rev_flow_table
);
264 COVERAGE_DEFINE(rev_mac_learning
);
265 COVERAGE_DEFINE(rev_mcast_snooping
);
267 /* All datapaths of a given type share a single dpif backer instance. */
274 struct ovs_rwlock odp_to_ofport_lock
;
275 struct hmap odp_to_ofport_map OVS_GUARDED
; /* Contains "struct ofport"s. */
277 struct simap tnl_backers
; /* Set of dpif ports backing tunnels. */
279 enum revalidate_reason need_revalidate
; /* Revalidate all flows. */
281 bool recv_set_enable
; /* Enables or disables receiving packets. */
283 /* Version string of the datapath stored in OVSDB. */
284 char *dp_version_string
;
286 /* Datapath feature support. */
287 struct dpif_backer_support support
;
288 struct atomic_count tnl_count
;
291 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
292 static struct shash all_dpif_backers
= SHASH_INITIALIZER(&all_dpif_backers
);
294 struct ofproto_dpif
{
295 struct hmap_node all_ofproto_dpifs_node
; /* In 'all_ofproto_dpifs'. */
297 struct dpif_backer
*backer
;
299 ATOMIC(cls_version_t
) tables_version
; /* For classifier lookups. */
301 uint64_t dump_seq
; /* Last read of udpif_dump_seq(). */
303 /* Special OpenFlow rules. */
304 struct rule_dpif
*miss_rule
; /* Sends flow table misses to controller. */
305 struct rule_dpif
*no_packet_in_rule
; /* Drops flow table misses. */
306 struct rule_dpif
*drop_frags_rule
; /* Used in OFPC_FRAG_DROP mode. */
309 struct netflow
*netflow
;
310 struct dpif_sflow
*sflow
;
311 struct dpif_ipfix
*ipfix
;
312 struct hmap bundles
; /* Contains "struct ofbundle"s. */
313 struct mac_learning
*ml
;
314 struct mcast_snooping
*ms
;
315 bool has_bonded_bundles
;
317 struct mbridge
*mbridge
;
319 struct ovs_mutex stats_mutex
;
320 struct netdev_stats stats OVS_GUARDED
; /* To account packets generated and
321 * consumed in userspace. */
325 long long int stp_last_tick
;
327 /* Rapid Spanning Tree. */
329 long long int rstp_last_tick
;
331 /* VLAN splinters. */
332 struct ovs_mutex vsp_mutex
;
333 struct hmap realdev_vid_map OVS_GUARDED
; /* (realdev,vid) -> vlandev. */
334 struct hmap vlandev_map OVS_GUARDED
; /* vlandev -> (realdev,vid). */
337 struct sset ports
; /* Set of standard port names. */
338 struct sset ghost_ports
; /* Ports with no datapath port. */
339 struct sset port_poll_set
; /* Queued names for port_poll() reply. */
340 int port_poll_errno
; /* Last errno for port_poll() reply. */
341 uint64_t change_seq
; /* Connectivity status changes. */
344 struct guarded_list pins
; /* Contains "struct ofputil_packet_in"s. */
345 struct seq
*pins_seq
; /* For notifying 'pins' reception. */
349 /* All existing ofproto_dpif instances, indexed by ->up.name. */
350 static struct hmap all_ofproto_dpifs
= HMAP_INITIALIZER(&all_ofproto_dpifs
);
352 static bool ofproto_use_tnl_push_pop
= true;
353 static void ofproto_unixctl_init(void);
355 static inline struct ofproto_dpif
*
356 ofproto_dpif_cast(const struct ofproto
*ofproto
)
358 ovs_assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
359 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
363 ofproto_dpif_get_enable_ufid(const struct dpif_backer
*backer
)
365 return backer
->support
.ufid
;
368 struct dpif_backer_support
*
369 ofproto_dpif_get_support(const struct ofproto_dpif
*ofproto
)
371 return &ofproto
->backer
->support
;
374 static void ofproto_trace(struct ofproto_dpif
*, struct flow
*,
375 const struct dp_packet
*packet
,
376 const struct ofpact
[], size_t ofpacts_len
,
379 /* Global variables. */
380 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
382 /* Initial mappings of port to bridge mappings. */
383 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
385 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
388 ofproto_dpif_flow_mod(struct ofproto_dpif
*ofproto
,
389 const struct ofputil_flow_mod
*fm
)
391 struct ofproto_flow_mod ofm
;
393 /* Multiple threads may do this for the same 'fm' at the same time.
394 * Allocate ofproto_flow_mod with execution context from stack.
396 * Note: This copy could be avoided by making ofproto_flow_mod more
397 * complex, but that may not be desireable, and a learn action is not that
398 * fast to begin with. */
400 ofproto_flow_mod(&ofproto
->up
, &ofm
);
403 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
404 * Takes ownership of 'pin' and pin->packet. */
406 ofproto_dpif_send_packet_in(struct ofproto_dpif
*ofproto
,
407 struct ofproto_packet_in
*pin
)
409 if (!guarded_list_push_back(&ofproto
->pins
, &pin
->list_node
, 1024)) {
410 COVERAGE_INC(packet_in_overflow
);
411 free(CONST_CAST(void *, pin
->up
.packet
));
415 /* Wakes up main thread for packet-in I/O. */
416 seq_change(ofproto
->pins_seq
);
419 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
420 * packet rather than to send the packet to the controller.
422 * This function returns false to indicate that a packet_in message
423 * for a "table-miss" should be sent to at least one controller.
424 * False otherwise. */
426 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif
*ofproto
)
428 return connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
);
431 /* Factory functions. */
434 init(const struct shash
*iface_hints
)
436 struct shash_node
*node
;
438 /* Make a local copy, since we don't own 'iface_hints' elements. */
439 SHASH_FOR_EACH(node
, iface_hints
) {
440 const struct iface_hint
*orig_hint
= node
->data
;
441 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
443 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
444 new_hint
->br_type
= xstrdup(orig_hint
->br_type
);
445 new_hint
->ofp_port
= orig_hint
->ofp_port
;
447 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
450 ofproto_unixctl_init();
455 enumerate_types(struct sset
*types
)
457 dp_enumerate_types(types
);
461 enumerate_names(const char *type
, struct sset
*names
)
463 struct ofproto_dpif
*ofproto
;
466 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
467 if (strcmp(type
, ofproto
->up
.type
)) {
470 sset_add(names
, ofproto
->up
.name
);
477 del(const char *type
, const char *name
)
482 error
= dpif_open(name
, type
, &dpif
);
484 error
= dpif_delete(dpif
);
491 port_open_type(const char *datapath_type
, const char *port_type
)
493 return dpif_port_open_type(datapath_type
, port_type
);
496 /* Type functions. */
498 static void process_dpif_port_changes(struct dpif_backer
*);
499 static void process_dpif_all_ports_changed(struct dpif_backer
*);
500 static void process_dpif_port_change(struct dpif_backer
*,
501 const char *devname
);
502 static void process_dpif_port_error(struct dpif_backer
*, int error
);
504 static struct ofproto_dpif
*
505 lookup_ofproto_dpif_by_port_name(const char *name
)
507 struct ofproto_dpif
*ofproto
;
509 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
510 if (sset_contains(&ofproto
->ports
, name
)) {
519 type_run(const char *type
)
521 struct dpif_backer
*backer
;
523 backer
= shash_find_data(&all_dpif_backers
, type
);
525 /* This is not necessarily a problem, since backers are only
526 * created on demand. */
531 if (dpif_run(backer
->dpif
)) {
532 backer
->need_revalidate
= REV_RECONFIGURE
;
535 udpif_run(backer
->udpif
);
537 /* If vswitchd started with other_config:flow_restore_wait set as "true",
538 * and the configuration has now changed to "false", enable receiving
539 * packets from the datapath. */
540 if (!backer
->recv_set_enable
&& !ofproto_get_flow_restore_wait()) {
543 backer
->recv_set_enable
= true;
545 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
547 VLOG_ERR("Failed to enable receiving packets in dpif.");
550 dpif_flow_flush(backer
->dpif
);
551 backer
->need_revalidate
= REV_RECONFIGURE
;
554 if (backer
->recv_set_enable
) {
555 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
558 dpif_poll_threads_set(backer
->dpif
, n_dpdk_rxqs
, pmd_cpu_mask
);
560 if (backer
->need_revalidate
) {
561 struct ofproto_dpif
*ofproto
;
562 struct simap_node
*node
;
563 struct simap tmp_backers
;
565 /* Handle tunnel garbage collection. */
566 simap_init(&tmp_backers
);
567 simap_swap(&backer
->tnl_backers
, &tmp_backers
);
569 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
570 struct ofport_dpif
*iter
;
572 if (backer
!= ofproto
->backer
) {
576 HMAP_FOR_EACH (iter
, up
.hmap_node
, &ofproto
->up
.ports
) {
577 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
580 if (!iter
->is_tunnel
) {
584 dp_port
= netdev_vport_get_dpif_port(iter
->up
.netdev
,
585 namebuf
, sizeof namebuf
);
586 node
= simap_find(&tmp_backers
, dp_port
);
588 simap_put(&backer
->tnl_backers
, dp_port
, node
->data
);
589 simap_delete(&tmp_backers
, node
);
590 node
= simap_find(&backer
->tnl_backers
, dp_port
);
592 node
= simap_find(&backer
->tnl_backers
, dp_port
);
594 odp_port_t odp_port
= ODPP_NONE
;
596 if (!dpif_port_add(backer
->dpif
, iter
->up
.netdev
,
598 simap_put(&backer
->tnl_backers
, dp_port
,
599 odp_to_u32(odp_port
));
600 node
= simap_find(&backer
->tnl_backers
, dp_port
);
605 iter
->odp_port
= node
? u32_to_odp(node
->data
) : ODPP_NONE
;
606 if (tnl_port_reconfigure(iter
, iter
->up
.netdev
,
608 ovs_native_tunneling_is_on(ofproto
), dp_port
)) {
609 backer
->need_revalidate
= REV_RECONFIGURE
;
614 SIMAP_FOR_EACH (node
, &tmp_backers
) {
615 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
));
617 simap_destroy(&tmp_backers
);
619 switch (backer
->need_revalidate
) {
620 case REV_RECONFIGURE
: COVERAGE_INC(rev_reconfigure
); break;
621 case REV_STP
: COVERAGE_INC(rev_stp
); break;
622 case REV_RSTP
: COVERAGE_INC(rev_rstp
); break;
623 case REV_BOND
: COVERAGE_INC(rev_bond
); break;
624 case REV_PORT_TOGGLED
: COVERAGE_INC(rev_port_toggled
); break;
625 case REV_FLOW_TABLE
: COVERAGE_INC(rev_flow_table
); break;
626 case REV_MAC_LEARNING
: COVERAGE_INC(rev_mac_learning
); break;
627 case REV_MCAST_SNOOPING
: COVERAGE_INC(rev_mcast_snooping
); break;
629 backer
->need_revalidate
= 0;
631 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
632 struct ofport_dpif
*ofport
;
633 struct ofbundle
*bundle
;
635 if (ofproto
->backer
!= backer
) {
640 xlate_ofproto_set(ofproto
, ofproto
->up
.name
,
641 ofproto
->backer
->dpif
, ofproto
->ml
,
642 ofproto
->stp
, ofproto
->rstp
, ofproto
->ms
,
643 ofproto
->mbridge
, ofproto
->sflow
, ofproto
->ipfix
,
645 ofproto
->up
.forward_bpdu
,
646 connmgr_has_in_band(ofproto
->up
.connmgr
),
647 &ofproto
->backer
->support
);
649 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
650 xlate_bundle_set(ofproto
, bundle
, bundle
->name
,
651 bundle
->vlan_mode
, bundle
->vlan
,
652 bundle
->trunks
, bundle
->use_priority_tags
,
653 bundle
->bond
, bundle
->lacp
,
657 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
658 int stp_port
= ofport
->stp_port
659 ? stp_port_no(ofport
->stp_port
)
661 xlate_ofport_set(ofproto
, ofport
->bundle
, ofport
,
662 ofport
->up
.ofp_port
, ofport
->odp_port
,
663 ofport
->up
.netdev
, ofport
->cfm
, ofport
->bfd
,
664 ofport
->lldp
, ofport
->peer
, stp_port
,
665 ofport
->rstp_port
, ofport
->qdscp
,
666 ofport
->n_qdscp
, ofport
->up
.pp
.config
,
667 ofport
->up
.pp
.state
, ofport
->is_tunnel
,
673 udpif_revalidate(backer
->udpif
);
676 process_dpif_port_changes(backer
);
681 /* Check for and handle port changes in 'backer''s dpif. */
683 process_dpif_port_changes(struct dpif_backer
*backer
)
689 error
= dpif_port_poll(backer
->dpif
, &devname
);
695 process_dpif_all_ports_changed(backer
);
699 process_dpif_port_change(backer
, devname
);
704 process_dpif_port_error(backer
, error
);
711 process_dpif_all_ports_changed(struct dpif_backer
*backer
)
713 struct ofproto_dpif
*ofproto
;
714 struct dpif_port dpif_port
;
715 struct dpif_port_dump dump
;
716 struct sset devnames
;
719 sset_init(&devnames
);
720 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
721 if (ofproto
->backer
== backer
) {
722 struct ofport
*ofport
;
724 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
725 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
729 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, backer
->dpif
) {
730 sset_add(&devnames
, dpif_port
.name
);
733 SSET_FOR_EACH (devname
, &devnames
) {
734 process_dpif_port_change(backer
, devname
);
736 sset_destroy(&devnames
);
740 process_dpif_port_change(struct dpif_backer
*backer
, const char *devname
)
742 struct ofproto_dpif
*ofproto
;
743 struct dpif_port port
;
745 /* Don't report on the datapath's device. */
746 if (!strcmp(devname
, dpif_base_name(backer
->dpif
))) {
750 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
,
751 &all_ofproto_dpifs
) {
752 if (simap_contains(&ofproto
->backer
->tnl_backers
, devname
)) {
757 ofproto
= lookup_ofproto_dpif_by_port_name(devname
);
758 if (dpif_port_query_by_name(backer
->dpif
, devname
, &port
)) {
759 /* The port was removed. If we know the datapath,
760 * report it through poll_set(). If we don't, it may be
761 * notifying us of a removal we initiated, so ignore it.
762 * If there's a pending ENOBUFS, let it stand, since
763 * everything will be reevaluated. */
764 if (ofproto
&& ofproto
->port_poll_errno
!= ENOBUFS
) {
765 sset_add(&ofproto
->port_poll_set
, devname
);
766 ofproto
->port_poll_errno
= 0;
768 } else if (!ofproto
) {
769 /* The port was added, but we don't know with which
770 * ofproto we should associate it. Delete it. */
771 dpif_port_del(backer
->dpif
, port
.port_no
);
773 struct ofport_dpif
*ofport
;
775 ofport
= ofport_dpif_cast(shash_find_data(
776 &ofproto
->up
.port_by_name
, devname
));
778 && ofport
->odp_port
!= port
.port_no
779 && !odp_port_to_ofport(backer
, port
.port_no
))
781 /* 'ofport''s datapath port number has changed from
782 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
783 * structures to match. */
784 ovs_rwlock_wrlock(&backer
->odp_to_ofport_lock
);
785 hmap_remove(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
);
786 ofport
->odp_port
= port
.port_no
;
787 hmap_insert(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
,
788 hash_odp_port(port
.port_no
));
789 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
790 backer
->need_revalidate
= REV_RECONFIGURE
;
793 dpif_port_destroy(&port
);
796 /* Propagate 'error' to all ofprotos based on 'backer'. */
798 process_dpif_port_error(struct dpif_backer
*backer
, int error
)
800 struct ofproto_dpif
*ofproto
;
802 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
803 if (ofproto
->backer
== backer
) {
804 sset_clear(&ofproto
->port_poll_set
);
805 ofproto
->port_poll_errno
= error
;
811 type_wait(const char *type
)
813 struct dpif_backer
*backer
;
815 backer
= shash_find_data(&all_dpif_backers
, type
);
817 /* This is not necessarily a problem, since backers are only
818 * created on demand. */
822 dpif_wait(backer
->dpif
);
825 /* Basic life-cycle. */
827 static int add_internal_flows(struct ofproto_dpif
*);
829 static struct ofproto
*
832 struct ofproto_dpif
*ofproto
= xzalloc(sizeof *ofproto
);
837 dealloc(struct ofproto
*ofproto_
)
839 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
844 close_dpif_backer(struct dpif_backer
*backer
)
846 ovs_assert(backer
->refcount
> 0);
848 if (--backer
->refcount
) {
852 udpif_destroy(backer
->udpif
);
854 simap_destroy(&backer
->tnl_backers
);
855 ovs_rwlock_destroy(&backer
->odp_to_ofport_lock
);
856 hmap_destroy(&backer
->odp_to_ofport_map
);
857 shash_find_and_delete(&all_dpif_backers
, backer
->type
);
859 free(backer
->dp_version_string
);
860 dpif_close(backer
->dpif
);
864 /* Datapath port slated for removal from datapath. */
866 struct ovs_list list_node
;
870 static bool check_variable_length_userdata(struct dpif_backer
*backer
);
871 static void check_support(struct dpif_backer
*backer
);
874 open_dpif_backer(const char *type
, struct dpif_backer
**backerp
)
876 struct dpif_backer
*backer
;
877 struct dpif_port_dump port_dump
;
878 struct dpif_port port
;
879 struct shash_node
*node
;
880 struct ovs_list garbage_list
;
881 struct odp_garbage
*garbage
;
890 backer
= shash_find_data(&all_dpif_backers
, type
);
897 backer_name
= xasprintf("ovs-%s", type
);
899 /* Remove any existing datapaths, since we assume we're the only
900 * userspace controlling the datapath. */
902 dp_enumerate_names(type
, &names
);
903 SSET_FOR_EACH(name
, &names
) {
904 struct dpif
*old_dpif
;
906 /* Don't remove our backer if it exists. */
907 if (!strcmp(name
, backer_name
)) {
911 if (dpif_open(name
, type
, &old_dpif
)) {
912 VLOG_WARN("couldn't open old datapath %s to remove it", name
);
914 dpif_delete(old_dpif
);
915 dpif_close(old_dpif
);
918 sset_destroy(&names
);
920 backer
= xmalloc(sizeof *backer
);
922 error
= dpif_create_and_open(backer_name
, type
, &backer
->dpif
);
925 VLOG_ERR("failed to open datapath of type %s: %s", type
,
926 ovs_strerror(error
));
930 backer
->udpif
= udpif_create(backer
, backer
->dpif
);
932 backer
->type
= xstrdup(type
);
933 backer
->refcount
= 1;
934 hmap_init(&backer
->odp_to_ofport_map
);
935 ovs_rwlock_init(&backer
->odp_to_ofport_lock
);
936 backer
->need_revalidate
= 0;
937 simap_init(&backer
->tnl_backers
);
938 backer
->recv_set_enable
= !ofproto_get_flow_restore_wait();
941 if (backer
->recv_set_enable
) {
942 dpif_flow_flush(backer
->dpif
);
945 /* Loop through the ports already on the datapath and remove any
946 * that we don't need anymore. */
947 list_init(&garbage_list
);
948 dpif_port_dump_start(&port_dump
, backer
->dpif
);
949 while (dpif_port_dump_next(&port_dump
, &port
)) {
950 node
= shash_find(&init_ofp_ports
, port
.name
);
951 if (!node
&& strcmp(port
.name
, dpif_base_name(backer
->dpif
))) {
952 garbage
= xmalloc(sizeof *garbage
);
953 garbage
->odp_port
= port
.port_no
;
954 list_push_front(&garbage_list
, &garbage
->list_node
);
957 dpif_port_dump_done(&port_dump
);
959 LIST_FOR_EACH_POP (garbage
, list_node
, &garbage_list
) {
960 dpif_port_del(backer
->dpif
, garbage
->odp_port
);
964 shash_add(&all_dpif_backers
, type
, backer
);
966 check_support(backer
);
967 atomic_count_init(&backer
->tnl_count
, 0);
969 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
971 VLOG_ERR("failed to listen on datapath of type %s: %s",
972 type
, ovs_strerror(error
));
973 close_dpif_backer(backer
);
977 if (backer
->recv_set_enable
) {
978 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
981 /* This check fails if performed before udpif threads have been set,
982 * as the kernel module checks that the 'pid' in userspace action
984 backer
->support
.variable_length_userdata
985 = check_variable_length_userdata(backer
);
986 backer
->dp_version_string
= dpif_get_dp_version(backer
->dpif
);
992 ovs_native_tunneling_is_on(struct ofproto_dpif
*ofproto
)
994 return ofproto_use_tnl_push_pop
&& ofproto
->backer
->support
.tnl_push_pop
&&
995 atomic_count_get(&ofproto
->backer
->tnl_count
);
998 /* Tests whether 'backer''s datapath supports recirculation. Only newer
999 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
1000 * features on older datapaths that don't support this feature.
1002 * Returns false if 'backer' definitely does not support recirculation, true if
1003 * it seems to support recirculation or if at least the error we get is
1006 check_recirc(struct dpif_backer
*backer
)
1009 struct odputil_keybuf keybuf
;
1012 struct odp_flow_key_parms odp_parms
= {
1019 memset(&flow
, 0, sizeof flow
);
1023 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1024 odp_flow_key_from_flow(&odp_parms
, &key
);
1025 enable_recirc
= dpif_probe_feature(backer
->dpif
, "recirculation", &key
,
1028 if (enable_recirc
) {
1029 VLOG_INFO("%s: Datapath supports recirculation",
1030 dpif_name(backer
->dpif
));
1032 VLOG_INFO("%s: Datapath does not support recirculation",
1033 dpif_name(backer
->dpif
));
1036 return enable_recirc
;
1039 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1040 * some flow attributes for datapaths that support this feature.
1042 * Returns true if 'dpif' supports UFID for flow operations.
1043 * Returns false if 'dpif' does not support UFID. */
1045 check_ufid(struct dpif_backer
*backer
)
1048 struct odputil_keybuf keybuf
;
1052 struct odp_flow_key_parms odp_parms
= {
1056 memset(&flow
, 0, sizeof flow
);
1057 flow
.dl_type
= htons(0x1234);
1059 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1060 odp_flow_key_from_flow(&odp_parms
, &key
);
1061 dpif_flow_hash(backer
->dpif
, key
.data
, key
.size
, &ufid
);
1063 enable_ufid
= dpif_probe_feature(backer
->dpif
, "UFID", &key
, &ufid
);
1066 VLOG_INFO("%s: Datapath supports unique flow ids",
1067 dpif_name(backer
->dpif
));
1069 VLOG_INFO("%s: Datapath does not support unique flow ids",
1070 dpif_name(backer
->dpif
));
1075 /* Tests whether 'backer''s datapath supports variable-length
1076 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1077 * to disable some features on older datapaths that don't support this
1080 * Returns false if 'backer' definitely does not support variable-length
1081 * userdata, true if it seems to support them or if at least the error we get
1084 check_variable_length_userdata(struct dpif_backer
*backer
)
1086 struct eth_header
*eth
;
1087 struct ofpbuf actions
;
1088 struct dpif_execute execute
;
1089 struct dp_packet packet
;
1093 /* Compose a userspace action that will cause an ERANGE error on older
1094 * datapaths that don't support variable-length userdata.
1096 * We really test for using userdata longer than 8 bytes, but older
1097 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1098 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1099 * test for that instead as a proxy for longer userdata support. */
1100 ofpbuf_init(&actions
, 64);
1101 start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_USERSPACE
);
1102 nl_msg_put_u32(&actions
, OVS_USERSPACE_ATTR_PID
,
1103 dpif_port_get_pid(backer
->dpif
, ODPP_NONE
, 0));
1104 nl_msg_put_unspec_zero(&actions
, OVS_USERSPACE_ATTR_USERDATA
, 4);
1105 nl_msg_end_nested(&actions
, start
);
1107 /* Compose a dummy ethernet packet. */
1108 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1109 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1110 eth
->eth_type
= htons(0x1234);
1112 /* Execute the actions. On older datapaths this fails with ERANGE, on
1113 * newer datapaths it succeeds. */
1114 execute
.actions
= actions
.data
;
1115 execute
.actions_len
= actions
.size
;
1116 execute
.packet
= &packet
;
1117 execute
.needs_help
= false;
1118 execute
.probe
= true;
1120 error
= dpif_execute(backer
->dpif
, &execute
);
1122 dp_packet_uninit(&packet
);
1123 ofpbuf_uninit(&actions
);
1130 /* Variable-length userdata is not supported. */
1131 VLOG_WARN("%s: datapath does not support variable-length userdata "
1132 "feature (needs Linux 3.10+ or kernel module from OVS "
1133 "1..11+). The NXAST_SAMPLE action will be ignored.",
1134 dpif_name(backer
->dpif
));
1138 /* Something odd happened. We're not sure whether variable-length
1139 * userdata is supported. Default to "yes". */
1140 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1141 dpif_name(backer
->dpif
), ovs_strerror(error
));
1146 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1148 * Returns the number of elements in a struct flow's mpls_lse field
1149 * if the datapath supports at least that many entries in an
1151 * Otherwise returns the number of MPLS push actions supported by
1154 check_max_mpls_depth(struct dpif_backer
*backer
)
1159 for (n
= 0; n
< FLOW_MAX_MPLS_LABELS
; n
++) {
1160 struct odputil_keybuf keybuf
;
1162 struct odp_flow_key_parms odp_parms
= {
1166 memset(&flow
, 0, sizeof flow
);
1167 flow
.dl_type
= htons(ETH_TYPE_MPLS
);
1168 flow_set_mpls_bos(&flow
, n
, 1);
1170 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1171 odp_flow_key_from_flow(&odp_parms
, &key
);
1172 if (!dpif_probe_feature(backer
->dpif
, "MPLS", &key
, NULL
)) {
1177 VLOG_INFO("%s: MPLS label stack length probed as %d",
1178 dpif_name(backer
->dpif
), n
);
1182 /* Tests whether 'backer''s datapath supports masked data in
1183 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1184 * datapaths that don't support this feature. */
1186 check_masked_set_action(struct dpif_backer
*backer
)
1188 struct eth_header
*eth
;
1189 struct ofpbuf actions
;
1190 struct dpif_execute execute
;
1191 struct dp_packet packet
;
1193 struct ovs_key_ethernet key
, mask
;
1195 /* Compose a set action that will cause an EINVAL error on older
1196 * datapaths that don't support masked set actions.
1197 * Avoid using a full mask, as it could be translated to a non-masked
1198 * set action instead. */
1199 ofpbuf_init(&actions
, 64);
1200 memset(&key
, 0x53, sizeof key
);
1201 memset(&mask
, 0x7f, sizeof mask
);
1202 commit_masked_set_action(&actions
, OVS_KEY_ATTR_ETHERNET
, &key
, &mask
,
1205 /* Compose a dummy ethernet packet. */
1206 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1207 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1208 eth
->eth_type
= htons(0x1234);
1210 /* Execute the actions. On older datapaths this fails with EINVAL, on
1211 * newer datapaths it succeeds. */
1212 execute
.actions
= actions
.data
;
1213 execute
.actions_len
= actions
.size
;
1214 execute
.packet
= &packet
;
1215 execute
.needs_help
= false;
1216 execute
.probe
= true;
1218 error
= dpif_execute(backer
->dpif
, &execute
);
1220 dp_packet_uninit(&packet
);
1221 ofpbuf_uninit(&actions
);
1224 /* Masked set action is not supported. */
1225 VLOG_INFO("%s: datapath does not support masked set action feature.",
1226 dpif_name(backer
->dpif
));
1232 check_support(struct dpif_backer
*backer
)
1234 /* This feature needs to be tested after udpif threads are set. */
1235 backer
->support
.variable_length_userdata
= false;
1237 backer
->support
.odp
.recirc
= check_recirc(backer
);
1238 backer
->support
.odp
.max_mpls_depth
= check_max_mpls_depth(backer
);
1239 backer
->support
.masked_set_action
= check_masked_set_action(backer
);
1240 backer
->support
.ufid
= check_ufid(backer
);
1241 backer
->support
.tnl_push_pop
= dpif_supports_tnl_push_pop(backer
->dpif
);
1245 construct(struct ofproto
*ofproto_
)
1247 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1248 struct shash_node
*node
, *next
;
1251 /* Tunnel module can get used right after the udpif threads are running. */
1252 ofproto_tunnel_init();
1254 error
= open_dpif_backer(ofproto
->up
.type
, &ofproto
->backer
);
1259 atomic_init(&ofproto
->tables_version
, CLS_MIN_VERSION
);
1260 ofproto
->netflow
= NULL
;
1261 ofproto
->sflow
= NULL
;
1262 ofproto
->ipfix
= NULL
;
1263 ofproto
->stp
= NULL
;
1264 ofproto
->rstp
= NULL
;
1265 ofproto
->dump_seq
= 0;
1266 hmap_init(&ofproto
->bundles
);
1267 ofproto
->ml
= mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME
);
1269 ofproto
->mbridge
= mbridge_create();
1270 ofproto
->has_bonded_bundles
= false;
1271 ofproto
->lacp_enabled
= false;
1272 ovs_mutex_init_adaptive(&ofproto
->stats_mutex
);
1273 ovs_mutex_init(&ofproto
->vsp_mutex
);
1275 guarded_list_init(&ofproto
->pins
);
1277 hmap_init(&ofproto
->vlandev_map
);
1278 hmap_init(&ofproto
->realdev_vid_map
);
1280 sset_init(&ofproto
->ports
);
1281 sset_init(&ofproto
->ghost_ports
);
1282 sset_init(&ofproto
->port_poll_set
);
1283 ofproto
->port_poll_errno
= 0;
1284 ofproto
->change_seq
= 0;
1285 ofproto
->pins_seq
= seq_create();
1286 ofproto
->pins_seqno
= seq_read(ofproto
->pins_seq
);
1289 SHASH_FOR_EACH_SAFE (node
, next
, &init_ofp_ports
) {
1290 struct iface_hint
*iface_hint
= node
->data
;
1292 if (!strcmp(iface_hint
->br_name
, ofproto
->up
.name
)) {
1293 /* Check if the datapath already has this port. */
1294 if (dpif_port_exists(ofproto
->backer
->dpif
, node
->name
)) {
1295 sset_add(&ofproto
->ports
, node
->name
);
1298 free(iface_hint
->br_name
);
1299 free(iface_hint
->br_type
);
1301 shash_delete(&init_ofp_ports
, node
);
1305 hmap_insert(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
,
1306 hash_string(ofproto
->up
.name
, 0));
1307 memset(&ofproto
->stats
, 0, sizeof ofproto
->stats
);
1309 ofproto_init_tables(ofproto_
, N_TABLES
);
1310 error
= add_internal_flows(ofproto
);
1312 ofproto
->up
.tables
[TBL_INTERNAL
].flags
= OFTABLE_HIDDEN
| OFTABLE_READONLY
;
1318 add_internal_miss_flow(struct ofproto_dpif
*ofproto
, int id
,
1319 const struct ofpbuf
*ofpacts
, struct rule_dpif
**rulep
)
1325 match_init_catchall(&match
);
1326 match_set_reg(&match
, 0, id
);
1328 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 0, 0, ofpacts
,
1330 *rulep
= error
? NULL
: rule_dpif_cast(rule
);
1336 add_internal_flows(struct ofproto_dpif
*ofproto
)
1338 struct ofpact_controller
*controller
;
1339 uint64_t ofpacts_stub
[128 / 8];
1340 struct ofpbuf ofpacts
;
1341 struct rule
*unused_rulep OVS_UNUSED
;
1346 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1349 controller
= ofpact_put_CONTROLLER(&ofpacts
);
1350 controller
->max_len
= UINT16_MAX
;
1351 controller
->controller_id
= 0;
1352 controller
->reason
= OFPR_NO_MATCH
;
1353 ofpact_pad(&ofpacts
);
1355 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1356 &ofproto
->miss_rule
);
1361 ofpbuf_clear(&ofpacts
);
1362 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1363 &ofproto
->no_packet_in_rule
);
1368 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1369 &ofproto
->drop_frags_rule
);
1374 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1375 * zero when reaching this rule.
1377 * (priority=2), recirc_id=0, actions=drop
1379 ofpbuf_clear(&ofpacts
);
1380 match_init_catchall(&match
);
1381 match_set_recirc_id(&match
, 0);
1382 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 2, 0, &ofpacts
,
1388 destruct(struct ofproto
*ofproto_
)
1390 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1391 struct ofproto_packet_in
*pin
;
1392 struct rule_dpif
*rule
;
1393 struct oftable
*table
;
1394 struct ovs_list pins
;
1396 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1398 xlate_remove_ofproto(ofproto
);
1401 /* Ensure that the upcall processing threads have no remaining references
1402 * to the ofproto or anything in it. */
1403 udpif_synchronize(ofproto
->backer
->udpif
);
1405 hmap_remove(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
);
1407 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1408 CLS_FOR_EACH (rule
, up
.cr
, &table
->cls
) {
1409 ofproto_rule_delete(&ofproto
->up
, &rule
->up
);
1412 ofproto_group_delete_all(&ofproto
->up
);
1414 guarded_list_pop_all(&ofproto
->pins
, &pins
);
1415 LIST_FOR_EACH_POP (pin
, list_node
, &pins
) {
1416 free(CONST_CAST(void *, pin
->up
.packet
));
1419 guarded_list_destroy(&ofproto
->pins
);
1421 recirc_free_ofproto(ofproto
, ofproto
->up
.name
);
1423 mbridge_unref(ofproto
->mbridge
);
1425 netflow_unref(ofproto
->netflow
);
1426 dpif_sflow_unref(ofproto
->sflow
);
1427 dpif_ipfix_unref(ofproto
->ipfix
);
1428 hmap_destroy(&ofproto
->bundles
);
1429 mac_learning_unref(ofproto
->ml
);
1430 mcast_snooping_unref(ofproto
->ms
);
1432 hmap_destroy(&ofproto
->vlandev_map
);
1433 hmap_destroy(&ofproto
->realdev_vid_map
);
1435 sset_destroy(&ofproto
->ports
);
1436 sset_destroy(&ofproto
->ghost_ports
);
1437 sset_destroy(&ofproto
->port_poll_set
);
1439 ovs_mutex_destroy(&ofproto
->stats_mutex
);
1440 ovs_mutex_destroy(&ofproto
->vsp_mutex
);
1442 seq_destroy(ofproto
->pins_seq
);
1444 close_dpif_backer(ofproto
->backer
);
1448 run(struct ofproto
*ofproto_
)
1450 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1451 uint64_t new_seq
, new_dump_seq
;
1453 if (mbridge_need_revalidate(ofproto
->mbridge
)) {
1454 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1455 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1456 mac_learning_flush(ofproto
->ml
);
1457 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1458 mcast_snooping_mdb_flush(ofproto
->ms
);
1461 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1462 * flow restore. Even though nothing is processed during flow restore,
1463 * all queued 'pins' will be handled immediately when flow restore
1465 ofproto
->pins_seqno
= seq_read(ofproto
->pins_seq
);
1467 /* Do not perform any periodic activity required by 'ofproto' while
1468 * waiting for flow restore to complete. */
1469 if (!ofproto_get_flow_restore_wait()) {
1470 struct ofproto_packet_in
*pin
;
1471 struct ovs_list pins
;
1473 guarded_list_pop_all(&ofproto
->pins
, &pins
);
1474 LIST_FOR_EACH_POP (pin
, list_node
, &pins
) {
1475 connmgr_send_packet_in(ofproto
->up
.connmgr
, pin
);
1476 free(CONST_CAST(void *, pin
->up
.packet
));
1481 if (ofproto
->netflow
) {
1482 netflow_run(ofproto
->netflow
);
1484 if (ofproto
->sflow
) {
1485 dpif_sflow_run(ofproto
->sflow
);
1487 if (ofproto
->ipfix
) {
1488 dpif_ipfix_run(ofproto
->ipfix
);
1491 new_seq
= seq_read(connectivity_seq_get());
1492 if (ofproto
->change_seq
!= new_seq
) {
1493 struct ofport_dpif
*ofport
;
1495 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1499 ofproto
->change_seq
= new_seq
;
1501 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1502 struct ofbundle
*bundle
;
1504 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1511 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1512 if (mac_learning_run(ofproto
->ml
)) {
1513 ofproto
->backer
->need_revalidate
= REV_MAC_LEARNING
;
1515 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1517 if (mcast_snooping_run(ofproto
->ms
)) {
1518 ofproto
->backer
->need_revalidate
= REV_MCAST_SNOOPING
;
1521 new_dump_seq
= seq_read(udpif_dump_seq(ofproto
->backer
->udpif
));
1522 if (ofproto
->dump_seq
!= new_dump_seq
) {
1523 struct rule
*rule
, *next_rule
;
1525 /* We know stats are relatively fresh, so now is a good time to do some
1527 ofproto
->dump_seq
= new_dump_seq
;
1529 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1531 ovs_mutex_lock(&ofproto_mutex
);
1532 LIST_FOR_EACH_SAFE (rule
, next_rule
, expirable
,
1533 &ofproto
->up
.expirable
) {
1534 rule_expire(rule_dpif_cast(rule
));
1536 ovs_mutex_unlock(&ofproto_mutex
);
1538 /* All outstanding data in existing flows has been accounted, so it's a
1539 * good time to do bond rebalancing. */
1540 if (ofproto
->has_bonded_bundles
) {
1541 struct ofbundle
*bundle
;
1543 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1545 bond_rebalance(bundle
->bond
);
1554 wait(struct ofproto
*ofproto_
)
1556 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1558 if (ofproto_get_flow_restore_wait()) {
1562 if (ofproto
->sflow
) {
1563 dpif_sflow_wait(ofproto
->sflow
);
1565 if (ofproto
->ipfix
) {
1566 dpif_ipfix_wait(ofproto
->ipfix
);
1568 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1569 struct ofbundle
*bundle
;
1571 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1572 bundle_wait(bundle
);
1575 if (ofproto
->netflow
) {
1576 netflow_wait(ofproto
->netflow
);
1578 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
1579 mac_learning_wait(ofproto
->ml
);
1580 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1581 mcast_snooping_wait(ofproto
->ms
);
1583 if (ofproto
->backer
->need_revalidate
) {
1584 /* Shouldn't happen, but if it does just go around again. */
1585 VLOG_DBG_RL(&rl
, "need revalidate in ofproto_wait_cb()");
1586 poll_immediate_wake();
1589 seq_wait(udpif_dump_seq(ofproto
->backer
->udpif
), ofproto
->dump_seq
);
1590 seq_wait(ofproto
->pins_seq
, ofproto
->pins_seqno
);
1594 type_get_memory_usage(const char *type
, struct simap
*usage
)
1596 struct dpif_backer
*backer
;
1598 backer
= shash_find_data(&all_dpif_backers
, type
);
1600 udpif_get_memory_usage(backer
->udpif
, usage
);
1605 flush(struct ofproto
*ofproto_
)
1607 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1608 struct dpif_backer
*backer
= ofproto
->backer
;
1611 udpif_flush(backer
->udpif
);
1616 query_tables(struct ofproto
*ofproto
,
1617 struct ofputil_table_features
*features
,
1618 struct ofputil_table_stats
*stats
)
1620 strcpy(features
->name
, "classifier");
1625 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
1626 unsigned long missed
, matched
;
1628 atomic_read_relaxed(&ofproto
->tables
[i
].n_matched
, &matched
);
1629 atomic_read_relaxed(&ofproto
->tables
[i
].n_missed
, &missed
);
1631 stats
[i
].matched_count
= matched
;
1632 stats
[i
].lookup_count
= matched
+ missed
;
1638 set_tables_version(struct ofproto
*ofproto_
, cls_version_t version
)
1640 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1642 atomic_store_relaxed(&ofproto
->tables_version
, version
);
1646 static struct ofport
*
1649 struct ofport_dpif
*port
= xzalloc(sizeof *port
);
1654 port_dealloc(struct ofport
*port_
)
1656 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1661 port_construct(struct ofport
*port_
)
1663 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1664 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1665 const struct netdev
*netdev
= port
->up
.netdev
;
1666 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1667 const char *dp_port_name
;
1668 struct dpif_port dpif_port
;
1671 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1672 port
->bundle
= NULL
;
1676 port
->may_enable
= false;
1677 port
->stp_port
= NULL
;
1678 port
->stp_state
= STP_DISABLED
;
1679 port
->rstp_port
= NULL
;
1680 port
->rstp_state
= RSTP_DISABLED
;
1681 port
->is_tunnel
= false;
1685 port
->realdev_ofp_port
= 0;
1686 port
->vlandev_vid
= 0;
1687 port
->carrier_seq
= netdev_get_carrier_resets(netdev
);
1688 port
->is_layer3
= netdev_vport_is_layer3(netdev
);
1690 if (netdev_vport_is_patch(netdev
)) {
1691 /* By bailing out here, we don't submit the port to the sFlow module
1692 * to be considered for counter polling export. This is correct
1693 * because the patch port represents an interface that sFlow considers
1694 * to be "internal" to the switch as a whole, and therefore not a
1695 * candidate for counter polling. */
1696 port
->odp_port
= ODPP_NONE
;
1697 ofport_update_peer(port
);
1701 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1702 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
, dp_port_name
,
1708 port
->odp_port
= dpif_port
.port_no
;
1710 if (netdev_get_tunnel_config(netdev
)) {
1711 atomic_count_inc(&ofproto
->backer
->tnl_count
);
1712 error
= tnl_port_add(port
, port
->up
.netdev
, port
->odp_port
,
1713 ovs_native_tunneling_is_on(ofproto
), dp_port_name
);
1715 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1716 dpif_port_destroy(&dpif_port
);
1720 port
->is_tunnel
= true;
1721 if (ofproto
->ipfix
) {
1722 dpif_ipfix_add_tunnel_port(ofproto
->ipfix
, port_
, port
->odp_port
);
1725 /* Sanity-check that a mapping doesn't already exist. This
1726 * shouldn't happen for non-tunnel ports. */
1727 if (odp_port_to_ofp_port(ofproto
, port
->odp_port
) != OFPP_NONE
) {
1728 VLOG_ERR("port %s already has an OpenFlow port number",
1730 dpif_port_destroy(&dpif_port
);
1734 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1735 hmap_insert(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
,
1736 hash_odp_port(port
->odp_port
));
1737 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1739 dpif_port_destroy(&dpif_port
);
1741 if (ofproto
->sflow
) {
1742 dpif_sflow_add_port(ofproto
->sflow
, port_
, port
->odp_port
);
1749 port_destruct(struct ofport
*port_
)
1751 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1752 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1753 const char *devname
= netdev_get_name(port
->up
.netdev
);
1754 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1755 const char *dp_port_name
;
1757 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1759 xlate_ofport_remove(port
);
1762 dp_port_name
= netdev_vport_get_dpif_port(port
->up
.netdev
, namebuf
,
1764 if (dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
1765 /* The underlying device is still there, so delete it. This
1766 * happens when the ofproto is being destroyed, since the caller
1767 * assumes that removal of attached ports will happen as part of
1769 if (!port
->is_tunnel
) {
1770 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
);
1775 port
->peer
->peer
= NULL
;
1779 if (port
->odp_port
!= ODPP_NONE
&& !port
->is_tunnel
) {
1780 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1781 hmap_remove(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
);
1782 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1785 if (port
->is_tunnel
) {
1786 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1789 if (port
->is_tunnel
&& ofproto
->ipfix
) {
1790 dpif_ipfix_del_tunnel_port(ofproto
->ipfix
, port
->odp_port
);
1794 sset_find_and_delete(&ofproto
->ports
, devname
);
1795 sset_find_and_delete(&ofproto
->ghost_ports
, devname
);
1796 bundle_remove(port_
);
1797 set_cfm(port_
, NULL
);
1798 set_bfd(port_
, NULL
);
1799 set_lldp(port_
, NULL
);
1800 if (port
->stp_port
) {
1801 stp_port_disable(port
->stp_port
);
1803 set_rstp_port(port_
, NULL
);
1804 if (ofproto
->sflow
) {
1805 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
1812 port_modified(struct ofport
*port_
)
1814 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1815 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1816 const char *dp_port_name
;
1817 struct netdev
*netdev
= port
->up
.netdev
;
1819 if (port
->bundle
&& port
->bundle
->bond
) {
1820 bond_slave_set_netdev(port
->bundle
->bond
, port
, netdev
);
1824 cfm_set_netdev(port
->cfm
, netdev
);
1828 bfd_set_netdev(port
->bfd
, netdev
);
1831 ofproto_dpif_monitor_port_update(port
, port
->bfd
, port
->cfm
,
1832 port
->lldp
, port
->up
.pp
.hw_addr
);
1834 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1836 if (port
->is_tunnel
) {
1837 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1839 if (tnl_port_reconfigure(port
, netdev
, port
->odp_port
,
1840 ovs_native_tunneling_is_on(ofproto
),
1842 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1846 ofport_update_peer(port
);
1850 port_reconfigured(struct ofport
*port_
, enum ofputil_port_config old_config
)
1852 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1853 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1854 enum ofputil_port_config changed
= old_config
^ port
->up
.pp
.config
;
1856 if (changed
& (OFPUTIL_PC_NO_RECV
| OFPUTIL_PC_NO_RECV_STP
|
1857 OFPUTIL_PC_NO_FWD
| OFPUTIL_PC_NO_FLOOD
|
1858 OFPUTIL_PC_NO_PACKET_IN
)) {
1859 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1861 if (changed
& OFPUTIL_PC_NO_FLOOD
&& port
->bundle
) {
1862 bundle_update(port
->bundle
);
1868 set_sflow(struct ofproto
*ofproto_
,
1869 const struct ofproto_sflow_options
*sflow_options
)
1871 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1872 struct dpif_sflow
*ds
= ofproto
->sflow
;
1874 if (sflow_options
) {
1875 uint32_t old_probability
= ds
? dpif_sflow_get_probability(ds
) : 0;
1877 struct ofport_dpif
*ofport
;
1879 ds
= ofproto
->sflow
= dpif_sflow_create();
1880 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1881 dpif_sflow_add_port(ds
, &ofport
->up
, ofport
->odp_port
);
1884 dpif_sflow_set_options(ds
, sflow_options
);
1885 if (dpif_sflow_get_probability(ds
) != old_probability
) {
1886 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1890 dpif_sflow_unref(ds
);
1891 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1892 ofproto
->sflow
= NULL
;
1900 struct ofproto
*ofproto_
,
1901 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
1902 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
1903 size_t n_flow_exporters_options
)
1905 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1906 struct dpif_ipfix
*di
= ofproto
->ipfix
;
1907 bool has_options
= bridge_exporter_options
|| flow_exporters_options
;
1908 bool new_di
= false;
1910 if (has_options
&& !di
) {
1911 di
= ofproto
->ipfix
= dpif_ipfix_create();
1916 /* Call set_options in any case to cleanly flush the flow
1917 * caches in the last exporters that are to be destroyed. */
1918 dpif_ipfix_set_options(
1919 di
, bridge_exporter_options
, flow_exporters_options
,
1920 n_flow_exporters_options
);
1922 /* Add tunnel ports only when a new ipfix created */
1923 if (new_di
== true) {
1924 struct ofport_dpif
*ofport
;
1925 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1926 if (ofport
->is_tunnel
== true) {
1927 dpif_ipfix_add_tunnel_port(di
, &ofport
->up
, ofport
->odp_port
);
1933 dpif_ipfix_unref(di
);
1934 ofproto
->ipfix
= NULL
;
1942 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
1944 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1945 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
1946 struct cfm
*old
= ofport
->cfm
;
1951 ofport
->cfm
= cfm_create(ofport
->up
.netdev
);
1954 if (cfm_configure(ofport
->cfm
, s
)) {
1961 cfm_unref(ofport
->cfm
);
1964 if (ofport
->cfm
!= old
) {
1965 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1967 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
1968 ofport
->lldp
, ofport
->up
.pp
.hw_addr
);
1973 cfm_status_changed(struct ofport
*ofport_
)
1975 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1977 return ofport
->cfm
? cfm_check_status_change(ofport
->cfm
) : true;
1981 get_cfm_status(const struct ofport
*ofport_
,
1982 struct cfm_status
*status
)
1984 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
1988 cfm_get_status(ofport
->cfm
, status
);
1997 set_bfd(struct ofport
*ofport_
, const struct smap
*cfg
)
1999 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
2000 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2004 ofport
->bfd
= bfd_configure(old
, netdev_get_name(ofport
->up
.netdev
),
2005 cfg
, ofport
->up
.netdev
);
2006 if (ofport
->bfd
!= old
) {
2007 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2009 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2010 ofport
->lldp
, ofport
->up
.pp
.hw_addr
);
2015 bfd_status_changed(struct ofport
*ofport_
)
2017 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2019 return ofport
->bfd
? bfd_check_status_change(ofport
->bfd
) : true;
2023 get_bfd_status(struct ofport
*ofport_
, struct smap
*smap
)
2025 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2029 bfd_get_status(ofport
->bfd
, smap
);
2038 set_lldp(struct ofport
*ofport_
,
2039 const struct smap
*cfg
)
2041 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2045 if (!ofport
->lldp
) {
2046 struct ofproto_dpif
*ofproto
;
2048 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2049 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2050 ofport
->lldp
= lldp_create(ofport
->up
.netdev
, ofport_
->mtu
, cfg
);
2053 if (!lldp_configure(ofport
->lldp
, cfg
)) {
2058 lldp_unref(ofport
->lldp
);
2059 ofport
->lldp
= NULL
;
2062 ofproto_dpif_monitor_port_update(ofport
,
2066 ofport
->up
.pp
.hw_addr
);
2071 get_lldp_status(const struct ofport
*ofport_
,
2072 struct lldp_status
*status OVS_UNUSED
)
2074 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2076 return ofport
->lldp
? true : false;
2080 set_aa(struct ofproto
*ofproto OVS_UNUSED
,
2081 const struct aa_settings
*s
)
2083 return aa_configure(s
);
2087 aa_mapping_set(struct ofproto
*ofproto_ OVS_UNUSED
, void *aux
,
2088 const struct aa_mapping_settings
*s
)
2090 return aa_mapping_register(aux
, s
);
2094 aa_mapping_unset(struct ofproto
*ofproto OVS_UNUSED
, void *aux
)
2096 return aa_mapping_unregister(aux
);
2100 aa_vlan_get_queued(struct ofproto
*ofproto OVS_UNUSED
, struct ovs_list
*list
)
2102 return aa_get_vlan_queued(list
);
2106 aa_vlan_get_queue_size(struct ofproto
*ofproto OVS_UNUSED
)
2108 return aa_get_vlan_queue_size();
2112 /* Spanning Tree. */
2114 /* Called while rstp_mutex is held. */
2116 rstp_send_bpdu_cb(struct dp_packet
*pkt
, void *ofport_
, void *ofproto_
)
2118 struct ofproto_dpif
*ofproto
= ofproto_
;
2119 struct ofport_dpif
*ofport
= ofport_
;
2120 struct eth_header
*eth
= dp_packet_l2(pkt
);
2122 netdev_get_etheraddr(ofport
->up
.netdev
, eth
->eth_src
);
2123 if (eth_addr_is_zero(eth
->eth_src
)) {
2124 VLOG_WARN_RL(&rl
, "%s port %d: cannot send RSTP BPDU on a port which "
2125 "does not have a configured source MAC address.",
2126 ofproto
->up
.name
, ofp_to_u16(ofport
->up
.ofp_port
));
2128 ofproto_dpif_send_packet(ofport
, pkt
);
2130 dp_packet_delete(pkt
);
2134 send_bpdu_cb(struct dp_packet
*pkt
, int port_num
, void *ofproto_
)
2136 struct ofproto_dpif
*ofproto
= ofproto_
;
2137 struct stp_port
*sp
= stp_get_port(ofproto
->stp
, port_num
);
2138 struct ofport_dpif
*ofport
;
2140 ofport
= stp_port_get_aux(sp
);
2142 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
2143 ofproto
->up
.name
, port_num
);
2145 struct eth_header
*eth
= dp_packet_l2(pkt
);
2147 netdev_get_etheraddr(ofport
->up
.netdev
, eth
->eth_src
);
2148 if (eth_addr_is_zero(eth
->eth_src
)) {
2149 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d "
2150 "with unknown MAC", ofproto
->up
.name
, port_num
);
2152 ofproto_dpif_send_packet(ofport
, pkt
);
2155 dp_packet_delete(pkt
);
2158 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2160 set_rstp(struct ofproto
*ofproto_
, const struct ofproto_rstp_settings
*s
)
2162 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2164 /* Only revalidate flows if the configuration changed. */
2165 if (!s
!= !ofproto
->rstp
) {
2166 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2170 if (!ofproto
->rstp
) {
2171 ofproto
->rstp
= rstp_create(ofproto_
->name
, s
->address
,
2172 rstp_send_bpdu_cb
, ofproto
);
2173 ofproto
->rstp_last_tick
= time_msec();
2175 rstp_set_bridge_address(ofproto
->rstp
, s
->address
);
2176 rstp_set_bridge_priority(ofproto
->rstp
, s
->priority
);
2177 rstp_set_bridge_ageing_time(ofproto
->rstp
, s
->ageing_time
);
2178 rstp_set_bridge_force_protocol_version(ofproto
->rstp
,
2179 s
->force_protocol_version
);
2180 rstp_set_bridge_max_age(ofproto
->rstp
, s
->bridge_max_age
);
2181 rstp_set_bridge_forward_delay(ofproto
->rstp
, s
->bridge_forward_delay
);
2182 rstp_set_bridge_transmit_hold_count(ofproto
->rstp
,
2183 s
->transmit_hold_count
);
2185 struct ofport
*ofport
;
2186 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2187 set_rstp_port(ofport
, NULL
);
2189 rstp_unref(ofproto
->rstp
);
2190 ofproto
->rstp
= NULL
;
2195 get_rstp_status(struct ofproto
*ofproto_
, struct ofproto_rstp_status
*s
)
2197 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2199 if (ofproto
->rstp
) {
2201 s
->root_id
= rstp_get_root_id(ofproto
->rstp
);
2202 s
->bridge_id
= rstp_get_bridge_id(ofproto
->rstp
);
2203 s
->designated_id
= rstp_get_designated_id(ofproto
->rstp
);
2204 s
->root_path_cost
= rstp_get_root_path_cost(ofproto
->rstp
);
2205 s
->designated_port_id
= rstp_get_designated_port_id(ofproto
->rstp
);
2206 s
->bridge_port_id
= rstp_get_bridge_port_id(ofproto
->rstp
);
2213 update_rstp_port_state(struct ofport_dpif
*ofport
)
2215 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2216 enum rstp_state state
;
2218 /* Figure out new state. */
2219 state
= ofport
->rstp_port
? rstp_port_get_state(ofport
->rstp_port
)
2223 if (ofport
->rstp_state
!= state
) {
2224 enum ofputil_port_state of_state
;
2227 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2228 netdev_get_name(ofport
->up
.netdev
),
2229 rstp_state_name(ofport
->rstp_state
),
2230 rstp_state_name(state
));
2232 if (rstp_learn_in_state(ofport
->rstp_state
)
2233 != rstp_learn_in_state(state
)) {
2234 /* XXX: Learning action flows should also be flushed. */
2235 if (ofport
->bundle
) {
2236 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2237 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2238 bundle_flush_macs(ofport
->bundle
, false);
2242 fwd_change
= rstp_forward_in_state(ofport
->rstp_state
)
2243 != rstp_forward_in_state(state
);
2245 ofproto
->backer
->need_revalidate
= REV_RSTP
;
2246 ofport
->rstp_state
= state
;
2248 if (fwd_change
&& ofport
->bundle
) {
2249 bundle_update(ofport
->bundle
);
2252 /* Update the RSTP state bits in the OpenFlow port description. */
2253 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2254 of_state
|= (state
== RSTP_LEARNING
? OFPUTIL_PS_STP_LEARN
2255 : state
== RSTP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2256 : state
== RSTP_DISCARDING
? OFPUTIL_PS_STP_LISTEN
2258 ofproto_port_set_state(&ofport
->up
, of_state
);
2263 rstp_run(struct ofproto_dpif
*ofproto
)
2265 if (ofproto
->rstp
) {
2266 long long int now
= time_msec();
2267 long long int elapsed
= now
- ofproto
->rstp_last_tick
;
2268 struct rstp_port
*rp
;
2269 struct ofport_dpif
*ofport
;
2271 /* Every second, decrease the values of the timers. */
2272 if (elapsed
>= 1000) {
2273 rstp_tick_timers(ofproto
->rstp
);
2274 ofproto
->rstp_last_tick
= now
;
2277 while ((ofport
= rstp_get_next_changed_port_aux(ofproto
->rstp
, &rp
))) {
2278 update_rstp_port_state(ofport
);
2282 /* FIXME: This check should be done on-event (i.e., when setting
2283 * p->fdb_flush) and not periodically.
2285 while ((ofport
= rstp_check_and_reset_fdb_flush(ofproto
->rstp
, &rp
))) {
2286 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2287 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2288 bundle_flush_macs(ofport
->bundle
, false);
2292 if (rstp_shift_root_learned_address(ofproto
->rstp
)) {
2293 bundle_move(((struct ofport_dpif
*)rstp_get_old_root_aux(ofproto
->rstp
))->bundle
,
2294 ((struct ofport_dpif
*)rstp_get_new_root_aux(ofproto
->rstp
))->bundle
);
2295 rstp_reset_root_changed(ofproto
->rstp
);
2300 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2302 set_stp(struct ofproto
*ofproto_
, const struct ofproto_stp_settings
*s
)
2304 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2306 /* Only revalidate flows if the configuration changed. */
2307 if (!s
!= !ofproto
->stp
) {
2308 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2312 if (!ofproto
->stp
) {
2313 ofproto
->stp
= stp_create(ofproto_
->name
, s
->system_id
,
2314 send_bpdu_cb
, ofproto
);
2315 ofproto
->stp_last_tick
= time_msec();
2318 stp_set_bridge_id(ofproto
->stp
, s
->system_id
);
2319 stp_set_bridge_priority(ofproto
->stp
, s
->priority
);
2320 stp_set_hello_time(ofproto
->stp
, s
->hello_time
);
2321 stp_set_max_age(ofproto
->stp
, s
->max_age
);
2322 stp_set_forward_delay(ofproto
->stp
, s
->fwd_delay
);
2324 struct ofport
*ofport
;
2326 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2327 set_stp_port(ofport
, NULL
);
2330 stp_unref(ofproto
->stp
);
2331 ofproto
->stp
= NULL
;
2338 get_stp_status(struct ofproto
*ofproto_
, struct ofproto_stp_status
*s
)
2340 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2344 s
->bridge_id
= stp_get_bridge_id(ofproto
->stp
);
2345 s
->designated_root
= stp_get_designated_root(ofproto
->stp
);
2346 s
->root_path_cost
= stp_get_root_path_cost(ofproto
->stp
);
2355 update_stp_port_state(struct ofport_dpif
*ofport
)
2357 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2358 enum stp_state state
;
2360 /* Figure out new state. */
2361 state
= ofport
->stp_port
? stp_port_get_state(ofport
->stp_port
)
2365 if (ofport
->stp_state
!= state
) {
2366 enum ofputil_port_state of_state
;
2369 VLOG_DBG("port %s: STP state changed from %s to %s",
2370 netdev_get_name(ofport
->up
.netdev
),
2371 stp_state_name(ofport
->stp_state
),
2372 stp_state_name(state
));
2373 if (stp_learn_in_state(ofport
->stp_state
)
2374 != stp_learn_in_state(state
)) {
2375 /* xxx Learning action flows should also be flushed. */
2376 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2377 mac_learning_flush(ofproto
->ml
);
2378 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2379 mcast_snooping_mdb_flush(ofproto
->ms
);
2381 fwd_change
= stp_forward_in_state(ofport
->stp_state
)
2382 != stp_forward_in_state(state
);
2384 ofproto
->backer
->need_revalidate
= REV_STP
;
2385 ofport
->stp_state
= state
;
2386 ofport
->stp_state_entered
= time_msec();
2388 if (fwd_change
&& ofport
->bundle
) {
2389 bundle_update(ofport
->bundle
);
2392 /* Update the STP state bits in the OpenFlow port description. */
2393 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2394 of_state
|= (state
== STP_LISTENING
? OFPUTIL_PS_STP_LISTEN
2395 : state
== STP_LEARNING
? OFPUTIL_PS_STP_LEARN
2396 : state
== STP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2397 : state
== STP_BLOCKING
? OFPUTIL_PS_STP_BLOCK
2399 ofproto_port_set_state(&ofport
->up
, of_state
);
2403 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2404 * caller is responsible for assigning STP port numbers and ensuring
2405 * there are no duplicates. */
2407 set_stp_port(struct ofport
*ofport_
,
2408 const struct ofproto_port_stp_settings
*s
)
2410 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2411 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2412 struct stp_port
*sp
= ofport
->stp_port
;
2414 if (!s
|| !s
->enable
) {
2416 ofport
->stp_port
= NULL
;
2417 stp_port_disable(sp
);
2418 update_stp_port_state(ofport
);
2421 } else if (sp
&& stp_port_no(sp
) != s
->port_num
2422 && ofport
== stp_port_get_aux(sp
)) {
2423 /* The port-id changed, so disable the old one if it's not
2424 * already in use by another port. */
2425 stp_port_disable(sp
);
2428 sp
= ofport
->stp_port
= stp_get_port(ofproto
->stp
, s
->port_num
);
2430 /* Set name before enabling the port so that debugging messages can print
2432 stp_port_set_name(sp
, netdev_get_name(ofport
->up
.netdev
));
2433 stp_port_enable(sp
);
2435 stp_port_set_aux(sp
, ofport
);
2436 stp_port_set_priority(sp
, s
->priority
);
2437 stp_port_set_path_cost(sp
, s
->path_cost
);
2439 update_stp_port_state(ofport
);
2445 get_stp_port_status(struct ofport
*ofport_
,
2446 struct ofproto_port_stp_status
*s
)
2448 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2449 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2450 struct stp_port
*sp
= ofport
->stp_port
;
2452 if (!ofproto
->stp
|| !sp
) {
2458 s
->port_id
= stp_port_get_id(sp
);
2459 s
->state
= stp_port_get_state(sp
);
2460 s
->sec_in_state
= (time_msec() - ofport
->stp_state_entered
) / 1000;
2461 s
->role
= stp_port_get_role(sp
);
2467 get_stp_port_stats(struct ofport
*ofport_
,
2468 struct ofproto_port_stp_stats
*s
)
2470 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2471 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2472 struct stp_port
*sp
= ofport
->stp_port
;
2474 if (!ofproto
->stp
|| !sp
) {
2480 stp_port_get_counts(sp
, &s
->tx_count
, &s
->rx_count
, &s
->error_count
);
2486 stp_run(struct ofproto_dpif
*ofproto
)
2489 long long int now
= time_msec();
2490 long long int elapsed
= now
- ofproto
->stp_last_tick
;
2491 struct stp_port
*sp
;
2494 stp_tick(ofproto
->stp
, MIN(INT_MAX
, elapsed
));
2495 ofproto
->stp_last_tick
= now
;
2497 while (stp_get_changed_port(ofproto
->stp
, &sp
)) {
2498 struct ofport_dpif
*ofport
= stp_port_get_aux(sp
);
2501 update_stp_port_state(ofport
);
2505 if (stp_check_and_reset_fdb_flush(ofproto
->stp
)) {
2506 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2507 mac_learning_flush(ofproto
->ml
);
2508 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2509 mcast_snooping_mdb_flush(ofproto
->ms
);
2515 stp_wait(struct ofproto_dpif
*ofproto
)
2518 poll_timer_wait(1000);
2522 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2523 * caller is responsible for assigning RSTP port numbers and ensuring
2524 * there are no duplicates. */
2526 set_rstp_port(struct ofport
*ofport_
,
2527 const struct ofproto_port_rstp_settings
*s
)
2529 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2530 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2531 struct rstp_port
*rp
= ofport
->rstp_port
;
2533 if (!s
|| !s
->enable
) {
2535 rstp_port_unref(rp
);
2536 ofport
->rstp_port
= NULL
;
2537 update_rstp_port_state(ofport
);
2542 /* Check if need to add a new port. */
2544 rp
= ofport
->rstp_port
= rstp_add_port(ofproto
->rstp
);
2547 rstp_port_set(rp
, s
->port_num
, s
->priority
, s
->path_cost
,
2548 s
->admin_edge_port
, s
->auto_edge
,
2549 s
->admin_p2p_mac_state
, s
->admin_port_state
, s
->mcheck
,
2551 update_rstp_port_state(ofport
);
2552 /* Synchronize operational status. */
2553 rstp_port_set_mac_operational(rp
, ofport
->may_enable
);
2557 get_rstp_port_status(struct ofport
*ofport_
,
2558 struct ofproto_port_rstp_status
*s
)
2560 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2561 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2562 struct rstp_port
*rp
= ofport
->rstp_port
;
2564 if (!ofproto
->rstp
|| !rp
) {
2570 rstp_port_get_status(rp
, &s
->port_id
, &s
->state
, &s
->role
,
2571 &s
->designated_bridge_id
, &s
->designated_port_id
,
2572 &s
->designated_path_cost
, &s
->tx_count
,
2573 &s
->rx_count
, &s
->error_count
, &s
->uptime
);
2578 set_queues(struct ofport
*ofport_
, const struct ofproto_port_queue
*qdscp
,
2581 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2582 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2584 if (ofport
->n_qdscp
!= n_qdscp
2585 || (n_qdscp
&& memcmp(ofport
->qdscp
, qdscp
,
2586 n_qdscp
* sizeof *qdscp
))) {
2587 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2588 free(ofport
->qdscp
);
2589 ofport
->qdscp
= n_qdscp
2590 ? xmemdup(qdscp
, n_qdscp
* sizeof *qdscp
)
2592 ofport
->n_qdscp
= n_qdscp
;
2600 /* Expires all MAC learning entries associated with 'bundle' and forces its
2601 * ofproto to revalidate every flow.
2603 * Normally MAC learning entries are removed only from the ofproto associated
2604 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2605 * are removed from every ofproto. When patch ports and SLB bonds are in use
2606 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2607 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2608 * with the host from which it migrated. */
2610 bundle_flush_macs(struct ofbundle
*bundle
, bool all_ofprotos
)
2612 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2613 struct mac_learning
*ml
= ofproto
->ml
;
2614 struct mac_entry
*mac
, *next_mac
;
2616 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2617 ovs_rwlock_wrlock(&ml
->rwlock
);
2618 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2619 if (mac_entry_get_port(ml
, mac
) == bundle
) {
2621 struct ofproto_dpif
*o
;
2623 HMAP_FOR_EACH (o
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
2625 struct mac_entry
*e
;
2627 ovs_rwlock_wrlock(&o
->ml
->rwlock
);
2628 e
= mac_learning_lookup(o
->ml
, mac
->mac
, mac
->vlan
);
2630 mac_learning_expire(o
->ml
, e
);
2632 ovs_rwlock_unlock(&o
->ml
->rwlock
);
2637 mac_learning_expire(ml
, mac
);
2640 ovs_rwlock_unlock(&ml
->rwlock
);
2644 bundle_move(struct ofbundle
*old
, struct ofbundle
*new)
2646 struct ofproto_dpif
*ofproto
= old
->ofproto
;
2647 struct mac_learning
*ml
= ofproto
->ml
;
2648 struct mac_entry
*mac
, *next_mac
;
2650 ovs_assert(new->ofproto
== old
->ofproto
);
2652 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2653 ovs_rwlock_wrlock(&ml
->rwlock
);
2654 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2655 if (mac_entry_get_port(ml
, mac
) == old
) {
2656 mac_entry_set_port(ml
, mac
, new);
2659 ovs_rwlock_unlock(&ml
->rwlock
);
2662 static struct ofbundle
*
2663 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
2665 struct ofbundle
*bundle
;
2667 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
2668 &ofproto
->bundles
) {
2669 if (bundle
->aux
== aux
) {
2677 bundle_update(struct ofbundle
*bundle
)
2679 struct ofport_dpif
*port
;
2681 bundle
->floodable
= true;
2682 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2683 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2685 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2686 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2687 bundle
->floodable
= false;
2694 bundle_del_port(struct ofport_dpif
*port
)
2696 struct ofbundle
*bundle
= port
->bundle
;
2698 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2700 list_remove(&port
->bundle_node
);
2701 port
->bundle
= NULL
;
2704 lacp_slave_unregister(bundle
->lacp
, port
);
2707 bond_slave_unregister(bundle
->bond
, port
);
2710 bundle_update(bundle
);
2714 bundle_add_port(struct ofbundle
*bundle
, ofp_port_t ofp_port
,
2715 struct lacp_slave_settings
*lacp
)
2717 struct ofport_dpif
*port
;
2719 port
= ofp_port_to_ofport(bundle
->ofproto
, ofp_port
);
2724 if (port
->bundle
!= bundle
) {
2725 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2727 bundle_remove(&port
->up
);
2730 port
->bundle
= bundle
;
2731 list_push_back(&bundle
->ports
, &port
->bundle_node
);
2732 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2734 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2735 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2736 bundle
->floodable
= false;
2740 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2741 lacp_slave_register(bundle
->lacp
, port
, lacp
);
2748 bundle_destroy(struct ofbundle
*bundle
)
2750 struct ofproto_dpif
*ofproto
;
2751 struct ofport_dpif
*port
, *next_port
;
2757 ofproto
= bundle
->ofproto
;
2758 mbridge_unregister_bundle(ofproto
->mbridge
, bundle
);
2761 xlate_bundle_remove(bundle
);
2764 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2765 bundle_del_port(port
);
2768 bundle_flush_macs(bundle
, true);
2769 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
2771 free(bundle
->trunks
);
2772 lacp_unref(bundle
->lacp
);
2773 bond_unref(bundle
->bond
);
2778 bundle_set(struct ofproto
*ofproto_
, void *aux
,
2779 const struct ofproto_bundle_settings
*s
)
2781 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2782 bool need_flush
= false;
2783 struct ofport_dpif
*port
;
2784 struct ofbundle
*bundle
;
2785 unsigned long *trunks
;
2791 bundle_destroy(bundle_lookup(ofproto
, aux
));
2795 ovs_assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
2796 ovs_assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
2798 bundle
= bundle_lookup(ofproto
, aux
);
2800 bundle
= xmalloc(sizeof *bundle
);
2802 bundle
->ofproto
= ofproto
;
2803 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
2804 hash_pointer(aux
, 0));
2806 bundle
->name
= NULL
;
2808 list_init(&bundle
->ports
);
2809 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
2811 bundle
->trunks
= NULL
;
2812 bundle
->use_priority_tags
= s
->use_priority_tags
;
2813 bundle
->lacp
= NULL
;
2814 bundle
->bond
= NULL
;
2816 bundle
->floodable
= true;
2817 mbridge_register_bundle(ofproto
->mbridge
, bundle
);
2820 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
2822 bundle
->name
= xstrdup(s
->name
);
2827 ofproto
->lacp_enabled
= true;
2828 if (!bundle
->lacp
) {
2829 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2830 bundle
->lacp
= lacp_create();
2832 lacp_configure(bundle
->lacp
, s
->lacp
);
2834 lacp_unref(bundle
->lacp
);
2835 bundle
->lacp
= NULL
;
2838 /* Update set of ports. */
2840 for (i
= 0; i
< s
->n_slaves
; i
++) {
2841 if (!bundle_add_port(bundle
, s
->slaves
[i
],
2842 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
)) {
2846 if (!ok
|| list_size(&bundle
->ports
) != s
->n_slaves
) {
2847 struct ofport_dpif
*next_port
;
2849 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2850 for (i
= 0; i
< s
->n_slaves
; i
++) {
2851 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
2856 bundle_del_port(port
);
2860 ovs_assert(list_size(&bundle
->ports
) <= s
->n_slaves
);
2862 if (list_is_empty(&bundle
->ports
)) {
2863 bundle_destroy(bundle
);
2867 /* Set VLAN tagging mode */
2868 if (s
->vlan_mode
!= bundle
->vlan_mode
2869 || s
->use_priority_tags
!= bundle
->use_priority_tags
) {
2870 bundle
->vlan_mode
= s
->vlan_mode
;
2871 bundle
->use_priority_tags
= s
->use_priority_tags
;
2876 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
2877 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
2879 if (vlan
!= bundle
->vlan
) {
2880 bundle
->vlan
= vlan
;
2884 /* Get trunked VLANs. */
2885 switch (s
->vlan_mode
) {
2886 case PORT_VLAN_ACCESS
:
2890 case PORT_VLAN_TRUNK
:
2891 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
2894 case PORT_VLAN_NATIVE_UNTAGGED
:
2895 case PORT_VLAN_NATIVE_TAGGED
:
2896 if (vlan
!= 0 && (!s
->trunks
2897 || !bitmap_is_set(s
->trunks
, vlan
)
2898 || bitmap_is_set(s
->trunks
, 0))) {
2899 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2901 trunks
= bitmap_clone(s
->trunks
, 4096);
2903 trunks
= bitmap_allocate1(4096);
2905 bitmap_set1(trunks
, vlan
);
2906 bitmap_set0(trunks
, 0);
2908 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
2915 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
2916 free(bundle
->trunks
);
2917 if (trunks
== s
->trunks
) {
2918 bundle
->trunks
= vlan_bitmap_clone(trunks
);
2920 bundle
->trunks
= trunks
;
2925 if (trunks
!= s
->trunks
) {
2930 if (!list_is_short(&bundle
->ports
)) {
2931 bundle
->ofproto
->has_bonded_bundles
= true;
2933 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
2934 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2937 bundle
->bond
= bond_create(s
->bond
, ofproto
);
2938 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2941 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2942 bond_slave_register(bundle
->bond
, port
,
2943 port
->up
.ofp_port
, port
->up
.netdev
);
2946 bond_unref(bundle
->bond
);
2947 bundle
->bond
= NULL
;
2950 /* If we changed something that would affect MAC learning, un-learn
2951 * everything on this port and force flow revalidation. */
2953 bundle_flush_macs(bundle
, false);
2960 bundle_remove(struct ofport
*port_
)
2962 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
2963 struct ofbundle
*bundle
= port
->bundle
;
2966 bundle_del_port(port
);
2967 if (list_is_empty(&bundle
->ports
)) {
2968 bundle_destroy(bundle
);
2969 } else if (list_is_short(&bundle
->ports
)) {
2970 bond_unref(bundle
->bond
);
2971 bundle
->bond
= NULL
;
2977 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
2979 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
2980 struct ofport_dpif
*port
= port_
;
2981 uint8_t ea
[ETH_ADDR_LEN
];
2984 error
= netdev_get_etheraddr(port
->up
.netdev
, ea
);
2986 struct dp_packet packet
;
2989 dp_packet_init(&packet
, 0);
2990 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
2992 memcpy(packet_pdu
, pdu
, pdu_size
);
2994 ofproto_dpif_send_packet(port
, &packet
);
2995 dp_packet_uninit(&packet
);
2997 VLOG_ERR_RL(&rl
, "port %s: cannot obtain Ethernet address of iface "
2998 "%s (%s)", port
->bundle
->name
,
2999 netdev_get_name(port
->up
.netdev
), ovs_strerror(error
));
3004 bundle_send_learning_packets(struct ofbundle
*bundle
)
3006 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
3007 int error
, n_packets
, n_errors
;
3008 struct mac_entry
*e
;
3010 struct ovs_list list_node
;
3011 struct ofport_dpif
*port
;
3012 struct dp_packet
*pkt
;
3014 struct ovs_list packets
;
3016 list_init(&packets
);
3017 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
3018 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
3019 if (mac_entry_get_port(ofproto
->ml
, e
) != bundle
) {
3020 pkt_node
= xmalloc(sizeof *pkt_node
);
3021 pkt_node
->pkt
= bond_compose_learning_packet(bundle
->bond
,
3023 (void **)&pkt_node
->port
);
3024 list_push_back(&packets
, &pkt_node
->list_node
);
3027 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3029 error
= n_packets
= n_errors
= 0;
3030 LIST_FOR_EACH_POP (pkt_node
, list_node
, &packets
) {
3033 ret
= ofproto_dpif_send_packet(pkt_node
->port
, pkt_node
->pkt
);
3034 dp_packet_delete(pkt_node
->pkt
);
3044 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3045 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
3046 "packets, last error was: %s",
3047 bundle
->name
, n_errors
, n_packets
, ovs_strerror(error
));
3049 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3050 bundle
->name
, n_packets
);
3055 bundle_run(struct ofbundle
*bundle
)
3058 lacp_run(bundle
->lacp
, send_pdu_cb
);
3061 struct ofport_dpif
*port
;
3063 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3064 bond_slave_set_may_enable(bundle
->bond
, port
, port
->may_enable
);
3067 if (bond_run(bundle
->bond
, lacp_status(bundle
->lacp
))) {
3068 bundle
->ofproto
->backer
->need_revalidate
= REV_BOND
;
3071 if (bond_should_send_learning_packets(bundle
->bond
)) {
3072 bundle_send_learning_packets(bundle
);
3078 bundle_wait(struct ofbundle
*bundle
)
3081 lacp_wait(bundle
->lacp
);
3084 bond_wait(bundle
->bond
);
3091 mirror_set__(struct ofproto
*ofproto_
, void *aux
,
3092 const struct ofproto_mirror_settings
*s
)
3094 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3095 struct ofbundle
**srcs
, **dsts
;
3100 mirror_destroy(ofproto
->mbridge
, aux
);
3104 srcs
= xmalloc(s
->n_srcs
* sizeof *srcs
);
3105 dsts
= xmalloc(s
->n_dsts
* sizeof *dsts
);
3107 for (i
= 0; i
< s
->n_srcs
; i
++) {
3108 srcs
[i
] = bundle_lookup(ofproto
, s
->srcs
[i
]);
3111 for (i
= 0; i
< s
->n_dsts
; i
++) {
3112 dsts
[i
] = bundle_lookup(ofproto
, s
->dsts
[i
]);
3115 error
= mirror_set(ofproto
->mbridge
, aux
, s
->name
, srcs
, s
->n_srcs
, dsts
,
3116 s
->n_dsts
, s
->src_vlans
,
3117 bundle_lookup(ofproto
, s
->out_bundle
), s
->out_vlan
);
3124 mirror_get_stats__(struct ofproto
*ofproto
, void *aux
,
3125 uint64_t *packets
, uint64_t *bytes
)
3127 return mirror_get_stats(ofproto_dpif_cast(ofproto
)->mbridge
, aux
, packets
,
3132 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
3134 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3135 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3136 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
3137 mac_learning_flush(ofproto
->ml
);
3139 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3144 is_mirror_output_bundle(const struct ofproto
*ofproto_
, void *aux
)
3146 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3147 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3148 return bundle
&& mirror_bundle_out(ofproto
->mbridge
, bundle
) != 0;
3152 forward_bpdu_changed(struct ofproto
*ofproto_
)
3154 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3155 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3159 set_mac_table_config(struct ofproto
*ofproto_
, unsigned int idle_time
,
3162 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3163 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3164 mac_learning_set_idle_time(ofproto
->ml
, idle_time
);
3165 mac_learning_set_max_entries(ofproto
->ml
, max_entries
);
3166 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3169 /* Configures multicast snooping on 'ofport' using the settings
3170 * defined in 's'. */
3172 set_mcast_snooping(struct ofproto
*ofproto_
,
3173 const struct ofproto_mcast_snooping_settings
*s
)
3175 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3177 /* Only revalidate flows if the configuration changed. */
3178 if (!s
!= !ofproto
->ms
) {
3179 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3184 ofproto
->ms
= mcast_snooping_create();
3187 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3188 mcast_snooping_set_idle_time(ofproto
->ms
, s
->idle_time
);
3189 mcast_snooping_set_max_entries(ofproto
->ms
, s
->max_entries
);
3190 if (mcast_snooping_set_flood_unreg(ofproto
->ms
, s
->flood_unreg
)) {
3191 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3193 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3195 mcast_snooping_unref(ofproto
->ms
);
3202 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3204 set_mcast_snooping_port(struct ofproto
*ofproto_
, void *aux
,
3205 const struct ofproto_mcast_snooping_port_settings
*s
)
3207 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3208 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3210 if (ofproto
->ms
&& s
) {
3211 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3212 mcast_snooping_set_port_flood(ofproto
->ms
, bundle
, s
->flood
);
3213 mcast_snooping_set_port_flood_reports(ofproto
->ms
, bundle
,
3215 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3223 struct ofport_dpif
*
3224 ofp_port_to_ofport(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
3226 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
3227 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
3231 ofproto_port_from_dpif_port(struct ofproto_dpif
*ofproto
,
3232 struct ofproto_port
*ofproto_port
,
3233 struct dpif_port
*dpif_port
)
3235 ofproto_port
->name
= dpif_port
->name
;
3236 ofproto_port
->type
= dpif_port
->type
;
3237 ofproto_port
->ofp_port
= odp_port_to_ofp_port(ofproto
, dpif_port
->port_no
);
3241 ofport_update_peer(struct ofport_dpif
*ofport
)
3243 const struct ofproto_dpif
*ofproto
;
3244 struct dpif_backer
*backer
;
3247 if (!netdev_vport_is_patch(ofport
->up
.netdev
)) {
3251 backer
= ofproto_dpif_cast(ofport
->up
.ofproto
)->backer
;
3252 backer
->need_revalidate
= REV_RECONFIGURE
;
3255 ofport
->peer
->peer
= NULL
;
3256 ofport
->peer
= NULL
;
3259 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3264 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
3265 struct ofport
*peer_ofport
;
3266 struct ofport_dpif
*peer
;
3269 if (ofproto
->backer
!= backer
) {
3273 peer_ofport
= shash_find_data(&ofproto
->up
.port_by_name
, peer_name
);
3278 peer
= ofport_dpif_cast(peer_ofport
);
3279 peer_peer
= netdev_vport_patch_peer(peer
->up
.netdev
);
3280 if (peer_peer
&& !strcmp(netdev_get_name(ofport
->up
.netdev
),
3282 ofport
->peer
= peer
;
3283 ofport
->peer
->peer
= ofport
;
3293 port_run(struct ofport_dpif
*ofport
)
3295 long long int carrier_seq
= netdev_get_carrier_resets(ofport
->up
.netdev
);
3296 bool carrier_changed
= carrier_seq
!= ofport
->carrier_seq
;
3297 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
3298 bool cfm_enable
= false;
3299 bool bfd_enable
= false;
3301 ofport
->carrier_seq
= carrier_seq
;
3304 int cfm_opup
= cfm_get_opup(ofport
->cfm
);
3306 cfm_enable
= !cfm_get_fault(ofport
->cfm
);
3308 if (cfm_opup
>= 0) {
3309 cfm_enable
= cfm_enable
&& cfm_opup
;
3314 bfd_enable
= bfd_forwarding(ofport
->bfd
);
3317 if (ofport
->bfd
|| ofport
->cfm
) {
3318 enable
= enable
&& (cfm_enable
|| bfd_enable
);
3321 if (ofport
->bundle
) {
3322 enable
= enable
&& lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
);
3323 if (carrier_changed
) {
3324 lacp_slave_carrier_changed(ofport
->bundle
->lacp
, ofport
);
3328 if (ofport
->may_enable
!= enable
) {
3329 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3331 ofproto
->backer
->need_revalidate
= REV_PORT_TOGGLED
;
3333 if (ofport
->rstp_port
) {
3334 rstp_port_set_mac_operational(ofport
->rstp_port
, enable
);
3338 ofport
->may_enable
= enable
;
3342 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
3343 struct ofproto_port
*ofproto_port
)
3345 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3346 struct dpif_port dpif_port
;
3349 if (sset_contains(&ofproto
->ghost_ports
, devname
)) {
3350 const char *type
= netdev_get_type_from_name(devname
);
3352 /* We may be called before ofproto->up.port_by_name is populated with
3353 * the appropriate ofport. For this reason, we must get the name and
3354 * type from the netdev layer directly. */
3356 const struct ofport
*ofport
;
3358 ofport
= shash_find_data(&ofproto
->up
.port_by_name
, devname
);
3359 ofproto_port
->ofp_port
= ofport
? ofport
->ofp_port
: OFPP_NONE
;
3360 ofproto_port
->name
= xstrdup(devname
);
3361 ofproto_port
->type
= xstrdup(type
);
3367 if (!sset_contains(&ofproto
->ports
, devname
)) {
3370 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
,
3371 devname
, &dpif_port
);
3373 ofproto_port_from_dpif_port(ofproto
, ofproto_port
, &dpif_port
);
3379 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
)
3381 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3382 const char *devname
= netdev_get_name(netdev
);
3383 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
3384 const char *dp_port_name
;
3386 if (netdev_vport_is_patch(netdev
)) {
3387 sset_add(&ofproto
->ghost_ports
, netdev_get_name(netdev
));
3391 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
3392 if (!dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
3393 odp_port_t port_no
= ODPP_NONE
;
3396 error
= dpif_port_add(ofproto
->backer
->dpif
, netdev
, &port_no
);
3400 if (netdev_get_tunnel_config(netdev
)) {
3401 simap_put(&ofproto
->backer
->tnl_backers
,
3402 dp_port_name
, odp_to_u32(port_no
));
3406 if (netdev_get_tunnel_config(netdev
)) {
3407 sset_add(&ofproto
->ghost_ports
, devname
);
3409 sset_add(&ofproto
->ports
, devname
);
3415 port_del(struct ofproto
*ofproto_
, ofp_port_t ofp_port
)
3417 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3418 struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
3425 sset_find_and_delete(&ofproto
->ghost_ports
,
3426 netdev_get_name(ofport
->up
.netdev
));
3427 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3428 if (!ofport
->is_tunnel
&& !netdev_vport_is_patch(ofport
->up
.netdev
)) {
3429 error
= dpif_port_del(ofproto
->backer
->dpif
, ofport
->odp_port
);
3431 /* The caller is going to close ofport->up.netdev. If this is a
3432 * bonded port, then the bond is using that netdev, so remove it
3433 * from the bond. The client will need to reconfigure everything
3434 * after deleting ports, so then the slave will get re-added. */
3435 bundle_remove(&ofport
->up
);
3442 port_get_stats(const struct ofport
*ofport_
, struct netdev_stats
*stats
)
3444 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3447 error
= netdev_get_stats(ofport
->up
.netdev
, stats
);
3449 if (!error
&& ofport_
->ofp_port
== OFPP_LOCAL
) {
3450 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3452 ovs_mutex_lock(&ofproto
->stats_mutex
);
3453 /* ofproto->stats.tx_packets represents packets that we created
3454 * internally and sent to some port (e.g. packets sent with
3455 * ofproto_dpif_send_packet()). Account for them as if they had
3456 * come from OFPP_LOCAL and got forwarded. */
3458 if (stats
->rx_packets
!= UINT64_MAX
) {
3459 stats
->rx_packets
+= ofproto
->stats
.tx_packets
;
3462 if (stats
->rx_bytes
!= UINT64_MAX
) {
3463 stats
->rx_bytes
+= ofproto
->stats
.tx_bytes
;
3466 /* ofproto->stats.rx_packets represents packets that were received on
3467 * some port and we processed internally and dropped (e.g. STP).
3468 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3470 if (stats
->tx_packets
!= UINT64_MAX
) {
3471 stats
->tx_packets
+= ofproto
->stats
.rx_packets
;
3474 if (stats
->tx_bytes
!= UINT64_MAX
) {
3475 stats
->tx_bytes
+= ofproto
->stats
.rx_bytes
;
3477 ovs_mutex_unlock(&ofproto
->stats_mutex
);
3484 port_get_lacp_stats(const struct ofport
*ofport_
, struct lacp_slave_stats
*stats
)
3486 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3487 if (ofport
->bundle
&& ofport
->bundle
->lacp
) {
3488 if (lacp_get_slave_stats(ofport
->bundle
->lacp
, ofport
, stats
)) {
3495 struct port_dump_state
{
3500 struct ofproto_port port
;
3505 port_dump_start(const struct ofproto
*ofproto_ OVS_UNUSED
, void **statep
)
3507 *statep
= xzalloc(sizeof(struct port_dump_state
));
3512 port_dump_next(const struct ofproto
*ofproto_
, void *state_
,
3513 struct ofproto_port
*port
)
3515 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3516 struct port_dump_state
*state
= state_
;
3517 const struct sset
*sset
;
3518 struct sset_node
*node
;
3520 if (state
->has_port
) {
3521 ofproto_port_destroy(&state
->port
);
3522 state
->has_port
= false;
3524 sset
= state
->ghost
? &ofproto
->ghost_ports
: &ofproto
->ports
;
3525 while ((node
= sset_at_position(sset
, &state
->bucket
, &state
->offset
))) {
3528 error
= port_query_by_name(ofproto_
, node
->name
, &state
->port
);
3530 *port
= state
->port
;
3531 state
->has_port
= true;
3533 } else if (error
!= ENODEV
) {
3538 if (!state
->ghost
) {
3539 state
->ghost
= true;
3542 return port_dump_next(ofproto_
, state_
, port
);
3549 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
3551 struct port_dump_state
*state
= state_
;
3553 if (state
->has_port
) {
3554 ofproto_port_destroy(&state
->port
);
3561 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
3563 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3565 if (ofproto
->port_poll_errno
) {
3566 int error
= ofproto
->port_poll_errno
;
3567 ofproto
->port_poll_errno
= 0;
3571 if (sset_is_empty(&ofproto
->port_poll_set
)) {
3575 *devnamep
= sset_pop(&ofproto
->port_poll_set
);
3580 port_poll_wait(const struct ofproto
*ofproto_
)
3582 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3583 dpif_port_poll_wait(ofproto
->backer
->dpif
);
3587 port_is_lacp_current(const struct ofport
*ofport_
)
3589 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3590 return (ofport
->bundle
&& ofport
->bundle
->lacp
3591 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
3595 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3596 * then delete it entirely. */
3598 rule_expire(struct rule_dpif
*rule
)
3599 OVS_REQUIRES(ofproto_mutex
)
3601 uint16_t hard_timeout
, idle_timeout
;
3602 long long int now
= time_msec();
3605 hard_timeout
= rule
->up
.hard_timeout
;
3606 idle_timeout
= rule
->up
.idle_timeout
;
3608 /* Has 'rule' expired? */
3610 long long int modified
;
3612 ovs_mutex_lock(&rule
->up
.mutex
);
3613 modified
= rule
->up
.modified
;
3614 ovs_mutex_unlock(&rule
->up
.mutex
);
3616 if (now
> modified
+ hard_timeout
* 1000) {
3617 reason
= OFPRR_HARD_TIMEOUT
;
3621 if (reason
< 0 && idle_timeout
) {
3624 ovs_mutex_lock(&rule
->stats_mutex
);
3625 used
= rule
->stats
.used
;
3626 ovs_mutex_unlock(&rule
->stats_mutex
);
3628 if (now
> used
+ idle_timeout
* 1000) {
3629 reason
= OFPRR_IDLE_TIMEOUT
;
3634 COVERAGE_INC(ofproto_dpif_expired
);
3635 ofproto_rule_expire(&rule
->up
, reason
);
3639 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3640 * 'flow' must reflect the data in 'packet'. */
3642 ofproto_dpif_execute_actions(struct ofproto_dpif
*ofproto
,
3643 const struct flow
*flow
,
3644 struct rule_dpif
*rule
,
3645 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3646 struct dp_packet
*packet
)
3648 struct dpif_flow_stats stats
;
3649 struct xlate_out xout
;
3650 struct xlate_in xin
;
3652 struct dpif_execute execute
;
3655 ovs_assert((rule
!= NULL
) != (ofpacts
!= NULL
));
3657 dpif_flow_stats_extract(flow
, packet
, time_msec(), &stats
);
3660 rule_dpif_credit_stats(rule
, &stats
);
3663 uint64_t odp_actions_stub
[1024 / 8];
3664 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
3665 xlate_in_init(&xin
, ofproto
, flow
, flow
->in_port
.ofp_port
, rule
,
3666 stats
.tcp_flags
, packet
, NULL
, &odp_actions
);
3667 xin
.ofpacts
= ofpacts
;
3668 xin
.ofpacts_len
= ofpacts_len
;
3669 xin
.resubmit_stats
= &stats
;
3670 xlate_actions(&xin
, &xout
);
3672 execute
.actions
= odp_actions
.data
;
3673 execute
.actions_len
= odp_actions
.size
;
3675 pkt_metadata_from_flow(&packet
->md
, flow
);
3676 execute
.packet
= packet
;
3677 execute
.needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
3678 execute
.probe
= false;
3680 /* Fix up in_port. */
3681 in_port
= flow
->in_port
.ofp_port
;
3682 if (in_port
== OFPP_NONE
) {
3683 in_port
= OFPP_LOCAL
;
3685 execute
.packet
->md
.in_port
.odp_port
= ofp_port_to_odp_port(ofproto
, in_port
);
3687 error
= dpif_execute(ofproto
->backer
->dpif
, &execute
);
3689 xlate_out_uninit(&xout
);
3690 ofpbuf_uninit(&odp_actions
);
3696 rule_dpif_credit_stats(struct rule_dpif
*rule
,
3697 const struct dpif_flow_stats
*stats
)
3699 ovs_mutex_lock(&rule
->stats_mutex
);
3700 if (OVS_UNLIKELY(rule
->new_rule
)) {
3701 rule_dpif_credit_stats(rule
->new_rule
, stats
);
3703 rule
->stats
.n_packets
+= stats
->n_packets
;
3704 rule
->stats
.n_bytes
+= stats
->n_bytes
;
3705 rule
->stats
.used
= MAX(rule
->stats
.used
, stats
->used
);
3707 ovs_mutex_unlock(&rule
->stats_mutex
);
3711 rule_dpif_get_flow_cookie(const struct rule_dpif
*rule
)
3712 OVS_REQUIRES(rule
->up
.mutex
)
3714 return rule
->up
.flow_cookie
;
3718 rule_dpif_reduce_timeouts(struct rule_dpif
*rule
, uint16_t idle_timeout
,
3719 uint16_t hard_timeout
)
3721 ofproto_rule_reduce_timeouts(&rule
->up
, idle_timeout
, hard_timeout
);
3724 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3725 * be read until the calling thread quiesces. */
3726 const struct rule_actions
*
3727 rule_dpif_get_actions(const struct rule_dpif
*rule
)
3729 return rule_get_actions(&rule
->up
);
3732 /* Sets 'rule''s recirculation id. */
3734 rule_dpif_set_recirc_id(struct rule_dpif
*rule
, uint32_t id
)
3735 OVS_REQUIRES(rule
->up
.mutex
)
3737 ovs_assert(!rule
->recirc_id
|| rule
->recirc_id
== id
);
3738 if (rule
->recirc_id
== id
) {
3739 /* Release the new reference to the same id. */
3742 rule
->recirc_id
= id
;
3746 /* Sets 'rule''s recirculation id. */
3748 rule_set_recirc_id(struct rule
*rule_
, uint32_t id
)
3750 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3752 ovs_mutex_lock(&rule
->up
.mutex
);
3753 rule_dpif_set_recirc_id(rule
, id
);
3754 ovs_mutex_unlock(&rule
->up
.mutex
);
3758 ofproto_dpif_get_tables_version(struct ofproto_dpif
*ofproto OVS_UNUSED
)
3760 cls_version_t version
;
3762 atomic_read_relaxed(&ofproto
->tables_version
, &version
);
3767 /* The returned rule (if any) is valid at least until the next RCU quiescent
3768 * period. If the rule needs to stay around longer, a non-zero 'take_ref'
3769 * must be passed in to cause a reference to be taken on it.
3771 * 'flow' is non-const to allow for temporary modifications during the lookup.
3772 * Any changes are restored before returning. */
3773 static struct rule_dpif
*
3774 rule_dpif_lookup_in_table(struct ofproto_dpif
*ofproto
, cls_version_t version
,
3775 uint8_t table_id
, struct flow
*flow
,
3776 struct flow_wildcards
*wc
, bool take_ref
)
3778 struct classifier
*cls
= &ofproto
->up
.tables
[table_id
].cls
;
3779 const struct cls_rule
*cls_rule
;
3780 struct rule_dpif
*rule
;
3783 cls_rule
= classifier_lookup(cls
, version
, flow
, wc
);
3785 rule
= rule_dpif_cast(rule_from_cls_rule(cls_rule
));
3787 /* Try again if the rule was released before we get the reference. */
3788 } while (rule
&& take_ref
&& !rule_dpif_try_ref(rule
));
3793 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3794 * table '*table_id'. Returns the rule that was found, which may be one of the
3795 * special rules according to packet miss hadling. If 'may_packet_in' is
3796 * false, returning of the miss_rule (which issues packet ins for the
3797 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3798 * that were used during the lookup.
3800 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3801 * if none is found then the table miss configuration for that table is
3802 * honored, which can result in additional lookups in other OpenFlow tables.
3803 * In this case the function updates '*table_id' to reflect the final OpenFlow
3804 * table that was searched.
3806 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3809 * The rule is returned in '*rule', which is valid at least until the next
3810 * RCU quiescent period. If the '*rule' needs to stay around longer,
3811 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3812 * on it before this returns.
3814 * 'in_port' allows the lookup to take place as if the in port had the value
3815 * 'in_port'. This is needed for resubmit action support.
3817 * 'flow' is non-const to allow for temporary modifications during the lookup.
3818 * Any changes are restored before returning. */
3820 rule_dpif_lookup_from_table(struct ofproto_dpif
*ofproto
,
3821 cls_version_t version
, struct flow
*flow
,
3822 struct flow_wildcards
*wc
, bool take_ref
,
3823 const struct dpif_flow_stats
*stats
,
3824 uint8_t *table_id
, ofp_port_t in_port
,
3825 bool may_packet_in
, bool honor_table_miss
)
3827 ovs_be16 old_tp_src
= flow
->tp_src
, old_tp_dst
= flow
->tp_dst
;
3828 ofp_port_t old_in_port
= flow
->in_port
.ofp_port
;
3829 enum ofputil_table_miss miss_config
;
3830 struct rule_dpif
*rule
;
3833 /* We always unwildcard nw_frag (for IP), so they
3834 * need not be unwildcarded here. */
3835 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
3836 && ofproto
->up
.frag_handling
!= OFPC_FRAG_NX_MATCH
) {
3837 if (ofproto
->up
.frag_handling
== OFPC_FRAG_NORMAL
) {
3838 /* We must pretend that transport ports are unavailable. */
3839 flow
->tp_src
= htons(0);
3840 flow
->tp_dst
= htons(0);
3842 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3843 * Use the drop_frags_rule (which cannot disappear). */
3844 rule
= ofproto
->drop_frags_rule
;
3846 rule_dpif_ref(rule
);
3849 struct oftable
*tbl
= &ofproto
->up
.tables
[*table_id
];
3852 atomic_add_relaxed(&tbl
->n_matched
, stats
->n_packets
, &orig
);
3858 /* Look up a flow with 'in_port' as the input port. Then restore the
3859 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3860 * have surprising behavior). */
3861 flow
->in_port
.ofp_port
= in_port
;
3863 /* Our current implementation depends on n_tables == N_TABLES, and
3864 * TBL_INTERNAL being the last table. */
3865 BUILD_ASSERT_DECL(N_TABLES
== TBL_INTERNAL
+ 1);
3867 miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
3869 for (next_id
= *table_id
;
3870 next_id
< ofproto
->up
.n_tables
;
3871 next_id
++, next_id
+= (next_id
== TBL_INTERNAL
))
3873 *table_id
= next_id
;
3874 rule
= rule_dpif_lookup_in_table(ofproto
, version
, next_id
, flow
, wc
,
3877 struct oftable
*tbl
= &ofproto
->up
.tables
[next_id
];
3880 atomic_add_relaxed(rule
? &tbl
->n_matched
: &tbl
->n_missed
,
3881 stats
->n_packets
, &orig
);
3884 goto out
; /* Match. */
3886 if (honor_table_miss
) {
3887 miss_config
= ofproto_table_get_miss_config(&ofproto
->up
,
3889 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
3896 rule
= ofproto
->no_packet_in_rule
;
3897 if (may_packet_in
) {
3898 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
3899 || miss_config
== OFPUTIL_TABLE_MISS_CONTROLLER
) {
3900 struct ofport_dpif
*port
;
3902 port
= ofp_port_to_ofport(ofproto
, old_in_port
);
3904 VLOG_WARN_RL(&rl
, "packet-in on unknown OpenFlow port %"PRIu16
,
3906 } else if (!(port
->up
.pp
.config
& OFPUTIL_PC_NO_PACKET_IN
)) {
3907 rule
= ofproto
->miss_rule
;
3909 } else if (miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
&&
3910 connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
)) {
3911 rule
= ofproto
->miss_rule
;
3915 rule_dpif_ref(rule
);
3918 /* Restore port numbers, as they may have been modified above. */
3919 flow
->tp_src
= old_tp_src
;
3920 flow
->tp_dst
= old_tp_dst
;
3921 /* Restore the old in port. */
3922 flow
->in_port
.ofp_port
= old_in_port
;
3928 complete_operation(struct rule_dpif
*rule
)
3929 OVS_REQUIRES(ofproto_mutex
)
3931 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
3933 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
3936 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
3938 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
3941 static struct rule
*
3944 struct rule_dpif
*rule
= xzalloc(sizeof *rule
);
3949 rule_dealloc(struct rule
*rule_
)
3951 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3956 rule_construct(struct rule
*rule_
)
3957 OVS_NO_THREAD_SAFETY_ANALYSIS
3959 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3960 ovs_mutex_init_adaptive(&rule
->stats_mutex
);
3961 rule
->stats
.n_packets
= 0;
3962 rule
->stats
.n_bytes
= 0;
3963 rule
->stats
.used
= rule
->up
.modified
;
3964 rule
->recirc_id
= 0;
3965 rule
->new_rule
= NULL
;
3971 rule_insert(struct rule
*rule_
, struct rule
*old_rule_
, bool forward_stats
)
3972 OVS_REQUIRES(ofproto_mutex
)
3974 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3976 if (old_rule_
&& forward_stats
) {
3977 struct rule_dpif
*old_rule
= rule_dpif_cast(old_rule_
);
3979 ovs_assert(!old_rule
->new_rule
);
3981 /* Take a reference to the new rule, and refer all stats updates from
3982 * the old rule to the new rule. */
3983 rule_dpif_ref(rule
);
3985 ovs_mutex_lock(&old_rule
->stats_mutex
);
3986 ovs_mutex_lock(&rule
->stats_mutex
);
3987 old_rule
->new_rule
= rule
; /* Forward future stats. */
3988 rule
->stats
= old_rule
->stats
; /* Transfer stats to the new rule. */
3989 ovs_mutex_unlock(&rule
->stats_mutex
);
3990 ovs_mutex_unlock(&old_rule
->stats_mutex
);
3993 complete_operation(rule
);
3997 rule_delete(struct rule
*rule_
)
3998 OVS_REQUIRES(ofproto_mutex
)
4000 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4001 complete_operation(rule
);
4005 rule_destruct(struct rule
*rule_
)
4006 OVS_NO_THREAD_SAFETY_ANALYSIS
4008 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4010 ovs_mutex_destroy(&rule
->stats_mutex
);
4011 /* Release reference to the new rule, if any. */
4012 if (rule
->new_rule
) {
4013 rule_dpif_unref(rule
->new_rule
);
4015 if (rule
->recirc_id
) {
4016 recirc_free_id(rule
->recirc_id
);
4021 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
,
4022 long long int *used
)
4024 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4026 ovs_mutex_lock(&rule
->stats_mutex
);
4027 if (OVS_UNLIKELY(rule
->new_rule
)) {
4028 rule_get_stats(&rule
->new_rule
->up
, packets
, bytes
, used
);
4030 *packets
= rule
->stats
.n_packets
;
4031 *bytes
= rule
->stats
.n_bytes
;
4032 *used
= rule
->stats
.used
;
4034 ovs_mutex_unlock(&rule
->stats_mutex
);
4038 rule_dpif_execute(struct rule_dpif
*rule
, const struct flow
*flow
,
4039 struct dp_packet
*packet
)
4041 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->up
.ofproto
);
4043 ofproto_dpif_execute_actions(ofproto
, flow
, rule
, NULL
, 0, packet
);
4047 rule_execute(struct rule
*rule
, const struct flow
*flow
,
4048 struct dp_packet
*packet
)
4050 rule_dpif_execute(rule_dpif_cast(rule
), flow
, packet
);
4051 dp_packet_delete(packet
);
4055 static struct group_dpif
*group_dpif_cast(const struct ofgroup
*group
)
4057 return group
? CONTAINER_OF(group
, struct group_dpif
, up
) : NULL
;
4060 static struct ofgroup
*
4063 struct group_dpif
*group
= xzalloc(sizeof *group
);
4068 group_dealloc(struct ofgroup
*group_
)
4070 struct group_dpif
*group
= group_dpif_cast(group_
);
4075 group_construct_stats(struct group_dpif
*group
)
4076 OVS_REQUIRES(group
->stats_mutex
)
4078 struct ofputil_bucket
*bucket
;
4079 const struct ovs_list
*buckets
;
4081 group
->packet_count
= 0;
4082 group
->byte_count
= 0;
4084 group_dpif_get_buckets(group
, &buckets
);
4085 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4086 bucket
->stats
.packet_count
= 0;
4087 bucket
->stats
.byte_count
= 0;
4092 group_dpif_credit_stats(struct group_dpif
*group
,
4093 struct ofputil_bucket
*bucket
,
4094 const struct dpif_flow_stats
*stats
)
4096 ovs_mutex_lock(&group
->stats_mutex
);
4097 group
->packet_count
+= stats
->n_packets
;
4098 group
->byte_count
+= stats
->n_bytes
;
4100 bucket
->stats
.packet_count
+= stats
->n_packets
;
4101 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4102 } else { /* Credit to all buckets */
4103 const struct ovs_list
*buckets
;
4105 group_dpif_get_buckets(group
, &buckets
);
4106 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4107 bucket
->stats
.packet_count
+= stats
->n_packets
;
4108 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4111 ovs_mutex_unlock(&group
->stats_mutex
);
4115 group_construct(struct ofgroup
*group_
)
4117 struct group_dpif
*group
= group_dpif_cast(group_
);
4119 ovs_mutex_init_adaptive(&group
->stats_mutex
);
4120 ovs_mutex_lock(&group
->stats_mutex
);
4121 group_construct_stats(group
);
4122 ovs_mutex_unlock(&group
->stats_mutex
);
4127 group_destruct(struct ofgroup
*group_
)
4129 struct group_dpif
*group
= group_dpif_cast(group_
);
4130 ovs_mutex_destroy(&group
->stats_mutex
);
4134 group_modify(struct ofgroup
*group_
)
4136 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(group_
->ofproto
);
4138 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
4144 group_get_stats(const struct ofgroup
*group_
, struct ofputil_group_stats
*ogs
)
4146 struct group_dpif
*group
= group_dpif_cast(group_
);
4147 struct ofputil_bucket
*bucket
;
4148 const struct ovs_list
*buckets
;
4149 struct bucket_counter
*bucket_stats
;
4151 ovs_mutex_lock(&group
->stats_mutex
);
4152 ogs
->packet_count
= group
->packet_count
;
4153 ogs
->byte_count
= group
->byte_count
;
4155 group_dpif_get_buckets(group
, &buckets
);
4156 bucket_stats
= ogs
->bucket_stats
;
4157 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
4158 bucket_stats
->packet_count
= bucket
->stats
.packet_count
;
4159 bucket_stats
->byte_count
= bucket
->stats
.byte_count
;
4162 ovs_mutex_unlock(&group
->stats_mutex
);
4167 /* If the group exists, this function increments the groups's reference count.
4169 * Make sure to call group_dpif_unref() after no longer needing to maintain
4170 * a reference to the group. */
4172 group_dpif_lookup(struct ofproto_dpif
*ofproto
, uint32_t group_id
,
4173 struct group_dpif
**group
)
4175 struct ofgroup
*ofgroup
;
4178 found
= ofproto_group_lookup(&ofproto
->up
, group_id
, &ofgroup
);
4179 *group
= found
? group_dpif_cast(ofgroup
) : NULL
;
4185 group_dpif_get_buckets(const struct group_dpif
*group
,
4186 const struct ovs_list
**buckets
)
4188 *buckets
= &group
->up
.buckets
;
4191 enum ofp11_group_type
4192 group_dpif_get_type(const struct group_dpif
*group
)
4194 return group
->up
.type
;
4198 group_dpif_get_selection_method(const struct group_dpif
*group
)
4200 return group
->up
.props
.selection_method
;
4203 /* Sends 'packet' out 'ofport'.
4204 * May modify 'packet'.
4205 * Returns 0 if successful, otherwise a positive errno value. */
4207 ofproto_dpif_send_packet(const struct ofport_dpif
*ofport
, struct dp_packet
*packet
)
4209 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
4212 error
= xlate_send_packet(ofport
, packet
);
4214 ovs_mutex_lock(&ofproto
->stats_mutex
);
4215 ofproto
->stats
.tx_packets
++;
4216 ofproto
->stats
.tx_bytes
+= dp_packet_size(packet
);
4217 ovs_mutex_unlock(&ofproto
->stats_mutex
);
4222 group_dpif_get_selection_method_param(const struct group_dpif
*group
)
4224 return group
->up
.props
.selection_method_param
;
4227 const struct field_array
*
4228 group_dpif_get_fields(const struct group_dpif
*group
)
4230 return &group
->up
.props
.fields
;
4233 /* Return the version string of the datapath that backs up
4237 get_datapath_version(const struct ofproto
*ofproto_
)
4239 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4241 return ofproto
->backer
->dp_version_string
;
4245 set_frag_handling(struct ofproto
*ofproto_
,
4246 enum ofp_config_flags frag_handling
)
4248 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4249 if (frag_handling
!= OFPC_FRAG_REASM
) {
4250 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4258 packet_out(struct ofproto
*ofproto_
, struct dp_packet
*packet
,
4259 const struct flow
*flow
,
4260 const struct ofpact
*ofpacts
, size_t ofpacts_len
)
4262 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4264 ofproto_dpif_execute_actions(ofproto
, flow
, NULL
, ofpacts
,
4265 ofpacts_len
, packet
);
4272 set_netflow(struct ofproto
*ofproto_
,
4273 const struct netflow_options
*netflow_options
)
4275 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4277 if (netflow_options
) {
4278 if (!ofproto
->netflow
) {
4279 ofproto
->netflow
= netflow_create();
4280 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4282 return netflow_set_options(ofproto
->netflow
, netflow_options
);
4283 } else if (ofproto
->netflow
) {
4284 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4285 netflow_unref(ofproto
->netflow
);
4286 ofproto
->netflow
= NULL
;
4293 get_netflow_ids(const struct ofproto
*ofproto_
,
4294 uint8_t *engine_type
, uint8_t *engine_id
)
4296 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4298 dpif_get_netflow_ids(ofproto
->backer
->dpif
, engine_type
, engine_id
);
4301 static struct ofproto_dpif
*
4302 ofproto_dpif_lookup(const char *name
)
4304 struct ofproto_dpif
*ofproto
;
4306 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_node
,
4307 hash_string(name
, 0), &all_ofproto_dpifs
) {
4308 if (!strcmp(ofproto
->up
.name
, name
)) {
4316 ofproto_unixctl_fdb_flush(struct unixctl_conn
*conn
, int argc
,
4317 const char *argv
[], void *aux OVS_UNUSED
)
4319 struct ofproto_dpif
*ofproto
;
4322 ofproto
= ofproto_dpif_lookup(argv
[1]);
4324 unixctl_command_reply_error(conn
, "no such bridge");
4327 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4328 mac_learning_flush(ofproto
->ml
);
4329 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4331 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4332 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4333 mac_learning_flush(ofproto
->ml
);
4334 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4338 unixctl_command_reply(conn
, "table successfully flushed");
4342 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn
*conn
, int argc
,
4343 const char *argv
[], void *aux OVS_UNUSED
)
4345 struct ofproto_dpif
*ofproto
;
4348 ofproto
= ofproto_dpif_lookup(argv
[1]);
4350 unixctl_command_reply_error(conn
, "no such bridge");
4354 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4355 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4358 mcast_snooping_mdb_flush(ofproto
->ms
);
4360 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4361 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4364 mcast_snooping_mdb_flush(ofproto
->ms
);
4368 unixctl_command_reply(conn
, "table successfully flushed");
4371 static struct ofport_dpif
*
4372 ofbundle_get_a_port(const struct ofbundle
*bundle
)
4374 return CONTAINER_OF(list_front(&bundle
->ports
), struct ofport_dpif
,
4379 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4380 const char *argv
[], void *aux OVS_UNUSED
)
4382 struct ds ds
= DS_EMPTY_INITIALIZER
;
4383 const struct ofproto_dpif
*ofproto
;
4384 const struct mac_entry
*e
;
4386 ofproto
= ofproto_dpif_lookup(argv
[1]);
4388 unixctl_command_reply_error(conn
, "no such bridge");
4392 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
4393 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
4394 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
4395 struct ofbundle
*bundle
= mac_entry_get_port(ofproto
->ml
, e
);
4396 char name
[OFP_MAX_PORT_NAME_LEN
];
4398 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4400 ds_put_format(&ds
, "%5s %4d "ETH_ADDR_FMT
" %3d\n",
4401 name
, e
->vlan
, ETH_ADDR_ARGS(e
->mac
),
4402 mac_entry_age(ofproto
->ml
, e
));
4404 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4405 unixctl_command_reply(conn
, ds_cstr(&ds
));
4410 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn
*conn
,
4411 int argc OVS_UNUSED
,
4413 void *aux OVS_UNUSED
)
4415 struct ds ds
= DS_EMPTY_INITIALIZER
;
4416 const struct ofproto_dpif
*ofproto
;
4417 const struct ofbundle
*bundle
;
4418 const struct mcast_group
*grp
;
4419 struct mcast_group_bundle
*b
;
4420 struct mcast_mrouter_bundle
*mrouter
;
4422 ofproto
= ofproto_dpif_lookup(argv
[1]);
4424 unixctl_command_reply_error(conn
, "no such bridge");
4428 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4429 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4433 ds_put_cstr(&ds
, " port VLAN GROUP Age\n");
4434 ovs_rwlock_rdlock(&ofproto
->ms
->rwlock
);
4435 LIST_FOR_EACH (grp
, group_node
, &ofproto
->ms
->group_lru
) {
4436 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
4437 char name
[OFP_MAX_PORT_NAME_LEN
];
4440 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4442 ds_put_format(&ds
, "%5s %4d ", name
, grp
->vlan
);
4443 print_ipv6_mapped(&ds
, &grp
->addr
);
4444 ds_put_format(&ds
, " %3d\n",
4445 mcast_bundle_age(ofproto
->ms
, b
));
4449 /* ports connected to multicast routers */
4450 LIST_FOR_EACH(mrouter
, mrouter_node
, &ofproto
->ms
->mrouter_lru
) {
4451 char name
[OFP_MAX_PORT_NAME_LEN
];
4453 bundle
= mrouter
->port
;
4454 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4456 ds_put_format(&ds
, "%5s %4d querier %3d\n",
4457 name
, mrouter
->vlan
,
4458 mcast_mrouter_age(ofproto
->ms
, mrouter
));
4460 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
4461 unixctl_command_reply(conn
, ds_cstr(&ds
));
4466 struct xlate_out xout
;
4467 struct xlate_in xin
;
4468 const struct flow
*key
;
4471 struct flow_wildcards wc
;
4472 struct ofpbuf odp_actions
;
4476 trace_format_rule(struct ds
*result
, int level
, const struct rule_dpif
*rule
)
4478 const struct rule_actions
*actions
;
4481 ds_put_char_multiple(result
, '\t', level
);
4483 ds_put_cstr(result
, "No match\n");
4487 ovs_mutex_lock(&rule
->up
.mutex
);
4488 cookie
= rule
->up
.flow_cookie
;
4489 ovs_mutex_unlock(&rule
->up
.mutex
);
4491 ds_put_format(result
, "Rule: table=%"PRIu8
" cookie=%#"PRIx64
" ",
4492 rule
? rule
->up
.table_id
: 0, ntohll(cookie
));
4493 cls_rule_format(&rule
->up
.cr
, result
);
4494 ds_put_char(result
, '\n');
4496 actions
= rule_dpif_get_actions(rule
);
4498 ds_put_char_multiple(result
, '\t', level
);
4499 ds_put_cstr(result
, "OpenFlow actions=");
4500 ofpacts_format(actions
->ofpacts
, actions
->ofpacts_len
, result
);
4501 ds_put_char(result
, '\n');
4505 trace_format_flow(struct ds
*result
, int level
, const char *title
,
4506 struct trace_ctx
*trace
)
4508 ds_put_char_multiple(result
, '\t', level
);
4509 ds_put_format(result
, "%s: ", title
);
4510 /* Do not report unchanged flows for resubmits. */
4511 if ((level
> 0 && flow_equal(&trace
->xin
.flow
, &trace
->flow
))
4512 || (level
== 0 && flow_equal(&trace
->xin
.flow
, trace
->key
))) {
4513 ds_put_cstr(result
, "unchanged");
4515 flow_format(result
, &trace
->xin
.flow
);
4516 trace
->flow
= trace
->xin
.flow
;
4518 ds_put_char(result
, '\n');
4522 trace_format_regs(struct ds
*result
, int level
, const char *title
,
4523 struct trace_ctx
*trace
)
4527 ds_put_char_multiple(result
, '\t', level
);
4528 ds_put_format(result
, "%s:", title
);
4529 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
4530 ds_put_format(result
, " reg%"PRIuSIZE
"=0x%"PRIx32
, i
, trace
->flow
.regs
[i
]);
4532 ds_put_char(result
, '\n');
4536 trace_format_odp(struct ds
*result
, int level
, const char *title
,
4537 struct trace_ctx
*trace
)
4539 struct ofpbuf
*odp_actions
= &trace
->odp_actions
;
4541 ds_put_char_multiple(result
, '\t', level
);
4542 ds_put_format(result
, "%s: ", title
);
4543 format_odp_actions(result
, odp_actions
->data
, odp_actions
->size
);
4544 ds_put_char(result
, '\n');
4548 trace_format_megaflow(struct ds
*result
, int level
, const char *title
,
4549 struct trace_ctx
*trace
)
4553 ds_put_char_multiple(result
, '\t', level
);
4554 ds_put_format(result
, "%s: ", title
);
4555 match_init(&match
, trace
->key
, &trace
->wc
);
4556 match_format(&match
, result
, OFP_DEFAULT_PRIORITY
);
4557 ds_put_char(result
, '\n');
4560 static void trace_report(struct xlate_in
*, int recurse
,
4561 const char *format
, ...)
4562 OVS_PRINTF_FORMAT(3, 4);
4563 static void trace_report_valist(struct xlate_in
*, int recurse
,
4564 const char *format
, va_list args
)
4565 OVS_PRINTF_FORMAT(3, 0);
4568 trace_resubmit(struct xlate_in
*xin
, struct rule_dpif
*rule
, int recurse
)
4570 struct trace_ctx
*trace
= CONTAINER_OF(xin
, struct trace_ctx
, xin
);
4571 struct ds
*result
= trace
->result
;
4574 if (rule
== xin
->ofproto
->miss_rule
) {
4575 trace_report(xin
, recurse
,
4576 "No match, flow generates \"packet in\"s.");
4577 } else if (rule
== xin
->ofproto
->no_packet_in_rule
) {
4578 trace_report(xin
, recurse
, "No match, packets dropped because "
4579 "OFPPC_NO_PACKET_IN is set on in_port.");
4580 } else if (rule
== xin
->ofproto
->drop_frags_rule
) {
4581 trace_report(xin
, recurse
, "Packets dropped because they are IP "
4582 "fragments and the fragment handling mode is "
4587 ds_put_char(result
, '\n');
4589 trace_format_flow(result
, recurse
, "Resubmitted flow", trace
);
4590 trace_format_regs(result
, recurse
, "Resubmitted regs", trace
);
4591 trace_format_odp(result
, recurse
, "Resubmitted odp", trace
);
4592 trace_format_megaflow(result
, recurse
, "Resubmitted megaflow", trace
);
4594 trace_format_rule(result
, recurse
, rule
);
4598 trace_report_valist(struct xlate_in
*xin
, int recurse
,
4599 const char *format
, va_list args
)
4601 struct trace_ctx
*trace
= CONTAINER_OF(xin
, struct trace_ctx
, xin
);
4602 struct ds
*result
= trace
->result
;
4604 ds_put_char_multiple(result
, '\t', recurse
);
4605 ds_put_format_valist(result
, format
, args
);
4606 ds_put_char(result
, '\n');
4610 trace_report(struct xlate_in
*xin
, int recurse
, const char *format
, ...)
4614 va_start(args
, format
);
4615 trace_report_valist(xin
, recurse
, format
, args
);
4619 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4620 * forms are supported:
4622 * - [dpname] odp_flow [-generate | packet]
4623 * - bridge br_flow [-generate | packet]
4625 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4626 * returns a nonnull malloced error message. */
4627 static char * OVS_WARN_UNUSED_RESULT
4628 parse_flow_and_packet(int argc
, const char *argv
[],
4629 struct ofproto_dpif
**ofprotop
, struct flow
*flow
,
4630 struct dp_packet
**packetp
)
4632 const struct dpif_backer
*backer
= NULL
;
4633 const char *error
= NULL
;
4635 struct simap port_names
= SIMAP_INITIALIZER(&port_names
);
4636 struct dp_packet
*packet
;
4637 struct ofpbuf odp_key
;
4638 struct ofpbuf odp_mask
;
4640 ofpbuf_init(&odp_key
, 0);
4641 ofpbuf_init(&odp_mask
, 0);
4643 /* Handle "-generate" or a hex string as the last argument. */
4644 if (!strcmp(argv
[argc
- 1], "-generate")) {
4645 packet
= dp_packet_new(0);
4648 error
= eth_from_hex(argv
[argc
- 1], &packet
);
4651 } else if (argc
== 4) {
4652 /* The 3-argument form must end in "-generate' or a hex string. */
4658 /* odp_flow can have its in_port specified as a name instead of port no.
4659 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4660 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4661 * we need to create a simap of name to port no. */
4663 const char *dp_type
;
4664 if (!strncmp(argv
[1], "ovs-", 4)) {
4665 dp_type
= argv
[1] + 4;
4669 backer
= shash_find_data(&all_dpif_backers
, dp_type
);
4670 } else if (argc
== 2) {
4671 struct shash_node
*node
;
4672 if (shash_count(&all_dpif_backers
) == 1) {
4673 node
= shash_first(&all_dpif_backers
);
4674 backer
= node
->data
;
4677 error
= "Syntax error";
4680 if (backer
&& backer
->dpif
) {
4681 struct dpif_port dpif_port
;
4682 struct dpif_port_dump port_dump
;
4683 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, backer
->dpif
) {
4684 simap_put(&port_names
, dpif_port
.name
,
4685 odp_to_u32(dpif_port
.port_no
));
4689 /* Parse the flow and determine whether a datapath or
4690 * bridge is specified. If function odp_flow_key_from_string()
4691 * returns 0, the flow is a odp_flow. If function
4692 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4693 if (!odp_flow_from_string(argv
[argc
- 1], &port_names
,
4694 &odp_key
, &odp_mask
)) {
4696 error
= "Cannot find the datapath";
4700 if (odp_flow_key_to_flow(odp_key
.data
, odp_key
.size
, flow
) == ODP_FIT_ERROR
) {
4701 error
= "Failed to parse datapath flow key";
4705 *ofprotop
= xlate_lookup_ofproto(backer
, flow
,
4706 &flow
->in_port
.ofp_port
);
4707 if (*ofprotop
== NULL
) {
4708 error
= "Invalid datapath flow";
4712 vsp_adjust_flow(*ofprotop
, flow
, NULL
);
4715 char *err
= parse_ofp_exact_flow(flow
, NULL
, argv
[argc
- 1], NULL
);
4718 m_err
= xasprintf("Bad openflow flow syntax: %s", err
);
4723 error
= "Must specify bridge name";
4727 *ofprotop
= ofproto_dpif_lookup(argv
[1]);
4729 error
= "Unknown bridge name";
4735 /* Generate a packet, if requested. */
4737 if (!dp_packet_size(packet
)) {
4738 flow_compose(packet
, flow
);
4740 /* Use the metadata from the flow and the packet argument
4741 * to reconstruct the flow. */
4742 pkt_metadata_from_flow(&packet
->md
, flow
);
4743 flow_extract(packet
, flow
);
4748 if (error
&& !m_err
) {
4749 m_err
= xstrdup(error
);
4752 dp_packet_delete(packet
);
4756 ofpbuf_uninit(&odp_key
);
4757 ofpbuf_uninit(&odp_mask
);
4758 simap_destroy(&port_names
);
4763 ofproto_unixctl_trace(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
4764 void *aux OVS_UNUSED
)
4766 struct ofproto_dpif
*ofproto
;
4767 struct dp_packet
*packet
;
4771 error
= parse_flow_and_packet(argc
, argv
, &ofproto
, &flow
, &packet
);
4776 ofproto_trace(ofproto
, &flow
, packet
, NULL
, 0, &result
);
4777 unixctl_command_reply(conn
, ds_cstr(&result
));
4778 ds_destroy(&result
);
4779 dp_packet_delete(packet
);
4781 unixctl_command_reply_error(conn
, error
);
4787 ofproto_unixctl_trace_actions(struct unixctl_conn
*conn
, int argc
,
4788 const char *argv
[], void *aux OVS_UNUSED
)
4790 enum ofputil_protocol usable_protocols
;
4791 struct ofproto_dpif
*ofproto
;
4792 bool enforce_consistency
;
4793 struct ofpbuf ofpacts
;
4794 struct dp_packet
*packet
;
4799 /* Three kinds of error return values! */
4805 ofpbuf_init(&ofpacts
, 0);
4807 /* Parse actions. */
4808 error
= ofpacts_parse_actions(argv
[--argc
], &ofpacts
, &usable_protocols
);
4810 unixctl_command_reply_error(conn
, error
);
4815 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4816 * consistency between the flow and the actions. With -consistent, we
4817 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4818 if (!strcmp(argv
[1], "-consistent")) {
4819 enforce_consistency
= true;
4823 enforce_consistency
= false;
4826 error
= parse_flow_and_packet(argc
, argv
, &ofproto
, &flow
, &packet
);
4828 unixctl_command_reply_error(conn
, error
);
4833 /* Do the same checks as handle_packet_out() in ofproto.c.
4835 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4836 * strictly correct because these actions aren't in any table, but it's OK
4837 * because it 'table_id' is used only to check goto_table instructions, but
4838 * packet-outs take a list of actions and therefore it can't include
4841 * We skip the "meter" check here because meter is an instruction, not an
4842 * action, and thus cannot appear in ofpacts. */
4843 in_port
= ofp_to_u16(flow
.in_port
.ofp_port
);
4844 if (in_port
>= ofproto
->up
.max_ports
&& in_port
< ofp_to_u16(OFPP_MAX
)) {
4845 unixctl_command_reply_error(conn
, "invalid in_port");
4848 if (enforce_consistency
) {
4849 retval
= ofpacts_check_consistency(ofpacts
.data
, ofpacts
.size
,
4850 &flow
, u16_to_ofp(ofproto
->up
.max_ports
),
4851 0, 0, usable_protocols
);
4853 retval
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &flow
,
4854 u16_to_ofp(ofproto
->up
.max_ports
), 0, 0,
4860 ds_put_format(&result
, "Bad actions: %s", ofperr_to_string(retval
));
4861 unixctl_command_reply_error(conn
, ds_cstr(&result
));
4865 ofproto_trace(ofproto
, &flow
, packet
,
4866 ofpacts
.data
, ofpacts
.size
, &result
);
4867 unixctl_command_reply(conn
, ds_cstr(&result
));
4870 ds_destroy(&result
);
4871 dp_packet_delete(packet
);
4872 ofpbuf_uninit(&ofpacts
);
4875 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4876 * description of the results to 'ds'.
4878 * The trace follows a packet with the specified 'flow' through the flow
4879 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4880 * side effects (if it is nonnull then its flow must be 'flow').
4882 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4883 * trace, otherwise the actions are determined by a flow table lookup. */
4885 ofproto_trace(struct ofproto_dpif
*ofproto
, struct flow
*flow
,
4886 const struct dp_packet
*packet
,
4887 const struct ofpact ofpacts
[], size_t ofpacts_len
,
4890 struct trace_ctx trace
;
4892 ds_put_format(ds
, "Bridge: %s\n", ofproto
->up
.name
);
4893 ds_put_cstr(ds
, "Flow: ");
4894 flow_format(ds
, flow
);
4895 ds_put_char(ds
, '\n');
4897 ofpbuf_init(&trace
.odp_actions
, 0);
4900 trace
.key
= flow
; /* Original flow key, used for megaflow. */
4901 trace
.flow
= *flow
; /* May be modified by actions. */
4902 xlate_in_init(&trace
.xin
, ofproto
, flow
, flow
->in_port
.ofp_port
, NULL
,
4903 ntohs(flow
->tcp_flags
), packet
, &trace
.wc
,
4904 &trace
.odp_actions
);
4905 trace
.xin
.ofpacts
= ofpacts
;
4906 trace
.xin
.ofpacts_len
= ofpacts_len
;
4907 trace
.xin
.resubmit_hook
= trace_resubmit
;
4908 trace
.xin
.report_hook
= trace_report_valist
;
4910 xlate_actions(&trace
.xin
, &trace
.xout
);
4912 ds_put_char(ds
, '\n');
4913 trace_format_flow(ds
, 0, "Final flow", &trace
);
4914 trace_format_megaflow(ds
, 0, "Megaflow", &trace
);
4916 ds_put_cstr(ds
, "Datapath actions: ");
4917 format_odp_actions(ds
, trace
.odp_actions
.data
, trace
.odp_actions
.size
);
4919 if (trace
.xout
.slow
) {
4920 enum slow_path_reason slow
;
4922 ds_put_cstr(ds
, "\nThis flow is handled by the userspace "
4923 "slow path because it:");
4925 slow
= trace
.xout
.slow
;
4927 enum slow_path_reason bit
= rightmost_1bit(slow
);
4929 ds_put_format(ds
, "\n\t- %s.",
4930 slow_path_reason_to_explanation(bit
));
4936 xlate_out_uninit(&trace
.xout
);
4937 ofpbuf_uninit(&trace
.odp_actions
);
4940 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4941 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4942 * to destroy 'ofproto_shash' and free the returned value. */
4943 static const struct shash_node
**
4944 get_ofprotos(struct shash
*ofproto_shash
)
4946 const struct ofproto_dpif
*ofproto
;
4948 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4949 char *name
= xasprintf("%s@%s", ofproto
->up
.type
, ofproto
->up
.name
);
4950 shash_add_nocopy(ofproto_shash
, name
, ofproto
);
4953 return shash_sort(ofproto_shash
);
4957 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4958 const char *argv
[] OVS_UNUSED
,
4959 void *aux OVS_UNUSED
)
4961 struct ds ds
= DS_EMPTY_INITIALIZER
;
4962 struct shash ofproto_shash
;
4963 const struct shash_node
**sorted_ofprotos
;
4966 shash_init(&ofproto_shash
);
4967 sorted_ofprotos
= get_ofprotos(&ofproto_shash
);
4968 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
4969 const struct shash_node
*node
= sorted_ofprotos
[i
];
4970 ds_put_format(&ds
, "%s\n", node
->name
);
4973 shash_destroy(&ofproto_shash
);
4974 free(sorted_ofprotos
);
4976 unixctl_command_reply(conn
, ds_cstr(&ds
));
4981 dpif_show_backer(const struct dpif_backer
*backer
, struct ds
*ds
)
4983 const struct shash_node
**ofprotos
;
4984 struct dpif_dp_stats dp_stats
;
4985 struct shash ofproto_shash
;
4988 dpif_get_dp_stats(backer
->dpif
, &dp_stats
);
4990 ds_put_format(ds
, "%s: hit:%"PRIu64
" missed:%"PRIu64
"\n",
4991 dpif_name(backer
->dpif
), dp_stats
.n_hit
, dp_stats
.n_missed
);
4993 shash_init(&ofproto_shash
);
4994 ofprotos
= get_ofprotos(&ofproto_shash
);
4995 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
4996 struct ofproto_dpif
*ofproto
= ofprotos
[i
]->data
;
4997 const struct shash_node
**ports
;
5000 if (ofproto
->backer
!= backer
) {
5004 ds_put_format(ds
, "\t%s:\n", ofproto
->up
.name
);
5006 ports
= shash_sort(&ofproto
->up
.port_by_name
);
5007 for (j
= 0; j
< shash_count(&ofproto
->up
.port_by_name
); j
++) {
5008 const struct shash_node
*node
= ports
[j
];
5009 struct ofport
*ofport
= node
->data
;
5011 odp_port_t odp_port
;
5013 ds_put_format(ds
, "\t\t%s %u/", netdev_get_name(ofport
->netdev
),
5016 odp_port
= ofp_port_to_odp_port(ofproto
, ofport
->ofp_port
);
5017 if (odp_port
!= ODPP_NONE
) {
5018 ds_put_format(ds
, "%"PRIu32
":", odp_port
);
5020 ds_put_cstr(ds
, "none:");
5023 ds_put_format(ds
, " (%s", netdev_get_type(ofport
->netdev
));
5026 if (!netdev_get_config(ofport
->netdev
, &config
)) {
5027 const struct smap_node
**nodes
;
5030 nodes
= smap_sort(&config
);
5031 for (i
= 0; i
< smap_count(&config
); i
++) {
5032 const struct smap_node
*node
= nodes
[i
];
5033 ds_put_format(ds
, "%c %s=%s", i
? ',' : ':',
5034 node
->key
, node
->value
);
5038 smap_destroy(&config
);
5040 ds_put_char(ds
, ')');
5041 ds_put_char(ds
, '\n');
5045 shash_destroy(&ofproto_shash
);
5050 ofproto_unixctl_dpif_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5051 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
5053 struct ds ds
= DS_EMPTY_INITIALIZER
;
5054 const struct shash_node
**backers
;
5057 backers
= shash_sort(&all_dpif_backers
);
5058 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5059 dpif_show_backer(backers
[i
]->data
, &ds
);
5063 unixctl_command_reply(conn
, ds_cstr(&ds
));
5068 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn
*conn
,
5069 int argc OVS_UNUSED
, const char *argv
[],
5070 void *aux OVS_UNUSED
)
5072 const struct ofproto_dpif
*ofproto
;
5074 struct ds ds
= DS_EMPTY_INITIALIZER
;
5075 bool verbosity
= false;
5077 struct dpif_port dpif_port
;
5078 struct dpif_port_dump port_dump
;
5079 struct hmap portno_names
;
5081 struct dpif_flow_dump
*flow_dump
;
5082 struct dpif_flow_dump_thread
*flow_dump_thread
;
5086 ofproto
= ofproto_dpif_lookup(argv
[argc
- 1]);
5088 unixctl_command_reply_error(conn
, "no such bridge");
5092 if (argc
> 2 && !strcmp(argv
[1], "-m")) {
5096 hmap_init(&portno_names
);
5097 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, ofproto
->backer
->dpif
) {
5098 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
5102 flow_dump
= dpif_flow_dump_create(ofproto
->backer
->dpif
, false);
5103 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
5104 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
5107 if (odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
) == ODP_FIT_ERROR
5108 || xlate_lookup_ofproto(ofproto
->backer
, &flow
, NULL
) != ofproto
) {
5113 odp_format_ufid(&f
.ufid
, &ds
);
5114 ds_put_cstr(&ds
, " ");
5116 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
5117 &portno_names
, &ds
, verbosity
);
5118 ds_put_cstr(&ds
, ", ");
5119 dpif_flow_stats_format(&f
.stats
, &ds
);
5120 ds_put_cstr(&ds
, ", actions:");
5121 format_odp_actions(&ds
, f
.actions
, f
.actions_len
);
5122 ds_put_char(&ds
, '\n');
5124 dpif_flow_dump_thread_destroy(flow_dump_thread
);
5125 error
= dpif_flow_dump_destroy(flow_dump
);
5129 ds_put_format(&ds
, "dpif/dump_flows failed: %s", ovs_strerror(errno
));
5130 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
5132 unixctl_command_reply(conn
, ds_cstr(&ds
));
5134 odp_portno_names_destroy(&portno_names
);
5135 hmap_destroy(&portno_names
);
5140 ofproto_revalidate_all_backers(void)
5142 const struct shash_node
**backers
;
5145 backers
= shash_sort(&all_dpif_backers
);
5146 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5147 struct dpif_backer
*backer
= backers
[i
]->data
;
5148 backer
->need_revalidate
= REV_RECONFIGURE
;
5154 disable_tnl_push_pop(struct unixctl_conn
*conn OVS_UNUSED
, int argc OVS_UNUSED
,
5155 const char *argv
[], void *aux OVS_UNUSED
)
5157 if (!strcasecmp(argv
[1], "off")) {
5158 ofproto_use_tnl_push_pop
= false;
5159 unixctl_command_reply(conn
, "Tunnel push-pop off");
5160 ofproto_revalidate_all_backers();
5161 } else if (!strcasecmp(argv
[1], "on")) {
5162 ofproto_use_tnl_push_pop
= true;
5163 unixctl_command_reply(conn
, "Tunnel push-pop on");
5164 ofproto_revalidate_all_backers();
5169 ofproto_unixctl_init(void)
5171 static bool registered
;
5177 unixctl_command_register(
5179 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5180 1, 3, ofproto_unixctl_trace
, NULL
);
5181 unixctl_command_register(
5182 "ofproto/trace-packet-out",
5183 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5184 2, 6, ofproto_unixctl_trace_actions
, NULL
);
5185 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5186 ofproto_unixctl_fdb_flush
, NULL
);
5187 unixctl_command_register("fdb/show", "bridge", 1, 1,
5188 ofproto_unixctl_fdb_show
, NULL
);
5189 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5190 ofproto_unixctl_mcast_snooping_flush
, NULL
);
5191 unixctl_command_register("mdb/show", "bridge", 1, 1,
5192 ofproto_unixctl_mcast_snooping_show
, NULL
);
5193 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5194 ofproto_unixctl_dpif_dump_dps
, NULL
);
5195 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show
,
5197 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5198 ofproto_unixctl_dpif_dump_flows
, NULL
);
5200 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5201 disable_tnl_push_pop
, NULL
);
5204 /* Returns true if 'table' is the table used for internal rules,
5205 * false otherwise. */
5207 table_is_internal(uint8_t table_id
)
5209 return table_id
== TBL_INTERNAL
;
5212 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5214 * This is deprecated. It is only for compatibility with broken device drivers
5215 * in old versions of Linux that do not properly support VLANs when VLAN
5216 * devices are not used. When broken device drivers are no longer in
5217 * widespread use, we will delete these interfaces. */
5220 set_realdev(struct ofport
*ofport_
, ofp_port_t realdev_ofp_port
, int vid
)
5222 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
5223 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
5225 if (realdev_ofp_port
== ofport
->realdev_ofp_port
5226 && vid
== ofport
->vlandev_vid
) {
5230 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5232 if (ofport
->realdev_ofp_port
) {
5235 if (realdev_ofp_port
&& ofport
->bundle
) {
5236 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5237 * themselves be part of a bundle. */
5238 bundle_set(ofport_
->ofproto
, ofport
->bundle
, NULL
);
5241 ofport
->realdev_ofp_port
= realdev_ofp_port
;
5242 ofport
->vlandev_vid
= vid
;
5244 if (realdev_ofp_port
) {
5245 vsp_add(ofport
, realdev_ofp_port
, vid
);
5252 hash_realdev_vid(ofp_port_t realdev_ofp_port
, int vid
)
5254 return hash_2words(ofp_to_u16(realdev_ofp_port
), vid
);
5258 ofproto_has_vlan_splinters(const struct ofproto_dpif
*ofproto
)
5259 OVS_EXCLUDED(ofproto
->vsp_mutex
)
5261 /* hmap_is_empty is thread safe. */
5262 return !hmap_is_empty(&ofproto
->realdev_vid_map
);
5267 vsp_realdev_to_vlandev__(const struct ofproto_dpif
*ofproto
,
5268 ofp_port_t realdev_ofp_port
, ovs_be16 vlan_tci
)
5269 OVS_REQUIRES(ofproto
->vsp_mutex
)
5271 if (!hmap_is_empty(&ofproto
->realdev_vid_map
)) {
5272 int vid
= vlan_tci_to_vid(vlan_tci
);
5273 const struct vlan_splinter
*vsp
;
5275 HMAP_FOR_EACH_WITH_HASH (vsp
, realdev_vid_node
,
5276 hash_realdev_vid(realdev_ofp_port
, vid
),
5277 &ofproto
->realdev_vid_map
) {
5278 if (vsp
->realdev_ofp_port
== realdev_ofp_port
5279 && vsp
->vid
== vid
) {
5280 return vsp
->vlandev_ofp_port
;
5284 return realdev_ofp_port
;
5287 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5288 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5289 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5290 * 'vlan_tci' 9, it would return the port number of eth0.9.
5292 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5293 * function just returns its 'realdev_ofp_port' argument. */
5295 vsp_realdev_to_vlandev(const struct ofproto_dpif
*ofproto
,
5296 ofp_port_t realdev_ofp_port
, ovs_be16 vlan_tci
)
5297 OVS_EXCLUDED(ofproto
->vsp_mutex
)
5301 /* hmap_is_empty is thread safe, see if we can return immediately. */
5302 if (hmap_is_empty(&ofproto
->realdev_vid_map
)) {
5303 return realdev_ofp_port
;
5305 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5306 ret
= vsp_realdev_to_vlandev__(ofproto
, realdev_ofp_port
, vlan_tci
);
5307 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5311 static struct vlan_splinter
*
5312 vlandev_find(const struct ofproto_dpif
*ofproto
, ofp_port_t vlandev_ofp_port
)
5314 struct vlan_splinter
*vsp
;
5316 HMAP_FOR_EACH_WITH_HASH (vsp
, vlandev_node
,
5317 hash_ofp_port(vlandev_ofp_port
),
5318 &ofproto
->vlandev_map
) {
5319 if (vsp
->vlandev_ofp_port
== vlandev_ofp_port
) {
5327 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5328 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5329 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5330 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5331 * eth0 and store 9 in '*vid'.
5333 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5334 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5337 vsp_vlandev_to_realdev(const struct ofproto_dpif
*ofproto
,
5338 ofp_port_t vlandev_ofp_port
, int *vid
)
5339 OVS_REQUIRES(ofproto
->vsp_mutex
)
5341 if (!hmap_is_empty(&ofproto
->vlandev_map
)) {
5342 const struct vlan_splinter
*vsp
;
5344 vsp
= vlandev_find(ofproto
, vlandev_ofp_port
);
5349 return vsp
->realdev_ofp_port
;
5355 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5356 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5357 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5358 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5359 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5360 * case unless VLAN splinters are enabled), returns false without making any
5363 vsp_adjust_flow(const struct ofproto_dpif
*ofproto
, struct flow
*flow
,
5364 struct dp_packet
*packet
)
5365 OVS_EXCLUDED(ofproto
->vsp_mutex
)
5370 /* hmap_is_empty is thread safe. */
5371 if (hmap_is_empty(&ofproto
->vlandev_map
)) {
5375 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5376 realdev
= vsp_vlandev_to_realdev(ofproto
, flow
->in_port
.ofp_port
, &vid
);
5377 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5382 /* Cause the flow to be processed as if it came in on the real device with
5383 * the VLAN device's VLAN ID. */
5384 flow
->in_port
.ofp_port
= realdev
;
5385 flow
->vlan_tci
= htons((vid
& VLAN_VID_MASK
) | VLAN_CFI
);
5388 /* Make the packet resemble the flow, so that it gets sent to an
5389 * OpenFlow controller properly, so that it looks correct for sFlow,
5390 * and so that flow_extract() will get the correct vlan_tci if it is
5391 * called on 'packet'. */
5392 eth_push_vlan(packet
, htons(ETH_TYPE_VLAN
), flow
->vlan_tci
);
5399 vsp_remove(struct ofport_dpif
*port
)
5401 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
5402 struct vlan_splinter
*vsp
;
5404 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5405 vsp
= vlandev_find(ofproto
, port
->up
.ofp_port
);
5407 hmap_remove(&ofproto
->vlandev_map
, &vsp
->vlandev_node
);
5408 hmap_remove(&ofproto
->realdev_vid_map
, &vsp
->realdev_vid_node
);
5411 port
->realdev_ofp_port
= 0;
5413 VLOG_ERR("missing vlan device record");
5415 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5419 vsp_add(struct ofport_dpif
*port
, ofp_port_t realdev_ofp_port
, int vid
)
5421 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
5423 ovs_mutex_lock(&ofproto
->vsp_mutex
);
5424 if (!vsp_vlandev_to_realdev(ofproto
, port
->up
.ofp_port
, NULL
)
5425 && (vsp_realdev_to_vlandev__(ofproto
, realdev_ofp_port
, htons(vid
))
5426 == realdev_ofp_port
)) {
5427 struct vlan_splinter
*vsp
;
5429 vsp
= xmalloc(sizeof *vsp
);
5430 vsp
->realdev_ofp_port
= realdev_ofp_port
;
5431 vsp
->vlandev_ofp_port
= port
->up
.ofp_port
;
5434 port
->realdev_ofp_port
= realdev_ofp_port
;
5436 hmap_insert(&ofproto
->vlandev_map
, &vsp
->vlandev_node
,
5437 hash_ofp_port(port
->up
.ofp_port
));
5438 hmap_insert(&ofproto
->realdev_vid_map
, &vsp
->realdev_vid_node
,
5439 hash_realdev_vid(realdev_ofp_port
, vid
));
5441 VLOG_ERR("duplicate vlan device record");
5443 ovs_mutex_unlock(&ofproto
->vsp_mutex
);
5447 ofp_port_to_odp_port(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
5449 const struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
5450 return ofport
? ofport
->odp_port
: ODPP_NONE
;
5453 struct ofport_dpif
*
5454 odp_port_to_ofport(const struct dpif_backer
*backer
, odp_port_t odp_port
)
5456 struct ofport_dpif
*port
;
5458 ovs_rwlock_rdlock(&backer
->odp_to_ofport_lock
);
5459 HMAP_FOR_EACH_IN_BUCKET (port
, odp_port_node
, hash_odp_port(odp_port
),
5460 &backer
->odp_to_ofport_map
) {
5461 if (port
->odp_port
== odp_port
) {
5462 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5467 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5472 odp_port_to_ofp_port(const struct ofproto_dpif
*ofproto
, odp_port_t odp_port
)
5474 struct ofport_dpif
*port
;
5476 port
= odp_port_to_ofport(ofproto
->backer
, odp_port
);
5477 if (port
&& &ofproto
->up
== port
->up
.ofproto
) {
5478 return port
->up
.ofp_port
;
5485 ofproto_dpif_add_internal_flow(struct ofproto_dpif
*ofproto
,
5486 const struct match
*match
, int priority
,
5487 uint16_t idle_timeout
,
5488 const struct ofpbuf
*ofpacts
,
5489 struct rule
**rulep
)
5491 struct ofproto_flow_mod ofm
;
5492 struct rule_dpif
*rule
;
5495 ofm
.fm
.match
= *match
;
5496 ofm
.fm
.priority
= priority
;
5497 ofm
.fm
.new_cookie
= htonll(0);
5498 ofm
.fm
.cookie
= htonll(0);
5499 ofm
.fm
.cookie_mask
= htonll(0);
5500 ofm
.fm
.modify_cookie
= false;
5501 ofm
.fm
.table_id
= TBL_INTERNAL
;
5502 ofm
.fm
.command
= OFPFC_ADD
;
5503 ofm
.fm
.idle_timeout
= idle_timeout
;
5504 ofm
.fm
.hard_timeout
= 0;
5505 ofm
.fm
.importance
= 0;
5506 ofm
.fm
.buffer_id
= 0;
5507 ofm
.fm
.out_port
= 0;
5508 ofm
.fm
.flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
;
5509 ofm
.fm
.ofpacts
= ofpacts
->data
;
5510 ofm
.fm
.ofpacts_len
= ofpacts
->size
;
5512 error
= ofproto_flow_mod(&ofproto
->up
, &ofm
);
5514 VLOG_ERR_RL(&rl
, "failed to add internal flow (%s)",
5515 ofperr_to_string(error
));
5520 rule
= rule_dpif_lookup_in_table(ofproto
,
5521 ofproto_dpif_get_tables_version(ofproto
),
5522 TBL_INTERNAL
, &ofm
.fm
.match
.flow
,
5523 &ofm
.fm
.match
.wc
, false);
5533 ofproto_dpif_delete_internal_flow(struct ofproto_dpif
*ofproto
,
5534 struct match
*match
, int priority
)
5536 struct ofproto_flow_mod ofm
;
5539 ofm
.fm
.match
= *match
;
5540 ofm
.fm
.priority
= priority
;
5541 ofm
.fm
.new_cookie
= htonll(0);
5542 ofm
.fm
.cookie
= htonll(0);
5543 ofm
.fm
.cookie_mask
= htonll(0);
5544 ofm
.fm
.modify_cookie
= false;
5545 ofm
.fm
.table_id
= TBL_INTERNAL
;
5546 ofm
.fm
.flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
;
5547 ofm
.fm
.command
= OFPFC_DELETE_STRICT
;
5549 error
= ofproto_flow_mod(&ofproto
->up
, &ofm
);
5551 VLOG_ERR_RL(&rl
, "failed to delete internal flow (%s)",
5552 ofperr_to_string(error
));
5559 const struct ofproto_class ofproto_dpif_class
= {
5573 NULL
, /* get_memory_usage. */
5574 type_get_memory_usage
,
5593 port_is_lacp_current
,
5594 port_get_lacp_stats
,
5595 NULL
, /* rule_choose_table */
5619 aa_vlan_get_queue_size
,
5626 get_stp_port_status
,
5631 get_rstp_port_status
,
5638 is_mirror_output_bundle
,
5639 forward_bpdu_changed
,
5640 set_mac_table_config
,
5642 set_mcast_snooping_port
,
5644 NULL
, /* meter_get_features */
5645 NULL
, /* meter_set */
5646 NULL
, /* meter_get */
5647 NULL
, /* meter_del */
5648 group_alloc
, /* group_alloc */
5649 group_construct
, /* group_construct */
5650 group_destruct
, /* group_destruct */
5651 group_dealloc
, /* group_dealloc */
5652 group_modify
, /* group_modify */
5653 group_get_stats
, /* group_get_stats */
5654 get_datapath_version
, /* get_datapath_version */