2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "byte-order.h"
24 #include "connectivity.h"
29 #include "fail-open.h"
30 #include "guarded-list.h"
34 #include "mac-learning.h"
35 #include "mcast-snooping.h"
36 #include "multipath.h"
37 #include "netdev-vport.h"
42 #include "odp-execute.h"
43 #include "ofproto/ofproto-dpif.h"
44 #include "ofproto/ofproto-provider.h"
45 #include "ofproto-dpif-ipfix.h"
46 #include "ofproto-dpif-mirror.h"
47 #include "ofproto-dpif-monitor.h"
48 #include "ofproto-dpif-rid.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "ofproto-dpif-trace.h"
51 #include "ofproto-dpif-upcall.h"
52 #include "ofproto-dpif-xlate.h"
53 #include "ofproto-dpif-xlate-cache.h"
54 #include "openvswitch/ofp-actions.h"
55 #include "openvswitch/dynamic-string.h"
56 #include "openvswitch/meta-flow.h"
57 #include "openvswitch/ofp-parse.h"
58 #include "openvswitch/ofp-print.h"
59 #include "openvswitch/ofp-util.h"
60 #include "openvswitch/ofpbuf.h"
61 #include "openvswitch/vlog.h"
64 #include "ovs-router.h"
65 #include "poll-loop.h"
71 #include "unaligned.h"
74 #include "vlan-bitmap.h"
76 VLOG_DEFINE_THIS_MODULE(ofproto_dpif
);
78 COVERAGE_DEFINE(ofproto_dpif_expired
);
79 COVERAGE_DEFINE(packet_in_overflow
);
83 static void rule_get_stats(struct rule
*, uint64_t *packets
, uint64_t *bytes
,
85 static struct rule_dpif
*rule_dpif_cast(const struct rule
*);
86 static void rule_expire(struct rule_dpif
*, long long now
);
89 struct hmap_node hmap_node
; /* In struct ofproto's "bundles" hmap. */
90 struct ofproto_dpif
*ofproto
; /* Owning ofproto. */
91 void *aux
; /* Key supplied by ofproto's client. */
92 char *name
; /* Identifier for log messages. */
95 struct ovs_list ports
; /* Contains "struct ofport"s. */
96 enum port_vlan_mode vlan_mode
; /* VLAN mode */
97 uint16_t qinq_ethtype
;
98 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
99 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1.
100 * NULL if all VLANs are trunked. */
101 unsigned long *cvlans
;
102 struct lacp
*lacp
; /* LACP if LACP is enabled, otherwise NULL. */
103 struct bond
*bond
; /* Nonnull iff more than one port. */
104 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
106 bool protected; /* Protected port mode */
109 bool floodable
; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
112 static void bundle_remove(struct ofport
*);
113 static void bundle_update(struct ofbundle
*);
114 static void bundle_destroy(struct ofbundle
*);
115 static void bundle_del_port(struct ofport_dpif
*);
116 static void bundle_run(struct ofbundle
*);
117 static void bundle_wait(struct ofbundle
*);
118 static void bundle_flush_macs(struct ofbundle
*, bool);
119 static void bundle_move(struct ofbundle
*, struct ofbundle
*);
121 static void stp_run(struct ofproto_dpif
*ofproto
);
122 static void stp_wait(struct ofproto_dpif
*ofproto
);
123 static int set_stp_port(struct ofport
*,
124 const struct ofproto_port_stp_settings
*);
126 static void rstp_run(struct ofproto_dpif
*ofproto
);
127 static void set_rstp_port(struct ofport
*,
128 const struct ofproto_port_rstp_settings
*);
131 struct hmap_node odp_port_node
; /* In dpif_backer's "odp_to_ofport_map". */
135 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
136 struct ovs_list bundle_node
;/* In struct ofbundle's "ports" list. */
137 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
138 struct bfd
*bfd
; /* BFD, if any. */
139 struct lldp
*lldp
; /* lldp, if any. */
140 bool may_enable
; /* May be enabled in bonds. */
141 bool is_tunnel
; /* This port is a tunnel. */
142 bool is_layer3
; /* This is a layer 3 port. */
143 long long int carrier_seq
; /* Carrier status changes. */
144 struct ofport_dpif
*peer
; /* Peer if patch port. */
147 struct stp_port
*stp_port
; /* Spanning Tree Protocol, if any. */
148 enum stp_state stp_state
; /* Always STP_DISABLED if STP not in use. */
149 long long int stp_state_entered
;
151 /* Rapid Spanning Tree. */
152 struct rstp_port
*rstp_port
; /* Rapid Spanning Tree Protocol, if any. */
153 enum rstp_state rstp_state
; /* Always RSTP_DISABLED if RSTP not in use. */
155 /* Queue to DSCP mapping. */
156 struct ofproto_port_queue
*qdscp
;
160 static odp_port_t
ofp_port_to_odp_port(const struct ofproto_dpif
*,
163 static ofp_port_t
odp_port_to_ofp_port(const struct ofproto_dpif
*,
166 static struct ofport_dpif
*
167 ofport_dpif_cast(const struct ofport
*ofport
)
169 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
172 static void port_run(struct ofport_dpif
*);
173 static int set_bfd(struct ofport
*, const struct smap
*);
174 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
175 static int set_lldp(struct ofport
*ofport_
, const struct smap
*cfg
);
176 static void ofport_update_peer(struct ofport_dpif
*);
178 COVERAGE_DEFINE(rev_reconfigure
);
179 COVERAGE_DEFINE(rev_stp
);
180 COVERAGE_DEFINE(rev_rstp
);
181 COVERAGE_DEFINE(rev_bond
);
182 COVERAGE_DEFINE(rev_port_toggled
);
183 COVERAGE_DEFINE(rev_flow_table
);
184 COVERAGE_DEFINE(rev_mac_learning
);
185 COVERAGE_DEFINE(rev_mcast_snooping
);
187 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
188 struct shash all_dpif_backers
= SHASH_INITIALIZER(&all_dpif_backers
);
190 /* All existing ofproto_dpif instances, indexed by ->up.name. */
191 struct hmap all_ofproto_dpifs
= HMAP_INITIALIZER(&all_ofproto_dpifs
);
193 static bool ofproto_use_tnl_push_pop
= true;
194 static void ofproto_unixctl_init(void);
196 static inline struct ofproto_dpif
*
197 ofproto_dpif_cast(const struct ofproto
*ofproto
)
199 ovs_assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
200 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
203 /* Global variables. */
204 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
206 /* Initial mappings of port to bridge mappings. */
207 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
209 /* Initialize 'ofm' for a learn action. If the rule already existed, reference
210 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
211 * rule reference in both cases. */
213 ofproto_dpif_flow_mod_init_for_learn(struct ofproto_dpif
*ofproto
,
214 const struct ofputil_flow_mod
*fm
,
215 struct ofproto_flow_mod
*ofm
)
217 /* This will not take the global 'ofproto_mutex'. */
218 return ofproto_flow_mod_init_for_learn(&ofproto
->up
, fm
, ofm
);
221 /* Appends 'am' to the queue of asynchronous messages to be sent to the
222 * controller. Takes ownership of 'am' and any data it points to. */
224 ofproto_dpif_send_async_msg(struct ofproto_dpif
*ofproto
,
225 struct ofproto_async_msg
*am
)
227 if (!guarded_list_push_back(&ofproto
->ams
, &am
->list_node
, 1024)) {
228 COVERAGE_INC(packet_in_overflow
);
229 ofproto_async_msg_free(am
);
232 /* Wakes up main thread for packet-in I/O. */
233 seq_change(ofproto
->ams_seq
);
236 /* Factory functions. */
239 init(const struct shash
*iface_hints
)
241 struct shash_node
*node
;
243 /* Make a local copy, since we don't own 'iface_hints' elements. */
244 SHASH_FOR_EACH(node
, iface_hints
) {
245 const struct iface_hint
*orig_hint
= node
->data
;
246 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
248 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
249 new_hint
->br_type
= xstrdup(orig_hint
->br_type
);
250 new_hint
->ofp_port
= orig_hint
->ofp_port
;
252 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
255 ofproto_unixctl_init();
256 ofproto_dpif_trace_init();
261 enumerate_types(struct sset
*types
)
263 dp_enumerate_types(types
);
267 enumerate_names(const char *type
, struct sset
*names
)
269 struct ofproto_dpif
*ofproto
;
272 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
273 if (strcmp(type
, ofproto
->up
.type
)) {
276 sset_add(names
, ofproto
->up
.name
);
283 del(const char *type
, const char *name
)
288 error
= dpif_open(name
, type
, &dpif
);
290 error
= dpif_delete(dpif
);
297 port_open_type(const char *datapath_type
, const char *port_type
)
299 return dpif_port_open_type(datapath_type
, port_type
);
302 /* Type functions. */
304 static void process_dpif_port_changes(struct dpif_backer
*);
305 static void process_dpif_all_ports_changed(struct dpif_backer
*);
306 static void process_dpif_port_change(struct dpif_backer
*,
307 const char *devname
);
308 static void process_dpif_port_error(struct dpif_backer
*, int error
);
310 static struct ofproto_dpif
*
311 lookup_ofproto_dpif_by_port_name(const char *name
)
313 struct ofproto_dpif
*ofproto
;
315 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
316 if (sset_contains(&ofproto
->ports
, name
)) {
325 type_run(const char *type
)
327 struct dpif_backer
*backer
;
329 backer
= shash_find_data(&all_dpif_backers
, type
);
331 /* This is not necessarily a problem, since backers are only
332 * created on demand. */
336 if (dpif_run(backer
->dpif
)) {
337 backer
->need_revalidate
= REV_RECONFIGURE
;
340 udpif_run(backer
->udpif
);
342 /* If vswitchd started with other_config:flow_restore_wait set as "true",
343 * and the configuration has now changed to "false", enable receiving
344 * packets from the datapath. */
345 if (!backer
->recv_set_enable
&& !ofproto_get_flow_restore_wait()) {
348 backer
->recv_set_enable
= true;
350 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
352 VLOG_ERR("Failed to enable receiving packets in dpif.");
355 dpif_flow_flush(backer
->dpif
);
356 backer
->need_revalidate
= REV_RECONFIGURE
;
359 if (backer
->recv_set_enable
) {
360 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
363 if (backer
->need_revalidate
) {
364 struct ofproto_dpif
*ofproto
;
365 struct simap_node
*node
;
366 struct simap tmp_backers
;
368 /* Handle tunnel garbage collection. */
369 simap_init(&tmp_backers
);
370 simap_swap(&backer
->tnl_backers
, &tmp_backers
);
372 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
373 struct ofport_dpif
*iter
;
375 if (backer
!= ofproto
->backer
) {
379 HMAP_FOR_EACH (iter
, up
.hmap_node
, &ofproto
->up
.ports
) {
380 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
383 if (!iter
->is_tunnel
) {
387 dp_port
= netdev_vport_get_dpif_port(iter
->up
.netdev
,
388 namebuf
, sizeof namebuf
);
389 node
= simap_find(&tmp_backers
, dp_port
);
391 simap_put(&backer
->tnl_backers
, dp_port
, node
->data
);
392 simap_delete(&tmp_backers
, node
);
393 node
= simap_find(&backer
->tnl_backers
, dp_port
);
395 node
= simap_find(&backer
->tnl_backers
, dp_port
);
397 odp_port_t odp_port
= ODPP_NONE
;
399 if (!dpif_port_add(backer
->dpif
, iter
->up
.netdev
,
401 simap_put(&backer
->tnl_backers
, dp_port
,
402 odp_to_u32(odp_port
));
403 node
= simap_find(&backer
->tnl_backers
, dp_port
);
408 iter
->odp_port
= node
? u32_to_odp(node
->data
) : ODPP_NONE
;
409 if (tnl_port_reconfigure(iter
, iter
->up
.netdev
,
411 ovs_native_tunneling_is_on(ofproto
), dp_port
)) {
412 backer
->need_revalidate
= REV_RECONFIGURE
;
417 SIMAP_FOR_EACH (node
, &tmp_backers
) {
418 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
));
420 simap_destroy(&tmp_backers
);
422 switch (backer
->need_revalidate
) {
423 case REV_RECONFIGURE
: COVERAGE_INC(rev_reconfigure
); break;
424 case REV_STP
: COVERAGE_INC(rev_stp
); break;
425 case REV_RSTP
: COVERAGE_INC(rev_rstp
); break;
426 case REV_BOND
: COVERAGE_INC(rev_bond
); break;
427 case REV_PORT_TOGGLED
: COVERAGE_INC(rev_port_toggled
); break;
428 case REV_FLOW_TABLE
: COVERAGE_INC(rev_flow_table
); break;
429 case REV_MAC_LEARNING
: COVERAGE_INC(rev_mac_learning
); break;
430 case REV_MCAST_SNOOPING
: COVERAGE_INC(rev_mcast_snooping
); break;
432 backer
->need_revalidate
= 0;
435 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
436 struct ofport_dpif
*ofport
;
437 struct ofbundle
*bundle
;
439 if (ofproto
->backer
!= backer
) {
443 xlate_ofproto_set(ofproto
, ofproto
->up
.name
,
444 ofproto
->backer
->dpif
, ofproto
->ml
,
445 ofproto
->stp
, ofproto
->rstp
, ofproto
->ms
,
446 ofproto
->mbridge
, ofproto
->sflow
, ofproto
->ipfix
,
448 ofproto
->up
.forward_bpdu
,
449 connmgr_has_in_band(ofproto
->up
.connmgr
),
450 &ofproto
->backer
->support
);
452 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
453 xlate_bundle_set(ofproto
, bundle
, bundle
->name
,
454 bundle
->vlan_mode
, bundle
->qinq_ethtype
,
455 bundle
->vlan
, bundle
->trunks
, bundle
->cvlans
,
456 bundle
->use_priority_tags
,
457 bundle
->bond
, bundle
->lacp
,
458 bundle
->floodable
, bundle
->protected);
461 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
462 int stp_port
= ofport
->stp_port
463 ? stp_port_no(ofport
->stp_port
)
465 xlate_ofport_set(ofproto
, ofport
->bundle
, ofport
,
466 ofport
->up
.ofp_port
, ofport
->odp_port
,
467 ofport
->up
.netdev
, ofport
->cfm
, ofport
->bfd
,
468 ofport
->lldp
, ofport
->peer
, stp_port
,
469 ofport
->rstp_port
, ofport
->qdscp
,
470 ofport
->n_qdscp
, ofport
->up
.pp
.config
,
471 ofport
->up
.pp
.state
, ofport
->is_tunnel
,
477 udpif_revalidate(backer
->udpif
);
480 process_dpif_port_changes(backer
);
485 /* Check for and handle port changes in 'backer''s dpif. */
487 process_dpif_port_changes(struct dpif_backer
*backer
)
493 error
= dpif_port_poll(backer
->dpif
, &devname
);
499 process_dpif_all_ports_changed(backer
);
503 process_dpif_port_change(backer
, devname
);
508 process_dpif_port_error(backer
, error
);
515 process_dpif_all_ports_changed(struct dpif_backer
*backer
)
517 struct ofproto_dpif
*ofproto
;
518 struct dpif_port dpif_port
;
519 struct dpif_port_dump dump
;
520 struct sset devnames
;
523 sset_init(&devnames
);
524 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
525 if (ofproto
->backer
== backer
) {
526 struct ofport
*ofport
;
528 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
529 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
533 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, backer
->dpif
) {
534 sset_add(&devnames
, dpif_port
.name
);
537 SSET_FOR_EACH (devname
, &devnames
) {
538 process_dpif_port_change(backer
, devname
);
540 sset_destroy(&devnames
);
544 process_dpif_port_change(struct dpif_backer
*backer
, const char *devname
)
546 struct ofproto_dpif
*ofproto
;
547 struct dpif_port port
;
549 /* Don't report on the datapath's device. */
550 if (!strcmp(devname
, dpif_base_name(backer
->dpif
))) {
554 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
,
555 &all_ofproto_dpifs
) {
556 if (simap_contains(&ofproto
->backer
->tnl_backers
, devname
)) {
561 ofproto
= lookup_ofproto_dpif_by_port_name(devname
);
562 if (dpif_port_query_by_name(backer
->dpif
, devname
, &port
)) {
563 /* The port was removed. If we know the datapath,
564 * report it through poll_set(). If we don't, it may be
565 * notifying us of a removal we initiated, so ignore it.
566 * If there's a pending ENOBUFS, let it stand, since
567 * everything will be reevaluated. */
568 if (ofproto
&& ofproto
->port_poll_errno
!= ENOBUFS
) {
569 sset_add(&ofproto
->port_poll_set
, devname
);
570 ofproto
->port_poll_errno
= 0;
572 } else if (!ofproto
) {
573 /* The port was added, but we don't know with which
574 * ofproto we should associate it. Delete it. */
575 dpif_port_del(backer
->dpif
, port
.port_no
);
577 struct ofport_dpif
*ofport
;
579 ofport
= ofport_dpif_cast(shash_find_data(
580 &ofproto
->up
.port_by_name
, devname
));
582 && ofport
->odp_port
!= port
.port_no
583 && !odp_port_to_ofport(backer
, port
.port_no
))
585 /* 'ofport''s datapath port number has changed from
586 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
587 * structures to match. */
588 ovs_rwlock_wrlock(&backer
->odp_to_ofport_lock
);
589 hmap_remove(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
);
590 ofport
->odp_port
= port
.port_no
;
591 hmap_insert(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
,
592 hash_odp_port(port
.port_no
));
593 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
594 backer
->need_revalidate
= REV_RECONFIGURE
;
597 dpif_port_destroy(&port
);
600 /* Propagate 'error' to all ofprotos based on 'backer'. */
602 process_dpif_port_error(struct dpif_backer
*backer
, int error
)
604 struct ofproto_dpif
*ofproto
;
606 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
607 if (ofproto
->backer
== backer
) {
608 sset_clear(&ofproto
->port_poll_set
);
609 ofproto
->port_poll_errno
= error
;
615 type_wait(const char *type
)
617 struct dpif_backer
*backer
;
619 backer
= shash_find_data(&all_dpif_backers
, type
);
621 /* This is not necessarily a problem, since backers are only
622 * created on demand. */
626 dpif_wait(backer
->dpif
);
629 /* Basic life-cycle. */
631 static int add_internal_flows(struct ofproto_dpif
*);
633 static struct ofproto
*
636 struct ofproto_dpif
*ofproto
= xzalloc(sizeof *ofproto
);
641 dealloc(struct ofproto
*ofproto_
)
643 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
648 close_dpif_backer(struct dpif_backer
*backer
)
650 ovs_assert(backer
->refcount
> 0);
652 if (--backer
->refcount
) {
656 udpif_destroy(backer
->udpif
);
658 simap_destroy(&backer
->tnl_backers
);
659 ovs_rwlock_destroy(&backer
->odp_to_ofport_lock
);
660 hmap_destroy(&backer
->odp_to_ofport_map
);
661 shash_find_and_delete(&all_dpif_backers
, backer
->type
);
663 free(backer
->dp_version_string
);
664 dpif_close(backer
->dpif
);
668 /* Datapath port slated for removal from datapath. */
670 struct ovs_list list_node
;
674 static bool check_variable_length_userdata(struct dpif_backer
*backer
);
675 static void check_support(struct dpif_backer
*backer
);
678 open_dpif_backer(const char *type
, struct dpif_backer
**backerp
)
680 struct dpif_backer
*backer
;
681 struct dpif_port_dump port_dump
;
682 struct dpif_port port
;
683 struct shash_node
*node
;
684 struct ovs_list garbage_list
;
685 struct odp_garbage
*garbage
;
692 backer
= shash_find_data(&all_dpif_backers
, type
);
699 backer_name
= xasprintf("ovs-%s", type
);
701 /* Remove any existing datapaths, since we assume we're the only
702 * userspace controlling the datapath. */
704 dp_enumerate_names(type
, &names
);
705 SSET_FOR_EACH(name
, &names
) {
706 struct dpif
*old_dpif
;
708 /* Don't remove our backer if it exists. */
709 if (!strcmp(name
, backer_name
)) {
713 if (dpif_open(name
, type
, &old_dpif
)) {
714 VLOG_WARN("couldn't open old datapath %s to remove it", name
);
716 dpif_delete(old_dpif
);
717 dpif_close(old_dpif
);
720 sset_destroy(&names
);
722 backer
= xmalloc(sizeof *backer
);
724 error
= dpif_create_and_open(backer_name
, type
, &backer
->dpif
);
727 VLOG_ERR("failed to open datapath of type %s: %s", type
,
728 ovs_strerror(error
));
732 backer
->udpif
= udpif_create(backer
, backer
->dpif
);
734 backer
->type
= xstrdup(type
);
735 backer
->refcount
= 1;
736 hmap_init(&backer
->odp_to_ofport_map
);
737 ovs_rwlock_init(&backer
->odp_to_ofport_lock
);
738 backer
->need_revalidate
= 0;
739 simap_init(&backer
->tnl_backers
);
740 backer
->recv_set_enable
= !ofproto_get_flow_restore_wait();
743 if (backer
->recv_set_enable
) {
744 dpif_flow_flush(backer
->dpif
);
747 /* Loop through the ports already on the datapath and remove any
748 * that we don't need anymore. */
749 ovs_list_init(&garbage_list
);
750 dpif_port_dump_start(&port_dump
, backer
->dpif
);
751 while (dpif_port_dump_next(&port_dump
, &port
)) {
752 node
= shash_find(&init_ofp_ports
, port
.name
);
753 if (!node
&& strcmp(port
.name
, dpif_base_name(backer
->dpif
))) {
754 garbage
= xmalloc(sizeof *garbage
);
755 garbage
->odp_port
= port
.port_no
;
756 ovs_list_push_front(&garbage_list
, &garbage
->list_node
);
759 dpif_port_dump_done(&port_dump
);
761 LIST_FOR_EACH_POP (garbage
, list_node
, &garbage_list
) {
762 dpif_port_del(backer
->dpif
, garbage
->odp_port
);
766 shash_add(&all_dpif_backers
, type
, backer
);
768 check_support(backer
);
769 atomic_count_init(&backer
->tnl_count
, 0);
771 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
773 VLOG_ERR("failed to listen on datapath of type %s: %s",
774 type
, ovs_strerror(error
));
775 close_dpif_backer(backer
);
779 if (backer
->recv_set_enable
) {
780 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
783 /* This check fails if performed before udpif threads have been set,
784 * as the kernel module checks that the 'pid' in userspace action
786 backer
->support
.variable_length_userdata
787 = check_variable_length_userdata(backer
);
788 backer
->dp_version_string
= dpif_get_dp_version(backer
->dpif
);
794 ovs_native_tunneling_is_on(struct ofproto_dpif
*ofproto
)
796 return ofproto_use_tnl_push_pop
&& ofproto
->backer
->support
.tnl_push_pop
&&
797 atomic_count_get(&ofproto
->backer
->tnl_count
);
800 /* Tests whether 'backer''s datapath supports recirculation. Only newer
801 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
802 * features on older datapaths that don't support this feature.
804 * Returns false if 'backer' definitely does not support recirculation, true if
805 * it seems to support recirculation or if at least the error we get is
808 check_recirc(struct dpif_backer
*backer
)
811 struct odputil_keybuf keybuf
;
814 struct odp_flow_key_parms odp_parms
= {
821 memset(&flow
, 0, sizeof flow
);
825 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
826 odp_flow_key_from_flow(&odp_parms
, &key
);
827 enable_recirc
= dpif_probe_feature(backer
->dpif
, "recirculation", &key
,
831 VLOG_INFO("%s: Datapath supports recirculation",
832 dpif_name(backer
->dpif
));
834 VLOG_INFO("%s: Datapath does not support recirculation",
835 dpif_name(backer
->dpif
));
838 return enable_recirc
;
841 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
842 * some flow attributes for datapaths that support this feature.
844 * Returns true if 'dpif' supports UFID for flow operations.
845 * Returns false if 'dpif' does not support UFID. */
847 check_ufid(struct dpif_backer
*backer
)
850 struct odputil_keybuf keybuf
;
854 struct odp_flow_key_parms odp_parms
= {
858 memset(&flow
, 0, sizeof flow
);
859 flow
.dl_type
= htons(0x1234);
861 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
862 odp_flow_key_from_flow(&odp_parms
, &key
);
863 dpif_flow_hash(backer
->dpif
, key
.data
, key
.size
, &ufid
);
865 enable_ufid
= dpif_probe_feature(backer
->dpif
, "UFID", &key
, NULL
, &ufid
);
868 VLOG_INFO("%s: Datapath supports unique flow ids",
869 dpif_name(backer
->dpif
));
871 VLOG_INFO("%s: Datapath does not support unique flow ids",
872 dpif_name(backer
->dpif
));
877 /* Tests whether 'backer''s datapath supports variable-length
878 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
879 * to disable some features on older datapaths that don't support this
882 * Returns false if 'backer' definitely does not support variable-length
883 * userdata, true if it seems to support them or if at least the error we get
886 check_variable_length_userdata(struct dpif_backer
*backer
)
888 struct eth_header
*eth
;
889 struct ofpbuf actions
;
890 struct dpif_execute execute
;
891 struct dp_packet packet
;
896 /* Compose a userspace action that will cause an ERANGE error on older
897 * datapaths that don't support variable-length userdata.
899 * We really test for using userdata longer than 8 bytes, but older
900 * datapaths accepted these, silently truncating the userdata to 8 bytes.
901 * The same older datapaths rejected userdata shorter than 8 bytes, so we
902 * test for that instead as a proxy for longer userdata support. */
903 ofpbuf_init(&actions
, 64);
904 start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_USERSPACE
);
905 nl_msg_put_u32(&actions
, OVS_USERSPACE_ATTR_PID
,
906 dpif_port_get_pid(backer
->dpif
, ODPP_NONE
, 0));
907 nl_msg_put_unspec_zero(&actions
, OVS_USERSPACE_ATTR_USERDATA
, 4);
908 nl_msg_end_nested(&actions
, start
);
910 /* Compose a dummy ethernet packet. */
911 dp_packet_init(&packet
, ETH_HEADER_LEN
);
912 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
913 eth
->eth_type
= htons(0x1234);
915 flow_extract(&packet
, &flow
);
917 /* Execute the actions. On older datapaths this fails with ERANGE, on
918 * newer datapaths it succeeds. */
919 execute
.actions
= actions
.data
;
920 execute
.actions_len
= actions
.size
;
921 execute
.packet
= &packet
;
922 execute
.flow
= &flow
;
923 execute
.needs_help
= false;
924 execute
.probe
= true;
927 error
= dpif_execute(backer
->dpif
, &execute
);
929 dp_packet_uninit(&packet
);
930 ofpbuf_uninit(&actions
);
937 /* Variable-length userdata is not supported. */
938 VLOG_WARN("%s: datapath does not support variable-length userdata "
939 "feature (needs Linux 3.10+ or kernel module from OVS "
940 "1..11+). The NXAST_SAMPLE action will be ignored.",
941 dpif_name(backer
->dpif
));
945 /* Something odd happened. We're not sure whether variable-length
946 * userdata is supported. Default to "yes". */
947 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
948 dpif_name(backer
->dpif
), ovs_strerror(error
));
953 /* Tests number of 802.1q VLAN headers supported by 'backer''s datapath.
955 * Returns the number of elements in a struct flow's vlan
956 * if the datapath supports at least that many VLAN headers. */
958 check_max_vlan_headers(struct dpif_backer
*backer
)
961 struct odp_flow_key_parms odp_parms
= {
967 memset(&flow
, 0, sizeof flow
);
968 flow
.dl_type
= htons(ETH_TYPE_IP
);
969 for (n
= 0; n
< FLOW_MAX_VLAN_HEADERS
; n
++) {
970 struct odputil_keybuf keybuf
;
973 flow_push_vlan_uninit(&flow
, NULL
);
974 flow
.vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
975 flow
.vlans
[0].tci
= htons(1) | htons(VLAN_CFI
);
977 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
978 odp_flow_key_from_flow(&odp_parms
, &key
);
979 if (!dpif_probe_feature(backer
->dpif
, "VLAN", &key
, NULL
, NULL
)) {
984 VLOG_INFO("%s: VLAN header stack length probed as %d",
985 dpif_name(backer
->dpif
), n
);
988 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
990 * Returns the number of elements in a struct flow's mpls_lse field
991 * if the datapath supports at least that many entries in an
993 * Otherwise returns the number of MPLS push actions supported by
996 check_max_mpls_depth(struct dpif_backer
*backer
)
1001 for (n
= 0; n
< FLOW_MAX_MPLS_LABELS
; n
++) {
1002 struct odputil_keybuf keybuf
;
1004 struct odp_flow_key_parms odp_parms
= {
1008 memset(&flow
, 0, sizeof flow
);
1009 flow
.dl_type
= htons(ETH_TYPE_MPLS
);
1010 flow_set_mpls_bos(&flow
, n
, 1);
1012 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1013 odp_flow_key_from_flow(&odp_parms
, &key
);
1014 if (!dpif_probe_feature(backer
->dpif
, "MPLS", &key
, NULL
, NULL
)) {
1019 VLOG_INFO("%s: MPLS label stack length probed as %d",
1020 dpif_name(backer
->dpif
), n
);
1025 add_sample_actions(struct ofpbuf
*actions
, int nesting
)
1028 nl_msg_put_odp_port(actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
1032 size_t start
, actions_start
;
1034 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1035 actions_start
= nl_msg_start_nested(actions
, OVS_SAMPLE_ATTR_ACTIONS
);
1036 add_sample_actions(actions
, nesting
- 1);
1037 nl_msg_end_nested(actions
, actions_start
);
1038 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
, UINT32_MAX
);
1039 nl_msg_end_nested(actions
, start
);
1042 /* Tests the nested sample actions levels supported by 'backer''s datapath.
1044 * Returns the number of nested sample actions accepted by the datapath. */
1046 check_max_sample_nesting(struct dpif_backer
*backer
)
1048 struct odputil_keybuf keybuf
;
1053 struct odp_flow_key_parms odp_parms
= {
1057 memset(&flow
, 0, sizeof flow
);
1058 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1059 odp_flow_key_from_flow(&odp_parms
, &key
);
1061 /* OVS datapath has always supported at least 3 nested levels. */
1062 for (n
= 3; n
< FLOW_MAX_SAMPLE_NESTING
; n
++) {
1063 struct ofpbuf actions
;
1066 ofpbuf_init(&actions
, 300);
1067 add_sample_actions(&actions
, n
);
1068 ok
= dpif_probe_feature(backer
->dpif
, "Sample action nesting", &key
,
1070 ofpbuf_uninit(&actions
);
1076 VLOG_INFO("%s: Max sample nesting level probed as %d",
1077 dpif_name(backer
->dpif
), n
);
1081 /* Tests whether 'backer''s datapath supports masked data in
1082 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1083 * datapaths that don't support this feature. */
1085 check_masked_set_action(struct dpif_backer
*backer
)
1087 struct eth_header
*eth
;
1088 struct ofpbuf actions
;
1089 struct dpif_execute execute
;
1090 struct dp_packet packet
;
1093 struct ovs_key_ethernet key
, mask
;
1095 /* Compose a set action that will cause an EINVAL error on older
1096 * datapaths that don't support masked set actions.
1097 * Avoid using a full mask, as it could be translated to a non-masked
1098 * set action instead. */
1099 ofpbuf_init(&actions
, 64);
1100 memset(&key
, 0x53, sizeof key
);
1101 memset(&mask
, 0x7f, sizeof mask
);
1102 commit_masked_set_action(&actions
, OVS_KEY_ATTR_ETHERNET
, &key
, &mask
,
1105 /* Compose a dummy ethernet packet. */
1106 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1107 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1108 eth
->eth_type
= htons(0x1234);
1110 flow_extract(&packet
, &flow
);
1112 /* Execute the actions. On older datapaths this fails with EINVAL, on
1113 * newer datapaths it succeeds. */
1114 execute
.actions
= actions
.data
;
1115 execute
.actions_len
= actions
.size
;
1116 execute
.packet
= &packet
;
1117 execute
.flow
= &flow
;
1118 execute
.needs_help
= false;
1119 execute
.probe
= true;
1122 error
= dpif_execute(backer
->dpif
, &execute
);
1124 dp_packet_uninit(&packet
);
1125 ofpbuf_uninit(&actions
);
1128 /* Masked set action is not supported. */
1129 VLOG_INFO("%s: datapath does not support masked set action feature.",
1130 dpif_name(backer
->dpif
));
1135 /* Tests whether 'backer''s datapath supports truncation of a packet in
1136 * OVS_ACTION_ATTR_TRUNC. We need to disable some features on older
1137 * datapaths that don't support this feature. */
1139 check_trunc_action(struct dpif_backer
*backer
)
1141 struct eth_header
*eth
;
1142 struct ofpbuf actions
;
1143 struct dpif_execute execute
;
1144 struct dp_packet packet
;
1145 struct ovs_action_trunc
*trunc
;
1149 /* Compose an action with output(port:1,
1150 * max_len:OVS_ACTION_OUTPUT_MIN + 1).
1151 * This translates to one truncate action and one output action. */
1152 ofpbuf_init(&actions
, 64);
1153 trunc
= nl_msg_put_unspec_uninit(&actions
,
1154 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1156 trunc
->max_len
= ETH_HEADER_LEN
+ 1;
1157 nl_msg_put_odp_port(&actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
1159 /* Compose a dummy Ethernet packet. */
1160 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1161 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1162 eth
->eth_type
= htons(0x1234);
1164 flow_extract(&packet
, &flow
);
1166 /* Execute the actions. On older datapaths this fails with EINVAL, on
1167 * newer datapaths it succeeds. */
1168 execute
.actions
= actions
.data
;
1169 execute
.actions_len
= actions
.size
;
1170 execute
.packet
= &packet
;
1171 execute
.flow
= &flow
;
1172 execute
.needs_help
= false;
1173 execute
.probe
= true;
1176 error
= dpif_execute(backer
->dpif
, &execute
);
1178 dp_packet_uninit(&packet
);
1179 ofpbuf_uninit(&actions
);
1182 VLOG_INFO("%s: Datapath does not support truncate action",
1183 dpif_name(backer
->dpif
));
1185 VLOG_INFO("%s: Datapath supports truncate action",
1186 dpif_name(backer
->dpif
));
1192 /* Tests whether 'backer''s datapath supports the clone action
1193 * OVS_ACTION_ATTR_CLONE. */
1195 check_clone(struct dpif_backer
*backer
)
1197 struct dpif_execute execute
;
1198 struct eth_header
*eth
;
1200 struct dp_packet packet
;
1201 struct ofpbuf actions
;
1205 /* Compose clone with an empty action list.
1206 * and check if datapath can decode the message. */
1207 ofpbuf_init(&actions
, 64);
1208 clone_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CLONE
);
1209 nl_msg_end_nested(&actions
, clone_start
);
1211 /* Compose a dummy Ethernet packet. */
1212 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1213 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1214 eth
->eth_type
= htons(0x1234);
1216 flow_extract(&packet
, &flow
);
1218 /* Execute the actions. On older datapaths this fails with EINVAL, on
1219 * newer datapaths it succeeds. */
1220 execute
.actions
= actions
.data
;
1221 execute
.actions_len
= actions
.size
;
1222 execute
.packet
= &packet
;
1223 execute
.flow
= &flow
;
1224 execute
.needs_help
= false;
1225 execute
.probe
= true;
1228 error
= dpif_execute(backer
->dpif
, &execute
);
1230 dp_packet_uninit(&packet
);
1231 ofpbuf_uninit(&actions
);
1234 VLOG_INFO("%s: Datapath does not support clone action",
1235 dpif_name(backer
->dpif
));
1237 VLOG_INFO("%s: Datapath supports clone action",
1238 dpif_name(backer
->dpif
));
1244 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE) \
1246 check_##NAME(struct dpif_backer *backer) \
1249 struct odputil_keybuf keybuf; \
1250 struct ofpbuf key; \
1252 struct odp_flow_key_parms odp_parms = { \
1259 memset(&flow, 0, sizeof flow); \
1260 flow.FIELD = VALUE; \
1262 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1263 odp_flow_key_from_flow(&odp_parms, &key); \
1264 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL, NULL); \
1267 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1269 VLOG_INFO("%s: Datapath does not support "#NAME, \
1270 dpif_name(backer->dpif)); \
1275 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1)
1277 CHECK_FEATURE(ct_state
)
1278 CHECK_FEATURE(ct_zone
)
1279 CHECK_FEATURE(ct_mark
)
1280 CHECK_FEATURE__(ct_label
, ct_label
, ct_label
.u64
.lo
, 1)
1281 CHECK_FEATURE__(ct_state_nat
, ct_state
, ct_state
, CS_TRACKED
|CS_SRC_NAT
)
1282 CHECK_FEATURE__(ct_orig_tuple
, ct_orig_tuple
, ct_nw_proto
, 1)
1284 #undef CHECK_FEATURE
1285 #undef CHECK_FEATURE__
1288 check_support(struct dpif_backer
*backer
)
1290 /* This feature needs to be tested after udpif threads are set. */
1291 backer
->support
.variable_length_userdata
= false;
1294 backer
->support
.odp
.recirc
= check_recirc(backer
);
1295 backer
->support
.odp
.max_vlan_headers
= check_max_vlan_headers(backer
);
1296 backer
->support
.odp
.max_mpls_depth
= check_max_mpls_depth(backer
);
1297 backer
->support
.masked_set_action
= check_masked_set_action(backer
);
1298 backer
->support
.trunc
= check_trunc_action(backer
);
1299 backer
->support
.ufid
= check_ufid(backer
);
1300 backer
->support
.tnl_push_pop
= dpif_supports_tnl_push_pop(backer
->dpif
);
1301 backer
->support
.clone
= check_clone(backer
);
1302 backer
->support
.sample_nesting
= check_max_sample_nesting(backer
);
1305 backer
->support
.odp
.ct_state
= check_ct_state(backer
);
1306 backer
->support
.odp
.ct_zone
= check_ct_zone(backer
);
1307 backer
->support
.odp
.ct_mark
= check_ct_mark(backer
);
1308 backer
->support
.odp
.ct_label
= check_ct_label(backer
);
1310 backer
->support
.odp
.ct_state_nat
= check_ct_state_nat(backer
);
1311 backer
->support
.odp
.ct_orig_tuple
= check_ct_orig_tuple(backer
);
1315 construct(struct ofproto
*ofproto_
)
1317 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1318 struct shash_node
*node
, *next
;
1321 /* Tunnel module can get used right after the udpif threads are running. */
1322 ofproto_tunnel_init();
1324 error
= open_dpif_backer(ofproto
->up
.type
, &ofproto
->backer
);
1329 uuid_generate(&ofproto
->uuid
);
1330 atomic_init(&ofproto
->tables_version
, OVS_VERSION_MIN
);
1331 ofproto
->netflow
= NULL
;
1332 ofproto
->sflow
= NULL
;
1333 ofproto
->ipfix
= NULL
;
1334 ofproto
->stp
= NULL
;
1335 ofproto
->rstp
= NULL
;
1336 ofproto
->dump_seq
= 0;
1337 hmap_init(&ofproto
->bundles
);
1338 ofproto
->ml
= mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME
);
1340 ofproto
->mbridge
= mbridge_create();
1341 ofproto
->has_bonded_bundles
= false;
1342 ofproto
->lacp_enabled
= false;
1343 ovs_mutex_init_adaptive(&ofproto
->stats_mutex
);
1345 guarded_list_init(&ofproto
->ams
);
1347 sset_init(&ofproto
->ports
);
1348 sset_init(&ofproto
->ghost_ports
);
1349 sset_init(&ofproto
->port_poll_set
);
1350 ofproto
->port_poll_errno
= 0;
1351 ofproto
->change_seq
= 0;
1352 ofproto
->ams_seq
= seq_create();
1353 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1356 SHASH_FOR_EACH_SAFE (node
, next
, &init_ofp_ports
) {
1357 struct iface_hint
*iface_hint
= node
->data
;
1359 if (!strcmp(iface_hint
->br_name
, ofproto
->up
.name
)) {
1360 /* Check if the datapath already has this port. */
1361 if (dpif_port_exists(ofproto
->backer
->dpif
, node
->name
)) {
1362 sset_add(&ofproto
->ports
, node
->name
);
1365 free(iface_hint
->br_name
);
1366 free(iface_hint
->br_type
);
1368 shash_delete(&init_ofp_ports
, node
);
1372 hmap_insert(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
,
1373 hash_string(ofproto
->up
.name
, 0));
1374 memset(&ofproto
->stats
, 0, sizeof ofproto
->stats
);
1376 ofproto_init_tables(ofproto_
, N_TABLES
);
1377 error
= add_internal_flows(ofproto
);
1379 ofproto
->up
.tables
[TBL_INTERNAL
].flags
= OFTABLE_HIDDEN
| OFTABLE_READONLY
;
1385 add_internal_miss_flow(struct ofproto_dpif
*ofproto
, int id
,
1386 const struct ofpbuf
*ofpacts
, struct rule_dpif
**rulep
)
1392 match_init_catchall(&match
);
1393 match_set_reg(&match
, 0, id
);
1395 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 0, 0, ofpacts
,
1397 *rulep
= error
? NULL
: rule_dpif_cast(rule
);
1403 add_internal_flows(struct ofproto_dpif
*ofproto
)
1405 struct ofpact_controller
*controller
;
1406 uint64_t ofpacts_stub
[128 / 8];
1407 struct ofpbuf ofpacts
;
1408 struct rule
*unused_rulep OVS_UNUSED
;
1413 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1416 controller
= ofpact_put_CONTROLLER(&ofpacts
);
1417 controller
->max_len
= UINT16_MAX
;
1418 controller
->controller_id
= 0;
1419 controller
->reason
= OFPR_IMPLICIT_MISS
;
1420 ofpact_finish_CONTROLLER(&ofpacts
, &controller
);
1422 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1423 &ofproto
->miss_rule
);
1428 ofpbuf_clear(&ofpacts
);
1429 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1430 &ofproto
->no_packet_in_rule
);
1435 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1436 &ofproto
->drop_frags_rule
);
1441 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1442 * zero when reaching this rule.
1444 * (priority=2), recirc_id=0, actions=drop
1446 ofpbuf_clear(&ofpacts
);
1447 match_init_catchall(&match
);
1448 match_set_recirc_id(&match
, 0);
1449 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 2, 0, &ofpacts
,
1455 destruct(struct ofproto
*ofproto_
)
1457 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1458 struct ofproto_async_msg
*am
;
1459 struct rule_dpif
*rule
;
1460 struct oftable
*table
;
1461 struct ovs_list ams
;
1463 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1465 xlate_remove_ofproto(ofproto
);
1468 /* Ensure that the upcall processing threads have no remaining references
1469 * to the ofproto or anything in it. */
1470 udpif_synchronize(ofproto
->backer
->udpif
);
1472 hmap_remove(&all_ofproto_dpifs
, &ofproto
->all_ofproto_dpifs_node
);
1474 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1475 CLS_FOR_EACH (rule
, up
.cr
, &table
->cls
) {
1476 ofproto_rule_delete(&ofproto
->up
, &rule
->up
);
1479 ofproto_group_delete_all(&ofproto
->up
);
1481 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1482 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1483 ofproto_async_msg_free(am
);
1485 guarded_list_destroy(&ofproto
->ams
);
1487 recirc_free_ofproto(ofproto
, ofproto
->up
.name
);
1489 mbridge_unref(ofproto
->mbridge
);
1491 netflow_unref(ofproto
->netflow
);
1492 dpif_sflow_unref(ofproto
->sflow
);
1493 dpif_ipfix_unref(ofproto
->ipfix
);
1494 hmap_destroy(&ofproto
->bundles
);
1495 mac_learning_unref(ofproto
->ml
);
1496 mcast_snooping_unref(ofproto
->ms
);
1498 sset_destroy(&ofproto
->ports
);
1499 sset_destroy(&ofproto
->ghost_ports
);
1500 sset_destroy(&ofproto
->port_poll_set
);
1502 ovs_mutex_destroy(&ofproto
->stats_mutex
);
1504 seq_destroy(ofproto
->ams_seq
);
1506 close_dpif_backer(ofproto
->backer
);
1510 run(struct ofproto
*ofproto_
)
1512 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1513 uint64_t new_seq
, new_dump_seq
;
1515 if (mbridge_need_revalidate(ofproto
->mbridge
)) {
1516 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1517 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1518 mac_learning_flush(ofproto
->ml
);
1519 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1520 mcast_snooping_mdb_flush(ofproto
->ms
);
1523 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1524 * flow restore. Even though nothing is processed during flow restore,
1525 * all queued 'ams' will be handled immediately when flow restore
1527 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1529 /* Do not perform any periodic activity required by 'ofproto' while
1530 * waiting for flow restore to complete. */
1531 if (!ofproto_get_flow_restore_wait()) {
1532 struct ofproto_async_msg
*am
;
1533 struct ovs_list ams
;
1535 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1536 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1537 connmgr_send_async_msg(ofproto
->up
.connmgr
, am
);
1538 ofproto_async_msg_free(am
);
1542 if (ofproto
->netflow
) {
1543 netflow_run(ofproto
->netflow
);
1545 if (ofproto
->sflow
) {
1546 dpif_sflow_run(ofproto
->sflow
);
1548 if (ofproto
->ipfix
) {
1549 dpif_ipfix_run(ofproto
->ipfix
);
1552 new_seq
= seq_read(connectivity_seq_get());
1553 if (ofproto
->change_seq
!= new_seq
) {
1554 struct ofport_dpif
*ofport
;
1556 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1560 ofproto
->change_seq
= new_seq
;
1562 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1563 struct ofbundle
*bundle
;
1565 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1572 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1573 if (mac_learning_run(ofproto
->ml
)) {
1574 ofproto
->backer
->need_revalidate
= REV_MAC_LEARNING
;
1576 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1578 if (mcast_snooping_run(ofproto
->ms
)) {
1579 ofproto
->backer
->need_revalidate
= REV_MCAST_SNOOPING
;
1582 new_dump_seq
= seq_read(udpif_dump_seq(ofproto
->backer
->udpif
));
1583 if (ofproto
->dump_seq
!= new_dump_seq
) {
1584 struct rule
*rule
, *next_rule
;
1585 long long now
= time_msec();
1587 /* We know stats are relatively fresh, so now is a good time to do some
1589 ofproto
->dump_seq
= new_dump_seq
;
1591 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1593 ovs_mutex_lock(&ofproto_mutex
);
1594 LIST_FOR_EACH_SAFE (rule
, next_rule
, expirable
,
1595 &ofproto
->up
.expirable
) {
1596 rule_expire(rule_dpif_cast(rule
), now
);
1598 ovs_mutex_unlock(&ofproto_mutex
);
1600 /* All outstanding data in existing flows has been accounted, so it's a
1601 * good time to do bond rebalancing. */
1602 if (ofproto
->has_bonded_bundles
) {
1603 struct ofbundle
*bundle
;
1605 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1607 bond_rebalance(bundle
->bond
);
1616 ofproto_dpif_wait(struct ofproto
*ofproto_
)
1618 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1620 if (ofproto_get_flow_restore_wait()) {
1624 if (ofproto
->sflow
) {
1625 dpif_sflow_wait(ofproto
->sflow
);
1627 if (ofproto
->ipfix
) {
1628 dpif_ipfix_wait(ofproto
->ipfix
);
1630 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1631 struct ofbundle
*bundle
;
1633 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1634 bundle_wait(bundle
);
1637 if (ofproto
->netflow
) {
1638 netflow_wait(ofproto
->netflow
);
1640 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
1641 mac_learning_wait(ofproto
->ml
);
1642 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1643 mcast_snooping_wait(ofproto
->ms
);
1645 if (ofproto
->backer
->need_revalidate
) {
1646 poll_immediate_wake();
1649 seq_wait(udpif_dump_seq(ofproto
->backer
->udpif
), ofproto
->dump_seq
);
1650 seq_wait(ofproto
->ams_seq
, ofproto
->ams_seqno
);
1654 type_get_memory_usage(const char *type
, struct simap
*usage
)
1656 struct dpif_backer
*backer
;
1658 backer
= shash_find_data(&all_dpif_backers
, type
);
1660 udpif_get_memory_usage(backer
->udpif
, usage
);
1665 flush(struct ofproto
*ofproto_
)
1667 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1668 struct dpif_backer
*backer
= ofproto
->backer
;
1671 udpif_flush(backer
->udpif
);
1676 query_tables(struct ofproto
*ofproto
,
1677 struct ofputil_table_features
*features
,
1678 struct ofputil_table_stats
*stats
)
1680 strcpy(features
->name
, "classifier");
1685 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
1686 unsigned long missed
, matched
;
1688 atomic_read_relaxed(&ofproto
->tables
[i
].n_matched
, &matched
);
1689 atomic_read_relaxed(&ofproto
->tables
[i
].n_missed
, &missed
);
1691 stats
[i
].matched_count
= matched
;
1692 stats
[i
].lookup_count
= matched
+ missed
;
1698 set_tables_version(struct ofproto
*ofproto_
, ovs_version_t version
)
1700 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1702 /* Use memory_order_release to signify that any prior memory accesses can
1703 * not be reordered to happen after this atomic store. This makes sure the
1704 * new version is properly set up when the readers can read this 'version'
1706 atomic_store_explicit(&ofproto
->tables_version
, version
,
1707 memory_order_release
);
1708 /* 'need_revalidate' can be reordered to happen before the atomic_store
1709 * above, but it does not matter as this variable is not accessed by other
1711 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
1714 static struct ofport
*
1717 struct ofport_dpif
*port
= xzalloc(sizeof *port
);
1722 port_dealloc(struct ofport
*port_
)
1724 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1729 port_construct(struct ofport
*port_
)
1731 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1732 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1733 const struct netdev
*netdev
= port
->up
.netdev
;
1734 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1735 const char *dp_port_name
;
1736 struct dpif_port dpif_port
;
1739 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1740 port
->bundle
= NULL
;
1744 port
->may_enable
= false;
1745 port
->stp_port
= NULL
;
1746 port
->stp_state
= STP_DISABLED
;
1747 port
->rstp_port
= NULL
;
1748 port
->rstp_state
= RSTP_DISABLED
;
1749 port
->is_tunnel
= false;
1753 port
->carrier_seq
= netdev_get_carrier_resets(netdev
);
1754 port
->is_layer3
= netdev_vport_is_layer3(netdev
);
1756 if (netdev_vport_is_patch(netdev
)) {
1757 /* By bailing out here, we don't submit the port to the sFlow module
1758 * to be considered for counter polling export. This is correct
1759 * because the patch port represents an interface that sFlow considers
1760 * to be "internal" to the switch as a whole, and therefore not a
1761 * candidate for counter polling. */
1762 port
->odp_port
= ODPP_NONE
;
1763 ofport_update_peer(port
);
1767 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1768 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
, dp_port_name
,
1774 port
->odp_port
= dpif_port
.port_no
;
1776 if (netdev_get_tunnel_config(netdev
)) {
1777 atomic_count_inc(&ofproto
->backer
->tnl_count
);
1778 error
= tnl_port_add(port
, port
->up
.netdev
, port
->odp_port
,
1779 ovs_native_tunneling_is_on(ofproto
), dp_port_name
);
1781 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1782 dpif_port_destroy(&dpif_port
);
1786 port
->is_tunnel
= true;
1787 if (ofproto
->ipfix
) {
1788 dpif_ipfix_add_tunnel_port(ofproto
->ipfix
, port_
, port
->odp_port
);
1791 /* Sanity-check that a mapping doesn't already exist. This
1792 * shouldn't happen for non-tunnel ports. */
1793 if (odp_port_to_ofp_port(ofproto
, port
->odp_port
) != OFPP_NONE
) {
1794 VLOG_ERR("port %s already has an OpenFlow port number",
1796 dpif_port_destroy(&dpif_port
);
1800 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1801 hmap_insert(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
,
1802 hash_odp_port(port
->odp_port
));
1803 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1805 dpif_port_destroy(&dpif_port
);
1807 if (ofproto
->sflow
) {
1808 dpif_sflow_add_port(ofproto
->sflow
, port_
, port
->odp_port
);
1815 port_destruct(struct ofport
*port_
, bool del
)
1817 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1818 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1819 const char *devname
= netdev_get_name(port
->up
.netdev
);
1820 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1821 const char *dp_port_name
;
1823 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1825 xlate_ofport_remove(port
);
1828 dp_port_name
= netdev_vport_get_dpif_port(port
->up
.netdev
, namebuf
,
1830 if (del
&& dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
1831 /* The underlying device is still there, so delete it. This
1832 * happens when the ofproto is being destroyed, since the caller
1833 * assumes that removal of attached ports will happen as part of
1835 if (!port
->is_tunnel
) {
1836 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
);
1841 port
->peer
->peer
= NULL
;
1845 if (port
->odp_port
!= ODPP_NONE
&& !port
->is_tunnel
) {
1846 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1847 hmap_remove(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
);
1848 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1851 if (port
->is_tunnel
) {
1852 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1855 if (port
->is_tunnel
&& ofproto
->ipfix
) {
1856 dpif_ipfix_del_tunnel_port(ofproto
->ipfix
, port
->odp_port
);
1860 sset_find_and_delete(&ofproto
->ports
, devname
);
1861 sset_find_and_delete(&ofproto
->ghost_ports
, devname
);
1862 bundle_remove(port_
);
1863 set_cfm(port_
, NULL
);
1864 set_bfd(port_
, NULL
);
1865 set_lldp(port_
, NULL
);
1866 if (port
->stp_port
) {
1867 stp_port_disable(port
->stp_port
);
1869 set_rstp_port(port_
, NULL
);
1870 if (ofproto
->sflow
) {
1871 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
1878 port_modified(struct ofport
*port_
)
1880 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1881 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1882 const char *dp_port_name
;
1883 struct netdev
*netdev
= port
->up
.netdev
;
1885 if (port
->bundle
&& port
->bundle
->bond
) {
1886 bond_slave_set_netdev(port
->bundle
->bond
, port
, netdev
);
1890 cfm_set_netdev(port
->cfm
, netdev
);
1894 bfd_set_netdev(port
->bfd
, netdev
);
1897 ofproto_dpif_monitor_port_update(port
, port
->bfd
, port
->cfm
,
1898 port
->lldp
, &port
->up
.pp
.hw_addr
);
1900 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1902 if (port
->is_tunnel
) {
1903 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1905 if (tnl_port_reconfigure(port
, netdev
, port
->odp_port
,
1906 ovs_native_tunneling_is_on(ofproto
),
1908 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1912 ofport_update_peer(port
);
1916 port_reconfigured(struct ofport
*port_
, enum ofputil_port_config old_config
)
1918 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1919 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1920 enum ofputil_port_config changed
= old_config
^ port
->up
.pp
.config
;
1922 if (changed
& (OFPUTIL_PC_NO_RECV
| OFPUTIL_PC_NO_RECV_STP
|
1923 OFPUTIL_PC_NO_FWD
| OFPUTIL_PC_NO_FLOOD
|
1924 OFPUTIL_PC_NO_PACKET_IN
)) {
1925 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1927 if (changed
& OFPUTIL_PC_NO_FLOOD
&& port
->bundle
) {
1928 bundle_update(port
->bundle
);
1934 set_sflow(struct ofproto
*ofproto_
,
1935 const struct ofproto_sflow_options
*sflow_options
)
1937 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1938 struct dpif_sflow
*ds
= ofproto
->sflow
;
1940 if (sflow_options
) {
1941 uint32_t old_probability
= ds
? dpif_sflow_get_probability(ds
) : 0;
1943 struct ofport_dpif
*ofport
;
1945 ds
= ofproto
->sflow
= dpif_sflow_create();
1946 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1947 dpif_sflow_add_port(ds
, &ofport
->up
, ofport
->odp_port
);
1950 dpif_sflow_set_options(ds
, sflow_options
);
1951 if (dpif_sflow_get_probability(ds
) != old_probability
) {
1952 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1956 dpif_sflow_unref(ds
);
1957 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1958 ofproto
->sflow
= NULL
;
1966 struct ofproto
*ofproto_
,
1967 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
1968 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
1969 size_t n_flow_exporters_options
)
1971 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1972 struct dpif_ipfix
*di
= ofproto
->ipfix
;
1973 bool has_options
= bridge_exporter_options
|| flow_exporters_options
;
1974 bool new_di
= false;
1976 if (has_options
&& !di
) {
1977 di
= ofproto
->ipfix
= dpif_ipfix_create();
1982 /* Call set_options in any case to cleanly flush the flow
1983 * caches in the last exporters that are to be destroyed. */
1984 dpif_ipfix_set_options(
1985 di
, bridge_exporter_options
, flow_exporters_options
,
1986 n_flow_exporters_options
);
1988 /* Add tunnel ports only when a new ipfix created */
1989 if (new_di
== true) {
1990 struct ofport_dpif
*ofport
;
1991 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1992 if (ofport
->is_tunnel
== true) {
1993 dpif_ipfix_add_tunnel_port(di
, &ofport
->up
, ofport
->odp_port
);
1999 dpif_ipfix_unref(di
);
2000 ofproto
->ipfix
= NULL
;
2008 get_ipfix_stats(const struct ofproto
*ofproto_
,
2010 struct ovs_list
*replies
)
2012 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2013 struct dpif_ipfix
*di
= ofproto
->ipfix
;
2016 return OFPERR_NXST_NOT_CONFIGURED
;
2019 return dpif_ipfix_get_stats(di
, bridge_ipfix
, replies
);
2023 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
2025 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2026 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2027 struct cfm
*old
= ofport
->cfm
;
2032 ofport
->cfm
= cfm_create(ofport
->up
.netdev
);
2035 if (cfm_configure(ofport
->cfm
, s
)) {
2042 cfm_unref(ofport
->cfm
);
2045 if (ofport
->cfm
!= old
) {
2046 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2048 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2049 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2054 cfm_status_changed(struct ofport
*ofport_
)
2056 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2058 return ofport
->cfm
? cfm_check_status_change(ofport
->cfm
) : true;
2062 get_cfm_status(const struct ofport
*ofport_
,
2063 struct cfm_status
*status
)
2065 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2069 cfm_get_status(ofport
->cfm
, status
);
2078 set_bfd(struct ofport
*ofport_
, const struct smap
*cfg
)
2080 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
2081 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2085 ofport
->bfd
= bfd_configure(old
, netdev_get_name(ofport
->up
.netdev
),
2086 cfg
, ofport
->up
.netdev
);
2087 if (ofport
->bfd
!= old
) {
2088 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2090 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2091 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2096 bfd_status_changed(struct ofport
*ofport_
)
2098 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2100 return ofport
->bfd
? bfd_check_status_change(ofport
->bfd
) : true;
2104 get_bfd_status(struct ofport
*ofport_
, struct smap
*smap
)
2106 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2110 bfd_get_status(ofport
->bfd
, smap
);
2119 set_lldp(struct ofport
*ofport_
,
2120 const struct smap
*cfg
)
2122 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2126 if (!ofport
->lldp
) {
2127 struct ofproto_dpif
*ofproto
;
2129 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2130 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2131 ofport
->lldp
= lldp_create(ofport
->up
.netdev
, ofport_
->mtu
, cfg
);
2134 if (!lldp_configure(ofport
->lldp
, cfg
)) {
2139 lldp_unref(ofport
->lldp
);
2140 ofport
->lldp
= NULL
;
2143 ofproto_dpif_monitor_port_update(ofport
,
2147 &ofport
->up
.pp
.hw_addr
);
2152 get_lldp_status(const struct ofport
*ofport_
,
2153 struct lldp_status
*status OVS_UNUSED
)
2155 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2157 return ofport
->lldp
? true : false;
2161 set_aa(struct ofproto
*ofproto OVS_UNUSED
,
2162 const struct aa_settings
*s
)
2164 return aa_configure(s
);
2168 aa_mapping_set(struct ofproto
*ofproto_ OVS_UNUSED
, void *aux
,
2169 const struct aa_mapping_settings
*s
)
2171 return aa_mapping_register(aux
, s
);
2175 aa_mapping_unset(struct ofproto
*ofproto OVS_UNUSED
, void *aux
)
2177 return aa_mapping_unregister(aux
);
2181 aa_vlan_get_queued(struct ofproto
*ofproto OVS_UNUSED
, struct ovs_list
*list
)
2183 return aa_get_vlan_queued(list
);
2187 aa_vlan_get_queue_size(struct ofproto
*ofproto OVS_UNUSED
)
2189 return aa_get_vlan_queue_size();
2193 /* Spanning Tree. */
2195 /* Called while rstp_mutex is held. */
2197 rstp_send_bpdu_cb(struct dp_packet
*pkt
, void *ofport_
, void *ofproto_
)
2199 struct ofproto_dpif
*ofproto
= ofproto_
;
2200 struct ofport_dpif
*ofport
= ofport_
;
2201 struct eth_header
*eth
= dp_packet_l2(pkt
);
2203 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2204 if (eth_addr_is_zero(eth
->eth_src
)) {
2205 VLOG_WARN_RL(&rl
, "%s port %d: cannot send RSTP BPDU on a port which "
2206 "does not have a configured source MAC address.",
2207 ofproto
->up
.name
, ofp_to_u16(ofport
->up
.ofp_port
));
2209 ofproto_dpif_send_packet(ofport
, false, pkt
);
2211 dp_packet_delete(pkt
);
2215 send_bpdu_cb(struct dp_packet
*pkt
, int port_num
, void *ofproto_
)
2217 struct ofproto_dpif
*ofproto
= ofproto_
;
2218 struct stp_port
*sp
= stp_get_port(ofproto
->stp
, port_num
);
2219 struct ofport_dpif
*ofport
;
2221 ofport
= stp_port_get_aux(sp
);
2223 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
2224 ofproto
->up
.name
, port_num
);
2226 struct eth_header
*eth
= dp_packet_l2(pkt
);
2228 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2229 if (eth_addr_is_zero(eth
->eth_src
)) {
2230 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d "
2231 "with unknown MAC", ofproto
->up
.name
, port_num
);
2233 ofproto_dpif_send_packet(ofport
, false, pkt
);
2236 dp_packet_delete(pkt
);
2239 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2241 set_rstp(struct ofproto
*ofproto_
, const struct ofproto_rstp_settings
*s
)
2243 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2245 /* Only revalidate flows if the configuration changed. */
2246 if (!s
!= !ofproto
->rstp
) {
2247 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2251 if (!ofproto
->rstp
) {
2252 ofproto
->rstp
= rstp_create(ofproto_
->name
, s
->address
,
2253 rstp_send_bpdu_cb
, ofproto
);
2254 ofproto
->rstp_last_tick
= time_msec();
2256 rstp_set_bridge_address(ofproto
->rstp
, s
->address
);
2257 rstp_set_bridge_priority(ofproto
->rstp
, s
->priority
);
2258 rstp_set_bridge_ageing_time(ofproto
->rstp
, s
->ageing_time
);
2259 rstp_set_bridge_force_protocol_version(ofproto
->rstp
,
2260 s
->force_protocol_version
);
2261 rstp_set_bridge_max_age(ofproto
->rstp
, s
->bridge_max_age
);
2262 rstp_set_bridge_forward_delay(ofproto
->rstp
, s
->bridge_forward_delay
);
2263 rstp_set_bridge_transmit_hold_count(ofproto
->rstp
,
2264 s
->transmit_hold_count
);
2266 struct ofport
*ofport
;
2267 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2268 set_rstp_port(ofport
, NULL
);
2270 rstp_unref(ofproto
->rstp
);
2271 ofproto
->rstp
= NULL
;
2276 get_rstp_status(struct ofproto
*ofproto_
, struct ofproto_rstp_status
*s
)
2278 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2280 if (ofproto
->rstp
) {
2282 s
->root_id
= rstp_get_root_id(ofproto
->rstp
);
2283 s
->bridge_id
= rstp_get_bridge_id(ofproto
->rstp
);
2284 s
->designated_id
= rstp_get_designated_id(ofproto
->rstp
);
2285 s
->root_path_cost
= rstp_get_root_path_cost(ofproto
->rstp
);
2286 s
->designated_port_id
= rstp_get_designated_port_id(ofproto
->rstp
);
2287 s
->bridge_port_id
= rstp_get_bridge_port_id(ofproto
->rstp
);
2294 update_rstp_port_state(struct ofport_dpif
*ofport
)
2296 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2297 enum rstp_state state
;
2299 /* Figure out new state. */
2300 state
= ofport
->rstp_port
? rstp_port_get_state(ofport
->rstp_port
)
2304 if (ofport
->rstp_state
!= state
) {
2305 enum ofputil_port_state of_state
;
2308 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2309 netdev_get_name(ofport
->up
.netdev
),
2310 rstp_state_name(ofport
->rstp_state
),
2311 rstp_state_name(state
));
2313 if (rstp_learn_in_state(ofport
->rstp_state
)
2314 != rstp_learn_in_state(state
)) {
2315 /* XXX: Learning action flows should also be flushed. */
2316 if (ofport
->bundle
) {
2317 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2318 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2319 bundle_flush_macs(ofport
->bundle
, false);
2323 fwd_change
= rstp_forward_in_state(ofport
->rstp_state
)
2324 != rstp_forward_in_state(state
);
2326 ofproto
->backer
->need_revalidate
= REV_RSTP
;
2327 ofport
->rstp_state
= state
;
2329 if (fwd_change
&& ofport
->bundle
) {
2330 bundle_update(ofport
->bundle
);
2333 /* Update the RSTP state bits in the OpenFlow port description. */
2334 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2335 of_state
|= (state
== RSTP_LEARNING
? OFPUTIL_PS_STP_LEARN
2336 : state
== RSTP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2337 : state
== RSTP_DISCARDING
? OFPUTIL_PS_STP_LISTEN
2339 ofproto_port_set_state(&ofport
->up
, of_state
);
2344 rstp_run(struct ofproto_dpif
*ofproto
)
2346 if (ofproto
->rstp
) {
2347 long long int now
= time_msec();
2348 long long int elapsed
= now
- ofproto
->rstp_last_tick
;
2349 struct rstp_port
*rp
;
2350 struct ofport_dpif
*ofport
;
2352 /* Every second, decrease the values of the timers. */
2353 if (elapsed
>= 1000) {
2354 rstp_tick_timers(ofproto
->rstp
);
2355 ofproto
->rstp_last_tick
= now
;
2358 while ((ofport
= rstp_get_next_changed_port_aux(ofproto
->rstp
, &rp
))) {
2359 update_rstp_port_state(ofport
);
2363 /* FIXME: This check should be done on-event (i.e., when setting
2364 * p->fdb_flush) and not periodically.
2366 while ((ofport
= rstp_check_and_reset_fdb_flush(ofproto
->rstp
, &rp
))) {
2367 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2368 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2369 bundle_flush_macs(ofport
->bundle
, false);
2373 if (rstp_shift_root_learned_address(ofproto
->rstp
)) {
2374 struct ofport_dpif
*old_root_aux
=
2375 (struct ofport_dpif
*)rstp_get_old_root_aux(ofproto
->rstp
);
2376 struct ofport_dpif
*new_root_aux
=
2377 (struct ofport_dpif
*)rstp_get_new_root_aux(ofproto
->rstp
);
2378 if (old_root_aux
!= NULL
&& new_root_aux
!= NULL
) {
2379 bundle_move(old_root_aux
->bundle
, new_root_aux
->bundle
);
2380 rstp_reset_root_changed(ofproto
->rstp
);
2386 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2388 set_stp(struct ofproto
*ofproto_
, const struct ofproto_stp_settings
*s
)
2390 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2392 /* Only revalidate flows if the configuration changed. */
2393 if (!s
!= !ofproto
->stp
) {
2394 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2398 if (!ofproto
->stp
) {
2399 ofproto
->stp
= stp_create(ofproto_
->name
, s
->system_id
,
2400 send_bpdu_cb
, ofproto
);
2401 ofproto
->stp_last_tick
= time_msec();
2404 stp_set_bridge_id(ofproto
->stp
, s
->system_id
);
2405 stp_set_bridge_priority(ofproto
->stp
, s
->priority
);
2406 stp_set_hello_time(ofproto
->stp
, s
->hello_time
);
2407 stp_set_max_age(ofproto
->stp
, s
->max_age
);
2408 stp_set_forward_delay(ofproto
->stp
, s
->fwd_delay
);
2410 struct ofport
*ofport
;
2412 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2413 set_stp_port(ofport
, NULL
);
2416 stp_unref(ofproto
->stp
);
2417 ofproto
->stp
= NULL
;
2424 get_stp_status(struct ofproto
*ofproto_
, struct ofproto_stp_status
*s
)
2426 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2430 s
->bridge_id
= stp_get_bridge_id(ofproto
->stp
);
2431 s
->designated_root
= stp_get_designated_root(ofproto
->stp
);
2432 s
->root_path_cost
= stp_get_root_path_cost(ofproto
->stp
);
2441 update_stp_port_state(struct ofport_dpif
*ofport
)
2443 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2444 enum stp_state state
;
2446 /* Figure out new state. */
2447 state
= ofport
->stp_port
? stp_port_get_state(ofport
->stp_port
)
2451 if (ofport
->stp_state
!= state
) {
2452 enum ofputil_port_state of_state
;
2455 VLOG_DBG("port %s: STP state changed from %s to %s",
2456 netdev_get_name(ofport
->up
.netdev
),
2457 stp_state_name(ofport
->stp_state
),
2458 stp_state_name(state
));
2459 if (stp_learn_in_state(ofport
->stp_state
)
2460 != stp_learn_in_state(state
)) {
2461 /* xxx Learning action flows should also be flushed. */
2462 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2463 mac_learning_flush(ofproto
->ml
);
2464 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2465 mcast_snooping_mdb_flush(ofproto
->ms
);
2467 fwd_change
= stp_forward_in_state(ofport
->stp_state
)
2468 != stp_forward_in_state(state
);
2470 ofproto
->backer
->need_revalidate
= REV_STP
;
2471 ofport
->stp_state
= state
;
2472 ofport
->stp_state_entered
= time_msec();
2474 if (fwd_change
&& ofport
->bundle
) {
2475 bundle_update(ofport
->bundle
);
2478 /* Update the STP state bits in the OpenFlow port description. */
2479 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2480 of_state
|= (state
== STP_LISTENING
? OFPUTIL_PS_STP_LISTEN
2481 : state
== STP_LEARNING
? OFPUTIL_PS_STP_LEARN
2482 : state
== STP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2483 : state
== STP_BLOCKING
? OFPUTIL_PS_STP_BLOCK
2485 ofproto_port_set_state(&ofport
->up
, of_state
);
2489 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2490 * caller is responsible for assigning STP port numbers and ensuring
2491 * there are no duplicates. */
2493 set_stp_port(struct ofport
*ofport_
,
2494 const struct ofproto_port_stp_settings
*s
)
2496 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2497 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2498 struct stp_port
*sp
= ofport
->stp_port
;
2500 if (!s
|| !s
->enable
) {
2502 ofport
->stp_port
= NULL
;
2503 stp_port_disable(sp
);
2504 update_stp_port_state(ofport
);
2507 } else if (sp
&& stp_port_no(sp
) != s
->port_num
2508 && ofport
== stp_port_get_aux(sp
)) {
2509 /* The port-id changed, so disable the old one if it's not
2510 * already in use by another port. */
2511 stp_port_disable(sp
);
2514 sp
= ofport
->stp_port
= stp_get_port(ofproto
->stp
, s
->port_num
);
2516 /* Set name before enabling the port so that debugging messages can print
2518 stp_port_set_name(sp
, netdev_get_name(ofport
->up
.netdev
));
2519 stp_port_enable(sp
);
2521 stp_port_set_aux(sp
, ofport
);
2522 stp_port_set_priority(sp
, s
->priority
);
2523 stp_port_set_path_cost(sp
, s
->path_cost
);
2525 update_stp_port_state(ofport
);
2531 get_stp_port_status(struct ofport
*ofport_
,
2532 struct ofproto_port_stp_status
*s
)
2534 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2535 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2536 struct stp_port
*sp
= ofport
->stp_port
;
2538 if (!ofproto
->stp
|| !sp
) {
2544 stp_port_get_status(sp
, &s
->port_id
, &s
->state
, &s
->role
);
2545 s
->sec_in_state
= (time_msec() - ofport
->stp_state_entered
) / 1000;
2551 get_stp_port_stats(struct ofport
*ofport_
,
2552 struct ofproto_port_stp_stats
*s
)
2554 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2555 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2556 struct stp_port
*sp
= ofport
->stp_port
;
2558 if (!ofproto
->stp
|| !sp
) {
2564 stp_port_get_counts(sp
, &s
->tx_count
, &s
->rx_count
, &s
->error_count
);
2570 stp_run(struct ofproto_dpif
*ofproto
)
2573 long long int now
= time_msec();
2574 long long int elapsed
= now
- ofproto
->stp_last_tick
;
2575 struct stp_port
*sp
;
2578 stp_tick(ofproto
->stp
, MIN(INT_MAX
, elapsed
));
2579 ofproto
->stp_last_tick
= now
;
2581 while (stp_get_changed_port(ofproto
->stp
, &sp
)) {
2582 struct ofport_dpif
*ofport
= stp_port_get_aux(sp
);
2585 update_stp_port_state(ofport
);
2589 if (stp_check_and_reset_fdb_flush(ofproto
->stp
)) {
2590 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2591 mac_learning_flush(ofproto
->ml
);
2592 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2593 mcast_snooping_mdb_flush(ofproto
->ms
);
2599 stp_wait(struct ofproto_dpif
*ofproto
)
2602 poll_timer_wait(1000);
2606 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2607 * caller is responsible for assigning RSTP port numbers and ensuring
2608 * there are no duplicates. */
2610 set_rstp_port(struct ofport
*ofport_
,
2611 const struct ofproto_port_rstp_settings
*s
)
2613 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2614 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2615 struct rstp_port
*rp
= ofport
->rstp_port
;
2617 if (!s
|| !s
->enable
) {
2619 rstp_port_set_aux(rp
, NULL
);
2620 rstp_port_set_state(rp
, RSTP_DISABLED
);
2621 rstp_port_set_mac_operational(rp
, false);
2622 ofport
->rstp_port
= NULL
;
2623 rstp_port_unref(rp
);
2624 update_rstp_port_state(ofport
);
2629 /* Check if need to add a new port. */
2631 rp
= ofport
->rstp_port
= rstp_add_port(ofproto
->rstp
);
2634 rstp_port_set(rp
, s
->port_num
, s
->priority
, s
->path_cost
,
2635 s
->admin_edge_port
, s
->auto_edge
,
2636 s
->admin_p2p_mac_state
, s
->admin_port_state
, s
->mcheck
,
2638 update_rstp_port_state(ofport
);
2639 /* Synchronize operational status. */
2640 rstp_port_set_mac_operational(rp
, ofport
->may_enable
);
2644 get_rstp_port_status(struct ofport
*ofport_
,
2645 struct ofproto_port_rstp_status
*s
)
2647 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2648 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2649 struct rstp_port
*rp
= ofport
->rstp_port
;
2651 if (!ofproto
->rstp
|| !rp
) {
2657 rstp_port_get_status(rp
, &s
->port_id
, &s
->state
, &s
->role
,
2658 &s
->designated_bridge_id
, &s
->designated_port_id
,
2659 &s
->designated_path_cost
, &s
->tx_count
,
2660 &s
->rx_count
, &s
->error_count
, &s
->uptime
);
2665 set_queues(struct ofport
*ofport_
, const struct ofproto_port_queue
*qdscp
,
2668 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2669 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2671 if (ofport
->n_qdscp
!= n_qdscp
2672 || (n_qdscp
&& memcmp(ofport
->qdscp
, qdscp
,
2673 n_qdscp
* sizeof *qdscp
))) {
2674 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2675 free(ofport
->qdscp
);
2676 ofport
->qdscp
= n_qdscp
2677 ? xmemdup(qdscp
, n_qdscp
* sizeof *qdscp
)
2679 ofport
->n_qdscp
= n_qdscp
;
2687 /* Expires all MAC learning entries associated with 'bundle' and forces its
2688 * ofproto to revalidate every flow.
2690 * Normally MAC learning entries are removed only from the ofproto associated
2691 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2692 * are removed from every ofproto. When patch ports and SLB bonds are in use
2693 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2694 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2695 * with the host from which it migrated. */
2697 bundle_flush_macs(struct ofbundle
*bundle
, bool all_ofprotos
)
2699 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2700 struct mac_learning
*ml
= ofproto
->ml
;
2701 struct mac_entry
*mac
, *next_mac
;
2703 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2704 ovs_rwlock_wrlock(&ml
->rwlock
);
2705 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2706 if (mac_entry_get_port(ml
, mac
) == bundle
) {
2708 struct ofproto_dpif
*o
;
2710 HMAP_FOR_EACH (o
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
2712 struct mac_entry
*e
;
2714 ovs_rwlock_wrlock(&o
->ml
->rwlock
);
2715 e
= mac_learning_lookup(o
->ml
, mac
->mac
, mac
->vlan
);
2717 mac_learning_expire(o
->ml
, e
);
2719 ovs_rwlock_unlock(&o
->ml
->rwlock
);
2724 mac_learning_expire(ml
, mac
);
2727 ovs_rwlock_unlock(&ml
->rwlock
);
2731 bundle_move(struct ofbundle
*old
, struct ofbundle
*new)
2733 struct ofproto_dpif
*ofproto
= old
->ofproto
;
2734 struct mac_learning
*ml
= ofproto
->ml
;
2735 struct mac_entry
*mac
, *next_mac
;
2737 ovs_assert(new->ofproto
== old
->ofproto
);
2739 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2740 ovs_rwlock_wrlock(&ml
->rwlock
);
2741 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2742 if (mac_entry_get_port(ml
, mac
) == old
) {
2743 mac_entry_set_port(ml
, mac
, new);
2746 ovs_rwlock_unlock(&ml
->rwlock
);
2749 static struct ofbundle
*
2750 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
2752 struct ofbundle
*bundle
;
2754 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
2755 &ofproto
->bundles
) {
2756 if (bundle
->aux
== aux
) {
2764 bundle_update(struct ofbundle
*bundle
)
2766 struct ofport_dpif
*port
;
2768 bundle
->floodable
= true;
2769 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2770 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2772 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2773 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2774 bundle
->floodable
= false;
2781 bundle_del_port(struct ofport_dpif
*port
)
2783 struct ofbundle
*bundle
= port
->bundle
;
2785 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2787 ovs_list_remove(&port
->bundle_node
);
2788 port
->bundle
= NULL
;
2791 lacp_slave_unregister(bundle
->lacp
, port
);
2794 bond_slave_unregister(bundle
->bond
, port
);
2797 bundle_update(bundle
);
2801 bundle_add_port(struct ofbundle
*bundle
, ofp_port_t ofp_port
,
2802 struct lacp_slave_settings
*lacp
)
2804 struct ofport_dpif
*port
;
2806 port
= ofp_port_to_ofport(bundle
->ofproto
, ofp_port
);
2811 if (port
->bundle
!= bundle
) {
2812 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2814 bundle_remove(&port
->up
);
2817 port
->bundle
= bundle
;
2818 ovs_list_push_back(&bundle
->ports
, &port
->bundle_node
);
2819 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2821 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2822 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2823 bundle
->floodable
= false;
2827 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2828 lacp_slave_register(bundle
->lacp
, port
, lacp
);
2835 bundle_destroy(struct ofbundle
*bundle
)
2837 struct ofproto_dpif
*ofproto
;
2838 struct ofport_dpif
*port
, *next_port
;
2844 ofproto
= bundle
->ofproto
;
2845 mbridge_unregister_bundle(ofproto
->mbridge
, bundle
);
2848 xlate_bundle_remove(bundle
);
2851 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2852 bundle_del_port(port
);
2855 bundle_flush_macs(bundle
, true);
2856 mcast_snooping_flush_bundle(ofproto
->ms
, bundle
);
2857 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
2859 free(bundle
->trunks
);
2860 free(bundle
->cvlans
);
2861 lacp_unref(bundle
->lacp
);
2862 bond_unref(bundle
->bond
);
2867 bundle_set(struct ofproto
*ofproto_
, void *aux
,
2868 const struct ofproto_bundle_settings
*s
)
2870 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2871 bool need_flush
= false;
2872 struct ofport_dpif
*port
;
2873 struct ofbundle
*bundle
;
2874 unsigned long *trunks
= NULL
;
2875 unsigned long *cvlans
= NULL
;
2881 bundle_destroy(bundle_lookup(ofproto
, aux
));
2885 ovs_assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
2886 ovs_assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
2888 bundle
= bundle_lookup(ofproto
, aux
);
2890 bundle
= xmalloc(sizeof *bundle
);
2892 bundle
->ofproto
= ofproto
;
2893 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
2894 hash_pointer(aux
, 0));
2896 bundle
->name
= NULL
;
2898 ovs_list_init(&bundle
->ports
);
2899 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
2900 bundle
->qinq_ethtype
= ETH_TYPE_VLAN_8021AD
;
2902 bundle
->trunks
= NULL
;
2903 bundle
->cvlans
= NULL
;
2904 bundle
->use_priority_tags
= s
->use_priority_tags
;
2905 bundle
->lacp
= NULL
;
2906 bundle
->bond
= NULL
;
2908 bundle
->floodable
= true;
2909 bundle
->protected = false;
2910 mbridge_register_bundle(ofproto
->mbridge
, bundle
);
2913 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
2915 bundle
->name
= xstrdup(s
->name
);
2920 ofproto
->lacp_enabled
= true;
2921 if (!bundle
->lacp
) {
2922 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2923 bundle
->lacp
= lacp_create();
2925 lacp_configure(bundle
->lacp
, s
->lacp
);
2927 lacp_unref(bundle
->lacp
);
2928 bundle
->lacp
= NULL
;
2931 /* Update set of ports. */
2933 for (i
= 0; i
< s
->n_slaves
; i
++) {
2934 if (!bundle_add_port(bundle
, s
->slaves
[i
],
2935 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
)) {
2939 if (!ok
|| ovs_list_size(&bundle
->ports
) != s
->n_slaves
) {
2940 struct ofport_dpif
*next_port
;
2942 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2943 for (i
= 0; i
< s
->n_slaves
; i
++) {
2944 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
2949 bundle_del_port(port
);
2953 ovs_assert(ovs_list_size(&bundle
->ports
) <= s
->n_slaves
);
2955 if (ovs_list_is_empty(&bundle
->ports
)) {
2956 bundle_destroy(bundle
);
2960 /* Set VLAN tagging mode */
2961 if (s
->vlan_mode
!= bundle
->vlan_mode
2962 || s
->use_priority_tags
!= bundle
->use_priority_tags
) {
2963 bundle
->vlan_mode
= s
->vlan_mode
;
2964 bundle
->use_priority_tags
= s
->use_priority_tags
;
2968 if (s
->qinq_ethtype
!= bundle
->qinq_ethtype
) {
2969 bundle
->qinq_ethtype
= s
->qinq_ethtype
;
2974 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
2975 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
2977 if (vlan
!= bundle
->vlan
) {
2978 bundle
->vlan
= vlan
;
2982 /* Get trunked VLANs. */
2983 switch (s
->vlan_mode
) {
2984 case PORT_VLAN_ACCESS
:
2988 case PORT_VLAN_TRUNK
:
2989 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
2992 case PORT_VLAN_NATIVE_UNTAGGED
:
2993 case PORT_VLAN_NATIVE_TAGGED
:
2994 if (vlan
!= 0 && (!s
->trunks
2995 || !bitmap_is_set(s
->trunks
, vlan
)
2996 || bitmap_is_set(s
->trunks
, 0))) {
2997 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2999 trunks
= bitmap_clone(s
->trunks
, 4096);
3001 trunks
= bitmap_allocate1(4096);
3003 bitmap_set1(trunks
, vlan
);
3004 bitmap_set0(trunks
, 0);
3006 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
3010 case PORT_VLAN_DOT1Q_TUNNEL
:
3011 cvlans
= CONST_CAST(unsigned long *, s
->cvlans
);
3017 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
3018 free(bundle
->trunks
);
3019 if (trunks
== s
->trunks
) {
3020 bundle
->trunks
= vlan_bitmap_clone(trunks
);
3022 bundle
->trunks
= trunks
;
3027 if (trunks
!= s
->trunks
) {
3031 if (!vlan_bitmap_equal(cvlans
, bundle
->cvlans
)) {
3032 free(bundle
->cvlans
);
3033 if (cvlans
== s
->cvlans
) {
3034 bundle
->cvlans
= vlan_bitmap_clone(cvlans
);
3036 bundle
->cvlans
= cvlans
;
3041 if (cvlans
!= s
->cvlans
) {
3046 if (!ovs_list_is_short(&bundle
->ports
)) {
3047 bundle
->ofproto
->has_bonded_bundles
= true;
3049 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
3050 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3053 bundle
->bond
= bond_create(s
->bond
, ofproto
);
3054 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3057 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3058 bond_slave_register(bundle
->bond
, port
,
3059 port
->up
.ofp_port
, port
->up
.netdev
);
3062 bond_unref(bundle
->bond
);
3063 bundle
->bond
= NULL
;
3066 /* Set proteced port mode */
3067 if (s
->protected != bundle
->protected) {
3068 bundle
->protected = s
->protected;
3072 /* If we changed something that would affect MAC learning, un-learn
3073 * everything on this port and force flow revalidation. */
3075 bundle_flush_macs(bundle
, false);
3076 mcast_snooping_flush_bundle(ofproto
->ms
, bundle
);
3083 bundle_remove(struct ofport
*port_
)
3085 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
3086 struct ofbundle
*bundle
= port
->bundle
;
3089 bundle_del_port(port
);
3090 if (ovs_list_is_empty(&bundle
->ports
)) {
3091 bundle_destroy(bundle
);
3092 } else if (ovs_list_is_short(&bundle
->ports
)) {
3093 bond_unref(bundle
->bond
);
3094 bundle
->bond
= NULL
;
3100 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
3102 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 10);
3103 struct ofport_dpif
*port
= port_
;
3107 error
= netdev_get_etheraddr(port
->up
.netdev
, &ea
);
3109 struct dp_packet packet
;
3112 dp_packet_init(&packet
, 0);
3113 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
3115 memcpy(packet_pdu
, pdu
, pdu_size
);
3117 ofproto_dpif_send_packet(port
, false, &packet
);
3118 dp_packet_uninit(&packet
);
3120 VLOG_ERR_RL(&rl
, "port %s: cannot obtain Ethernet address of iface "
3121 "%s (%s)", port
->bundle
->name
,
3122 netdev_get_name(port
->up
.netdev
), ovs_strerror(error
));
3127 bundle_send_learning_packets(struct ofbundle
*bundle
)
3129 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
3130 int error
, n_packets
, n_errors
;
3131 struct mac_entry
*e
;
3133 struct ovs_list list_node
;
3134 struct ofport_dpif
*port
;
3135 struct dp_packet
*pkt
;
3137 struct ovs_list packets
;
3139 ovs_list_init(&packets
);
3140 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
3141 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
3142 if (mac_entry_get_port(ofproto
->ml
, e
) != bundle
) {
3143 pkt_node
= xmalloc(sizeof *pkt_node
);
3144 pkt_node
->pkt
= bond_compose_learning_packet(bundle
->bond
,
3146 (void **)&pkt_node
->port
);
3147 ovs_list_push_back(&packets
, &pkt_node
->list_node
);
3150 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3152 error
= n_packets
= n_errors
= 0;
3153 LIST_FOR_EACH_POP (pkt_node
, list_node
, &packets
) {
3156 ret
= ofproto_dpif_send_packet(pkt_node
->port
, false, pkt_node
->pkt
);
3157 dp_packet_delete(pkt_node
->pkt
);
3167 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3168 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
3169 "packets, last error was: %s",
3170 bundle
->name
, n_errors
, n_packets
, ovs_strerror(error
));
3172 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3173 bundle
->name
, n_packets
);
3178 bundle_run(struct ofbundle
*bundle
)
3181 lacp_run(bundle
->lacp
, send_pdu_cb
);
3184 struct ofport_dpif
*port
;
3186 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3187 bond_slave_set_may_enable(bundle
->bond
, port
, port
->may_enable
);
3190 if (bond_run(bundle
->bond
, lacp_status(bundle
->lacp
))) {
3191 bundle
->ofproto
->backer
->need_revalidate
= REV_BOND
;
3194 if (bond_should_send_learning_packets(bundle
->bond
)) {
3195 bundle_send_learning_packets(bundle
);
3201 bundle_wait(struct ofbundle
*bundle
)
3204 lacp_wait(bundle
->lacp
);
3207 bond_wait(bundle
->bond
);
3214 mirror_set__(struct ofproto
*ofproto_
, void *aux
,
3215 const struct ofproto_mirror_settings
*s
)
3217 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3218 struct ofbundle
**srcs
, **dsts
;
3223 mirror_destroy(ofproto
->mbridge
, aux
);
3227 srcs
= xmalloc(s
->n_srcs
* sizeof *srcs
);
3228 dsts
= xmalloc(s
->n_dsts
* sizeof *dsts
);
3230 for (i
= 0; i
< s
->n_srcs
; i
++) {
3231 srcs
[i
] = bundle_lookup(ofproto
, s
->srcs
[i
]);
3234 for (i
= 0; i
< s
->n_dsts
; i
++) {
3235 dsts
[i
] = bundle_lookup(ofproto
, s
->dsts
[i
]);
3238 error
= mirror_set(ofproto
->mbridge
, aux
, s
->name
, srcs
, s
->n_srcs
, dsts
,
3239 s
->n_dsts
, s
->src_vlans
,
3240 bundle_lookup(ofproto
, s
->out_bundle
),
3241 s
->snaplen
, s
->out_vlan
);
3248 mirror_get_stats__(struct ofproto
*ofproto
, void *aux
,
3249 uint64_t *packets
, uint64_t *bytes
)
3251 return mirror_get_stats(ofproto_dpif_cast(ofproto
)->mbridge
, aux
, packets
,
3256 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
3258 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3259 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3260 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
3261 mac_learning_flush(ofproto
->ml
);
3263 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3268 is_mirror_output_bundle(const struct ofproto
*ofproto_
, void *aux
)
3270 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3271 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3272 return bundle
&& mirror_bundle_out(ofproto
->mbridge
, bundle
) != 0;
3276 forward_bpdu_changed(struct ofproto
*ofproto_
)
3278 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3279 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3283 set_mac_table_config(struct ofproto
*ofproto_
, unsigned int idle_time
,
3286 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3287 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3288 mac_learning_set_idle_time(ofproto
->ml
, idle_time
);
3289 mac_learning_set_max_entries(ofproto
->ml
, max_entries
);
3290 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3293 /* Configures multicast snooping on 'ofport' using the settings
3294 * defined in 's'. */
3296 set_mcast_snooping(struct ofproto
*ofproto_
,
3297 const struct ofproto_mcast_snooping_settings
*s
)
3299 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3301 /* Only revalidate flows if the configuration changed. */
3302 if (!s
!= !ofproto
->ms
) {
3303 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3308 ofproto
->ms
= mcast_snooping_create();
3311 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3312 mcast_snooping_set_idle_time(ofproto
->ms
, s
->idle_time
);
3313 mcast_snooping_set_max_entries(ofproto
->ms
, s
->max_entries
);
3314 if (mcast_snooping_set_flood_unreg(ofproto
->ms
, s
->flood_unreg
)) {
3315 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3317 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3319 mcast_snooping_unref(ofproto
->ms
);
3326 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3328 set_mcast_snooping_port(struct ofproto
*ofproto_
, void *aux
,
3329 const struct ofproto_mcast_snooping_port_settings
*s
)
3331 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3332 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3334 if (ofproto
->ms
&& s
) {
3335 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3336 mcast_snooping_set_port_flood(ofproto
->ms
, bundle
, s
->flood
);
3337 mcast_snooping_set_port_flood_reports(ofproto
->ms
, bundle
,
3339 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3347 struct ofport_dpif
*
3348 ofp_port_to_ofport(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
3350 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
3351 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
3355 ofproto_port_from_dpif_port(struct ofproto_dpif
*ofproto
,
3356 struct ofproto_port
*ofproto_port
,
3357 struct dpif_port
*dpif_port
)
3359 ofproto_port
->name
= dpif_port
->name
;
3360 ofproto_port
->type
= dpif_port
->type
;
3361 ofproto_port
->ofp_port
= odp_port_to_ofp_port(ofproto
, dpif_port
->port_no
);
3365 ofport_update_peer(struct ofport_dpif
*ofport
)
3367 const struct ofproto_dpif
*ofproto
;
3368 struct dpif_backer
*backer
;
3371 if (!netdev_vport_is_patch(ofport
->up
.netdev
)) {
3375 backer
= ofproto_dpif_cast(ofport
->up
.ofproto
)->backer
;
3376 backer
->need_revalidate
= REV_RECONFIGURE
;
3379 ofport
->peer
->peer
= NULL
;
3380 ofport
->peer
= NULL
;
3383 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3388 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
3389 struct ofport
*peer_ofport
;
3390 struct ofport_dpif
*peer
;
3393 if (ofproto
->backer
!= backer
) {
3397 peer_ofport
= shash_find_data(&ofproto
->up
.port_by_name
, peer_name
);
3402 peer
= ofport_dpif_cast(peer_ofport
);
3403 peer_peer
= netdev_vport_patch_peer(peer
->up
.netdev
);
3404 if (peer_peer
&& !strcmp(netdev_get_name(ofport
->up
.netdev
),
3406 ofport
->peer
= peer
;
3407 ofport
->peer
->peer
= ofport
;
3417 port_run(struct ofport_dpif
*ofport
)
3419 long long int carrier_seq
= netdev_get_carrier_resets(ofport
->up
.netdev
);
3420 bool carrier_changed
= carrier_seq
!= ofport
->carrier_seq
;
3421 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
3422 bool cfm_enable
= false;
3423 bool bfd_enable
= false;
3425 ofport
->carrier_seq
= carrier_seq
;
3428 int cfm_opup
= cfm_get_opup(ofport
->cfm
);
3430 cfm_enable
= !cfm_get_fault(ofport
->cfm
);
3432 if (cfm_opup
>= 0) {
3433 cfm_enable
= cfm_enable
&& cfm_opup
;
3438 bfd_enable
= bfd_forwarding(ofport
->bfd
);
3441 if (ofport
->bfd
|| ofport
->cfm
) {
3442 enable
= enable
&& (cfm_enable
|| bfd_enable
);
3445 if (ofport
->bundle
) {
3446 enable
= enable
&& lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
);
3447 if (carrier_changed
) {
3448 lacp_slave_carrier_changed(ofport
->bundle
->lacp
, ofport
);
3452 if (ofport
->may_enable
!= enable
) {
3453 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3455 ofproto
->backer
->need_revalidate
= REV_PORT_TOGGLED
;
3457 if (ofport
->rstp_port
) {
3458 rstp_port_set_mac_operational(ofport
->rstp_port
, enable
);
3462 ofport
->may_enable
= enable
;
3466 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
3467 struct ofproto_port
*ofproto_port
)
3469 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3470 struct dpif_port dpif_port
;
3473 if (sset_contains(&ofproto
->ghost_ports
, devname
)) {
3474 const char *type
= netdev_get_type_from_name(devname
);
3476 /* We may be called before ofproto->up.port_by_name is populated with
3477 * the appropriate ofport. For this reason, we must get the name and
3478 * type from the netdev layer directly. */
3480 const struct ofport
*ofport
;
3482 ofport
= shash_find_data(&ofproto
->up
.port_by_name
, devname
);
3483 ofproto_port
->ofp_port
= ofport
? ofport
->ofp_port
: OFPP_NONE
;
3484 ofproto_port
->name
= xstrdup(devname
);
3485 ofproto_port
->type
= xstrdup(type
);
3491 if (!sset_contains(&ofproto
->ports
, devname
)) {
3494 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
,
3495 devname
, &dpif_port
);
3497 ofproto_port_from_dpif_port(ofproto
, ofproto_port
, &dpif_port
);
3503 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
)
3505 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3506 const char *devname
= netdev_get_name(netdev
);
3507 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
3508 const char *dp_port_name
;
3510 if (netdev_vport_is_patch(netdev
)) {
3511 sset_add(&ofproto
->ghost_ports
, netdev_get_name(netdev
));
3515 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
3516 if (!dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
3517 odp_port_t port_no
= ODPP_NONE
;
3520 error
= dpif_port_add(ofproto
->backer
->dpif
, netdev
, &port_no
);
3524 if (netdev_get_tunnel_config(netdev
)) {
3525 simap_put(&ofproto
->backer
->tnl_backers
,
3526 dp_port_name
, odp_to_u32(port_no
));
3530 if (netdev_get_tunnel_config(netdev
)) {
3531 sset_add(&ofproto
->ghost_ports
, devname
);
3533 sset_add(&ofproto
->ports
, devname
);
3539 port_del(struct ofproto
*ofproto_
, ofp_port_t ofp_port
)
3541 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3542 struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
3549 sset_find_and_delete(&ofproto
->ghost_ports
,
3550 netdev_get_name(ofport
->up
.netdev
));
3551 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3552 if (!ofport
->is_tunnel
&& !netdev_vport_is_patch(ofport
->up
.netdev
)) {
3553 error
= dpif_port_del(ofproto
->backer
->dpif
, ofport
->odp_port
);
3555 /* The caller is going to close ofport->up.netdev. If this is a
3556 * bonded port, then the bond is using that netdev, so remove it
3557 * from the bond. The client will need to reconfigure everything
3558 * after deleting ports, so then the slave will get re-added. */
3559 bundle_remove(&ofport
->up
);
3566 port_set_config(const struct ofport
*ofport_
, const struct smap
*cfg
)
3568 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3569 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3571 if (sset_contains(&ofproto
->ghost_ports
,
3572 netdev_get_name(ofport
->up
.netdev
))) {
3576 return dpif_port_set_config(ofproto
->backer
->dpif
, ofport
->odp_port
, cfg
);
3580 port_get_stats(const struct ofport
*ofport_
, struct netdev_stats
*stats
)
3582 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3585 error
= netdev_get_stats(ofport
->up
.netdev
, stats
);
3587 if (!error
&& ofport_
->ofp_port
== OFPP_LOCAL
) {
3588 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3590 ovs_mutex_lock(&ofproto
->stats_mutex
);
3591 /* ofproto->stats.tx_packets represents packets that we created
3592 * internally and sent to some port (e.g. packets sent with
3593 * ofproto_dpif_send_packet()). Account for them as if they had
3594 * come from OFPP_LOCAL and got forwarded. */
3596 if (stats
->rx_packets
!= UINT64_MAX
) {
3597 stats
->rx_packets
+= ofproto
->stats
.tx_packets
;
3600 if (stats
->rx_bytes
!= UINT64_MAX
) {
3601 stats
->rx_bytes
+= ofproto
->stats
.tx_bytes
;
3604 /* ofproto->stats.rx_packets represents packets that were received on
3605 * some port and we processed internally and dropped (e.g. STP).
3606 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3608 if (stats
->tx_packets
!= UINT64_MAX
) {
3609 stats
->tx_packets
+= ofproto
->stats
.rx_packets
;
3612 if (stats
->tx_bytes
!= UINT64_MAX
) {
3613 stats
->tx_bytes
+= ofproto
->stats
.rx_bytes
;
3615 ovs_mutex_unlock(&ofproto
->stats_mutex
);
3622 port_get_lacp_stats(const struct ofport
*ofport_
, struct lacp_slave_stats
*stats
)
3624 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3625 if (ofport
->bundle
&& ofport
->bundle
->lacp
) {
3626 if (lacp_get_slave_stats(ofport
->bundle
->lacp
, ofport
, stats
)) {
3633 struct port_dump_state
{
3634 struct sset_position pos
;
3637 struct ofproto_port port
;
3642 port_dump_start(const struct ofproto
*ofproto_ OVS_UNUSED
, void **statep
)
3644 *statep
= xzalloc(sizeof(struct port_dump_state
));
3649 port_dump_next(const struct ofproto
*ofproto_
, void *state_
,
3650 struct ofproto_port
*port
)
3652 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3653 struct port_dump_state
*state
= state_
;
3654 const struct sset
*sset
;
3655 struct sset_node
*node
;
3657 if (state
->has_port
) {
3658 ofproto_port_destroy(&state
->port
);
3659 state
->has_port
= false;
3661 sset
= state
->ghost
? &ofproto
->ghost_ports
: &ofproto
->ports
;
3662 while ((node
= sset_at_position(sset
, &state
->pos
))) {
3665 error
= port_query_by_name(ofproto_
, node
->name
, &state
->port
);
3667 *port
= state
->port
;
3668 state
->has_port
= true;
3670 } else if (error
!= ENODEV
) {
3675 if (!state
->ghost
) {
3676 state
->ghost
= true;
3677 memset(&state
->pos
, 0, sizeof state
->pos
);
3678 return port_dump_next(ofproto_
, state_
, port
);
3685 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
3687 struct port_dump_state
*state
= state_
;
3689 if (state
->has_port
) {
3690 ofproto_port_destroy(&state
->port
);
3697 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
3699 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3701 if (ofproto
->port_poll_errno
) {
3702 int error
= ofproto
->port_poll_errno
;
3703 ofproto
->port_poll_errno
= 0;
3707 if (sset_is_empty(&ofproto
->port_poll_set
)) {
3711 *devnamep
= sset_pop(&ofproto
->port_poll_set
);
3716 port_poll_wait(const struct ofproto
*ofproto_
)
3718 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3719 dpif_port_poll_wait(ofproto
->backer
->dpif
);
3723 port_is_lacp_current(const struct ofport
*ofport_
)
3725 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3726 return (ofport
->bundle
&& ofport
->bundle
->lacp
3727 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
3731 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3732 * then delete it entirely. */
3734 rule_expire(struct rule_dpif
*rule
, long long now
)
3735 OVS_REQUIRES(ofproto_mutex
)
3737 uint16_t hard_timeout
, idle_timeout
;
3740 hard_timeout
= rule
->up
.hard_timeout
;
3741 idle_timeout
= rule
->up
.idle_timeout
;
3743 /* Has 'rule' expired? */
3745 long long int modified
;
3747 ovs_mutex_lock(&rule
->up
.mutex
);
3748 modified
= rule
->up
.modified
;
3749 ovs_mutex_unlock(&rule
->up
.mutex
);
3751 if (now
> modified
+ hard_timeout
* 1000) {
3752 reason
= OFPRR_HARD_TIMEOUT
;
3756 if (reason
< 0 && idle_timeout
) {
3759 ovs_mutex_lock(&rule
->stats_mutex
);
3760 used
= rule
->stats
.used
;
3761 ovs_mutex_unlock(&rule
->stats_mutex
);
3763 if (now
> used
+ idle_timeout
* 1000) {
3764 reason
= OFPRR_IDLE_TIMEOUT
;
3769 COVERAGE_INC(ofproto_dpif_expired
);
3770 ofproto_rule_expire(&rule
->up
, reason
);
3775 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif
*ofproto
,
3776 ofp_port_t in_port
, struct dp_packet
*packet
)
3778 if (in_port
== OFPP_NONE
) {
3779 in_port
= OFPP_LOCAL
;
3781 packet
->md
.in_port
.odp_port
= ofp_port_to_odp_port(ofproto
, in_port
);
3785 ofproto_dpif_execute_actions__(struct ofproto_dpif
*ofproto
,
3786 ovs_version_t version
, const struct flow
*flow
,
3787 struct rule_dpif
*rule
,
3788 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3789 int depth
, int resubmits
,
3790 struct dp_packet
*packet
)
3792 struct dpif_flow_stats stats
;
3793 struct xlate_out xout
;
3794 struct xlate_in xin
;
3795 struct dpif_execute execute
;
3798 ovs_assert((rule
!= NULL
) != (ofpacts
!= NULL
));
3800 dpif_flow_stats_extract(flow
, packet
, time_msec(), &stats
);
3803 rule_dpif_credit_stats(rule
, &stats
);
3806 uint64_t odp_actions_stub
[1024 / 8];
3807 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
3808 xlate_in_init(&xin
, ofproto
, version
, flow
, flow
->in_port
.ofp_port
, rule
,
3809 stats
.tcp_flags
, packet
, NULL
, &odp_actions
);
3810 xin
.ofpacts
= ofpacts
;
3811 xin
.ofpacts_len
= ofpacts_len
;
3812 xin
.resubmit_stats
= &stats
;
3814 xin
.resubmits
= resubmits
;
3815 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
3820 execute
.actions
= odp_actions
.data
;
3821 execute
.actions_len
= odp_actions
.size
;
3823 pkt_metadata_from_flow(&packet
->md
, flow
);
3824 execute
.packet
= packet
;
3825 execute
.flow
= flow
;
3826 execute
.needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
3827 execute
.probe
= false;
3830 /* Fix up in_port. */
3831 ofproto_dpif_set_packet_odp_port(ofproto
, flow
->in_port
.ofp_port
, packet
);
3833 error
= dpif_execute(ofproto
->backer
->dpif
, &execute
);
3835 xlate_out_uninit(&xout
);
3836 ofpbuf_uninit(&odp_actions
);
3841 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3842 * 'flow' must reflect the data in 'packet'. */
3844 ofproto_dpif_execute_actions(struct ofproto_dpif
*ofproto
,
3845 ovs_version_t version
, const struct flow
*flow
,
3846 struct rule_dpif
*rule
,
3847 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3848 struct dp_packet
*packet
)
3850 return ofproto_dpif_execute_actions__(ofproto
, version
, flow
, rule
,
3851 ofpacts
, ofpacts_len
, 0, 0, packet
);
3855 rule_dpif_credit_stats__(struct rule_dpif
*rule
,
3856 const struct dpif_flow_stats
*stats
,
3858 OVS_REQUIRES(rule
->stats_mutex
)
3860 if (credit_counts
) {
3861 rule
->stats
.n_packets
+= stats
->n_packets
;
3862 rule
->stats
.n_bytes
+= stats
->n_bytes
;
3864 rule
->stats
.used
= MAX(rule
->stats
.used
, stats
->used
);
3868 rule_dpif_credit_stats(struct rule_dpif
*rule
,
3869 const struct dpif_flow_stats
*stats
)
3871 ovs_mutex_lock(&rule
->stats_mutex
);
3872 if (OVS_UNLIKELY(rule
->new_rule
)) {
3873 ovs_mutex_lock(&rule
->new_rule
->stats_mutex
);
3874 rule_dpif_credit_stats__(rule
->new_rule
, stats
, rule
->forward_counts
);
3875 ovs_mutex_unlock(&rule
->new_rule
->stats_mutex
);
3877 rule_dpif_credit_stats__(rule
, stats
, true);
3879 ovs_mutex_unlock(&rule
->stats_mutex
);
3882 /* Sets 'rule''s recirculation id. */
3884 rule_dpif_set_recirc_id(struct rule_dpif
*rule
, uint32_t id
)
3885 OVS_REQUIRES(rule
->up
.mutex
)
3887 ovs_assert(!rule
->recirc_id
|| rule
->recirc_id
== id
);
3888 if (rule
->recirc_id
== id
) {
3889 /* Release the new reference to the same id. */
3892 rule
->recirc_id
= id
;
3896 /* Sets 'rule''s recirculation id. */
3898 rule_set_recirc_id(struct rule
*rule_
, uint32_t id
)
3900 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
3902 ovs_mutex_lock(&rule
->up
.mutex
);
3903 rule_dpif_set_recirc_id(rule
, id
);
3904 ovs_mutex_unlock(&rule
->up
.mutex
);
3908 ofproto_dpif_get_tables_version(struct ofproto_dpif
*ofproto
)
3910 ovs_version_t version
;
3912 /* Use memory_order_acquire to signify that any following memory accesses
3913 * can not be reordered to happen before this atomic read. This makes sure
3914 * all following reads relate to this or a newer version, but never to an
3916 atomic_read_explicit(&ofproto
->tables_version
, &version
,
3917 memory_order_acquire
);
3921 /* The returned rule (if any) is valid at least until the next RCU quiescent
3922 * period. If the rule needs to stay around longer, the caller should take
3925 * 'flow' is non-const to allow for temporary modifications during the lookup.
3926 * Any changes are restored before returning. */
3927 static struct rule_dpif
*
3928 rule_dpif_lookup_in_table(struct ofproto_dpif
*ofproto
, ovs_version_t version
,
3929 uint8_t table_id
, struct flow
*flow
,
3930 struct flow_wildcards
*wc
)
3932 struct classifier
*cls
= &ofproto
->up
.tables
[table_id
].cls
;
3933 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls
, version
,
3938 ofproto_dpif_credit_table_stats(struct ofproto_dpif
*ofproto
, uint8_t table_id
,
3939 uint64_t n_matches
, uint64_t n_misses
)
3941 struct oftable
*tbl
= &ofproto
->up
.tables
[table_id
];
3945 atomic_add_relaxed(&tbl
->n_matched
, n_matches
, &orig
);
3948 atomic_add_relaxed(&tbl
->n_missed
, n_misses
, &orig
);
3952 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3953 * table '*table_id'. Returns the rule that was found, which may be one of the
3954 * special rules according to packet miss hadling. If 'may_packet_in' is
3955 * false, returning of the miss_rule (which issues packet ins for the
3956 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3957 * that were used during the lookup.
3959 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3960 * if none is found then the table miss configuration for that table is
3961 * honored, which can result in additional lookups in other OpenFlow tables.
3962 * In this case the function updates '*table_id' to reflect the final OpenFlow
3963 * table that was searched.
3965 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3968 * The rule is returned in '*rule', which is valid at least until the next
3969 * RCU quiescent period. If the '*rule' needs to stay around longer, the
3970 * caller must take a reference.
3972 * 'in_port' allows the lookup to take place as if the in port had the value
3973 * 'in_port'. This is needed for resubmit action support.
3975 * 'flow' is non-const to allow for temporary modifications during the lookup.
3976 * Any changes are restored before returning. */
3978 rule_dpif_lookup_from_table(struct ofproto_dpif
*ofproto
,
3979 ovs_version_t version
, struct flow
*flow
,
3980 struct flow_wildcards
*wc
,
3981 const struct dpif_flow_stats
*stats
,
3982 uint8_t *table_id
, ofp_port_t in_port
,
3983 bool may_packet_in
, bool honor_table_miss
,
3984 struct xlate_cache
*xcache
)
3986 ovs_be16 old_tp_src
= flow
->tp_src
, old_tp_dst
= flow
->tp_dst
;
3987 ofp_port_t old_in_port
= flow
->in_port
.ofp_port
;
3988 enum ofputil_table_miss miss_config
;
3989 struct rule_dpif
*rule
;
3992 /* We always unwildcard nw_frag (for IP), so they
3993 * need not be unwildcarded here. */
3994 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
3995 && ofproto
->up
.frag_handling
!= OFPUTIL_FRAG_NX_MATCH
) {
3996 if (ofproto
->up
.frag_handling
== OFPUTIL_FRAG_NORMAL
) {
3997 /* We must pretend that transport ports are unavailable. */
3998 flow
->tp_src
= htons(0);
3999 flow
->tp_dst
= htons(0);
4001 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
4002 * Use the drop_frags_rule (which cannot disappear). */
4003 rule
= ofproto
->drop_frags_rule
;
4005 struct oftable
*tbl
= &ofproto
->up
.tables
[*table_id
];
4008 atomic_add_relaxed(&tbl
->n_matched
, stats
->n_packets
, &orig
);
4011 struct xc_entry
*entry
;
4013 entry
= xlate_cache_add_entry(xcache
, XC_TABLE
);
4014 entry
->table
.ofproto
= ofproto
;
4015 entry
->table
.id
= *table_id
;
4016 entry
->table
.match
= true;
4022 /* Look up a flow with 'in_port' as the input port. Then restore the
4023 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
4024 * have surprising behavior). */
4025 flow
->in_port
.ofp_port
= in_port
;
4027 /* Our current implementation depends on n_tables == N_TABLES, and
4028 * TBL_INTERNAL being the last table. */
4029 BUILD_ASSERT_DECL(N_TABLES
== TBL_INTERNAL
+ 1);
4031 miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
4033 for (next_id
= *table_id
;
4034 next_id
< ofproto
->up
.n_tables
;
4035 next_id
++, next_id
+= (next_id
== TBL_INTERNAL
))
4037 *table_id
= next_id
;
4038 rule
= rule_dpif_lookup_in_table(ofproto
, version
, next_id
, flow
, wc
);
4040 struct oftable
*tbl
= &ofproto
->up
.tables
[next_id
];
4043 atomic_add_relaxed(rule
? &tbl
->n_matched
: &tbl
->n_missed
,
4044 stats
->n_packets
, &orig
);
4047 struct xc_entry
*entry
;
4049 entry
= xlate_cache_add_entry(xcache
, XC_TABLE
);
4050 entry
->table
.ofproto
= ofproto
;
4051 entry
->table
.id
= next_id
;
4052 entry
->table
.match
= (rule
!= NULL
);
4055 goto out
; /* Match. */
4057 if (honor_table_miss
) {
4058 miss_config
= ofproto_table_get_miss_config(&ofproto
->up
,
4060 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
4067 rule
= ofproto
->no_packet_in_rule
;
4068 if (may_packet_in
) {
4069 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
4070 || miss_config
== OFPUTIL_TABLE_MISS_CONTROLLER
) {
4071 struct ofport_dpif
*port
;
4073 port
= ofp_port_to_ofport(ofproto
, old_in_port
);
4075 VLOG_WARN_RL(&rl
, "packet-in on unknown OpenFlow port %"PRIu32
,
4077 } else if (!(port
->up
.pp
.config
& OFPUTIL_PC_NO_PACKET_IN
)) {
4078 rule
= ofproto
->miss_rule
;
4080 } else if (miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
&&
4081 connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
)) {
4082 rule
= ofproto
->miss_rule
;
4086 /* Restore port numbers, as they may have been modified above. */
4087 flow
->tp_src
= old_tp_src
;
4088 flow
->tp_dst
= old_tp_dst
;
4089 /* Restore the old in port. */
4090 flow
->in_port
.ofp_port
= old_in_port
;
4095 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
4097 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
4100 static struct rule
*
4103 struct rule_dpif
*rule
= xzalloc(sizeof *rule
);
4108 rule_dealloc(struct rule
*rule_
)
4110 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4115 check_mask(struct ofproto_dpif
*ofproto
, const struct miniflow
*flow
)
4117 const struct odp_support
*support
;
4118 uint16_t ct_state
, ct_zone
;
4122 support
= &ofproto
->backer
->support
.odp
;
4123 ct_state
= MINIFLOW_GET_U8(flow
, ct_state
);
4125 /* Do not bother dissecting the flow further if the datapath supports all
4126 * the features we know of. */
4127 if (support
->ct_state
&& support
->ct_zone
&& support
->ct_mark
4128 && support
->ct_label
&& support
->ct_state_nat
4129 && support
->ct_orig_tuple
) {
4130 return ct_state
& CS_UNSUPPORTED_MASK
? OFPERR_OFPBMC_BAD_MASK
: 0;
4133 ct_zone
= MINIFLOW_GET_U16(flow
, ct_zone
);
4134 ct_mark
= MINIFLOW_GET_U32(flow
, ct_mark
);
4135 ct_label
= MINIFLOW_GET_U128(flow
, ct_label
);
4137 if ((ct_state
&& !support
->ct_state
)
4138 || (ct_state
& CS_UNSUPPORTED_MASK
)
4139 || ((ct_state
& (CS_SRC_NAT
| CS_DST_NAT
)) && !support
->ct_state_nat
)
4140 || (ct_zone
&& !support
->ct_zone
)
4141 || (ct_mark
&& !support
->ct_mark
)
4142 || (!ovs_u128_is_zero(ct_label
) && !support
->ct_label
)) {
4143 return OFPERR_OFPBMC_BAD_MASK
;
4146 if (!support
->ct_orig_tuple
&&
4147 (MINIFLOW_GET_U8(flow
, ct_nw_proto
) ||
4148 MINIFLOW_GET_U16(flow
, ct_tp_src
) ||
4149 MINIFLOW_GET_U16(flow
, ct_tp_dst
) ||
4150 MINIFLOW_GET_U32(flow
, ct_nw_src
) ||
4151 MINIFLOW_GET_U32(flow
, ct_nw_dst
) ||
4152 !ovs_u128_is_zero(MINIFLOW_GET_U128(flow
, ct_ipv6_src
)) ||
4153 !ovs_u128_is_zero(MINIFLOW_GET_U128(flow
, ct_ipv6_dst
)))) {
4154 return OFPERR_OFPBMC_BAD_MASK
;
4161 report_unsupported_act(const char *action
, const char *detail
)
4163 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4164 VLOG_WARN_RL(&rl
, "Rejecting %s action because datapath does not support"
4165 "%s%s (your kernel module may be out of date)",
4166 action
, detail
? " " : "", detail
? detail
: "");
4170 check_actions(const struct ofproto_dpif
*ofproto
,
4171 const struct rule_actions
*const actions
)
4173 const struct ofpact
*ofpact
;
4174 const struct odp_support
*support
= &ofproto
->backer
->support
.odp
;
4176 OFPACT_FOR_EACH (ofpact
, actions
->ofpacts
, actions
->ofpacts_len
) {
4177 if (ofpact
->type
== OFPACT_CT
) {
4178 const struct ofpact_conntrack
*ct
;
4179 const struct ofpact
*a
;
4181 ct
= CONTAINER_OF(ofpact
, struct ofpact_conntrack
, ofpact
);
4183 if (!support
->ct_state
) {
4184 report_unsupported_act("ct", "ct action");
4185 return OFPERR_OFPBAC_BAD_TYPE
;
4187 if ((ct
->zone_imm
|| ct
->zone_src
.field
) && !support
->ct_zone
) {
4188 report_unsupported_act("ct", "ct zones");
4189 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4191 /* So far the force commit feature is implemented together with the
4192 * original direction tuple feature by all datapaths, so we use the
4193 * support flag for the 'ct_orig_tuple' to indicate support for the
4194 * force commit feature as well. */
4195 if ((ct
->flags
& NX_CT_F_FORCE
) && !support
->ct_orig_tuple
) {
4196 report_unsupported_act("ct", "force commit");
4197 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4200 OFPACT_FOR_EACH(a
, ct
->actions
, ofpact_ct_get_action_len(ct
)) {
4201 const struct mf_field
*dst
= ofpact_get_mf_dst(a
);
4203 if (a
->type
== OFPACT_NAT
&& !support
->ct_state_nat
) {
4204 /* The backer doesn't seem to support the NAT bits in
4205 * 'ct_state': assume that it doesn't support the NAT
4207 report_unsupported_act("ct", "nat");
4208 return OFPERR_OFPBAC_BAD_TYPE
;
4210 if (dst
&& ((dst
->id
== MFF_CT_MARK
&& !support
->ct_mark
) ||
4211 (dst
->id
== MFF_CT_LABEL
&& !support
->ct_label
))) {
4212 report_unsupported_act("ct", "setting mark and/or label");
4213 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
4216 } else if (ofpact
->type
== OFPACT_RESUBMIT
) {
4217 struct ofpact_resubmit
*resubmit
= ofpact_get_RESUBMIT(ofpact
);
4219 if (resubmit
->with_ct_orig
&& !support
->ct_orig_tuple
) {
4220 report_unsupported_act("resubmit",
4221 "ct original direction tuple");
4222 return OFPERR_OFPBAC_BAD_TYPE
;
4231 rule_check(struct rule
*rule
)
4233 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->ofproto
);
4236 err
= check_mask(ofproto
, &rule
->cr
.match
.mask
->masks
);
4240 return check_actions(ofproto
, rule
->actions
);
4244 rule_construct(struct rule
*rule_
)
4245 OVS_NO_THREAD_SAFETY_ANALYSIS
4247 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4250 error
= rule_check(rule_
);
4255 ovs_mutex_init_adaptive(&rule
->stats_mutex
);
4256 rule
->stats
.n_packets
= 0;
4257 rule
->stats
.n_bytes
= 0;
4258 rule
->stats
.used
= rule
->up
.modified
;
4259 rule
->recirc_id
= 0;
4260 rule
->new_rule
= NULL
;
4261 rule
->forward_counts
= false;
4267 rule_insert(struct rule
*rule_
, struct rule
*old_rule_
, bool forward_counts
)
4268 OVS_REQUIRES(ofproto_mutex
)
4270 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4273 struct rule_dpif
*old_rule
= rule_dpif_cast(old_rule_
);
4275 ovs_assert(!old_rule
->new_rule
);
4277 /* Take a reference to the new rule, and refer all stats updates from
4278 * the old rule to the new rule. */
4279 ofproto_rule_ref(&rule
->up
);
4281 ovs_mutex_lock(&old_rule
->stats_mutex
);
4282 ovs_mutex_lock(&rule
->stats_mutex
);
4283 old_rule
->new_rule
= rule
; /* Forward future stats. */
4284 old_rule
->forward_counts
= forward_counts
;
4286 if (forward_counts
) {
4287 rule
->stats
= old_rule
->stats
; /* Transfer stats to the new
4290 /* Used timestamp must be forwarded whenever a rule is modified. */
4291 rule
->stats
.used
= old_rule
->stats
.used
;
4293 ovs_mutex_unlock(&rule
->stats_mutex
);
4294 ovs_mutex_unlock(&old_rule
->stats_mutex
);
4299 rule_destruct(struct rule
*rule_
)
4300 OVS_NO_THREAD_SAFETY_ANALYSIS
4302 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4304 ovs_mutex_destroy(&rule
->stats_mutex
);
4305 /* Release reference to the new rule, if any. */
4306 if (rule
->new_rule
) {
4307 ofproto_rule_unref(&rule
->new_rule
->up
);
4309 if (rule
->recirc_id
) {
4310 recirc_free_id(rule
->recirc_id
);
4315 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
,
4316 long long int *used
)
4318 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4320 ovs_mutex_lock(&rule
->stats_mutex
);
4321 if (OVS_UNLIKELY(rule
->new_rule
)) {
4322 rule_get_stats(&rule
->new_rule
->up
, packets
, bytes
, used
);
4324 *packets
= rule
->stats
.n_packets
;
4325 *bytes
= rule
->stats
.n_bytes
;
4326 *used
= rule
->stats
.used
;
4328 ovs_mutex_unlock(&rule
->stats_mutex
);
4331 struct ofproto_dpif_packet_out
{
4332 struct xlate_cache xcache
;
4333 struct ofpbuf odp_actions
;
4334 struct recirc_refs rr
;
4339 static struct ofproto_dpif_packet_out
*
4340 ofproto_dpif_packet_out_new(void)
4342 struct ofproto_dpif_packet_out
*aux
= xmalloc(sizeof *aux
);
4343 xlate_cache_init(&aux
->xcache
);
4344 ofpbuf_init(&aux
->odp_actions
, 64);
4345 aux
->rr
= RECIRC_REFS_EMPTY_INITIALIZER
;
4346 aux
->needs_help
= false;
4352 ofproto_dpif_packet_out_delete(struct ofproto_dpif_packet_out
*aux
)
4355 xlate_cache_uninit(&aux
->xcache
);
4356 ofpbuf_uninit(&aux
->odp_actions
);
4357 recirc_refs_unref(&aux
->rr
);
4363 packet_xlate(struct ofproto
*ofproto_
, struct ofproto_packet_out
*opo
)
4364 OVS_REQUIRES(ofproto_mutex
)
4366 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4367 struct xlate_out xout
;
4368 struct xlate_in xin
;
4369 enum ofperr error
= 0;
4371 struct ofproto_dpif_packet_out
*aux
= ofproto_dpif_packet_out_new();
4373 xlate_in_init(&xin
, ofproto
, opo
->version
, opo
->flow
,
4374 opo
->flow
->in_port
.ofp_port
, NULL
, 0, opo
->packet
, NULL
,
4376 xin
.ofpacts
= opo
->ofpacts
;
4377 xin
.ofpacts_len
= opo
->ofpacts_len
;
4378 /* No learning or stats, but collect side effects to xcache. */
4379 xin
.allow_side_effects
= false;
4380 xin
.resubmit_stats
= NULL
;
4381 xin
.xcache
= &aux
->xcache
;
4383 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
4384 error
= OFPERR_OFPFMFC_UNKNOWN
; /* Error processing actions. */
4387 /* Prepare learn actions. */
4388 struct xc_entry
*entry
;
4389 struct ofpbuf entries
= aux
->xcache
.entries
;
4391 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4392 if (entry
->type
== XC_LEARN
) {
4393 struct ofproto_flow_mod
*ofm
= entry
->learn
.ofm
;
4395 error
= ofproto_flow_mod_learn_refresh(ofm
);
4399 struct rule
*rule
= ofm
->temp_rule
;
4400 ofm
->learn_adds_rule
= (rule
->state
== RULE_INITIALIZED
);
4401 if (ofm
->learn_adds_rule
) {
4402 /* If learning on a different bridge, must use its next
4403 * version number. */
4404 ofm
->version
= (rule
->ofproto
== ofproto_
)
4405 ? opo
->version
: rule
->ofproto
->tables_version
+ 1;
4406 error
= ofproto_flow_mod_learn_start(ofm
);
4415 aux
->needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
4416 recirc_refs_swap(&aux
->rr
, &xout
.recircs
); /* Hold recirc refs. */
4418 xlate_out_uninit(&xout
);
4423 xlate_out_uninit(&xout
);
4424 ofproto_dpif_packet_out_delete(aux
);
4430 packet_xlate_revert(struct ofproto
*ofproto OVS_UNUSED
,
4431 struct ofproto_packet_out
*opo
)
4432 OVS_REQUIRES(ofproto_mutex
)
4434 struct ofproto_dpif_packet_out
*aux
= opo
->aux
;
4437 /* Revert the learned flows. */
4438 struct xc_entry
*entry
;
4439 struct ofpbuf entries
= aux
->xcache
.entries
;
4441 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4442 if (entry
->type
== XC_LEARN
&& entry
->learn
.ofm
->learn_adds_rule
) {
4443 ofproto_flow_mod_learn_revert(entry
->learn
.ofm
);
4447 ofproto_dpif_packet_out_delete(aux
);
4451 /* Push stats and perform side effects of flow translation. */
4453 ofproto_dpif_xcache_execute(struct ofproto_dpif
*ofproto
,
4454 struct xlate_cache
*xcache
,
4455 const struct dpif_flow_stats
*stats
)
4456 OVS_REQUIRES(ofproto_mutex
)
4458 struct xc_entry
*entry
;
4459 struct ofpbuf entries
= xcache
->entries
;
4461 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4462 switch (entry
->type
) {
4464 /* Finish the learned flows. */
4465 if (entry
->learn
.ofm
->learn_adds_rule
) {
4466 ofproto_flow_mod_learn_finish(entry
->learn
.ofm
, &ofproto
->up
);
4469 case XC_FIN_TIMEOUT
:
4470 if (stats
->tcp_flags
& (TCP_FIN
| TCP_RST
)) {
4471 /* 'ofproto_mutex' already held */
4472 ofproto_rule_reduce_timeouts__(&entry
->fin
.rule
->up
,
4477 /* All the rest can be dealt with by the xlate layer. */
4488 xlate_push_stats_entry(entry
, stats
);
4497 packet_execute(struct ofproto
*ofproto_
, struct ofproto_packet_out
*opo
)
4498 OVS_REQUIRES(ofproto_mutex
)
4500 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4501 struct dpif_flow_stats stats
;
4502 struct dpif_execute execute
;
4504 struct ofproto_dpif_packet_out
*aux
= opo
->aux
;
4507 /* Run the side effects from the xcache. */
4508 dpif_flow_stats_extract(opo
->flow
, opo
->packet
, time_msec(), &stats
);
4509 ofproto_dpif_xcache_execute(ofproto
, &aux
->xcache
, &stats
);
4511 execute
.actions
= aux
->odp_actions
.data
;
4512 execute
.actions_len
= aux
->odp_actions
.size
;
4514 pkt_metadata_from_flow(&opo
->packet
->md
, opo
->flow
);
4515 execute
.packet
= opo
->packet
;
4516 execute
.flow
= opo
->flow
;
4517 execute
.needs_help
= aux
->needs_help
;
4518 execute
.probe
= false;
4521 /* Fix up in_port. */
4522 ofproto_dpif_set_packet_odp_port(ofproto
, opo
->flow
->in_port
.ofp_port
,
4525 dpif_execute(ofproto
->backer
->dpif
, &execute
);
4526 ofproto_dpif_packet_out_delete(aux
);
4530 static struct group_dpif
*group_dpif_cast(const struct ofgroup
*group
)
4532 return group
? CONTAINER_OF(group
, struct group_dpif
, up
) : NULL
;
4535 static struct ofgroup
*
4538 struct group_dpif
*group
= xzalloc(sizeof *group
);
4543 group_dealloc(struct ofgroup
*group_
)
4545 struct group_dpif
*group
= group_dpif_cast(group_
);
4550 group_construct_stats(struct group_dpif
*group
)
4551 OVS_REQUIRES(group
->stats_mutex
)
4553 group
->packet_count
= 0;
4554 group
->byte_count
= 0;
4556 struct ofputil_bucket
*bucket
;
4557 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4558 bucket
->stats
.packet_count
= 0;
4559 bucket
->stats
.byte_count
= 0;
4564 group_dpif_credit_stats(struct group_dpif
*group
,
4565 struct ofputil_bucket
*bucket
,
4566 const struct dpif_flow_stats
*stats
)
4568 ovs_mutex_lock(&group
->stats_mutex
);
4569 group
->packet_count
+= stats
->n_packets
;
4570 group
->byte_count
+= stats
->n_bytes
;
4572 bucket
->stats
.packet_count
+= stats
->n_packets
;
4573 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4574 } else { /* Credit to all buckets */
4575 struct ofputil_bucket
*bucket
;
4576 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4577 bucket
->stats
.packet_count
+= stats
->n_packets
;
4578 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4581 ovs_mutex_unlock(&group
->stats_mutex
);
4585 group_construct(struct ofgroup
*group_
)
4587 struct group_dpif
*group
= group_dpif_cast(group_
);
4589 ovs_mutex_init_adaptive(&group
->stats_mutex
);
4590 ovs_mutex_lock(&group
->stats_mutex
);
4591 group_construct_stats(group
);
4592 ovs_mutex_unlock(&group
->stats_mutex
);
4597 group_destruct(struct ofgroup
*group_
)
4599 struct group_dpif
*group
= group_dpif_cast(group_
);
4600 ovs_mutex_destroy(&group
->stats_mutex
);
4604 group_get_stats(const struct ofgroup
*group_
, struct ofputil_group_stats
*ogs
)
4606 struct group_dpif
*group
= group_dpif_cast(group_
);
4608 ovs_mutex_lock(&group
->stats_mutex
);
4609 ogs
->packet_count
= group
->packet_count
;
4610 ogs
->byte_count
= group
->byte_count
;
4612 struct bucket_counter
*bucket_stats
= ogs
->bucket_stats
;
4613 struct ofputil_bucket
*bucket
;
4614 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4615 bucket_stats
->packet_count
= bucket
->stats
.packet_count
;
4616 bucket_stats
->byte_count
= bucket
->stats
.byte_count
;
4619 ovs_mutex_unlock(&group
->stats_mutex
);
4624 /* If the group exists, this function increments the groups's reference count.
4626 * Make sure to call ofproto_group_unref() after no longer needing to maintain
4627 * a reference to the group. */
4629 group_dpif_lookup(struct ofproto_dpif
*ofproto
, uint32_t group_id
,
4630 ovs_version_t version
, bool take_ref
)
4632 struct ofgroup
*ofgroup
= ofproto_group_lookup(&ofproto
->up
, group_id
,
4634 return ofgroup
? group_dpif_cast(ofgroup
) : NULL
;
4637 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4638 * supports a notion of an OAM flag, sets it if 'oam' is true.
4639 * May modify 'packet'.
4640 * Returns 0 if successful, otherwise a positive errno value. */
4642 ofproto_dpif_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
4643 struct dp_packet
*packet
)
4645 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
4648 error
= xlate_send_packet(ofport
, oam
, packet
);
4650 ovs_mutex_lock(&ofproto
->stats_mutex
);
4651 ofproto
->stats
.tx_packets
++;
4652 ofproto
->stats
.tx_bytes
+= dp_packet_size(packet
);
4653 ovs_mutex_unlock(&ofproto
->stats_mutex
);
4657 /* Return the version string of the datapath that backs up
4661 get_datapath_version(const struct ofproto
*ofproto_
)
4663 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4665 return ofproto
->backer
->dp_version_string
;
4669 type_set_config(const char *type
, const struct smap
*other_config
)
4671 struct dpif_backer
*backer
;
4673 backer
= shash_find_data(&all_dpif_backers
, type
);
4675 /* This is not necessarily a problem, since backers are only
4676 * created on demand. */
4680 dpif_set_config(backer
->dpif
, other_config
);
4684 ct_flush(const struct ofproto
*ofproto_
, const uint16_t *zone
)
4686 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4688 ct_dpif_flush(ofproto
->backer
->dpif
, zone
);
4692 set_frag_handling(struct ofproto
*ofproto_
,
4693 enum ofputil_frag_handling frag_handling
)
4695 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4696 if (frag_handling
!= OFPUTIL_FRAG_REASM
) {
4697 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4705 nxt_resume(struct ofproto
*ofproto_
,
4706 const struct ofputil_packet_in_private
*pin
)
4708 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4710 /* Translate pin into datapath actions. */
4711 uint64_t odp_actions_stub
[1024 / 8];
4712 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
4713 enum slow_path_reason slow
;
4714 enum ofperr error
= xlate_resume(ofproto
, pin
, &odp_actions
, &slow
);
4716 /* Steal 'pin->packet' and put it into a dp_packet. */
4717 struct dp_packet packet
;
4718 dp_packet_init(&packet
, pin
->public.packet_len
);
4719 dp_packet_put(&packet
, pin
->public.packet
, pin
->public.packet_len
);
4721 pkt_metadata_from_flow(&packet
.md
, &pin
->public.flow_metadata
.flow
);
4723 /* Fix up in_port. */
4724 ofproto_dpif_set_packet_odp_port(ofproto
,
4725 pin
->public.flow_metadata
.flow
.in_port
.ofp_port
,
4728 struct flow headers
;
4729 flow_extract(&packet
, &headers
);
4731 /* Execute the datapath actions on the packet. */
4732 struct dpif_execute execute
= {
4733 .actions
= odp_actions
.data
,
4734 .actions_len
= odp_actions
.size
,
4735 .needs_help
= (slow
& SLOW_ACTION
) != 0,
4739 dpif_execute(ofproto
->backer
->dpif
, &execute
);
4742 ofpbuf_uninit(&odp_actions
);
4743 dp_packet_uninit(&packet
);
4751 set_netflow(struct ofproto
*ofproto_
,
4752 const struct netflow_options
*netflow_options
)
4754 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4756 if (netflow_options
) {
4757 if (!ofproto
->netflow
) {
4758 ofproto
->netflow
= netflow_create();
4759 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4761 return netflow_set_options(ofproto
->netflow
, netflow_options
);
4762 } else if (ofproto
->netflow
) {
4763 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
4764 netflow_unref(ofproto
->netflow
);
4765 ofproto
->netflow
= NULL
;
4772 get_netflow_ids(const struct ofproto
*ofproto_
,
4773 uint8_t *engine_type
, uint8_t *engine_id
)
4775 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4777 dpif_get_netflow_ids(ofproto
->backer
->dpif
, engine_type
, engine_id
);
4780 struct ofproto_dpif
*
4781 ofproto_dpif_lookup(const char *name
)
4783 struct ofproto_dpif
*ofproto
;
4785 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_node
,
4786 hash_string(name
, 0), &all_ofproto_dpifs
) {
4787 if (!strcmp(ofproto
->up
.name
, name
)) {
4795 ofproto_unixctl_fdb_flush(struct unixctl_conn
*conn
, int argc
,
4796 const char *argv
[], void *aux OVS_UNUSED
)
4798 struct ofproto_dpif
*ofproto
;
4801 ofproto
= ofproto_dpif_lookup(argv
[1]);
4803 unixctl_command_reply_error(conn
, "no such bridge");
4806 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4807 mac_learning_flush(ofproto
->ml
);
4808 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4810 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4811 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
4812 mac_learning_flush(ofproto
->ml
);
4813 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4817 unixctl_command_reply(conn
, "table successfully flushed");
4821 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn
*conn
, int argc
,
4822 const char *argv
[], void *aux OVS_UNUSED
)
4824 struct ofproto_dpif
*ofproto
;
4827 ofproto
= ofproto_dpif_lookup(argv
[1]);
4829 unixctl_command_reply_error(conn
, "no such bridge");
4833 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4834 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4837 mcast_snooping_mdb_flush(ofproto
->ms
);
4839 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4840 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4843 mcast_snooping_mdb_flush(ofproto
->ms
);
4847 unixctl_command_reply(conn
, "table successfully flushed");
4850 static struct ofport_dpif
*
4851 ofbundle_get_a_port(const struct ofbundle
*bundle
)
4853 return CONTAINER_OF(ovs_list_front(&bundle
->ports
), struct ofport_dpif
,
4858 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4859 const char *argv
[], void *aux OVS_UNUSED
)
4861 struct ds ds
= DS_EMPTY_INITIALIZER
;
4862 const struct ofproto_dpif
*ofproto
;
4863 const struct mac_entry
*e
;
4865 ofproto
= ofproto_dpif_lookup(argv
[1]);
4867 unixctl_command_reply_error(conn
, "no such bridge");
4871 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
4872 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
4873 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
4874 struct ofbundle
*bundle
= mac_entry_get_port(ofproto
->ml
, e
);
4875 char name
[OFP10_MAX_PORT_NAME_LEN
];
4877 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4879 ds_put_format(&ds
, "%5s %4d "ETH_ADDR_FMT
" %3d\n",
4880 name
, e
->vlan
, ETH_ADDR_ARGS(e
->mac
),
4881 mac_entry_age(ofproto
->ml
, e
));
4883 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
4884 unixctl_command_reply(conn
, ds_cstr(&ds
));
4889 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn
*conn
,
4890 int argc OVS_UNUSED
,
4892 void *aux OVS_UNUSED
)
4894 struct ds ds
= DS_EMPTY_INITIALIZER
;
4895 const struct ofproto_dpif
*ofproto
;
4896 const struct ofbundle
*bundle
;
4897 const struct mcast_group
*grp
;
4898 struct mcast_group_bundle
*b
;
4899 struct mcast_mrouter_bundle
*mrouter
;
4901 ofproto
= ofproto_dpif_lookup(argv
[1]);
4903 unixctl_command_reply_error(conn
, "no such bridge");
4907 if (!mcast_snooping_enabled(ofproto
->ms
)) {
4908 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
4912 ds_put_cstr(&ds
, " port VLAN GROUP Age\n");
4913 ovs_rwlock_rdlock(&ofproto
->ms
->rwlock
);
4914 LIST_FOR_EACH (grp
, group_node
, &ofproto
->ms
->group_lru
) {
4915 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
4916 char name
[OFP10_MAX_PORT_NAME_LEN
];
4919 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4921 ds_put_format(&ds
, "%5s %4d ", name
, grp
->vlan
);
4922 ipv6_format_mapped(&grp
->addr
, &ds
);
4923 ds_put_format(&ds
, " %3d\n",
4924 mcast_bundle_age(ofproto
->ms
, b
));
4928 /* ports connected to multicast routers */
4929 LIST_FOR_EACH(mrouter
, mrouter_node
, &ofproto
->ms
->mrouter_lru
) {
4930 char name
[OFP10_MAX_PORT_NAME_LEN
];
4932 bundle
= mrouter
->port
;
4933 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
4935 ds_put_format(&ds
, "%5s %4d querier %3d\n",
4936 name
, mrouter
->vlan
,
4937 mcast_mrouter_age(ofproto
->ms
, mrouter
));
4939 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
4940 unixctl_command_reply(conn
, ds_cstr(&ds
));
4944 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4945 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4946 * to destroy 'ofproto_shash' and free the returned value. */
4947 static const struct shash_node
**
4948 get_ofprotos(struct shash
*ofproto_shash
)
4950 const struct ofproto_dpif
*ofproto
;
4952 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_node
, &all_ofproto_dpifs
) {
4953 char *name
= xasprintf("%s@%s", ofproto
->up
.type
, ofproto
->up
.name
);
4954 shash_add_nocopy(ofproto_shash
, name
, ofproto
);
4957 return shash_sort(ofproto_shash
);
4961 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
4962 const char *argv
[] OVS_UNUSED
,
4963 void *aux OVS_UNUSED
)
4965 struct ds ds
= DS_EMPTY_INITIALIZER
;
4966 struct shash ofproto_shash
;
4967 const struct shash_node
**sorted_ofprotos
;
4970 shash_init(&ofproto_shash
);
4971 sorted_ofprotos
= get_ofprotos(&ofproto_shash
);
4972 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
4973 const struct shash_node
*node
= sorted_ofprotos
[i
];
4974 ds_put_format(&ds
, "%s\n", node
->name
);
4977 shash_destroy(&ofproto_shash
);
4978 free(sorted_ofprotos
);
4980 unixctl_command_reply(conn
, ds_cstr(&ds
));
4985 show_dp_feature_bool(struct ds
*ds
, const char *feature
, bool b
)
4987 ds_put_format(ds
, "%s: %s\n", feature
, b
? "Yes" : "No");
4991 show_dp_feature_size_t(struct ds
*ds
, const char *feature
, size_t s
)
4993 ds_put_format(ds
, "%s: %"PRIuSIZE
"\n", feature
, s
);
4997 dpif_show_support(const struct dpif_backer_support
*support
, struct ds
*ds
)
4999 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5000 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5002 #undef DPIF_SUPPORT_FIELD
5004 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5005 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5007 #undef ODP_SUPPORT_FIELD
5011 dpif_show_backer(const struct dpif_backer
*backer
, struct ds
*ds
)
5013 const struct shash_node
**ofprotos
;
5014 struct dpif_dp_stats dp_stats
;
5015 struct shash ofproto_shash
;
5018 dpif_get_dp_stats(backer
->dpif
, &dp_stats
);
5019 ds_put_format(ds
, "%s: hit:%"PRIu64
" missed:%"PRIu64
"\n",
5020 dpif_name(backer
->dpif
), dp_stats
.n_hit
, dp_stats
.n_missed
);
5022 shash_init(&ofproto_shash
);
5023 ofprotos
= get_ofprotos(&ofproto_shash
);
5024 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
5025 struct ofproto_dpif
*ofproto
= ofprotos
[i
]->data
;
5026 const struct shash_node
**ports
;
5029 if (ofproto
->backer
!= backer
) {
5033 ds_put_format(ds
, "\t%s:\n", ofproto
->up
.name
);
5035 ports
= shash_sort(&ofproto
->up
.port_by_name
);
5036 for (j
= 0; j
< shash_count(&ofproto
->up
.port_by_name
); j
++) {
5037 const struct shash_node
*node
= ports
[j
];
5038 struct ofport
*ofport
= node
->data
;
5040 odp_port_t odp_port
;
5042 ds_put_format(ds
, "\t\t%s %u/", netdev_get_name(ofport
->netdev
),
5045 odp_port
= ofp_port_to_odp_port(ofproto
, ofport
->ofp_port
);
5046 if (odp_port
!= ODPP_NONE
) {
5047 ds_put_format(ds
, "%"PRIu32
":", odp_port
);
5049 ds_put_cstr(ds
, "none:");
5052 ds_put_format(ds
, " (%s", netdev_get_type(ofport
->netdev
));
5055 if (!netdev_get_config(ofport
->netdev
, &config
)) {
5056 const struct smap_node
**nodes
;
5059 nodes
= smap_sort(&config
);
5060 for (i
= 0; i
< smap_count(&config
); i
++) {
5061 const struct smap_node
*node
= nodes
[i
];
5062 ds_put_format(ds
, "%c %s=%s", i
? ',' : ':',
5063 node
->key
, node
->value
);
5067 smap_destroy(&config
);
5069 ds_put_char(ds
, ')');
5070 ds_put_char(ds
, '\n');
5074 shash_destroy(&ofproto_shash
);
5079 ofproto_unixctl_dpif_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5080 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
5082 struct ds ds
= DS_EMPTY_INITIALIZER
;
5083 const struct shash_node
**backers
;
5086 backers
= shash_sort(&all_dpif_backers
);
5087 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5088 dpif_show_backer(backers
[i
]->data
, &ds
);
5092 unixctl_command_reply(conn
, ds_cstr(&ds
));
5097 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn
*conn
,
5098 int argc OVS_UNUSED
, const char *argv
[],
5099 void *aux OVS_UNUSED
)
5101 const struct ofproto_dpif
*ofproto
;
5103 struct ds ds
= DS_EMPTY_INITIALIZER
;
5104 bool verbosity
= false;
5106 struct dpif_port dpif_port
;
5107 struct dpif_port_dump port_dump
;
5108 struct hmap portno_names
;
5110 struct dpif_flow_dump
*flow_dump
;
5111 struct dpif_flow_dump_thread
*flow_dump_thread
;
5115 ofproto
= ofproto_dpif_lookup(argv
[argc
- 1]);
5117 unixctl_command_reply_error(conn
, "no such bridge");
5121 if (argc
> 2 && !strcmp(argv
[1], "-m")) {
5125 hmap_init(&portno_names
);
5126 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, ofproto
->backer
->dpif
) {
5127 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
5131 flow_dump
= dpif_flow_dump_create(ofproto
->backer
->dpif
, false);
5132 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
5133 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
5136 if (odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
) == ODP_FIT_ERROR
5137 || xlate_lookup_ofproto(ofproto
->backer
, &flow
, NULL
) != ofproto
) {
5142 odp_format_ufid(&f
.ufid
, &ds
);
5143 ds_put_cstr(&ds
, " ");
5145 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
5146 &portno_names
, &ds
, verbosity
);
5147 ds_put_cstr(&ds
, ", ");
5148 dpif_flow_stats_format(&f
.stats
, &ds
);
5149 ds_put_cstr(&ds
, ", actions:");
5150 format_odp_actions(&ds
, f
.actions
, f
.actions_len
);
5151 ds_put_char(&ds
, '\n');
5153 dpif_flow_dump_thread_destroy(flow_dump_thread
);
5154 error
= dpif_flow_dump_destroy(flow_dump
);
5158 ds_put_format(&ds
, "dpif/dump_flows failed: %s", ovs_strerror(errno
));
5159 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
5161 unixctl_command_reply(conn
, ds_cstr(&ds
));
5163 odp_portno_names_destroy(&portno_names
);
5164 hmap_destroy(&portno_names
);
5169 ofproto_revalidate_all_backers(void)
5171 const struct shash_node
**backers
;
5174 backers
= shash_sort(&all_dpif_backers
);
5175 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5176 struct dpif_backer
*backer
= backers
[i
]->data
;
5177 backer
->need_revalidate
= REV_RECONFIGURE
;
5183 disable_tnl_push_pop(struct unixctl_conn
*conn OVS_UNUSED
, int argc OVS_UNUSED
,
5184 const char *argv
[], void *aux OVS_UNUSED
)
5186 if (!strcasecmp(argv
[1], "off")) {
5187 ofproto_use_tnl_push_pop
= false;
5188 unixctl_command_reply(conn
, "Tunnel push-pop off");
5189 ofproto_revalidate_all_backers();
5190 } else if (!strcasecmp(argv
[1], "on")) {
5191 ofproto_use_tnl_push_pop
= true;
5192 unixctl_command_reply(conn
, "Tunnel push-pop on");
5193 ofproto_revalidate_all_backers();
5195 unixctl_command_reply_error(conn
, "Invalid argument");
5200 disable_datapath_truncate(struct unixctl_conn
*conn OVS_UNUSED
,
5201 int argc OVS_UNUSED
,
5202 const char *argv
[] OVS_UNUSED
,
5203 void *aux OVS_UNUSED
)
5205 const struct shash_node
**backers
;
5208 backers
= shash_sort(&all_dpif_backers
);
5209 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5210 struct dpif_backer
*backer
= backers
[i
]->data
;
5211 backer
->support
.trunc
= false;
5214 unixctl_command_reply(conn
, "Datapath truncate action diabled");
5218 disable_datapath_clone(struct unixctl_conn
*conn OVS_UNUSED
,
5219 int argc
, const char *argv
[],
5220 void *aux OVS_UNUSED
)
5222 struct ds ds
= DS_EMPTY_INITIALIZER
;
5223 const char *br
= argv
[argc
-1];
5224 struct ofproto_dpif
*ofproto
;
5226 ofproto
= ofproto_dpif_lookup(br
);
5228 unixctl_command_reply_error(conn
, "no such bridge");
5231 xlate_disable_dp_clone(ofproto
);
5232 udpif_flush(ofproto
->backer
->udpif
);
5233 ds_put_format(&ds
, "Datapath clone action disabled for bridge %s", br
);
5234 unixctl_command_reply(conn
, ds_cstr(&ds
));
5238 ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn
*conn
,
5239 int argc
, const char *argv
[],
5240 void *aux OVS_UNUSED
)
5242 struct ds ds
= DS_EMPTY_INITIALIZER
;
5243 const char *br
= argv
[argc
-1];
5244 struct ofproto_dpif
*ofproto
= ofproto_dpif_lookup(br
);
5247 unixctl_command_reply_error(conn
, "no such bridge");
5251 dpif_show_support(&ofproto
->backer
->support
, &ds
);
5252 unixctl_command_reply(conn
, ds_cstr(&ds
));
5256 ofproto_unixctl_init(void)
5258 static bool registered
;
5264 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5265 ofproto_unixctl_fdb_flush
, NULL
);
5266 unixctl_command_register("fdb/show", "bridge", 1, 1,
5267 ofproto_unixctl_fdb_show
, NULL
);
5268 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5269 ofproto_unixctl_mcast_snooping_flush
, NULL
);
5270 unixctl_command_register("mdb/show", "bridge", 1, 1,
5271 ofproto_unixctl_mcast_snooping_show
, NULL
);
5272 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5273 ofproto_unixctl_dpif_dump_dps
, NULL
);
5274 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show
,
5276 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
5277 ofproto_unixctl_dpif_show_dp_features
, NULL
);
5278 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5279 ofproto_unixctl_dpif_dump_flows
, NULL
);
5281 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5282 disable_tnl_push_pop
, NULL
);
5284 unixctl_command_register("dpif/disable-truncate", "", 0, 0,
5285 disable_datapath_truncate
, NULL
);
5287 unixctl_command_register("dpif/disable-dp-clone", "bridge", 1, 1,
5288 disable_datapath_clone
, NULL
);
5292 ofp_port_to_odp_port(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
5294 const struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
5295 return ofport
? ofport
->odp_port
: ODPP_NONE
;
5298 struct ofport_dpif
*
5299 odp_port_to_ofport(const struct dpif_backer
*backer
, odp_port_t odp_port
)
5301 struct ofport_dpif
*port
;
5303 ovs_rwlock_rdlock(&backer
->odp_to_ofport_lock
);
5304 HMAP_FOR_EACH_IN_BUCKET (port
, odp_port_node
, hash_odp_port(odp_port
),
5305 &backer
->odp_to_ofport_map
) {
5306 if (port
->odp_port
== odp_port
) {
5307 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5312 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5317 odp_port_to_ofp_port(const struct ofproto_dpif
*ofproto
, odp_port_t odp_port
)
5319 struct ofport_dpif
*port
;
5321 port
= odp_port_to_ofport(ofproto
->backer
, odp_port
);
5322 if (port
&& &ofproto
->up
== port
->up
.ofproto
) {
5323 return port
->up
.ofp_port
;
5330 ofproto_dpif_add_internal_flow(struct ofproto_dpif
*ofproto
,
5331 const struct match
*match
, int priority
,
5332 uint16_t idle_timeout
,
5333 const struct ofpbuf
*ofpacts
,
5334 struct rule
**rulep
)
5336 struct ofputil_flow_mod fm
;
5337 struct rule_dpif
*rule
;
5340 fm
= (struct ofputil_flow_mod
) {
5341 .buffer_id
= UINT32_MAX
,
5343 .priority
= priority
,
5344 .table_id
= TBL_INTERNAL
,
5345 .command
= OFPFC_ADD
,
5346 .idle_timeout
= idle_timeout
,
5347 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
5348 .ofpacts
= ofpacts
->data
,
5349 .ofpacts_len
= ofpacts
->size
,
5352 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
5354 VLOG_ERR_RL(&rl
, "failed to add internal flow (%s)",
5355 ofperr_to_string(error
));
5360 rule
= rule_dpif_lookup_in_table(ofproto
,
5361 ofproto_dpif_get_tables_version(ofproto
),
5362 TBL_INTERNAL
, &fm
.match
.flow
,
5373 ofproto_dpif_delete_internal_flow(struct ofproto_dpif
*ofproto
,
5374 struct match
*match
, int priority
)
5376 struct ofputil_flow_mod fm
;
5379 fm
= (struct ofputil_flow_mod
) {
5380 .buffer_id
= UINT32_MAX
,
5382 .priority
= priority
,
5383 .table_id
= TBL_INTERNAL
,
5384 .out_port
= OFPP_ANY
,
5385 .out_group
= OFPG_ANY
,
5386 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
5387 .command
= OFPFC_DELETE_STRICT
,
5390 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
5392 VLOG_ERR_RL(&rl
, "failed to delete internal flow (%s)",
5393 ofperr_to_string(error
));
5401 meter_get_features(const struct ofproto
*ofproto_
,
5402 struct ofputil_meter_features
*features
)
5404 const struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5406 dpif_meter_get_features(ofproto
->backer
->dpif
, features
);
5410 meter_set(struct ofproto
*ofproto_
, ofproto_meter_id
*meter_id
,
5411 struct ofputil_meter_config
*config
)
5413 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5415 switch (dpif_meter_set(ofproto
->backer
->dpif
, meter_id
, config
)) {
5418 case EFBIG
: /* meter_id out of range */
5419 case ENOMEM
: /* Cannot allocate meter */
5420 return OFPERR_OFPMMFC_OUT_OF_METERS
;
5421 case EBADF
: /* Unsupported flags */
5422 return OFPERR_OFPMMFC_BAD_FLAGS
;
5423 case EINVAL
: /* Too many bands */
5424 return OFPERR_OFPMMFC_OUT_OF_BANDS
;
5425 case ENODEV
: /* Unsupported band type */
5426 return OFPERR_OFPMMFC_BAD_BAND
;
5428 return OFPERR_OFPMMFC_UNKNOWN
;
5433 meter_get(const struct ofproto
*ofproto_
, ofproto_meter_id meter_id
,
5434 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5436 const struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5438 if (!dpif_meter_get(ofproto
->backer
->dpif
, meter_id
, stats
, n_bands
)) {
5441 return OFPERR_OFPMMFC_UNKNOWN_METER
;
5445 meter_del(struct ofproto
*ofproto_
, ofproto_meter_id meter_id
)
5447 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5449 dpif_meter_del(ofproto
->backer
->dpif
, meter_id
, NULL
, 0);
5452 const struct ofproto_class ofproto_dpif_class
= {
5466 NULL
, /* get_memory_usage. */
5467 type_get_memory_usage
,
5487 port_is_lacp_current
,
5488 port_get_lacp_stats
,
5489 NULL
, /* rule_choose_table */
5493 NULL
, /* rule_delete */
5498 packet_xlate_revert
,
5516 aa_vlan_get_queue_size
,
5523 get_stp_port_status
,
5528 get_rstp_port_status
,
5535 is_mirror_output_bundle
,
5536 forward_bpdu_changed
,
5537 set_mac_table_config
,
5539 set_mcast_snooping_port
,
5544 group_alloc
, /* group_alloc */
5545 group_construct
, /* group_construct */
5546 group_destruct
, /* group_destruct */
5547 group_dealloc
, /* group_dealloc */
5548 NULL
, /* group_modify */
5549 group_get_stats
, /* group_get_stats */
5550 get_datapath_version
, /* get_datapath_version */
5552 ct_flush
, /* ct_flush */