3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include "byte-order.h"
23 #include "connectivity.h"
28 #include "fail-open.h"
29 #include "guarded-list.h"
33 #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-print.h"
58 #include "openvswitch/ofpbuf.h"
59 #include "openvswitch/uuid.h"
60 #include "openvswitch/vlog.h"
63 #include "ovs-router.h"
64 #include "openvswitch/poll-loop.h"
70 #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_dpif"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 enum port_priority_tags_mode use_priority_tags
;
105 /* Use 802.1p tag for frames in VLAN 0? */
107 bool protected; /* Protected port mode */
110 bool floodable
; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
113 static void bundle_remove(struct ofport
*);
114 static void bundle_update(struct ofbundle
*);
115 static void bundle_destroy(struct ofbundle
*);
116 static void bundle_del_port(struct ofport_dpif
*);
117 static void bundle_run(struct ofbundle
*);
118 static void bundle_wait(struct ofbundle
*);
119 static void bundle_flush_macs(struct ofbundle
*, bool);
120 static void bundle_move(struct ofbundle
*, struct ofbundle
*);
122 static void stp_run(struct ofproto_dpif
*ofproto
);
123 static void stp_wait(struct ofproto_dpif
*ofproto
);
124 static int set_stp_port(struct ofport
*,
125 const struct ofproto_port_stp_settings
*);
127 static void rstp_run(struct ofproto_dpif
*ofproto
);
128 static void set_rstp_port(struct ofport
*,
129 const struct ofproto_port_rstp_settings
*);
132 struct hmap_node odp_port_node
; /* In dpif_backer's "odp_to_ofport_map". */
136 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
137 struct ovs_list bundle_node
;/* In struct ofbundle's "ports" list. */
138 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
139 struct bfd
*bfd
; /* BFD, if any. */
140 struct lldp
*lldp
; /* lldp, if any. */
141 bool is_tunnel
; /* This port is a tunnel. */
142 long long int carrier_seq
; /* Carrier status changes. */
143 struct ofport_dpif
*peer
; /* Peer if patch port. */
146 struct stp_port
*stp_port
; /* Spanning Tree Protocol, if any. */
147 enum stp_state stp_state
; /* Always STP_DISABLED if STP not in use. */
148 long long int stp_state_entered
;
150 /* Rapid Spanning Tree. */
151 struct rstp_port
*rstp_port
; /* Rapid Spanning Tree Protocol, if any. */
152 enum rstp_state rstp_state
; /* Always RSTP_DISABLED if RSTP not in use. */
154 /* Queue to DSCP mapping. */
155 struct ofproto_port_queue
*qdscp
;
159 struct ct_timeout_policy
{
160 int ref_count
; /* The number of ct zones that use this
162 uint32_t tp_id
; /* Timeout policy id in the datapath. */
163 struct simap tp
; /* A map from timeout policy attribute to
165 struct hmap_node node
; /* Element in struct dpif_backer's "ct_tps"
167 struct ovs_list list_node
; /* Element in struct dpif_backer's
168 * "ct_tp_kill_list" list. */
171 /* Periodically try to purge deleted timeout policies from the datapath. Retry
172 * may be necessary if the kernel datapath has a non-zero datapath flow
173 * reference count for the timeout policy. */
174 #define TIMEOUT_POLICY_CLEANUP_INTERVAL (20000) /* 20 seconds. */
175 static long long int timeout_policy_cleanup_timer
= LLONG_MIN
;
179 struct ct_timeout_policy
*ct_tp
;
180 struct cmap_node node
; /* Element in struct dpif_backer's
181 * "ct_zones" cmap. */
184 static odp_port_t
ofp_port_to_odp_port(const struct ofproto_dpif
*,
187 static ofp_port_t
odp_port_to_ofp_port(const struct ofproto_dpif
*,
190 static struct ofport_dpif
*
191 ofport_dpif_cast(const struct ofport
*ofport
)
193 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
196 static void port_run(struct ofport_dpif
*);
197 static int set_bfd(struct ofport
*, const struct smap
*);
198 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
199 static int set_lldp(struct ofport
*ofport_
, const struct smap
*cfg
);
200 static void ofport_update_peer(struct ofport_dpif
*);
202 COVERAGE_DEFINE(rev_reconfigure
);
203 COVERAGE_DEFINE(rev_stp
);
204 COVERAGE_DEFINE(rev_rstp
);
205 COVERAGE_DEFINE(rev_bond
);
206 COVERAGE_DEFINE(rev_port_toggled
);
207 COVERAGE_DEFINE(rev_flow_table
);
208 COVERAGE_DEFINE(rev_mac_learning
);
209 COVERAGE_DEFINE(rev_mcast_snooping
);
211 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
212 struct shash all_dpif_backers
= SHASH_INITIALIZER(&all_dpif_backers
);
214 /* All existing ofproto_dpif instances, indexed by ->up.name. */
215 static struct hmap all_ofproto_dpifs_by_name
=
216 HMAP_INITIALIZER(&all_ofproto_dpifs_by_name
);
218 /* All existing ofproto_dpif instances, indexed by ->uuid. */
219 static struct hmap all_ofproto_dpifs_by_uuid
=
220 HMAP_INITIALIZER(&all_ofproto_dpifs_by_uuid
);
222 static bool ofproto_use_tnl_push_pop
= true;
223 static void ofproto_unixctl_init(void);
224 static void ct_zone_config_init(struct dpif_backer
*backer
);
225 static void ct_zone_config_uninit(struct dpif_backer
*backer
);
226 static void ct_zone_timeout_policy_sweep(struct dpif_backer
*backer
);
228 static inline struct ofproto_dpif
*
229 ofproto_dpif_cast(const struct ofproto
*ofproto
)
231 ovs_assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
232 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
235 /* Global variables. */
236 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
238 /* Initial mappings of port to bridge mappings. */
239 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
241 /* Initialize 'ofm' for a learn action. If the rule already existed, reference
242 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
243 * rule reference in both cases. */
245 ofproto_dpif_flow_mod_init_for_learn(struct ofproto_dpif
*ofproto
,
246 const struct ofputil_flow_mod
*fm
,
247 struct ofproto_flow_mod
*ofm
)
249 /* This will not take the global 'ofproto_mutex'. */
250 return ofproto_flow_mod_init_for_learn(&ofproto
->up
, fm
, ofm
);
253 /* Appends 'am' to the queue of asynchronous messages to be sent to the
254 * controller. Takes ownership of 'am' and any data it points to. */
256 ofproto_dpif_send_async_msg(struct ofproto_dpif
*ofproto
,
257 struct ofproto_async_msg
*am
)
259 if (!guarded_list_push_back(&ofproto
->ams
, &am
->list_node
, 1024)) {
260 COVERAGE_INC(packet_in_overflow
);
261 ofproto_async_msg_free(am
);
264 /* Wakes up main thread for packet-in I/O. */
265 seq_change(ofproto
->ams_seq
);
268 /* Factory functions. */
271 init(const struct shash
*iface_hints
)
273 struct shash_node
*node
;
275 /* Make a local copy, since we don't own 'iface_hints' elements. */
276 SHASH_FOR_EACH(node
, iface_hints
) {
277 const struct iface_hint
*orig_hint
= node
->data
;
278 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
280 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
281 new_hint
->br_type
= xstrdup(orig_hint
->br_type
);
282 new_hint
->ofp_port
= orig_hint
->ofp_port
;
284 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
287 ofproto_unixctl_init();
288 ofproto_dpif_trace_init();
293 enumerate_types(struct sset
*types
)
295 dp_enumerate_types(types
);
299 enumerate_names(const char *type
, struct sset
*names
)
301 struct ofproto_dpif
*ofproto
;
304 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
305 &all_ofproto_dpifs_by_name
) {
306 if (strcmp(type
, ofproto
->up
.type
)) {
309 sset_add(names
, ofproto
->up
.name
);
316 del(const char *type
, const char *name
)
321 error
= dpif_open(name
, type
, &dpif
);
323 error
= dpif_delete(dpif
);
330 port_open_type(const char *datapath_type
, const char *port_type
)
332 return dpif_port_open_type(datapath_type
, port_type
);
335 /* Type functions. */
337 static void process_dpif_port_changes(struct dpif_backer
*);
338 static void process_dpif_all_ports_changed(struct dpif_backer
*);
339 static void process_dpif_port_change(struct dpif_backer
*,
340 const char *devname
);
341 static void process_dpif_port_error(struct dpif_backer
*, int error
);
343 static struct ofproto_dpif
*
344 lookup_ofproto_dpif_by_port_name(const char *name
)
346 struct ofproto_dpif
*ofproto
;
348 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
349 &all_ofproto_dpifs_by_name
) {
350 if (sset_contains(&ofproto
->ports
, name
)) {
359 type_run(const char *type
)
361 struct dpif_backer
*backer
;
363 backer
= shash_find_data(&all_dpif_backers
, type
);
365 /* This is not necessarily a problem, since backers are only
366 * created on demand. */
370 if (dpif_run(backer
->dpif
)) {
371 backer
->need_revalidate
= REV_RECONFIGURE
;
374 udpif_run(backer
->udpif
);
376 /* If vswitchd started with other_config:flow_restore_wait set as "true",
377 * and the configuration has now changed to "false", enable receiving
378 * packets from the datapath. */
379 if (!backer
->recv_set_enable
&& !ofproto_get_flow_restore_wait()) {
382 backer
->recv_set_enable
= true;
384 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
386 VLOG_ERR("Failed to enable receiving packets in dpif.");
389 dpif_flow_flush(backer
->dpif
);
390 backer
->need_revalidate
= REV_RECONFIGURE
;
393 if (backer
->recv_set_enable
) {
394 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
397 if (backer
->need_revalidate
) {
398 struct ofproto_dpif
*ofproto
;
399 struct simap_node
*node
;
400 struct simap tmp_backers
;
402 /* Handle tunnel garbage collection. */
403 simap_init(&tmp_backers
);
404 simap_swap(&backer
->tnl_backers
, &tmp_backers
);
406 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
407 &all_ofproto_dpifs_by_name
) {
408 struct ofport_dpif
*iter
;
410 if (backer
!= ofproto
->backer
) {
414 HMAP_FOR_EACH (iter
, up
.hmap_node
, &ofproto
->up
.ports
) {
415 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
417 odp_port_t old_odp_port
;
419 if (!iter
->is_tunnel
) {
423 dp_port
= netdev_vport_get_dpif_port(iter
->up
.netdev
,
424 namebuf
, sizeof namebuf
);
425 old_odp_port
= iter
->odp_port
;
426 node
= simap_find(&tmp_backers
, dp_port
);
428 simap_put(&backer
->tnl_backers
, dp_port
, node
->data
);
429 simap_delete(&tmp_backers
, node
);
430 node
= simap_find(&backer
->tnl_backers
, dp_port
);
432 node
= simap_find(&backer
->tnl_backers
, dp_port
);
434 odp_port_t odp_port
= ODPP_NONE
;
436 if (!dpif_port_add(backer
->dpif
, iter
->up
.netdev
,
438 simap_put(&backer
->tnl_backers
, dp_port
,
439 odp_to_u32(odp_port
));
440 node
= simap_find(&backer
->tnl_backers
, dp_port
);
445 iter
->odp_port
= node
? u32_to_odp(node
->data
) : ODPP_NONE
;
446 if (tnl_port_reconfigure(iter
, iter
->up
.netdev
,
447 iter
->odp_port
, old_odp_port
,
448 ovs_native_tunneling_is_on(ofproto
), dp_port
)) {
449 backer
->need_revalidate
= REV_RECONFIGURE
;
454 SIMAP_FOR_EACH (node
, &tmp_backers
) {
455 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
), false);
457 simap_destroy(&tmp_backers
);
459 switch (backer
->need_revalidate
) {
460 case REV_RECONFIGURE
: COVERAGE_INC(rev_reconfigure
); break;
461 case REV_STP
: COVERAGE_INC(rev_stp
); break;
462 case REV_RSTP
: COVERAGE_INC(rev_rstp
); break;
463 case REV_BOND
: COVERAGE_INC(rev_bond
); break;
464 case REV_PORT_TOGGLED
: COVERAGE_INC(rev_port_toggled
); break;
465 case REV_FLOW_TABLE
: COVERAGE_INC(rev_flow_table
); break;
466 case REV_MAC_LEARNING
: COVERAGE_INC(rev_mac_learning
); break;
467 case REV_MCAST_SNOOPING
: COVERAGE_INC(rev_mcast_snooping
); break;
469 backer
->need_revalidate
= 0;
472 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
473 &all_ofproto_dpifs_by_name
) {
474 struct ofport_dpif
*ofport
;
475 struct ofbundle
*bundle
;
477 if (ofproto
->backer
!= backer
) {
481 xlate_ofproto_set(ofproto
, ofproto
->up
.name
,
482 ofproto
->backer
->dpif
, ofproto
->ml
,
483 ofproto
->stp
, ofproto
->rstp
, ofproto
->ms
,
484 ofproto
->mbridge
, ofproto
->sflow
, ofproto
->ipfix
,
486 ofproto
->up
.forward_bpdu
,
487 connmgr_has_in_band(ofproto
->up
.connmgr
),
488 &ofproto
->backer
->rt_support
);
490 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
491 xlate_bundle_set(ofproto
, bundle
, bundle
->name
,
492 bundle
->vlan_mode
, bundle
->qinq_ethtype
,
493 bundle
->vlan
, bundle
->trunks
, bundle
->cvlans
,
494 bundle
->use_priority_tags
,
495 bundle
->bond
, bundle
->lacp
,
496 bundle
->floodable
, bundle
->protected);
499 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
500 int stp_port
= ofport
->stp_port
501 ? stp_port_no(ofport
->stp_port
)
503 xlate_ofport_set(ofproto
, ofport
->bundle
, ofport
,
504 ofport
->up
.ofp_port
, ofport
->odp_port
,
505 ofport
->up
.netdev
, ofport
->cfm
, ofport
->bfd
,
506 ofport
->lldp
, ofport
->peer
, stp_port
,
507 ofport
->rstp_port
, ofport
->qdscp
,
508 ofport
->n_qdscp
, ofport
->up
.pp
.config
,
509 ofport
->up
.pp
.state
, ofport
->is_tunnel
,
510 ofport
->up
.may_enable
);
515 udpif_revalidate(backer
->udpif
);
518 process_dpif_port_changes(backer
);
519 ct_zone_timeout_policy_sweep(backer
);
524 /* Check for and handle port changes in 'backer''s dpif. */
526 process_dpif_port_changes(struct dpif_backer
*backer
)
532 error
= dpif_port_poll(backer
->dpif
, &devname
);
538 process_dpif_all_ports_changed(backer
);
542 process_dpif_port_change(backer
, devname
);
547 process_dpif_port_error(backer
, error
);
554 process_dpif_all_ports_changed(struct dpif_backer
*backer
)
556 struct ofproto_dpif
*ofproto
;
557 struct dpif_port dpif_port
;
558 struct dpif_port_dump dump
;
559 struct sset devnames
;
562 sset_init(&devnames
);
563 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
564 &all_ofproto_dpifs_by_name
) {
565 if (ofproto
->backer
== backer
) {
566 struct ofport
*ofport
;
568 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
569 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
573 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, backer
->dpif
) {
574 sset_add(&devnames
, dpif_port
.name
);
577 SSET_FOR_EACH (devname
, &devnames
) {
578 process_dpif_port_change(backer
, devname
);
580 sset_destroy(&devnames
);
584 process_dpif_port_change(struct dpif_backer
*backer
, const char *devname
)
586 struct ofproto_dpif
*ofproto
;
587 struct dpif_port port
;
589 /* Don't report on the datapath's device. */
590 if (!strcmp(devname
, dpif_base_name(backer
->dpif
))) {
594 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
595 &all_ofproto_dpifs_by_name
) {
596 if (simap_contains(&ofproto
->backer
->tnl_backers
, devname
)) {
601 ofproto
= lookup_ofproto_dpif_by_port_name(devname
);
602 if (dpif_port_query_by_name(backer
->dpif
, devname
, &port
)) {
603 /* The port was removed. If we know the datapath,
604 * report it through poll_set(). If we don't, it may be
605 * notifying us of a removal we initiated, so ignore it.
606 * If there's a pending ENOBUFS, let it stand, since
607 * everything will be reevaluated. */
608 if (ofproto
&& ofproto
->port_poll_errno
!= ENOBUFS
) {
609 sset_add(&ofproto
->port_poll_set
, devname
);
610 ofproto
->port_poll_errno
= 0;
612 } else if (!ofproto
) {
613 /* The port was added, but we don't know with which
614 * ofproto we should associate it. Delete it. */
615 dpif_port_del(backer
->dpif
, port
.port_no
, false);
617 struct ofport_dpif
*ofport
;
619 ofport
= ofport_dpif_cast(shash_find_data(
620 &ofproto
->up
.port_by_name
, devname
));
622 && ofport
->odp_port
!= port
.port_no
623 && !odp_port_to_ofport(backer
, port
.port_no
))
625 /* 'ofport''s datapath port number has changed from
626 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
627 * structures to match. */
628 ovs_rwlock_wrlock(&backer
->odp_to_ofport_lock
);
629 hmap_remove(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
);
630 ofport
->odp_port
= port
.port_no
;
631 hmap_insert(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
,
632 hash_odp_port(port
.port_no
));
633 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
634 backer
->need_revalidate
= REV_RECONFIGURE
;
637 dpif_port_destroy(&port
);
640 /* Propagate 'error' to all ofprotos based on 'backer'. */
642 process_dpif_port_error(struct dpif_backer
*backer
, int error
)
644 struct ofproto_dpif
*ofproto
;
646 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
647 &all_ofproto_dpifs_by_name
) {
648 if (ofproto
->backer
== backer
) {
649 sset_clear(&ofproto
->port_poll_set
);
650 ofproto
->port_poll_errno
= error
;
656 type_wait(const char *type
)
658 struct dpif_backer
*backer
;
660 backer
= shash_find_data(&all_dpif_backers
, type
);
662 /* This is not necessarily a problem, since backers are only
663 * created on demand. */
667 dpif_wait(backer
->dpif
);
670 /* Basic life-cycle. */
672 static int add_internal_flows(struct ofproto_dpif
*);
674 static struct ofproto
*
677 struct ofproto_dpif
*ofproto
= xzalloc(sizeof *ofproto
);
682 dealloc(struct ofproto
*ofproto_
)
684 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
689 close_dpif_backer(struct dpif_backer
*backer
, bool del
)
691 struct simap_node
*node
;
693 ovs_assert(backer
->refcount
> 0);
695 if (--backer
->refcount
) {
699 udpif_destroy(backer
->udpif
);
701 SIMAP_FOR_EACH (node
, &backer
->tnl_backers
) {
702 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
), false);
704 simap_destroy(&backer
->tnl_backers
);
705 ovs_rwlock_destroy(&backer
->odp_to_ofport_lock
);
706 hmap_destroy(&backer
->odp_to_ofport_map
);
707 shash_find_and_delete(&all_dpif_backers
, backer
->type
);
709 free(backer
->dp_version_string
);
711 dpif_delete(backer
->dpif
);
713 dpif_close(backer
->dpif
);
714 id_pool_destroy(backer
->meter_ids
);
715 ct_zone_config_uninit(backer
);
719 /* Datapath port slated for removal from datapath. */
721 struct ovs_list list_node
;
725 static void check_support(struct dpif_backer
*backer
);
728 open_dpif_backer(const char *type
, struct dpif_backer
**backerp
)
730 struct dpif_backer
*backer
;
731 struct dpif_port_dump port_dump
;
732 struct dpif_port port
;
733 struct shash_node
*node
;
734 struct ovs_list garbage_list
;
735 struct odp_garbage
*garbage
;
742 backer
= shash_find_data(&all_dpif_backers
, type
);
749 backer_name
= xasprintf("ovs-%s", type
);
751 /* Remove any existing datapaths, since we assume we're the only
752 * userspace controlling the datapath. */
754 dp_enumerate_names(type
, &names
);
755 SSET_FOR_EACH(name
, &names
) {
756 struct dpif
*old_dpif
;
758 /* Don't remove our backer if it exists. */
759 if (!strcmp(name
, backer_name
)) {
763 if (dpif_open(name
, type
, &old_dpif
)) {
764 VLOG_WARN("couldn't open old datapath %s to remove it", name
);
766 dpif_delete(old_dpif
);
767 dpif_close(old_dpif
);
770 sset_destroy(&names
);
772 backer
= xmalloc(sizeof *backer
);
774 error
= dpif_create_and_open(backer_name
, type
, &backer
->dpif
);
777 VLOG_ERR("failed to open datapath of type %s: %s", type
,
778 ovs_strerror(error
));
782 backer
->udpif
= udpif_create(backer
, backer
->dpif
);
784 backer
->type
= xstrdup(type
);
785 backer
->refcount
= 1;
786 hmap_init(&backer
->odp_to_ofport_map
);
787 ovs_rwlock_init(&backer
->odp_to_ofport_lock
);
788 backer
->need_revalidate
= 0;
789 simap_init(&backer
->tnl_backers
);
790 backer
->recv_set_enable
= !ofproto_get_flow_restore_wait();
793 if (backer
->recv_set_enable
) {
794 dpif_flow_flush(backer
->dpif
);
797 /* Loop through the ports already on the datapath and remove any
798 * that we don't need anymore. */
799 ovs_list_init(&garbage_list
);
800 dpif_port_dump_start(&port_dump
, backer
->dpif
);
801 while (dpif_port_dump_next(&port_dump
, &port
)) {
802 node
= shash_find(&init_ofp_ports
, port
.name
);
803 if (!node
&& strcmp(port
.name
, dpif_base_name(backer
->dpif
))) {
804 garbage
= xmalloc(sizeof *garbage
);
805 garbage
->odp_port
= port
.port_no
;
806 ovs_list_push_front(&garbage_list
, &garbage
->list_node
);
809 dpif_port_dump_done(&port_dump
);
811 LIST_FOR_EACH_POP (garbage
, list_node
, &garbage_list
) {
812 dpif_port_del(backer
->dpif
, garbage
->odp_port
, false);
816 shash_add(&all_dpif_backers
, type
, backer
);
818 check_support(backer
);
819 atomic_count_init(&backer
->tnl_count
, 0);
821 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
823 VLOG_ERR("failed to listen on datapath of type %s: %s",
824 type
, ovs_strerror(error
));
825 close_dpif_backer(backer
, false);
829 if (backer
->recv_set_enable
) {
830 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
833 backer
->dp_version_string
= dpif_get_dp_version(backer
->dpif
);
835 /* Manage Datapath meter IDs if supported. */
836 struct ofputil_meter_features features
;
837 dpif_meter_get_features(backer
->dpif
, &features
);
838 if (features
.max_meters
) {
839 backer
->meter_ids
= id_pool_create(0, features
.max_meters
);
841 backer
->meter_ids
= NULL
;
844 ct_zone_config_init(backer
);
846 /* Make a pristine snapshot of 'support' into 'boottime_support'.
847 * 'boottime_support' can be checked to prevent 'support' to be changed
848 * beyond the datapath capabilities. In case 'support' is changed by
849 * the user, 'boottime_support' can be used to restore it. */
850 backer
->bt_support
= backer
->rt_support
;
856 ovs_native_tunneling_is_on(struct ofproto_dpif
*ofproto
)
858 return ofproto_use_tnl_push_pop
859 && ofproto
->backer
->rt_support
.tnl_push_pop
860 && atomic_count_get(&ofproto
->backer
->tnl_count
);
863 /* Tests whether 'backer''s datapath supports recirculation. Only newer
864 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
865 * features on older datapaths that don't support this feature.
867 * Returns false if 'backer' definitely does not support recirculation, true if
868 * it seems to support recirculation or if at least the error we get is
871 check_recirc(struct dpif_backer
*backer
)
874 struct odputil_keybuf keybuf
;
877 struct odp_flow_key_parms odp_parms
= {
884 memset(&flow
, 0, sizeof flow
);
888 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
889 odp_flow_key_from_flow(&odp_parms
, &key
);
890 enable_recirc
= dpif_probe_feature(backer
->dpif
, "recirculation", &key
,
894 VLOG_INFO("%s: Datapath supports recirculation",
895 dpif_name(backer
->dpif
));
897 VLOG_INFO("%s: Datapath does not support recirculation",
898 dpif_name(backer
->dpif
));
901 return enable_recirc
;
904 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
905 * some flow attributes for datapaths that support this feature.
907 * Returns true if 'dpif' supports UFID for flow operations.
908 * Returns false if 'dpif' does not support UFID. */
910 check_ufid(struct dpif_backer
*backer
)
913 struct odputil_keybuf keybuf
;
917 struct odp_flow_key_parms odp_parms
= {
921 memset(&flow
, 0, sizeof flow
);
922 flow
.dl_type
= htons(0x1234);
924 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
925 odp_flow_key_from_flow(&odp_parms
, &key
);
926 dpif_flow_hash(backer
->dpif
, key
.data
, key
.size
, &ufid
);
928 enable_ufid
= dpif_probe_feature(backer
->dpif
, "UFID", &key
, NULL
, &ufid
);
931 VLOG_INFO("%s: Datapath supports unique flow ids",
932 dpif_name(backer
->dpif
));
934 VLOG_INFO("%s: Datapath does not support unique flow ids",
935 dpif_name(backer
->dpif
));
940 /* Tests number of 802.1q VLAN headers supported by 'backer''s datapath.
942 * Returns the number of elements in a struct flow's vlan
943 * if the datapath supports at least that many VLAN headers. */
945 check_max_vlan_headers(struct dpif_backer
*backer
)
948 struct odp_flow_key_parms odp_parms
= {
954 memset(&flow
, 0, sizeof flow
);
955 flow
.dl_type
= htons(ETH_TYPE_IP
);
956 for (n
= 0; n
< FLOW_MAX_VLAN_HEADERS
; n
++) {
957 struct odputil_keybuf keybuf
;
960 flow_push_vlan_uninit(&flow
, NULL
);
961 flow
.vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
962 flow
.vlans
[0].tci
= htons(1) | htons(VLAN_CFI
);
964 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
965 odp_flow_key_from_flow(&odp_parms
, &key
);
966 if (!dpif_probe_feature(backer
->dpif
, "VLAN", &key
, NULL
, NULL
)) {
971 VLOG_INFO("%s: VLAN header stack length probed as %d",
972 dpif_name(backer
->dpif
), n
);
975 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
977 * Returns the number of elements in a struct flow's mpls_lse field
978 * if the datapath supports at least that many entries in an
980 * Otherwise returns the number of MPLS push actions supported by
983 check_max_mpls_depth(struct dpif_backer
*backer
)
988 for (n
= 0; n
< FLOW_MAX_MPLS_LABELS
; n
++) {
989 struct odputil_keybuf keybuf
;
991 struct odp_flow_key_parms odp_parms
= {
995 memset(&flow
, 0, sizeof flow
);
996 flow
.dl_type
= htons(ETH_TYPE_MPLS
);
997 flow_set_mpls_bos(&flow
, n
, 1);
999 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1000 odp_flow_key_from_flow(&odp_parms
, &key
);
1001 if (!dpif_probe_feature(backer
->dpif
, "MPLS", &key
, NULL
, NULL
)) {
1006 VLOG_INFO("%s: MPLS label stack length probed as %d",
1007 dpif_name(backer
->dpif
), n
);
1012 add_sample_actions(struct ofpbuf
*actions
, int nesting
)
1015 nl_msg_put_odp_port(actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
1019 size_t start
, actions_start
;
1021 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1022 actions_start
= nl_msg_start_nested(actions
, OVS_SAMPLE_ATTR_ACTIONS
);
1023 add_sample_actions(actions
, nesting
- 1);
1024 nl_msg_end_nested(actions
, actions_start
);
1025 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
, UINT32_MAX
);
1026 nl_msg_end_nested(actions
, start
);
1029 /* Tests the nested sample actions levels supported by 'backer''s datapath.
1031 * Returns the number of nested sample actions accepted by the datapath. */
1033 check_max_sample_nesting(struct dpif_backer
*backer
)
1035 struct odputil_keybuf keybuf
;
1040 struct odp_flow_key_parms odp_parms
= {
1044 memset(&flow
, 0, sizeof flow
);
1045 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1046 odp_flow_key_from_flow(&odp_parms
, &key
);
1048 /* OVS datapath has always supported at least 3 nested levels. */
1049 for (n
= 3; n
< FLOW_MAX_SAMPLE_NESTING
; n
++) {
1050 struct ofpbuf actions
;
1053 ofpbuf_init(&actions
, 300);
1054 add_sample_actions(&actions
, n
);
1055 ok
= dpif_probe_feature(backer
->dpif
, "Sample action nesting", &key
,
1057 ofpbuf_uninit(&actions
);
1063 VLOG_INFO("%s: Max sample nesting level probed as %d",
1064 dpif_name(backer
->dpif
), n
);
1068 /* Tests whether 'backer''s datapath supports masked data in
1069 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1070 * datapaths that don't support this feature. */
1072 check_masked_set_action(struct dpif_backer
*backer
)
1074 struct eth_header
*eth
;
1075 struct ofpbuf actions
;
1076 struct dpif_execute execute
;
1077 struct dp_packet packet
;
1080 struct ovs_key_ethernet key
, mask
;
1082 /* Compose a set action that will cause an EINVAL error on older
1083 * datapaths that don't support masked set actions.
1084 * Avoid using a full mask, as it could be translated to a non-masked
1085 * set action instead. */
1086 ofpbuf_init(&actions
, 64);
1087 memset(&key
, 0x53, sizeof key
);
1088 memset(&mask
, 0x7f, sizeof mask
);
1089 commit_masked_set_action(&actions
, OVS_KEY_ATTR_ETHERNET
, &key
, &mask
,
1092 /* Compose a dummy ethernet packet. */
1093 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1094 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1095 eth
->eth_type
= htons(0x1234);
1097 flow_extract(&packet
, &flow
);
1099 /* Execute the actions. On older datapaths this fails with EINVAL, on
1100 * newer datapaths it succeeds. */
1101 execute
.actions
= actions
.data
;
1102 execute
.actions_len
= actions
.size
;
1103 execute
.packet
= &packet
;
1104 execute
.flow
= &flow
;
1105 execute
.needs_help
= false;
1106 execute
.probe
= true;
1109 error
= dpif_execute(backer
->dpif
, &execute
);
1111 dp_packet_uninit(&packet
);
1112 ofpbuf_uninit(&actions
);
1115 /* Masked set action is not supported. */
1116 VLOG_INFO("%s: datapath does not support masked set action feature.",
1117 dpif_name(backer
->dpif
));
1122 /* Tests whether 'backer''s datapath supports truncation of a packet in
1123 * OVS_ACTION_ATTR_TRUNC. We need to disable some features on older
1124 * datapaths that don't support this feature. */
1126 check_trunc_action(struct dpif_backer
*backer
)
1128 struct eth_header
*eth
;
1129 struct ofpbuf actions
;
1130 struct dpif_execute execute
;
1131 struct dp_packet packet
;
1132 struct ovs_action_trunc
*trunc
;
1136 /* Compose an action with output(port:1,
1137 * max_len:OVS_ACTION_OUTPUT_MIN + 1).
1138 * This translates to one truncate action and one output action. */
1139 ofpbuf_init(&actions
, 64);
1140 trunc
= nl_msg_put_unspec_uninit(&actions
,
1141 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1143 trunc
->max_len
= ETH_HEADER_LEN
+ 1;
1144 nl_msg_put_odp_port(&actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
1146 /* Compose a dummy Ethernet packet. */
1147 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1148 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1149 eth
->eth_type
= htons(0x1234);
1151 flow_extract(&packet
, &flow
);
1153 /* Execute the actions. On older datapaths this fails with EINVAL, on
1154 * newer datapaths it succeeds. */
1155 execute
.actions
= actions
.data
;
1156 execute
.actions_len
= actions
.size
;
1157 execute
.packet
= &packet
;
1158 execute
.flow
= &flow
;
1159 execute
.needs_help
= false;
1160 execute
.probe
= true;
1163 error
= dpif_execute(backer
->dpif
, &execute
);
1165 dp_packet_uninit(&packet
);
1166 ofpbuf_uninit(&actions
);
1169 VLOG_INFO("%s: Datapath does not support truncate action",
1170 dpif_name(backer
->dpif
));
1172 VLOG_INFO("%s: Datapath supports truncate action",
1173 dpif_name(backer
->dpif
));
1179 /* Tests whether 'backer''s datapath supports the clone action
1180 * OVS_ACTION_ATTR_CLONE. */
1182 check_clone(struct dpif_backer
*backer
)
1184 struct dpif_execute execute
;
1185 struct eth_header
*eth
;
1187 struct dp_packet packet
;
1188 struct ofpbuf actions
;
1192 /* Compose clone with an empty action list.
1193 * and check if datapath can decode the message. */
1194 ofpbuf_init(&actions
, 64);
1195 clone_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CLONE
);
1196 nl_msg_end_nested(&actions
, clone_start
);
1198 /* Compose a dummy Ethernet packet. */
1199 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1200 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1201 eth
->eth_type
= htons(0x1234);
1203 flow_extract(&packet
, &flow
);
1205 /* Execute the actions. On older datapaths this fails with EINVAL, on
1206 * newer datapaths it succeeds. */
1207 execute
.actions
= actions
.data
;
1208 execute
.actions_len
= actions
.size
;
1209 execute
.packet
= &packet
;
1210 execute
.flow
= &flow
;
1211 execute
.needs_help
= false;
1212 execute
.probe
= true;
1215 error
= dpif_execute(backer
->dpif
, &execute
);
1217 dp_packet_uninit(&packet
);
1218 ofpbuf_uninit(&actions
);
1221 VLOG_INFO("%s: Datapath does not support clone action",
1222 dpif_name(backer
->dpif
));
1224 VLOG_INFO("%s: Datapath supports clone action",
1225 dpif_name(backer
->dpif
));
1231 /* Tests whether 'backer''s datapath supports the OVS_CT_ATTR_EVENTMASK
1232 * attribute in OVS_ACTION_ATTR_CT. */
1234 check_ct_eventmask(struct dpif_backer
*backer
)
1236 struct dpif_execute execute
;
1237 struct dp_packet packet
;
1238 struct ofpbuf actions
;
1239 struct flow flow
= {
1240 .dl_type
= CONSTANT_HTONS(ETH_TYPE_IP
),
1241 .nw_proto
= IPPROTO_UDP
,
1243 /* Use the broadcast address on the loopback address range 127/8 to
1244 * avoid hitting any real conntrack entries. We leave the UDP ports to
1245 * zeroes for the same purpose. */
1246 .nw_src
= CONSTANT_HTONL(0x7fffffff),
1247 .nw_dst
= CONSTANT_HTONL(0x7fffffff),
1252 /* Compose CT action with eventmask attribute and check if datapath can
1253 * decode the message. */
1254 ofpbuf_init(&actions
, 64);
1255 ct_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CT
);
1256 /* Eventmask has no effect without the commit flag, but currently the
1257 * datapath will accept an eventmask even without commit. This is useful
1258 * as we do not want to persist the probe connection in the conntrack
1260 nl_msg_put_u32(&actions
, OVS_CT_ATTR_EVENTMASK
, ~0);
1261 nl_msg_end_nested(&actions
, ct_start
);
1263 /* Compose a dummy UDP packet. */
1264 dp_packet_init(&packet
, 0);
1265 flow_compose(&packet
, &flow
, NULL
, 64);
1267 /* Execute the actions. On older datapaths this fails with EINVAL, on
1268 * newer datapaths it succeeds. */
1269 execute
.actions
= actions
.data
;
1270 execute
.actions_len
= actions
.size
;
1271 execute
.packet
= &packet
;
1272 execute
.flow
= &flow
;
1273 execute
.needs_help
= false;
1274 execute
.probe
= true;
1277 error
= dpif_execute(backer
->dpif
, &execute
);
1279 dp_packet_uninit(&packet
);
1280 ofpbuf_uninit(&actions
);
1283 VLOG_INFO("%s: Datapath does not support eventmask in conntrack action",
1284 dpif_name(backer
->dpif
));
1286 VLOG_INFO("%s: Datapath supports eventmask in conntrack action",
1287 dpif_name(backer
->dpif
));
1293 /* Tests whether 'backer''s datapath supports the OVS_ACTION_ATTR_CT_CLEAR
1296 check_ct_clear(struct dpif_backer
*backer
)
1298 struct odputil_keybuf keybuf
;
1299 uint8_t actbuf
[NL_A_FLAG_SIZE
];
1300 struct ofpbuf actions
;
1305 struct odp_flow_key_parms odp_parms
= {
1310 memset(&flow
, 0, sizeof flow
);
1311 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1312 odp_flow_key_from_flow(&odp_parms
, &key
);
1314 ofpbuf_use_stack(&actions
, &actbuf
, sizeof actbuf
);
1315 nl_msg_put_flag(&actions
, OVS_ACTION_ATTR_CT_CLEAR
);
1317 supported
= dpif_probe_feature(backer
->dpif
, "ct_clear", &key
,
1320 VLOG_INFO("%s: Datapath %s ct_clear action",
1321 dpif_name(backer
->dpif
), (supported
) ? "supports"
1322 : "does not support");
1326 /* Tests whether 'backer''s datapath supports the OVS_CT_ATTR_TIMEOUT
1327 * attribute in OVS_ACTION_ATTR_CT. */
1329 check_ct_timeout_policy(struct dpif_backer
*backer
)
1331 struct dpif_execute execute
;
1332 struct dp_packet packet
;
1333 struct ofpbuf actions
;
1334 struct flow flow
= {
1335 .dl_type
= CONSTANT_HTONS(ETH_TYPE_IP
),
1336 .nw_proto
= IPPROTO_UDP
,
1338 /* Use the broadcast address on the loopback address range 127/8 to
1339 * avoid hitting any real conntrack entries. We leave the UDP ports to
1340 * zeroes for the same purpose. */
1341 .nw_src
= CONSTANT_HTONL(0x7fffffff),
1342 .nw_dst
= CONSTANT_HTONL(0x7fffffff),
1347 /* Compose CT action with timeout policy attribute and check if datapath
1348 * can decode the message. */
1349 ofpbuf_init(&actions
, 64);
1350 ct_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CT
);
1351 /* Timeout policy has no effect without the commit flag, but currently the
1352 * datapath will accept a timeout policy even without commit. This is
1353 * useful as we do not want to persist the probe connection in the
1354 * conntrack table. */
1355 nl_msg_put_string(&actions
, OVS_CT_ATTR_TIMEOUT
, "ovs_test_tp");
1356 nl_msg_end_nested(&actions
, ct_start
);
1358 /* Compose a dummy UDP packet. */
1359 dp_packet_init(&packet
, 0);
1360 flow_compose(&packet
, &flow
, NULL
, 64);
1362 /* Execute the actions. On older datapaths this fails with EINVAL, on
1363 * newer datapaths it succeeds. */
1364 execute
.actions
= actions
.data
;
1365 execute
.actions_len
= actions
.size
;
1366 execute
.packet
= &packet
;
1367 execute
.flow
= &flow
;
1368 execute
.needs_help
= false;
1369 execute
.probe
= true;
1372 error
= dpif_execute(backer
->dpif
, &execute
);
1374 dp_packet_uninit(&packet
);
1375 ofpbuf_uninit(&actions
);
1378 VLOG_INFO("%s: Datapath does not support timeout policy in conntrack "
1379 "action", dpif_name(backer
->dpif
));
1381 VLOG_INFO("%s: Datapath supports timeout policy in conntrack action",
1382 dpif_name(backer
->dpif
));
1388 /* Tests whether 'backer''s datapath supports the
1389 * OVS_ACTION_ATTR_CHECK_PKT_LEN action. */
1391 check_check_pkt_len(struct dpif_backer
*backer
)
1393 struct odputil_keybuf keybuf
;
1394 struct ofpbuf actions
;
1399 struct odp_flow_key_parms odp_parms
= {
1404 memset(&flow
, 0, sizeof flow
);
1405 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1406 odp_flow_key_from_flow(&odp_parms
, &key
);
1407 ofpbuf_init(&actions
, 64);
1410 cpl_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CHECK_PKT_LEN
);
1411 nl_msg_put_u16(&actions
, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
, 100);
1413 /* Putting these actions without any data is good enough to check
1414 * if check_pkt_len is supported or not. */
1415 nl_msg_put_flag(&actions
, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
);
1416 nl_msg_put_flag(&actions
, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
);
1418 nl_msg_end_nested(&actions
, cpl_start
);
1420 supported
= dpif_probe_feature(backer
->dpif
, "check_pkt_len", &key
,
1422 ofpbuf_uninit(&actions
);
1423 VLOG_INFO("%s: Datapath %s check_pkt_len action",
1424 dpif_name(backer
->dpif
), supported
? "supports"
1425 : "does not support");
1429 /* Probe the highest dp_hash algorithm supported by the datapath. */
1431 check_max_dp_hash_alg(struct dpif_backer
*backer
)
1433 struct odputil_keybuf keybuf
;
1436 struct ovs_action_hash
*hash
;
1439 struct odp_flow_key_parms odp_parms
= {
1444 memset(&flow
, 0, sizeof flow
);
1445 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1446 odp_flow_key_from_flow(&odp_parms
, &key
);
1448 /* All datapaths support algortithm 0 (OVS_HASH_ALG_L4). */
1449 for (int alg
= 1; alg
< __OVS_HASH_MAX
; alg
++) {
1450 struct ofpbuf actions
;
1453 ofpbuf_init(&actions
, 300);
1454 hash
= nl_msg_put_unspec_uninit(&actions
,
1455 OVS_ACTION_ATTR_HASH
, sizeof *hash
);
1456 hash
->hash_basis
= 0;
1457 hash
->hash_alg
= alg
;
1458 ok
= dpif_probe_feature(backer
->dpif
, "Max dp_hash algorithm", &key
,
1460 ofpbuf_uninit(&actions
);
1468 VLOG_INFO("%s: Max dp_hash algorithm probed to be %d",
1469 dpif_name(backer
->dpif
), max_alg
);
1473 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE, ETHTYPE) \
1475 check_##NAME(struct dpif_backer *backer) \
1478 struct odputil_keybuf keybuf; \
1479 struct ofpbuf key; \
1481 struct odp_flow_key_parms odp_parms = { \
1488 memset(&flow, 0, sizeof flow); \
1489 flow.FIELD = VALUE; \
1490 flow.dl_type = htons(ETHTYPE); \
1492 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1493 odp_flow_key_from_flow(&odp_parms, &key); \
1494 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL, NULL); \
1497 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1499 VLOG_INFO("%s: Datapath does not support "#NAME, \
1500 dpif_name(backer->dpif)); \
1505 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1, \
1508 CHECK_FEATURE(ct_state
)
1509 CHECK_FEATURE(ct_zone
)
1510 CHECK_FEATURE(ct_mark
)
1511 CHECK_FEATURE__(ct_label
, ct_label
, ct_label
.u64
.lo
, 1, ETH_TYPE_IP
)
1512 CHECK_FEATURE__(ct_state_nat
, ct_state
, ct_state
, \
1513 CS_TRACKED
|CS_SRC_NAT
, ETH_TYPE_IP
)
1514 CHECK_FEATURE__(ct_orig_tuple
, ct_orig_tuple
, ct_nw_proto
, 1, ETH_TYPE_IP
)
1515 CHECK_FEATURE__(ct_orig_tuple6
, ct_orig_tuple6
, ct_nw_proto
, 1, ETH_TYPE_IPV6
)
1517 #undef CHECK_FEATURE
1518 #undef CHECK_FEATURE__
1521 check_support(struct dpif_backer
*backer
)
1524 backer
->rt_support
.odp
.recirc
= check_recirc(backer
);
1525 backer
->rt_support
.odp
.max_vlan_headers
= check_max_vlan_headers(backer
);
1526 backer
->rt_support
.odp
.max_mpls_depth
= check_max_mpls_depth(backer
);
1527 backer
->rt_support
.masked_set_action
= check_masked_set_action(backer
);
1528 backer
->rt_support
.trunc
= check_trunc_action(backer
);
1529 backer
->rt_support
.ufid
= check_ufid(backer
);
1530 backer
->rt_support
.tnl_push_pop
= dpif_supports_tnl_push_pop(backer
->dpif
);
1531 backer
->rt_support
.clone
= check_clone(backer
);
1532 backer
->rt_support
.sample_nesting
= check_max_sample_nesting(backer
);
1533 backer
->rt_support
.ct_eventmask
= check_ct_eventmask(backer
);
1534 backer
->rt_support
.ct_clear
= check_ct_clear(backer
);
1535 backer
->rt_support
.max_hash_alg
= check_max_dp_hash_alg(backer
);
1536 backer
->rt_support
.check_pkt_len
= check_check_pkt_len(backer
);
1537 backer
->rt_support
.ct_timeout
= check_ct_timeout_policy(backer
);
1540 backer
->rt_support
.odp
.ct_state
= check_ct_state(backer
);
1541 backer
->rt_support
.odp
.ct_zone
= check_ct_zone(backer
);
1542 backer
->rt_support
.odp
.ct_mark
= check_ct_mark(backer
);
1543 backer
->rt_support
.odp
.ct_label
= check_ct_label(backer
);
1545 backer
->rt_support
.odp
.ct_state_nat
= check_ct_state_nat(backer
);
1546 backer
->rt_support
.odp
.ct_orig_tuple
= check_ct_orig_tuple(backer
);
1547 backer
->rt_support
.odp
.ct_orig_tuple6
= check_ct_orig_tuple6(backer
);
1551 construct(struct ofproto
*ofproto_
)
1553 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1554 struct shash_node
*node
, *next
;
1557 /* Tunnel module can get used right after the udpif threads are running. */
1558 ofproto_tunnel_init();
1560 error
= open_dpif_backer(ofproto
->up
.type
, &ofproto
->backer
);
1565 uuid_generate(&ofproto
->uuid
);
1566 atomic_init(&ofproto
->tables_version
, OVS_VERSION_MIN
);
1567 ofproto
->netflow
= NULL
;
1568 ofproto
->sflow
= NULL
;
1569 ofproto
->ipfix
= NULL
;
1570 ofproto
->stp
= NULL
;
1571 ofproto
->rstp
= NULL
;
1572 ofproto
->dump_seq
= 0;
1573 hmap_init(&ofproto
->bundles
);
1574 ofproto
->ml
= mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME
);
1576 ofproto
->mbridge
= mbridge_create();
1577 ofproto
->has_bonded_bundles
= false;
1578 ofproto
->lacp_enabled
= false;
1579 ovs_mutex_init_adaptive(&ofproto
->stats_mutex
);
1581 guarded_list_init(&ofproto
->ams
);
1583 sset_init(&ofproto
->ports
);
1584 sset_init(&ofproto
->ghost_ports
);
1585 sset_init(&ofproto
->port_poll_set
);
1586 ofproto
->port_poll_errno
= 0;
1587 ofproto
->change_seq
= 0;
1588 ofproto
->ams_seq
= seq_create();
1589 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1592 SHASH_FOR_EACH_SAFE (node
, next
, &init_ofp_ports
) {
1593 struct iface_hint
*iface_hint
= node
->data
;
1595 if (!strcmp(iface_hint
->br_name
, ofproto
->up
.name
)) {
1596 /* Check if the datapath already has this port. */
1597 if (dpif_port_exists(ofproto
->backer
->dpif
, node
->name
)) {
1598 sset_add(&ofproto
->ports
, node
->name
);
1601 free(iface_hint
->br_name
);
1602 free(iface_hint
->br_type
);
1604 shash_delete(&init_ofp_ports
, node
);
1608 hmap_insert(&all_ofproto_dpifs_by_name
,
1609 &ofproto
->all_ofproto_dpifs_by_name_node
,
1610 hash_string(ofproto
->up
.name
, 0));
1611 hmap_insert(&all_ofproto_dpifs_by_uuid
,
1612 &ofproto
->all_ofproto_dpifs_by_uuid_node
,
1613 uuid_hash(&ofproto
->uuid
));
1614 memset(&ofproto
->stats
, 0, sizeof ofproto
->stats
);
1616 ofproto_init_tables(ofproto_
, N_TABLES
);
1617 error
= add_internal_flows(ofproto
);
1619 ofproto
->up
.tables
[TBL_INTERNAL
].flags
= OFTABLE_HIDDEN
| OFTABLE_READONLY
;
1625 add_internal_miss_flow(struct ofproto_dpif
*ofproto
, int id
,
1626 const struct ofpbuf
*ofpacts
, struct rule_dpif
**rulep
)
1632 match_init_catchall(&match
);
1633 match_set_reg(&match
, 0, id
);
1635 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 0, 0, ofpacts
,
1637 *rulep
= error
? NULL
: rule_dpif_cast(rule
);
1643 add_internal_flows(struct ofproto_dpif
*ofproto
)
1645 struct ofpact_controller
*controller
;
1646 uint64_t ofpacts_stub
[128 / 8];
1647 struct ofpbuf ofpacts
;
1648 struct rule
*unused_rulep OVS_UNUSED
;
1653 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1656 controller
= ofpact_put_CONTROLLER(&ofpacts
);
1657 controller
->max_len
= UINT16_MAX
;
1658 controller
->controller_id
= 0;
1659 controller
->reason
= OFPR_IMPLICIT_MISS
;
1660 controller
->meter_id
= NX_CTLR_NO_METER
;
1661 ofpact_finish_CONTROLLER(&ofpacts
, &controller
);
1663 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1664 &ofproto
->miss_rule
);
1669 ofpbuf_clear(&ofpacts
);
1670 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1671 &ofproto
->no_packet_in_rule
);
1676 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1677 &ofproto
->drop_frags_rule
);
1682 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1683 * zero when reaching this rule.
1685 * (priority=2), recirc_id=0, actions=drop
1687 ofpbuf_clear(&ofpacts
);
1688 match_init_catchall(&match
);
1689 match_set_recirc_id(&match
, 0);
1690 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 2, 0, &ofpacts
,
1696 destruct(struct ofproto
*ofproto_
, bool del
)
1698 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1699 struct ofproto_async_msg
*am
;
1700 struct rule_dpif
*rule
;
1701 struct oftable
*table
;
1702 struct ovs_list ams
;
1704 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1706 xlate_remove_ofproto(ofproto
);
1709 /* Ensure that the upcall processing threads have no remaining references
1710 * to the ofproto or anything in it. */
1711 udpif_synchronize(ofproto
->backer
->udpif
);
1713 hmap_remove(&all_ofproto_dpifs_by_name
,
1714 &ofproto
->all_ofproto_dpifs_by_name_node
);
1715 hmap_remove(&all_ofproto_dpifs_by_uuid
,
1716 &ofproto
->all_ofproto_dpifs_by_uuid_node
);
1718 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1719 CLS_FOR_EACH (rule
, up
.cr
, &table
->cls
) {
1720 ofproto_rule_delete(&ofproto
->up
, &rule
->up
);
1723 ofproto_group_delete_all(&ofproto
->up
);
1725 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1726 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1727 ofproto_async_msg_free(am
);
1729 guarded_list_destroy(&ofproto
->ams
);
1731 recirc_free_ofproto(ofproto
, ofproto
->up
.name
);
1733 mbridge_unref(ofproto
->mbridge
);
1735 netflow_unref(ofproto
->netflow
);
1736 dpif_sflow_unref(ofproto
->sflow
);
1737 dpif_ipfix_unref(ofproto
->ipfix
);
1738 hmap_destroy(&ofproto
->bundles
);
1739 mac_learning_unref(ofproto
->ml
);
1740 mcast_snooping_unref(ofproto
->ms
);
1741 stp_unref(ofproto
->stp
);
1742 rstp_unref(ofproto
->rstp
);
1744 sset_destroy(&ofproto
->ports
);
1745 sset_destroy(&ofproto
->ghost_ports
);
1746 sset_destroy(&ofproto
->port_poll_set
);
1748 ovs_mutex_destroy(&ofproto
->stats_mutex
);
1750 seq_destroy(ofproto
->ams_seq
);
1752 close_dpif_backer(ofproto
->backer
, del
);
1756 run(struct ofproto
*ofproto_
)
1758 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1759 uint64_t new_seq
, new_dump_seq
;
1761 if (mbridge_need_revalidate(ofproto
->mbridge
)) {
1762 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1763 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1764 mac_learning_flush(ofproto
->ml
);
1765 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1766 mcast_snooping_mdb_flush(ofproto
->ms
);
1769 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1770 * flow restore. Even though nothing is processed during flow restore,
1771 * all queued 'ams' will be handled immediately when flow restore
1773 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1775 /* Do not perform any periodic activity required by 'ofproto' while
1776 * waiting for flow restore to complete. */
1777 if (!ofproto_get_flow_restore_wait()) {
1778 struct ofproto_async_msg
*am
;
1779 struct ovs_list ams
;
1781 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1782 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1783 connmgr_send_async_msg(ofproto
->up
.connmgr
, am
);
1784 ofproto_async_msg_free(am
);
1788 if (ofproto
->netflow
) {
1789 netflow_run(ofproto
->netflow
);
1791 if (ofproto
->sflow
) {
1792 dpif_sflow_run(ofproto
->sflow
);
1794 if (ofproto
->ipfix
) {
1795 dpif_ipfix_run(ofproto
->ipfix
);
1798 new_seq
= seq_read(connectivity_seq_get());
1799 if (ofproto
->change_seq
!= new_seq
) {
1800 struct ofport_dpif
*ofport
;
1802 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1806 ofproto
->change_seq
= new_seq
;
1808 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1809 struct ofbundle
*bundle
;
1811 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1818 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1819 if (mac_learning_run(ofproto
->ml
)) {
1820 ofproto
->backer
->need_revalidate
= REV_MAC_LEARNING
;
1822 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1824 if (mcast_snooping_run(ofproto
->ms
)) {
1825 ofproto
->backer
->need_revalidate
= REV_MCAST_SNOOPING
;
1828 new_dump_seq
= seq_read(udpif_dump_seq(ofproto
->backer
->udpif
));
1829 if (ofproto
->dump_seq
!= new_dump_seq
) {
1830 struct rule
*rule
, *next_rule
;
1831 long long now
= time_msec();
1833 /* We know stats are relatively fresh, so now is a good time to do some
1835 ofproto
->dump_seq
= new_dump_seq
;
1837 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1839 ovs_mutex_lock(&ofproto_mutex
);
1840 LIST_FOR_EACH_SAFE (rule
, next_rule
, expirable
,
1841 &ofproto
->up
.expirable
) {
1842 rule_expire(rule_dpif_cast(rule
), now
);
1844 ovs_mutex_unlock(&ofproto_mutex
);
1846 /* All outstanding data in existing flows has been accounted, so it's a
1847 * good time to do bond rebalancing. */
1848 if (ofproto
->has_bonded_bundles
) {
1849 struct ofbundle
*bundle
;
1851 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1853 bond_rebalance(bundle
->bond
);
1862 ofproto_dpif_wait(struct ofproto
*ofproto_
)
1864 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1866 if (ofproto_get_flow_restore_wait()) {
1870 if (ofproto
->sflow
) {
1871 dpif_sflow_wait(ofproto
->sflow
);
1873 if (ofproto
->ipfix
) {
1874 dpif_ipfix_wait(ofproto
->ipfix
);
1876 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1877 struct ofbundle
*bundle
;
1879 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1880 bundle_wait(bundle
);
1883 if (ofproto
->netflow
) {
1884 netflow_wait(ofproto
->netflow
);
1886 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
1887 mac_learning_wait(ofproto
->ml
);
1888 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1889 mcast_snooping_wait(ofproto
->ms
);
1891 if (ofproto
->backer
->need_revalidate
) {
1892 poll_immediate_wake();
1895 seq_wait(udpif_dump_seq(ofproto
->backer
->udpif
), ofproto
->dump_seq
);
1896 seq_wait(ofproto
->ams_seq
, ofproto
->ams_seqno
);
1900 type_get_memory_usage(const char *type
, struct simap
*usage
)
1902 struct dpif_backer
*backer
;
1904 backer
= shash_find_data(&all_dpif_backers
, type
);
1906 udpif_get_memory_usage(backer
->udpif
, usage
);
1911 flush(struct ofproto
*ofproto_
)
1913 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1914 struct dpif_backer
*backer
= ofproto
->backer
;
1917 udpif_flush(backer
->udpif
);
1922 query_tables(struct ofproto
*ofproto
,
1923 struct ofputil_table_features
*features OVS_UNUSED
,
1924 struct ofputil_table_stats
*stats
)
1929 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
1930 unsigned long missed
, matched
;
1932 atomic_read_relaxed(&ofproto
->tables
[i
].n_matched
, &matched
);
1933 atomic_read_relaxed(&ofproto
->tables
[i
].n_missed
, &missed
);
1935 stats
[i
].matched_count
= matched
;
1936 stats
[i
].lookup_count
= matched
+ missed
;
1942 set_tables_version(struct ofproto
*ofproto_
, ovs_version_t version
)
1944 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1946 /* Use memory_order_release to signify that any prior memory accesses can
1947 * not be reordered to happen after this atomic store. This makes sure the
1948 * new version is properly set up when the readers can read this 'version'
1950 atomic_store_explicit(&ofproto
->tables_version
, version
,
1951 memory_order_release
);
1952 /* 'need_revalidate' can be reordered to happen before the atomic_store
1953 * above, but it does not matter as this variable is not accessed by other
1955 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
1958 static struct ofport
*
1961 struct ofport_dpif
*port
= xzalloc(sizeof *port
);
1966 port_dealloc(struct ofport
*port_
)
1968 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1973 port_construct(struct ofport
*port_
)
1975 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1976 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1977 const struct netdev
*netdev
= port
->up
.netdev
;
1978 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1979 const char *dp_port_name
;
1980 struct dpif_port dpif_port
;
1983 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1984 port
->bundle
= NULL
;
1988 port
->stp_port
= NULL
;
1989 port
->stp_state
= STP_DISABLED
;
1990 port
->rstp_port
= NULL
;
1991 port
->rstp_state
= RSTP_DISABLED
;
1992 port
->is_tunnel
= false;
1996 port
->carrier_seq
= netdev_get_carrier_resets(netdev
);
1998 if (netdev_vport_is_patch(netdev
)) {
1999 /* By bailing out here, we don't submit the port to the sFlow module
2000 * to be considered for counter polling export. This is correct
2001 * because the patch port represents an interface that sFlow considers
2002 * to be "internal" to the switch as a whole, and therefore not a
2003 * candidate for counter polling. */
2004 port
->odp_port
= ODPP_NONE
;
2005 ofport_update_peer(port
);
2009 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
2010 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
, dp_port_name
,
2016 port
->odp_port
= dpif_port
.port_no
;
2018 if (netdev_get_tunnel_config(netdev
)) {
2019 atomic_count_inc(&ofproto
->backer
->tnl_count
);
2020 error
= tnl_port_add(port
, port
->up
.netdev
, port
->odp_port
,
2021 ovs_native_tunneling_is_on(ofproto
), dp_port_name
);
2023 atomic_count_dec(&ofproto
->backer
->tnl_count
);
2024 dpif_port_destroy(&dpif_port
);
2028 port
->is_tunnel
= true;
2030 /* Sanity-check that a mapping doesn't already exist. This
2031 * shouldn't happen for non-tunnel ports. */
2032 if (odp_port_to_ofp_port(ofproto
, port
->odp_port
) != OFPP_NONE
) {
2033 VLOG_ERR("port %s already has an OpenFlow port number",
2035 dpif_port_destroy(&dpif_port
);
2039 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
2040 hmap_insert(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
,
2041 hash_odp_port(port
->odp_port
));
2042 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
2044 dpif_port_destroy(&dpif_port
);
2046 if (ofproto
->sflow
) {
2047 dpif_sflow_add_port(ofproto
->sflow
, port_
, port
->odp_port
);
2049 if (ofproto
->ipfix
) {
2050 dpif_ipfix_add_port(ofproto
->ipfix
, port_
, port
->odp_port
);
2057 port_destruct(struct ofport
*port_
, bool del
)
2059 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
2060 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
2061 const char *devname
= netdev_get_name(port
->up
.netdev
);
2062 const char *netdev_type
= netdev_get_type(port
->up
.netdev
);
2063 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
2064 const char *dp_port_name
;
2066 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2068 xlate_ofport_remove(port
);
2071 if (!del
&& strcmp(netdev_type
,
2072 ofproto_port_open_type(port
->up
.ofproto
, "internal"))) {
2073 /* Check if datapath requires removal of attached ports. Avoid
2074 * removal of 'internal' ports to preserve user ip/route settings. */
2075 del
= dpif_cleanup_required(ofproto
->backer
->dpif
);
2078 dp_port_name
= netdev_vport_get_dpif_port(port
->up
.netdev
, namebuf
,
2080 if (del
&& dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
2081 /* The underlying device is still there, so delete it. This
2082 * happens when the ofproto is being destroyed, since the caller
2083 * assumes that removal of attached ports will happen as part of
2085 if (!port
->is_tunnel
) {
2086 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
, false);
2089 /* The underlying device is already deleted (e.g. tunctl -d).
2090 * Calling dpif_port_remove to do local cleanup for the netdev */
2091 if (!port
->is_tunnel
) {
2092 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
, true);
2097 port
->peer
->peer
= NULL
;
2101 if (port
->odp_port
!= ODPP_NONE
&& !port
->is_tunnel
) {
2102 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
2103 hmap_remove(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
);
2104 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
2107 if (port
->is_tunnel
) {
2108 atomic_count_dec(&ofproto
->backer
->tnl_count
);
2111 tnl_port_del(port
, port
->odp_port
);
2112 sset_find_and_delete(&ofproto
->ports
, devname
);
2113 sset_find_and_delete(&ofproto
->ghost_ports
, devname
);
2114 bundle_remove(port_
);
2115 set_cfm(port_
, NULL
);
2116 set_bfd(port_
, NULL
);
2117 set_lldp(port_
, NULL
);
2118 if (port
->stp_port
) {
2119 stp_port_disable(port
->stp_port
);
2121 set_rstp_port(port_
, NULL
);
2122 if (ofproto
->sflow
) {
2123 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
2125 if (ofproto
->ipfix
) {
2126 dpif_ipfix_del_port(ofproto
->ipfix
, port
->odp_port
);
2133 port_modified(struct ofport
*port_
)
2135 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
2136 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
2137 const char *dp_port_name
;
2138 struct netdev
*netdev
= port
->up
.netdev
;
2140 if (port
->bundle
&& port
->bundle
->bond
) {
2141 bond_slave_set_netdev(port
->bundle
->bond
, port
, netdev
);
2145 cfm_set_netdev(port
->cfm
, netdev
);
2149 bfd_set_netdev(port
->bfd
, netdev
);
2152 ofproto_dpif_monitor_port_update(port
, port
->bfd
, port
->cfm
,
2153 port
->lldp
, &port
->up
.pp
.hw_addr
);
2155 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
2157 if (port
->is_tunnel
) {
2158 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
2160 if (tnl_port_reconfigure(port
, netdev
, port
->odp_port
, port
->odp_port
,
2161 ovs_native_tunneling_is_on(ofproto
),
2163 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2167 ofport_update_peer(port
);
2171 port_reconfigured(struct ofport
*port_
, enum ofputil_port_config old_config
)
2173 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
2174 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
2175 enum ofputil_port_config changed
= old_config
^ port
->up
.pp
.config
;
2177 if (changed
& (OFPUTIL_PC_NO_RECV
| OFPUTIL_PC_NO_RECV_STP
|
2178 OFPUTIL_PC_NO_FWD
| OFPUTIL_PC_NO_FLOOD
|
2179 OFPUTIL_PC_NO_PACKET_IN
)) {
2180 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2182 if (changed
& OFPUTIL_PC_NO_FLOOD
&& port
->bundle
) {
2183 bundle_update(port
->bundle
);
2190 set_sflow(struct ofproto
*ofproto_
,
2191 const struct ofproto_sflow_options
*sflow_options
)
2193 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2194 struct dpif_sflow
*ds
= ofproto
->sflow
;
2196 if (sflow_options
) {
2197 uint32_t old_probability
= ds
? dpif_sflow_get_probability(ds
) : 0;
2199 struct ofport_dpif
*ofport
;
2201 ds
= ofproto
->sflow
= dpif_sflow_create();
2202 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2203 dpif_sflow_add_port(ds
, &ofport
->up
, ofport
->odp_port
);
2206 dpif_sflow_set_options(ds
, sflow_options
);
2207 if (dpif_sflow_get_probability(ds
) != old_probability
) {
2208 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2212 dpif_sflow_unref(ds
);
2213 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2214 ofproto
->sflow
= NULL
;
2222 struct ofproto
*ofproto_
,
2223 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
2224 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
2225 size_t n_flow_exporters_options
)
2227 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2228 struct dpif_ipfix
*di
= ofproto
->ipfix
;
2229 bool has_options
= bridge_exporter_options
|| flow_exporters_options
;
2230 bool new_di
= false;
2232 if (has_options
&& !di
) {
2233 di
= ofproto
->ipfix
= dpif_ipfix_create();
2238 /* Call set_options in any case to cleanly flush the flow
2239 * caches in the last exporters that are to be destroyed. */
2240 dpif_ipfix_set_options(
2241 di
, bridge_exporter_options
, flow_exporters_options
,
2242 n_flow_exporters_options
);
2244 /* Add ports only when a new ipfix created */
2245 if (new_di
== true) {
2246 struct ofport_dpif
*ofport
;
2247 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2248 dpif_ipfix_add_port(di
, &ofport
->up
, ofport
->odp_port
);
2253 dpif_ipfix_unref(di
);
2254 ofproto
->ipfix
= NULL
;
2262 get_ipfix_stats(const struct ofproto
*ofproto_
,
2264 struct ovs_list
*replies
)
2266 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2267 struct dpif_ipfix
*di
= ofproto
->ipfix
;
2270 return OFPERR_NXST_NOT_CONFIGURED
;
2273 return dpif_ipfix_get_stats(di
, bridge_ipfix
, replies
);
2277 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
2279 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2280 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2281 struct cfm
*old
= ofport
->cfm
;
2286 ofport
->cfm
= cfm_create(ofport
->up
.netdev
);
2289 if (cfm_configure(ofport
->cfm
, s
)) {
2296 cfm_unref(ofport
->cfm
);
2299 if (ofport
->cfm
!= old
) {
2300 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2302 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2303 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2308 cfm_status_changed(struct ofport
*ofport_
)
2310 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2312 return ofport
->cfm
? cfm_check_status_change(ofport
->cfm
) : true;
2316 get_cfm_status(const struct ofport
*ofport_
,
2317 struct cfm_status
*status
)
2319 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2323 cfm_get_status(ofport
->cfm
, status
);
2332 set_bfd(struct ofport
*ofport_
, const struct smap
*cfg
)
2334 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
2335 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2339 ofport
->bfd
= bfd_configure(old
, netdev_get_name(ofport
->up
.netdev
),
2340 cfg
, ofport
->up
.netdev
);
2341 if (ofport
->bfd
!= old
) {
2342 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2344 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2345 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2350 bfd_status_changed(struct ofport
*ofport_
)
2352 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2354 return ofport
->bfd
? bfd_check_status_change(ofport
->bfd
) : true;
2358 get_bfd_status(struct ofport
*ofport_
, struct smap
*smap
)
2360 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2364 bfd_get_status(ofport
->bfd
, smap
);
2373 set_lldp(struct ofport
*ofport_
,
2374 const struct smap
*cfg
)
2376 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2377 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2381 if (!ofport
->lldp
) {
2382 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2383 ofport
->lldp
= lldp_create(ofport
->up
.netdev
, ofport_
->mtu
, cfg
);
2386 if (!lldp_configure(ofport
->lldp
, cfg
)) {
2387 lldp_unref(ofport
->lldp
);
2388 ofport
->lldp
= NULL
;
2391 } else if (ofport
->lldp
) {
2392 lldp_unref(ofport
->lldp
);
2393 ofport
->lldp
= NULL
;
2394 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2397 ofproto_dpif_monitor_port_update(ofport
,
2401 &ofport
->up
.pp
.hw_addr
);
2406 get_lldp_status(const struct ofport
*ofport_
,
2407 struct lldp_status
*status OVS_UNUSED
)
2409 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2411 return ofport
->lldp
? true : false;
2415 set_aa(struct ofproto
*ofproto OVS_UNUSED
,
2416 const struct aa_settings
*s
)
2418 return aa_configure(s
);
2422 aa_mapping_set(struct ofproto
*ofproto_ OVS_UNUSED
, void *aux
,
2423 const struct aa_mapping_settings
*s
)
2425 return aa_mapping_register(aux
, s
);
2429 aa_mapping_unset(struct ofproto
*ofproto OVS_UNUSED
, void *aux
)
2431 return aa_mapping_unregister(aux
);
2435 aa_vlan_get_queued(struct ofproto
*ofproto OVS_UNUSED
, struct ovs_list
*list
)
2437 return aa_get_vlan_queued(list
);
2441 aa_vlan_get_queue_size(struct ofproto
*ofproto OVS_UNUSED
)
2443 return aa_get_vlan_queue_size();
2447 /* Spanning Tree. */
2449 /* Called while rstp_mutex is held. */
2451 rstp_send_bpdu_cb(struct dp_packet
*pkt
, void *ofport_
, void *ofproto_
)
2453 struct ofproto_dpif
*ofproto
= ofproto_
;
2454 struct ofport_dpif
*ofport
= ofport_
;
2455 struct eth_header
*eth
= dp_packet_eth(pkt
);
2457 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2458 if (eth_addr_is_zero(eth
->eth_src
)) {
2459 VLOG_WARN_RL(&rl
, "%s port %d: cannot send RSTP BPDU on a port which "
2460 "does not have a configured source MAC address.",
2461 ofproto
->up
.name
, ofp_to_u16(ofport
->up
.ofp_port
));
2463 ofproto_dpif_send_packet(ofport
, false, pkt
);
2465 dp_packet_delete(pkt
);
2469 send_bpdu_cb(struct dp_packet
*pkt
, int port_num
, void *ofproto_
)
2471 struct ofproto_dpif
*ofproto
= ofproto_
;
2472 struct stp_port
*sp
= stp_get_port(ofproto
->stp
, port_num
);
2473 struct ofport_dpif
*ofport
;
2475 ofport
= stp_port_get_aux(sp
);
2477 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
2478 ofproto
->up
.name
, port_num
);
2480 struct eth_header
*eth
= dp_packet_eth(pkt
);
2482 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2483 if (eth_addr_is_zero(eth
->eth_src
)) {
2484 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d "
2485 "with unknown MAC", ofproto
->up
.name
, port_num
);
2487 ofproto_dpif_send_packet(ofport
, false, pkt
);
2490 dp_packet_delete(pkt
);
2493 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2495 set_rstp(struct ofproto
*ofproto_
, const struct ofproto_rstp_settings
*s
)
2497 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2499 /* Only revalidate flows if the configuration changed. */
2500 if (!s
!= !ofproto
->rstp
) {
2501 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2505 if (!ofproto
->rstp
) {
2506 ofproto
->rstp
= rstp_create(ofproto_
->name
, s
->address
,
2507 rstp_send_bpdu_cb
, ofproto
);
2508 ofproto
->rstp_last_tick
= time_msec();
2510 rstp_set_bridge_address(ofproto
->rstp
, s
->address
);
2511 rstp_set_bridge_priority(ofproto
->rstp
, s
->priority
);
2512 rstp_set_bridge_ageing_time(ofproto
->rstp
, s
->ageing_time
);
2513 rstp_set_bridge_force_protocol_version(ofproto
->rstp
,
2514 s
->force_protocol_version
);
2515 rstp_set_bridge_max_age(ofproto
->rstp
, s
->bridge_max_age
);
2516 rstp_set_bridge_forward_delay(ofproto
->rstp
, s
->bridge_forward_delay
);
2517 rstp_set_bridge_transmit_hold_count(ofproto
->rstp
,
2518 s
->transmit_hold_count
);
2520 struct ofport
*ofport
;
2521 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2522 set_rstp_port(ofport
, NULL
);
2524 rstp_unref(ofproto
->rstp
);
2525 ofproto
->rstp
= NULL
;
2530 get_rstp_status(struct ofproto
*ofproto_
, struct ofproto_rstp_status
*s
)
2532 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2534 if (ofproto
->rstp
) {
2536 s
->root_id
= rstp_get_root_id(ofproto
->rstp
);
2537 s
->bridge_id
= rstp_get_bridge_id(ofproto
->rstp
);
2538 s
->designated_id
= rstp_get_designated_id(ofproto
->rstp
);
2539 s
->root_path_cost
= rstp_get_root_path_cost(ofproto
->rstp
);
2540 s
->designated_port_id
= rstp_get_designated_port_id(ofproto
->rstp
);
2541 s
->bridge_port_id
= rstp_get_bridge_port_id(ofproto
->rstp
);
2548 update_rstp_port_state(struct ofport_dpif
*ofport
)
2550 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2551 enum rstp_state state
;
2553 /* Figure out new state. */
2554 state
= ofport
->rstp_port
? rstp_port_get_state(ofport
->rstp_port
)
2558 if (ofport
->rstp_state
!= state
) {
2559 enum ofputil_port_state of_state
;
2562 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2563 netdev_get_name(ofport
->up
.netdev
),
2564 rstp_state_name(ofport
->rstp_state
),
2565 rstp_state_name(state
));
2567 if (rstp_learn_in_state(ofport
->rstp_state
)
2568 != rstp_learn_in_state(state
)) {
2569 /* XXX: Learning action flows should also be flushed. */
2570 if (ofport
->bundle
) {
2571 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2572 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2573 bundle_flush_macs(ofport
->bundle
, false);
2577 fwd_change
= rstp_forward_in_state(ofport
->rstp_state
)
2578 != rstp_forward_in_state(state
);
2580 ofproto
->backer
->need_revalidate
= REV_RSTP
;
2581 ofport
->rstp_state
= state
;
2583 if (fwd_change
&& ofport
->bundle
) {
2584 bundle_update(ofport
->bundle
);
2587 /* Update the RSTP state bits in the OpenFlow port description. */
2588 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2589 of_state
|= (state
== RSTP_LEARNING
? OFPUTIL_PS_STP_LEARN
2590 : state
== RSTP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2591 : state
== RSTP_DISCARDING
? OFPUTIL_PS_STP_LISTEN
2593 ofproto_port_set_state(&ofport
->up
, of_state
);
2598 rstp_run(struct ofproto_dpif
*ofproto
)
2600 if (ofproto
->rstp
) {
2601 long long int now
= time_msec();
2602 long long int elapsed
= now
- ofproto
->rstp_last_tick
;
2603 struct rstp_port
*rp
;
2604 struct ofport_dpif
*ofport
;
2606 /* Every second, decrease the values of the timers. */
2607 if (elapsed
>= 1000) {
2608 rstp_tick_timers(ofproto
->rstp
);
2609 ofproto
->rstp_last_tick
= now
;
2612 while ((ofport
= rstp_get_next_changed_port_aux(ofproto
->rstp
, &rp
))) {
2613 update_rstp_port_state(ofport
);
2617 /* FIXME: This check should be done on-event (i.e., when setting
2618 * p->fdb_flush) and not periodically.
2620 while ((ofport
= rstp_check_and_reset_fdb_flush(ofproto
->rstp
, &rp
))) {
2621 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2622 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2623 bundle_flush_macs(ofport
->bundle
, false);
2627 if (rstp_shift_root_learned_address(ofproto
->rstp
)) {
2628 struct ofport_dpif
*old_root_aux
=
2629 (struct ofport_dpif
*)rstp_get_old_root_aux(ofproto
->rstp
);
2630 struct ofport_dpif
*new_root_aux
=
2631 (struct ofport_dpif
*)rstp_get_new_root_aux(ofproto
->rstp
);
2632 if (old_root_aux
!= NULL
&& new_root_aux
!= NULL
) {
2633 bundle_move(old_root_aux
->bundle
, new_root_aux
->bundle
);
2634 rstp_reset_root_changed(ofproto
->rstp
);
2640 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2642 set_stp(struct ofproto
*ofproto_
, const struct ofproto_stp_settings
*s
)
2644 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2646 /* Only revalidate flows if the configuration changed. */
2647 if (!s
!= !ofproto
->stp
) {
2648 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2652 if (!ofproto
->stp
) {
2653 ofproto
->stp
= stp_create(ofproto_
->name
, s
->system_id
,
2654 send_bpdu_cb
, ofproto
);
2655 ofproto
->stp_last_tick
= time_msec();
2658 stp_set_bridge_id(ofproto
->stp
, s
->system_id
);
2659 stp_set_bridge_priority(ofproto
->stp
, s
->priority
);
2660 stp_set_hello_time(ofproto
->stp
, s
->hello_time
);
2661 stp_set_max_age(ofproto
->stp
, s
->max_age
);
2662 stp_set_forward_delay(ofproto
->stp
, s
->fwd_delay
);
2664 struct ofport
*ofport
;
2666 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2667 set_stp_port(ofport
, NULL
);
2670 stp_unref(ofproto
->stp
);
2671 ofproto
->stp
= NULL
;
2678 get_stp_status(struct ofproto
*ofproto_
, struct ofproto_stp_status
*s
)
2680 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2684 s
->bridge_id
= stp_get_bridge_id(ofproto
->stp
);
2685 s
->designated_root
= stp_get_designated_root(ofproto
->stp
);
2686 s
->root_path_cost
= stp_get_root_path_cost(ofproto
->stp
);
2695 update_stp_port_state(struct ofport_dpif
*ofport
)
2697 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2698 enum stp_state state
;
2700 /* Figure out new state. */
2701 state
= ofport
->stp_port
? stp_port_get_state(ofport
->stp_port
)
2705 if (ofport
->stp_state
!= state
) {
2706 enum ofputil_port_state of_state
;
2709 VLOG_DBG("port %s: STP state changed from %s to %s",
2710 netdev_get_name(ofport
->up
.netdev
),
2711 stp_state_name(ofport
->stp_state
),
2712 stp_state_name(state
));
2713 if (stp_learn_in_state(ofport
->stp_state
)
2714 != stp_learn_in_state(state
)) {
2715 /* xxx Learning action flows should also be flushed. */
2716 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2717 mac_learning_flush(ofproto
->ml
);
2718 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2719 mcast_snooping_mdb_flush(ofproto
->ms
);
2721 fwd_change
= stp_forward_in_state(ofport
->stp_state
)
2722 != stp_forward_in_state(state
);
2724 ofproto
->backer
->need_revalidate
= REV_STP
;
2725 ofport
->stp_state
= state
;
2726 ofport
->stp_state_entered
= time_msec();
2728 if (fwd_change
&& ofport
->bundle
) {
2729 bundle_update(ofport
->bundle
);
2732 /* Update the STP state bits in the OpenFlow port description. */
2733 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2734 of_state
|= (state
== STP_LISTENING
? OFPUTIL_PS_STP_LISTEN
2735 : state
== STP_LEARNING
? OFPUTIL_PS_STP_LEARN
2736 : state
== STP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2737 : state
== STP_BLOCKING
? OFPUTIL_PS_STP_BLOCK
2739 ofproto_port_set_state(&ofport
->up
, of_state
);
2744 stp_check_and_update_link_state(struct ofproto_dpif
*ofproto
)
2746 struct ofport_dpif
*ofport
;
2748 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2749 bool up
= netdev_get_carrier(ofport
->up
.netdev
);
2751 if (ofport
->stp_port
&&
2752 up
!= (stp_port_get_state(ofport
->stp_port
) != STP_DISABLED
)) {
2754 VLOG_DBG("bridge %s, port %s is %s, %s it.",
2755 ofproto
->up
.name
, netdev_get_name(ofport
->up
.netdev
),
2757 up
? "enabling" : "disabling");
2760 stp_port_enable(ofport
->stp_port
);
2761 stp_port_set_aux(ofport
->stp_port
, ofport
);
2763 stp_port_disable(ofport
->stp_port
);
2766 update_stp_port_state(ofport
);
2771 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2772 * caller is responsible for assigning STP port numbers and ensuring
2773 * there are no duplicates. */
2775 set_stp_port(struct ofport
*ofport_
,
2776 const struct ofproto_port_stp_settings
*s
)
2778 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2779 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2780 struct stp_port
*sp
= ofport
->stp_port
;
2782 if (!s
|| !s
->enable
) {
2784 ofport
->stp_port
= NULL
;
2785 stp_port_disable(sp
);
2786 update_stp_port_state(ofport
);
2789 } else if (sp
&& stp_port_no(sp
) != s
->port_num
2790 && ofport
== stp_port_get_aux(sp
)) {
2791 /* The port-id changed, so disable the old one if it's not
2792 * already in use by another port. */
2793 stp_port_disable(sp
);
2796 sp
= ofport
->stp_port
= stp_get_port(ofproto
->stp
, s
->port_num
);
2798 /* Set name before enabling the port so that debugging messages can print
2800 stp_port_set_name(sp
, netdev_get_name(ofport
->up
.netdev
));
2802 if (netdev_get_carrier(ofport_
->netdev
)) {
2803 stp_port_enable(sp
);
2805 stp_port_disable(sp
);
2808 stp_port_set_aux(sp
, ofport
);
2809 stp_port_set_priority(sp
, s
->priority
);
2810 stp_port_set_path_cost(sp
, s
->path_cost
);
2812 update_stp_port_state(ofport
);
2818 get_stp_port_status(struct ofport
*ofport_
,
2819 struct ofproto_port_stp_status
*s
)
2821 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2822 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2823 struct stp_port
*sp
= ofport
->stp_port
;
2825 if (!ofproto
->stp
|| !sp
) {
2831 stp_port_get_status(sp
, &s
->port_id
, &s
->state
, &s
->role
);
2832 s
->sec_in_state
= (time_msec() - ofport
->stp_state_entered
) / 1000;
2838 get_stp_port_stats(struct ofport
*ofport_
,
2839 struct ofproto_port_stp_stats
*s
)
2841 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2842 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2843 struct stp_port
*sp
= ofport
->stp_port
;
2845 if (!ofproto
->stp
|| !sp
) {
2851 stp_port_get_counts(sp
, &s
->tx_count
, &s
->rx_count
, &s
->error_count
);
2857 stp_run(struct ofproto_dpif
*ofproto
)
2860 long long int now
= time_msec();
2861 long long int elapsed
= now
- ofproto
->stp_last_tick
;
2862 struct stp_port
*sp
;
2865 stp_tick(ofproto
->stp
, MIN(INT_MAX
, elapsed
));
2866 ofproto
->stp_last_tick
= now
;
2869 stp_check_and_update_link_state(ofproto
);
2871 while (stp_get_changed_port(ofproto
->stp
, &sp
)) {
2872 struct ofport_dpif
*ofport
= stp_port_get_aux(sp
);
2875 update_stp_port_state(ofport
);
2879 if (stp_check_and_reset_fdb_flush(ofproto
->stp
)) {
2880 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2881 mac_learning_flush(ofproto
->ml
);
2882 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2883 mcast_snooping_mdb_flush(ofproto
->ms
);
2889 stp_wait(struct ofproto_dpif
*ofproto
)
2892 poll_timer_wait(1000);
2896 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2897 * caller is responsible for assigning RSTP port numbers and ensuring
2898 * there are no duplicates. */
2900 set_rstp_port(struct ofport
*ofport_
,
2901 const struct ofproto_port_rstp_settings
*s
)
2903 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2904 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2905 struct rstp_port
*rp
= ofport
->rstp_port
;
2907 if (!s
|| !s
->enable
) {
2909 rstp_port_set_aux(rp
, NULL
);
2910 rstp_port_set_state(rp
, RSTP_DISABLED
);
2911 rstp_port_set_mac_operational(rp
, false);
2912 ofport
->rstp_port
= NULL
;
2913 rstp_port_unref(rp
);
2914 update_rstp_port_state(ofport
);
2919 /* Check if need to add a new port. */
2921 rp
= ofport
->rstp_port
= rstp_add_port(ofproto
->rstp
);
2924 rstp_port_set(rp
, s
->port_num
, s
->priority
, s
->path_cost
,
2925 s
->admin_edge_port
, s
->auto_edge
,
2926 s
->admin_p2p_mac_state
, s
->admin_port_state
, s
->mcheck
,
2927 ofport
, netdev_get_name(ofport
->up
.netdev
));
2928 update_rstp_port_state(ofport
);
2929 /* Synchronize operational status. */
2930 rstp_port_set_mac_operational(rp
, ofport
->up
.may_enable
);
2934 get_rstp_port_status(struct ofport
*ofport_
,
2935 struct ofproto_port_rstp_status
*s
)
2937 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2938 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2939 struct rstp_port
*rp
= ofport
->rstp_port
;
2941 if (!ofproto
->rstp
|| !rp
) {
2947 rstp_port_get_status(rp
, &s
->port_id
, &s
->state
, &s
->role
,
2948 &s
->designated_bridge_id
, &s
->designated_port_id
,
2949 &s
->designated_path_cost
, &s
->tx_count
,
2950 &s
->rx_count
, &s
->error_count
, &s
->uptime
);
2955 set_queues(struct ofport
*ofport_
, const struct ofproto_port_queue
*qdscp
,
2958 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2959 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2961 if (ofport
->n_qdscp
!= n_qdscp
2962 || (n_qdscp
&& memcmp(ofport
->qdscp
, qdscp
,
2963 n_qdscp
* sizeof *qdscp
))) {
2964 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2965 free(ofport
->qdscp
);
2966 ofport
->qdscp
= n_qdscp
2967 ? xmemdup(qdscp
, n_qdscp
* sizeof *qdscp
)
2969 ofport
->n_qdscp
= n_qdscp
;
2977 /* Expires all MAC learning entries associated with 'bundle' and forces its
2978 * ofproto to revalidate every flow.
2980 * Normally MAC learning entries are removed only from the ofproto associated
2981 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2982 * are removed from every ofproto. When patch ports and SLB bonds are in use
2983 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2984 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2985 * with the host from which it migrated. */
2987 bundle_flush_macs(struct ofbundle
*bundle
, bool all_ofprotos
)
2989 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2990 struct mac_learning
*ml
= ofproto
->ml
;
2991 struct mac_entry
*mac
, *next_mac
;
2993 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2994 ovs_rwlock_wrlock(&ml
->rwlock
);
2995 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2996 if (mac_entry_get_port(ml
, mac
) == bundle
) {
2998 struct ofproto_dpif
*o
;
3000 HMAP_FOR_EACH (o
, all_ofproto_dpifs_by_name_node
,
3001 &all_ofproto_dpifs_by_name
) {
3003 struct mac_entry
*e
;
3005 ovs_rwlock_wrlock(&o
->ml
->rwlock
);
3006 e
= mac_learning_lookup(o
->ml
, mac
->mac
, mac
->vlan
);
3008 mac_learning_expire(o
->ml
, e
);
3010 ovs_rwlock_unlock(&o
->ml
->rwlock
);
3015 mac_learning_expire(ml
, mac
);
3018 ovs_rwlock_unlock(&ml
->rwlock
);
3022 bundle_move(struct ofbundle
*old
, struct ofbundle
*new)
3024 struct ofproto_dpif
*ofproto
= old
->ofproto
;
3025 struct mac_learning
*ml
= ofproto
->ml
;
3026 struct mac_entry
*mac
, *next_mac
;
3028 ovs_assert(new->ofproto
== old
->ofproto
);
3030 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3031 ovs_rwlock_wrlock(&ml
->rwlock
);
3032 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
3033 if (mac_entry_get_port(ml
, mac
) == old
) {
3034 mac_entry_set_port(ml
, mac
, new);
3037 ovs_rwlock_unlock(&ml
->rwlock
);
3040 static struct ofbundle
*
3041 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
3043 struct ofbundle
*bundle
;
3045 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
3046 &ofproto
->bundles
) {
3047 if (bundle
->aux
== aux
) {
3055 bundle_update(struct ofbundle
*bundle
)
3057 struct ofport_dpif
*port
;
3059 bundle
->floodable
= true;
3060 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3061 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
3062 || netdev_get_pt_mode(port
->up
.netdev
) == NETDEV_PT_LEGACY_L3
3063 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
3064 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
3065 bundle
->floodable
= false;
3072 bundle_del_port(struct ofport_dpif
*port
)
3074 struct ofbundle
*bundle
= port
->bundle
;
3076 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3078 ovs_list_remove(&port
->bundle_node
);
3079 port
->bundle
= NULL
;
3082 lacp_slave_unregister(bundle
->lacp
, port
);
3085 bond_slave_unregister(bundle
->bond
, port
);
3088 bundle_update(bundle
);
3092 bundle_add_port(struct ofbundle
*bundle
, ofp_port_t ofp_port
,
3093 struct lacp_slave_settings
*lacp
)
3095 struct ofport_dpif
*port
;
3097 port
= ofp_port_to_ofport(bundle
->ofproto
, ofp_port
);
3102 if (port
->bundle
!= bundle
) {
3103 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3105 bundle_remove(&port
->up
);
3108 port
->bundle
= bundle
;
3109 ovs_list_push_back(&bundle
->ports
, &port
->bundle_node
);
3110 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
3111 || netdev_get_pt_mode(port
->up
.netdev
) == NETDEV_PT_LEGACY_L3
3112 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
3113 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
3114 bundle
->floodable
= false;
3118 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3119 lacp_slave_register(bundle
->lacp
, port
, lacp
);
3126 bundle_destroy(struct ofbundle
*bundle
)
3128 struct ofproto_dpif
*ofproto
;
3129 struct ofport_dpif
*port
, *next_port
;
3135 ofproto
= bundle
->ofproto
;
3136 mbridge_unregister_bundle(ofproto
->mbridge
, bundle
);
3139 xlate_bundle_remove(bundle
);
3142 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
3143 bundle_del_port(port
);
3146 bundle_flush_macs(bundle
, true);
3147 mcast_snooping_flush_bundle(ofproto
->ms
, bundle
);
3148 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
3150 free(bundle
->trunks
);
3151 free(bundle
->cvlans
);
3152 lacp_unref(bundle
->lacp
);
3153 bond_unref(bundle
->bond
);
3158 bundle_set(struct ofproto
*ofproto_
, void *aux
,
3159 const struct ofproto_bundle_settings
*s
)
3161 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3162 bool need_flush
= false;
3163 struct ofport_dpif
*port
;
3164 struct ofbundle
*bundle
;
3165 unsigned long *trunks
= NULL
;
3166 unsigned long *cvlans
= NULL
;
3171 bundle
= bundle_lookup(ofproto
, aux
);
3174 bundle_destroy(bundle
);
3178 ovs_assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
3179 ovs_assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
3182 bundle
= xmalloc(sizeof *bundle
);
3184 bundle
->ofproto
= ofproto
;
3185 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
3186 hash_pointer(aux
, 0));
3188 bundle
->name
= NULL
;
3190 ovs_list_init(&bundle
->ports
);
3191 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
3192 bundle
->qinq_ethtype
= ETH_TYPE_VLAN_8021AD
;
3194 bundle
->trunks
= NULL
;
3195 bundle
->cvlans
= NULL
;
3196 bundle
->use_priority_tags
= s
->use_priority_tags
;
3197 bundle
->lacp
= NULL
;
3198 bundle
->bond
= NULL
;
3200 bundle
->floodable
= true;
3201 bundle
->protected = false;
3202 mbridge_register_bundle(ofproto
->mbridge
, bundle
);
3205 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
3207 bundle
->name
= xstrdup(s
->name
);
3212 ofproto
->lacp_enabled
= true;
3213 if (!bundle
->lacp
) {
3214 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3215 bundle
->lacp
= lacp_create();
3217 lacp_configure(bundle
->lacp
, s
->lacp
);
3219 lacp_unref(bundle
->lacp
);
3220 bundle
->lacp
= NULL
;
3223 /* Update set of ports. */
3225 for (i
= 0; i
< s
->n_slaves
; i
++) {
3226 if (!bundle_add_port(bundle
, s
->slaves
[i
],
3227 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
)) {
3231 if (!ok
|| ovs_list_size(&bundle
->ports
) != s
->n_slaves
) {
3232 struct ofport_dpif
*next_port
;
3234 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
3235 for (i
= 0; i
< s
->n_slaves
; i
++) {
3236 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
3241 bundle_del_port(port
);
3245 ovs_assert(ovs_list_size(&bundle
->ports
) <= s
->n_slaves
);
3247 if (ovs_list_is_empty(&bundle
->ports
)) {
3248 bundle_destroy(bundle
);
3252 /* Set VLAN tagging mode */
3253 if (s
->vlan_mode
!= bundle
->vlan_mode
3254 || s
->use_priority_tags
!= bundle
->use_priority_tags
) {
3255 bundle
->vlan_mode
= s
->vlan_mode
;
3256 bundle
->use_priority_tags
= s
->use_priority_tags
;
3260 if (s
->qinq_ethtype
!= bundle
->qinq_ethtype
) {
3261 bundle
->qinq_ethtype
= s
->qinq_ethtype
;
3266 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
3267 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
3269 if (vlan
!= bundle
->vlan
) {
3270 bundle
->vlan
= vlan
;
3274 /* Get trunked VLANs. */
3275 switch (s
->vlan_mode
) {
3276 case PORT_VLAN_ACCESS
:
3280 case PORT_VLAN_TRUNK
:
3281 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
3284 case PORT_VLAN_NATIVE_UNTAGGED
:
3285 case PORT_VLAN_NATIVE_TAGGED
:
3286 if (vlan
!= 0 && (!s
->trunks
3287 || !bitmap_is_set(s
->trunks
, vlan
)
3288 || bitmap_is_set(s
->trunks
, 0))) {
3289 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3291 trunks
= bitmap_clone(s
->trunks
, 4096);
3293 trunks
= bitmap_allocate1(4096);
3295 bitmap_set1(trunks
, vlan
);
3296 bitmap_set0(trunks
, 0);
3298 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
3302 case PORT_VLAN_DOT1Q_TUNNEL
:
3303 cvlans
= CONST_CAST(unsigned long *, s
->cvlans
);
3309 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
3310 free(bundle
->trunks
);
3311 if (trunks
== s
->trunks
) {
3312 bundle
->trunks
= vlan_bitmap_clone(trunks
);
3314 bundle
->trunks
= trunks
;
3319 if (trunks
!= s
->trunks
) {
3323 if (!vlan_bitmap_equal(cvlans
, bundle
->cvlans
)) {
3324 free(bundle
->cvlans
);
3325 if (cvlans
== s
->cvlans
) {
3326 bundle
->cvlans
= vlan_bitmap_clone(cvlans
);
3328 bundle
->cvlans
= cvlans
;
3333 if (cvlans
!= s
->cvlans
) {
3338 if (!ovs_list_is_short(&bundle
->ports
)) {
3339 bundle
->ofproto
->has_bonded_bundles
= true;
3341 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
3342 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3345 bundle
->bond
= bond_create(s
->bond
, ofproto
);
3346 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3349 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3350 bond_slave_register(bundle
->bond
, port
,
3351 port
->up
.ofp_port
, port
->up
.netdev
);
3354 bond_unref(bundle
->bond
);
3355 bundle
->bond
= NULL
;
3358 /* Set proteced port mode */
3359 if (s
->protected != bundle
->protected) {
3360 bundle
->protected = s
->protected;
3364 /* If we changed something that would affect MAC learning, un-learn
3365 * everything on this port and force flow revalidation. */
3367 bundle_flush_macs(bundle
, false);
3368 mcast_snooping_flush_bundle(ofproto
->ms
, bundle
);
3375 bundle_remove(struct ofport
*port_
)
3377 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
3378 struct ofbundle
*bundle
= port
->bundle
;
3381 bundle_del_port(port
);
3382 if (ovs_list_is_empty(&bundle
->ports
)) {
3383 bundle_destroy(bundle
);
3384 } else if (ovs_list_is_short(&bundle
->ports
)) {
3385 bond_unref(bundle
->bond
);
3386 bundle
->bond
= NULL
;
3392 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
3394 struct ofport_dpif
*port
= port_
;
3398 error
= netdev_get_etheraddr(port
->up
.netdev
, &ea
);
3400 struct dp_packet packet
;
3403 dp_packet_init(&packet
, 0);
3404 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
3406 memcpy(packet_pdu
, pdu
, pdu_size
);
3408 error
= ofproto_dpif_send_packet(port
, false, &packet
);
3410 VLOG_WARN_RL(&rl
, "port %s: cannot transmit LACP PDU (%s).",
3411 port
->bundle
->name
, ovs_strerror(error
));
3413 dp_packet_uninit(&packet
);
3415 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 10);
3416 VLOG_ERR_RL(&rll
, "port %s: cannot obtain Ethernet address of iface "
3417 "%s (%s)", port
->bundle
->name
,
3418 netdev_get_name(port
->up
.netdev
), ovs_strerror(error
));
3423 bundle_send_learning_packets(struct ofbundle
*bundle
)
3425 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
3426 int error
, n_packets
, n_errors
;
3427 struct mac_entry
*e
;
3429 struct ovs_list list_node
;
3430 struct ofport_dpif
*port
;
3431 struct dp_packet
*pkt
;
3433 struct ovs_list packets
;
3435 ovs_list_init(&packets
);
3436 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
3437 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
3438 if (mac_entry_get_port(ofproto
->ml
, e
) != bundle
) {
3439 pkt_node
= xmalloc(sizeof *pkt_node
);
3440 pkt_node
->pkt
= bond_compose_learning_packet(bundle
->bond
,
3442 (void **)&pkt_node
->port
);
3443 ovs_list_push_back(&packets
, &pkt_node
->list_node
);
3446 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3448 error
= n_packets
= n_errors
= 0;
3449 LIST_FOR_EACH_POP (pkt_node
, list_node
, &packets
) {
3452 ret
= ofproto_dpif_send_packet(pkt_node
->port
, false, pkt_node
->pkt
);
3453 dp_packet_delete(pkt_node
->pkt
);
3463 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
3464 VLOG_WARN_RL(&rll
, "bond %s: %d errors sending %d gratuitous learning "
3465 "packets, last error was: %s",
3466 bundle
->name
, n_errors
, n_packets
, ovs_strerror(error
));
3468 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3469 bundle
->name
, n_packets
);
3474 bundle_run(struct ofbundle
*bundle
)
3477 lacp_run(bundle
->lacp
, send_pdu_cb
);
3480 struct ofport_dpif
*port
;
3482 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3483 bond_slave_set_may_enable(bundle
->bond
, port
, port
->up
.may_enable
);
3486 if (bond_run(bundle
->bond
, lacp_status(bundle
->lacp
))) {
3487 bundle
->ofproto
->backer
->need_revalidate
= REV_BOND
;
3490 if (bond_should_send_learning_packets(bundle
->bond
)) {
3491 bundle_send_learning_packets(bundle
);
3497 bundle_wait(struct ofbundle
*bundle
)
3500 lacp_wait(bundle
->lacp
);
3503 bond_wait(bundle
->bond
);
3510 mirror_set__(struct ofproto
*ofproto_
, void *aux
,
3511 const struct ofproto_mirror_settings
*s
)
3513 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3514 struct ofbundle
**srcs
, **dsts
;
3519 mirror_destroy(ofproto
->mbridge
, aux
);
3523 srcs
= xmalloc(s
->n_srcs
* sizeof *srcs
);
3524 dsts
= xmalloc(s
->n_dsts
* sizeof *dsts
);
3526 for (i
= 0; i
< s
->n_srcs
; i
++) {
3527 srcs
[i
] = bundle_lookup(ofproto
, s
->srcs
[i
]);
3530 for (i
= 0; i
< s
->n_dsts
; i
++) {
3531 dsts
[i
] = bundle_lookup(ofproto
, s
->dsts
[i
]);
3534 error
= mirror_set(ofproto
->mbridge
, aux
, s
->name
, srcs
, s
->n_srcs
, dsts
,
3535 s
->n_dsts
, s
->src_vlans
,
3536 bundle_lookup(ofproto
, s
->out_bundle
),
3537 s
->snaplen
, s
->out_vlan
);
3544 mirror_get_stats__(struct ofproto
*ofproto
, void *aux
,
3545 uint64_t *packets
, uint64_t *bytes
)
3547 return mirror_get_stats(ofproto_dpif_cast(ofproto
)->mbridge
, aux
, packets
,
3552 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
3554 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3555 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3556 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
3557 mac_learning_flush(ofproto
->ml
);
3559 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3564 is_mirror_output_bundle(const struct ofproto
*ofproto_
, void *aux
)
3566 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3567 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3568 return bundle
&& mirror_bundle_out(ofproto
->mbridge
, bundle
) != 0;
3572 forward_bpdu_changed(struct ofproto
*ofproto_
)
3574 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3575 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3579 set_mac_table_config(struct ofproto
*ofproto_
, unsigned int idle_time
,
3582 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3583 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3584 mac_learning_set_idle_time(ofproto
->ml
, idle_time
);
3585 mac_learning_set_max_entries(ofproto
->ml
, max_entries
);
3586 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3589 /* Configures multicast snooping on 'ofport' using the settings
3590 * defined in 's'. */
3592 set_mcast_snooping(struct ofproto
*ofproto_
,
3593 const struct ofproto_mcast_snooping_settings
*s
)
3595 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3597 /* Only revalidate flows if the configuration changed. */
3598 if (!s
!= !ofproto
->ms
) {
3599 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3604 ofproto
->ms
= mcast_snooping_create();
3607 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3608 mcast_snooping_set_idle_time(ofproto
->ms
, s
->idle_time
);
3609 mcast_snooping_set_max_entries(ofproto
->ms
, s
->max_entries
);
3610 if (mcast_snooping_set_flood_unreg(ofproto
->ms
, s
->flood_unreg
)) {
3611 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3613 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3615 mcast_snooping_unref(ofproto
->ms
);
3622 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3624 set_mcast_snooping_port(struct ofproto
*ofproto_
, void *aux
,
3625 const struct ofproto_mcast_snooping_port_settings
*s
)
3627 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3628 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3630 if (ofproto
->ms
&& s
) {
3631 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3632 mcast_snooping_set_port_flood(ofproto
->ms
, bundle
, s
->flood
);
3633 mcast_snooping_set_port_flood_reports(ofproto
->ms
, bundle
,
3635 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3643 struct ofport_dpif
*
3644 ofp_port_to_ofport(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
3646 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
3647 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
3651 ofproto_port_from_dpif_port(struct ofproto_dpif
*ofproto
,
3652 struct ofproto_port
*ofproto_port
,
3653 struct dpif_port
*dpif_port
)
3655 ofproto_port
->name
= dpif_port
->name
;
3656 ofproto_port
->type
= dpif_port
->type
;
3657 ofproto_port
->ofp_port
= odp_port_to_ofp_port(ofproto
, dpif_port
->port_no
);
3661 ofport_update_peer(struct ofport_dpif
*ofport
)
3663 const struct ofproto_dpif
*ofproto
;
3664 struct dpif_backer
*backer
;
3667 if (!netdev_vport_is_patch(ofport
->up
.netdev
)) {
3671 backer
= ofproto_dpif_cast(ofport
->up
.ofproto
)->backer
;
3672 backer
->need_revalidate
= REV_RECONFIGURE
;
3675 ofport
->peer
->peer
= NULL
;
3676 ofport
->peer
= NULL
;
3679 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3684 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
3685 &all_ofproto_dpifs_by_name
) {
3686 struct ofport
*peer_ofport
;
3687 struct ofport_dpif
*peer
;
3690 if (ofproto
->backer
!= backer
) {
3694 peer_ofport
= shash_find_data(&ofproto
->up
.port_by_name
, peer_name
);
3699 peer
= ofport_dpif_cast(peer_ofport
);
3700 peer_peer
= netdev_vport_patch_peer(peer
->up
.netdev
);
3701 if (peer_peer
&& !strcmp(netdev_get_name(ofport
->up
.netdev
),
3703 ofport
->peer
= peer
;
3704 ofport
->peer
->peer
= ofport
;
3714 may_enable_port(struct ofport_dpif
*ofport
)
3716 /* If CFM or BFD is enabled, then at least one of them must report that the
3718 if ((ofport
->bfd
|| ofport
->cfm
)
3720 && !cfm_get_fault(ofport
->cfm
)
3721 && cfm_get_opup(ofport
->cfm
) != 0)
3723 && bfd_forwarding(ofport
->bfd
))) {
3727 /* If LACP is enabled, it must report that the link is enabled. */
3729 && !lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
)) {
3737 port_run(struct ofport_dpif
*ofport
)
3739 long long int carrier_seq
= netdev_get_carrier_resets(ofport
->up
.netdev
);
3740 bool carrier_changed
= carrier_seq
!= ofport
->carrier_seq
;
3741 bool enable
= netdev_get_carrier(ofport
->up
.netdev
);
3743 ofport
->carrier_seq
= carrier_seq
;
3744 if (carrier_changed
&& ofport
->bundle
) {
3745 lacp_slave_carrier_changed(ofport
->bundle
->lacp
, ofport
, enable
);
3749 enable
= may_enable_port(ofport
);
3752 if (ofport
->up
.may_enable
!= enable
) {
3753 ofproto_port_set_enable(&ofport
->up
, enable
);
3755 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3756 ofproto
->backer
->need_revalidate
= REV_PORT_TOGGLED
;
3758 if (ofport
->rstp_port
) {
3759 rstp_port_set_mac_operational(ofport
->rstp_port
, enable
);
3765 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
3766 struct ofproto_port
*ofproto_port
)
3768 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3769 struct dpif_port dpif_port
;
3772 if (sset_contains(&ofproto
->ghost_ports
, devname
)) {
3773 const char *type
= netdev_get_type_from_name(devname
);
3775 /* We may be called before ofproto->up.port_by_name is populated with
3776 * the appropriate ofport. For this reason, we must get the name and
3777 * type from the netdev layer directly. */
3779 const struct ofport
*ofport
;
3781 ofport
= shash_find_data(&ofproto
->up
.port_by_name
, devname
);
3782 ofproto_port
->ofp_port
= ofport
? ofport
->ofp_port
: OFPP_NONE
;
3783 ofproto_port
->name
= xstrdup(devname
);
3784 ofproto_port
->type
= xstrdup(type
);
3790 if (!sset_contains(&ofproto
->ports
, devname
)) {
3793 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
,
3794 devname
, &dpif_port
);
3796 ofproto_port_from_dpif_port(ofproto
, ofproto_port
, &dpif_port
);
3802 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
)
3804 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3805 const char *devname
= netdev_get_name(netdev
);
3806 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
3807 const char *dp_port_name
;
3809 if (netdev_vport_is_patch(netdev
)) {
3810 sset_add(&ofproto
->ghost_ports
, netdev_get_name(netdev
));
3814 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
3815 if (!dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
3816 odp_port_t port_no
= ODPP_NONE
;
3819 error
= dpif_port_add(ofproto
->backer
->dpif
, netdev
, &port_no
);
3823 if (netdev_get_tunnel_config(netdev
)) {
3824 simap_put(&ofproto
->backer
->tnl_backers
,
3825 dp_port_name
, odp_to_u32(port_no
));
3829 if (netdev_get_tunnel_config(netdev
)) {
3830 sset_add(&ofproto
->ghost_ports
, devname
);
3832 sset_add(&ofproto
->ports
, devname
);
3838 port_del(struct ofproto
*ofproto_
, ofp_port_t ofp_port
)
3840 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3841 struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
3848 sset_find_and_delete(&ofproto
->ghost_ports
,
3849 netdev_get_name(ofport
->up
.netdev
));
3850 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3851 if (!ofport
->is_tunnel
&& !netdev_vport_is_patch(ofport
->up
.netdev
)) {
3852 error
= dpif_port_del(ofproto
->backer
->dpif
, ofport
->odp_port
, false);
3854 /* The caller is going to close ofport->up.netdev. If this is a
3855 * bonded port, then the bond is using that netdev, so remove it
3856 * from the bond. The client will need to reconfigure everything
3857 * after deleting ports, so then the slave will get re-added. */
3858 bundle_remove(&ofport
->up
);
3865 port_set_config(const struct ofport
*ofport_
, const struct smap
*cfg
)
3867 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3868 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3870 if (sset_contains(&ofproto
->ghost_ports
,
3871 netdev_get_name(ofport
->up
.netdev
))) {
3875 return dpif_port_set_config(ofproto
->backer
->dpif
, ofport
->odp_port
, cfg
);
3879 port_get_stats(const struct ofport
*ofport_
, struct netdev_stats
*stats
)
3881 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3884 error
= netdev_get_stats(ofport
->up
.netdev
, stats
);
3886 if (!error
&& ofport_
->ofp_port
== OFPP_LOCAL
) {
3887 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3889 ovs_mutex_lock(&ofproto
->stats_mutex
);
3890 /* ofproto->stats.tx_packets represents packets that we created
3891 * internally and sent to some port (e.g. packets sent with
3892 * ofproto_dpif_send_packet()). Account for them as if they had
3893 * come from OFPP_LOCAL and got forwarded. */
3895 if (stats
->rx_packets
!= UINT64_MAX
) {
3896 stats
->rx_packets
+= ofproto
->stats
.tx_packets
;
3899 if (stats
->rx_bytes
!= UINT64_MAX
) {
3900 stats
->rx_bytes
+= ofproto
->stats
.tx_bytes
;
3903 /* ofproto->stats.rx_packets represents packets that were received on
3904 * some port and we processed internally and dropped (e.g. STP).
3905 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3907 if (stats
->tx_packets
!= UINT64_MAX
) {
3908 stats
->tx_packets
+= ofproto
->stats
.rx_packets
;
3911 if (stats
->tx_bytes
!= UINT64_MAX
) {
3912 stats
->tx_bytes
+= ofproto
->stats
.rx_bytes
;
3914 ovs_mutex_unlock(&ofproto
->stats_mutex
);
3921 vport_get_status(const struct ofport
*ofport_
, char **errp
)
3923 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3926 if (!netdev_vport_is_patch(ofport
->up
.netdev
) || ofport
->peer
) {
3930 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3934 *errp
= xasprintf("No usable peer '%s' exists in '%s' datapath.",
3935 peer_name
, ofport
->up
.ofproto
->type
);
3941 port_get_lacp_stats(const struct ofport
*ofport_
, struct lacp_slave_stats
*stats
)
3943 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3944 if (ofport
->bundle
&& ofport
->bundle
->lacp
) {
3945 if (lacp_get_slave_stats(ofport
->bundle
->lacp
, ofport
, stats
)) {
3952 struct port_dump_state
{
3953 struct sset_position pos
;
3956 struct ofproto_port port
;
3961 port_dump_start(const struct ofproto
*ofproto_ OVS_UNUSED
, void **statep
)
3963 *statep
= xzalloc(sizeof(struct port_dump_state
));
3968 port_dump_next(const struct ofproto
*ofproto_
, void *state_
,
3969 struct ofproto_port
*port
)
3971 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3972 struct port_dump_state
*state
= state_
;
3973 const struct sset
*sset
;
3974 struct sset_node
*node
;
3976 if (state
->has_port
) {
3977 ofproto_port_destroy(&state
->port
);
3978 state
->has_port
= false;
3980 sset
= state
->ghost
? &ofproto
->ghost_ports
: &ofproto
->ports
;
3981 while ((node
= sset_at_position(sset
, &state
->pos
))) {
3984 error
= port_query_by_name(ofproto_
, node
->name
, &state
->port
);
3986 *port
= state
->port
;
3987 state
->has_port
= true;
3989 } else if (error
!= ENODEV
) {
3994 if (!state
->ghost
) {
3995 state
->ghost
= true;
3996 memset(&state
->pos
, 0, sizeof state
->pos
);
3997 return port_dump_next(ofproto_
, state_
, port
);
4004 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
4006 struct port_dump_state
*state
= state_
;
4008 if (state
->has_port
) {
4009 ofproto_port_destroy(&state
->port
);
4016 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
4018 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4020 if (ofproto
->port_poll_errno
) {
4021 int error
= ofproto
->port_poll_errno
;
4022 ofproto
->port_poll_errno
= 0;
4026 if (sset_is_empty(&ofproto
->port_poll_set
)) {
4030 *devnamep
= sset_pop(&ofproto
->port_poll_set
);
4035 port_poll_wait(const struct ofproto
*ofproto_
)
4037 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4038 dpif_port_poll_wait(ofproto
->backer
->dpif
);
4042 port_is_lacp_current(const struct ofport
*ofport_
)
4044 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
4045 return (ofport
->bundle
&& ofport
->bundle
->lacp
4046 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
4050 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4051 * then delete it entirely. */
4053 rule_expire(struct rule_dpif
*rule
, long long now
)
4054 OVS_REQUIRES(ofproto_mutex
)
4056 uint16_t hard_timeout
, idle_timeout
;
4059 hard_timeout
= rule
->up
.hard_timeout
;
4060 idle_timeout
= rule
->up
.idle_timeout
;
4062 /* Has 'rule' expired? */
4064 long long int modified
;
4066 ovs_mutex_lock(&rule
->up
.mutex
);
4067 modified
= rule
->up
.modified
;
4068 ovs_mutex_unlock(&rule
->up
.mutex
);
4070 if (now
> modified
+ hard_timeout
* 1000) {
4071 reason
= OFPRR_HARD_TIMEOUT
;
4075 if (reason
< 0 && idle_timeout
) {
4078 ovs_mutex_lock(&rule
->stats_mutex
);
4079 used
= rule
->stats
.used
;
4080 ovs_mutex_unlock(&rule
->stats_mutex
);
4082 if (now
> used
+ idle_timeout
* 1000) {
4083 reason
= OFPRR_IDLE_TIMEOUT
;
4088 COVERAGE_INC(ofproto_dpif_expired
);
4089 ofproto_rule_expire(&rule
->up
, reason
);
4094 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif
*ofproto
,
4095 ofp_port_t in_port
, struct dp_packet
*packet
)
4097 if (in_port
== OFPP_NONE
) {
4098 in_port
= OFPP_LOCAL
;
4100 packet
->md
.in_port
.odp_port
= ofp_port_to_odp_port(ofproto
, in_port
);
4104 ofproto_dpif_execute_actions__(struct ofproto_dpif
*ofproto
,
4105 ovs_version_t version
, const struct flow
*flow
,
4106 struct rule_dpif
*rule
,
4107 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
4108 int depth
, int resubmits
,
4109 struct dp_packet
*packet
)
4111 struct dpif_flow_stats stats
;
4112 struct xlate_out xout
;
4113 struct xlate_in xin
;
4114 struct dpif_execute execute
;
4117 ovs_assert((rule
!= NULL
) != (ofpacts
!= NULL
));
4119 dpif_flow_stats_extract(flow
, packet
, time_msec(), &stats
);
4122 rule_dpif_credit_stats(rule
, &stats
);
4125 uint64_t odp_actions_stub
[1024 / 8];
4126 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
4127 xlate_in_init(&xin
, ofproto
, version
, flow
, flow
->in_port
.ofp_port
, rule
,
4128 stats
.tcp_flags
, packet
, NULL
, &odp_actions
);
4129 xin
.ofpacts
= ofpacts
;
4130 xin
.ofpacts_len
= ofpacts_len
;
4131 xin
.resubmit_stats
= &stats
;
4133 xin
.resubmits
= resubmits
;
4134 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
4139 execute
.actions
= odp_actions
.data
;
4140 execute
.actions_len
= odp_actions
.size
;
4142 pkt_metadata_from_flow(&packet
->md
, flow
);
4143 execute
.packet
= packet
;
4144 execute
.flow
= flow
;
4145 execute
.needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
4146 execute
.probe
= false;
4149 /* Fix up in_port. */
4150 ofproto_dpif_set_packet_odp_port(ofproto
, flow
->in_port
.ofp_port
, packet
);
4152 error
= dpif_execute(ofproto
->backer
->dpif
, &execute
);
4154 xlate_out_uninit(&xout
);
4155 ofpbuf_uninit(&odp_actions
);
4160 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
4161 * 'flow' must reflect the data in 'packet'. */
4163 ofproto_dpif_execute_actions(struct ofproto_dpif
*ofproto
,
4164 ovs_version_t version
, const struct flow
*flow
,
4165 struct rule_dpif
*rule
,
4166 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
4167 struct dp_packet
*packet
)
4169 return ofproto_dpif_execute_actions__(ofproto
, version
, flow
, rule
,
4170 ofpacts
, ofpacts_len
, 0, 0, packet
);
4174 rule_dpif_credit_stats__(struct rule_dpif
*rule
,
4175 const struct dpif_flow_stats
*stats
,
4177 OVS_REQUIRES(rule
->stats_mutex
)
4179 if (credit_counts
) {
4180 rule
->stats
.n_packets
+= stats
->n_packets
;
4181 rule
->stats
.n_bytes
+= stats
->n_bytes
;
4183 rule
->stats
.used
= MAX(rule
->stats
.used
, stats
->used
);
4187 rule_dpif_credit_stats(struct rule_dpif
*rule
,
4188 const struct dpif_flow_stats
*stats
)
4190 ovs_mutex_lock(&rule
->stats_mutex
);
4191 if (OVS_UNLIKELY(rule
->new_rule
)) {
4192 ovs_mutex_lock(&rule
->new_rule
->stats_mutex
);
4193 rule_dpif_credit_stats__(rule
->new_rule
, stats
, rule
->forward_counts
);
4194 ovs_mutex_unlock(&rule
->new_rule
->stats_mutex
);
4196 rule_dpif_credit_stats__(rule
, stats
, true);
4198 ovs_mutex_unlock(&rule
->stats_mutex
);
4201 /* Sets 'rule''s recirculation id. */
4203 rule_dpif_set_recirc_id(struct rule_dpif
*rule
, uint32_t id
)
4204 OVS_REQUIRES(rule
->up
.mutex
)
4206 ovs_assert(!rule
->recirc_id
|| rule
->recirc_id
== id
);
4207 if (rule
->recirc_id
== id
) {
4208 /* Release the new reference to the same id. */
4211 rule
->recirc_id
= id
;
4215 /* Sets 'rule''s recirculation id. */
4217 rule_set_recirc_id(struct rule
*rule_
, uint32_t id
)
4219 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4221 ovs_mutex_lock(&rule
->up
.mutex
);
4222 rule_dpif_set_recirc_id(rule
, id
);
4223 ovs_mutex_unlock(&rule
->up
.mutex
);
4227 ofproto_dpif_get_tables_version(struct ofproto_dpif
*ofproto
)
4229 ovs_version_t version
;
4231 /* Use memory_order_acquire to signify that any following memory accesses
4232 * can not be reordered to happen before this atomic read. This makes sure
4233 * all following reads relate to this or a newer version, but never to an
4235 atomic_read_explicit(&ofproto
->tables_version
, &version
,
4236 memory_order_acquire
);
4240 /* The returned rule (if any) is valid at least until the next RCU quiescent
4241 * period. If the rule needs to stay around longer, the caller should take
4244 * 'flow' is non-const to allow for temporary modifications during the lookup.
4245 * Any changes are restored before returning. */
4246 static struct rule_dpif
*
4247 rule_dpif_lookup_in_table(struct ofproto_dpif
*ofproto
, ovs_version_t version
,
4248 uint8_t table_id
, struct flow
*flow
,
4249 struct flow_wildcards
*wc
)
4251 struct classifier
*cls
= &ofproto
->up
.tables
[table_id
].cls
;
4252 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls
, version
,
4257 ofproto_dpif_credit_table_stats(struct ofproto_dpif
*ofproto
, uint8_t table_id
,
4258 uint64_t n_matches
, uint64_t n_misses
)
4260 struct oftable
*tbl
= &ofproto
->up
.tables
[table_id
];
4264 atomic_add_relaxed(&tbl
->n_matched
, n_matches
, &orig
);
4267 atomic_add_relaxed(&tbl
->n_missed
, n_misses
, &orig
);
4271 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
4272 * table '*table_id'. Returns the rule that was found, which may be one of the
4273 * special rules according to packet miss hadling. If 'may_packet_in' is
4274 * false, returning of the miss_rule (which issues packet ins for the
4275 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
4276 * that were used during the lookup.
4278 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
4279 * if none is found then the table miss configuration for that table is
4280 * honored, which can result in additional lookups in other OpenFlow tables.
4281 * In this case the function updates '*table_id' to reflect the final OpenFlow
4282 * table that was searched.
4284 * If 'honor_table_miss' is false, then only one table lookup occurs, in
4287 * The rule is returned in '*rule', which is valid at least until the next
4288 * RCU quiescent period. If the '*rule' needs to stay around longer, the
4289 * caller must take a reference.
4291 * 'in_port' allows the lookup to take place as if the in port had the value
4292 * 'in_port'. This is needed for resubmit action support.
4294 * 'flow' is non-const to allow for temporary modifications during the lookup.
4295 * Any changes are restored before returning. */
4297 rule_dpif_lookup_from_table(struct ofproto_dpif
*ofproto
,
4298 ovs_version_t version
, struct flow
*flow
,
4299 struct flow_wildcards
*wc
,
4300 const struct dpif_flow_stats
*stats
,
4301 uint8_t *table_id
, ofp_port_t in_port
,
4302 bool may_packet_in
, bool honor_table_miss
,
4303 struct xlate_cache
*xcache
)
4305 ovs_be16 old_tp_src
= flow
->tp_src
, old_tp_dst
= flow
->tp_dst
;
4306 ofp_port_t old_in_port
= flow
->in_port
.ofp_port
;
4307 enum ofputil_table_miss miss_config
;
4308 struct rule_dpif
*rule
;
4311 /* We always unwildcard nw_frag (for IP), so they
4312 * need not be unwildcarded here. */
4313 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
4314 && ofproto
->up
.frag_handling
!= OFPUTIL_FRAG_NX_MATCH
) {
4315 if (ofproto
->up
.frag_handling
== OFPUTIL_FRAG_NORMAL
) {
4316 /* We must pretend that transport ports are unavailable. */
4317 flow
->tp_src
= htons(0);
4318 flow
->tp_dst
= htons(0);
4320 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
4321 * Use the drop_frags_rule (which cannot disappear). */
4322 rule
= ofproto
->drop_frags_rule
;
4324 struct oftable
*tbl
= &ofproto
->up
.tables
[*table_id
];
4327 atomic_add_relaxed(&tbl
->n_matched
, stats
->n_packets
, &orig
);
4330 struct xc_entry
*entry
;
4332 entry
= xlate_cache_add_entry(xcache
, XC_TABLE
);
4333 entry
->table
.ofproto
= ofproto
;
4334 entry
->table
.id
= *table_id
;
4335 entry
->table
.match
= true;
4341 /* Look up a flow with 'in_port' as the input port. Then restore the
4342 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
4343 * have surprising behavior). */
4344 flow
->in_port
.ofp_port
= in_port
;
4346 /* Our current implementation depends on n_tables == N_TABLES, and
4347 * TBL_INTERNAL being the last table. */
4348 BUILD_ASSERT_DECL(N_TABLES
== TBL_INTERNAL
+ 1);
4350 miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
4352 for (next_id
= *table_id
;
4353 next_id
< ofproto
->up
.n_tables
;
4354 next_id
++, next_id
+= (next_id
== TBL_INTERNAL
))
4356 *table_id
= next_id
;
4357 rule
= rule_dpif_lookup_in_table(ofproto
, version
, next_id
, flow
, wc
);
4359 struct oftable
*tbl
= &ofproto
->up
.tables
[next_id
];
4362 atomic_add_relaxed(rule
? &tbl
->n_matched
: &tbl
->n_missed
,
4363 stats
->n_packets
, &orig
);
4366 struct xc_entry
*entry
;
4368 entry
= xlate_cache_add_entry(xcache
, XC_TABLE
);
4369 entry
->table
.ofproto
= ofproto
;
4370 entry
->table
.id
= next_id
;
4371 entry
->table
.match
= (rule
!= NULL
);
4374 goto out
; /* Match. */
4376 if (honor_table_miss
) {
4377 miss_config
= ofproto_table_get_miss_config(&ofproto
->up
,
4379 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
4386 rule
= ofproto
->no_packet_in_rule
;
4387 if (may_packet_in
) {
4388 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
4389 || miss_config
== OFPUTIL_TABLE_MISS_CONTROLLER
) {
4390 struct ofport_dpif
*port
;
4392 port
= ofp_port_to_ofport(ofproto
, old_in_port
);
4394 VLOG_WARN_RL(&rl
, "packet-in on unknown OpenFlow port %"PRIu32
,
4396 } else if (!(port
->up
.pp
.config
& OFPUTIL_PC_NO_PACKET_IN
)) {
4397 rule
= ofproto
->miss_rule
;
4399 } else if (miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
&&
4400 connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
)) {
4401 rule
= ofproto
->miss_rule
;
4405 /* Restore port numbers, as they may have been modified above. */
4406 flow
->tp_src
= old_tp_src
;
4407 flow
->tp_dst
= old_tp_dst
;
4408 /* Restore the old in port. */
4409 flow
->in_port
.ofp_port
= old_in_port
;
4414 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
4416 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
4419 static struct rule
*
4422 struct rule_dpif
*rule
= xzalloc(sizeof *rule
);
4427 rule_dealloc(struct rule
*rule_
)
4429 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4434 check_mask(struct ofproto_dpif
*ofproto
, const struct miniflow
*flow
)
4436 const struct odp_support
*support
;
4437 uint16_t ct_state
, ct_zone
;
4441 support
= &ofproto
->backer
->rt_support
.odp
;
4442 ct_state
= MINIFLOW_GET_U8(flow
, ct_state
);
4444 if (ct_state
& CS_UNSUPPORTED_MASK
) {
4445 return OFPERR_OFPBMC_BAD_MASK
;
4448 /* Do not bother dissecting the flow further if the datapath supports all
4449 * the features we know of. */
4450 if (support
->ct_state
&& support
->ct_zone
&& support
->ct_mark
4451 && support
->ct_label
&& support
->ct_state_nat
4452 && support
->ct_orig_tuple
&& support
->ct_orig_tuple6
) {
4456 ct_zone
= MINIFLOW_GET_U16(flow
, ct_zone
);
4457 ct_mark
= MINIFLOW_GET_U32(flow
, ct_mark
);
4458 ct_label
= MINIFLOW_GET_U128(flow
, ct_label
);
4460 if ((ct_state
&& !support
->ct_state
)
4461 || ((ct_state
& (CS_SRC_NAT
| CS_DST_NAT
)) && !support
->ct_state_nat
)
4462 || (ct_zone
&& !support
->ct_zone
)
4463 || (ct_mark
&& !support
->ct_mark
)
4464 || (!ovs_u128_is_zero(ct_label
) && !support
->ct_label
)) {
4465 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4468 if (!support
->ct_orig_tuple
&& !support
->ct_orig_tuple6
4469 && (MINIFLOW_GET_U8(flow
, ct_nw_proto
)
4470 || MINIFLOW_GET_U16(flow
, ct_tp_src
)
4471 || MINIFLOW_GET_U16(flow
, ct_tp_dst
))) {
4472 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4475 if (!support
->ct_orig_tuple
4476 && (MINIFLOW_GET_U32(flow
, ct_nw_src
)
4477 || MINIFLOW_GET_U32(flow
, ct_nw_dst
))) {
4478 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4481 if (!support
->ct_orig_tuple6
4482 && (!ovs_u128_is_zero(MINIFLOW_GET_U128(flow
, ct_ipv6_src
))
4483 || !ovs_u128_is_zero(MINIFLOW_GET_U128(flow
, ct_ipv6_dst
)))) {
4484 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4491 report_unsupported_act(const char *action
, const char *detail
)
4493 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
4494 VLOG_WARN_RL(&rll
, "Rejecting %s action because datapath does not support"
4495 "%s%s (your kernel module may be out of date)",
4496 action
, detail
? " " : "", detail
? detail
: "");
4500 check_actions(const struct ofproto_dpif
*ofproto
,
4501 const struct rule_actions
*const actions
)
4503 const struct ofpact
*ofpact
;
4504 const struct odp_support
*support
= &ofproto
->backer
->rt_support
.odp
;
4506 OFPACT_FOR_EACH (ofpact
, actions
->ofpacts
, actions
->ofpacts_len
) {
4507 if (ofpact
->type
== OFPACT_CT
) {
4508 const struct ofpact_conntrack
*ct
;
4509 const struct ofpact
*a
;
4511 ct
= CONTAINER_OF(ofpact
, struct ofpact_conntrack
, ofpact
);
4513 if (!support
->ct_state
) {
4514 report_unsupported_act("ct", "ct action");
4515 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4517 if ((ct
->zone_imm
|| ct
->zone_src
.field
) && !support
->ct_zone
) {
4518 report_unsupported_act("ct", "ct zones");
4519 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4521 /* So far the force commit feature is implemented together with the
4522 * original direction tuple feature by all datapaths, so we use the
4523 * support flag for the 'ct_orig_tuple' to indicate support for the
4524 * force commit feature as well. */
4525 if ((ct
->flags
& NX_CT_F_FORCE
) && !support
->ct_orig_tuple
) {
4526 report_unsupported_act("ct", "force commit");
4527 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4530 OFPACT_FOR_EACH(a
, ct
->actions
, ofpact_ct_get_action_len(ct
)) {
4531 const struct mf_field
*dst
= ofpact_get_mf_dst(a
);
4533 if (a
->type
== OFPACT_NAT
&& !support
->ct_state_nat
) {
4534 /* The backer doesn't seem to support the NAT bits in
4535 * 'ct_state': assume that it doesn't support the NAT
4537 report_unsupported_act("ct", "nat");
4538 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4540 if (dst
&& ((dst
->id
== MFF_CT_MARK
&& !support
->ct_mark
) ||
4541 (dst
->id
== MFF_CT_LABEL
&& !support
->ct_label
))) {
4542 report_unsupported_act("ct", "setting mark and/or label");
4543 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4546 } else if (ofpact
->type
== OFPACT_RESUBMIT
) {
4547 struct ofpact_resubmit
*resubmit
= ofpact_get_RESUBMIT(ofpact
);
4549 if (resubmit
->with_ct_orig
&& !support
->ct_orig_tuple
) {
4550 report_unsupported_act("resubmit",
4551 "ct original direction tuple");
4552 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4561 rule_check(struct rule
*rule
)
4563 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->ofproto
);
4566 err
= check_mask(ofproto
, &rule
->cr
.match
.mask
->masks
);
4570 return check_actions(ofproto
, rule
->actions
);
4574 rule_construct(struct rule
*rule_
)
4575 OVS_NO_THREAD_SAFETY_ANALYSIS
4577 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4580 error
= rule_check(rule_
);
4585 ovs_mutex_init_adaptive(&rule
->stats_mutex
);
4586 rule
->stats
.n_packets
= 0;
4587 rule
->stats
.n_bytes
= 0;
4588 rule
->stats
.used
= rule
->up
.modified
;
4589 rule
->recirc_id
= 0;
4590 rule
->new_rule
= NULL
;
4591 rule
->forward_counts
= false;
4597 rule_insert(struct rule
*rule_
, struct rule
*old_rule_
, bool forward_counts
)
4598 OVS_REQUIRES(ofproto_mutex
)
4600 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4603 struct rule_dpif
*old_rule
= rule_dpif_cast(old_rule_
);
4605 ovs_assert(!old_rule
->new_rule
);
4607 /* Take a reference to the new rule, and refer all stats updates from
4608 * the old rule to the new rule. */
4609 ofproto_rule_ref(&rule
->up
);
4611 ovs_mutex_lock(&old_rule
->stats_mutex
);
4612 ovs_mutex_lock(&rule
->stats_mutex
);
4613 old_rule
->new_rule
= rule
; /* Forward future stats. */
4614 old_rule
->forward_counts
= forward_counts
;
4616 if (forward_counts
) {
4617 rule
->stats
= old_rule
->stats
; /* Transfer stats to the new
4620 /* Used timestamp must be forwarded whenever a rule is modified. */
4621 rule
->stats
.used
= old_rule
->stats
.used
;
4623 ovs_mutex_unlock(&rule
->stats_mutex
);
4624 ovs_mutex_unlock(&old_rule
->stats_mutex
);
4631 rule_destruct(struct rule
*rule_
)
4632 OVS_NO_THREAD_SAFETY_ANALYSIS
4634 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4636 ovs_mutex_destroy(&rule
->stats_mutex
);
4637 /* Release reference to the new rule, if any. */
4638 if (rule
->new_rule
) {
4639 ofproto_rule_unref(&rule
->new_rule
->up
);
4641 if (rule
->recirc_id
) {
4642 recirc_free_id(rule
->recirc_id
);
4647 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
,
4648 long long int *used
)
4650 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4652 ovs_mutex_lock(&rule
->stats_mutex
);
4653 if (OVS_UNLIKELY(rule
->new_rule
)) {
4654 rule_get_stats(&rule
->new_rule
->up
, packets
, bytes
, used
);
4656 *packets
= rule
->stats
.n_packets
;
4657 *bytes
= rule
->stats
.n_bytes
;
4658 *used
= rule
->stats
.used
;
4660 ovs_mutex_unlock(&rule
->stats_mutex
);
4663 struct ofproto_dpif_packet_out
{
4664 struct xlate_cache xcache
;
4665 struct ofpbuf odp_actions
;
4666 struct recirc_refs rr
;
4671 static struct ofproto_dpif_packet_out
*
4672 ofproto_dpif_packet_out_new(void)
4674 struct ofproto_dpif_packet_out
*aux
= xmalloc(sizeof *aux
);
4675 xlate_cache_init(&aux
->xcache
);
4676 ofpbuf_init(&aux
->odp_actions
, 64);
4677 aux
->rr
= RECIRC_REFS_EMPTY_INITIALIZER
;
4678 aux
->needs_help
= false;
4684 ofproto_dpif_packet_out_delete(struct ofproto_dpif_packet_out
*aux
)
4687 xlate_cache_uninit(&aux
->xcache
);
4688 ofpbuf_uninit(&aux
->odp_actions
);
4689 recirc_refs_unref(&aux
->rr
);
4695 packet_xlate(struct ofproto
*ofproto_
, struct ofproto_packet_out
*opo
)
4696 OVS_REQUIRES(ofproto_mutex
)
4698 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4699 struct xlate_out xout
;
4700 struct xlate_in xin
;
4701 enum ofperr error
= 0;
4703 struct ofproto_dpif_packet_out
*aux
= ofproto_dpif_packet_out_new();
4705 xlate_in_init(&xin
, ofproto
, opo
->version
, opo
->flow
,
4706 opo
->flow
->in_port
.ofp_port
, NULL
, 0, opo
->packet
, NULL
,
4708 xin
.ofpacts
= opo
->ofpacts
;
4709 xin
.ofpacts_len
= opo
->ofpacts_len
;
4710 /* No learning or stats, but collect side effects to xcache. */
4711 xin
.allow_side_effects
= false;
4712 xin
.resubmit_stats
= NULL
;
4713 xin
.xcache
= &aux
->xcache
;
4714 xin
.in_packet_out
= true;
4716 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
4717 error
= OFPERR_OFPFMFC_UNKNOWN
; /* Error processing actions. */
4720 /* Prepare learn actions. */
4721 struct xc_entry
*entry
;
4722 struct ofpbuf entries
= aux
->xcache
.entries
;
4724 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4725 if (entry
->type
== XC_LEARN
) {
4726 struct ofproto_flow_mod
*ofm
= entry
->learn
.ofm
;
4728 error
= ofproto_flow_mod_learn_refresh(ofm
);
4732 struct rule
*rule
= ofm
->temp_rule
;
4733 ofm
->learn_adds_rule
= (rule
->state
== RULE_INITIALIZED
);
4734 if (ofm
->learn_adds_rule
) {
4735 /* If learning on a different bridge, must use its next
4736 * version number. */
4737 ofm
->version
= (rule
->ofproto
== ofproto_
)
4738 ? opo
->version
: rule
->ofproto
->tables_version
+ 1;
4739 error
= ofproto_flow_mod_learn_start(ofm
);
4748 aux
->needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
4749 recirc_refs_swap(&aux
->rr
, &xout
.recircs
); /* Hold recirc refs. */
4751 xlate_out_uninit(&xout
);
4756 xlate_out_uninit(&xout
);
4757 ofproto_dpif_packet_out_delete(aux
);
4763 packet_xlate_revert(struct ofproto
*ofproto OVS_UNUSED
,
4764 struct ofproto_packet_out
*opo
)
4765 OVS_REQUIRES(ofproto_mutex
)
4767 struct ofproto_dpif_packet_out
*aux
= opo
->aux
;
4770 /* Revert the learned flows. */
4771 struct xc_entry
*entry
;
4772 struct ofpbuf entries
= aux
->xcache
.entries
;
4774 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4775 if (entry
->type
== XC_LEARN
&& entry
->learn
.ofm
->learn_adds_rule
) {
4776 ofproto_flow_mod_learn_revert(entry
->learn
.ofm
);
4780 ofproto_dpif_packet_out_delete(aux
);
4784 /* Push stats and perform side effects of flow translation. */
4786 ofproto_dpif_xcache_execute(struct ofproto_dpif
*ofproto
,
4787 struct xlate_cache
*xcache
,
4788 struct dpif_flow_stats
*stats
)
4789 OVS_REQUIRES(ofproto_mutex
)
4791 struct xc_entry
*entry
;
4792 struct ofpbuf entries
= xcache
->entries
;
4794 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4795 switch (entry
->type
) {
4797 /* Finish the learned flows. */
4798 if (entry
->learn
.ofm
->learn_adds_rule
) {
4799 ofproto_flow_mod_learn_finish(entry
->learn
.ofm
, &ofproto
->up
);
4802 case XC_FIN_TIMEOUT
:
4803 if (stats
->tcp_flags
& (TCP_FIN
| TCP_RST
)) {
4804 /* 'ofproto_mutex' already held */
4805 ofproto_rule_reduce_timeouts__(&entry
->fin
.rule
->up
,
4810 /* All the rest can be dealt with by the xlate layer. */
4820 case XC_TUNNEL_HEADER
:
4821 xlate_push_stats_entry(entry
, stats
);
4830 packet_execute(struct ofproto
*ofproto_
, struct ofproto_packet_out
*opo
)
4831 OVS_REQUIRES(ofproto_mutex
)
4833 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4834 struct dpif_flow_stats stats
;
4835 struct dpif_execute execute
;
4837 struct ofproto_dpif_packet_out
*aux
= opo
->aux
;
4840 /* Run the side effects from the xcache. */
4841 dpif_flow_stats_extract(opo
->flow
, opo
->packet
, time_msec(), &stats
);
4842 ofproto_dpif_xcache_execute(ofproto
, &aux
->xcache
, &stats
);
4844 execute
.actions
= aux
->odp_actions
.data
;
4845 execute
.actions_len
= aux
->odp_actions
.size
;
4847 pkt_metadata_from_flow(&opo
->packet
->md
, opo
->flow
);
4848 execute
.packet
= opo
->packet
;
4849 execute
.flow
= opo
->flow
;
4850 execute
.needs_help
= aux
->needs_help
;
4851 execute
.probe
= false;
4854 /* Fix up in_port. */
4855 ofproto_dpif_set_packet_odp_port(ofproto
, opo
->flow
->in_port
.ofp_port
,
4858 dpif_execute(ofproto
->backer
->dpif
, &execute
);
4859 ofproto_dpif_packet_out_delete(aux
);
4863 static struct group_dpif
*group_dpif_cast(const struct ofgroup
*group
)
4865 return group
? CONTAINER_OF(group
, struct group_dpif
, up
) : NULL
;
4868 static struct ofgroup
*
4871 struct group_dpif
*group
= xzalloc(sizeof *group
);
4876 group_dealloc(struct ofgroup
*group_
)
4878 struct group_dpif
*group
= group_dpif_cast(group_
);
4883 group_construct_stats(struct group_dpif
*group
)
4884 OVS_REQUIRES(group
->stats_mutex
)
4886 group
->packet_count
= 0;
4887 group
->byte_count
= 0;
4889 struct ofputil_bucket
*bucket
;
4890 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4891 bucket
->stats
.packet_count
= 0;
4892 bucket
->stats
.byte_count
= 0;
4897 group_dpif_credit_stats(struct group_dpif
*group
,
4898 struct ofputil_bucket
*bucket
,
4899 const struct dpif_flow_stats
*stats
)
4901 ovs_mutex_lock(&group
->stats_mutex
);
4902 group
->packet_count
+= stats
->n_packets
;
4903 group
->byte_count
+= stats
->n_bytes
;
4905 bucket
->stats
.packet_count
+= stats
->n_packets
;
4906 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4907 } else { /* Credit to all buckets */
4908 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4909 bucket
->stats
.packet_count
+= stats
->n_packets
;
4910 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4913 ovs_mutex_unlock(&group
->stats_mutex
);
4916 /* Calculate the dp_hash mask needed to provide the least weighted bucket
4917 * with at least one hash value and construct a mapping table from masked
4918 * dp_hash value to group bucket using the Webster method.
4919 * If the caller specifies a non-zero max_hash value, abort and return false
4920 * if more hash values would be required. The absolute maximum number of
4921 * hash values supported is 256. */
4923 #define MAX_SELECT_GROUP_HASH_VALUES 256
4926 group_setup_dp_hash_table(struct group_dpif
*group
, size_t max_hash
)
4928 struct ofputil_bucket
*bucket
;
4929 uint32_t n_buckets
= group
->up
.n_buckets
;
4930 uint64_t total_weight
= 0;
4931 uint16_t min_weight
= UINT16_MAX
;
4933 struct ofputil_bucket
*bucket
;
4939 if (n_buckets
== 0) {
4940 VLOG_DBG(" Don't apply dp_hash method without buckets.");
4944 webster
= xcalloc(n_buckets
, sizeof(struct webster
));
4946 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4947 if (bucket
->weight
> 0 && bucket
->weight
< min_weight
) {
4948 min_weight
= bucket
->weight
;
4950 total_weight
+= bucket
->weight
;
4951 webster
[i
].bucket
= bucket
;
4952 webster
[i
].divisor
= 1;
4953 webster
[i
].value
= bucket
->weight
;
4954 webster
[i
].hits
= 0;
4958 if (total_weight
== 0) {
4959 VLOG_DBG(" Total weight is zero. No active buckets.");
4963 VLOG_DBG(" Minimum weight: %d, total weight: %"PRIu64
,
4964 min_weight
, total_weight
);
4966 uint64_t min_slots
= DIV_ROUND_UP(total_weight
, min_weight
);
4967 uint64_t min_slots2
= ROUND_UP_POW2(min_slots
);
4968 uint64_t n_hash
= MAX(16, min_slots2
);
4969 if (n_hash
> MAX_SELECT_GROUP_HASH_VALUES
||
4970 (max_hash
!= 0 && n_hash
> max_hash
)) {
4971 VLOG_DBG(" Too many hash values required: %"PRIu64
, n_hash
);
4976 VLOG_DBG(" Using %"PRIu64
" hash values:", n_hash
);
4977 group
->hash_mask
= n_hash
- 1;
4978 if (group
->hash_map
) {
4979 free(group
->hash_map
);
4981 group
->hash_map
= xcalloc(n_hash
, sizeof(struct ofputil_bucket
*));
4983 /* Use Webster method to distribute hash values over buckets. */
4984 for (int hash
= 0; hash
< n_hash
; hash
++) {
4985 struct webster
*winner
= &webster
[0];
4986 for (i
= 1; i
< n_buckets
; i
++) {
4987 if (webster
[i
].value
> winner
->value
) {
4988 winner
= &webster
[i
];
4992 winner
->divisor
+= 2;
4993 winner
->value
= (double) winner
->bucket
->weight
/ winner
->divisor
;
4994 group
->hash_map
[hash
] = winner
->bucket
;
4998 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4999 double target
= (n_hash
* bucket
->weight
) / (double) total_weight
;
5000 VLOG_DBG(" Bucket %d: weight=%d, target=%.2f hits=%d",
5001 bucket
->bucket_id
, bucket
->weight
,
5002 target
, webster
[i
].hits
);
5011 group_set_selection_method(struct group_dpif
*group
)
5013 const struct ofputil_group_props
*props
= &group
->up
.props
;
5014 const char *selection_method
= props
->selection_method
;
5016 VLOG_DBG("Constructing select group %"PRIu32
, group
->up
.group_id
);
5017 if (selection_method
[0] == '\0') {
5018 VLOG_DBG("No selection method specified. Trying dp_hash.");
5019 /* If the controller has not specified a selection method, check if
5020 * the dp_hash selection method with max 64 hash values is appropriate
5021 * for the given bucket configuration. */
5022 if (group_setup_dp_hash_table(group
, 64)) {
5023 /* Use dp_hash selection method with symmetric L4 hash. */
5024 group
->selection_method
= SEL_METHOD_DP_HASH
;
5025 group
->hash_alg
= OVS_HASH_ALG_SYM_L4
;
5026 group
->hash_basis
= 0;
5027 VLOG_DBG("Use dp_hash with %d hash values using algorithm %d.",
5028 group
->hash_mask
+ 1, group
->hash_alg
);
5030 /* Fall back to original default hashing in slow path. */
5031 VLOG_DBG("Falling back to default hash method.");
5032 group
->selection_method
= SEL_METHOD_DEFAULT
;
5034 } else if (!strcmp(selection_method
, "dp_hash")) {
5035 VLOG_DBG("Selection method specified: dp_hash.");
5036 /* Try to use dp_hash if possible at all. */
5037 if (group_setup_dp_hash_table(group
, 0)) {
5038 group
->selection_method
= SEL_METHOD_DP_HASH
;
5039 group
->hash_alg
= props
->selection_method_param
>> 32;
5040 if (group
->hash_alg
>= __OVS_HASH_MAX
) {
5041 VLOG_DBG("Invalid dp_hash algorithm %d. "
5042 "Defaulting to OVS_HASH_ALG_L4", group
->hash_alg
);
5043 group
->hash_alg
= OVS_HASH_ALG_L4
;
5045 group
->hash_basis
= (uint32_t) props
->selection_method_param
;
5046 VLOG_DBG("Use dp_hash with %d hash values using algorithm %d.",
5047 group
->hash_mask
+ 1, group
->hash_alg
);
5049 /* Fall back to original default hashing in slow path. */
5050 VLOG_DBG("Falling back to default hash method.");
5051 group
->selection_method
= SEL_METHOD_DEFAULT
;
5053 } else if (!strcmp(selection_method
, "hash")) {
5054 VLOG_DBG("Selection method specified: hash.");
5055 if (props
->fields
.values_size
> 0) {
5056 /* Controller has specified hash fields. */
5057 struct ds s
= DS_EMPTY_INITIALIZER
;
5058 oxm_format_field_array(&s
, &props
->fields
);
5059 VLOG_DBG("Hash fields: %s", ds_cstr(&s
));
5061 group
->selection_method
= SEL_METHOD_HASH
;
5063 /* No hash fields. Fall back to original default hashing. */
5064 VLOG_DBG("No hash fields. Falling back to default hash method.");
5065 group
->selection_method
= SEL_METHOD_DEFAULT
;
5068 /* Parsing of groups should ensure this never happens */
5074 group_construct(struct ofgroup
*group_
)
5076 struct group_dpif
*group
= group_dpif_cast(group_
);
5078 ovs_mutex_init_adaptive(&group
->stats_mutex
);
5079 ovs_mutex_lock(&group
->stats_mutex
);
5080 group_construct_stats(group
);
5081 group
->hash_map
= NULL
;
5082 if (group
->up
.type
== OFPGT11_SELECT
) {
5083 group_set_selection_method(group
);
5085 ovs_mutex_unlock(&group
->stats_mutex
);
5090 group_destruct(struct ofgroup
*group_
)
5092 struct group_dpif
*group
= group_dpif_cast(group_
);
5093 ovs_mutex_destroy(&group
->stats_mutex
);
5094 if (group
->hash_map
) {
5095 free(group
->hash_map
);
5096 group
->hash_map
= NULL
;
5101 group_get_stats(const struct ofgroup
*group_
, struct ofputil_group_stats
*ogs
)
5103 struct group_dpif
*group
= group_dpif_cast(group_
);
5105 ovs_mutex_lock(&group
->stats_mutex
);
5106 ogs
->packet_count
= group
->packet_count
;
5107 ogs
->byte_count
= group
->byte_count
;
5109 struct bucket_counter
*bucket_stats
= ogs
->bucket_stats
;
5110 struct ofputil_bucket
*bucket
;
5111 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
5112 bucket_stats
->packet_count
= bucket
->stats
.packet_count
;
5113 bucket_stats
->byte_count
= bucket
->stats
.byte_count
;
5116 ovs_mutex_unlock(&group
->stats_mutex
);
5121 /* If the group exists, this function increments the groups's reference count.
5123 * Make sure to call ofproto_group_unref() after no longer needing to maintain
5124 * a reference to the group. */
5126 group_dpif_lookup(struct ofproto_dpif
*ofproto
, uint32_t group_id
,
5127 ovs_version_t version
, bool take_ref
)
5129 struct ofgroup
*ofgroup
= ofproto_group_lookup(&ofproto
->up
, group_id
,
5131 return ofgroup
? group_dpif_cast(ofgroup
) : NULL
;
5134 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
5135 * supports a notion of an OAM flag, sets it if 'oam' is true.
5136 * May modify 'packet'.
5137 * Returns 0 if successful, otherwise a positive errno value. */
5139 ofproto_dpif_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
5140 struct dp_packet
*packet
)
5142 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
5145 error
= xlate_send_packet(ofport
, oam
, packet
);
5147 ovs_mutex_lock(&ofproto
->stats_mutex
);
5148 ofproto
->stats
.tx_packets
++;
5149 ofproto
->stats
.tx_bytes
+= dp_packet_size(packet
);
5150 ovs_mutex_unlock(&ofproto
->stats_mutex
);
5154 /* Return the version string of the datapath that backs up
5158 get_datapath_version(const struct ofproto
*ofproto_
)
5160 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5162 return ofproto
->backer
->dp_version_string
;
5166 type_set_config(const char *type
, const struct smap
*other_config
)
5168 struct dpif_backer
*backer
;
5170 backer
= shash_find_data(&all_dpif_backers
, type
);
5172 /* This is not necessarily a problem, since backers are only
5173 * created on demand. */
5177 dpif_set_config(backer
->dpif
, other_config
);
5181 ct_flush(const struct ofproto
*ofproto_
, const uint16_t *zone
)
5183 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5185 ct_dpif_flush(ofproto
->backer
->dpif
, zone
, NULL
);
5188 static struct ct_timeout_policy
*
5189 ct_timeout_policy_lookup(const struct hmap
*ct_tps
, struct simap
*tp
)
5191 struct ct_timeout_policy
*ct_tp
;
5193 HMAP_FOR_EACH_WITH_HASH (ct_tp
, node
, simap_hash(tp
), ct_tps
) {
5194 if (simap_equal(&ct_tp
->tp
, tp
)) {
5201 static struct ct_timeout_policy
*
5202 ct_timeout_policy_alloc__(void)
5204 struct ct_timeout_policy
*ct_tp
= xzalloc(sizeof *ct_tp
);
5205 simap_init(&ct_tp
->tp
);
5209 static struct ct_timeout_policy
*
5210 ct_timeout_policy_alloc(struct simap
*tp
, struct id_pool
*tp_ids
)
5212 struct simap_node
*node
;
5214 struct ct_timeout_policy
*ct_tp
= ct_timeout_policy_alloc__();
5215 SIMAP_FOR_EACH (node
, tp
) {
5216 simap_put(&ct_tp
->tp
, node
->name
, node
->data
);
5219 if (!id_pool_alloc_id(tp_ids
, &ct_tp
->tp_id
)) {
5220 VLOG_ERR_RL(&rl
, "failed to allocate timeout policy id.");
5221 simap_destroy(&ct_tp
->tp
);
5230 ct_timeout_policy_destroy__(struct ct_timeout_policy
*ct_tp
)
5232 simap_destroy(&ct_tp
->tp
);
5237 ct_timeout_policy_destroy(struct ct_timeout_policy
*ct_tp
,
5238 struct id_pool
*tp_ids
)
5240 id_pool_free_id(tp_ids
, ct_tp
->tp_id
);
5241 ovsrcu_postpone(ct_timeout_policy_destroy__
, ct_tp
);
5245 ct_timeout_policy_unref(struct dpif_backer
*backer
,
5246 struct ct_timeout_policy
*ct_tp
)
5251 if (!ct_tp
->ref_count
) {
5252 hmap_remove(&backer
->ct_tps
, &ct_tp
->node
);
5253 ovs_list_push_back(&backer
->ct_tp_kill_list
, &ct_tp
->list_node
);
5258 static struct ct_zone
*
5259 ct_zone_lookup(const struct cmap
*ct_zones
, uint16_t zone_id
)
5261 struct ct_zone
*ct_zone
;
5263 CMAP_FOR_EACH_WITH_HASH (ct_zone
, node
, hash_int(zone_id
, 0), ct_zones
) {
5264 if (ct_zone
->zone_id
== zone_id
) {
5271 static struct ct_zone
*
5272 ct_zone_alloc(uint16_t zone_id
)
5274 struct ct_zone
*ct_zone
= xzalloc(sizeof *ct_zone
);
5275 ct_zone
->zone_id
= zone_id
;
5280 ct_zone_destroy(struct ct_zone
*ct_zone
)
5282 ovsrcu_postpone(free
, ct_zone
);
5286 ct_zone_remove_and_destroy(struct dpif_backer
*backer
, struct ct_zone
*ct_zone
)
5288 cmap_remove(&backer
->ct_zones
, &ct_zone
->node
,
5289 hash_int(ct_zone
->zone_id
, 0));
5290 ct_zone_destroy(ct_zone
);
5294 ct_add_timeout_policy_to_dpif(struct dpif
*dpif
,
5295 struct ct_timeout_policy
*ct_tp
)
5297 struct ct_dpif_timeout_policy cdtp
;
5298 struct simap_node
*node
;
5300 cdtp
.id
= ct_tp
->tp_id
;
5301 SIMAP_FOR_EACH (node
, &ct_tp
->tp
) {
5302 ct_dpif_set_timeout_policy_attr_by_name(&cdtp
, node
->name
, node
->data
);
5305 int err
= ct_dpif_set_timeout_policy(dpif
, &cdtp
);
5307 VLOG_ERR_RL(&rl
, "failed to set timeout policy %"PRIu32
" (%s)",
5308 ct_tp
->tp_id
, ovs_strerror(err
));
5313 clear_existing_ct_timeout_policies(struct dpif_backer
*backer
)
5315 /* In kernel datapath, when OVS starts, there may be some pre-existing
5316 * timeout policies in the kernel. To avoid reassigning the same timeout
5317 * policy ids, we dump all the pre-existing timeout policies and keep
5318 * the ids in the pool. Since OVS will not use those timeout policies
5319 * for new datapath flow, we add them to the kill list and remove
5321 struct ct_dpif_timeout_policy cdtp
;
5324 if (ct_dpif_timeout_policy_dump_start(backer
->dpif
, &state
)) {
5328 while (!ct_dpif_timeout_policy_dump_next(backer
->dpif
, state
, &cdtp
)) {
5329 struct ct_timeout_policy
*ct_tp
= ct_timeout_policy_alloc__();
5330 ct_tp
->tp_id
= cdtp
.id
;
5331 id_pool_add(backer
->tp_ids
, cdtp
.id
);
5332 ovs_list_push_back(&backer
->ct_tp_kill_list
, &ct_tp
->list_node
);
5335 ct_dpif_timeout_policy_dump_done(backer
->dpif
, state
);
5338 #define MAX_TIMEOUT_POLICY_ID UINT32_MAX
5341 ct_zone_config_init(struct dpif_backer
*backer
)
5343 backer
->tp_ids
= id_pool_create(0, MAX_TIMEOUT_POLICY_ID
);
5344 cmap_init(&backer
->ct_zones
);
5345 hmap_init(&backer
->ct_tps
);
5346 ovs_list_init(&backer
->ct_tp_kill_list
);
5347 clear_existing_ct_timeout_policies(backer
);
5351 ct_zone_config_uninit(struct dpif_backer
*backer
)
5353 struct ct_zone
*ct_zone
;
5354 CMAP_FOR_EACH (ct_zone
, node
, &backer
->ct_zones
) {
5355 ct_zone_remove_and_destroy(backer
, ct_zone
);
5358 struct ct_timeout_policy
*ct_tp
;
5359 HMAP_FOR_EACH_POP (ct_tp
, node
, &backer
->ct_tps
) {
5360 ct_timeout_policy_destroy(ct_tp
, backer
->tp_ids
);
5363 LIST_FOR_EACH_POP (ct_tp
, list_node
, &backer
->ct_tp_kill_list
) {
5364 ct_timeout_policy_destroy(ct_tp
, backer
->tp_ids
);
5367 id_pool_destroy(backer
->tp_ids
);
5368 cmap_destroy(&backer
->ct_zones
);
5369 hmap_destroy(&backer
->ct_tps
);
5373 ct_zone_timeout_policy_sweep(struct dpif_backer
*backer
)
5375 if (!ovs_list_is_empty(&backer
->ct_tp_kill_list
)
5376 && time_msec() >= timeout_policy_cleanup_timer
) {
5377 struct ct_timeout_policy
*ct_tp
, *next
;
5379 LIST_FOR_EACH_SAFE (ct_tp
, next
, list_node
, &backer
->ct_tp_kill_list
) {
5380 if (!ct_dpif_del_timeout_policy(backer
->dpif
, ct_tp
->tp_id
)) {
5381 ovs_list_remove(&ct_tp
->list_node
);
5382 ct_timeout_policy_destroy(ct_tp
, backer
->tp_ids
);
5384 /* INFO log raised by 'dpif' layer. */
5387 timeout_policy_cleanup_timer
= time_msec() +
5388 TIMEOUT_POLICY_CLEANUP_INTERVAL
;
5393 ct_set_zone_timeout_policy(const char *datapath_type
, uint16_t zone_id
,
5394 struct simap
*timeout_policy
)
5396 struct dpif_backer
*backer
= shash_find_data(&all_dpif_backers
,
5402 struct ct_timeout_policy
*ct_tp
= ct_timeout_policy_lookup(&backer
->ct_tps
,
5405 ct_tp
= ct_timeout_policy_alloc(timeout_policy
, backer
->tp_ids
);
5407 hmap_insert(&backer
->ct_tps
, &ct_tp
->node
, simap_hash(&ct_tp
->tp
));
5408 ct_add_timeout_policy_to_dpif(backer
->dpif
, ct_tp
);
5414 struct ct_zone
*ct_zone
= ct_zone_lookup(&backer
->ct_zones
, zone_id
);
5416 if (ct_zone
->ct_tp
!= ct_tp
) {
5417 /* Update the zone timeout policy. */
5418 ct_timeout_policy_unref(backer
, ct_zone
->ct_tp
);
5419 ct_zone
->ct_tp
= ct_tp
;
5423 struct ct_zone
*new_ct_zone
= ct_zone_alloc(zone_id
);
5424 new_ct_zone
->ct_tp
= ct_tp
;
5425 cmap_insert(&backer
->ct_zones
, &new_ct_zone
->node
,
5426 hash_int(zone_id
, 0));
5432 ct_del_zone_timeout_policy(const char *datapath_type
, uint16_t zone_id
)
5434 struct dpif_backer
*backer
= shash_find_data(&all_dpif_backers
,
5440 struct ct_zone
*ct_zone
= ct_zone_lookup(&backer
->ct_zones
, zone_id
);
5442 ct_timeout_policy_unref(backer
, ct_zone
->ct_tp
);
5443 ct_zone_remove_and_destroy(backer
, ct_zone
);
5447 /* Gets timeout policy name in 'backer' based on 'zone', 'dl_type' and
5448 * 'nw_proto'. Returns true if the zone-based timeout policy is configured.
5449 * On success, stores the timeout policy name in 'tp_name', and sets
5450 * 'unwildcard' based on the dpif implementation. If 'unwildcard' is true,
5451 * the returned timeout policy is 'dl_type' and 'nw_proto' specific, and OVS
5452 * needs to unwildcard the datapath flow for this timeout policy in flow
5455 * The caller is responsible for freeing 'tp_name'. */
5457 ofproto_dpif_ct_zone_timeout_policy_get_name(
5458 const struct dpif_backer
*backer
, uint16_t zone
, uint16_t dl_type
,
5459 uint8_t nw_proto
, char **tp_name
, bool *unwildcard
)
5461 if (!ct_dpif_timeout_policy_support_ipproto(nw_proto
)) {
5465 struct ct_zone
*ct_zone
= ct_zone_lookup(&backer
->ct_zones
, zone
);
5471 if (ct_dpif_get_timeout_policy_name(backer
->dpif
,
5472 ct_zone
->ct_tp
->tp_id
, dl_type
,
5473 nw_proto
, tp_name
, &is_generic
)) {
5477 /* Unwildcard datapath flow if it is not a generic timeout policy. */
5478 *unwildcard
= !is_generic
;
5483 set_frag_handling(struct ofproto
*ofproto_
,
5484 enum ofputil_frag_handling frag_handling
)
5486 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5487 if (frag_handling
!= OFPUTIL_FRAG_REASM
) {
5488 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5496 nxt_resume(struct ofproto
*ofproto_
,
5497 const struct ofputil_packet_in_private
*pin
)
5499 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5500 struct dpif_flow_stats stats
;
5501 struct xlate_cache xcache
;
5503 xlate_cache_init(&xcache
);
5505 /* Translate pin into datapath actions. */
5506 uint64_t odp_actions_stub
[1024 / 8];
5507 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
5508 enum slow_path_reason slow
;
5509 enum ofperr error
= xlate_resume(ofproto
, pin
, &odp_actions
, &slow
,
5512 /* Steal 'pin->packet' and put it into a dp_packet. */
5513 struct dp_packet packet
;
5514 dp_packet_init(&packet
, pin
->base
.packet_len
);
5515 dp_packet_put(&packet
, pin
->base
.packet
, pin
->base
.packet_len
);
5517 /* Run the side effects from the xcache. */
5518 dpif_flow_stats_extract(&flow
, &packet
, time_msec(), &stats
);
5519 ovs_mutex_lock(&ofproto_mutex
);
5520 ofproto_dpif_xcache_execute(ofproto
, &xcache
, &stats
);
5521 ovs_mutex_unlock(&ofproto_mutex
);
5523 pkt_metadata_from_flow(&packet
.md
, &pin
->base
.flow_metadata
.flow
);
5525 /* Fix up in_port. */
5526 packet
.md
.in_port
.odp_port
= pin
->odp_port
;
5528 struct flow headers
;
5529 flow_extract(&packet
, &headers
);
5531 /* Execute the datapath actions on the packet. */
5532 struct dpif_execute execute
= {
5533 .actions
= odp_actions
.data
,
5534 .actions_len
= odp_actions
.size
,
5535 .needs_help
= (slow
& SLOW_ACTION
) != 0,
5539 dpif_execute(ofproto
->backer
->dpif
, &execute
);
5542 ofpbuf_uninit(&odp_actions
);
5543 dp_packet_uninit(&packet
);
5544 xlate_cache_uninit(&xcache
);
5552 set_netflow(struct ofproto
*ofproto_
,
5553 const struct netflow_options
*netflow_options
)
5555 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5557 if (netflow_options
) {
5558 if (!ofproto
->netflow
) {
5559 ofproto
->netflow
= netflow_create();
5560 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5562 return netflow_set_options(ofproto
->netflow
, netflow_options
);
5563 } else if (ofproto
->netflow
) {
5564 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5565 netflow_unref(ofproto
->netflow
);
5566 ofproto
->netflow
= NULL
;
5573 get_netflow_ids(const struct ofproto
*ofproto_
,
5574 uint8_t *engine_type
, uint8_t *engine_id
)
5576 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5578 dpif_get_netflow_ids(ofproto
->backer
->dpif
, engine_type
, engine_id
);
5581 struct ofproto_dpif
*
5582 ofproto_dpif_lookup_by_name(const char *name
)
5584 struct ofproto_dpif
*ofproto
;
5586 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_by_name_node
,
5587 hash_string(name
, 0),
5588 &all_ofproto_dpifs_by_name
) {
5589 if (!strcmp(ofproto
->up
.name
, name
)) {
5596 struct ofproto_dpif
*
5597 ofproto_dpif_lookup_by_uuid(const struct uuid
*uuid
)
5599 struct ofproto_dpif
*ofproto
;
5601 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_by_uuid_node
,
5602 uuid_hash(uuid
), &all_ofproto_dpifs_by_uuid
) {
5603 if (uuid_equals(&ofproto
->uuid
, uuid
)) {
5611 ofproto_unixctl_fdb_flush(struct unixctl_conn
*conn
, int argc
,
5612 const char *argv
[], void *aux OVS_UNUSED
)
5614 struct ofproto_dpif
*ofproto
;
5617 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5619 unixctl_command_reply_error(conn
, "no such bridge");
5622 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5623 mac_learning_flush(ofproto
->ml
);
5624 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5626 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5627 &all_ofproto_dpifs_by_name
) {
5628 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5629 mac_learning_flush(ofproto
->ml
);
5630 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5634 unixctl_command_reply(conn
, "table successfully flushed");
5638 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn
*conn
, int argc
,
5639 const char *argv
[], void *aux OVS_UNUSED
)
5641 struct ofproto_dpif
*ofproto
;
5644 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5646 unixctl_command_reply_error(conn
, "no such bridge");
5650 if (!mcast_snooping_enabled(ofproto
->ms
)) {
5651 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
5654 mcast_snooping_mdb_flush(ofproto
->ms
);
5656 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5657 &all_ofproto_dpifs_by_name
) {
5658 if (!mcast_snooping_enabled(ofproto
->ms
)) {
5661 mcast_snooping_mdb_flush(ofproto
->ms
);
5665 unixctl_command_reply(conn
, "table successfully flushed");
5668 static struct ofport_dpif
*
5669 ofbundle_get_a_port(const struct ofbundle
*bundle
)
5671 return CONTAINER_OF(ovs_list_front(&bundle
->ports
), struct ofport_dpif
,
5676 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5677 const char *argv
[], void *aux OVS_UNUSED
)
5679 struct ds ds
= DS_EMPTY_INITIALIZER
;
5680 const struct ofproto_dpif
*ofproto
;
5681 const struct mac_entry
*e
;
5683 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5685 unixctl_command_reply_error(conn
, "no such bridge");
5689 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
5690 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
5691 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
5692 struct ofbundle
*bundle
= mac_entry_get_port(ofproto
->ml
, e
);
5693 char name
[OFP_MAX_PORT_NAME_LEN
];
5695 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
5696 NULL
, name
, sizeof name
);
5697 ds_put_format(&ds
, "%5s %4d "ETH_ADDR_FMT
" %3d\n",
5698 name
, e
->vlan
, ETH_ADDR_ARGS(e
->mac
),
5699 mac_entry_age(ofproto
->ml
, e
));
5701 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5702 unixctl_command_reply(conn
, ds_cstr(&ds
));
5707 ofproto_unixctl_fdb_stats_clear(struct unixctl_conn
*conn
, int argc
,
5708 const char *argv
[], void *aux OVS_UNUSED
)
5710 struct ofproto_dpif
*ofproto
;
5713 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5715 unixctl_command_reply_error(conn
, "no such bridge");
5718 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5719 mac_learning_clear_statistics(ofproto
->ml
);
5720 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5722 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5723 &all_ofproto_dpifs_by_name
) {
5724 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5725 mac_learning_clear_statistics(ofproto
->ml
);
5726 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5730 unixctl_command_reply(conn
, "statistics successfully cleared");
5734 ofproto_unixctl_fdb_stats_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5735 const char *argv
[], void *aux OVS_UNUSED
)
5737 struct ds ds
= DS_EMPTY_INITIALIZER
;
5738 const struct ofproto_dpif
*ofproto
;
5739 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5741 unixctl_command_reply_error(conn
, "no such bridge");
5745 ds_put_format(&ds
, "Statistics for bridge \"%s\":\n", argv
[1]);
5746 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
5748 ds_put_format(&ds
, " Current/maximum MAC entries in the table: %"
5749 PRIuSIZE
"/%"PRIuSIZE
"\n",
5750 hmap_count(&ofproto
->ml
->table
), ofproto
->ml
->max_entries
);
5752 " Total number of learned MAC entries : %"PRIu64
"\n",
5753 ofproto
->ml
->total_learned
);
5755 " Total number of expired MAC entries : %"PRIu64
"\n",
5756 ofproto
->ml
->total_expired
);
5758 " Total number of evicted MAC entries : %"PRIu64
"\n",
5759 ofproto
->ml
->total_evicted
);
5761 " Total number of port moved MAC entries : %"PRIu64
"\n",
5762 ofproto
->ml
->total_moved
);
5764 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5765 unixctl_command_reply(conn
, ds_cstr(&ds
));
5770 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn
*conn
,
5771 int argc OVS_UNUSED
,
5773 void *aux OVS_UNUSED
)
5775 struct ds ds
= DS_EMPTY_INITIALIZER
;
5776 const struct ofproto_dpif
*ofproto
;
5777 const struct ofbundle
*bundle
;
5778 const struct mcast_group
*grp
;
5779 struct mcast_group_bundle
*b
;
5780 struct mcast_mrouter_bundle
*mrouter
;
5782 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5784 unixctl_command_reply_error(conn
, "no such bridge");
5788 if (!mcast_snooping_enabled(ofproto
->ms
)) {
5789 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
5793 ds_put_cstr(&ds
, " port VLAN GROUP Age\n");
5794 ovs_rwlock_rdlock(&ofproto
->ms
->rwlock
);
5795 LIST_FOR_EACH (grp
, group_node
, &ofproto
->ms
->group_lru
) {
5796 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
5797 char name
[OFP_MAX_PORT_NAME_LEN
];
5800 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
5801 NULL
, name
, sizeof name
);
5802 ds_put_format(&ds
, "%5s %4d ", name
, grp
->vlan
);
5803 ipv6_format_mapped(&grp
->addr
, &ds
);
5804 ds_put_format(&ds
, " %3d\n",
5805 mcast_bundle_age(ofproto
->ms
, b
));
5809 /* ports connected to multicast routers */
5810 LIST_FOR_EACH(mrouter
, mrouter_node
, &ofproto
->ms
->mrouter_lru
) {
5811 char name
[OFP_MAX_PORT_NAME_LEN
];
5813 bundle
= mrouter
->port
;
5814 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
5815 NULL
, name
, sizeof name
);
5816 ds_put_format(&ds
, "%5s %4d querier %3d\n",
5817 name
, mrouter
->vlan
,
5818 mcast_mrouter_age(ofproto
->ms
, mrouter
));
5820 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
5821 unixctl_command_reply(conn
, ds_cstr(&ds
));
5825 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5826 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5827 * to destroy 'ofproto_shash' and free the returned value. */
5828 static const struct shash_node
**
5829 get_ofprotos(struct shash
*ofproto_shash
)
5831 const struct ofproto_dpif
*ofproto
;
5833 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5834 &all_ofproto_dpifs_by_name
) {
5835 char *name
= xasprintf("%s@%s", ofproto
->up
.type
, ofproto
->up
.name
);
5836 shash_add_nocopy(ofproto_shash
, name
, ofproto
);
5839 return shash_sort(ofproto_shash
);
5843 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5844 const char *argv
[] OVS_UNUSED
,
5845 void *aux OVS_UNUSED
)
5847 struct ds ds
= DS_EMPTY_INITIALIZER
;
5848 struct shash ofproto_shash
;
5849 const struct shash_node
**sorted_ofprotos
;
5852 shash_init(&ofproto_shash
);
5853 sorted_ofprotos
= get_ofprotos(&ofproto_shash
);
5854 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
5855 const struct shash_node
*node
= sorted_ofprotos
[i
];
5856 ds_put_format(&ds
, "%s\n", node
->name
);
5859 shash_destroy(&ofproto_shash
);
5860 free(sorted_ofprotos
);
5862 unixctl_command_reply(conn
, ds_cstr(&ds
));
5867 show_dp_feature_bool(struct ds
*ds
, const char *feature
, bool b
)
5869 ds_put_format(ds
, "%s: %s\n", feature
, b
? "Yes" : "No");
5873 show_dp_feature_size_t(struct ds
*ds
, const char *feature
, size_t s
)
5875 ds_put_format(ds
, "%s: %"PRIuSIZE
"\n", feature
, s
);
5878 enum dpif_support_field_type
{
5879 DPIF_SUPPORT_FIELD_bool
,
5880 DPIF_SUPPORT_FIELD_size_t
,
5883 struct dpif_support_field
{
5884 void *rt_ptr
; /* Points to the 'rt_support' field. */
5885 const void *bt_ptr
; /* Points to the 'bt_support' field. */
5887 enum dpif_support_field_type type
;
5890 #define DPIF_SUPPORT_FIELD_INTIALIZER(RT_PTR, BT_PTR, TITLE, TYPE) \
5891 (struct dpif_support_field) {RT_PTR, BT_PTR, TITLE, TYPE}
5894 dpif_show_support(const struct dpif_backer_support
*support
, struct ds
*ds
)
5896 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5897 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5899 #undef DPIF_SUPPORT_FIELD
5901 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5902 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5904 #undef ODP_SUPPORT_FIELD
5908 display_support_field(const char *name
,
5909 const struct dpif_support_field
*field
,
5912 switch (field
->type
) {
5913 case DPIF_SUPPORT_FIELD_bool
: {
5914 bool v
= *(bool *)field
->rt_ptr
;
5915 bool b
= *(bool *)field
->bt_ptr
;
5916 ds_put_format(ds
, "%s (%s) : [run time]:%s, [boot time]:%s\n", name
,
5917 field
->title
, v
? "true" : "false",
5918 b
? "true" : "false");
5921 case DPIF_SUPPORT_FIELD_size_t
:
5922 ds_put_format(ds
, "%s (%s) : [run time]:%"PRIuSIZE
5923 ", [boot time]:%"PRIuSIZE
"\n", name
,
5924 field
->title
, *(size_t *)field
->rt_ptr
,
5925 *(size_t *)field
->bt_ptr
);
5932 /* Set a field of 'rt_support' to a new value.
5934 * Returns 'true' if the value is actually set. */
5936 dpif_set_support(struct dpif_backer_support
*rt_support
,
5937 struct dpif_backer_support
*bt_support
,
5938 const char *name
, const char *value
, struct ds
*ds
)
5940 struct shash all_fields
= SHASH_INITIALIZER(&all_fields
);
5941 struct dpif_support_field
*field
;
5942 struct shash_node
*node
;
5943 bool changed
= false;
5945 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5947 struct dpif_support_field *f = xmalloc(sizeof *f); \
5948 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->NAME, \
5949 &bt_support->NAME, \
5951 DPIF_SUPPORT_FIELD_##TYPE);\
5952 shash_add_once(&all_fields, #NAME, f); \
5954 DPIF_SUPPORT_FIELDS
;
5955 #undef DPIF_SUPPORT_FIELD
5957 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5959 struct dpif_support_field *f = xmalloc(sizeof *f); \
5960 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->odp.NAME, \
5961 &bt_support->odp.NAME, \
5963 DPIF_SUPPORT_FIELD_##TYPE);\
5964 shash_add_once(&all_fields, #NAME, f); \
5967 #undef ODP_SUPPORT_FIELD
5970 SHASH_FOR_EACH (node
, &all_fields
) {
5971 display_support_field(node
->name
, node
->data
, ds
);
5976 node
= shash_find(&all_fields
, name
);
5978 ds_put_cstr(ds
, "Unexpected support field");
5984 display_support_field(node
->name
, field
, ds
);
5988 if (field
->type
== DPIF_SUPPORT_FIELD_bool
) {
5989 if (!strcasecmp(value
, "true")) {
5990 if (*(bool *)field
->bt_ptr
) {
5991 *(bool *)field
->rt_ptr
= true;
5994 ds_put_cstr(ds
, "Can not enable features not supported by the datapth");
5996 } else if (!strcasecmp(value
, "false")) {
5997 *(bool *)field
->rt_ptr
= false;
6000 ds_put_cstr(ds
, "Boolean value expected");
6002 } else if (field
->type
== DPIF_SUPPORT_FIELD_size_t
) {
6004 if (str_to_int(value
, 10, &v
)) {
6006 if (v
<= *(size_t *)field
->bt_ptr
) {
6007 *(size_t *)field
->rt_ptr
= v
;
6010 ds_put_cstr(ds
, "Can not set value beyond the datapath capability");
6013 ds_put_format(ds
, "Negative number not expected");
6016 ds_put_cstr(ds
, "Integer number expected");
6021 shash_destroy_free_data(&all_fields
);
6026 dpif_show_backer(const struct dpif_backer
*backer
, struct ds
*ds
)
6028 const struct shash_node
**ofprotos
;
6029 struct dpif_dp_stats dp_stats
;
6030 struct shash ofproto_shash
;
6033 dpif_get_dp_stats(backer
->dpif
, &dp_stats
);
6034 ds_put_format(ds
, "%s: hit:%"PRIu64
" missed:%"PRIu64
"\n",
6035 dpif_name(backer
->dpif
), dp_stats
.n_hit
, dp_stats
.n_missed
);
6037 shash_init(&ofproto_shash
);
6038 ofprotos
= get_ofprotos(&ofproto_shash
);
6039 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
6040 struct ofproto_dpif
*ofproto
= ofprotos
[i
]->data
;
6041 const struct shash_node
**ports
;
6044 if (ofproto
->backer
!= backer
) {
6048 ds_put_format(ds
, " %s:\n", ofproto
->up
.name
);
6050 ports
= shash_sort(&ofproto
->up
.port_by_name
);
6051 for (j
= 0; j
< shash_count(&ofproto
->up
.port_by_name
); j
++) {
6052 const struct shash_node
*node
= ports
[j
];
6053 struct ofport
*ofport
= node
->data
;
6055 odp_port_t odp_port
;
6057 ds_put_format(ds
, " %s %u/", netdev_get_name(ofport
->netdev
),
6060 odp_port
= ofp_port_to_odp_port(ofproto
, ofport
->ofp_port
);
6061 if (odp_port
!= ODPP_NONE
) {
6062 ds_put_format(ds
, "%"PRIu32
":", odp_port
);
6064 ds_put_cstr(ds
, "none:");
6067 ds_put_format(ds
, " (%s", netdev_get_type(ofport
->netdev
));
6070 if (!netdev_get_config(ofport
->netdev
, &config
)) {
6071 const struct smap_node
**nodes
= smap_sort(&config
);
6072 for (size_t k
= 0; k
< smap_count(&config
); k
++) {
6073 ds_put_format(ds
, "%c %s=%s", k
? ',' : ':',
6074 nodes
[k
]->key
, nodes
[k
]->value
);
6078 smap_destroy(&config
);
6080 ds_put_char(ds
, ')');
6081 ds_put_char(ds
, '\n');
6085 shash_destroy(&ofproto_shash
);
6090 ofproto_unixctl_dpif_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
6091 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
6093 struct ds ds
= DS_EMPTY_INITIALIZER
;
6094 const struct shash_node
**backers
;
6097 backers
= shash_sort(&all_dpif_backers
);
6098 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
6099 dpif_show_backer(backers
[i
]->data
, &ds
);
6103 unixctl_command_reply(conn
, ds_cstr(&ds
));
6108 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn
*conn
,
6109 int argc OVS_UNUSED
, const char *argv
[],
6110 void *aux OVS_UNUSED
)
6112 const struct ofproto_dpif
*ofproto
;
6114 struct ds ds
= DS_EMPTY_INITIALIZER
;
6116 struct dpif_flow_dump
*flow_dump
;
6117 struct dpif_flow_dump_thread
*flow_dump_thread
;
6121 ofproto
= ofproto_dpif_lookup_by_name(argv
[argc
- 1]);
6123 unixctl_command_reply_error(conn
, "no such bridge");
6127 bool verbosity
= false;
6129 bool set_names
= false;
6130 for (int i
= 1; i
< argc
- 1; i
++) {
6131 if (!strcmp(argv
[i
], "-m")) {
6133 } else if (!strcmp(argv
[i
], "--names")) {
6136 } else if (!strcmp(argv
[i
], "--no-names")) {
6145 struct hmap
*portno_names
= NULL
;
6147 portno_names
= xmalloc(sizeof *portno_names
);
6148 hmap_init(portno_names
);
6150 struct dpif_port dpif_port
;
6151 struct dpif_port_dump port_dump
;
6152 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, ofproto
->backer
->dpif
) {
6153 odp_portno_names_set(portno_names
, dpif_port
.port_no
,
6159 flow_dump
= dpif_flow_dump_create(ofproto
->backer
->dpif
, false, NULL
);
6160 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
6161 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
6164 if ((odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
, NULL
)
6166 || (xlate_lookup_ofproto(ofproto
->backer
, &flow
, NULL
, NULL
)
6172 odp_format_ufid(&f
.ufid
, &ds
);
6173 ds_put_cstr(&ds
, " ");
6175 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
6176 portno_names
, &ds
, verbosity
);
6177 ds_put_cstr(&ds
, ", ");
6178 dpif_flow_stats_format(&f
.stats
, &ds
);
6179 ds_put_cstr(&ds
, ", actions:");
6180 format_odp_actions(&ds
, f
.actions
, f
.actions_len
, portno_names
);
6181 ds_put_char(&ds
, '\n');
6183 dpif_flow_dump_thread_destroy(flow_dump_thread
);
6184 error
= dpif_flow_dump_destroy(flow_dump
);
6188 ds_put_format(&ds
, "dpif/dump_flows failed: %s", ovs_strerror(errno
));
6189 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
6191 unixctl_command_reply(conn
, ds_cstr(&ds
));
6194 odp_portno_names_destroy(portno_names
);
6195 hmap_destroy(portno_names
);
6202 ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn
*conn
,
6203 int argc
, const char *argv
[],
6204 void *aux OVS_UNUSED
)
6206 struct ds ds
= DS_EMPTY_INITIALIZER
;
6207 const char *br
= argv
[argc
-1];
6208 struct ofproto_dpif
*ofproto
= ofproto_dpif_lookup_by_name(br
);
6211 unixctl_command_reply_error(conn
, "no such bridge");
6215 dpif_show_support(&ofproto
->backer
->bt_support
, &ds
);
6216 unixctl_command_reply(conn
, ds_cstr(&ds
));
6220 ofproto_unixctl_dpif_set_dp_features(struct unixctl_conn
*conn
,
6221 int argc
, const char *argv
[],
6222 void *aux OVS_UNUSED
)
6224 struct ds ds
= DS_EMPTY_INITIALIZER
;
6225 const char *br
= argv
[1];
6226 const char *name
, *value
;
6227 struct ofproto_dpif
*ofproto
= ofproto_dpif_lookup_by_name(br
);
6231 unixctl_command_reply_error(conn
, "no such bridge");
6235 name
= argc
> 2 ? argv
[2] : NULL
;
6236 value
= argc
> 3 ? argv
[3] : NULL
;
6237 changed
= dpif_set_support(&ofproto
->backer
->rt_support
,
6238 &ofproto
->backer
->bt_support
,
6241 xlate_set_support(ofproto
, &ofproto
->backer
->rt_support
);
6242 udpif_flush(ofproto
->backer
->udpif
);
6244 unixctl_command_reply(conn
, ds_cstr(&ds
));
6249 ofproto_unixctl_init(void)
6251 static bool registered
;
6257 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
6258 ofproto_unixctl_fdb_flush
, NULL
);
6259 unixctl_command_register("fdb/show", "bridge", 1, 1,
6260 ofproto_unixctl_fdb_show
, NULL
);
6261 unixctl_command_register("fdb/stats-clear", "[bridge]", 0, 1,
6262 ofproto_unixctl_fdb_stats_clear
, NULL
);
6263 unixctl_command_register("fdb/stats-show", "bridge", 1, 1,
6264 ofproto_unixctl_fdb_stats_show
, NULL
);
6265 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
6266 ofproto_unixctl_mcast_snooping_flush
, NULL
);
6267 unixctl_command_register("mdb/show", "bridge", 1, 1,
6268 ofproto_unixctl_mcast_snooping_show
, NULL
);
6269 unixctl_command_register("dpif/dump-dps", "", 0, 0,
6270 ofproto_unixctl_dpif_dump_dps
, NULL
);
6271 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show
,
6273 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
6274 ofproto_unixctl_dpif_show_dp_features
, NULL
);
6275 unixctl_command_register("dpif/dump-flows",
6276 "[-m] [--names | --no-names] bridge", 1, INT_MAX
,
6277 ofproto_unixctl_dpif_dump_flows
, NULL
);
6278 unixctl_command_register("dpif/set-dp-features", "bridge", 1, 3 ,
6279 ofproto_unixctl_dpif_set_dp_features
, NULL
);
6283 ofp_port_to_odp_port(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
6285 const struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
6286 return ofport
? ofport
->odp_port
: ODPP_NONE
;
6289 struct ofport_dpif
*
6290 odp_port_to_ofport(const struct dpif_backer
*backer
, odp_port_t odp_port
)
6292 struct ofport_dpif
*port
;
6294 ovs_rwlock_rdlock(&backer
->odp_to_ofport_lock
);
6295 HMAP_FOR_EACH_IN_BUCKET (port
, odp_port_node
, hash_odp_port(odp_port
),
6296 &backer
->odp_to_ofport_map
) {
6297 if (port
->odp_port
== odp_port
) {
6298 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
6303 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
6308 odp_port_to_ofp_port(const struct ofproto_dpif
*ofproto
, odp_port_t odp_port
)
6310 struct ofport_dpif
*port
;
6312 port
= odp_port_to_ofport(ofproto
->backer
, odp_port
);
6313 if (port
&& &ofproto
->up
== port
->up
.ofproto
) {
6314 return port
->up
.ofp_port
;
6320 /* 'match' is non-const to allow for temporary modifications. Any changes are
6321 * restored before returning. */
6323 ofproto_dpif_add_internal_flow(struct ofproto_dpif
*ofproto
,
6324 struct match
*match
, int priority
,
6325 uint16_t idle_timeout
,
6326 const struct ofpbuf
*ofpacts
,
6327 struct rule
**rulep
)
6329 struct ofputil_flow_mod fm
;
6330 struct rule_dpif
*rule
;
6333 fm
= (struct ofputil_flow_mod
) {
6334 .buffer_id
= UINT32_MAX
,
6335 .priority
= priority
,
6336 .table_id
= TBL_INTERNAL
,
6337 .command
= OFPFC_ADD
,
6338 .idle_timeout
= idle_timeout
,
6339 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
6340 .ofpacts
= ofpacts
->data
,
6341 .ofpacts_len
= ofpacts
->size
,
6343 minimatch_init(&fm
.match
, match
);
6344 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
6345 minimatch_destroy(&fm
.match
);
6348 VLOG_ERR_RL(&rl
, "failed to add internal flow (%s)",
6349 ofperr_to_string(error
));
6354 rule
= rule_dpif_lookup_in_table(ofproto
,
6355 ofproto_dpif_get_tables_version(ofproto
),
6356 TBL_INTERNAL
, &match
->flow
, &match
->wc
);
6366 ofproto_dpif_delete_internal_flow(struct ofproto_dpif
*ofproto
,
6367 struct match
*match
, int priority
)
6369 struct ofputil_flow_mod fm
;
6372 fm
= (struct ofputil_flow_mod
) {
6373 .buffer_id
= UINT32_MAX
,
6374 .priority
= priority
,
6375 .table_id
= TBL_INTERNAL
,
6376 .out_port
= OFPP_ANY
,
6377 .out_group
= OFPG_ANY
,
6378 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
6379 .command
= OFPFC_DELETE_STRICT
,
6381 minimatch_init(&fm
.match
, match
);
6382 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
6383 minimatch_destroy(&fm
.match
);
6386 VLOG_ERR_RL(&rl
, "failed to delete internal flow (%s)",
6387 ofperr_to_string(error
));
6395 meter_get_features(const struct ofproto
*ofproto_
,
6396 struct ofputil_meter_features
*features
)
6398 const struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
6400 dpif_meter_get_features(ofproto
->backer
->dpif
, features
);
6404 meter_set(struct ofproto
*ofproto_
, ofproto_meter_id
*meter_id
,
6405 struct ofputil_meter_config
*config
)
6407 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
6409 /* Provider ID unknown. Use backer to allocate a new DP meter */
6410 if (meter_id
->uint32
== UINT32_MAX
) {
6411 if (!ofproto
->backer
->meter_ids
) {
6412 return OFPERR_OFPMMFC_OUT_OF_METERS
; /* Meters not supported. */
6415 if(!id_pool_alloc_id(ofproto
->backer
->meter_ids
, &meter_id
->uint32
)) {
6416 return OFPERR_OFPMMFC_OUT_OF_METERS
; /* Can't allocate meter. */
6420 switch (dpif_meter_set(ofproto
->backer
->dpif
, *meter_id
, config
)) {
6423 case EFBIG
: /* meter_id out of range */
6424 case ENOMEM
: /* Cannot allocate meter */
6425 return OFPERR_OFPMMFC_OUT_OF_METERS
;
6426 case EBADF
: /* Unsupported flags */
6427 return OFPERR_OFPMMFC_BAD_FLAGS
;
6428 case EINVAL
: /* Too many bands */
6429 return OFPERR_OFPMMFC_OUT_OF_BANDS
;
6430 case ENODEV
: /* Unsupported band type */
6431 return OFPERR_OFPMMFC_BAD_BAND
;
6432 case EDOM
: /* Rate must be non-zero */
6433 return OFPERR_OFPMMFC_BAD_RATE
;
6435 return OFPERR_OFPMMFC_UNKNOWN
;
6440 meter_get(const struct ofproto
*ofproto_
, ofproto_meter_id meter_id
,
6441 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
6443 const struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
6445 if (!dpif_meter_get(ofproto
->backer
->dpif
, meter_id
, stats
, n_bands
)) {
6448 return OFPERR_OFPMMFC_UNKNOWN_METER
;
6451 struct free_meter_id_args
{
6452 struct ofproto_dpif
*ofproto
;
6453 ofproto_meter_id meter_id
;
6457 free_meter_id(struct free_meter_id_args
*args
)
6459 struct ofproto_dpif
*ofproto
= args
->ofproto
;
6461 dpif_meter_del(ofproto
->backer
->dpif
, args
->meter_id
, NULL
, 0);
6462 id_pool_free_id(ofproto
->backer
->meter_ids
, args
->meter_id
.uint32
);
6467 meter_del(struct ofproto
*ofproto_
, ofproto_meter_id meter_id
)
6469 struct free_meter_id_args
*arg
= xmalloc(sizeof *arg
);
6471 /* Before a meter can be deleted, Openflow spec requires all rules
6472 * referring to the meter to be (automatically) removed before the
6473 * meter is deleted. However, since vswitchd is multi-threaded,
6474 * those rules and their actions remain accessible by other threads,
6475 * especially by the handler and revalidator threads.
6476 * Postpone meter deletion after RCU grace period, so that ongoing
6477 * upcall translation or flow revalidation can complete. */
6478 arg
->ofproto
= ofproto_dpif_cast(ofproto_
);
6479 arg
->meter_id
= meter_id
;
6480 ovsrcu_postpone(free_meter_id
, arg
);
6483 const struct ofproto_class ofproto_dpif_class
= {
6497 NULL
, /* get_memory_usage. */
6498 type_get_memory_usage
,
6501 NULL
, /* modify_tables */
6520 port_is_lacp_current
,
6521 port_get_lacp_stats
,
6522 NULL
, /* rule_choose_table */
6526 NULL
, /* rule_delete */
6531 packet_xlate_revert
,
6549 aa_vlan_get_queue_size
,
6556 get_stp_port_status
,
6561 get_rstp_port_status
,
6568 is_mirror_output_bundle
,
6569 forward_bpdu_changed
,
6570 set_mac_table_config
,
6572 set_mcast_snooping_port
,
6577 group_alloc
, /* group_alloc */
6578 group_construct
, /* group_construct */
6579 group_destruct
, /* group_destruct */
6580 group_dealloc
, /* group_dealloc */
6581 NULL
, /* group_modify */
6582 group_get_stats
, /* group_get_stats */
6583 get_datapath_version
, /* get_datapath_version */
6585 ct_flush
, /* ct_flush */
6586 ct_set_zone_timeout_policy
,
6587 ct_del_zone_timeout_policy
,