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 bool use_priority_tags
; /* Use 802.1p tag for frames in VLAN 0? */
106 bool protected; /* Protected port mode */
109 bool floodable
; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
112 static void bundle_remove(struct ofport
*);
113 static void bundle_update(struct ofbundle
*);
114 static void bundle_destroy(struct ofbundle
*);
115 static void bundle_del_port(struct ofport_dpif
*);
116 static void bundle_run(struct ofbundle
*);
117 static void bundle_wait(struct ofbundle
*);
118 static void bundle_flush_macs(struct ofbundle
*, bool);
119 static void bundle_move(struct ofbundle
*, struct ofbundle
*);
121 static void stp_run(struct ofproto_dpif
*ofproto
);
122 static void stp_wait(struct ofproto_dpif
*ofproto
);
123 static int set_stp_port(struct ofport
*,
124 const struct ofproto_port_stp_settings
*);
126 static void rstp_run(struct ofproto_dpif
*ofproto
);
127 static void set_rstp_port(struct ofport
*,
128 const struct ofproto_port_rstp_settings
*);
131 struct hmap_node odp_port_node
; /* In dpif_backer's "odp_to_ofport_map". */
135 struct ofbundle
*bundle
; /* Bundle that contains this port, if any. */
136 struct ovs_list bundle_node
;/* In struct ofbundle's "ports" list. */
137 struct cfm
*cfm
; /* Connectivity Fault Management, if any. */
138 struct bfd
*bfd
; /* BFD, if any. */
139 struct lldp
*lldp
; /* lldp, if any. */
140 bool is_tunnel
; /* This port is a tunnel. */
141 long long int carrier_seq
; /* Carrier status changes. */
142 struct ofport_dpif
*peer
; /* Peer if patch port. */
145 struct stp_port
*stp_port
; /* Spanning Tree Protocol, if any. */
146 enum stp_state stp_state
; /* Always STP_DISABLED if STP not in use. */
147 long long int stp_state_entered
;
149 /* Rapid Spanning Tree. */
150 struct rstp_port
*rstp_port
; /* Rapid Spanning Tree Protocol, if any. */
151 enum rstp_state rstp_state
; /* Always RSTP_DISABLED if RSTP not in use. */
153 /* Queue to DSCP mapping. */
154 struct ofproto_port_queue
*qdscp
;
158 static odp_port_t
ofp_port_to_odp_port(const struct ofproto_dpif
*,
161 static ofp_port_t
odp_port_to_ofp_port(const struct ofproto_dpif
*,
164 static struct ofport_dpif
*
165 ofport_dpif_cast(const struct ofport
*ofport
)
167 return ofport
? CONTAINER_OF(ofport
, struct ofport_dpif
, up
) : NULL
;
170 static void port_run(struct ofport_dpif
*);
171 static int set_bfd(struct ofport
*, const struct smap
*);
172 static int set_cfm(struct ofport
*, const struct cfm_settings
*);
173 static int set_lldp(struct ofport
*ofport_
, const struct smap
*cfg
);
174 static void ofport_update_peer(struct ofport_dpif
*);
176 COVERAGE_DEFINE(rev_reconfigure
);
177 COVERAGE_DEFINE(rev_stp
);
178 COVERAGE_DEFINE(rev_rstp
);
179 COVERAGE_DEFINE(rev_bond
);
180 COVERAGE_DEFINE(rev_port_toggled
);
181 COVERAGE_DEFINE(rev_flow_table
);
182 COVERAGE_DEFINE(rev_mac_learning
);
183 COVERAGE_DEFINE(rev_mcast_snooping
);
185 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
186 struct shash all_dpif_backers
= SHASH_INITIALIZER(&all_dpif_backers
);
188 /* All existing ofproto_dpif instances, indexed by ->up.name. */
189 static struct hmap all_ofproto_dpifs_by_name
=
190 HMAP_INITIALIZER(&all_ofproto_dpifs_by_name
);
192 /* All existing ofproto_dpif instances, indexed by ->uuid. */
193 static struct hmap all_ofproto_dpifs_by_uuid
=
194 HMAP_INITIALIZER(&all_ofproto_dpifs_by_uuid
);
196 static bool ofproto_use_tnl_push_pop
= true;
197 static void ofproto_unixctl_init(void);
199 static inline struct ofproto_dpif
*
200 ofproto_dpif_cast(const struct ofproto
*ofproto
)
202 ovs_assert(ofproto
->ofproto_class
== &ofproto_dpif_class
);
203 return CONTAINER_OF(ofproto
, struct ofproto_dpif
, up
);
206 /* Global variables. */
207 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
209 /* Initial mappings of port to bridge mappings. */
210 static struct shash init_ofp_ports
= SHASH_INITIALIZER(&init_ofp_ports
);
212 /* Initialize 'ofm' for a learn action. If the rule already existed, reference
213 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
214 * rule reference in both cases. */
216 ofproto_dpif_flow_mod_init_for_learn(struct ofproto_dpif
*ofproto
,
217 const struct ofputil_flow_mod
*fm
,
218 struct ofproto_flow_mod
*ofm
)
220 /* This will not take the global 'ofproto_mutex'. */
221 return ofproto_flow_mod_init_for_learn(&ofproto
->up
, fm
, ofm
);
224 /* Appends 'am' to the queue of asynchronous messages to be sent to the
225 * controller. Takes ownership of 'am' and any data it points to. */
227 ofproto_dpif_send_async_msg(struct ofproto_dpif
*ofproto
,
228 struct ofproto_async_msg
*am
)
230 if (!guarded_list_push_back(&ofproto
->ams
, &am
->list_node
, 1024)) {
231 COVERAGE_INC(packet_in_overflow
);
232 ofproto_async_msg_free(am
);
235 /* Wakes up main thread for packet-in I/O. */
236 seq_change(ofproto
->ams_seq
);
239 /* Factory functions. */
242 init(const struct shash
*iface_hints
)
244 struct shash_node
*node
;
246 /* Make a local copy, since we don't own 'iface_hints' elements. */
247 SHASH_FOR_EACH(node
, iface_hints
) {
248 const struct iface_hint
*orig_hint
= node
->data
;
249 struct iface_hint
*new_hint
= xmalloc(sizeof *new_hint
);
251 new_hint
->br_name
= xstrdup(orig_hint
->br_name
);
252 new_hint
->br_type
= xstrdup(orig_hint
->br_type
);
253 new_hint
->ofp_port
= orig_hint
->ofp_port
;
255 shash_add(&init_ofp_ports
, node
->name
, new_hint
);
258 ofproto_unixctl_init();
259 ofproto_dpif_trace_init();
264 enumerate_types(struct sset
*types
)
266 dp_enumerate_types(types
);
270 enumerate_names(const char *type
, struct sset
*names
)
272 struct ofproto_dpif
*ofproto
;
275 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
276 &all_ofproto_dpifs_by_name
) {
277 if (strcmp(type
, ofproto
->up
.type
)) {
280 sset_add(names
, ofproto
->up
.name
);
287 del(const char *type
, const char *name
)
292 error
= dpif_open(name
, type
, &dpif
);
294 error
= dpif_delete(dpif
);
301 port_open_type(const char *datapath_type
, const char *port_type
)
303 return dpif_port_open_type(datapath_type
, port_type
);
306 /* Type functions. */
308 static void process_dpif_port_changes(struct dpif_backer
*);
309 static void process_dpif_all_ports_changed(struct dpif_backer
*);
310 static void process_dpif_port_change(struct dpif_backer
*,
311 const char *devname
);
312 static void process_dpif_port_error(struct dpif_backer
*, int error
);
314 static struct ofproto_dpif
*
315 lookup_ofproto_dpif_by_port_name(const char *name
)
317 struct ofproto_dpif
*ofproto
;
319 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
320 &all_ofproto_dpifs_by_name
) {
321 if (sset_contains(&ofproto
->ports
, name
)) {
330 type_run(const char *type
)
332 struct dpif_backer
*backer
;
334 backer
= shash_find_data(&all_dpif_backers
, type
);
336 /* This is not necessarily a problem, since backers are only
337 * created on demand. */
341 if (dpif_run(backer
->dpif
)) {
342 backer
->need_revalidate
= REV_RECONFIGURE
;
345 udpif_run(backer
->udpif
);
347 /* If vswitchd started with other_config:flow_restore_wait set as "true",
348 * and the configuration has now changed to "false", enable receiving
349 * packets from the datapath. */
350 if (!backer
->recv_set_enable
&& !ofproto_get_flow_restore_wait()) {
353 backer
->recv_set_enable
= true;
355 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
357 VLOG_ERR("Failed to enable receiving packets in dpif.");
360 dpif_flow_flush(backer
->dpif
);
361 backer
->need_revalidate
= REV_RECONFIGURE
;
364 if (backer
->recv_set_enable
) {
365 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
368 if (backer
->need_revalidate
) {
369 struct ofproto_dpif
*ofproto
;
370 struct simap_node
*node
;
371 struct simap tmp_backers
;
373 /* Handle tunnel garbage collection. */
374 simap_init(&tmp_backers
);
375 simap_swap(&backer
->tnl_backers
, &tmp_backers
);
377 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
378 &all_ofproto_dpifs_by_name
) {
379 struct ofport_dpif
*iter
;
381 if (backer
!= ofproto
->backer
) {
385 HMAP_FOR_EACH (iter
, up
.hmap_node
, &ofproto
->up
.ports
) {
386 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
388 odp_port_t old_odp_port
;
390 if (!iter
->is_tunnel
) {
394 dp_port
= netdev_vport_get_dpif_port(iter
->up
.netdev
,
395 namebuf
, sizeof namebuf
);
396 old_odp_port
= iter
->odp_port
;
397 node
= simap_find(&tmp_backers
, dp_port
);
399 simap_put(&backer
->tnl_backers
, dp_port
, node
->data
);
400 simap_delete(&tmp_backers
, node
);
401 node
= simap_find(&backer
->tnl_backers
, dp_port
);
403 node
= simap_find(&backer
->tnl_backers
, dp_port
);
405 odp_port_t odp_port
= ODPP_NONE
;
407 if (!dpif_port_add(backer
->dpif
, iter
->up
.netdev
,
409 simap_put(&backer
->tnl_backers
, dp_port
,
410 odp_to_u32(odp_port
));
411 node
= simap_find(&backer
->tnl_backers
, dp_port
);
416 iter
->odp_port
= node
? u32_to_odp(node
->data
) : ODPP_NONE
;
417 if (tnl_port_reconfigure(iter
, iter
->up
.netdev
,
418 iter
->odp_port
, old_odp_port
,
419 ovs_native_tunneling_is_on(ofproto
), dp_port
)) {
420 backer
->need_revalidate
= REV_RECONFIGURE
;
425 SIMAP_FOR_EACH (node
, &tmp_backers
) {
426 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
), false);
428 simap_destroy(&tmp_backers
);
430 switch (backer
->need_revalidate
) {
431 case REV_RECONFIGURE
: COVERAGE_INC(rev_reconfigure
); break;
432 case REV_STP
: COVERAGE_INC(rev_stp
); break;
433 case REV_RSTP
: COVERAGE_INC(rev_rstp
); break;
434 case REV_BOND
: COVERAGE_INC(rev_bond
); break;
435 case REV_PORT_TOGGLED
: COVERAGE_INC(rev_port_toggled
); break;
436 case REV_FLOW_TABLE
: COVERAGE_INC(rev_flow_table
); break;
437 case REV_MAC_LEARNING
: COVERAGE_INC(rev_mac_learning
); break;
438 case REV_MCAST_SNOOPING
: COVERAGE_INC(rev_mcast_snooping
); break;
440 backer
->need_revalidate
= 0;
443 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
444 &all_ofproto_dpifs_by_name
) {
445 struct ofport_dpif
*ofport
;
446 struct ofbundle
*bundle
;
448 if (ofproto
->backer
!= backer
) {
452 xlate_ofproto_set(ofproto
, ofproto
->up
.name
,
453 ofproto
->backer
->dpif
, ofproto
->ml
,
454 ofproto
->stp
, ofproto
->rstp
, ofproto
->ms
,
455 ofproto
->mbridge
, ofproto
->sflow
, ofproto
->ipfix
,
457 ofproto
->up
.forward_bpdu
,
458 connmgr_has_in_band(ofproto
->up
.connmgr
),
459 &ofproto
->backer
->rt_support
);
461 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
462 xlate_bundle_set(ofproto
, bundle
, bundle
->name
,
463 bundle
->vlan_mode
, bundle
->qinq_ethtype
,
464 bundle
->vlan
, bundle
->trunks
, bundle
->cvlans
,
465 bundle
->use_priority_tags
,
466 bundle
->bond
, bundle
->lacp
,
467 bundle
->floodable
, bundle
->protected);
470 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
471 int stp_port
= ofport
->stp_port
472 ? stp_port_no(ofport
->stp_port
)
474 xlate_ofport_set(ofproto
, ofport
->bundle
, ofport
,
475 ofport
->up
.ofp_port
, ofport
->odp_port
,
476 ofport
->up
.netdev
, ofport
->cfm
, ofport
->bfd
,
477 ofport
->lldp
, ofport
->peer
, stp_port
,
478 ofport
->rstp_port
, ofport
->qdscp
,
479 ofport
->n_qdscp
, ofport
->up
.pp
.config
,
480 ofport
->up
.pp
.state
, ofport
->is_tunnel
,
481 ofport
->up
.may_enable
);
486 udpif_revalidate(backer
->udpif
);
489 process_dpif_port_changes(backer
);
494 /* Check for and handle port changes in 'backer''s dpif. */
496 process_dpif_port_changes(struct dpif_backer
*backer
)
502 error
= dpif_port_poll(backer
->dpif
, &devname
);
508 process_dpif_all_ports_changed(backer
);
512 process_dpif_port_change(backer
, devname
);
517 process_dpif_port_error(backer
, error
);
524 process_dpif_all_ports_changed(struct dpif_backer
*backer
)
526 struct ofproto_dpif
*ofproto
;
527 struct dpif_port dpif_port
;
528 struct dpif_port_dump dump
;
529 struct sset devnames
;
532 sset_init(&devnames
);
533 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
534 &all_ofproto_dpifs_by_name
) {
535 if (ofproto
->backer
== backer
) {
536 struct ofport
*ofport
;
538 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
539 sset_add(&devnames
, netdev_get_name(ofport
->netdev
));
543 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, backer
->dpif
) {
544 sset_add(&devnames
, dpif_port
.name
);
547 SSET_FOR_EACH (devname
, &devnames
) {
548 process_dpif_port_change(backer
, devname
);
550 sset_destroy(&devnames
);
554 process_dpif_port_change(struct dpif_backer
*backer
, const char *devname
)
556 struct ofproto_dpif
*ofproto
;
557 struct dpif_port port
;
559 /* Don't report on the datapath's device. */
560 if (!strcmp(devname
, dpif_base_name(backer
->dpif
))) {
564 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
565 &all_ofproto_dpifs_by_name
) {
566 if (simap_contains(&ofproto
->backer
->tnl_backers
, devname
)) {
571 ofproto
= lookup_ofproto_dpif_by_port_name(devname
);
572 if (dpif_port_query_by_name(backer
->dpif
, devname
, &port
)) {
573 /* The port was removed. If we know the datapath,
574 * report it through poll_set(). If we don't, it may be
575 * notifying us of a removal we initiated, so ignore it.
576 * If there's a pending ENOBUFS, let it stand, since
577 * everything will be reevaluated. */
578 if (ofproto
&& ofproto
->port_poll_errno
!= ENOBUFS
) {
579 sset_add(&ofproto
->port_poll_set
, devname
);
580 ofproto
->port_poll_errno
= 0;
582 } else if (!ofproto
) {
583 /* The port was added, but we don't know with which
584 * ofproto we should associate it. Delete it. */
585 dpif_port_del(backer
->dpif
, port
.port_no
, false);
587 struct ofport_dpif
*ofport
;
589 ofport
= ofport_dpif_cast(shash_find_data(
590 &ofproto
->up
.port_by_name
, devname
));
592 && ofport
->odp_port
!= port
.port_no
593 && !odp_port_to_ofport(backer
, port
.port_no
))
595 /* 'ofport''s datapath port number has changed from
596 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
597 * structures to match. */
598 ovs_rwlock_wrlock(&backer
->odp_to_ofport_lock
);
599 hmap_remove(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
);
600 ofport
->odp_port
= port
.port_no
;
601 hmap_insert(&backer
->odp_to_ofport_map
, &ofport
->odp_port_node
,
602 hash_odp_port(port
.port_no
));
603 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
604 backer
->need_revalidate
= REV_RECONFIGURE
;
607 dpif_port_destroy(&port
);
610 /* Propagate 'error' to all ofprotos based on 'backer'. */
612 process_dpif_port_error(struct dpif_backer
*backer
, int error
)
614 struct ofproto_dpif
*ofproto
;
616 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
617 &all_ofproto_dpifs_by_name
) {
618 if (ofproto
->backer
== backer
) {
619 sset_clear(&ofproto
->port_poll_set
);
620 ofproto
->port_poll_errno
= error
;
626 type_wait(const char *type
)
628 struct dpif_backer
*backer
;
630 backer
= shash_find_data(&all_dpif_backers
, type
);
632 /* This is not necessarily a problem, since backers are only
633 * created on demand. */
637 dpif_wait(backer
->dpif
);
640 /* Basic life-cycle. */
642 static int add_internal_flows(struct ofproto_dpif
*);
644 static struct ofproto
*
647 struct ofproto_dpif
*ofproto
= xzalloc(sizeof *ofproto
);
652 dealloc(struct ofproto
*ofproto_
)
654 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
659 close_dpif_backer(struct dpif_backer
*backer
, bool del
)
661 struct simap_node
*node
;
663 ovs_assert(backer
->refcount
> 0);
665 if (--backer
->refcount
) {
669 udpif_destroy(backer
->udpif
);
671 SIMAP_FOR_EACH (node
, &backer
->tnl_backers
) {
672 dpif_port_del(backer
->dpif
, u32_to_odp(node
->data
), false);
674 simap_destroy(&backer
->tnl_backers
);
675 ovs_rwlock_destroy(&backer
->odp_to_ofport_lock
);
676 hmap_destroy(&backer
->odp_to_ofport_map
);
677 shash_find_and_delete(&all_dpif_backers
, backer
->type
);
679 free(backer
->dp_version_string
);
681 dpif_delete(backer
->dpif
);
683 dpif_close(backer
->dpif
);
684 id_pool_destroy(backer
->meter_ids
);
688 /* Datapath port slated for removal from datapath. */
690 struct ovs_list list_node
;
694 static void check_support(struct dpif_backer
*backer
);
697 open_dpif_backer(const char *type
, struct dpif_backer
**backerp
)
699 struct dpif_backer
*backer
;
700 struct dpif_port_dump port_dump
;
701 struct dpif_port port
;
702 struct shash_node
*node
;
703 struct ovs_list garbage_list
;
704 struct odp_garbage
*garbage
;
711 backer
= shash_find_data(&all_dpif_backers
, type
);
718 backer_name
= xasprintf("ovs-%s", type
);
720 /* Remove any existing datapaths, since we assume we're the only
721 * userspace controlling the datapath. */
723 dp_enumerate_names(type
, &names
);
724 SSET_FOR_EACH(name
, &names
) {
725 struct dpif
*old_dpif
;
727 /* Don't remove our backer if it exists. */
728 if (!strcmp(name
, backer_name
)) {
732 if (dpif_open(name
, type
, &old_dpif
)) {
733 VLOG_WARN("couldn't open old datapath %s to remove it", name
);
735 dpif_delete(old_dpif
);
736 dpif_close(old_dpif
);
739 sset_destroy(&names
);
741 backer
= xmalloc(sizeof *backer
);
743 error
= dpif_create_and_open(backer_name
, type
, &backer
->dpif
);
746 VLOG_ERR("failed to open datapath of type %s: %s", type
,
747 ovs_strerror(error
));
751 backer
->udpif
= udpif_create(backer
, backer
->dpif
);
753 backer
->type
= xstrdup(type
);
754 backer
->refcount
= 1;
755 hmap_init(&backer
->odp_to_ofport_map
);
756 ovs_rwlock_init(&backer
->odp_to_ofport_lock
);
757 backer
->need_revalidate
= 0;
758 simap_init(&backer
->tnl_backers
);
759 backer
->recv_set_enable
= !ofproto_get_flow_restore_wait();
762 if (backer
->recv_set_enable
) {
763 dpif_flow_flush(backer
->dpif
);
766 /* Loop through the ports already on the datapath and remove any
767 * that we don't need anymore. */
768 ovs_list_init(&garbage_list
);
769 dpif_port_dump_start(&port_dump
, backer
->dpif
);
770 while (dpif_port_dump_next(&port_dump
, &port
)) {
771 node
= shash_find(&init_ofp_ports
, port
.name
);
772 if (!node
&& strcmp(port
.name
, dpif_base_name(backer
->dpif
))) {
773 garbage
= xmalloc(sizeof *garbage
);
774 garbage
->odp_port
= port
.port_no
;
775 ovs_list_push_front(&garbage_list
, &garbage
->list_node
);
778 dpif_port_dump_done(&port_dump
);
780 LIST_FOR_EACH_POP (garbage
, list_node
, &garbage_list
) {
781 dpif_port_del(backer
->dpif
, garbage
->odp_port
, false);
785 shash_add(&all_dpif_backers
, type
, backer
);
787 check_support(backer
);
788 atomic_count_init(&backer
->tnl_count
, 0);
790 error
= dpif_recv_set(backer
->dpif
, backer
->recv_set_enable
);
792 VLOG_ERR("failed to listen on datapath of type %s: %s",
793 type
, ovs_strerror(error
));
794 close_dpif_backer(backer
, false);
798 if (backer
->recv_set_enable
) {
799 udpif_set_threads(backer
->udpif
, n_handlers
, n_revalidators
);
802 backer
->dp_version_string
= dpif_get_dp_version(backer
->dpif
);
804 /* Manage Datapath meter IDs if supported. */
805 struct ofputil_meter_features features
;
806 dpif_meter_get_features(backer
->dpif
, &features
);
807 if (features
.max_meters
) {
808 backer
->meter_ids
= id_pool_create(0, features
.max_meters
);
810 backer
->meter_ids
= NULL
;
813 /* Make a pristine snapshot of 'support' into 'boottime_support'.
814 * 'boottime_support' can be checked to prevent 'support' to be changed
815 * beyond the datapath capabilities. In case 'support' is changed by
816 * the user, 'boottime_support' can be used to restore it. */
817 backer
->bt_support
= backer
->rt_support
;
823 ovs_native_tunneling_is_on(struct ofproto_dpif
*ofproto
)
825 return ofproto_use_tnl_push_pop
826 && ofproto
->backer
->rt_support
.tnl_push_pop
827 && atomic_count_get(&ofproto
->backer
->tnl_count
);
830 /* Tests whether 'backer''s datapath supports recirculation. Only newer
831 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
832 * features on older datapaths that don't support this feature.
834 * Returns false if 'backer' definitely does not support recirculation, true if
835 * it seems to support recirculation or if at least the error we get is
838 check_recirc(struct dpif_backer
*backer
)
841 struct odputil_keybuf keybuf
;
844 struct odp_flow_key_parms odp_parms
= {
851 memset(&flow
, 0, sizeof flow
);
855 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
856 odp_flow_key_from_flow(&odp_parms
, &key
);
857 enable_recirc
= dpif_probe_feature(backer
->dpif
, "recirculation", &key
,
861 VLOG_INFO("%s: Datapath supports recirculation",
862 dpif_name(backer
->dpif
));
864 VLOG_INFO("%s: Datapath does not support recirculation",
865 dpif_name(backer
->dpif
));
868 return enable_recirc
;
871 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
872 * some flow attributes for datapaths that support this feature.
874 * Returns true if 'dpif' supports UFID for flow operations.
875 * Returns false if 'dpif' does not support UFID. */
877 check_ufid(struct dpif_backer
*backer
)
880 struct odputil_keybuf keybuf
;
884 struct odp_flow_key_parms odp_parms
= {
888 memset(&flow
, 0, sizeof flow
);
889 flow
.dl_type
= htons(0x1234);
891 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
892 odp_flow_key_from_flow(&odp_parms
, &key
);
893 dpif_flow_hash(backer
->dpif
, key
.data
, key
.size
, &ufid
);
895 enable_ufid
= dpif_probe_feature(backer
->dpif
, "UFID", &key
, NULL
, &ufid
);
898 VLOG_INFO("%s: Datapath supports unique flow ids",
899 dpif_name(backer
->dpif
));
901 VLOG_INFO("%s: Datapath does not support unique flow ids",
902 dpif_name(backer
->dpif
));
907 /* Tests number of 802.1q VLAN headers supported by 'backer''s datapath.
909 * Returns the number of elements in a struct flow's vlan
910 * if the datapath supports at least that many VLAN headers. */
912 check_max_vlan_headers(struct dpif_backer
*backer
)
915 struct odp_flow_key_parms odp_parms
= {
921 memset(&flow
, 0, sizeof flow
);
922 flow
.dl_type
= htons(ETH_TYPE_IP
);
923 for (n
= 0; n
< FLOW_MAX_VLAN_HEADERS
; n
++) {
924 struct odputil_keybuf keybuf
;
927 flow_push_vlan_uninit(&flow
, NULL
);
928 flow
.vlans
[0].tpid
= htons(ETH_TYPE_VLAN
);
929 flow
.vlans
[0].tci
= htons(1) | htons(VLAN_CFI
);
931 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
932 odp_flow_key_from_flow(&odp_parms
, &key
);
933 if (!dpif_probe_feature(backer
->dpif
, "VLAN", &key
, NULL
, NULL
)) {
938 VLOG_INFO("%s: VLAN header stack length probed as %d",
939 dpif_name(backer
->dpif
), n
);
942 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
944 * Returns the number of elements in a struct flow's mpls_lse field
945 * if the datapath supports at least that many entries in an
947 * Otherwise returns the number of MPLS push actions supported by
950 check_max_mpls_depth(struct dpif_backer
*backer
)
955 for (n
= 0; n
< FLOW_MAX_MPLS_LABELS
; n
++) {
956 struct odputil_keybuf keybuf
;
958 struct odp_flow_key_parms odp_parms
= {
962 memset(&flow
, 0, sizeof flow
);
963 flow
.dl_type
= htons(ETH_TYPE_MPLS
);
964 flow_set_mpls_bos(&flow
, n
, 1);
966 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
967 odp_flow_key_from_flow(&odp_parms
, &key
);
968 if (!dpif_probe_feature(backer
->dpif
, "MPLS", &key
, NULL
, NULL
)) {
973 VLOG_INFO("%s: MPLS label stack length probed as %d",
974 dpif_name(backer
->dpif
), n
);
979 add_sample_actions(struct ofpbuf
*actions
, int nesting
)
982 nl_msg_put_odp_port(actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
986 size_t start
, actions_start
;
988 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
989 actions_start
= nl_msg_start_nested(actions
, OVS_SAMPLE_ATTR_ACTIONS
);
990 add_sample_actions(actions
, nesting
- 1);
991 nl_msg_end_nested(actions
, actions_start
);
992 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
, UINT32_MAX
);
993 nl_msg_end_nested(actions
, start
);
996 /* Tests the nested sample actions levels supported by 'backer''s datapath.
998 * Returns the number of nested sample actions accepted by the datapath. */
1000 check_max_sample_nesting(struct dpif_backer
*backer
)
1002 struct odputil_keybuf keybuf
;
1007 struct odp_flow_key_parms odp_parms
= {
1011 memset(&flow
, 0, sizeof flow
);
1012 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1013 odp_flow_key_from_flow(&odp_parms
, &key
);
1015 /* OVS datapath has always supported at least 3 nested levels. */
1016 for (n
= 3; n
< FLOW_MAX_SAMPLE_NESTING
; n
++) {
1017 struct ofpbuf actions
;
1020 ofpbuf_init(&actions
, 300);
1021 add_sample_actions(&actions
, n
);
1022 ok
= dpif_probe_feature(backer
->dpif
, "Sample action nesting", &key
,
1024 ofpbuf_uninit(&actions
);
1030 VLOG_INFO("%s: Max sample nesting level probed as %d",
1031 dpif_name(backer
->dpif
), n
);
1035 /* Tests whether 'backer''s datapath supports masked data in
1036 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1037 * datapaths that don't support this feature. */
1039 check_masked_set_action(struct dpif_backer
*backer
)
1041 struct eth_header
*eth
;
1042 struct ofpbuf actions
;
1043 struct dpif_execute execute
;
1044 struct dp_packet packet
;
1047 struct ovs_key_ethernet key
, mask
;
1049 /* Compose a set action that will cause an EINVAL error on older
1050 * datapaths that don't support masked set actions.
1051 * Avoid using a full mask, as it could be translated to a non-masked
1052 * set action instead. */
1053 ofpbuf_init(&actions
, 64);
1054 memset(&key
, 0x53, sizeof key
);
1055 memset(&mask
, 0x7f, sizeof mask
);
1056 commit_masked_set_action(&actions
, OVS_KEY_ATTR_ETHERNET
, &key
, &mask
,
1059 /* Compose a dummy ethernet packet. */
1060 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1061 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1062 eth
->eth_type
= htons(0x1234);
1064 flow_extract(&packet
, &flow
);
1066 /* Execute the actions. On older datapaths this fails with EINVAL, on
1067 * newer datapaths it succeeds. */
1068 execute
.actions
= actions
.data
;
1069 execute
.actions_len
= actions
.size
;
1070 execute
.packet
= &packet
;
1071 execute
.flow
= &flow
;
1072 execute
.needs_help
= false;
1073 execute
.probe
= true;
1076 error
= dpif_execute(backer
->dpif
, &execute
);
1078 dp_packet_uninit(&packet
);
1079 ofpbuf_uninit(&actions
);
1082 /* Masked set action is not supported. */
1083 VLOG_INFO("%s: datapath does not support masked set action feature.",
1084 dpif_name(backer
->dpif
));
1089 /* Tests whether 'backer''s datapath supports truncation of a packet in
1090 * OVS_ACTION_ATTR_TRUNC. We need to disable some features on older
1091 * datapaths that don't support this feature. */
1093 check_trunc_action(struct dpif_backer
*backer
)
1095 struct eth_header
*eth
;
1096 struct ofpbuf actions
;
1097 struct dpif_execute execute
;
1098 struct dp_packet packet
;
1099 struct ovs_action_trunc
*trunc
;
1103 /* Compose an action with output(port:1,
1104 * max_len:OVS_ACTION_OUTPUT_MIN + 1).
1105 * This translates to one truncate action and one output action. */
1106 ofpbuf_init(&actions
, 64);
1107 trunc
= nl_msg_put_unspec_uninit(&actions
,
1108 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1110 trunc
->max_len
= ETH_HEADER_LEN
+ 1;
1111 nl_msg_put_odp_port(&actions
, OVS_ACTION_ATTR_OUTPUT
, u32_to_odp(1));
1113 /* Compose a dummy Ethernet packet. */
1114 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1115 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1116 eth
->eth_type
= htons(0x1234);
1118 flow_extract(&packet
, &flow
);
1120 /* Execute the actions. On older datapaths this fails with EINVAL, on
1121 * newer datapaths it succeeds. */
1122 execute
.actions
= actions
.data
;
1123 execute
.actions_len
= actions
.size
;
1124 execute
.packet
= &packet
;
1125 execute
.flow
= &flow
;
1126 execute
.needs_help
= false;
1127 execute
.probe
= true;
1130 error
= dpif_execute(backer
->dpif
, &execute
);
1132 dp_packet_uninit(&packet
);
1133 ofpbuf_uninit(&actions
);
1136 VLOG_INFO("%s: Datapath does not support truncate action",
1137 dpif_name(backer
->dpif
));
1139 VLOG_INFO("%s: Datapath supports truncate action",
1140 dpif_name(backer
->dpif
));
1146 /* Tests whether 'backer''s datapath supports the clone action
1147 * OVS_ACTION_ATTR_CLONE. */
1149 check_clone(struct dpif_backer
*backer
)
1151 struct dpif_execute execute
;
1152 struct eth_header
*eth
;
1154 struct dp_packet packet
;
1155 struct ofpbuf actions
;
1159 /* Compose clone with an empty action list.
1160 * and check if datapath can decode the message. */
1161 ofpbuf_init(&actions
, 64);
1162 clone_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CLONE
);
1163 nl_msg_end_nested(&actions
, clone_start
);
1165 /* Compose a dummy Ethernet packet. */
1166 dp_packet_init(&packet
, ETH_HEADER_LEN
);
1167 eth
= dp_packet_put_zeros(&packet
, ETH_HEADER_LEN
);
1168 eth
->eth_type
= htons(0x1234);
1170 flow_extract(&packet
, &flow
);
1172 /* Execute the actions. On older datapaths this fails with EINVAL, on
1173 * newer datapaths it succeeds. */
1174 execute
.actions
= actions
.data
;
1175 execute
.actions_len
= actions
.size
;
1176 execute
.packet
= &packet
;
1177 execute
.flow
= &flow
;
1178 execute
.needs_help
= false;
1179 execute
.probe
= true;
1182 error
= dpif_execute(backer
->dpif
, &execute
);
1184 dp_packet_uninit(&packet
);
1185 ofpbuf_uninit(&actions
);
1188 VLOG_INFO("%s: Datapath does not support clone action",
1189 dpif_name(backer
->dpif
));
1191 VLOG_INFO("%s: Datapath supports clone action",
1192 dpif_name(backer
->dpif
));
1198 /* Tests whether 'backer''s datapath supports the OVS_CT_ATTR_EVENTMASK
1199 * attribute in OVS_ACTION_ATTR_CT. */
1201 check_ct_eventmask(struct dpif_backer
*backer
)
1203 struct dpif_execute execute
;
1204 struct dp_packet packet
;
1205 struct ofpbuf actions
;
1206 struct flow flow
= {
1207 .dl_type
= CONSTANT_HTONS(ETH_TYPE_IP
),
1208 .nw_proto
= IPPROTO_UDP
,
1210 /* Use the broadcast address on the loopback address range 127/8 to
1211 * avoid hitting any real conntrack entries. We leave the UDP ports to
1212 * zeroes for the same purpose. */
1213 .nw_src
= CONSTANT_HTONL(0x7fffffff),
1214 .nw_dst
= CONSTANT_HTONL(0x7fffffff),
1219 /* Compose CT action with eventmask attribute and check if datapath can
1220 * decode the message. */
1221 ofpbuf_init(&actions
, 64);
1222 ct_start
= nl_msg_start_nested(&actions
, OVS_ACTION_ATTR_CT
);
1223 /* Eventmask has no effect without the commit flag, but currently the
1224 * datapath will accept an eventmask even without commit. This is useful
1225 * as we do not want to persist the probe connection in the conntrack
1227 nl_msg_put_u32(&actions
, OVS_CT_ATTR_EVENTMASK
, ~0);
1228 nl_msg_end_nested(&actions
, ct_start
);
1230 /* Compose a dummy UDP packet. */
1231 dp_packet_init(&packet
, 0);
1232 flow_compose(&packet
, &flow
, NULL
, 64);
1234 /* Execute the actions. On older datapaths this fails with EINVAL, on
1235 * newer datapaths it succeeds. */
1236 execute
.actions
= actions
.data
;
1237 execute
.actions_len
= actions
.size
;
1238 execute
.packet
= &packet
;
1239 execute
.flow
= &flow
;
1240 execute
.needs_help
= false;
1241 execute
.probe
= true;
1244 error
= dpif_execute(backer
->dpif
, &execute
);
1246 dp_packet_uninit(&packet
);
1247 ofpbuf_uninit(&actions
);
1250 VLOG_INFO("%s: Datapath does not support eventmask in conntrack action",
1251 dpif_name(backer
->dpif
));
1253 VLOG_INFO("%s: Datapath supports eventmask in conntrack action",
1254 dpif_name(backer
->dpif
));
1260 /* Tests whether 'backer''s datapath supports the OVS_ACTION_ATTR_CT_CLEAR
1263 check_ct_clear(struct dpif_backer
*backer
)
1265 struct odputil_keybuf keybuf
;
1266 uint8_t actbuf
[NL_A_FLAG_SIZE
];
1267 struct ofpbuf actions
;
1272 struct odp_flow_key_parms odp_parms
= {
1277 memset(&flow
, 0, sizeof flow
);
1278 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1279 odp_flow_key_from_flow(&odp_parms
, &key
);
1281 ofpbuf_use_stack(&actions
, &actbuf
, sizeof actbuf
);
1282 nl_msg_put_flag(&actions
, OVS_ACTION_ATTR_CT_CLEAR
);
1284 supported
= dpif_probe_feature(backer
->dpif
, "ct_clear", &key
,
1287 VLOG_INFO("%s: Datapath %s ct_clear action",
1288 dpif_name(backer
->dpif
), (supported
) ? "supports"
1289 : "does not support");
1293 /* Probe the highest dp_hash algorithm supported by the datapath. */
1295 check_max_dp_hash_alg(struct dpif_backer
*backer
)
1297 struct odputil_keybuf keybuf
;
1300 struct ovs_action_hash
*hash
;
1303 struct odp_flow_key_parms odp_parms
= {
1308 memset(&flow
, 0, sizeof flow
);
1309 ofpbuf_use_stack(&key
, &keybuf
, sizeof keybuf
);
1310 odp_flow_key_from_flow(&odp_parms
, &key
);
1312 /* All datapaths support algortithm 0 (OVS_HASH_ALG_L4). */
1313 for (int alg
= 1; alg
< __OVS_HASH_MAX
; alg
++) {
1314 struct ofpbuf actions
;
1317 ofpbuf_init(&actions
, 300);
1318 hash
= nl_msg_put_unspec_uninit(&actions
,
1319 OVS_ACTION_ATTR_HASH
, sizeof *hash
);
1320 hash
->hash_basis
= 0;
1321 hash
->hash_alg
= alg
;
1322 ok
= dpif_probe_feature(backer
->dpif
, "Max dp_hash algorithm", &key
,
1324 ofpbuf_uninit(&actions
);
1332 VLOG_INFO("%s: Max dp_hash algorithm probed to be %d",
1333 dpif_name(backer
->dpif
), max_alg
);
1337 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE, ETHTYPE) \
1339 check_##NAME(struct dpif_backer *backer) \
1342 struct odputil_keybuf keybuf; \
1343 struct ofpbuf key; \
1345 struct odp_flow_key_parms odp_parms = { \
1352 memset(&flow, 0, sizeof flow); \
1353 flow.FIELD = VALUE; \
1354 flow.dl_type = htons(ETHTYPE); \
1356 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1357 odp_flow_key_from_flow(&odp_parms, &key); \
1358 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL, NULL); \
1361 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1363 VLOG_INFO("%s: Datapath does not support "#NAME, \
1364 dpif_name(backer->dpif)); \
1369 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1, \
1372 CHECK_FEATURE(ct_state
)
1373 CHECK_FEATURE(ct_zone
)
1374 CHECK_FEATURE(ct_mark
)
1375 CHECK_FEATURE__(ct_label
, ct_label
, ct_label
.u64
.lo
, 1, ETH_TYPE_IP
)
1376 CHECK_FEATURE__(ct_state_nat
, ct_state
, ct_state
, \
1377 CS_TRACKED
|CS_SRC_NAT
, ETH_TYPE_IP
)
1378 CHECK_FEATURE__(ct_orig_tuple
, ct_orig_tuple
, ct_nw_proto
, 1, ETH_TYPE_IP
)
1379 CHECK_FEATURE__(ct_orig_tuple6
, ct_orig_tuple6
, ct_nw_proto
, 1, ETH_TYPE_IPV6
)
1381 #undef CHECK_FEATURE
1382 #undef CHECK_FEATURE__
1385 check_support(struct dpif_backer
*backer
)
1388 backer
->rt_support
.odp
.recirc
= check_recirc(backer
);
1389 backer
->rt_support
.odp
.max_vlan_headers
= check_max_vlan_headers(backer
);
1390 backer
->rt_support
.odp
.max_mpls_depth
= check_max_mpls_depth(backer
);
1391 backer
->rt_support
.masked_set_action
= check_masked_set_action(backer
);
1392 backer
->rt_support
.trunc
= check_trunc_action(backer
);
1393 backer
->rt_support
.ufid
= check_ufid(backer
);
1394 backer
->rt_support
.tnl_push_pop
= dpif_supports_tnl_push_pop(backer
->dpif
);
1395 backer
->rt_support
.clone
= check_clone(backer
);
1396 backer
->rt_support
.sample_nesting
= check_max_sample_nesting(backer
);
1397 backer
->rt_support
.ct_eventmask
= check_ct_eventmask(backer
);
1398 backer
->rt_support
.ct_clear
= check_ct_clear(backer
);
1399 backer
->rt_support
.max_hash_alg
= check_max_dp_hash_alg(backer
);
1402 backer
->rt_support
.odp
.ct_state
= check_ct_state(backer
);
1403 backer
->rt_support
.odp
.ct_zone
= check_ct_zone(backer
);
1404 backer
->rt_support
.odp
.ct_mark
= check_ct_mark(backer
);
1405 backer
->rt_support
.odp
.ct_label
= check_ct_label(backer
);
1407 backer
->rt_support
.odp
.ct_state_nat
= check_ct_state_nat(backer
);
1408 backer
->rt_support
.odp
.ct_orig_tuple
= check_ct_orig_tuple(backer
);
1409 backer
->rt_support
.odp
.ct_orig_tuple6
= check_ct_orig_tuple6(backer
);
1413 construct(struct ofproto
*ofproto_
)
1415 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1416 struct shash_node
*node
, *next
;
1419 /* Tunnel module can get used right after the udpif threads are running. */
1420 ofproto_tunnel_init();
1422 error
= open_dpif_backer(ofproto
->up
.type
, &ofproto
->backer
);
1427 uuid_generate(&ofproto
->uuid
);
1428 atomic_init(&ofproto
->tables_version
, OVS_VERSION_MIN
);
1429 ofproto
->netflow
= NULL
;
1430 ofproto
->sflow
= NULL
;
1431 ofproto
->ipfix
= NULL
;
1432 ofproto
->stp
= NULL
;
1433 ofproto
->rstp
= NULL
;
1434 ofproto
->dump_seq
= 0;
1435 hmap_init(&ofproto
->bundles
);
1436 ofproto
->ml
= mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME
);
1438 ofproto
->mbridge
= mbridge_create();
1439 ofproto
->has_bonded_bundles
= false;
1440 ofproto
->lacp_enabled
= false;
1441 ovs_mutex_init_adaptive(&ofproto
->stats_mutex
);
1443 guarded_list_init(&ofproto
->ams
);
1445 sset_init(&ofproto
->ports
);
1446 sset_init(&ofproto
->ghost_ports
);
1447 sset_init(&ofproto
->port_poll_set
);
1448 ofproto
->port_poll_errno
= 0;
1449 ofproto
->change_seq
= 0;
1450 ofproto
->ams_seq
= seq_create();
1451 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1454 SHASH_FOR_EACH_SAFE (node
, next
, &init_ofp_ports
) {
1455 struct iface_hint
*iface_hint
= node
->data
;
1457 if (!strcmp(iface_hint
->br_name
, ofproto
->up
.name
)) {
1458 /* Check if the datapath already has this port. */
1459 if (dpif_port_exists(ofproto
->backer
->dpif
, node
->name
)) {
1460 sset_add(&ofproto
->ports
, node
->name
);
1463 free(iface_hint
->br_name
);
1464 free(iface_hint
->br_type
);
1466 shash_delete(&init_ofp_ports
, node
);
1470 hmap_insert(&all_ofproto_dpifs_by_name
,
1471 &ofproto
->all_ofproto_dpifs_by_name_node
,
1472 hash_string(ofproto
->up
.name
, 0));
1473 hmap_insert(&all_ofproto_dpifs_by_uuid
,
1474 &ofproto
->all_ofproto_dpifs_by_uuid_node
,
1475 uuid_hash(&ofproto
->uuid
));
1476 memset(&ofproto
->stats
, 0, sizeof ofproto
->stats
);
1478 ofproto_init_tables(ofproto_
, N_TABLES
);
1479 error
= add_internal_flows(ofproto
);
1481 ofproto
->up
.tables
[TBL_INTERNAL
].flags
= OFTABLE_HIDDEN
| OFTABLE_READONLY
;
1487 add_internal_miss_flow(struct ofproto_dpif
*ofproto
, int id
,
1488 const struct ofpbuf
*ofpacts
, struct rule_dpif
**rulep
)
1494 match_init_catchall(&match
);
1495 match_set_reg(&match
, 0, id
);
1497 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 0, 0, ofpacts
,
1499 *rulep
= error
? NULL
: rule_dpif_cast(rule
);
1505 add_internal_flows(struct ofproto_dpif
*ofproto
)
1507 struct ofpact_controller
*controller
;
1508 uint64_t ofpacts_stub
[128 / 8];
1509 struct ofpbuf ofpacts
;
1510 struct rule
*unused_rulep OVS_UNUSED
;
1515 ofpbuf_use_stack(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1518 controller
= ofpact_put_CONTROLLER(&ofpacts
);
1519 controller
->max_len
= UINT16_MAX
;
1520 controller
->controller_id
= 0;
1521 controller
->reason
= OFPR_IMPLICIT_MISS
;
1522 controller
->meter_id
= NX_CTLR_NO_METER
;
1523 ofpact_finish_CONTROLLER(&ofpacts
, &controller
);
1525 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1526 &ofproto
->miss_rule
);
1531 ofpbuf_clear(&ofpacts
);
1532 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1533 &ofproto
->no_packet_in_rule
);
1538 error
= add_internal_miss_flow(ofproto
, id
++, &ofpacts
,
1539 &ofproto
->drop_frags_rule
);
1544 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1545 * zero when reaching this rule.
1547 * (priority=2), recirc_id=0, actions=drop
1549 ofpbuf_clear(&ofpacts
);
1550 match_init_catchall(&match
);
1551 match_set_recirc_id(&match
, 0);
1552 error
= ofproto_dpif_add_internal_flow(ofproto
, &match
, 2, 0, &ofpacts
,
1558 destruct(struct ofproto
*ofproto_
, bool del
)
1560 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1561 struct ofproto_async_msg
*am
;
1562 struct rule_dpif
*rule
;
1563 struct oftable
*table
;
1564 struct ovs_list ams
;
1566 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1568 xlate_remove_ofproto(ofproto
);
1571 /* Ensure that the upcall processing threads have no remaining references
1572 * to the ofproto or anything in it. */
1573 udpif_synchronize(ofproto
->backer
->udpif
);
1575 hmap_remove(&all_ofproto_dpifs_by_name
,
1576 &ofproto
->all_ofproto_dpifs_by_name_node
);
1577 hmap_remove(&all_ofproto_dpifs_by_uuid
,
1578 &ofproto
->all_ofproto_dpifs_by_uuid_node
);
1580 OFPROTO_FOR_EACH_TABLE (table
, &ofproto
->up
) {
1581 CLS_FOR_EACH (rule
, up
.cr
, &table
->cls
) {
1582 ofproto_rule_delete(&ofproto
->up
, &rule
->up
);
1585 ofproto_group_delete_all(&ofproto
->up
);
1587 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1588 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1589 ofproto_async_msg_free(am
);
1591 guarded_list_destroy(&ofproto
->ams
);
1593 recirc_free_ofproto(ofproto
, ofproto
->up
.name
);
1595 mbridge_unref(ofproto
->mbridge
);
1597 netflow_unref(ofproto
->netflow
);
1598 dpif_sflow_unref(ofproto
->sflow
);
1599 dpif_ipfix_unref(ofproto
->ipfix
);
1600 hmap_destroy(&ofproto
->bundles
);
1601 mac_learning_unref(ofproto
->ml
);
1602 mcast_snooping_unref(ofproto
->ms
);
1603 stp_unref(ofproto
->stp
);
1604 rstp_unref(ofproto
->rstp
);
1606 sset_destroy(&ofproto
->ports
);
1607 sset_destroy(&ofproto
->ghost_ports
);
1608 sset_destroy(&ofproto
->port_poll_set
);
1610 ovs_mutex_destroy(&ofproto
->stats_mutex
);
1612 seq_destroy(ofproto
->ams_seq
);
1614 close_dpif_backer(ofproto
->backer
, del
);
1618 run(struct ofproto
*ofproto_
)
1620 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1621 uint64_t new_seq
, new_dump_seq
;
1623 if (mbridge_need_revalidate(ofproto
->mbridge
)) {
1624 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1625 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1626 mac_learning_flush(ofproto
->ml
);
1627 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1628 mcast_snooping_mdb_flush(ofproto
->ms
);
1631 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1632 * flow restore. Even though nothing is processed during flow restore,
1633 * all queued 'ams' will be handled immediately when flow restore
1635 ofproto
->ams_seqno
= seq_read(ofproto
->ams_seq
);
1637 /* Do not perform any periodic activity required by 'ofproto' while
1638 * waiting for flow restore to complete. */
1639 if (!ofproto_get_flow_restore_wait()) {
1640 struct ofproto_async_msg
*am
;
1641 struct ovs_list ams
;
1643 guarded_list_pop_all(&ofproto
->ams
, &ams
);
1644 LIST_FOR_EACH_POP (am
, list_node
, &ams
) {
1645 connmgr_send_async_msg(ofproto
->up
.connmgr
, am
);
1646 ofproto_async_msg_free(am
);
1650 if (ofproto
->netflow
) {
1651 netflow_run(ofproto
->netflow
);
1653 if (ofproto
->sflow
) {
1654 dpif_sflow_run(ofproto
->sflow
);
1656 if (ofproto
->ipfix
) {
1657 dpif_ipfix_run(ofproto
->ipfix
);
1660 new_seq
= seq_read(connectivity_seq_get());
1661 if (ofproto
->change_seq
!= new_seq
) {
1662 struct ofport_dpif
*ofport
;
1664 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
1668 ofproto
->change_seq
= new_seq
;
1670 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1671 struct ofbundle
*bundle
;
1673 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1680 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
1681 if (mac_learning_run(ofproto
->ml
)) {
1682 ofproto
->backer
->need_revalidate
= REV_MAC_LEARNING
;
1684 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1686 if (mcast_snooping_run(ofproto
->ms
)) {
1687 ofproto
->backer
->need_revalidate
= REV_MCAST_SNOOPING
;
1690 new_dump_seq
= seq_read(udpif_dump_seq(ofproto
->backer
->udpif
));
1691 if (ofproto
->dump_seq
!= new_dump_seq
) {
1692 struct rule
*rule
, *next_rule
;
1693 long long now
= time_msec();
1695 /* We know stats are relatively fresh, so now is a good time to do some
1697 ofproto
->dump_seq
= new_dump_seq
;
1699 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1701 ovs_mutex_lock(&ofproto_mutex
);
1702 LIST_FOR_EACH_SAFE (rule
, next_rule
, expirable
,
1703 &ofproto
->up
.expirable
) {
1704 rule_expire(rule_dpif_cast(rule
), now
);
1706 ovs_mutex_unlock(&ofproto_mutex
);
1708 /* All outstanding data in existing flows has been accounted, so it's a
1709 * good time to do bond rebalancing. */
1710 if (ofproto
->has_bonded_bundles
) {
1711 struct ofbundle
*bundle
;
1713 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1715 bond_rebalance(bundle
->bond
);
1724 ofproto_dpif_wait(struct ofproto
*ofproto_
)
1726 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1728 if (ofproto_get_flow_restore_wait()) {
1732 if (ofproto
->sflow
) {
1733 dpif_sflow_wait(ofproto
->sflow
);
1735 if (ofproto
->ipfix
) {
1736 dpif_ipfix_wait(ofproto
->ipfix
);
1738 if (ofproto
->lacp_enabled
|| ofproto
->has_bonded_bundles
) {
1739 struct ofbundle
*bundle
;
1741 HMAP_FOR_EACH (bundle
, hmap_node
, &ofproto
->bundles
) {
1742 bundle_wait(bundle
);
1745 if (ofproto
->netflow
) {
1746 netflow_wait(ofproto
->netflow
);
1748 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
1749 mac_learning_wait(ofproto
->ml
);
1750 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
1751 mcast_snooping_wait(ofproto
->ms
);
1753 if (ofproto
->backer
->need_revalidate
) {
1754 poll_immediate_wake();
1757 seq_wait(udpif_dump_seq(ofproto
->backer
->udpif
), ofproto
->dump_seq
);
1758 seq_wait(ofproto
->ams_seq
, ofproto
->ams_seqno
);
1762 type_get_memory_usage(const char *type
, struct simap
*usage
)
1764 struct dpif_backer
*backer
;
1766 backer
= shash_find_data(&all_dpif_backers
, type
);
1768 udpif_get_memory_usage(backer
->udpif
, usage
);
1773 flush(struct ofproto
*ofproto_
)
1775 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1776 struct dpif_backer
*backer
= ofproto
->backer
;
1779 udpif_flush(backer
->udpif
);
1784 query_tables(struct ofproto
*ofproto
,
1785 struct ofputil_table_features
*features OVS_UNUSED
,
1786 struct ofputil_table_stats
*stats
)
1791 for (i
= 0; i
< ofproto
->n_tables
; i
++) {
1792 unsigned long missed
, matched
;
1794 atomic_read_relaxed(&ofproto
->tables
[i
].n_matched
, &matched
);
1795 atomic_read_relaxed(&ofproto
->tables
[i
].n_missed
, &missed
);
1797 stats
[i
].matched_count
= matched
;
1798 stats
[i
].lookup_count
= matched
+ missed
;
1804 set_tables_version(struct ofproto
*ofproto_
, ovs_version_t version
)
1806 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
1808 /* Use memory_order_release to signify that any prior memory accesses can
1809 * not be reordered to happen after this atomic store. This makes sure the
1810 * new version is properly set up when the readers can read this 'version'
1812 atomic_store_explicit(&ofproto
->tables_version
, version
,
1813 memory_order_release
);
1814 /* 'need_revalidate' can be reordered to happen before the atomic_store
1815 * above, but it does not matter as this variable is not accessed by other
1817 ofproto
->backer
->need_revalidate
= REV_FLOW_TABLE
;
1820 static struct ofport
*
1823 struct ofport_dpif
*port
= xzalloc(sizeof *port
);
1828 port_dealloc(struct ofport
*port_
)
1830 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1835 port_construct(struct ofport
*port_
)
1837 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1838 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1839 const struct netdev
*netdev
= port
->up
.netdev
;
1840 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1841 const char *dp_port_name
;
1842 struct dpif_port dpif_port
;
1845 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1846 port
->bundle
= NULL
;
1850 port
->stp_port
= NULL
;
1851 port
->stp_state
= STP_DISABLED
;
1852 port
->rstp_port
= NULL
;
1853 port
->rstp_state
= RSTP_DISABLED
;
1854 port
->is_tunnel
= false;
1858 port
->carrier_seq
= netdev_get_carrier_resets(netdev
);
1860 if (netdev_vport_is_patch(netdev
)) {
1861 /* By bailing out here, we don't submit the port to the sFlow module
1862 * to be considered for counter polling export. This is correct
1863 * because the patch port represents an interface that sFlow considers
1864 * to be "internal" to the switch as a whole, and therefore not a
1865 * candidate for counter polling. */
1866 port
->odp_port
= ODPP_NONE
;
1867 ofport_update_peer(port
);
1871 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
1872 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
, dp_port_name
,
1878 port
->odp_port
= dpif_port
.port_no
;
1880 if (netdev_get_tunnel_config(netdev
)) {
1881 atomic_count_inc(&ofproto
->backer
->tnl_count
);
1882 error
= tnl_port_add(port
, port
->up
.netdev
, port
->odp_port
,
1883 ovs_native_tunneling_is_on(ofproto
), dp_port_name
);
1885 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1886 dpif_port_destroy(&dpif_port
);
1890 port
->is_tunnel
= true;
1892 /* Sanity-check that a mapping doesn't already exist. This
1893 * shouldn't happen for non-tunnel ports. */
1894 if (odp_port_to_ofp_port(ofproto
, port
->odp_port
) != OFPP_NONE
) {
1895 VLOG_ERR("port %s already has an OpenFlow port number",
1897 dpif_port_destroy(&dpif_port
);
1901 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1902 hmap_insert(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
,
1903 hash_odp_port(port
->odp_port
));
1904 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1906 dpif_port_destroy(&dpif_port
);
1908 if (ofproto
->sflow
) {
1909 dpif_sflow_add_port(ofproto
->sflow
, port_
, port
->odp_port
);
1911 if (ofproto
->ipfix
) {
1912 dpif_ipfix_add_port(ofproto
->ipfix
, port_
, port
->odp_port
);
1919 port_destruct(struct ofport
*port_
, bool del
)
1921 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1922 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
1923 const char *devname
= netdev_get_name(port
->up
.netdev
);
1924 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1925 const char *dp_port_name
;
1927 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
1929 xlate_ofport_remove(port
);
1932 dp_port_name
= netdev_vport_get_dpif_port(port
->up
.netdev
, namebuf
,
1934 if (del
&& dpif_port_exists(ofproto
->backer
->dpif
, dp_port_name
)) {
1935 /* The underlying device is still there, so delete it. This
1936 * happens when the ofproto is being destroyed, since the caller
1937 * assumes that removal of attached ports will happen as part of
1939 if (!port
->is_tunnel
) {
1940 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
, false);
1943 /* The underlying device is already deleted (e.g. tunctl -d).
1944 * Calling dpif_port_remove to do local cleanup for the netdev */
1945 if (!port
->is_tunnel
) {
1946 dpif_port_del(ofproto
->backer
->dpif
, port
->odp_port
, true);
1951 port
->peer
->peer
= NULL
;
1955 if (port
->odp_port
!= ODPP_NONE
&& !port
->is_tunnel
) {
1956 ovs_rwlock_wrlock(&ofproto
->backer
->odp_to_ofport_lock
);
1957 hmap_remove(&ofproto
->backer
->odp_to_ofport_map
, &port
->odp_port_node
);
1958 ovs_rwlock_unlock(&ofproto
->backer
->odp_to_ofport_lock
);
1961 if (port
->is_tunnel
) {
1962 atomic_count_dec(&ofproto
->backer
->tnl_count
);
1965 tnl_port_del(port
, port
->odp_port
);
1966 sset_find_and_delete(&ofproto
->ports
, devname
);
1967 sset_find_and_delete(&ofproto
->ghost_ports
, devname
);
1968 bundle_remove(port_
);
1969 set_cfm(port_
, NULL
);
1970 set_bfd(port_
, NULL
);
1971 set_lldp(port_
, NULL
);
1972 if (port
->stp_port
) {
1973 stp_port_disable(port
->stp_port
);
1975 set_rstp_port(port_
, NULL
);
1976 if (ofproto
->sflow
) {
1977 dpif_sflow_del_port(ofproto
->sflow
, port
->odp_port
);
1979 if (ofproto
->ipfix
) {
1980 dpif_ipfix_del_port(ofproto
->ipfix
, port
->odp_port
);
1987 port_modified(struct ofport
*port_
)
1989 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
1990 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
1991 const char *dp_port_name
;
1992 struct netdev
*netdev
= port
->up
.netdev
;
1994 if (port
->bundle
&& port
->bundle
->bond
) {
1995 bond_slave_set_netdev(port
->bundle
->bond
, port
, netdev
);
1999 cfm_set_netdev(port
->cfm
, netdev
);
2003 bfd_set_netdev(port
->bfd
, netdev
);
2006 ofproto_dpif_monitor_port_update(port
, port
->bfd
, port
->cfm
,
2007 port
->lldp
, &port
->up
.pp
.hw_addr
);
2009 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
2011 if (port
->is_tunnel
) {
2012 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
2014 if (tnl_port_reconfigure(port
, netdev
, port
->odp_port
, port
->odp_port
,
2015 ovs_native_tunneling_is_on(ofproto
),
2017 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2021 ofport_update_peer(port
);
2025 port_reconfigured(struct ofport
*port_
, enum ofputil_port_config old_config
)
2027 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
2028 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(port
->up
.ofproto
);
2029 enum ofputil_port_config changed
= old_config
^ port
->up
.pp
.config
;
2031 if (changed
& (OFPUTIL_PC_NO_RECV
| OFPUTIL_PC_NO_RECV_STP
|
2032 OFPUTIL_PC_NO_FWD
| OFPUTIL_PC_NO_FLOOD
|
2033 OFPUTIL_PC_NO_PACKET_IN
)) {
2034 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2036 if (changed
& OFPUTIL_PC_NO_FLOOD
&& port
->bundle
) {
2037 bundle_update(port
->bundle
);
2044 set_sflow(struct ofproto
*ofproto_
,
2045 const struct ofproto_sflow_options
*sflow_options
)
2047 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2048 struct dpif_sflow
*ds
= ofproto
->sflow
;
2050 if (sflow_options
) {
2051 uint32_t old_probability
= ds
? dpif_sflow_get_probability(ds
) : 0;
2053 struct ofport_dpif
*ofport
;
2055 ds
= ofproto
->sflow
= dpif_sflow_create();
2056 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2057 dpif_sflow_add_port(ds
, &ofport
->up
, ofport
->odp_port
);
2060 dpif_sflow_set_options(ds
, sflow_options
);
2061 if (dpif_sflow_get_probability(ds
) != old_probability
) {
2062 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2066 dpif_sflow_unref(ds
);
2067 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2068 ofproto
->sflow
= NULL
;
2076 struct ofproto
*ofproto_
,
2077 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
2078 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
2079 size_t n_flow_exporters_options
)
2081 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2082 struct dpif_ipfix
*di
= ofproto
->ipfix
;
2083 bool has_options
= bridge_exporter_options
|| flow_exporters_options
;
2084 bool new_di
= false;
2086 if (has_options
&& !di
) {
2087 di
= ofproto
->ipfix
= dpif_ipfix_create();
2092 /* Call set_options in any case to cleanly flush the flow
2093 * caches in the last exporters that are to be destroyed. */
2094 dpif_ipfix_set_options(
2095 di
, bridge_exporter_options
, flow_exporters_options
,
2096 n_flow_exporters_options
);
2098 /* Add ports only when a new ipfix created */
2099 if (new_di
== true) {
2100 struct ofport_dpif
*ofport
;
2101 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2102 dpif_ipfix_add_port(di
, &ofport
->up
, ofport
->odp_port
);
2107 dpif_ipfix_unref(di
);
2108 ofproto
->ipfix
= NULL
;
2116 get_ipfix_stats(const struct ofproto
*ofproto_
,
2118 struct ovs_list
*replies
)
2120 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2121 struct dpif_ipfix
*di
= ofproto
->ipfix
;
2124 return OFPERR_NXST_NOT_CONFIGURED
;
2127 return dpif_ipfix_get_stats(di
, bridge_ipfix
, replies
);
2131 set_cfm(struct ofport
*ofport_
, const struct cfm_settings
*s
)
2133 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2134 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2135 struct cfm
*old
= ofport
->cfm
;
2140 ofport
->cfm
= cfm_create(ofport
->up
.netdev
);
2143 if (cfm_configure(ofport
->cfm
, s
)) {
2150 cfm_unref(ofport
->cfm
);
2153 if (ofport
->cfm
!= old
) {
2154 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2156 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2157 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2162 cfm_status_changed(struct ofport
*ofport_
)
2164 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2166 return ofport
->cfm
? cfm_check_status_change(ofport
->cfm
) : true;
2170 get_cfm_status(const struct ofport
*ofport_
,
2171 struct cfm_status
*status
)
2173 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2177 cfm_get_status(ofport
->cfm
, status
);
2186 set_bfd(struct ofport
*ofport_
, const struct smap
*cfg
)
2188 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport_
->ofproto
);
2189 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2193 ofport
->bfd
= bfd_configure(old
, netdev_get_name(ofport
->up
.netdev
),
2194 cfg
, ofport
->up
.netdev
);
2195 if (ofport
->bfd
!= old
) {
2196 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2198 ofproto_dpif_monitor_port_update(ofport
, ofport
->bfd
, ofport
->cfm
,
2199 ofport
->lldp
, &ofport
->up
.pp
.hw_addr
);
2204 bfd_status_changed(struct ofport
*ofport_
)
2206 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2208 return ofport
->bfd
? bfd_check_status_change(ofport
->bfd
) : true;
2212 get_bfd_status(struct ofport
*ofport_
, struct smap
*smap
)
2214 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2218 bfd_get_status(ofport
->bfd
, smap
);
2227 set_lldp(struct ofport
*ofport_
,
2228 const struct smap
*cfg
)
2230 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2234 if (!ofport
->lldp
) {
2235 struct ofproto_dpif
*ofproto
;
2237 ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2238 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2239 ofport
->lldp
= lldp_create(ofport
->up
.netdev
, ofport_
->mtu
, cfg
);
2242 if (!lldp_configure(ofport
->lldp
, cfg
)) {
2247 lldp_unref(ofport
->lldp
);
2248 ofport
->lldp
= NULL
;
2251 ofproto_dpif_monitor_port_update(ofport
,
2255 &ofport
->up
.pp
.hw_addr
);
2260 get_lldp_status(const struct ofport
*ofport_
,
2261 struct lldp_status
*status OVS_UNUSED
)
2263 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2265 return ofport
->lldp
? true : false;
2269 set_aa(struct ofproto
*ofproto OVS_UNUSED
,
2270 const struct aa_settings
*s
)
2272 return aa_configure(s
);
2276 aa_mapping_set(struct ofproto
*ofproto_ OVS_UNUSED
, void *aux
,
2277 const struct aa_mapping_settings
*s
)
2279 return aa_mapping_register(aux
, s
);
2283 aa_mapping_unset(struct ofproto
*ofproto OVS_UNUSED
, void *aux
)
2285 return aa_mapping_unregister(aux
);
2289 aa_vlan_get_queued(struct ofproto
*ofproto OVS_UNUSED
, struct ovs_list
*list
)
2291 return aa_get_vlan_queued(list
);
2295 aa_vlan_get_queue_size(struct ofproto
*ofproto OVS_UNUSED
)
2297 return aa_get_vlan_queue_size();
2301 /* Spanning Tree. */
2303 /* Called while rstp_mutex is held. */
2305 rstp_send_bpdu_cb(struct dp_packet
*pkt
, void *ofport_
, void *ofproto_
)
2307 struct ofproto_dpif
*ofproto
= ofproto_
;
2308 struct ofport_dpif
*ofport
= ofport_
;
2309 struct eth_header
*eth
= dp_packet_eth(pkt
);
2311 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2312 if (eth_addr_is_zero(eth
->eth_src
)) {
2313 VLOG_WARN_RL(&rl
, "%s port %d: cannot send RSTP BPDU on a port which "
2314 "does not have a configured source MAC address.",
2315 ofproto
->up
.name
, ofp_to_u16(ofport
->up
.ofp_port
));
2317 ofproto_dpif_send_packet(ofport
, false, pkt
);
2319 dp_packet_delete(pkt
);
2323 send_bpdu_cb(struct dp_packet
*pkt
, int port_num
, void *ofproto_
)
2325 struct ofproto_dpif
*ofproto
= ofproto_
;
2326 struct stp_port
*sp
= stp_get_port(ofproto
->stp
, port_num
);
2327 struct ofport_dpif
*ofport
;
2329 ofport
= stp_port_get_aux(sp
);
2331 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
2332 ofproto
->up
.name
, port_num
);
2334 struct eth_header
*eth
= dp_packet_eth(pkt
);
2336 netdev_get_etheraddr(ofport
->up
.netdev
, ð
->eth_src
);
2337 if (eth_addr_is_zero(eth
->eth_src
)) {
2338 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d "
2339 "with unknown MAC", ofproto
->up
.name
, port_num
);
2341 ofproto_dpif_send_packet(ofport
, false, pkt
);
2344 dp_packet_delete(pkt
);
2347 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2349 set_rstp(struct ofproto
*ofproto_
, const struct ofproto_rstp_settings
*s
)
2351 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2353 /* Only revalidate flows if the configuration changed. */
2354 if (!s
!= !ofproto
->rstp
) {
2355 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2359 if (!ofproto
->rstp
) {
2360 ofproto
->rstp
= rstp_create(ofproto_
->name
, s
->address
,
2361 rstp_send_bpdu_cb
, ofproto
);
2362 ofproto
->rstp_last_tick
= time_msec();
2364 rstp_set_bridge_address(ofproto
->rstp
, s
->address
);
2365 rstp_set_bridge_priority(ofproto
->rstp
, s
->priority
);
2366 rstp_set_bridge_ageing_time(ofproto
->rstp
, s
->ageing_time
);
2367 rstp_set_bridge_force_protocol_version(ofproto
->rstp
,
2368 s
->force_protocol_version
);
2369 rstp_set_bridge_max_age(ofproto
->rstp
, s
->bridge_max_age
);
2370 rstp_set_bridge_forward_delay(ofproto
->rstp
, s
->bridge_forward_delay
);
2371 rstp_set_bridge_transmit_hold_count(ofproto
->rstp
,
2372 s
->transmit_hold_count
);
2374 struct ofport
*ofport
;
2375 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2376 set_rstp_port(ofport
, NULL
);
2378 rstp_unref(ofproto
->rstp
);
2379 ofproto
->rstp
= NULL
;
2384 get_rstp_status(struct ofproto
*ofproto_
, struct ofproto_rstp_status
*s
)
2386 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2388 if (ofproto
->rstp
) {
2390 s
->root_id
= rstp_get_root_id(ofproto
->rstp
);
2391 s
->bridge_id
= rstp_get_bridge_id(ofproto
->rstp
);
2392 s
->designated_id
= rstp_get_designated_id(ofproto
->rstp
);
2393 s
->root_path_cost
= rstp_get_root_path_cost(ofproto
->rstp
);
2394 s
->designated_port_id
= rstp_get_designated_port_id(ofproto
->rstp
);
2395 s
->bridge_port_id
= rstp_get_bridge_port_id(ofproto
->rstp
);
2402 update_rstp_port_state(struct ofport_dpif
*ofport
)
2404 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2405 enum rstp_state state
;
2407 /* Figure out new state. */
2408 state
= ofport
->rstp_port
? rstp_port_get_state(ofport
->rstp_port
)
2412 if (ofport
->rstp_state
!= state
) {
2413 enum ofputil_port_state of_state
;
2416 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2417 netdev_get_name(ofport
->up
.netdev
),
2418 rstp_state_name(ofport
->rstp_state
),
2419 rstp_state_name(state
));
2421 if (rstp_learn_in_state(ofport
->rstp_state
)
2422 != rstp_learn_in_state(state
)) {
2423 /* XXX: Learning action flows should also be flushed. */
2424 if (ofport
->bundle
) {
2425 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2426 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2427 bundle_flush_macs(ofport
->bundle
, false);
2431 fwd_change
= rstp_forward_in_state(ofport
->rstp_state
)
2432 != rstp_forward_in_state(state
);
2434 ofproto
->backer
->need_revalidate
= REV_RSTP
;
2435 ofport
->rstp_state
= state
;
2437 if (fwd_change
&& ofport
->bundle
) {
2438 bundle_update(ofport
->bundle
);
2441 /* Update the RSTP state bits in the OpenFlow port description. */
2442 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2443 of_state
|= (state
== RSTP_LEARNING
? OFPUTIL_PS_STP_LEARN
2444 : state
== RSTP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2445 : state
== RSTP_DISCARDING
? OFPUTIL_PS_STP_LISTEN
2447 ofproto_port_set_state(&ofport
->up
, of_state
);
2452 rstp_run(struct ofproto_dpif
*ofproto
)
2454 if (ofproto
->rstp
) {
2455 long long int now
= time_msec();
2456 long long int elapsed
= now
- ofproto
->rstp_last_tick
;
2457 struct rstp_port
*rp
;
2458 struct ofport_dpif
*ofport
;
2460 /* Every second, decrease the values of the timers. */
2461 if (elapsed
>= 1000) {
2462 rstp_tick_timers(ofproto
->rstp
);
2463 ofproto
->rstp_last_tick
= now
;
2466 while ((ofport
= rstp_get_next_changed_port_aux(ofproto
->rstp
, &rp
))) {
2467 update_rstp_port_state(ofport
);
2471 /* FIXME: This check should be done on-event (i.e., when setting
2472 * p->fdb_flush) and not periodically.
2474 while ((ofport
= rstp_check_and_reset_fdb_flush(ofproto
->rstp
, &rp
))) {
2475 if (!rstp_shift_root_learned_address(ofproto
->rstp
)
2476 || rstp_get_old_root_aux(ofproto
->rstp
) != ofport
) {
2477 bundle_flush_macs(ofport
->bundle
, false);
2481 if (rstp_shift_root_learned_address(ofproto
->rstp
)) {
2482 struct ofport_dpif
*old_root_aux
=
2483 (struct ofport_dpif
*)rstp_get_old_root_aux(ofproto
->rstp
);
2484 struct ofport_dpif
*new_root_aux
=
2485 (struct ofport_dpif
*)rstp_get_new_root_aux(ofproto
->rstp
);
2486 if (old_root_aux
!= NULL
&& new_root_aux
!= NULL
) {
2487 bundle_move(old_root_aux
->bundle
, new_root_aux
->bundle
);
2488 rstp_reset_root_changed(ofproto
->rstp
);
2494 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2496 set_stp(struct ofproto
*ofproto_
, const struct ofproto_stp_settings
*s
)
2498 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2500 /* Only revalidate flows if the configuration changed. */
2501 if (!s
!= !ofproto
->stp
) {
2502 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2506 if (!ofproto
->stp
) {
2507 ofproto
->stp
= stp_create(ofproto_
->name
, s
->system_id
,
2508 send_bpdu_cb
, ofproto
);
2509 ofproto
->stp_last_tick
= time_msec();
2512 stp_set_bridge_id(ofproto
->stp
, s
->system_id
);
2513 stp_set_bridge_priority(ofproto
->stp
, s
->priority
);
2514 stp_set_hello_time(ofproto
->stp
, s
->hello_time
);
2515 stp_set_max_age(ofproto
->stp
, s
->max_age
);
2516 stp_set_forward_delay(ofproto
->stp
, s
->fwd_delay
);
2518 struct ofport
*ofport
;
2520 HMAP_FOR_EACH (ofport
, hmap_node
, &ofproto
->up
.ports
) {
2521 set_stp_port(ofport
, NULL
);
2524 stp_unref(ofproto
->stp
);
2525 ofproto
->stp
= NULL
;
2532 get_stp_status(struct ofproto
*ofproto_
, struct ofproto_stp_status
*s
)
2534 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
2538 s
->bridge_id
= stp_get_bridge_id(ofproto
->stp
);
2539 s
->designated_root
= stp_get_designated_root(ofproto
->stp
);
2540 s
->root_path_cost
= stp_get_root_path_cost(ofproto
->stp
);
2549 update_stp_port_state(struct ofport_dpif
*ofport
)
2551 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2552 enum stp_state state
;
2554 /* Figure out new state. */
2555 state
= ofport
->stp_port
? stp_port_get_state(ofport
->stp_port
)
2559 if (ofport
->stp_state
!= state
) {
2560 enum ofputil_port_state of_state
;
2563 VLOG_DBG("port %s: STP state changed from %s to %s",
2564 netdev_get_name(ofport
->up
.netdev
),
2565 stp_state_name(ofport
->stp_state
),
2566 stp_state_name(state
));
2567 if (stp_learn_in_state(ofport
->stp_state
)
2568 != stp_learn_in_state(state
)) {
2569 /* xxx Learning action flows should also be flushed. */
2570 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2571 mac_learning_flush(ofproto
->ml
);
2572 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2573 mcast_snooping_mdb_flush(ofproto
->ms
);
2575 fwd_change
= stp_forward_in_state(ofport
->stp_state
)
2576 != stp_forward_in_state(state
);
2578 ofproto
->backer
->need_revalidate
= REV_STP
;
2579 ofport
->stp_state
= state
;
2580 ofport
->stp_state_entered
= time_msec();
2582 if (fwd_change
&& ofport
->bundle
) {
2583 bundle_update(ofport
->bundle
);
2586 /* Update the STP state bits in the OpenFlow port description. */
2587 of_state
= ofport
->up
.pp
.state
& ~OFPUTIL_PS_STP_MASK
;
2588 of_state
|= (state
== STP_LISTENING
? OFPUTIL_PS_STP_LISTEN
2589 : state
== STP_LEARNING
? OFPUTIL_PS_STP_LEARN
2590 : state
== STP_FORWARDING
? OFPUTIL_PS_STP_FORWARD
2591 : state
== STP_BLOCKING
? OFPUTIL_PS_STP_BLOCK
2593 ofproto_port_set_state(&ofport
->up
, of_state
);
2598 stp_check_and_update_link_state(struct ofproto_dpif
*ofproto
)
2600 struct ofport_dpif
*ofport
;
2602 HMAP_FOR_EACH (ofport
, up
.hmap_node
, &ofproto
->up
.ports
) {
2603 bool up
= netdev_get_carrier(ofport
->up
.netdev
);
2605 if (ofport
->stp_port
&&
2606 up
!= (stp_port_get_state(ofport
->stp_port
) != STP_DISABLED
)) {
2608 VLOG_DBG("bridge %s, port %s is %s, %s it.",
2609 ofproto
->up
.name
, netdev_get_name(ofport
->up
.netdev
),
2611 up
? "enabling" : "disabling");
2614 stp_port_enable(ofport
->stp_port
);
2615 stp_port_set_aux(ofport
->stp_port
, ofport
);
2617 stp_port_disable(ofport
->stp_port
);
2620 update_stp_port_state(ofport
);
2625 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2626 * caller is responsible for assigning STP port numbers and ensuring
2627 * there are no duplicates. */
2629 set_stp_port(struct ofport
*ofport_
,
2630 const struct ofproto_port_stp_settings
*s
)
2632 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2633 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2634 struct stp_port
*sp
= ofport
->stp_port
;
2636 if (!s
|| !s
->enable
) {
2638 ofport
->stp_port
= NULL
;
2639 stp_port_disable(sp
);
2640 update_stp_port_state(ofport
);
2643 } else if (sp
&& stp_port_no(sp
) != s
->port_num
2644 && ofport
== stp_port_get_aux(sp
)) {
2645 /* The port-id changed, so disable the old one if it's not
2646 * already in use by another port. */
2647 stp_port_disable(sp
);
2650 sp
= ofport
->stp_port
= stp_get_port(ofproto
->stp
, s
->port_num
);
2652 /* Set name before enabling the port so that debugging messages can print
2654 stp_port_set_name(sp
, netdev_get_name(ofport
->up
.netdev
));
2656 if (netdev_get_carrier(ofport_
->netdev
)) {
2657 stp_port_enable(sp
);
2659 stp_port_disable(sp
);
2662 stp_port_set_aux(sp
, ofport
);
2663 stp_port_set_priority(sp
, s
->priority
);
2664 stp_port_set_path_cost(sp
, s
->path_cost
);
2666 update_stp_port_state(ofport
);
2672 get_stp_port_status(struct ofport
*ofport_
,
2673 struct ofproto_port_stp_status
*s
)
2675 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2676 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2677 struct stp_port
*sp
= ofport
->stp_port
;
2679 if (!ofproto
->stp
|| !sp
) {
2685 stp_port_get_status(sp
, &s
->port_id
, &s
->state
, &s
->role
);
2686 s
->sec_in_state
= (time_msec() - ofport
->stp_state_entered
) / 1000;
2692 get_stp_port_stats(struct ofport
*ofport_
,
2693 struct ofproto_port_stp_stats
*s
)
2695 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2696 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2697 struct stp_port
*sp
= ofport
->stp_port
;
2699 if (!ofproto
->stp
|| !sp
) {
2705 stp_port_get_counts(sp
, &s
->tx_count
, &s
->rx_count
, &s
->error_count
);
2711 stp_run(struct ofproto_dpif
*ofproto
)
2714 long long int now
= time_msec();
2715 long long int elapsed
= now
- ofproto
->stp_last_tick
;
2716 struct stp_port
*sp
;
2719 stp_tick(ofproto
->stp
, MIN(INT_MAX
, elapsed
));
2720 ofproto
->stp_last_tick
= now
;
2723 stp_check_and_update_link_state(ofproto
);
2725 while (stp_get_changed_port(ofproto
->stp
, &sp
)) {
2726 struct ofport_dpif
*ofport
= stp_port_get_aux(sp
);
2729 update_stp_port_state(ofport
);
2733 if (stp_check_and_reset_fdb_flush(ofproto
->stp
)) {
2734 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
2735 mac_learning_flush(ofproto
->ml
);
2736 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
2737 mcast_snooping_mdb_flush(ofproto
->ms
);
2743 stp_wait(struct ofproto_dpif
*ofproto
)
2746 poll_timer_wait(1000);
2750 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2751 * caller is responsible for assigning RSTP port numbers and ensuring
2752 * there are no duplicates. */
2754 set_rstp_port(struct ofport
*ofport_
,
2755 const struct ofproto_port_rstp_settings
*s
)
2757 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2758 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2759 struct rstp_port
*rp
= ofport
->rstp_port
;
2761 if (!s
|| !s
->enable
) {
2763 rstp_port_set_aux(rp
, NULL
);
2764 rstp_port_set_state(rp
, RSTP_DISABLED
);
2765 rstp_port_set_mac_operational(rp
, false);
2766 ofport
->rstp_port
= NULL
;
2767 rstp_port_unref(rp
);
2768 update_rstp_port_state(ofport
);
2773 /* Check if need to add a new port. */
2775 rp
= ofport
->rstp_port
= rstp_add_port(ofproto
->rstp
);
2778 rstp_port_set(rp
, s
->port_num
, s
->priority
, s
->path_cost
,
2779 s
->admin_edge_port
, s
->auto_edge
,
2780 s
->admin_p2p_mac_state
, s
->admin_port_state
, s
->mcheck
,
2781 ofport
, netdev_get_name(ofport
->up
.netdev
));
2782 update_rstp_port_state(ofport
);
2783 /* Synchronize operational status. */
2784 rstp_port_set_mac_operational(rp
, ofport
->up
.may_enable
);
2788 get_rstp_port_status(struct ofport
*ofport_
,
2789 struct ofproto_port_rstp_status
*s
)
2791 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2792 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2793 struct rstp_port
*rp
= ofport
->rstp_port
;
2795 if (!ofproto
->rstp
|| !rp
) {
2801 rstp_port_get_status(rp
, &s
->port_id
, &s
->state
, &s
->role
,
2802 &s
->designated_bridge_id
, &s
->designated_port_id
,
2803 &s
->designated_path_cost
, &s
->tx_count
,
2804 &s
->rx_count
, &s
->error_count
, &s
->uptime
);
2809 set_queues(struct ofport
*ofport_
, const struct ofproto_port_queue
*qdscp
,
2812 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
2813 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
2815 if (ofport
->n_qdscp
!= n_qdscp
2816 || (n_qdscp
&& memcmp(ofport
->qdscp
, qdscp
,
2817 n_qdscp
* sizeof *qdscp
))) {
2818 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2819 free(ofport
->qdscp
);
2820 ofport
->qdscp
= n_qdscp
2821 ? xmemdup(qdscp
, n_qdscp
* sizeof *qdscp
)
2823 ofport
->n_qdscp
= n_qdscp
;
2831 /* Expires all MAC learning entries associated with 'bundle' and forces its
2832 * ofproto to revalidate every flow.
2834 * Normally MAC learning entries are removed only from the ofproto associated
2835 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2836 * are removed from every ofproto. When patch ports and SLB bonds are in use
2837 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2838 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2839 * with the host from which it migrated. */
2841 bundle_flush_macs(struct ofbundle
*bundle
, bool all_ofprotos
)
2843 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
2844 struct mac_learning
*ml
= ofproto
->ml
;
2845 struct mac_entry
*mac
, *next_mac
;
2847 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2848 ovs_rwlock_wrlock(&ml
->rwlock
);
2849 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2850 if (mac_entry_get_port(ml
, mac
) == bundle
) {
2852 struct ofproto_dpif
*o
;
2854 HMAP_FOR_EACH (o
, all_ofproto_dpifs_by_name_node
,
2855 &all_ofproto_dpifs_by_name
) {
2857 struct mac_entry
*e
;
2859 ovs_rwlock_wrlock(&o
->ml
->rwlock
);
2860 e
= mac_learning_lookup(o
->ml
, mac
->mac
, mac
->vlan
);
2862 mac_learning_expire(o
->ml
, e
);
2864 ovs_rwlock_unlock(&o
->ml
->rwlock
);
2869 mac_learning_expire(ml
, mac
);
2872 ovs_rwlock_unlock(&ml
->rwlock
);
2876 bundle_move(struct ofbundle
*old
, struct ofbundle
*new)
2878 struct ofproto_dpif
*ofproto
= old
->ofproto
;
2879 struct mac_learning
*ml
= ofproto
->ml
;
2880 struct mac_entry
*mac
, *next_mac
;
2882 ovs_assert(new->ofproto
== old
->ofproto
);
2884 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2885 ovs_rwlock_wrlock(&ml
->rwlock
);
2886 LIST_FOR_EACH_SAFE (mac
, next_mac
, lru_node
, &ml
->lrus
) {
2887 if (mac_entry_get_port(ml
, mac
) == old
) {
2888 mac_entry_set_port(ml
, mac
, new);
2891 ovs_rwlock_unlock(&ml
->rwlock
);
2894 static struct ofbundle
*
2895 bundle_lookup(const struct ofproto_dpif
*ofproto
, void *aux
)
2897 struct ofbundle
*bundle
;
2899 HMAP_FOR_EACH_IN_BUCKET (bundle
, hmap_node
, hash_pointer(aux
, 0),
2900 &ofproto
->bundles
) {
2901 if (bundle
->aux
== aux
) {
2909 bundle_update(struct ofbundle
*bundle
)
2911 struct ofport_dpif
*port
;
2913 bundle
->floodable
= true;
2914 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
2915 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2916 || netdev_get_pt_mode(port
->up
.netdev
) == NETDEV_PT_LEGACY_L3
2917 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2918 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2919 bundle
->floodable
= false;
2926 bundle_del_port(struct ofport_dpif
*port
)
2928 struct ofbundle
*bundle
= port
->bundle
;
2930 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2932 ovs_list_remove(&port
->bundle_node
);
2933 port
->bundle
= NULL
;
2936 lacp_slave_unregister(bundle
->lacp
, port
);
2939 bond_slave_unregister(bundle
->bond
, port
);
2942 bundle_update(bundle
);
2946 bundle_add_port(struct ofbundle
*bundle
, ofp_port_t ofp_port
,
2947 struct lacp_slave_settings
*lacp
)
2949 struct ofport_dpif
*port
;
2951 port
= ofp_port_to_ofport(bundle
->ofproto
, ofp_port
);
2956 if (port
->bundle
!= bundle
) {
2957 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2959 bundle_remove(&port
->up
);
2962 port
->bundle
= bundle
;
2963 ovs_list_push_back(&bundle
->ports
, &port
->bundle_node
);
2964 if (port
->up
.pp
.config
& OFPUTIL_PC_NO_FLOOD
2965 || netdev_get_pt_mode(port
->up
.netdev
) == NETDEV_PT_LEGACY_L3
2966 || (bundle
->ofproto
->stp
&& !stp_forward_in_state(port
->stp_state
))
2967 || (bundle
->ofproto
->rstp
&& !rstp_forward_in_state(port
->rstp_state
))) {
2968 bundle
->floodable
= false;
2972 bundle
->ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
2973 lacp_slave_register(bundle
->lacp
, port
, lacp
);
2980 bundle_destroy(struct ofbundle
*bundle
)
2982 struct ofproto_dpif
*ofproto
;
2983 struct ofport_dpif
*port
, *next_port
;
2989 ofproto
= bundle
->ofproto
;
2990 mbridge_unregister_bundle(ofproto
->mbridge
, bundle
);
2993 xlate_bundle_remove(bundle
);
2996 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
2997 bundle_del_port(port
);
3000 bundle_flush_macs(bundle
, true);
3001 mcast_snooping_flush_bundle(ofproto
->ms
, bundle
);
3002 hmap_remove(&ofproto
->bundles
, &bundle
->hmap_node
);
3004 free(bundle
->trunks
);
3005 free(bundle
->cvlans
);
3006 lacp_unref(bundle
->lacp
);
3007 bond_unref(bundle
->bond
);
3012 bundle_set(struct ofproto
*ofproto_
, void *aux
,
3013 const struct ofproto_bundle_settings
*s
)
3015 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3016 bool need_flush
= false;
3017 struct ofport_dpif
*port
;
3018 struct ofbundle
*bundle
;
3019 unsigned long *trunks
= NULL
;
3020 unsigned long *cvlans
= NULL
;
3025 bundle
= bundle_lookup(ofproto
, aux
);
3028 bundle_destroy(bundle
);
3032 ovs_assert(s
->n_slaves
== 1 || s
->bond
!= NULL
);
3033 ovs_assert((s
->lacp
!= NULL
) == (s
->lacp_slaves
!= NULL
));
3036 bundle
= xmalloc(sizeof *bundle
);
3038 bundle
->ofproto
= ofproto
;
3039 hmap_insert(&ofproto
->bundles
, &bundle
->hmap_node
,
3040 hash_pointer(aux
, 0));
3042 bundle
->name
= NULL
;
3044 ovs_list_init(&bundle
->ports
);
3045 bundle
->vlan_mode
= PORT_VLAN_TRUNK
;
3046 bundle
->qinq_ethtype
= ETH_TYPE_VLAN_8021AD
;
3048 bundle
->trunks
= NULL
;
3049 bundle
->cvlans
= NULL
;
3050 bundle
->use_priority_tags
= s
->use_priority_tags
;
3051 bundle
->lacp
= NULL
;
3052 bundle
->bond
= NULL
;
3054 bundle
->floodable
= true;
3055 bundle
->protected = false;
3056 mbridge_register_bundle(ofproto
->mbridge
, bundle
);
3059 if (!bundle
->name
|| strcmp(s
->name
, bundle
->name
)) {
3061 bundle
->name
= xstrdup(s
->name
);
3066 ofproto
->lacp_enabled
= true;
3067 if (!bundle
->lacp
) {
3068 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3069 bundle
->lacp
= lacp_create();
3071 lacp_configure(bundle
->lacp
, s
->lacp
);
3073 lacp_unref(bundle
->lacp
);
3074 bundle
->lacp
= NULL
;
3077 /* Update set of ports. */
3079 for (i
= 0; i
< s
->n_slaves
; i
++) {
3080 if (!bundle_add_port(bundle
, s
->slaves
[i
],
3081 s
->lacp
? &s
->lacp_slaves
[i
] : NULL
)) {
3085 if (!ok
|| ovs_list_size(&bundle
->ports
) != s
->n_slaves
) {
3086 struct ofport_dpif
*next_port
;
3088 LIST_FOR_EACH_SAFE (port
, next_port
, bundle_node
, &bundle
->ports
) {
3089 for (i
= 0; i
< s
->n_slaves
; i
++) {
3090 if (s
->slaves
[i
] == port
->up
.ofp_port
) {
3095 bundle_del_port(port
);
3099 ovs_assert(ovs_list_size(&bundle
->ports
) <= s
->n_slaves
);
3101 if (ovs_list_is_empty(&bundle
->ports
)) {
3102 bundle_destroy(bundle
);
3106 /* Set VLAN tagging mode */
3107 if (s
->vlan_mode
!= bundle
->vlan_mode
3108 || s
->use_priority_tags
!= bundle
->use_priority_tags
) {
3109 bundle
->vlan_mode
= s
->vlan_mode
;
3110 bundle
->use_priority_tags
= s
->use_priority_tags
;
3114 if (s
->qinq_ethtype
!= bundle
->qinq_ethtype
) {
3115 bundle
->qinq_ethtype
= s
->qinq_ethtype
;
3120 vlan
= (s
->vlan_mode
== PORT_VLAN_TRUNK
? -1
3121 : s
->vlan
>= 0 && s
->vlan
<= 4095 ? s
->vlan
3123 if (vlan
!= bundle
->vlan
) {
3124 bundle
->vlan
= vlan
;
3128 /* Get trunked VLANs. */
3129 switch (s
->vlan_mode
) {
3130 case PORT_VLAN_ACCESS
:
3134 case PORT_VLAN_TRUNK
:
3135 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
3138 case PORT_VLAN_NATIVE_UNTAGGED
:
3139 case PORT_VLAN_NATIVE_TAGGED
:
3140 if (vlan
!= 0 && (!s
->trunks
3141 || !bitmap_is_set(s
->trunks
, vlan
)
3142 || bitmap_is_set(s
->trunks
, 0))) {
3143 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3145 trunks
= bitmap_clone(s
->trunks
, 4096);
3147 trunks
= bitmap_allocate1(4096);
3149 bitmap_set1(trunks
, vlan
);
3150 bitmap_set0(trunks
, 0);
3152 trunks
= CONST_CAST(unsigned long *, s
->trunks
);
3156 case PORT_VLAN_DOT1Q_TUNNEL
:
3157 cvlans
= CONST_CAST(unsigned long *, s
->cvlans
);
3163 if (!vlan_bitmap_equal(trunks
, bundle
->trunks
)) {
3164 free(bundle
->trunks
);
3165 if (trunks
== s
->trunks
) {
3166 bundle
->trunks
= vlan_bitmap_clone(trunks
);
3168 bundle
->trunks
= trunks
;
3173 if (trunks
!= s
->trunks
) {
3177 if (!vlan_bitmap_equal(cvlans
, bundle
->cvlans
)) {
3178 free(bundle
->cvlans
);
3179 if (cvlans
== s
->cvlans
) {
3180 bundle
->cvlans
= vlan_bitmap_clone(cvlans
);
3182 bundle
->cvlans
= cvlans
;
3187 if (cvlans
!= s
->cvlans
) {
3192 if (!ovs_list_is_short(&bundle
->ports
)) {
3193 bundle
->ofproto
->has_bonded_bundles
= true;
3195 if (bond_reconfigure(bundle
->bond
, s
->bond
)) {
3196 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3199 bundle
->bond
= bond_create(s
->bond
, ofproto
);
3200 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3203 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3204 bond_slave_register(bundle
->bond
, port
,
3205 port
->up
.ofp_port
, port
->up
.netdev
);
3208 bond_unref(bundle
->bond
);
3209 bundle
->bond
= NULL
;
3212 /* Set proteced port mode */
3213 if (s
->protected != bundle
->protected) {
3214 bundle
->protected = s
->protected;
3218 /* If we changed something that would affect MAC learning, un-learn
3219 * everything on this port and force flow revalidation. */
3221 bundle_flush_macs(bundle
, false);
3222 mcast_snooping_flush_bundle(ofproto
->ms
, bundle
);
3229 bundle_remove(struct ofport
*port_
)
3231 struct ofport_dpif
*port
= ofport_dpif_cast(port_
);
3232 struct ofbundle
*bundle
= port
->bundle
;
3235 bundle_del_port(port
);
3236 if (ovs_list_is_empty(&bundle
->ports
)) {
3237 bundle_destroy(bundle
);
3238 } else if (ovs_list_is_short(&bundle
->ports
)) {
3239 bond_unref(bundle
->bond
);
3240 bundle
->bond
= NULL
;
3246 send_pdu_cb(void *port_
, const void *pdu
, size_t pdu_size
)
3248 struct ofport_dpif
*port
= port_
;
3252 error
= netdev_get_etheraddr(port
->up
.netdev
, &ea
);
3254 struct dp_packet packet
;
3257 dp_packet_init(&packet
, 0);
3258 packet_pdu
= eth_compose(&packet
, eth_addr_lacp
, ea
, ETH_TYPE_LACP
,
3260 memcpy(packet_pdu
, pdu
, pdu_size
);
3262 ofproto_dpif_send_packet(port
, false, &packet
);
3263 dp_packet_uninit(&packet
);
3265 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 10);
3266 VLOG_ERR_RL(&rll
, "port %s: cannot obtain Ethernet address of iface "
3267 "%s (%s)", port
->bundle
->name
,
3268 netdev_get_name(port
->up
.netdev
), ovs_strerror(error
));
3273 bundle_send_learning_packets(struct ofbundle
*bundle
)
3275 struct ofproto_dpif
*ofproto
= bundle
->ofproto
;
3276 int error
, n_packets
, n_errors
;
3277 struct mac_entry
*e
;
3279 struct ovs_list list_node
;
3280 struct ofport_dpif
*port
;
3281 struct dp_packet
*pkt
;
3283 struct ovs_list packets
;
3285 ovs_list_init(&packets
);
3286 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
3287 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
3288 if (mac_entry_get_port(ofproto
->ml
, e
) != bundle
) {
3289 pkt_node
= xmalloc(sizeof *pkt_node
);
3290 pkt_node
->pkt
= bond_compose_learning_packet(bundle
->bond
,
3292 (void **)&pkt_node
->port
);
3293 ovs_list_push_back(&packets
, &pkt_node
->list_node
);
3296 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3298 error
= n_packets
= n_errors
= 0;
3299 LIST_FOR_EACH_POP (pkt_node
, list_node
, &packets
) {
3302 ret
= ofproto_dpif_send_packet(pkt_node
->port
, false, pkt_node
->pkt
);
3303 dp_packet_delete(pkt_node
->pkt
);
3313 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
3314 VLOG_WARN_RL(&rll
, "bond %s: %d errors sending %d gratuitous learning "
3315 "packets, last error was: %s",
3316 bundle
->name
, n_errors
, n_packets
, ovs_strerror(error
));
3318 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3319 bundle
->name
, n_packets
);
3324 bundle_run(struct ofbundle
*bundle
)
3327 lacp_run(bundle
->lacp
, send_pdu_cb
);
3330 struct ofport_dpif
*port
;
3332 LIST_FOR_EACH (port
, bundle_node
, &bundle
->ports
) {
3333 bond_slave_set_may_enable(bundle
->bond
, port
, port
->up
.may_enable
);
3336 if (bond_run(bundle
->bond
, lacp_status(bundle
->lacp
))) {
3337 bundle
->ofproto
->backer
->need_revalidate
= REV_BOND
;
3340 if (bond_should_send_learning_packets(bundle
->bond
)) {
3341 bundle_send_learning_packets(bundle
);
3347 bundle_wait(struct ofbundle
*bundle
)
3350 lacp_wait(bundle
->lacp
);
3353 bond_wait(bundle
->bond
);
3360 mirror_set__(struct ofproto
*ofproto_
, void *aux
,
3361 const struct ofproto_mirror_settings
*s
)
3363 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3364 struct ofbundle
**srcs
, **dsts
;
3369 mirror_destroy(ofproto
->mbridge
, aux
);
3373 srcs
= xmalloc(s
->n_srcs
* sizeof *srcs
);
3374 dsts
= xmalloc(s
->n_dsts
* sizeof *dsts
);
3376 for (i
= 0; i
< s
->n_srcs
; i
++) {
3377 srcs
[i
] = bundle_lookup(ofproto
, s
->srcs
[i
]);
3380 for (i
= 0; i
< s
->n_dsts
; i
++) {
3381 dsts
[i
] = bundle_lookup(ofproto
, s
->dsts
[i
]);
3384 error
= mirror_set(ofproto
->mbridge
, aux
, s
->name
, srcs
, s
->n_srcs
, dsts
,
3385 s
->n_dsts
, s
->src_vlans
,
3386 bundle_lookup(ofproto
, s
->out_bundle
),
3387 s
->snaplen
, s
->out_vlan
);
3394 mirror_get_stats__(struct ofproto
*ofproto
, void *aux
,
3395 uint64_t *packets
, uint64_t *bytes
)
3397 return mirror_get_stats(ofproto_dpif_cast(ofproto
)->mbridge
, aux
, packets
,
3402 set_flood_vlans(struct ofproto
*ofproto_
, unsigned long *flood_vlans
)
3404 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3405 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3406 if (mac_learning_set_flood_vlans(ofproto
->ml
, flood_vlans
)) {
3407 mac_learning_flush(ofproto
->ml
);
3409 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3414 is_mirror_output_bundle(const struct ofproto
*ofproto_
, void *aux
)
3416 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3417 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3418 return bundle
&& mirror_bundle_out(ofproto
->mbridge
, bundle
) != 0;
3422 forward_bpdu_changed(struct ofproto
*ofproto_
)
3424 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3425 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3429 set_mac_table_config(struct ofproto
*ofproto_
, unsigned int idle_time
,
3432 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3433 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
3434 mac_learning_set_idle_time(ofproto
->ml
, idle_time
);
3435 mac_learning_set_max_entries(ofproto
->ml
, max_entries
);
3436 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
3439 /* Configures multicast snooping on 'ofport' using the settings
3440 * defined in 's'. */
3442 set_mcast_snooping(struct ofproto
*ofproto_
,
3443 const struct ofproto_mcast_snooping_settings
*s
)
3445 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3447 /* Only revalidate flows if the configuration changed. */
3448 if (!s
!= !ofproto
->ms
) {
3449 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3454 ofproto
->ms
= mcast_snooping_create();
3457 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3458 mcast_snooping_set_idle_time(ofproto
->ms
, s
->idle_time
);
3459 mcast_snooping_set_max_entries(ofproto
->ms
, s
->max_entries
);
3460 if (mcast_snooping_set_flood_unreg(ofproto
->ms
, s
->flood_unreg
)) {
3461 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3463 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3465 mcast_snooping_unref(ofproto
->ms
);
3472 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3474 set_mcast_snooping_port(struct ofproto
*ofproto_
, void *aux
,
3475 const struct ofproto_mcast_snooping_port_settings
*s
)
3477 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3478 struct ofbundle
*bundle
= bundle_lookup(ofproto
, aux
);
3480 if (ofproto
->ms
&& s
) {
3481 ovs_rwlock_wrlock(&ofproto
->ms
->rwlock
);
3482 mcast_snooping_set_port_flood(ofproto
->ms
, bundle
, s
->flood
);
3483 mcast_snooping_set_port_flood_reports(ofproto
->ms
, bundle
,
3485 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
3493 struct ofport_dpif
*
3494 ofp_port_to_ofport(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
3496 struct ofport
*ofport
= ofproto_get_port(&ofproto
->up
, ofp_port
);
3497 return ofport
? ofport_dpif_cast(ofport
) : NULL
;
3501 ofproto_port_from_dpif_port(struct ofproto_dpif
*ofproto
,
3502 struct ofproto_port
*ofproto_port
,
3503 struct dpif_port
*dpif_port
)
3505 ofproto_port
->name
= dpif_port
->name
;
3506 ofproto_port
->type
= dpif_port
->type
;
3507 ofproto_port
->ofp_port
= odp_port_to_ofp_port(ofproto
, dpif_port
->port_no
);
3511 ofport_update_peer(struct ofport_dpif
*ofport
)
3513 const struct ofproto_dpif
*ofproto
;
3514 struct dpif_backer
*backer
;
3517 if (!netdev_vport_is_patch(ofport
->up
.netdev
)) {
3521 backer
= ofproto_dpif_cast(ofport
->up
.ofproto
)->backer
;
3522 backer
->need_revalidate
= REV_RECONFIGURE
;
3525 ofport
->peer
->peer
= NULL
;
3526 ofport
->peer
= NULL
;
3529 peer_name
= netdev_vport_patch_peer(ofport
->up
.netdev
);
3534 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
3535 &all_ofproto_dpifs_by_name
) {
3536 struct ofport
*peer_ofport
;
3537 struct ofport_dpif
*peer
;
3540 if (ofproto
->backer
!= backer
) {
3544 peer_ofport
= shash_find_data(&ofproto
->up
.port_by_name
, peer_name
);
3549 peer
= ofport_dpif_cast(peer_ofport
);
3550 peer_peer
= netdev_vport_patch_peer(peer
->up
.netdev
);
3551 if (peer_peer
&& !strcmp(netdev_get_name(ofport
->up
.netdev
),
3553 ofport
->peer
= peer
;
3554 ofport
->peer
->peer
= ofport
;
3564 may_enable_port(struct ofport_dpif
*ofport
)
3566 /* Carrier must be up. */
3567 if (!netdev_get_carrier(ofport
->up
.netdev
)) {
3571 /* If CFM or BFD is enabled, then at least one of them must report that the
3573 if ((ofport
->bfd
|| ofport
->cfm
)
3575 && !cfm_get_fault(ofport
->cfm
)
3576 && cfm_get_opup(ofport
->cfm
) != 0)
3578 && bfd_forwarding(ofport
->bfd
))) {
3582 /* If LACP is enabled, it must report that the link is enabled. */
3584 && !lacp_slave_may_enable(ofport
->bundle
->lacp
, ofport
)) {
3592 port_run(struct ofport_dpif
*ofport
)
3594 long long int carrier_seq
= netdev_get_carrier_resets(ofport
->up
.netdev
);
3595 bool carrier_changed
= carrier_seq
!= ofport
->carrier_seq
;
3596 ofport
->carrier_seq
= carrier_seq
;
3597 if (carrier_changed
&& ofport
->bundle
) {
3598 lacp_slave_carrier_changed(ofport
->bundle
->lacp
, ofport
);
3601 bool enable
= may_enable_port(ofport
);
3602 if (ofport
->up
.may_enable
!= enable
) {
3603 ofproto_port_set_enable(&ofport
->up
, enable
);
3605 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3606 ofproto
->backer
->need_revalidate
= REV_PORT_TOGGLED
;
3608 if (ofport
->rstp_port
) {
3609 rstp_port_set_mac_operational(ofport
->rstp_port
, enable
);
3615 port_query_by_name(const struct ofproto
*ofproto_
, const char *devname
,
3616 struct ofproto_port
*ofproto_port
)
3618 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3619 struct dpif_port dpif_port
;
3622 if (sset_contains(&ofproto
->ghost_ports
, devname
)) {
3623 const char *type
= netdev_get_type_from_name(devname
);
3625 /* We may be called before ofproto->up.port_by_name is populated with
3626 * the appropriate ofport. For this reason, we must get the name and
3627 * type from the netdev layer directly. */
3629 const struct ofport
*ofport
;
3631 ofport
= shash_find_data(&ofproto
->up
.port_by_name
, devname
);
3632 ofproto_port
->ofp_port
= ofport
? ofport
->ofp_port
: OFPP_NONE
;
3633 ofproto_port
->name
= xstrdup(devname
);
3634 ofproto_port
->type
= xstrdup(type
);
3640 if (!sset_contains(&ofproto
->ports
, devname
)) {
3643 error
= dpif_port_query_by_name(ofproto
->backer
->dpif
,
3644 devname
, &dpif_port
);
3646 ofproto_port_from_dpif_port(ofproto
, ofproto_port
, &dpif_port
);
3652 port_add(struct ofproto
*ofproto_
, struct netdev
*netdev
)
3654 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3655 const char *devname
= netdev_get_name(netdev
);
3656 char namebuf
[NETDEV_VPORT_NAME_BUFSIZE
];
3657 const char *dp_port_name
;
3659 if (netdev_vport_is_patch(netdev
)) {
3660 sset_add(&ofproto
->ghost_ports
, netdev_get_name(netdev
));
3664 dp_port_name
= netdev_vport_get_dpif_port(netdev
, namebuf
, sizeof namebuf
);
3666 odp_port_t port_no
= ODPP_NONE
;
3667 int error
= dpif_port_add(ofproto
->backer
->dpif
, netdev
, &port_no
);
3668 if (error
!= EEXIST
&& error
!= EBUSY
) {
3672 if (netdev_get_tunnel_config(netdev
)) {
3673 simap_put(&ofproto
->backer
->tnl_backers
,
3674 dp_port_name
, odp_to_u32(port_no
));
3678 if (netdev_get_tunnel_config(netdev
)) {
3679 sset_add(&ofproto
->ghost_ports
, devname
);
3681 sset_add(&ofproto
->ports
, devname
);
3687 port_del(struct ofproto
*ofproto_
, ofp_port_t ofp_port
)
3689 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3690 struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
3697 sset_find_and_delete(&ofproto
->ghost_ports
,
3698 netdev_get_name(ofport
->up
.netdev
));
3699 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
3700 if (!ofport
->is_tunnel
&& !netdev_vport_is_patch(ofport
->up
.netdev
)) {
3701 error
= dpif_port_del(ofproto
->backer
->dpif
, ofport
->odp_port
, false);
3703 /* The caller is going to close ofport->up.netdev. If this is a
3704 * bonded port, then the bond is using that netdev, so remove it
3705 * from the bond. The client will need to reconfigure everything
3706 * after deleting ports, so then the slave will get re-added. */
3707 bundle_remove(&ofport
->up
);
3714 port_set_config(const struct ofport
*ofport_
, const struct smap
*cfg
)
3716 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3717 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3719 if (sset_contains(&ofproto
->ghost_ports
,
3720 netdev_get_name(ofport
->up
.netdev
))) {
3724 return dpif_port_set_config(ofproto
->backer
->dpif
, ofport
->odp_port
, cfg
);
3728 port_get_stats(const struct ofport
*ofport_
, struct netdev_stats
*stats
)
3730 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3733 error
= netdev_get_stats(ofport
->up
.netdev
, stats
);
3735 if (!error
&& ofport_
->ofp_port
== OFPP_LOCAL
) {
3736 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
3738 ovs_mutex_lock(&ofproto
->stats_mutex
);
3739 /* ofproto->stats.tx_packets represents packets that we created
3740 * internally and sent to some port (e.g. packets sent with
3741 * ofproto_dpif_send_packet()). Account for them as if they had
3742 * come from OFPP_LOCAL and got forwarded. */
3744 if (stats
->rx_packets
!= UINT64_MAX
) {
3745 stats
->rx_packets
+= ofproto
->stats
.tx_packets
;
3748 if (stats
->rx_bytes
!= UINT64_MAX
) {
3749 stats
->rx_bytes
+= ofproto
->stats
.tx_bytes
;
3752 /* ofproto->stats.rx_packets represents packets that were received on
3753 * some port and we processed internally and dropped (e.g. STP).
3754 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3756 if (stats
->tx_packets
!= UINT64_MAX
) {
3757 stats
->tx_packets
+= ofproto
->stats
.rx_packets
;
3760 if (stats
->tx_bytes
!= UINT64_MAX
) {
3761 stats
->tx_bytes
+= ofproto
->stats
.rx_bytes
;
3763 ovs_mutex_unlock(&ofproto
->stats_mutex
);
3770 port_get_lacp_stats(const struct ofport
*ofport_
, struct lacp_slave_stats
*stats
)
3772 struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3773 if (ofport
->bundle
&& ofport
->bundle
->lacp
) {
3774 if (lacp_get_slave_stats(ofport
->bundle
->lacp
, ofport
, stats
)) {
3781 struct port_dump_state
{
3782 struct sset_position pos
;
3785 struct ofproto_port port
;
3790 port_dump_start(const struct ofproto
*ofproto_ OVS_UNUSED
, void **statep
)
3792 *statep
= xzalloc(sizeof(struct port_dump_state
));
3797 port_dump_next(const struct ofproto
*ofproto_
, void *state_
,
3798 struct ofproto_port
*port
)
3800 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3801 struct port_dump_state
*state
= state_
;
3802 const struct sset
*sset
;
3803 struct sset_node
*node
;
3805 if (state
->has_port
) {
3806 ofproto_port_destroy(&state
->port
);
3807 state
->has_port
= false;
3809 sset
= state
->ghost
? &ofproto
->ghost_ports
: &ofproto
->ports
;
3810 while ((node
= sset_at_position(sset
, &state
->pos
))) {
3813 error
= port_query_by_name(ofproto_
, node
->name
, &state
->port
);
3815 *port
= state
->port
;
3816 state
->has_port
= true;
3818 } else if (error
!= ENODEV
) {
3823 if (!state
->ghost
) {
3824 state
->ghost
= true;
3825 memset(&state
->pos
, 0, sizeof state
->pos
);
3826 return port_dump_next(ofproto_
, state_
, port
);
3833 port_dump_done(const struct ofproto
*ofproto_ OVS_UNUSED
, void *state_
)
3835 struct port_dump_state
*state
= state_
;
3837 if (state
->has_port
) {
3838 ofproto_port_destroy(&state
->port
);
3845 port_poll(const struct ofproto
*ofproto_
, char **devnamep
)
3847 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3849 if (ofproto
->port_poll_errno
) {
3850 int error
= ofproto
->port_poll_errno
;
3851 ofproto
->port_poll_errno
= 0;
3855 if (sset_is_empty(&ofproto
->port_poll_set
)) {
3859 *devnamep
= sset_pop(&ofproto
->port_poll_set
);
3864 port_poll_wait(const struct ofproto
*ofproto_
)
3866 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
3867 dpif_port_poll_wait(ofproto
->backer
->dpif
);
3871 port_is_lacp_current(const struct ofport
*ofport_
)
3873 const struct ofport_dpif
*ofport
= ofport_dpif_cast(ofport_
);
3874 return (ofport
->bundle
&& ofport
->bundle
->lacp
3875 ? lacp_slave_is_current(ofport
->bundle
->lacp
, ofport
)
3879 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3880 * then delete it entirely. */
3882 rule_expire(struct rule_dpif
*rule
, long long now
)
3883 OVS_REQUIRES(ofproto_mutex
)
3885 uint16_t hard_timeout
, idle_timeout
;
3888 hard_timeout
= rule
->up
.hard_timeout
;
3889 idle_timeout
= rule
->up
.idle_timeout
;
3891 /* Has 'rule' expired? */
3893 long long int modified
;
3895 ovs_mutex_lock(&rule
->up
.mutex
);
3896 modified
= rule
->up
.modified
;
3897 ovs_mutex_unlock(&rule
->up
.mutex
);
3899 if (now
> modified
+ hard_timeout
* 1000) {
3900 reason
= OFPRR_HARD_TIMEOUT
;
3904 if (reason
< 0 && idle_timeout
) {
3907 ovs_mutex_lock(&rule
->stats_mutex
);
3908 used
= rule
->stats
.used
;
3909 ovs_mutex_unlock(&rule
->stats_mutex
);
3911 if (now
> used
+ idle_timeout
* 1000) {
3912 reason
= OFPRR_IDLE_TIMEOUT
;
3917 COVERAGE_INC(ofproto_dpif_expired
);
3918 ofproto_rule_expire(&rule
->up
, reason
);
3923 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif
*ofproto
,
3924 ofp_port_t in_port
, struct dp_packet
*packet
)
3926 if (in_port
== OFPP_NONE
) {
3927 in_port
= OFPP_LOCAL
;
3929 packet
->md
.in_port
.odp_port
= ofp_port_to_odp_port(ofproto
, in_port
);
3933 ofproto_dpif_execute_actions__(struct ofproto_dpif
*ofproto
,
3934 ovs_version_t version
, const struct flow
*flow
,
3935 struct rule_dpif
*rule
,
3936 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3937 int depth
, int resubmits
,
3938 struct dp_packet
*packet
)
3940 struct dpif_flow_stats stats
;
3941 struct xlate_out xout
;
3942 struct xlate_in xin
;
3943 struct dpif_execute execute
;
3946 ovs_assert((rule
!= NULL
) != (ofpacts
!= NULL
));
3948 dpif_flow_stats_extract(flow
, packet
, time_msec(), &stats
);
3951 rule_dpif_credit_stats(rule
, &stats
);
3954 uint64_t odp_actions_stub
[1024 / 8];
3955 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
3956 xlate_in_init(&xin
, ofproto
, version
, flow
, flow
->in_port
.ofp_port
, rule
,
3957 stats
.tcp_flags
, packet
, NULL
, &odp_actions
);
3958 xin
.ofpacts
= ofpacts
;
3959 xin
.ofpacts_len
= ofpacts_len
;
3960 xin
.resubmit_stats
= &stats
;
3962 xin
.resubmits
= resubmits
;
3963 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
3968 execute
.actions
= odp_actions
.data
;
3969 execute
.actions_len
= odp_actions
.size
;
3971 pkt_metadata_from_flow(&packet
->md
, flow
);
3972 execute
.packet
= packet
;
3973 execute
.flow
= flow
;
3974 execute
.needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
3975 execute
.probe
= false;
3978 /* Fix up in_port. */
3979 ofproto_dpif_set_packet_odp_port(ofproto
, flow
->in_port
.ofp_port
, packet
);
3981 error
= dpif_execute(ofproto
->backer
->dpif
, &execute
);
3983 xlate_out_uninit(&xout
);
3984 ofpbuf_uninit(&odp_actions
);
3989 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3990 * 'flow' must reflect the data in 'packet'. */
3992 ofproto_dpif_execute_actions(struct ofproto_dpif
*ofproto
,
3993 ovs_version_t version
, const struct flow
*flow
,
3994 struct rule_dpif
*rule
,
3995 const struct ofpact
*ofpacts
, size_t ofpacts_len
,
3996 struct dp_packet
*packet
)
3998 return ofproto_dpif_execute_actions__(ofproto
, version
, flow
, rule
,
3999 ofpacts
, ofpacts_len
, 0, 0, packet
);
4003 rule_dpif_credit_stats__(struct rule_dpif
*rule
,
4004 const struct dpif_flow_stats
*stats
,
4006 OVS_REQUIRES(rule
->stats_mutex
)
4008 if (credit_counts
) {
4009 rule
->stats
.n_packets
+= stats
->n_packets
;
4010 rule
->stats
.n_bytes
+= stats
->n_bytes
;
4012 rule
->stats
.used
= MAX(rule
->stats
.used
, stats
->used
);
4016 rule_dpif_credit_stats(struct rule_dpif
*rule
,
4017 const struct dpif_flow_stats
*stats
)
4019 ovs_mutex_lock(&rule
->stats_mutex
);
4020 if (OVS_UNLIKELY(rule
->new_rule
)) {
4021 ovs_mutex_lock(&rule
->new_rule
->stats_mutex
);
4022 rule_dpif_credit_stats__(rule
->new_rule
, stats
, rule
->forward_counts
);
4023 ovs_mutex_unlock(&rule
->new_rule
->stats_mutex
);
4025 rule_dpif_credit_stats__(rule
, stats
, true);
4027 ovs_mutex_unlock(&rule
->stats_mutex
);
4030 /* Sets 'rule''s recirculation id. */
4032 rule_dpif_set_recirc_id(struct rule_dpif
*rule
, uint32_t id
)
4033 OVS_REQUIRES(rule
->up
.mutex
)
4035 ovs_assert(!rule
->recirc_id
|| rule
->recirc_id
== id
);
4036 if (rule
->recirc_id
== id
) {
4037 /* Release the new reference to the same id. */
4040 rule
->recirc_id
= id
;
4044 /* Sets 'rule''s recirculation id. */
4046 rule_set_recirc_id(struct rule
*rule_
, uint32_t id
)
4048 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4050 ovs_mutex_lock(&rule
->up
.mutex
);
4051 rule_dpif_set_recirc_id(rule
, id
);
4052 ovs_mutex_unlock(&rule
->up
.mutex
);
4056 ofproto_dpif_get_tables_version(struct ofproto_dpif
*ofproto
)
4058 ovs_version_t version
;
4060 /* Use memory_order_acquire to signify that any following memory accesses
4061 * can not be reordered to happen before this atomic read. This makes sure
4062 * all following reads relate to this or a newer version, but never to an
4064 atomic_read_explicit(&ofproto
->tables_version
, &version
,
4065 memory_order_acquire
);
4069 /* The returned rule (if any) is valid at least until the next RCU quiescent
4070 * period. If the rule needs to stay around longer, the caller should take
4073 * 'flow' is non-const to allow for temporary modifications during the lookup.
4074 * Any changes are restored before returning. */
4075 static struct rule_dpif
*
4076 rule_dpif_lookup_in_table(struct ofproto_dpif
*ofproto
, ovs_version_t version
,
4077 uint8_t table_id
, struct flow
*flow
,
4078 struct flow_wildcards
*wc
)
4080 struct classifier
*cls
= &ofproto
->up
.tables
[table_id
].cls
;
4081 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls
, version
,
4086 ofproto_dpif_credit_table_stats(struct ofproto_dpif
*ofproto
, uint8_t table_id
,
4087 uint64_t n_matches
, uint64_t n_misses
)
4089 struct oftable
*tbl
= &ofproto
->up
.tables
[table_id
];
4093 atomic_add_relaxed(&tbl
->n_matched
, n_matches
, &orig
);
4096 atomic_add_relaxed(&tbl
->n_missed
, n_misses
, &orig
);
4100 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
4101 * table '*table_id'. Returns the rule that was found, which may be one of the
4102 * special rules according to packet miss hadling. If 'may_packet_in' is
4103 * false, returning of the miss_rule (which issues packet ins for the
4104 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
4105 * that were used during the lookup.
4107 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
4108 * if none is found then the table miss configuration for that table is
4109 * honored, which can result in additional lookups in other OpenFlow tables.
4110 * In this case the function updates '*table_id' to reflect the final OpenFlow
4111 * table that was searched.
4113 * If 'honor_table_miss' is false, then only one table lookup occurs, in
4116 * The rule is returned in '*rule', which is valid at least until the next
4117 * RCU quiescent period. If the '*rule' needs to stay around longer, the
4118 * caller must take a reference.
4120 * 'in_port' allows the lookup to take place as if the in port had the value
4121 * 'in_port'. This is needed for resubmit action support.
4123 * 'flow' is non-const to allow for temporary modifications during the lookup.
4124 * Any changes are restored before returning. */
4126 rule_dpif_lookup_from_table(struct ofproto_dpif
*ofproto
,
4127 ovs_version_t version
, struct flow
*flow
,
4128 struct flow_wildcards
*wc
,
4129 const struct dpif_flow_stats
*stats
,
4130 uint8_t *table_id
, ofp_port_t in_port
,
4131 bool may_packet_in
, bool honor_table_miss
,
4132 struct xlate_cache
*xcache
)
4134 ovs_be16 old_tp_src
= flow
->tp_src
, old_tp_dst
= flow
->tp_dst
;
4135 ofp_port_t old_in_port
= flow
->in_port
.ofp_port
;
4136 enum ofputil_table_miss miss_config
;
4137 struct rule_dpif
*rule
;
4140 /* We always unwildcard nw_frag (for IP), so they
4141 * need not be unwildcarded here. */
4142 if (flow
->nw_frag
& FLOW_NW_FRAG_ANY
4143 && ofproto
->up
.frag_handling
!= OFPUTIL_FRAG_NX_MATCH
) {
4144 if (ofproto
->up
.frag_handling
== OFPUTIL_FRAG_NORMAL
) {
4145 /* We must pretend that transport ports are unavailable. */
4146 flow
->tp_src
= htons(0);
4147 flow
->tp_dst
= htons(0);
4149 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
4150 * Use the drop_frags_rule (which cannot disappear). */
4151 rule
= ofproto
->drop_frags_rule
;
4153 struct oftable
*tbl
= &ofproto
->up
.tables
[*table_id
];
4156 atomic_add_relaxed(&tbl
->n_matched
, stats
->n_packets
, &orig
);
4159 struct xc_entry
*entry
;
4161 entry
= xlate_cache_add_entry(xcache
, XC_TABLE
);
4162 entry
->table
.ofproto
= ofproto
;
4163 entry
->table
.id
= *table_id
;
4164 entry
->table
.match
= true;
4170 /* Look up a flow with 'in_port' as the input port. Then restore the
4171 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
4172 * have surprising behavior). */
4173 flow
->in_port
.ofp_port
= in_port
;
4175 /* Our current implementation depends on n_tables == N_TABLES, and
4176 * TBL_INTERNAL being the last table. */
4177 BUILD_ASSERT_DECL(N_TABLES
== TBL_INTERNAL
+ 1);
4179 miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
4181 for (next_id
= *table_id
;
4182 next_id
< ofproto
->up
.n_tables
;
4183 next_id
++, next_id
+= (next_id
== TBL_INTERNAL
))
4185 *table_id
= next_id
;
4186 rule
= rule_dpif_lookup_in_table(ofproto
, version
, next_id
, flow
, wc
);
4188 struct oftable
*tbl
= &ofproto
->up
.tables
[next_id
];
4191 atomic_add_relaxed(rule
? &tbl
->n_matched
: &tbl
->n_missed
,
4192 stats
->n_packets
, &orig
);
4195 struct xc_entry
*entry
;
4197 entry
= xlate_cache_add_entry(xcache
, XC_TABLE
);
4198 entry
->table
.ofproto
= ofproto
;
4199 entry
->table
.id
= next_id
;
4200 entry
->table
.match
= (rule
!= NULL
);
4203 goto out
; /* Match. */
4205 if (honor_table_miss
) {
4206 miss_config
= ofproto_table_get_miss_config(&ofproto
->up
,
4208 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
4215 rule
= ofproto
->no_packet_in_rule
;
4216 if (may_packet_in
) {
4217 if (miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
4218 || miss_config
== OFPUTIL_TABLE_MISS_CONTROLLER
) {
4219 struct ofport_dpif
*port
;
4221 port
= ofp_port_to_ofport(ofproto
, old_in_port
);
4223 VLOG_WARN_RL(&rl
, "packet-in on unknown OpenFlow port %"PRIu32
,
4225 } else if (!(port
->up
.pp
.config
& OFPUTIL_PC_NO_PACKET_IN
)) {
4226 rule
= ofproto
->miss_rule
;
4228 } else if (miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
&&
4229 connmgr_wants_packet_in_on_miss(ofproto
->up
.connmgr
)) {
4230 rule
= ofproto
->miss_rule
;
4234 /* Restore port numbers, as they may have been modified above. */
4235 flow
->tp_src
= old_tp_src
;
4236 flow
->tp_dst
= old_tp_dst
;
4237 /* Restore the old in port. */
4238 flow
->in_port
.ofp_port
= old_in_port
;
4243 static struct rule_dpif
*rule_dpif_cast(const struct rule
*rule
)
4245 return rule
? CONTAINER_OF(rule
, struct rule_dpif
, up
) : NULL
;
4248 static struct rule
*
4251 struct rule_dpif
*rule
= xzalloc(sizeof *rule
);
4256 rule_dealloc(struct rule
*rule_
)
4258 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4263 check_mask(struct ofproto_dpif
*ofproto
, const struct miniflow
*flow
)
4265 const struct odp_support
*support
;
4266 uint16_t ct_state
, ct_zone
;
4270 support
= &ofproto
->backer
->rt_support
.odp
;
4271 ct_state
= MINIFLOW_GET_U8(flow
, ct_state
);
4273 if (ct_state
& CS_UNSUPPORTED_MASK
) {
4274 return OFPERR_OFPBMC_BAD_MASK
;
4277 /* Do not bother dissecting the flow further if the datapath supports all
4278 * the features we know of. */
4279 if (support
->ct_state
&& support
->ct_zone
&& support
->ct_mark
4280 && support
->ct_label
&& support
->ct_state_nat
4281 && support
->ct_orig_tuple
&& support
->ct_orig_tuple6
) {
4285 ct_zone
= MINIFLOW_GET_U16(flow
, ct_zone
);
4286 ct_mark
= MINIFLOW_GET_U32(flow
, ct_mark
);
4287 ct_label
= MINIFLOW_GET_U128(flow
, ct_label
);
4289 if ((ct_state
&& !support
->ct_state
)
4290 || ((ct_state
& (CS_SRC_NAT
| CS_DST_NAT
)) && !support
->ct_state_nat
)
4291 || (ct_zone
&& !support
->ct_zone
)
4292 || (ct_mark
&& !support
->ct_mark
)
4293 || (!ovs_u128_is_zero(ct_label
) && !support
->ct_label
)) {
4294 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4297 if (!support
->ct_orig_tuple
&& !support
->ct_orig_tuple6
4298 && (MINIFLOW_GET_U8(flow
, ct_nw_proto
)
4299 || MINIFLOW_GET_U16(flow
, ct_tp_src
)
4300 || MINIFLOW_GET_U16(flow
, ct_tp_dst
))) {
4301 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4304 if (!support
->ct_orig_tuple
4305 && (MINIFLOW_GET_U32(flow
, ct_nw_src
)
4306 || MINIFLOW_GET_U32(flow
, ct_nw_dst
))) {
4307 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4310 if (!support
->ct_orig_tuple6
4311 && (!ovs_u128_is_zero(MINIFLOW_GET_U128(flow
, ct_ipv6_src
))
4312 || !ovs_u128_is_zero(MINIFLOW_GET_U128(flow
, ct_ipv6_dst
)))) {
4313 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT
;
4320 report_unsupported_act(const char *action
, const char *detail
)
4322 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
4323 VLOG_WARN_RL(&rll
, "Rejecting %s action because datapath does not support"
4324 "%s%s (your kernel module may be out of date)",
4325 action
, detail
? " " : "", detail
? detail
: "");
4329 check_actions(const struct ofproto_dpif
*ofproto
,
4330 const struct rule_actions
*const actions
)
4332 const struct ofpact
*ofpact
;
4333 const struct odp_support
*support
= &ofproto
->backer
->rt_support
.odp
;
4335 OFPACT_FOR_EACH (ofpact
, actions
->ofpacts
, actions
->ofpacts_len
) {
4336 if (ofpact
->type
== OFPACT_CT
) {
4337 const struct ofpact_conntrack
*ct
;
4338 const struct ofpact
*a
;
4340 ct
= CONTAINER_OF(ofpact
, struct ofpact_conntrack
, ofpact
);
4342 if (!support
->ct_state
) {
4343 report_unsupported_act("ct", "ct action");
4344 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4346 if ((ct
->zone_imm
|| ct
->zone_src
.field
) && !support
->ct_zone
) {
4347 report_unsupported_act("ct", "ct zones");
4348 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4350 /* So far the force commit feature is implemented together with the
4351 * original direction tuple feature by all datapaths, so we use the
4352 * support flag for the 'ct_orig_tuple' to indicate support for the
4353 * force commit feature as well. */
4354 if ((ct
->flags
& NX_CT_F_FORCE
) && !support
->ct_orig_tuple
) {
4355 report_unsupported_act("ct", "force commit");
4356 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4359 OFPACT_FOR_EACH(a
, ct
->actions
, ofpact_ct_get_action_len(ct
)) {
4360 const struct mf_field
*dst
= ofpact_get_mf_dst(a
);
4362 if (a
->type
== OFPACT_NAT
&& !support
->ct_state_nat
) {
4363 /* The backer doesn't seem to support the NAT bits in
4364 * 'ct_state': assume that it doesn't support the NAT
4366 report_unsupported_act("ct", "nat");
4367 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4369 if (dst
&& ((dst
->id
== MFF_CT_MARK
&& !support
->ct_mark
) ||
4370 (dst
->id
== MFF_CT_LABEL
&& !support
->ct_label
))) {
4371 report_unsupported_act("ct", "setting mark and/or label");
4372 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4375 } else if (ofpact
->type
== OFPACT_RESUBMIT
) {
4376 struct ofpact_resubmit
*resubmit
= ofpact_get_RESUBMIT(ofpact
);
4378 if (resubmit
->with_ct_orig
&& !support
->ct_orig_tuple
) {
4379 report_unsupported_act("resubmit",
4380 "ct original direction tuple");
4381 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT
;
4390 rule_check(struct rule
*rule
)
4392 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(rule
->ofproto
);
4395 err
= check_mask(ofproto
, &rule
->cr
.match
.mask
->masks
);
4399 return check_actions(ofproto
, rule
->actions
);
4403 rule_construct(struct rule
*rule_
)
4404 OVS_NO_THREAD_SAFETY_ANALYSIS
4406 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4409 error
= rule_check(rule_
);
4414 ovs_mutex_init_adaptive(&rule
->stats_mutex
);
4415 rule
->stats
.n_packets
= 0;
4416 rule
->stats
.n_bytes
= 0;
4417 rule
->stats
.used
= rule
->up
.modified
;
4418 rule
->recirc_id
= 0;
4419 rule
->new_rule
= NULL
;
4420 rule
->forward_counts
= false;
4426 rule_insert(struct rule
*rule_
, struct rule
*old_rule_
, bool forward_counts
)
4427 OVS_REQUIRES(ofproto_mutex
)
4429 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4432 struct rule_dpif
*old_rule
= rule_dpif_cast(old_rule_
);
4434 ovs_assert(!old_rule
->new_rule
);
4436 /* Take a reference to the new rule, and refer all stats updates from
4437 * the old rule to the new rule. */
4438 ofproto_rule_ref(&rule
->up
);
4440 ovs_mutex_lock(&old_rule
->stats_mutex
);
4441 ovs_mutex_lock(&rule
->stats_mutex
);
4442 old_rule
->new_rule
= rule
; /* Forward future stats. */
4443 old_rule
->forward_counts
= forward_counts
;
4445 if (forward_counts
) {
4446 rule
->stats
= old_rule
->stats
; /* Transfer stats to the new
4449 /* Used timestamp must be forwarded whenever a rule is modified. */
4450 rule
->stats
.used
= old_rule
->stats
.used
;
4452 ovs_mutex_unlock(&rule
->stats_mutex
);
4453 ovs_mutex_unlock(&old_rule
->stats_mutex
);
4458 rule_destruct(struct rule
*rule_
)
4459 OVS_NO_THREAD_SAFETY_ANALYSIS
4461 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4463 ovs_mutex_destroy(&rule
->stats_mutex
);
4464 /* Release reference to the new rule, if any. */
4465 if (rule
->new_rule
) {
4466 ofproto_rule_unref(&rule
->new_rule
->up
);
4468 if (rule
->recirc_id
) {
4469 recirc_free_id(rule
->recirc_id
);
4474 rule_get_stats(struct rule
*rule_
, uint64_t *packets
, uint64_t *bytes
,
4475 long long int *used
)
4477 struct rule_dpif
*rule
= rule_dpif_cast(rule_
);
4479 ovs_mutex_lock(&rule
->stats_mutex
);
4480 if (OVS_UNLIKELY(rule
->new_rule
)) {
4481 rule_get_stats(&rule
->new_rule
->up
, packets
, bytes
, used
);
4483 *packets
= rule
->stats
.n_packets
;
4484 *bytes
= rule
->stats
.n_bytes
;
4485 *used
= rule
->stats
.used
;
4487 ovs_mutex_unlock(&rule
->stats_mutex
);
4490 struct ofproto_dpif_packet_out
{
4491 struct xlate_cache xcache
;
4492 struct ofpbuf odp_actions
;
4493 struct recirc_refs rr
;
4498 static struct ofproto_dpif_packet_out
*
4499 ofproto_dpif_packet_out_new(void)
4501 struct ofproto_dpif_packet_out
*aux
= xmalloc(sizeof *aux
);
4502 xlate_cache_init(&aux
->xcache
);
4503 ofpbuf_init(&aux
->odp_actions
, 64);
4504 aux
->rr
= RECIRC_REFS_EMPTY_INITIALIZER
;
4505 aux
->needs_help
= false;
4511 ofproto_dpif_packet_out_delete(struct ofproto_dpif_packet_out
*aux
)
4514 xlate_cache_uninit(&aux
->xcache
);
4515 ofpbuf_uninit(&aux
->odp_actions
);
4516 recirc_refs_unref(&aux
->rr
);
4522 packet_xlate(struct ofproto
*ofproto_
, struct ofproto_packet_out
*opo
)
4523 OVS_REQUIRES(ofproto_mutex
)
4525 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4526 struct xlate_out xout
;
4527 struct xlate_in xin
;
4528 enum ofperr error
= 0;
4530 struct ofproto_dpif_packet_out
*aux
= ofproto_dpif_packet_out_new();
4532 xlate_in_init(&xin
, ofproto
, opo
->version
, opo
->flow
,
4533 opo
->flow
->in_port
.ofp_port
, NULL
, 0, opo
->packet
, NULL
,
4535 xin
.ofpacts
= opo
->ofpacts
;
4536 xin
.ofpacts_len
= opo
->ofpacts_len
;
4537 /* No learning or stats, but collect side effects to xcache. */
4538 xin
.allow_side_effects
= false;
4539 xin
.resubmit_stats
= NULL
;
4540 xin
.xcache
= &aux
->xcache
;
4541 xin
.in_packet_out
= true;
4543 if (xlate_actions(&xin
, &xout
) != XLATE_OK
) {
4544 error
= OFPERR_OFPFMFC_UNKNOWN
; /* Error processing actions. */
4547 /* Prepare learn actions. */
4548 struct xc_entry
*entry
;
4549 struct ofpbuf entries
= aux
->xcache
.entries
;
4551 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4552 if (entry
->type
== XC_LEARN
) {
4553 struct ofproto_flow_mod
*ofm
= entry
->learn
.ofm
;
4555 error
= ofproto_flow_mod_learn_refresh(ofm
);
4559 struct rule
*rule
= ofm
->temp_rule
;
4560 ofm
->learn_adds_rule
= (rule
->state
== RULE_INITIALIZED
);
4561 if (ofm
->learn_adds_rule
) {
4562 /* If learning on a different bridge, must use its next
4563 * version number. */
4564 ofm
->version
= (rule
->ofproto
== ofproto_
)
4565 ? opo
->version
: rule
->ofproto
->tables_version
+ 1;
4566 error
= ofproto_flow_mod_learn_start(ofm
);
4575 aux
->needs_help
= (xout
.slow
& SLOW_ACTION
) != 0;
4576 recirc_refs_swap(&aux
->rr
, &xout
.recircs
); /* Hold recirc refs. */
4578 xlate_out_uninit(&xout
);
4583 xlate_out_uninit(&xout
);
4584 ofproto_dpif_packet_out_delete(aux
);
4590 packet_xlate_revert(struct ofproto
*ofproto OVS_UNUSED
,
4591 struct ofproto_packet_out
*opo
)
4592 OVS_REQUIRES(ofproto_mutex
)
4594 struct ofproto_dpif_packet_out
*aux
= opo
->aux
;
4597 /* Revert the learned flows. */
4598 struct xc_entry
*entry
;
4599 struct ofpbuf entries
= aux
->xcache
.entries
;
4601 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4602 if (entry
->type
== XC_LEARN
&& entry
->learn
.ofm
->learn_adds_rule
) {
4603 ofproto_flow_mod_learn_revert(entry
->learn
.ofm
);
4607 ofproto_dpif_packet_out_delete(aux
);
4611 /* Push stats and perform side effects of flow translation. */
4613 ofproto_dpif_xcache_execute(struct ofproto_dpif
*ofproto
,
4614 struct xlate_cache
*xcache
,
4615 struct dpif_flow_stats
*stats
)
4616 OVS_REQUIRES(ofproto_mutex
)
4618 struct xc_entry
*entry
;
4619 struct ofpbuf entries
= xcache
->entries
;
4621 XC_ENTRY_FOR_EACH (entry
, &entries
) {
4622 switch (entry
->type
) {
4624 /* Finish the learned flows. */
4625 if (entry
->learn
.ofm
->learn_adds_rule
) {
4626 ofproto_flow_mod_learn_finish(entry
->learn
.ofm
, &ofproto
->up
);
4629 case XC_FIN_TIMEOUT
:
4630 if (stats
->tcp_flags
& (TCP_FIN
| TCP_RST
)) {
4631 /* 'ofproto_mutex' already held */
4632 ofproto_rule_reduce_timeouts__(&entry
->fin
.rule
->up
,
4637 /* All the rest can be dealt with by the xlate layer. */
4647 case XC_TUNNEL_HEADER
:
4648 xlate_push_stats_entry(entry
, stats
);
4657 packet_execute(struct ofproto
*ofproto_
, struct ofproto_packet_out
*opo
)
4658 OVS_REQUIRES(ofproto_mutex
)
4660 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4661 struct dpif_flow_stats stats
;
4662 struct dpif_execute execute
;
4664 struct ofproto_dpif_packet_out
*aux
= opo
->aux
;
4667 /* Run the side effects from the xcache. */
4668 dpif_flow_stats_extract(opo
->flow
, opo
->packet
, time_msec(), &stats
);
4669 ofproto_dpif_xcache_execute(ofproto
, &aux
->xcache
, &stats
);
4671 execute
.actions
= aux
->odp_actions
.data
;
4672 execute
.actions_len
= aux
->odp_actions
.size
;
4674 pkt_metadata_from_flow(&opo
->packet
->md
, opo
->flow
);
4675 execute
.packet
= opo
->packet
;
4676 execute
.flow
= opo
->flow
;
4677 execute
.needs_help
= aux
->needs_help
;
4678 execute
.probe
= false;
4681 /* Fix up in_port. */
4682 ofproto_dpif_set_packet_odp_port(ofproto
, opo
->flow
->in_port
.ofp_port
,
4685 dpif_execute(ofproto
->backer
->dpif
, &execute
);
4686 ofproto_dpif_packet_out_delete(aux
);
4690 static struct group_dpif
*group_dpif_cast(const struct ofgroup
*group
)
4692 return group
? CONTAINER_OF(group
, struct group_dpif
, up
) : NULL
;
4695 static struct ofgroup
*
4698 struct group_dpif
*group
= xzalloc(sizeof *group
);
4703 group_dealloc(struct ofgroup
*group_
)
4705 struct group_dpif
*group
= group_dpif_cast(group_
);
4710 group_construct_stats(struct group_dpif
*group
)
4711 OVS_REQUIRES(group
->stats_mutex
)
4713 group
->packet_count
= 0;
4714 group
->byte_count
= 0;
4716 struct ofputil_bucket
*bucket
;
4717 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4718 bucket
->stats
.packet_count
= 0;
4719 bucket
->stats
.byte_count
= 0;
4724 group_dpif_credit_stats(struct group_dpif
*group
,
4725 struct ofputil_bucket
*bucket
,
4726 const struct dpif_flow_stats
*stats
)
4728 ovs_mutex_lock(&group
->stats_mutex
);
4729 group
->packet_count
+= stats
->n_packets
;
4730 group
->byte_count
+= stats
->n_bytes
;
4732 bucket
->stats
.packet_count
+= stats
->n_packets
;
4733 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4734 } else { /* Credit to all buckets */
4735 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4736 bucket
->stats
.packet_count
+= stats
->n_packets
;
4737 bucket
->stats
.byte_count
+= stats
->n_bytes
;
4740 ovs_mutex_unlock(&group
->stats_mutex
);
4743 /* Calculate the dp_hash mask needed to provide the least weighted bucket
4744 * with at least one hash value and construct a mapping table from masked
4745 * dp_hash value to group bucket using the Webster method.
4746 * If the caller specifies a non-zero max_hash value, abort and return false
4747 * if more hash values would be required. The absolute maximum number of
4748 * hash values supported is 256. */
4750 #define MAX_SELECT_GROUP_HASH_VALUES 256
4753 group_setup_dp_hash_table(struct group_dpif
*group
, size_t max_hash
)
4755 struct ofputil_bucket
*bucket
;
4756 uint32_t n_buckets
= group
->up
.n_buckets
;
4757 uint64_t total_weight
= 0;
4758 uint16_t min_weight
= UINT16_MAX
;
4760 struct ofputil_bucket
*bucket
;
4766 if (n_buckets
== 0) {
4767 VLOG_DBG(" Don't apply dp_hash method without buckets.");
4771 webster
= xcalloc(n_buckets
, sizeof(struct webster
));
4773 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4774 if (bucket
->weight
> 0 && bucket
->weight
< min_weight
) {
4775 min_weight
= bucket
->weight
;
4777 total_weight
+= bucket
->weight
;
4778 webster
[i
].bucket
= bucket
;
4779 webster
[i
].divisor
= 1;
4780 webster
[i
].value
= bucket
->weight
;
4781 webster
[i
].hits
= 0;
4785 if (total_weight
== 0) {
4786 VLOG_DBG(" Total weight is zero. No active buckets.");
4790 VLOG_DBG(" Minimum weight: %d, total weight: %"PRIu64
,
4791 min_weight
, total_weight
);
4793 uint64_t min_slots
= DIV_ROUND_UP(total_weight
, min_weight
);
4794 uint64_t min_slots2
= ROUND_UP_POW2(min_slots
);
4795 uint64_t n_hash
= MAX(16, min_slots2
);
4796 if (n_hash
> MAX_SELECT_GROUP_HASH_VALUES
||
4797 (max_hash
!= 0 && n_hash
> max_hash
)) {
4798 VLOG_DBG(" Too many hash values required: %"PRIu64
, n_hash
);
4802 VLOG_DBG(" Using %"PRIu64
" hash values:", n_hash
);
4803 group
->hash_mask
= n_hash
- 1;
4804 if (group
->hash_map
) {
4805 free(group
->hash_map
);
4807 group
->hash_map
= xcalloc(n_hash
, sizeof(struct ofputil_bucket
*));
4809 /* Use Webster method to distribute hash values over buckets. */
4810 for (int hash
= 0; hash
< n_hash
; hash
++) {
4811 struct webster
*winner
= &webster
[0];
4812 for (i
= 1; i
< n_buckets
; i
++) {
4813 if (webster
[i
].value
> winner
->value
) {
4814 winner
= &webster
[i
];
4818 winner
->divisor
+= 2;
4819 winner
->value
= (double) winner
->bucket
->weight
/ winner
->divisor
;
4820 group
->hash_map
[hash
] = winner
->bucket
;
4824 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4825 double target
= (n_hash
* bucket
->weight
) / (double) total_weight
;
4826 VLOG_DBG(" Bucket %d: weight=%d, target=%.2f hits=%d",
4827 bucket
->bucket_id
, bucket
->weight
,
4828 target
, webster
[i
].hits
);
4837 group_set_selection_method(struct group_dpif
*group
)
4839 const struct ofputil_group_props
*props
= &group
->up
.props
;
4840 const char *selection_method
= props
->selection_method
;
4842 VLOG_DBG("Constructing select group %"PRIu32
, group
->up
.group_id
);
4843 if (selection_method
[0] == '\0') {
4844 VLOG_DBG("No selection method specified. Trying dp_hash.");
4845 /* If the controller has not specified a selection method, check if
4846 * the dp_hash selection method with max 64 hash values is appropriate
4847 * for the given bucket configuration. */
4848 if (group_setup_dp_hash_table(group
, 64)) {
4849 /* Use dp_hash selection method with symmetric L4 hash. */
4850 group
->selection_method
= SEL_METHOD_DP_HASH
;
4851 group
->hash_alg
= OVS_HASH_ALG_SYM_L4
;
4852 group
->hash_basis
= 0;
4853 VLOG_DBG("Use dp_hash with %d hash values using algorithm %d.",
4854 group
->hash_mask
+ 1, group
->hash_alg
);
4856 /* Fall back to original default hashing in slow path. */
4857 VLOG_DBG("Falling back to default hash method.");
4858 group
->selection_method
= SEL_METHOD_DEFAULT
;
4860 } else if (!strcmp(selection_method
, "dp_hash")) {
4861 VLOG_DBG("Selection method specified: dp_hash.");
4862 /* Try to use dp_hash if possible at all. */
4863 if (group_setup_dp_hash_table(group
, 0)) {
4864 group
->selection_method
= SEL_METHOD_DP_HASH
;
4865 group
->hash_alg
= props
->selection_method_param
>> 32;
4866 if (group
->hash_alg
>= __OVS_HASH_MAX
) {
4867 VLOG_DBG("Invalid dp_hash algorithm %d. "
4868 "Defaulting to OVS_HASH_ALG_L4", group
->hash_alg
);
4869 group
->hash_alg
= OVS_HASH_ALG_L4
;
4871 group
->hash_basis
= (uint32_t) props
->selection_method_param
;
4872 VLOG_DBG("Use dp_hash with %d hash values using algorithm %d.",
4873 group
->hash_mask
+ 1, group
->hash_alg
);
4875 /* Fall back to original default hashing in slow path. */
4876 VLOG_DBG("Falling back to default hash method.");
4877 group
->selection_method
= SEL_METHOD_DEFAULT
;
4879 } else if (!strcmp(selection_method
, "hash")) {
4880 VLOG_DBG("Selection method specified: hash.");
4881 if (props
->fields
.values_size
> 0) {
4882 /* Controller has specified hash fields. */
4883 struct ds s
= DS_EMPTY_INITIALIZER
;
4884 oxm_format_field_array(&s
, &props
->fields
);
4885 VLOG_DBG("Hash fields: %s", ds_cstr(&s
));
4887 group
->selection_method
= SEL_METHOD_HASH
;
4889 /* No hash fields. Fall back to original default hashing. */
4890 VLOG_DBG("No hash fields. Falling back to default hash method.");
4891 group
->selection_method
= SEL_METHOD_DEFAULT
;
4894 /* Parsing of groups should ensure this never happens */
4900 group_construct(struct ofgroup
*group_
)
4902 struct group_dpif
*group
= group_dpif_cast(group_
);
4904 ovs_mutex_init_adaptive(&group
->stats_mutex
);
4905 ovs_mutex_lock(&group
->stats_mutex
);
4906 group_construct_stats(group
);
4907 group
->hash_map
= NULL
;
4908 if (group
->up
.type
== OFPGT11_SELECT
) {
4909 group_set_selection_method(group
);
4911 ovs_mutex_unlock(&group
->stats_mutex
);
4916 group_destruct(struct ofgroup
*group_
)
4918 struct group_dpif
*group
= group_dpif_cast(group_
);
4919 ovs_mutex_destroy(&group
->stats_mutex
);
4920 if (group
->hash_map
) {
4921 free(group
->hash_map
);
4922 group
->hash_map
= NULL
;
4927 group_get_stats(const struct ofgroup
*group_
, struct ofputil_group_stats
*ogs
)
4929 struct group_dpif
*group
= group_dpif_cast(group_
);
4931 ovs_mutex_lock(&group
->stats_mutex
);
4932 ogs
->packet_count
= group
->packet_count
;
4933 ogs
->byte_count
= group
->byte_count
;
4935 struct bucket_counter
*bucket_stats
= ogs
->bucket_stats
;
4936 struct ofputil_bucket
*bucket
;
4937 LIST_FOR_EACH (bucket
, list_node
, &group
->up
.buckets
) {
4938 bucket_stats
->packet_count
= bucket
->stats
.packet_count
;
4939 bucket_stats
->byte_count
= bucket
->stats
.byte_count
;
4942 ovs_mutex_unlock(&group
->stats_mutex
);
4947 /* If the group exists, this function increments the groups's reference count.
4949 * Make sure to call ofproto_group_unref() after no longer needing to maintain
4950 * a reference to the group. */
4952 group_dpif_lookup(struct ofproto_dpif
*ofproto
, uint32_t group_id
,
4953 ovs_version_t version
, bool take_ref
)
4955 struct ofgroup
*ofgroup
= ofproto_group_lookup(&ofproto
->up
, group_id
,
4957 return ofgroup
? group_dpif_cast(ofgroup
) : NULL
;
4960 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4961 * supports a notion of an OAM flag, sets it if 'oam' is true.
4962 * May modify 'packet'.
4963 * Returns 0 if successful, otherwise a positive errno value. */
4965 ofproto_dpif_send_packet(const struct ofport_dpif
*ofport
, bool oam
,
4966 struct dp_packet
*packet
)
4968 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofport
->up
.ofproto
);
4971 error
= xlate_send_packet(ofport
, oam
, packet
);
4973 ovs_mutex_lock(&ofproto
->stats_mutex
);
4974 ofproto
->stats
.tx_packets
++;
4975 ofproto
->stats
.tx_bytes
+= dp_packet_size(packet
);
4976 ovs_mutex_unlock(&ofproto
->stats_mutex
);
4980 /* Return the version string of the datapath that backs up
4984 get_datapath_version(const struct ofproto
*ofproto_
)
4986 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
4988 return ofproto
->backer
->dp_version_string
;
4992 type_set_config(const char *type
, const struct smap
*other_config
)
4994 struct dpif_backer
*backer
;
4996 backer
= shash_find_data(&all_dpif_backers
, type
);
4998 /* This is not necessarily a problem, since backers are only
4999 * created on demand. */
5003 dpif_set_config(backer
->dpif
, other_config
);
5007 ct_flush(const struct ofproto
*ofproto_
, const uint16_t *zone
)
5009 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5011 ct_dpif_flush(ofproto
->backer
->dpif
, zone
, NULL
);
5015 set_frag_handling(struct ofproto
*ofproto_
,
5016 enum ofputil_frag_handling frag_handling
)
5018 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5019 if (frag_handling
!= OFPUTIL_FRAG_REASM
) {
5020 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5028 nxt_resume(struct ofproto
*ofproto_
,
5029 const struct ofputil_packet_in_private
*pin
)
5031 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5032 struct dpif_flow_stats stats
;
5033 struct xlate_cache xcache
;
5035 xlate_cache_init(&xcache
);
5037 /* Translate pin into datapath actions. */
5038 uint64_t odp_actions_stub
[1024 / 8];
5039 struct ofpbuf odp_actions
= OFPBUF_STUB_INITIALIZER(odp_actions_stub
);
5040 enum slow_path_reason slow
;
5041 enum ofperr error
= xlate_resume(ofproto
, pin
, &odp_actions
, &slow
,
5044 /* Steal 'pin->packet' and put it into a dp_packet. */
5045 struct dp_packet packet
;
5046 dp_packet_init(&packet
, pin
->base
.packet_len
);
5047 dp_packet_put(&packet
, pin
->base
.packet
, pin
->base
.packet_len
);
5049 /* Run the side effects from the xcache. */
5050 dpif_flow_stats_extract(&flow
, &packet
, time_msec(), &stats
);
5051 ovs_mutex_lock(&ofproto_mutex
);
5052 ofproto_dpif_xcache_execute(ofproto
, &xcache
, &stats
);
5053 ovs_mutex_unlock(&ofproto_mutex
);
5055 pkt_metadata_from_flow(&packet
.md
, &pin
->base
.flow_metadata
.flow
);
5057 /* Fix up in_port. */
5058 ofproto_dpif_set_packet_odp_port(ofproto
,
5059 pin
->base
.flow_metadata
.flow
.in_port
.ofp_port
,
5062 struct flow headers
;
5063 flow_extract(&packet
, &headers
);
5065 /* Execute the datapath actions on the packet. */
5066 struct dpif_execute execute
= {
5067 .actions
= odp_actions
.data
,
5068 .actions_len
= odp_actions
.size
,
5069 .needs_help
= (slow
& SLOW_ACTION
) != 0,
5073 dpif_execute(ofproto
->backer
->dpif
, &execute
);
5076 ofpbuf_uninit(&odp_actions
);
5077 dp_packet_uninit(&packet
);
5085 set_netflow(struct ofproto
*ofproto_
,
5086 const struct netflow_options
*netflow_options
)
5088 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5090 if (netflow_options
) {
5091 if (!ofproto
->netflow
) {
5092 ofproto
->netflow
= netflow_create();
5093 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5095 return netflow_set_options(ofproto
->netflow
, netflow_options
);
5096 } else if (ofproto
->netflow
) {
5097 ofproto
->backer
->need_revalidate
= REV_RECONFIGURE
;
5098 netflow_unref(ofproto
->netflow
);
5099 ofproto
->netflow
= NULL
;
5106 get_netflow_ids(const struct ofproto
*ofproto_
,
5107 uint8_t *engine_type
, uint8_t *engine_id
)
5109 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5111 dpif_get_netflow_ids(ofproto
->backer
->dpif
, engine_type
, engine_id
);
5114 struct ofproto_dpif
*
5115 ofproto_dpif_lookup_by_name(const char *name
)
5117 struct ofproto_dpif
*ofproto
;
5119 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_by_name_node
,
5120 hash_string(name
, 0),
5121 &all_ofproto_dpifs_by_name
) {
5122 if (!strcmp(ofproto
->up
.name
, name
)) {
5129 struct ofproto_dpif
*
5130 ofproto_dpif_lookup_by_uuid(const struct uuid
*uuid
)
5132 struct ofproto_dpif
*ofproto
;
5134 HMAP_FOR_EACH_WITH_HASH (ofproto
, all_ofproto_dpifs_by_uuid_node
,
5135 uuid_hash(uuid
), &all_ofproto_dpifs_by_uuid
) {
5136 if (uuid_equals(&ofproto
->uuid
, uuid
)) {
5144 ofproto_unixctl_fdb_flush(struct unixctl_conn
*conn
, int argc
,
5145 const char *argv
[], void *aux OVS_UNUSED
)
5147 struct ofproto_dpif
*ofproto
;
5150 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5152 unixctl_command_reply_error(conn
, "no such bridge");
5155 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5156 mac_learning_flush(ofproto
->ml
);
5157 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5159 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5160 &all_ofproto_dpifs_by_name
) {
5161 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5162 mac_learning_flush(ofproto
->ml
);
5163 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5167 unixctl_command_reply(conn
, "table successfully flushed");
5171 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn
*conn
, int argc
,
5172 const char *argv
[], void *aux OVS_UNUSED
)
5174 struct ofproto_dpif
*ofproto
;
5177 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5179 unixctl_command_reply_error(conn
, "no such bridge");
5183 if (!mcast_snooping_enabled(ofproto
->ms
)) {
5184 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
5187 mcast_snooping_mdb_flush(ofproto
->ms
);
5189 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5190 &all_ofproto_dpifs_by_name
) {
5191 if (!mcast_snooping_enabled(ofproto
->ms
)) {
5194 mcast_snooping_mdb_flush(ofproto
->ms
);
5198 unixctl_command_reply(conn
, "table successfully flushed");
5201 static struct ofport_dpif
*
5202 ofbundle_get_a_port(const struct ofbundle
*bundle
)
5204 return CONTAINER_OF(ovs_list_front(&bundle
->ports
), struct ofport_dpif
,
5209 ofproto_unixctl_fdb_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5210 const char *argv
[], void *aux OVS_UNUSED
)
5212 struct ds ds
= DS_EMPTY_INITIALIZER
;
5213 const struct ofproto_dpif
*ofproto
;
5214 const struct mac_entry
*e
;
5216 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5218 unixctl_command_reply_error(conn
, "no such bridge");
5222 ds_put_cstr(&ds
, " port VLAN MAC Age\n");
5223 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
5224 LIST_FOR_EACH (e
, lru_node
, &ofproto
->ml
->lrus
) {
5225 struct ofbundle
*bundle
= mac_entry_get_port(ofproto
->ml
, e
);
5226 char name
[OFP10_MAX_PORT_NAME_LEN
];
5228 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
5229 NULL
, name
, sizeof name
);
5230 ds_put_format(&ds
, "%5s %4d "ETH_ADDR_FMT
" %3d\n",
5231 name
, e
->vlan
, ETH_ADDR_ARGS(e
->mac
),
5232 mac_entry_age(ofproto
->ml
, e
));
5234 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5235 unixctl_command_reply(conn
, ds_cstr(&ds
));
5240 ofproto_unixctl_fdb_stats_clear(struct unixctl_conn
*conn
, int argc
,
5241 const char *argv
[], void *aux OVS_UNUSED
)
5243 struct ofproto_dpif
*ofproto
;
5246 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5248 unixctl_command_reply_error(conn
, "no such bridge");
5251 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5252 mac_learning_clear_statistics(ofproto
->ml
);
5253 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5255 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5256 &all_ofproto_dpifs_by_name
) {
5257 ovs_rwlock_wrlock(&ofproto
->ml
->rwlock
);
5258 mac_learning_clear_statistics(ofproto
->ml
);
5259 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5263 unixctl_command_reply(conn
, "statistics successfully cleared");
5267 ofproto_unixctl_fdb_stats_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5268 const char *argv
[], void *aux OVS_UNUSED
)
5270 struct ds ds
= DS_EMPTY_INITIALIZER
;
5271 const struct ofproto_dpif
*ofproto
;
5272 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5274 unixctl_command_reply_error(conn
, "no such bridge");
5278 ds_put_format(&ds
, "Statistics for bridge \"%s\":\n", argv
[1]);
5279 ovs_rwlock_rdlock(&ofproto
->ml
->rwlock
);
5281 ds_put_format(&ds
, " Current/maximum MAC entries in the table: %"
5282 PRIuSIZE
"/%"PRIuSIZE
"\n",
5283 hmap_count(&ofproto
->ml
->table
), ofproto
->ml
->max_entries
);
5285 " Total number of learned MAC entries : %"PRIu64
"\n",
5286 ofproto
->ml
->total_learned
);
5288 " Total number of expired MAC entries : %"PRIu64
"\n",
5289 ofproto
->ml
->total_expired
);
5291 " Total number of evicted MAC entries : %"PRIu64
"\n",
5292 ofproto
->ml
->total_evicted
);
5294 " Total number of port moved MAC entries : %"PRIu64
"\n",
5295 ofproto
->ml
->total_moved
);
5297 ovs_rwlock_unlock(&ofproto
->ml
->rwlock
);
5298 unixctl_command_reply(conn
, ds_cstr(&ds
));
5303 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn
*conn
,
5304 int argc OVS_UNUSED
,
5306 void *aux OVS_UNUSED
)
5308 struct ds ds
= DS_EMPTY_INITIALIZER
;
5309 const struct ofproto_dpif
*ofproto
;
5310 const struct ofbundle
*bundle
;
5311 const struct mcast_group
*grp
;
5312 struct mcast_group_bundle
*b
;
5313 struct mcast_mrouter_bundle
*mrouter
;
5315 ofproto
= ofproto_dpif_lookup_by_name(argv
[1]);
5317 unixctl_command_reply_error(conn
, "no such bridge");
5321 if (!mcast_snooping_enabled(ofproto
->ms
)) {
5322 unixctl_command_reply_error(conn
, "multicast snooping is disabled");
5326 ds_put_cstr(&ds
, " port VLAN GROUP Age\n");
5327 ovs_rwlock_rdlock(&ofproto
->ms
->rwlock
);
5328 LIST_FOR_EACH (grp
, group_node
, &ofproto
->ms
->group_lru
) {
5329 LIST_FOR_EACH(b
, bundle_node
, &grp
->bundle_lru
) {
5330 char name
[OFP10_MAX_PORT_NAME_LEN
];
5333 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
5334 NULL
, name
, sizeof name
);
5335 ds_put_format(&ds
, "%5s %4d ", name
, grp
->vlan
);
5336 ipv6_format_mapped(&grp
->addr
, &ds
);
5337 ds_put_format(&ds
, " %3d\n",
5338 mcast_bundle_age(ofproto
->ms
, b
));
5342 /* ports connected to multicast routers */
5343 LIST_FOR_EACH(mrouter
, mrouter_node
, &ofproto
->ms
->mrouter_lru
) {
5344 char name
[OFP10_MAX_PORT_NAME_LEN
];
5346 bundle
= mrouter
->port
;
5347 ofputil_port_to_string(ofbundle_get_a_port(bundle
)->up
.ofp_port
,
5348 NULL
, name
, sizeof name
);
5349 ds_put_format(&ds
, "%5s %4d querier %3d\n",
5350 name
, mrouter
->vlan
,
5351 mcast_mrouter_age(ofproto
->ms
, mrouter
));
5353 ovs_rwlock_unlock(&ofproto
->ms
->rwlock
);
5354 unixctl_command_reply(conn
, ds_cstr(&ds
));
5358 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5359 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5360 * to destroy 'ofproto_shash' and free the returned value. */
5361 static const struct shash_node
**
5362 get_ofprotos(struct shash
*ofproto_shash
)
5364 const struct ofproto_dpif
*ofproto
;
5366 HMAP_FOR_EACH (ofproto
, all_ofproto_dpifs_by_name_node
,
5367 &all_ofproto_dpifs_by_name
) {
5368 char *name
= xasprintf("%s@%s", ofproto
->up
.type
, ofproto
->up
.name
);
5369 shash_add_nocopy(ofproto_shash
, name
, ofproto
);
5372 return shash_sort(ofproto_shash
);
5376 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5377 const char *argv
[] OVS_UNUSED
,
5378 void *aux OVS_UNUSED
)
5380 struct ds ds
= DS_EMPTY_INITIALIZER
;
5381 struct shash ofproto_shash
;
5382 const struct shash_node
**sorted_ofprotos
;
5385 shash_init(&ofproto_shash
);
5386 sorted_ofprotos
= get_ofprotos(&ofproto_shash
);
5387 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
5388 const struct shash_node
*node
= sorted_ofprotos
[i
];
5389 ds_put_format(&ds
, "%s\n", node
->name
);
5392 shash_destroy(&ofproto_shash
);
5393 free(sorted_ofprotos
);
5395 unixctl_command_reply(conn
, ds_cstr(&ds
));
5400 show_dp_feature_bool(struct ds
*ds
, const char *feature
, bool b
)
5402 ds_put_format(ds
, "%s: %s\n", feature
, b
? "Yes" : "No");
5406 show_dp_feature_size_t(struct ds
*ds
, const char *feature
, size_t s
)
5408 ds_put_format(ds
, "%s: %"PRIuSIZE
"\n", feature
, s
);
5411 enum dpif_support_field_type
{
5412 DPIF_SUPPORT_FIELD_bool
,
5413 DPIF_SUPPORT_FIELD_size_t
,
5416 struct dpif_support_field
{
5417 void *rt_ptr
; /* Points to the 'rt_support' field. */
5418 const void *bt_ptr
; /* Points to the 'bt_support' field. */
5420 enum dpif_support_field_type type
;
5423 #define DPIF_SUPPORT_FIELD_INTIALIZER(RT_PTR, BT_PTR, TITLE, TYPE) \
5424 (struct dpif_support_field) {RT_PTR, BT_PTR, TITLE, TYPE}
5427 dpif_show_support(const struct dpif_backer_support
*support
, struct ds
*ds
)
5429 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5430 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5432 #undef DPIF_SUPPORT_FIELD
5434 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5435 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5437 #undef ODP_SUPPORT_FIELD
5441 display_support_field(const char *name
,
5442 const struct dpif_support_field
*field
,
5445 switch (field
->type
) {
5446 case DPIF_SUPPORT_FIELD_bool
: {
5447 bool v
= *(bool *)field
->rt_ptr
;
5448 bool b
= *(bool *)field
->bt_ptr
;
5449 ds_put_format(ds
, "%s (%s) : [run time]:%s, [boot time]:%s\n", name
,
5450 field
->title
, v
? "true" : "false",
5451 b
? "true" : "false");
5454 case DPIF_SUPPORT_FIELD_size_t
:
5455 ds_put_format(ds
, "%s (%s) : [run time]:%"PRIuSIZE
5456 ", [boot time]:%"PRIuSIZE
"\n", name
,
5457 field
->title
, *(size_t *)field
->rt_ptr
,
5458 *(size_t *)field
->bt_ptr
);
5465 /* Set a field of 'rt_support' to a new value.
5467 * Returns 'true' if the value is actually set. */
5469 dpif_set_support(struct dpif_backer_support
*rt_support
,
5470 struct dpif_backer_support
*bt_support
,
5471 const char *name
, const char *value
, struct ds
*ds
)
5473 struct shash all_fields
= SHASH_INITIALIZER(&all_fields
);
5474 struct dpif_support_field
*field
;
5475 struct shash_node
*node
;
5476 bool changed
= false;
5478 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5480 struct dpif_support_field *f = xmalloc(sizeof *f); \
5481 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->NAME, \
5482 &bt_support->NAME, \
5484 DPIF_SUPPORT_FIELD_##TYPE);\
5485 shash_add_once(&all_fields, #NAME, f); \
5487 DPIF_SUPPORT_FIELDS
;
5488 #undef DPIF_SUPPORT_FIELD
5490 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5492 struct dpif_support_field *f = xmalloc(sizeof *f); \
5493 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->odp.NAME, \
5494 &bt_support->odp.NAME, \
5496 DPIF_SUPPORT_FIELD_##TYPE);\
5497 shash_add_once(&all_fields, #NAME, f); \
5500 #undef ODP_SUPPORT_FIELD
5503 SHASH_FOR_EACH (node
, &all_fields
) {
5504 display_support_field(node
->name
, node
->data
, ds
);
5509 node
= shash_find(&all_fields
, name
);
5511 ds_put_cstr(ds
, "Unexpected support field");
5517 display_support_field(node
->name
, field
, ds
);
5521 if (field
->type
== DPIF_SUPPORT_FIELD_bool
) {
5522 if (!strcasecmp(value
, "true")) {
5523 if (*(bool *)field
->bt_ptr
) {
5524 *(bool *)field
->rt_ptr
= true;
5527 ds_put_cstr(ds
, "Can not enable features not supported by the datapth");
5529 } else if (!strcasecmp(value
, "false")) {
5530 *(bool *)field
->rt_ptr
= false;
5533 ds_put_cstr(ds
, "Boolean value expected");
5535 } else if (field
->type
== DPIF_SUPPORT_FIELD_size_t
) {
5537 if (str_to_int(value
, 10, &v
)) {
5539 if (v
<= *(size_t *)field
->bt_ptr
) {
5540 *(size_t *)field
->rt_ptr
= v
;
5543 ds_put_cstr(ds
, "Can not set value beyond the datapath capability");
5546 ds_put_format(ds
, "Negative number not expected");
5549 ds_put_cstr(ds
, "Integer number expected");
5554 shash_destroy_free_data(&all_fields
);
5559 dpif_show_backer(const struct dpif_backer
*backer
, struct ds
*ds
)
5561 const struct shash_node
**ofprotos
;
5562 struct dpif_dp_stats dp_stats
;
5563 struct shash ofproto_shash
;
5566 dpif_get_dp_stats(backer
->dpif
, &dp_stats
);
5567 ds_put_format(ds
, "%s: hit:%"PRIu64
" missed:%"PRIu64
"\n",
5568 dpif_name(backer
->dpif
), dp_stats
.n_hit
, dp_stats
.n_missed
);
5570 shash_init(&ofproto_shash
);
5571 ofprotos
= get_ofprotos(&ofproto_shash
);
5572 for (i
= 0; i
< shash_count(&ofproto_shash
); i
++) {
5573 struct ofproto_dpif
*ofproto
= ofprotos
[i
]->data
;
5574 const struct shash_node
**ports
;
5577 if (ofproto
->backer
!= backer
) {
5581 ds_put_format(ds
, " %s:\n", ofproto
->up
.name
);
5583 ports
= shash_sort(&ofproto
->up
.port_by_name
);
5584 for (j
= 0; j
< shash_count(&ofproto
->up
.port_by_name
); j
++) {
5585 const struct shash_node
*node
= ports
[j
];
5586 struct ofport
*ofport
= node
->data
;
5588 odp_port_t odp_port
;
5590 ds_put_format(ds
, " %s %u/", netdev_get_name(ofport
->netdev
),
5593 odp_port
= ofp_port_to_odp_port(ofproto
, ofport
->ofp_port
);
5594 if (odp_port
!= ODPP_NONE
) {
5595 ds_put_format(ds
, "%"PRIu32
":", odp_port
);
5597 ds_put_cstr(ds
, "none:");
5600 ds_put_format(ds
, " (%s", netdev_get_type(ofport
->netdev
));
5603 if (!netdev_get_config(ofport
->netdev
, &config
)) {
5604 const struct smap_node
**nodes
= smap_sort(&config
);
5605 for (size_t k
= 0; k
< smap_count(&config
); k
++) {
5606 ds_put_format(ds
, "%c %s=%s", k
? ',' : ':',
5607 nodes
[k
]->key
, nodes
[k
]->value
);
5611 smap_destroy(&config
);
5613 ds_put_char(ds
, ')');
5614 ds_put_char(ds
, '\n');
5618 shash_destroy(&ofproto_shash
);
5623 ofproto_unixctl_dpif_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
5624 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
5626 struct ds ds
= DS_EMPTY_INITIALIZER
;
5627 const struct shash_node
**backers
;
5630 backers
= shash_sort(&all_dpif_backers
);
5631 for (i
= 0; i
< shash_count(&all_dpif_backers
); i
++) {
5632 dpif_show_backer(backers
[i
]->data
, &ds
);
5636 unixctl_command_reply(conn
, ds_cstr(&ds
));
5641 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn
*conn
,
5642 int argc OVS_UNUSED
, const char *argv
[],
5643 void *aux OVS_UNUSED
)
5645 const struct ofproto_dpif
*ofproto
;
5647 struct ds ds
= DS_EMPTY_INITIALIZER
;
5649 struct dpif_flow_dump
*flow_dump
;
5650 struct dpif_flow_dump_thread
*flow_dump_thread
;
5654 ofproto
= ofproto_dpif_lookup_by_name(argv
[argc
- 1]);
5656 unixctl_command_reply_error(conn
, "no such bridge");
5660 bool verbosity
= false;
5662 bool set_names
= false;
5663 for (int i
= 1; i
< argc
- 1; i
++) {
5664 if (!strcmp(argv
[i
], "-m")) {
5666 } else if (!strcmp(argv
[i
], "--names")) {
5669 } else if (!strcmp(argv
[i
], "--no-names")) {
5678 struct hmap
*portno_names
= NULL
;
5680 portno_names
= xmalloc(sizeof *portno_names
);
5681 hmap_init(portno_names
);
5683 struct dpif_port dpif_port
;
5684 struct dpif_port_dump port_dump
;
5685 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, ofproto
->backer
->dpif
) {
5686 odp_portno_names_set(portno_names
, dpif_port
.port_no
,
5692 flow_dump
= dpif_flow_dump_create(ofproto
->backer
->dpif
, false, NULL
);
5693 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
5694 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
5697 if (odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
) == ODP_FIT_ERROR
5698 || xlate_lookup_ofproto(ofproto
->backer
, &flow
, NULL
) != ofproto
) {
5703 odp_format_ufid(&f
.ufid
, &ds
);
5704 ds_put_cstr(&ds
, " ");
5706 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
5707 portno_names
, &ds
, verbosity
);
5708 ds_put_cstr(&ds
, ", ");
5709 dpif_flow_stats_format(&f
.stats
, &ds
);
5710 ds_put_cstr(&ds
, ", actions:");
5711 format_odp_actions(&ds
, f
.actions
, f
.actions_len
, portno_names
);
5712 ds_put_char(&ds
, '\n');
5714 dpif_flow_dump_thread_destroy(flow_dump_thread
);
5715 error
= dpif_flow_dump_destroy(flow_dump
);
5719 ds_put_format(&ds
, "dpif/dump_flows failed: %s", ovs_strerror(errno
));
5720 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
5722 unixctl_command_reply(conn
, ds_cstr(&ds
));
5725 odp_portno_names_destroy(portno_names
);
5726 hmap_destroy(portno_names
);
5733 ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn
*conn
,
5734 int argc
, const char *argv
[],
5735 void *aux OVS_UNUSED
)
5737 struct ds ds
= DS_EMPTY_INITIALIZER
;
5738 const char *br
= argv
[argc
-1];
5739 struct ofproto_dpif
*ofproto
= ofproto_dpif_lookup_by_name(br
);
5742 unixctl_command_reply_error(conn
, "no such bridge");
5746 dpif_show_support(&ofproto
->backer
->bt_support
, &ds
);
5747 unixctl_command_reply(conn
, ds_cstr(&ds
));
5751 ofproto_unixctl_dpif_set_dp_features(struct unixctl_conn
*conn
,
5752 int argc
, const char *argv
[],
5753 void *aux OVS_UNUSED
)
5755 struct ds ds
= DS_EMPTY_INITIALIZER
;
5756 const char *br
= argv
[1];
5757 const char *name
, *value
;
5758 struct ofproto_dpif
*ofproto
= ofproto_dpif_lookup_by_name(br
);
5762 unixctl_command_reply_error(conn
, "no such bridge");
5766 name
= argc
> 2 ? argv
[2] : NULL
;
5767 value
= argc
> 3 ? argv
[3] : NULL
;
5768 changed
= dpif_set_support(&ofproto
->backer
->rt_support
,
5769 &ofproto
->backer
->bt_support
,
5772 xlate_set_support(ofproto
, &ofproto
->backer
->rt_support
);
5773 udpif_flush(ofproto
->backer
->udpif
);
5775 unixctl_command_reply(conn
, ds_cstr(&ds
));
5780 ofproto_unixctl_init(void)
5782 static bool registered
;
5788 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5789 ofproto_unixctl_fdb_flush
, NULL
);
5790 unixctl_command_register("fdb/show", "bridge", 1, 1,
5791 ofproto_unixctl_fdb_show
, NULL
);
5792 unixctl_command_register("fdb/stats-clear", "[bridge]", 0, 1,
5793 ofproto_unixctl_fdb_stats_clear
, NULL
);
5794 unixctl_command_register("fdb/stats-show", "bridge", 1, 1,
5795 ofproto_unixctl_fdb_stats_show
, NULL
);
5796 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5797 ofproto_unixctl_mcast_snooping_flush
, NULL
);
5798 unixctl_command_register("mdb/show", "bridge", 1, 1,
5799 ofproto_unixctl_mcast_snooping_show
, NULL
);
5800 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5801 ofproto_unixctl_dpif_dump_dps
, NULL
);
5802 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show
,
5804 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
5805 ofproto_unixctl_dpif_show_dp_features
, NULL
);
5806 unixctl_command_register("dpif/dump-flows",
5807 "[-m] [--names | --no-names] bridge", 1, INT_MAX
,
5808 ofproto_unixctl_dpif_dump_flows
, NULL
);
5809 unixctl_command_register("dpif/set-dp-features", "bridge", 1, 3 ,
5810 ofproto_unixctl_dpif_set_dp_features
, NULL
);
5814 ofp_port_to_odp_port(const struct ofproto_dpif
*ofproto
, ofp_port_t ofp_port
)
5816 const struct ofport_dpif
*ofport
= ofp_port_to_ofport(ofproto
, ofp_port
);
5817 return ofport
? ofport
->odp_port
: ODPP_NONE
;
5820 struct ofport_dpif
*
5821 odp_port_to_ofport(const struct dpif_backer
*backer
, odp_port_t odp_port
)
5823 struct ofport_dpif
*port
;
5825 ovs_rwlock_rdlock(&backer
->odp_to_ofport_lock
);
5826 HMAP_FOR_EACH_IN_BUCKET (port
, odp_port_node
, hash_odp_port(odp_port
),
5827 &backer
->odp_to_ofport_map
) {
5828 if (port
->odp_port
== odp_port
) {
5829 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5834 ovs_rwlock_unlock(&backer
->odp_to_ofport_lock
);
5839 odp_port_to_ofp_port(const struct ofproto_dpif
*ofproto
, odp_port_t odp_port
)
5841 struct ofport_dpif
*port
;
5843 port
= odp_port_to_ofport(ofproto
->backer
, odp_port
);
5844 if (port
&& &ofproto
->up
== port
->up
.ofproto
) {
5845 return port
->up
.ofp_port
;
5851 /* 'match' is non-const to allow for temporary modifications. Any changes are
5852 * restored before returning. */
5854 ofproto_dpif_add_internal_flow(struct ofproto_dpif
*ofproto
,
5855 struct match
*match
, int priority
,
5856 uint16_t idle_timeout
,
5857 const struct ofpbuf
*ofpacts
,
5858 struct rule
**rulep
)
5860 struct ofputil_flow_mod fm
;
5861 struct rule_dpif
*rule
;
5864 fm
= (struct ofputil_flow_mod
) {
5865 .buffer_id
= UINT32_MAX
,
5866 .priority
= priority
,
5867 .table_id
= TBL_INTERNAL
,
5868 .command
= OFPFC_ADD
,
5869 .idle_timeout
= idle_timeout
,
5870 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
5871 .ofpacts
= ofpacts
->data
,
5872 .ofpacts_len
= ofpacts
->size
,
5874 minimatch_init(&fm
.match
, match
);
5875 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
5876 minimatch_destroy(&fm
.match
);
5879 VLOG_ERR_RL(&rl
, "failed to add internal flow (%s)",
5880 ofperr_to_string(error
));
5885 rule
= rule_dpif_lookup_in_table(ofproto
,
5886 ofproto_dpif_get_tables_version(ofproto
),
5887 TBL_INTERNAL
, &match
->flow
, &match
->wc
);
5897 ofproto_dpif_delete_internal_flow(struct ofproto_dpif
*ofproto
,
5898 struct match
*match
, int priority
)
5900 struct ofputil_flow_mod fm
;
5903 fm
= (struct ofputil_flow_mod
) {
5904 .buffer_id
= UINT32_MAX
,
5905 .priority
= priority
,
5906 .table_id
= TBL_INTERNAL
,
5907 .out_port
= OFPP_ANY
,
5908 .out_group
= OFPG_ANY
,
5909 .flags
= OFPUTIL_FF_HIDDEN_FIELDS
| OFPUTIL_FF_NO_READONLY
,
5910 .command
= OFPFC_DELETE_STRICT
,
5912 minimatch_init(&fm
.match
, match
);
5913 error
= ofproto_flow_mod(&ofproto
->up
, &fm
);
5914 minimatch_destroy(&fm
.match
);
5917 VLOG_ERR_RL(&rl
, "failed to delete internal flow (%s)",
5918 ofperr_to_string(error
));
5926 meter_get_features(const struct ofproto
*ofproto_
,
5927 struct ofputil_meter_features
*features
)
5929 const struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5931 dpif_meter_get_features(ofproto
->backer
->dpif
, features
);
5935 meter_set(struct ofproto
*ofproto_
, ofproto_meter_id
*meter_id
,
5936 struct ofputil_meter_config
*config
)
5938 struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5940 /* Provider ID unknown. Use backer to allocate a new DP meter */
5941 if (meter_id
->uint32
== UINT32_MAX
) {
5942 if (!ofproto
->backer
->meter_ids
) {
5943 return EFBIG
; /* Datapath does not support meter. */
5946 if(!id_pool_alloc_id(ofproto
->backer
->meter_ids
, &meter_id
->uint32
)) {
5947 return ENOMEM
; /* Can't allocate a DP meter. */
5951 switch (dpif_meter_set(ofproto
->backer
->dpif
, *meter_id
, config
)) {
5954 case EFBIG
: /* meter_id out of range */
5955 case ENOMEM
: /* Cannot allocate meter */
5956 return OFPERR_OFPMMFC_OUT_OF_METERS
;
5957 case EBADF
: /* Unsupported flags */
5958 return OFPERR_OFPMMFC_BAD_FLAGS
;
5959 case EINVAL
: /* Too many bands */
5960 return OFPERR_OFPMMFC_OUT_OF_BANDS
;
5961 case ENODEV
: /* Unsupported band type */
5962 return OFPERR_OFPMMFC_BAD_BAND
;
5963 case EDOM
: /* Rate must be non-zero */
5964 return OFPERR_OFPMMFC_BAD_RATE
;
5966 return OFPERR_OFPMMFC_UNKNOWN
;
5971 meter_get(const struct ofproto
*ofproto_
, ofproto_meter_id meter_id
,
5972 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
5974 const struct ofproto_dpif
*ofproto
= ofproto_dpif_cast(ofproto_
);
5976 if (!dpif_meter_get(ofproto
->backer
->dpif
, meter_id
, stats
, n_bands
)) {
5979 return OFPERR_OFPMMFC_UNKNOWN_METER
;
5982 struct free_meter_id_args
{
5983 struct ofproto_dpif
*ofproto
;
5984 ofproto_meter_id meter_id
;
5988 free_meter_id(struct free_meter_id_args
*args
)
5990 struct ofproto_dpif
*ofproto
= args
->ofproto
;
5992 dpif_meter_del(ofproto
->backer
->dpif
, args
->meter_id
, NULL
, 0);
5993 id_pool_free_id(ofproto
->backer
->meter_ids
, args
->meter_id
.uint32
);
5998 meter_del(struct ofproto
*ofproto_
, ofproto_meter_id meter_id
)
6000 struct free_meter_id_args
*arg
= xmalloc(sizeof *arg
);
6002 /* Before a meter can be deleted, Openflow spec requires all rules
6003 * referring to the meter to be (automatically) removed before the
6004 * meter is deleted. However, since vswitchd is multi-threaded,
6005 * those rules and their actions remain accessible by other threads,
6006 * especially by the handler and revalidator threads.
6007 * Postpone meter deletion after RCU grace period, so that ongoing
6008 * upcall translation or flow revalidation can complete. */
6009 arg
->ofproto
= ofproto_dpif_cast(ofproto_
);
6010 arg
->meter_id
= meter_id
;
6011 ovsrcu_postpone(free_meter_id
, arg
);
6014 const struct ofproto_class ofproto_dpif_class
= {
6028 NULL
, /* get_memory_usage. */
6029 type_get_memory_usage
,
6049 port_is_lacp_current
,
6050 port_get_lacp_stats
,
6051 NULL
, /* rule_choose_table */
6055 NULL
, /* rule_delete */
6060 packet_xlate_revert
,
6078 aa_vlan_get_queue_size
,
6085 get_stp_port_status
,
6090 get_rstp_port_status
,
6097 is_mirror_output_bundle
,
6098 forward_bpdu_changed
,
6099 set_mac_table_config
,
6101 set_mcast_snooping_port
,
6106 group_alloc
, /* group_alloc */
6107 group_construct
, /* group_construct */
6108 group_destruct
, /* group_destruct */
6109 group_dealloc
, /* group_dealloc */
6110 NULL
, /* group_modify */
6111 group_get_stats
, /* group_get_stats */
6112 get_datapath_version
, /* get_datapath_version */
6114 ct_flush
, /* ct_flush */