]> git.proxmox.com Git - ovs.git/blob - ofproto/ofproto-dpif.c
flow: Refactor flow_compose() API.
[ovs.git] / ofproto / ofproto-dpif.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include <errno.h>
19
20 #include "bfd.h"
21 #include "bond.h"
22 #include "bundle.h"
23 #include "byte-order.h"
24 #include "connectivity.h"
25 #include "connmgr.h"
26 #include "coverage.h"
27 #include "cfm.h"
28 #include "ct-dpif.h"
29 #include "fail-open.h"
30 #include "guarded-list.h"
31 #include "hmapx.h"
32 #include "lacp.h"
33 #include "learn.h"
34 #include "mac-learning.h"
35 #include "mcast-snooping.h"
36 #include "multipath.h"
37 #include "netdev-vport.h"
38 #include "netdev.h"
39 #include "netlink.h"
40 #include "nx-match.h"
41 #include "odp-util.h"
42 #include "odp-execute.h"
43 #include "ofproto/ofproto-dpif.h"
44 #include "ofproto/ofproto-provider.h"
45 #include "ofproto-dpif-ipfix.h"
46 #include "ofproto-dpif-mirror.h"
47 #include "ofproto-dpif-monitor.h"
48 #include "ofproto-dpif-rid.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "ofproto-dpif-trace.h"
51 #include "ofproto-dpif-upcall.h"
52 #include "ofproto-dpif-xlate.h"
53 #include "ofproto-dpif-xlate-cache.h"
54 #include "openvswitch/ofp-actions.h"
55 #include "openvswitch/dynamic-string.h"
56 #include "openvswitch/meta-flow.h"
57 #include "openvswitch/ofp-parse.h"
58 #include "openvswitch/ofp-print.h"
59 #include "openvswitch/ofp-util.h"
60 #include "openvswitch/ofpbuf.h"
61 #include "openvswitch/vlog.h"
62 #include "ovs-lldp.h"
63 #include "ovs-rcu.h"
64 #include "ovs-router.h"
65 #include "poll-loop.h"
66 #include "seq.h"
67 #include "simap.h"
68 #include "smap.h"
69 #include "timer.h"
70 #include "tunnel.h"
71 #include "unaligned.h"
72 #include "unixctl.h"
73 #include "util.h"
74 #include "vlan-bitmap.h"
75
76 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
77
78 COVERAGE_DEFINE(ofproto_dpif_expired);
79 COVERAGE_DEFINE(packet_in_overflow);
80
81 struct flow_miss;
82
83 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
84 long long int *used);
85 static struct rule_dpif *rule_dpif_cast(const struct rule *);
86 static void rule_expire(struct rule_dpif *, long long now);
87
88 struct ofbundle {
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. */
93
94 /* Configuration. */
95 struct ovs_list ports; /* Contains "struct ofport"s. */
96 enum port_vlan_mode vlan_mode; /* VLAN mode */
97 uint16_t qinq_ethtype;
98 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
99 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
100 * NULL if all VLANs are trunked. */
101 unsigned long *cvlans;
102 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
103 struct bond *bond; /* Nonnull iff more than one port. */
104 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
105
106 bool protected; /* Protected port mode */
107
108 /* Status. */
109 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
110 };
111
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 *);
120
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 *);
125
126 static void rstp_run(struct ofproto_dpif *ofproto);
127 static void set_rstp_port(struct ofport *,
128 const struct ofproto_port_rstp_settings *);
129
130 struct ofport_dpif {
131 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
132 struct ofport up;
133
134 odp_port_t odp_port;
135 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
136 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
137 struct cfm *cfm; /* Connectivity Fault Management, if any. */
138 struct bfd *bfd; /* BFD, if any. */
139 struct lldp *lldp; /* lldp, if any. */
140 bool may_enable; /* May be enabled in bonds. */
141 bool is_tunnel; /* This port is a tunnel. */
142 long long int carrier_seq; /* Carrier status changes. */
143 struct ofport_dpif *peer; /* Peer if patch port. */
144
145 /* Spanning tree. */
146 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
147 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
148 long long int stp_state_entered;
149
150 /* Rapid Spanning Tree. */
151 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
152 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
153
154 /* Queue to DSCP mapping. */
155 struct ofproto_port_queue *qdscp;
156 size_t n_qdscp;
157 };
158
159 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
160 ofp_port_t);
161
162 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
163 odp_port_t);
164
165 static struct ofport_dpif *
166 ofport_dpif_cast(const struct ofport *ofport)
167 {
168 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
169 }
170
171 static void port_run(struct ofport_dpif *);
172 static int set_bfd(struct ofport *, const struct smap *);
173 static int set_cfm(struct ofport *, const struct cfm_settings *);
174 static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
175 static void ofport_update_peer(struct ofport_dpif *);
176
177 COVERAGE_DEFINE(rev_reconfigure);
178 COVERAGE_DEFINE(rev_stp);
179 COVERAGE_DEFINE(rev_rstp);
180 COVERAGE_DEFINE(rev_bond);
181 COVERAGE_DEFINE(rev_port_toggled);
182 COVERAGE_DEFINE(rev_flow_table);
183 COVERAGE_DEFINE(rev_mac_learning);
184 COVERAGE_DEFINE(rev_mcast_snooping);
185
186 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
187 struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
188
189 /* All existing ofproto_dpif instances, indexed by ->up.name. */
190 struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
191
192 static bool ofproto_use_tnl_push_pop = true;
193 static void ofproto_unixctl_init(void);
194
195 static inline struct ofproto_dpif *
196 ofproto_dpif_cast(const struct ofproto *ofproto)
197 {
198 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
199 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
200 }
201
202 /* Global variables. */
203 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
204
205 /* Initial mappings of port to bridge mappings. */
206 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
207
208 /* Initialize 'ofm' for a learn action. If the rule already existed, reference
209 * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the
210 * rule reference in both cases. */
211 enum ofperr
212 ofproto_dpif_flow_mod_init_for_learn(struct ofproto_dpif *ofproto,
213 const struct ofputil_flow_mod *fm,
214 struct ofproto_flow_mod *ofm)
215 {
216 /* This will not take the global 'ofproto_mutex'. */
217 return ofproto_flow_mod_init_for_learn(&ofproto->up, fm, ofm);
218 }
219
220 /* Appends 'am' to the queue of asynchronous messages to be sent to the
221 * controller. Takes ownership of 'am' and any data it points to. */
222 void
223 ofproto_dpif_send_async_msg(struct ofproto_dpif *ofproto,
224 struct ofproto_async_msg *am)
225 {
226 if (!guarded_list_push_back(&ofproto->ams, &am->list_node, 1024)) {
227 COVERAGE_INC(packet_in_overflow);
228 ofproto_async_msg_free(am);
229 }
230
231 /* Wakes up main thread for packet-in I/O. */
232 seq_change(ofproto->ams_seq);
233 }
234 \f
235 /* Factory functions. */
236
237 static void
238 init(const struct shash *iface_hints)
239 {
240 struct shash_node *node;
241
242 /* Make a local copy, since we don't own 'iface_hints' elements. */
243 SHASH_FOR_EACH(node, iface_hints) {
244 const struct iface_hint *orig_hint = node->data;
245 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
246
247 new_hint->br_name = xstrdup(orig_hint->br_name);
248 new_hint->br_type = xstrdup(orig_hint->br_type);
249 new_hint->ofp_port = orig_hint->ofp_port;
250
251 shash_add(&init_ofp_ports, node->name, new_hint);
252 }
253
254 ofproto_unixctl_init();
255 ofproto_dpif_trace_init();
256 udpif_init();
257 }
258
259 static void
260 enumerate_types(struct sset *types)
261 {
262 dp_enumerate_types(types);
263 }
264
265 static int
266 enumerate_names(const char *type, struct sset *names)
267 {
268 struct ofproto_dpif *ofproto;
269
270 sset_clear(names);
271 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
272 if (strcmp(type, ofproto->up.type)) {
273 continue;
274 }
275 sset_add(names, ofproto->up.name);
276 }
277
278 return 0;
279 }
280
281 static int
282 del(const char *type, const char *name)
283 {
284 struct dpif *dpif;
285 int error;
286
287 error = dpif_open(name, type, &dpif);
288 if (!error) {
289 error = dpif_delete(dpif);
290 dpif_close(dpif);
291 }
292 return error;
293 }
294 \f
295 static const char *
296 port_open_type(const char *datapath_type, const char *port_type)
297 {
298 return dpif_port_open_type(datapath_type, port_type);
299 }
300
301 /* Type functions. */
302
303 static void process_dpif_port_changes(struct dpif_backer *);
304 static void process_dpif_all_ports_changed(struct dpif_backer *);
305 static void process_dpif_port_change(struct dpif_backer *,
306 const char *devname);
307 static void process_dpif_port_error(struct dpif_backer *, int error);
308
309 static struct ofproto_dpif *
310 lookup_ofproto_dpif_by_port_name(const char *name)
311 {
312 struct ofproto_dpif *ofproto;
313
314 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
315 if (sset_contains(&ofproto->ports, name)) {
316 return ofproto;
317 }
318 }
319
320 return NULL;
321 }
322
323 static int
324 type_run(const char *type)
325 {
326 struct dpif_backer *backer;
327
328 backer = shash_find_data(&all_dpif_backers, type);
329 if (!backer) {
330 /* This is not necessarily a problem, since backers are only
331 * created on demand. */
332 return 0;
333 }
334
335 if (dpif_run(backer->dpif)) {
336 backer->need_revalidate = REV_RECONFIGURE;
337 }
338
339 udpif_run(backer->udpif);
340
341 /* If vswitchd started with other_config:flow_restore_wait set as "true",
342 * and the configuration has now changed to "false", enable receiving
343 * packets from the datapath. */
344 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
345 int error;
346
347 backer->recv_set_enable = true;
348
349 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
350 if (error) {
351 VLOG_ERR("Failed to enable receiving packets in dpif.");
352 return error;
353 }
354 dpif_flow_flush(backer->dpif);
355 backer->need_revalidate = REV_RECONFIGURE;
356 }
357
358 if (backer->recv_set_enable) {
359 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
360 }
361
362 if (backer->need_revalidate) {
363 struct ofproto_dpif *ofproto;
364 struct simap_node *node;
365 struct simap tmp_backers;
366
367 /* Handle tunnel garbage collection. */
368 simap_init(&tmp_backers);
369 simap_swap(&backer->tnl_backers, &tmp_backers);
370
371 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
372 struct ofport_dpif *iter;
373
374 if (backer != ofproto->backer) {
375 continue;
376 }
377
378 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
379 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
380 const char *dp_port;
381
382 if (!iter->is_tunnel) {
383 continue;
384 }
385
386 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
387 namebuf, sizeof namebuf);
388 node = simap_find(&tmp_backers, dp_port);
389 if (node) {
390 simap_put(&backer->tnl_backers, dp_port, node->data);
391 simap_delete(&tmp_backers, node);
392 node = simap_find(&backer->tnl_backers, dp_port);
393 } else {
394 node = simap_find(&backer->tnl_backers, dp_port);
395 if (!node) {
396 odp_port_t odp_port = ODPP_NONE;
397
398 if (!dpif_port_add(backer->dpif, iter->up.netdev,
399 &odp_port)) {
400 simap_put(&backer->tnl_backers, dp_port,
401 odp_to_u32(odp_port));
402 node = simap_find(&backer->tnl_backers, dp_port);
403 }
404 }
405 }
406
407 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
408 if (tnl_port_reconfigure(iter, iter->up.netdev,
409 iter->odp_port,
410 ovs_native_tunneling_is_on(ofproto), dp_port)) {
411 backer->need_revalidate = REV_RECONFIGURE;
412 }
413 }
414 }
415
416 SIMAP_FOR_EACH (node, &tmp_backers) {
417 dpif_port_del(backer->dpif, u32_to_odp(node->data));
418 }
419 simap_destroy(&tmp_backers);
420
421 switch (backer->need_revalidate) {
422 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
423 case REV_STP: COVERAGE_INC(rev_stp); break;
424 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
425 case REV_BOND: COVERAGE_INC(rev_bond); break;
426 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
427 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
428 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
429 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
430 }
431 backer->need_revalidate = 0;
432
433 xlate_txn_start();
434 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
435 struct ofport_dpif *ofport;
436 struct ofbundle *bundle;
437
438 if (ofproto->backer != backer) {
439 continue;
440 }
441
442 xlate_ofproto_set(ofproto, ofproto->up.name,
443 ofproto->backer->dpif, ofproto->ml,
444 ofproto->stp, ofproto->rstp, ofproto->ms,
445 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
446 ofproto->netflow,
447 ofproto->up.forward_bpdu,
448 connmgr_has_in_band(ofproto->up.connmgr),
449 &ofproto->backer->support);
450
451 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
452 xlate_bundle_set(ofproto, bundle, bundle->name,
453 bundle->vlan_mode, bundle->qinq_ethtype,
454 bundle->vlan, bundle->trunks, bundle->cvlans,
455 bundle->use_priority_tags,
456 bundle->bond, bundle->lacp,
457 bundle->floodable, bundle->protected);
458 }
459
460 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
461 int stp_port = ofport->stp_port
462 ? stp_port_no(ofport->stp_port)
463 : -1;
464 xlate_ofport_set(ofproto, ofport->bundle, ofport,
465 ofport->up.ofp_port, ofport->odp_port,
466 ofport->up.netdev, ofport->cfm, ofport->bfd,
467 ofport->lldp, ofport->peer, stp_port,
468 ofport->rstp_port, ofport->qdscp,
469 ofport->n_qdscp, ofport->up.pp.config,
470 ofport->up.pp.state, ofport->is_tunnel,
471 ofport->may_enable);
472 }
473 }
474 xlate_txn_commit();
475
476 udpif_revalidate(backer->udpif);
477 }
478
479 process_dpif_port_changes(backer);
480
481 return 0;
482 }
483
484 /* Check for and handle port changes in 'backer''s dpif. */
485 static void
486 process_dpif_port_changes(struct dpif_backer *backer)
487 {
488 for (;;) {
489 char *devname;
490 int error;
491
492 error = dpif_port_poll(backer->dpif, &devname);
493 switch (error) {
494 case EAGAIN:
495 return;
496
497 case ENOBUFS:
498 process_dpif_all_ports_changed(backer);
499 break;
500
501 case 0:
502 process_dpif_port_change(backer, devname);
503 free(devname);
504 break;
505
506 default:
507 process_dpif_port_error(backer, error);
508 break;
509 }
510 }
511 }
512
513 static void
514 process_dpif_all_ports_changed(struct dpif_backer *backer)
515 {
516 struct ofproto_dpif *ofproto;
517 struct dpif_port dpif_port;
518 struct dpif_port_dump dump;
519 struct sset devnames;
520 const char *devname;
521
522 sset_init(&devnames);
523 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
524 if (ofproto->backer == backer) {
525 struct ofport *ofport;
526
527 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
528 sset_add(&devnames, netdev_get_name(ofport->netdev));
529 }
530 }
531 }
532 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
533 sset_add(&devnames, dpif_port.name);
534 }
535
536 SSET_FOR_EACH (devname, &devnames) {
537 process_dpif_port_change(backer, devname);
538 }
539 sset_destroy(&devnames);
540 }
541
542 static void
543 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
544 {
545 struct ofproto_dpif *ofproto;
546 struct dpif_port port;
547
548 /* Don't report on the datapath's device. */
549 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
550 return;
551 }
552
553 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
554 &all_ofproto_dpifs) {
555 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
556 return;
557 }
558 }
559
560 ofproto = lookup_ofproto_dpif_by_port_name(devname);
561 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
562 /* The port was removed. If we know the datapath,
563 * report it through poll_set(). If we don't, it may be
564 * notifying us of a removal we initiated, so ignore it.
565 * If there's a pending ENOBUFS, let it stand, since
566 * everything will be reevaluated. */
567 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
568 sset_add(&ofproto->port_poll_set, devname);
569 ofproto->port_poll_errno = 0;
570 }
571 } else if (!ofproto) {
572 /* The port was added, but we don't know with which
573 * ofproto we should associate it. Delete it. */
574 dpif_port_del(backer->dpif, port.port_no);
575 } else {
576 struct ofport_dpif *ofport;
577
578 ofport = ofport_dpif_cast(shash_find_data(
579 &ofproto->up.port_by_name, devname));
580 if (ofport
581 && ofport->odp_port != port.port_no
582 && !odp_port_to_ofport(backer, port.port_no))
583 {
584 /* 'ofport''s datapath port number has changed from
585 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
586 * structures to match. */
587 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
588 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
589 ofport->odp_port = port.port_no;
590 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
591 hash_odp_port(port.port_no));
592 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
593 backer->need_revalidate = REV_RECONFIGURE;
594 }
595 }
596 dpif_port_destroy(&port);
597 }
598
599 /* Propagate 'error' to all ofprotos based on 'backer'. */
600 static void
601 process_dpif_port_error(struct dpif_backer *backer, int error)
602 {
603 struct ofproto_dpif *ofproto;
604
605 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
606 if (ofproto->backer == backer) {
607 sset_clear(&ofproto->port_poll_set);
608 ofproto->port_poll_errno = error;
609 }
610 }
611 }
612
613 static void
614 type_wait(const char *type)
615 {
616 struct dpif_backer *backer;
617
618 backer = shash_find_data(&all_dpif_backers, type);
619 if (!backer) {
620 /* This is not necessarily a problem, since backers are only
621 * created on demand. */
622 return;
623 }
624
625 dpif_wait(backer->dpif);
626 }
627 \f
628 /* Basic life-cycle. */
629
630 static int add_internal_flows(struct ofproto_dpif *);
631
632 static struct ofproto *
633 alloc(void)
634 {
635 struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
636 return &ofproto->up;
637 }
638
639 static void
640 dealloc(struct ofproto *ofproto_)
641 {
642 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
643 free(ofproto);
644 }
645
646 static void
647 close_dpif_backer(struct dpif_backer *backer, bool del)
648 {
649 ovs_assert(backer->refcount > 0);
650
651 if (--backer->refcount) {
652 return;
653 }
654
655 udpif_destroy(backer->udpif);
656
657 simap_destroy(&backer->tnl_backers);
658 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
659 hmap_destroy(&backer->odp_to_ofport_map);
660 shash_find_and_delete(&all_dpif_backers, backer->type);
661 free(backer->type);
662 free(backer->dp_version_string);
663 if (del) {
664 dpif_delete(backer->dpif);
665 }
666 dpif_close(backer->dpif);
667 id_pool_destroy(backer->meter_ids);
668 free(backer);
669 }
670
671 /* Datapath port slated for removal from datapath. */
672 struct odp_garbage {
673 struct ovs_list list_node;
674 odp_port_t odp_port;
675 };
676
677 static bool check_variable_length_userdata(struct dpif_backer *backer);
678 static void check_support(struct dpif_backer *backer);
679
680 static int
681 open_dpif_backer(const char *type, struct dpif_backer **backerp)
682 {
683 struct dpif_backer *backer;
684 struct dpif_port_dump port_dump;
685 struct dpif_port port;
686 struct shash_node *node;
687 struct ovs_list garbage_list;
688 struct odp_garbage *garbage;
689
690 struct sset names;
691 char *backer_name;
692 const char *name;
693 int error;
694
695 backer = shash_find_data(&all_dpif_backers, type);
696 if (backer) {
697 backer->refcount++;
698 *backerp = backer;
699 return 0;
700 }
701
702 backer_name = xasprintf("ovs-%s", type);
703
704 /* Remove any existing datapaths, since we assume we're the only
705 * userspace controlling the datapath. */
706 sset_init(&names);
707 dp_enumerate_names(type, &names);
708 SSET_FOR_EACH(name, &names) {
709 struct dpif *old_dpif;
710
711 /* Don't remove our backer if it exists. */
712 if (!strcmp(name, backer_name)) {
713 continue;
714 }
715
716 if (dpif_open(name, type, &old_dpif)) {
717 VLOG_WARN("couldn't open old datapath %s to remove it", name);
718 } else {
719 dpif_delete(old_dpif);
720 dpif_close(old_dpif);
721 }
722 }
723 sset_destroy(&names);
724
725 backer = xmalloc(sizeof *backer);
726
727 error = dpif_create_and_open(backer_name, type, &backer->dpif);
728 free(backer_name);
729 if (error) {
730 VLOG_ERR("failed to open datapath of type %s: %s", type,
731 ovs_strerror(error));
732 free(backer);
733 return error;
734 }
735 backer->udpif = udpif_create(backer, backer->dpif);
736
737 backer->type = xstrdup(type);
738 backer->refcount = 1;
739 hmap_init(&backer->odp_to_ofport_map);
740 ovs_rwlock_init(&backer->odp_to_ofport_lock);
741 backer->need_revalidate = 0;
742 simap_init(&backer->tnl_backers);
743 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
744 *backerp = backer;
745
746 if (backer->recv_set_enable) {
747 dpif_flow_flush(backer->dpif);
748 }
749
750 /* Loop through the ports already on the datapath and remove any
751 * that we don't need anymore. */
752 ovs_list_init(&garbage_list);
753 dpif_port_dump_start(&port_dump, backer->dpif);
754 while (dpif_port_dump_next(&port_dump, &port)) {
755 node = shash_find(&init_ofp_ports, port.name);
756 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
757 garbage = xmalloc(sizeof *garbage);
758 garbage->odp_port = port.port_no;
759 ovs_list_push_front(&garbage_list, &garbage->list_node);
760 }
761 }
762 dpif_port_dump_done(&port_dump);
763
764 LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
765 dpif_port_del(backer->dpif, garbage->odp_port);
766 free(garbage);
767 }
768
769 shash_add(&all_dpif_backers, type, backer);
770
771 check_support(backer);
772 atomic_count_init(&backer->tnl_count, 0);
773
774 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
775 if (error) {
776 VLOG_ERR("failed to listen on datapath of type %s: %s",
777 type, ovs_strerror(error));
778 close_dpif_backer(backer, false);
779 return error;
780 }
781
782 if (backer->recv_set_enable) {
783 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
784 }
785
786 /* This check fails if performed before udpif threads have been set,
787 * as the kernel module checks that the 'pid' in userspace action
788 * is non-zero. */
789 backer->support.variable_length_userdata
790 = check_variable_length_userdata(backer);
791 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
792
793 /* Manage Datapath meter IDs if supported. */
794 struct ofputil_meter_features features;
795 dpif_meter_get_features(backer->dpif, &features);
796 if (features.max_meters) {
797 backer->meter_ids = id_pool_create(0, features.max_meters);
798 } else {
799 backer->meter_ids = NULL;
800 }
801
802 return error;
803 }
804
805 bool
806 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
807 {
808 return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
809 atomic_count_get(&ofproto->backer->tnl_count);
810 }
811
812 /* Tests whether 'backer''s datapath supports recirculation. Only newer
813 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
814 * features on older datapaths that don't support this feature.
815 *
816 * Returns false if 'backer' definitely does not support recirculation, true if
817 * it seems to support recirculation or if at least the error we get is
818 * ambiguous. */
819 static bool
820 check_recirc(struct dpif_backer *backer)
821 {
822 struct flow flow;
823 struct odputil_keybuf keybuf;
824 struct ofpbuf key;
825 bool enable_recirc;
826 struct odp_flow_key_parms odp_parms = {
827 .flow = &flow,
828 .support = {
829 .recirc = true,
830 },
831 };
832
833 memset(&flow, 0, sizeof flow);
834 flow.recirc_id = 1;
835 flow.dp_hash = 1;
836
837 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
838 odp_flow_key_from_flow(&odp_parms, &key);
839 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
840 NULL, NULL);
841
842 if (enable_recirc) {
843 VLOG_INFO("%s: Datapath supports recirculation",
844 dpif_name(backer->dpif));
845 } else {
846 VLOG_INFO("%s: Datapath does not support recirculation",
847 dpif_name(backer->dpif));
848 }
849
850 return enable_recirc;
851 }
852
853 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
854 * some flow attributes for datapaths that support this feature.
855 *
856 * Returns true if 'dpif' supports UFID for flow operations.
857 * Returns false if 'dpif' does not support UFID. */
858 static bool
859 check_ufid(struct dpif_backer *backer)
860 {
861 struct flow flow;
862 struct odputil_keybuf keybuf;
863 struct ofpbuf key;
864 ovs_u128 ufid;
865 bool enable_ufid;
866 struct odp_flow_key_parms odp_parms = {
867 .flow = &flow,
868 };
869
870 memset(&flow, 0, sizeof flow);
871 flow.dl_type = htons(0x1234);
872
873 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
874 odp_flow_key_from_flow(&odp_parms, &key);
875 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
876
877 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, NULL, &ufid);
878
879 if (enable_ufid) {
880 VLOG_INFO("%s: Datapath supports unique flow ids",
881 dpif_name(backer->dpif));
882 } else {
883 VLOG_INFO("%s: Datapath does not support unique flow ids",
884 dpif_name(backer->dpif));
885 }
886 return enable_ufid;
887 }
888
889 /* Tests whether 'backer''s datapath supports variable-length
890 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
891 * to disable some features on older datapaths that don't support this
892 * feature.
893 *
894 * Returns false if 'backer' definitely does not support variable-length
895 * userdata, true if it seems to support them or if at least the error we get
896 * is ambiguous. */
897 static bool
898 check_variable_length_userdata(struct dpif_backer *backer)
899 {
900 struct eth_header *eth;
901 struct ofpbuf actions;
902 struct dpif_execute execute;
903 struct dp_packet packet;
904 struct flow flow;
905 size_t start;
906 int error;
907
908 /* Compose a userspace action that will cause an ERANGE error on older
909 * datapaths that don't support variable-length userdata.
910 *
911 * We really test for using userdata longer than 8 bytes, but older
912 * datapaths accepted these, silently truncating the userdata to 8 bytes.
913 * The same older datapaths rejected userdata shorter than 8 bytes, so we
914 * test for that instead as a proxy for longer userdata support. */
915 ofpbuf_init(&actions, 64);
916 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
917 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
918 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
919 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
920 nl_msg_end_nested(&actions, start);
921
922 /* Compose a dummy ethernet packet. */
923 dp_packet_init(&packet, ETH_HEADER_LEN);
924 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
925 eth->eth_type = htons(0x1234);
926
927 flow_extract(&packet, &flow);
928
929 /* Execute the actions. On older datapaths this fails with ERANGE, on
930 * newer datapaths it succeeds. */
931 execute.actions = actions.data;
932 execute.actions_len = actions.size;
933 execute.packet = &packet;
934 execute.flow = &flow;
935 execute.needs_help = false;
936 execute.probe = true;
937 execute.mtu = 0;
938
939 error = dpif_execute(backer->dpif, &execute);
940
941 dp_packet_uninit(&packet);
942 ofpbuf_uninit(&actions);
943
944 switch (error) {
945 case 0:
946 return true;
947
948 case ERANGE:
949 /* Variable-length userdata is not supported. */
950 VLOG_WARN("%s: datapath does not support variable-length userdata "
951 "feature (needs Linux 3.10+ or kernel module from OVS "
952 "1..11+). The NXAST_SAMPLE action will be ignored.",
953 dpif_name(backer->dpif));
954 return false;
955
956 default:
957 /* Something odd happened. We're not sure whether variable-length
958 * userdata is supported. Default to "yes". */
959 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
960 dpif_name(backer->dpif), ovs_strerror(error));
961 return true;
962 }
963 }
964
965 /* Tests number of 802.1q VLAN headers supported by 'backer''s datapath.
966 *
967 * Returns the number of elements in a struct flow's vlan
968 * if the datapath supports at least that many VLAN headers. */
969 static size_t
970 check_max_vlan_headers(struct dpif_backer *backer)
971 {
972 struct flow flow;
973 struct odp_flow_key_parms odp_parms = {
974 .flow = &flow,
975 .probe = true,
976 };
977 int n;
978
979 memset(&flow, 0, sizeof flow);
980 flow.dl_type = htons(ETH_TYPE_IP);
981 for (n = 0; n < FLOW_MAX_VLAN_HEADERS; n++) {
982 struct odputil_keybuf keybuf;
983 struct ofpbuf key;
984
985 flow_push_vlan_uninit(&flow, NULL);
986 flow.vlans[0].tpid = htons(ETH_TYPE_VLAN);
987 flow.vlans[0].tci = htons(1) | htons(VLAN_CFI);
988
989 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
990 odp_flow_key_from_flow(&odp_parms, &key);
991 if (!dpif_probe_feature(backer->dpif, "VLAN", &key, NULL, NULL)) {
992 break;
993 }
994 }
995
996 VLOG_INFO("%s: VLAN header stack length probed as %d",
997 dpif_name(backer->dpif), n);
998 return n;
999 }
1000 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1001 *
1002 * Returns the number of elements in a struct flow's mpls_lse field
1003 * if the datapath supports at least that many entries in an
1004 * MPLS label stack.
1005 * Otherwise returns the number of MPLS push actions supported by
1006 * the datapath. */
1007 static size_t
1008 check_max_mpls_depth(struct dpif_backer *backer)
1009 {
1010 struct flow flow;
1011 int n;
1012
1013 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1014 struct odputil_keybuf keybuf;
1015 struct ofpbuf key;
1016 struct odp_flow_key_parms odp_parms = {
1017 .flow = &flow,
1018 };
1019
1020 memset(&flow, 0, sizeof flow);
1021 flow.dl_type = htons(ETH_TYPE_MPLS);
1022 flow_set_mpls_bos(&flow, n, 1);
1023
1024 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1025 odp_flow_key_from_flow(&odp_parms, &key);
1026 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL, NULL)) {
1027 break;
1028 }
1029 }
1030
1031 VLOG_INFO("%s: MPLS label stack length probed as %d",
1032 dpif_name(backer->dpif), n);
1033 return n;
1034 }
1035
1036 static void
1037 add_sample_actions(struct ofpbuf *actions, int nesting)
1038 {
1039 if (nesting == 0) {
1040 nl_msg_put_odp_port(actions, OVS_ACTION_ATTR_OUTPUT, u32_to_odp(1));
1041 return;
1042 }
1043
1044 size_t start, actions_start;
1045
1046 start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1047 actions_start = nl_msg_start_nested(actions, OVS_SAMPLE_ATTR_ACTIONS);
1048 add_sample_actions(actions, nesting - 1);
1049 nl_msg_end_nested(actions, actions_start);
1050 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY, UINT32_MAX);
1051 nl_msg_end_nested(actions, start);
1052 }
1053
1054 /* Tests the nested sample actions levels supported by 'backer''s datapath.
1055 *
1056 * Returns the number of nested sample actions accepted by the datapath. */
1057 static size_t
1058 check_max_sample_nesting(struct dpif_backer *backer)
1059 {
1060 struct odputil_keybuf keybuf;
1061 struct ofpbuf key;
1062 struct flow flow;
1063 int n;
1064
1065 struct odp_flow_key_parms odp_parms = {
1066 .flow = &flow,
1067 };
1068
1069 memset(&flow, 0, sizeof flow);
1070 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1071 odp_flow_key_from_flow(&odp_parms, &key);
1072
1073 /* OVS datapath has always supported at least 3 nested levels. */
1074 for (n = 3; n < FLOW_MAX_SAMPLE_NESTING; n++) {
1075 struct ofpbuf actions;
1076 bool ok;
1077
1078 ofpbuf_init(&actions, 300);
1079 add_sample_actions(&actions, n);
1080 ok = dpif_probe_feature(backer->dpif, "Sample action nesting", &key,
1081 &actions, NULL);
1082 ofpbuf_uninit(&actions);
1083 if (!ok) {
1084 break;
1085 }
1086 }
1087
1088 VLOG_INFO("%s: Max sample nesting level probed as %d",
1089 dpif_name(backer->dpif), n);
1090 return n;
1091 }
1092
1093 /* Tests whether 'backer''s datapath supports masked data in
1094 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1095 * datapaths that don't support this feature. */
1096 static bool
1097 check_masked_set_action(struct dpif_backer *backer)
1098 {
1099 struct eth_header *eth;
1100 struct ofpbuf actions;
1101 struct dpif_execute execute;
1102 struct dp_packet packet;
1103 struct flow flow;
1104 int error;
1105 struct ovs_key_ethernet key, mask;
1106
1107 /* Compose a set action that will cause an EINVAL error on older
1108 * datapaths that don't support masked set actions.
1109 * Avoid using a full mask, as it could be translated to a non-masked
1110 * set action instead. */
1111 ofpbuf_init(&actions, 64);
1112 memset(&key, 0x53, sizeof key);
1113 memset(&mask, 0x7f, sizeof mask);
1114 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1115 sizeof key);
1116
1117 /* Compose a dummy ethernet packet. */
1118 dp_packet_init(&packet, ETH_HEADER_LEN);
1119 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1120 eth->eth_type = htons(0x1234);
1121
1122 flow_extract(&packet, &flow);
1123
1124 /* Execute the actions. On older datapaths this fails with EINVAL, on
1125 * newer datapaths it succeeds. */
1126 execute.actions = actions.data;
1127 execute.actions_len = actions.size;
1128 execute.packet = &packet;
1129 execute.flow = &flow;
1130 execute.needs_help = false;
1131 execute.probe = true;
1132 execute.mtu = 0;
1133
1134 error = dpif_execute(backer->dpif, &execute);
1135
1136 dp_packet_uninit(&packet);
1137 ofpbuf_uninit(&actions);
1138
1139 if (error) {
1140 /* Masked set action is not supported. */
1141 VLOG_INFO("%s: datapath does not support masked set action feature.",
1142 dpif_name(backer->dpif));
1143 }
1144 return !error;
1145 }
1146
1147 /* Tests whether 'backer''s datapath supports truncation of a packet in
1148 * OVS_ACTION_ATTR_TRUNC. We need to disable some features on older
1149 * datapaths that don't support this feature. */
1150 static bool
1151 check_trunc_action(struct dpif_backer *backer)
1152 {
1153 struct eth_header *eth;
1154 struct ofpbuf actions;
1155 struct dpif_execute execute;
1156 struct dp_packet packet;
1157 struct ovs_action_trunc *trunc;
1158 struct flow flow;
1159 int error;
1160
1161 /* Compose an action with output(port:1,
1162 * max_len:OVS_ACTION_OUTPUT_MIN + 1).
1163 * This translates to one truncate action and one output action. */
1164 ofpbuf_init(&actions, 64);
1165 trunc = nl_msg_put_unspec_uninit(&actions,
1166 OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
1167
1168 trunc->max_len = ETH_HEADER_LEN + 1;
1169 nl_msg_put_odp_port(&actions, OVS_ACTION_ATTR_OUTPUT, u32_to_odp(1));
1170
1171 /* Compose a dummy Ethernet packet. */
1172 dp_packet_init(&packet, ETH_HEADER_LEN);
1173 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1174 eth->eth_type = htons(0x1234);
1175
1176 flow_extract(&packet, &flow);
1177
1178 /* Execute the actions. On older datapaths this fails with EINVAL, on
1179 * newer datapaths it succeeds. */
1180 execute.actions = actions.data;
1181 execute.actions_len = actions.size;
1182 execute.packet = &packet;
1183 execute.flow = &flow;
1184 execute.needs_help = false;
1185 execute.probe = true;
1186 execute.mtu = 0;
1187
1188 error = dpif_execute(backer->dpif, &execute);
1189
1190 dp_packet_uninit(&packet);
1191 ofpbuf_uninit(&actions);
1192
1193 if (error) {
1194 VLOG_INFO("%s: Datapath does not support truncate action",
1195 dpif_name(backer->dpif));
1196 } else {
1197 VLOG_INFO("%s: Datapath supports truncate action",
1198 dpif_name(backer->dpif));
1199 }
1200
1201 return !error;
1202 }
1203
1204 /* Tests whether 'backer''s datapath supports the clone action
1205 * OVS_ACTION_ATTR_CLONE. */
1206 static bool
1207 check_clone(struct dpif_backer *backer)
1208 {
1209 struct dpif_execute execute;
1210 struct eth_header *eth;
1211 struct flow flow;
1212 struct dp_packet packet;
1213 struct ofpbuf actions;
1214 size_t clone_start;
1215 int error;
1216
1217 /* Compose clone with an empty action list.
1218 * and check if datapath can decode the message. */
1219 ofpbuf_init(&actions, 64);
1220 clone_start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_CLONE);
1221 nl_msg_end_nested(&actions, clone_start);
1222
1223 /* Compose a dummy Ethernet packet. */
1224 dp_packet_init(&packet, ETH_HEADER_LEN);
1225 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1226 eth->eth_type = htons(0x1234);
1227
1228 flow_extract(&packet, &flow);
1229
1230 /* Execute the actions. On older datapaths this fails with EINVAL, on
1231 * newer datapaths it succeeds. */
1232 execute.actions = actions.data;
1233 execute.actions_len = actions.size;
1234 execute.packet = &packet;
1235 execute.flow = &flow;
1236 execute.needs_help = false;
1237 execute.probe = true;
1238 execute.mtu = 0;
1239
1240 error = dpif_execute(backer->dpif, &execute);
1241
1242 dp_packet_uninit(&packet);
1243 ofpbuf_uninit(&actions);
1244
1245 if (error) {
1246 VLOG_INFO("%s: Datapath does not support clone action",
1247 dpif_name(backer->dpif));
1248 } else {
1249 VLOG_INFO("%s: Datapath supports clone action",
1250 dpif_name(backer->dpif));
1251 }
1252
1253 return !error;
1254 }
1255
1256 /* Tests whether 'backer''s datapath supports the OVS_CT_ATTR_EVENTMASK
1257 * attribute in OVS_ACTION_ATTR_CT. */
1258 static bool
1259 check_ct_eventmask(struct dpif_backer *backer)
1260 {
1261 struct dpif_execute execute;
1262 struct dp_packet packet;
1263 struct ofpbuf actions;
1264 struct flow flow = {
1265 .dl_type = CONSTANT_HTONS(ETH_TYPE_IP),
1266 .nw_proto = IPPROTO_UDP,
1267 .nw_ttl = 64,
1268 /* Use the broadcast address on the loopback address range 127/8 to
1269 * avoid hitting any real conntrack entries. We leave the UDP ports to
1270 * zeroes for the same purpose. */
1271 .nw_src = CONSTANT_HTONL(0x7fffffff),
1272 .nw_dst = CONSTANT_HTONL(0x7fffffff),
1273 };
1274 size_t ct_start;
1275 int error;
1276
1277 /* Compose CT action with eventmask attribute and check if datapath can
1278 * decode the message. */
1279 ofpbuf_init(&actions, 64);
1280 ct_start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_CT);
1281 /* Eventmask has no effect without the commit flag, but currently the
1282 * datapath will accept an eventmask even without commit. This is useful
1283 * as we do not want to persist the probe connection in the conntrack
1284 * table. */
1285 nl_msg_put_u32(&actions, OVS_CT_ATTR_EVENTMASK, ~0);
1286 nl_msg_end_nested(&actions, ct_start);
1287
1288 /* Compose a dummy UDP packet. */
1289 dp_packet_init(&packet, 0);
1290 flow_compose(&packet, &flow, 0);
1291
1292 /* Execute the actions. On older datapaths this fails with EINVAL, on
1293 * newer datapaths it succeeds. */
1294 execute.actions = actions.data;
1295 execute.actions_len = actions.size;
1296 execute.packet = &packet;
1297 execute.flow = &flow;
1298 execute.needs_help = false;
1299 execute.probe = true;
1300 execute.mtu = 0;
1301
1302 error = dpif_execute(backer->dpif, &execute);
1303
1304 dp_packet_uninit(&packet);
1305 ofpbuf_uninit(&actions);
1306
1307 if (error) {
1308 VLOG_INFO("%s: Datapath does not support eventmask in conntrack action",
1309 dpif_name(backer->dpif));
1310 } else {
1311 VLOG_INFO("%s: Datapath supports eventmask in conntrack action",
1312 dpif_name(backer->dpif));
1313 }
1314
1315 return !error;
1316 }
1317
1318 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE, ETHTYPE) \
1319 static bool \
1320 check_##NAME(struct dpif_backer *backer) \
1321 { \
1322 struct flow flow; \
1323 struct odputil_keybuf keybuf; \
1324 struct ofpbuf key; \
1325 bool enable; \
1326 struct odp_flow_key_parms odp_parms = { \
1327 .flow = &flow, \
1328 .support = { \
1329 .SUPPORT = true, \
1330 }, \
1331 }; \
1332 \
1333 memset(&flow, 0, sizeof flow); \
1334 flow.FIELD = VALUE; \
1335 flow.dl_type = htons(ETHTYPE); \
1336 \
1337 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1338 odp_flow_key_from_flow(&odp_parms, &key); \
1339 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL, NULL); \
1340 \
1341 if (enable) { \
1342 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1343 } else { \
1344 VLOG_INFO("%s: Datapath does not support "#NAME, \
1345 dpif_name(backer->dpif)); \
1346 } \
1347 \
1348 return enable; \
1349 }
1350 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1, \
1351 ETH_TYPE_IP)
1352
1353 CHECK_FEATURE(ct_state)
1354 CHECK_FEATURE(ct_zone)
1355 CHECK_FEATURE(ct_mark)
1356 CHECK_FEATURE__(ct_label, ct_label, ct_label.u64.lo, 1, ETH_TYPE_IP)
1357 CHECK_FEATURE__(ct_state_nat, ct_state, ct_state, \
1358 CS_TRACKED|CS_SRC_NAT, ETH_TYPE_IP)
1359 CHECK_FEATURE__(ct_orig_tuple, ct_orig_tuple, ct_nw_proto, 1, ETH_TYPE_IP)
1360 CHECK_FEATURE__(ct_orig_tuple6, ct_orig_tuple6, ct_nw_proto, 1, ETH_TYPE_IPV6)
1361
1362 #undef CHECK_FEATURE
1363 #undef CHECK_FEATURE__
1364
1365 static void
1366 check_support(struct dpif_backer *backer)
1367 {
1368 /* This feature needs to be tested after udpif threads are set. */
1369 backer->support.variable_length_userdata = false;
1370
1371 /* Actions. */
1372 backer->support.odp.recirc = check_recirc(backer);
1373 backer->support.odp.max_vlan_headers = check_max_vlan_headers(backer);
1374 backer->support.odp.max_mpls_depth = check_max_mpls_depth(backer);
1375 backer->support.masked_set_action = check_masked_set_action(backer);
1376 backer->support.trunc = check_trunc_action(backer);
1377 backer->support.ufid = check_ufid(backer);
1378 backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1379 backer->support.clone = check_clone(backer);
1380 backer->support.sample_nesting = check_max_sample_nesting(backer);
1381 backer->support.ct_eventmask = check_ct_eventmask(backer);
1382
1383 /* Flow fields. */
1384 backer->support.odp.ct_state = check_ct_state(backer);
1385 backer->support.odp.ct_zone = check_ct_zone(backer);
1386 backer->support.odp.ct_mark = check_ct_mark(backer);
1387 backer->support.odp.ct_label = check_ct_label(backer);
1388
1389 backer->support.odp.ct_state_nat = check_ct_state_nat(backer);
1390 backer->support.odp.ct_orig_tuple = check_ct_orig_tuple(backer);
1391 backer->support.odp.ct_orig_tuple6 = check_ct_orig_tuple6(backer);
1392 }
1393
1394 static int
1395 construct(struct ofproto *ofproto_)
1396 {
1397 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1398 struct shash_node *node, *next;
1399 int error;
1400
1401 /* Tunnel module can get used right after the udpif threads are running. */
1402 ofproto_tunnel_init();
1403
1404 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1405 if (error) {
1406 return error;
1407 }
1408
1409 uuid_generate(&ofproto->uuid);
1410 atomic_init(&ofproto->tables_version, OVS_VERSION_MIN);
1411 ofproto->netflow = NULL;
1412 ofproto->sflow = NULL;
1413 ofproto->ipfix = NULL;
1414 ofproto->stp = NULL;
1415 ofproto->rstp = NULL;
1416 ofproto->dump_seq = 0;
1417 hmap_init(&ofproto->bundles);
1418 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1419 ofproto->ms = NULL;
1420 ofproto->mbridge = mbridge_create();
1421 ofproto->has_bonded_bundles = false;
1422 ofproto->lacp_enabled = false;
1423 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1424
1425 guarded_list_init(&ofproto->ams);
1426
1427 sset_init(&ofproto->ports);
1428 sset_init(&ofproto->ghost_ports);
1429 sset_init(&ofproto->port_poll_set);
1430 ofproto->port_poll_errno = 0;
1431 ofproto->change_seq = 0;
1432 ofproto->ams_seq = seq_create();
1433 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1434
1435
1436 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1437 struct iface_hint *iface_hint = node->data;
1438
1439 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1440 /* Check if the datapath already has this port. */
1441 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1442 sset_add(&ofproto->ports, node->name);
1443 }
1444
1445 free(iface_hint->br_name);
1446 free(iface_hint->br_type);
1447 free(iface_hint);
1448 shash_delete(&init_ofp_ports, node);
1449 }
1450 }
1451
1452 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1453 hash_string(ofproto->up.name, 0));
1454 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1455
1456 ofproto_init_tables(ofproto_, N_TABLES);
1457 error = add_internal_flows(ofproto);
1458
1459 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1460
1461 return error;
1462 }
1463
1464 static int
1465 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1466 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1467 {
1468 struct match match;
1469 int error;
1470 struct rule *rule;
1471
1472 match_init_catchall(&match);
1473 match_set_reg(&match, 0, id);
1474
1475 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1476 &rule);
1477 *rulep = error ? NULL : rule_dpif_cast(rule);
1478
1479 return error;
1480 }
1481
1482 static int
1483 add_internal_flows(struct ofproto_dpif *ofproto)
1484 {
1485 struct ofpact_controller *controller;
1486 uint64_t ofpacts_stub[128 / 8];
1487 struct ofpbuf ofpacts;
1488 struct rule *unused_rulep OVS_UNUSED;
1489 struct match match;
1490 int error;
1491 int id;
1492
1493 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1494 id = 1;
1495
1496 controller = ofpact_put_CONTROLLER(&ofpacts);
1497 controller->max_len = UINT16_MAX;
1498 controller->controller_id = 0;
1499 controller->reason = OFPR_IMPLICIT_MISS;
1500 ofpact_finish_CONTROLLER(&ofpacts, &controller);
1501
1502 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1503 &ofproto->miss_rule);
1504 if (error) {
1505 return error;
1506 }
1507
1508 ofpbuf_clear(&ofpacts);
1509 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1510 &ofproto->no_packet_in_rule);
1511 if (error) {
1512 return error;
1513 }
1514
1515 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1516 &ofproto->drop_frags_rule);
1517 if (error) {
1518 return error;
1519 }
1520
1521 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1522 * zero when reaching this rule.
1523 *
1524 * (priority=2), recirc_id=0, actions=drop
1525 */
1526 ofpbuf_clear(&ofpacts);
1527 match_init_catchall(&match);
1528 match_set_recirc_id(&match, 0);
1529 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1530 &unused_rulep);
1531 return error;
1532 }
1533
1534 static void
1535 destruct(struct ofproto *ofproto_, bool del)
1536 {
1537 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1538 struct ofproto_async_msg *am;
1539 struct rule_dpif *rule;
1540 struct oftable *table;
1541 struct ovs_list ams;
1542
1543 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1544 xlate_txn_start();
1545 xlate_remove_ofproto(ofproto);
1546 xlate_txn_commit();
1547
1548 /* Ensure that the upcall processing threads have no remaining references
1549 * to the ofproto or anything in it. */
1550 udpif_synchronize(ofproto->backer->udpif);
1551
1552 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1553
1554 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1555 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1556 ofproto_rule_delete(&ofproto->up, &rule->up);
1557 }
1558 }
1559 ofproto_group_delete_all(&ofproto->up);
1560
1561 guarded_list_pop_all(&ofproto->ams, &ams);
1562 LIST_FOR_EACH_POP (am, list_node, &ams) {
1563 ofproto_async_msg_free(am);
1564 }
1565 guarded_list_destroy(&ofproto->ams);
1566
1567 recirc_free_ofproto(ofproto, ofproto->up.name);
1568
1569 mbridge_unref(ofproto->mbridge);
1570
1571 netflow_unref(ofproto->netflow);
1572 dpif_sflow_unref(ofproto->sflow);
1573 dpif_ipfix_unref(ofproto->ipfix);
1574 hmap_destroy(&ofproto->bundles);
1575 mac_learning_unref(ofproto->ml);
1576 mcast_snooping_unref(ofproto->ms);
1577 stp_unref(ofproto->stp);
1578 rstp_unref(ofproto->rstp);
1579
1580 sset_destroy(&ofproto->ports);
1581 sset_destroy(&ofproto->ghost_ports);
1582 sset_destroy(&ofproto->port_poll_set);
1583
1584 ovs_mutex_destroy(&ofproto->stats_mutex);
1585
1586 seq_destroy(ofproto->ams_seq);
1587
1588 close_dpif_backer(ofproto->backer, del);
1589 }
1590
1591 static int
1592 run(struct ofproto *ofproto_)
1593 {
1594 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1595 uint64_t new_seq, new_dump_seq;
1596
1597 if (mbridge_need_revalidate(ofproto->mbridge)) {
1598 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1599 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1600 mac_learning_flush(ofproto->ml);
1601 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1602 mcast_snooping_mdb_flush(ofproto->ms);
1603 }
1604
1605 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1606 * flow restore. Even though nothing is processed during flow restore,
1607 * all queued 'ams' will be handled immediately when flow restore
1608 * completes. */
1609 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1610
1611 /* Do not perform any periodic activity required by 'ofproto' while
1612 * waiting for flow restore to complete. */
1613 if (!ofproto_get_flow_restore_wait()) {
1614 struct ofproto_async_msg *am;
1615 struct ovs_list ams;
1616
1617 guarded_list_pop_all(&ofproto->ams, &ams);
1618 LIST_FOR_EACH_POP (am, list_node, &ams) {
1619 connmgr_send_async_msg(ofproto->up.connmgr, am);
1620 ofproto_async_msg_free(am);
1621 }
1622 }
1623
1624 if (ofproto->netflow) {
1625 netflow_run(ofproto->netflow);
1626 }
1627 if (ofproto->sflow) {
1628 dpif_sflow_run(ofproto->sflow);
1629 }
1630 if (ofproto->ipfix) {
1631 dpif_ipfix_run(ofproto->ipfix);
1632 }
1633
1634 new_seq = seq_read(connectivity_seq_get());
1635 if (ofproto->change_seq != new_seq) {
1636 struct ofport_dpif *ofport;
1637
1638 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1639 port_run(ofport);
1640 }
1641
1642 ofproto->change_seq = new_seq;
1643 }
1644 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1645 struct ofbundle *bundle;
1646
1647 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1648 bundle_run(bundle);
1649 }
1650 }
1651
1652 stp_run(ofproto);
1653 rstp_run(ofproto);
1654 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1655 if (mac_learning_run(ofproto->ml)) {
1656 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1657 }
1658 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1659
1660 if (mcast_snooping_run(ofproto->ms)) {
1661 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1662 }
1663
1664 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1665 if (ofproto->dump_seq != new_dump_seq) {
1666 struct rule *rule, *next_rule;
1667 long long now = time_msec();
1668
1669 /* We know stats are relatively fresh, so now is a good time to do some
1670 * periodic work. */
1671 ofproto->dump_seq = new_dump_seq;
1672
1673 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1674 * has passed. */
1675 ovs_mutex_lock(&ofproto_mutex);
1676 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1677 &ofproto->up.expirable) {
1678 rule_expire(rule_dpif_cast(rule), now);
1679 }
1680 ovs_mutex_unlock(&ofproto_mutex);
1681
1682 /* All outstanding data in existing flows has been accounted, so it's a
1683 * good time to do bond rebalancing. */
1684 if (ofproto->has_bonded_bundles) {
1685 struct ofbundle *bundle;
1686
1687 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1688 if (bundle->bond) {
1689 bond_rebalance(bundle->bond);
1690 }
1691 }
1692 }
1693 }
1694 return 0;
1695 }
1696
1697 static void
1698 ofproto_dpif_wait(struct ofproto *ofproto_)
1699 {
1700 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1701
1702 if (ofproto_get_flow_restore_wait()) {
1703 return;
1704 }
1705
1706 if (ofproto->sflow) {
1707 dpif_sflow_wait(ofproto->sflow);
1708 }
1709 if (ofproto->ipfix) {
1710 dpif_ipfix_wait(ofproto->ipfix);
1711 }
1712 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1713 struct ofbundle *bundle;
1714
1715 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1716 bundle_wait(bundle);
1717 }
1718 }
1719 if (ofproto->netflow) {
1720 netflow_wait(ofproto->netflow);
1721 }
1722 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1723 mac_learning_wait(ofproto->ml);
1724 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1725 mcast_snooping_wait(ofproto->ms);
1726 stp_wait(ofproto);
1727 if (ofproto->backer->need_revalidate) {
1728 poll_immediate_wake();
1729 }
1730
1731 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1732 seq_wait(ofproto->ams_seq, ofproto->ams_seqno);
1733 }
1734
1735 static void
1736 type_get_memory_usage(const char *type, struct simap *usage)
1737 {
1738 struct dpif_backer *backer;
1739
1740 backer = shash_find_data(&all_dpif_backers, type);
1741 if (backer) {
1742 udpif_get_memory_usage(backer->udpif, usage);
1743 }
1744 }
1745
1746 static void
1747 flush(struct ofproto *ofproto_)
1748 {
1749 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1750 struct dpif_backer *backer = ofproto->backer;
1751
1752 if (backer) {
1753 udpif_flush(backer->udpif);
1754 }
1755 }
1756
1757 static void
1758 query_tables(struct ofproto *ofproto,
1759 struct ofputil_table_features *features,
1760 struct ofputil_table_stats *stats)
1761 {
1762 strcpy(features->name, "classifier");
1763
1764 if (stats) {
1765 int i;
1766
1767 for (i = 0; i < ofproto->n_tables; i++) {
1768 unsigned long missed, matched;
1769
1770 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1771 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1772
1773 stats[i].matched_count = matched;
1774 stats[i].lookup_count = matched + missed;
1775 }
1776 }
1777 }
1778
1779 static void
1780 set_tables_version(struct ofproto *ofproto_, ovs_version_t version)
1781 {
1782 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1783
1784 /* Use memory_order_release to signify that any prior memory accesses can
1785 * not be reordered to happen after this atomic store. This makes sure the
1786 * new version is properly set up when the readers can read this 'version'
1787 * value. */
1788 atomic_store_explicit(&ofproto->tables_version, version,
1789 memory_order_release);
1790 /* 'need_revalidate' can be reordered to happen before the atomic_store
1791 * above, but it does not matter as this variable is not accessed by other
1792 * threads. */
1793 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
1794 }
1795
1796 static struct ofport *
1797 port_alloc(void)
1798 {
1799 struct ofport_dpif *port = xzalloc(sizeof *port);
1800 return &port->up;
1801 }
1802
1803 static void
1804 port_dealloc(struct ofport *port_)
1805 {
1806 struct ofport_dpif *port = ofport_dpif_cast(port_);
1807 free(port);
1808 }
1809
1810 static int
1811 port_construct(struct ofport *port_)
1812 {
1813 struct ofport_dpif *port = ofport_dpif_cast(port_);
1814 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1815 const struct netdev *netdev = port->up.netdev;
1816 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1817 const char *dp_port_name;
1818 struct dpif_port dpif_port;
1819 int error;
1820
1821 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1822 port->bundle = NULL;
1823 port->cfm = NULL;
1824 port->bfd = NULL;
1825 port->lldp = NULL;
1826 port->may_enable = false;
1827 port->stp_port = NULL;
1828 port->stp_state = STP_DISABLED;
1829 port->rstp_port = NULL;
1830 port->rstp_state = RSTP_DISABLED;
1831 port->is_tunnel = false;
1832 port->peer = NULL;
1833 port->qdscp = NULL;
1834 port->n_qdscp = 0;
1835 port->carrier_seq = netdev_get_carrier_resets(netdev);
1836
1837 if (netdev_vport_is_patch(netdev)) {
1838 /* By bailing out here, we don't submit the port to the sFlow module
1839 * to be considered for counter polling export. This is correct
1840 * because the patch port represents an interface that sFlow considers
1841 * to be "internal" to the switch as a whole, and therefore not a
1842 * candidate for counter polling. */
1843 port->odp_port = ODPP_NONE;
1844 ofport_update_peer(port);
1845 return 0;
1846 }
1847
1848 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1849 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1850 &dpif_port);
1851 if (error) {
1852 return error;
1853 }
1854
1855 port->odp_port = dpif_port.port_no;
1856
1857 if (netdev_get_tunnel_config(netdev)) {
1858 atomic_count_inc(&ofproto->backer->tnl_count);
1859 error = tnl_port_add(port, port->up.netdev, port->odp_port,
1860 ovs_native_tunneling_is_on(ofproto), dp_port_name);
1861 if (error) {
1862 atomic_count_dec(&ofproto->backer->tnl_count);
1863 dpif_port_destroy(&dpif_port);
1864 return error;
1865 }
1866
1867 port->is_tunnel = true;
1868 if (ofproto->ipfix) {
1869 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1870 }
1871 } else {
1872 /* Sanity-check that a mapping doesn't already exist. This
1873 * shouldn't happen for non-tunnel ports. */
1874 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1875 VLOG_ERR("port %s already has an OpenFlow port number",
1876 dpif_port.name);
1877 dpif_port_destroy(&dpif_port);
1878 return EBUSY;
1879 }
1880
1881 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1882 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1883 hash_odp_port(port->odp_port));
1884 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1885 }
1886 dpif_port_destroy(&dpif_port);
1887
1888 if (ofproto->sflow) {
1889 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1890 }
1891
1892 return 0;
1893 }
1894
1895 static void
1896 port_destruct(struct ofport *port_, bool del)
1897 {
1898 struct ofport_dpif *port = ofport_dpif_cast(port_);
1899 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1900 const char *devname = netdev_get_name(port->up.netdev);
1901 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1902 const char *dp_port_name;
1903
1904 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1905 xlate_txn_start();
1906 xlate_ofport_remove(port);
1907 xlate_txn_commit();
1908
1909 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1910 sizeof namebuf);
1911 if (del && dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1912 /* The underlying device is still there, so delete it. This
1913 * happens when the ofproto is being destroyed, since the caller
1914 * assumes that removal of attached ports will happen as part of
1915 * destruction. */
1916 if (!port->is_tunnel) {
1917 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1918 }
1919 }
1920
1921 if (port->peer) {
1922 port->peer->peer = NULL;
1923 port->peer = NULL;
1924 }
1925
1926 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1927 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1928 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1929 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1930 }
1931
1932 if (port->is_tunnel) {
1933 atomic_count_dec(&ofproto->backer->tnl_count);
1934 }
1935
1936 if (port->is_tunnel && ofproto->ipfix) {
1937 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1938 }
1939
1940 tnl_port_del(port);
1941 sset_find_and_delete(&ofproto->ports, devname);
1942 sset_find_and_delete(&ofproto->ghost_ports, devname);
1943 bundle_remove(port_);
1944 set_cfm(port_, NULL);
1945 set_bfd(port_, NULL);
1946 set_lldp(port_, NULL);
1947 if (port->stp_port) {
1948 stp_port_disable(port->stp_port);
1949 }
1950 set_rstp_port(port_, NULL);
1951 if (ofproto->sflow) {
1952 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1953 }
1954
1955 free(port->qdscp);
1956 }
1957
1958 static void
1959 port_modified(struct ofport *port_)
1960 {
1961 struct ofport_dpif *port = ofport_dpif_cast(port_);
1962 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1963 const char *dp_port_name;
1964 struct netdev *netdev = port->up.netdev;
1965
1966 if (port->bundle && port->bundle->bond) {
1967 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1968 }
1969
1970 if (port->cfm) {
1971 cfm_set_netdev(port->cfm, netdev);
1972 }
1973
1974 if (port->bfd) {
1975 bfd_set_netdev(port->bfd, netdev);
1976 }
1977
1978 /* Set liveness, unless the link is administratively or
1979 * operationally down or link monitoring false */
1980 if (!(port->up.pp.config & OFPUTIL_PC_PORT_DOWN) &&
1981 !(port->up.pp.state & OFPUTIL_PS_LINK_DOWN) &&
1982 port->may_enable) {
1983 port->up.pp.state |= OFPUTIL_PS_LIVE;
1984 } else {
1985 port->up.pp.state &= ~OFPUTIL_PS_LIVE;
1986 }
1987
1988 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1989 port->lldp, &port->up.pp.hw_addr);
1990
1991 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1992
1993 if (port->is_tunnel) {
1994 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1995
1996 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1997 ovs_native_tunneling_is_on(ofproto),
1998 dp_port_name)) {
1999 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2000 }
2001 }
2002
2003 ofport_update_peer(port);
2004 }
2005
2006 static void
2007 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
2008 {
2009 struct ofport_dpif *port = ofport_dpif_cast(port_);
2010 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
2011 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
2012
2013 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
2014 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
2015 OFPUTIL_PC_NO_PACKET_IN)) {
2016 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2017
2018 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
2019 bundle_update(port->bundle);
2020 }
2021 }
2022 }
2023
2024 static int
2025 set_sflow(struct ofproto *ofproto_,
2026 const struct ofproto_sflow_options *sflow_options)
2027 {
2028 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2029 struct dpif_sflow *ds = ofproto->sflow;
2030
2031 if (sflow_options) {
2032 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
2033 if (!ds) {
2034 struct ofport_dpif *ofport;
2035
2036 ds = ofproto->sflow = dpif_sflow_create();
2037 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2038 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
2039 }
2040 }
2041 dpif_sflow_set_options(ds, sflow_options);
2042 if (dpif_sflow_get_probability(ds) != old_probability) {
2043 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2044 }
2045 } else {
2046 if (ds) {
2047 dpif_sflow_unref(ds);
2048 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2049 ofproto->sflow = NULL;
2050 }
2051 }
2052 return 0;
2053 }
2054
2055 static int
2056 set_ipfix(
2057 struct ofproto *ofproto_,
2058 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
2059 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
2060 size_t n_flow_exporters_options)
2061 {
2062 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2063 struct dpif_ipfix *di = ofproto->ipfix;
2064 bool has_options = bridge_exporter_options || flow_exporters_options;
2065 bool new_di = false;
2066
2067 if (has_options && !di) {
2068 di = ofproto->ipfix = dpif_ipfix_create();
2069 new_di = true;
2070 }
2071
2072 if (di) {
2073 /* Call set_options in any case to cleanly flush the flow
2074 * caches in the last exporters that are to be destroyed. */
2075 dpif_ipfix_set_options(
2076 di, bridge_exporter_options, flow_exporters_options,
2077 n_flow_exporters_options);
2078
2079 /* Add tunnel ports only when a new ipfix created */
2080 if (new_di == true) {
2081 struct ofport_dpif *ofport;
2082 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2083 if (ofport->is_tunnel == true) {
2084 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
2085 }
2086 }
2087 }
2088
2089 if (!has_options) {
2090 dpif_ipfix_unref(di);
2091 ofproto->ipfix = NULL;
2092 }
2093 }
2094
2095 return 0;
2096 }
2097
2098 static int
2099 get_ipfix_stats(const struct ofproto *ofproto_,
2100 bool bridge_ipfix,
2101 struct ovs_list *replies)
2102 {
2103 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2104 struct dpif_ipfix *di = ofproto->ipfix;
2105
2106 if (!di) {
2107 return OFPERR_NXST_NOT_CONFIGURED;
2108 }
2109
2110 return dpif_ipfix_get_stats(di, bridge_ipfix, replies);
2111 }
2112
2113 static int
2114 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
2115 {
2116 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2117 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2118 struct cfm *old = ofport->cfm;
2119 int error = 0;
2120
2121 if (s) {
2122 if (!ofport->cfm) {
2123 ofport->cfm = cfm_create(ofport->up.netdev);
2124 }
2125
2126 if (cfm_configure(ofport->cfm, s)) {
2127 error = 0;
2128 goto out;
2129 }
2130
2131 error = EINVAL;
2132 }
2133 cfm_unref(ofport->cfm);
2134 ofport->cfm = NULL;
2135 out:
2136 if (ofport->cfm != old) {
2137 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2138 }
2139 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2140 ofport->lldp, &ofport->up.pp.hw_addr);
2141 return error;
2142 }
2143
2144 static bool
2145 cfm_status_changed(struct ofport *ofport_)
2146 {
2147 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2148
2149 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2150 }
2151
2152 static int
2153 get_cfm_status(const struct ofport *ofport_,
2154 struct cfm_status *status)
2155 {
2156 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2157 int ret = 0;
2158
2159 if (ofport->cfm) {
2160 cfm_get_status(ofport->cfm, status);
2161 } else {
2162 ret = ENOENT;
2163 }
2164
2165 return ret;
2166 }
2167
2168 static int
2169 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2170 {
2171 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2172 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2173 struct bfd *old;
2174
2175 old = ofport->bfd;
2176 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2177 cfg, ofport->up.netdev);
2178 if (ofport->bfd != old) {
2179 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2180 }
2181 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2182 ofport->lldp, &ofport->up.pp.hw_addr);
2183 return 0;
2184 }
2185
2186 static bool
2187 bfd_status_changed(struct ofport *ofport_)
2188 {
2189 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2190
2191 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2192 }
2193
2194 static int
2195 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2196 {
2197 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2198 int ret = 0;
2199
2200 if (ofport->bfd) {
2201 bfd_get_status(ofport->bfd, smap);
2202 } else {
2203 ret = ENOENT;
2204 }
2205
2206 return ret;
2207 }
2208
2209 static int
2210 set_lldp(struct ofport *ofport_,
2211 const struct smap *cfg)
2212 {
2213 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2214 int error = 0;
2215
2216 if (cfg) {
2217 if (!ofport->lldp) {
2218 struct ofproto_dpif *ofproto;
2219
2220 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2221 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2222 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2223 }
2224
2225 if (!lldp_configure(ofport->lldp, cfg)) {
2226 error = EINVAL;
2227 }
2228 }
2229 if (error) {
2230 lldp_unref(ofport->lldp);
2231 ofport->lldp = NULL;
2232 }
2233
2234 ofproto_dpif_monitor_port_update(ofport,
2235 ofport->bfd,
2236 ofport->cfm,
2237 ofport->lldp,
2238 &ofport->up.pp.hw_addr);
2239 return error;
2240 }
2241
2242 static bool
2243 get_lldp_status(const struct ofport *ofport_,
2244 struct lldp_status *status OVS_UNUSED)
2245 {
2246 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2247
2248 return ofport->lldp ? true : false;
2249 }
2250
2251 static int
2252 set_aa(struct ofproto *ofproto OVS_UNUSED,
2253 const struct aa_settings *s)
2254 {
2255 return aa_configure(s);
2256 }
2257
2258 static int
2259 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2260 const struct aa_mapping_settings *s)
2261 {
2262 return aa_mapping_register(aux, s);
2263 }
2264
2265 static int
2266 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2267 {
2268 return aa_mapping_unregister(aux);
2269 }
2270
2271 static int
2272 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2273 {
2274 return aa_get_vlan_queued(list);
2275 }
2276
2277 static unsigned int
2278 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2279 {
2280 return aa_get_vlan_queue_size();
2281 }
2282
2283 \f
2284 /* Spanning Tree. */
2285
2286 /* Called while rstp_mutex is held. */
2287 static void
2288 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2289 {
2290 struct ofproto_dpif *ofproto = ofproto_;
2291 struct ofport_dpif *ofport = ofport_;
2292 struct eth_header *eth = dp_packet_eth(pkt);
2293
2294 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
2295 if (eth_addr_is_zero(eth->eth_src)) {
2296 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2297 "does not have a configured source MAC address.",
2298 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2299 } else {
2300 ofproto_dpif_send_packet(ofport, false, pkt);
2301 }
2302 dp_packet_delete(pkt);
2303 }
2304
2305 static void
2306 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2307 {
2308 struct ofproto_dpif *ofproto = ofproto_;
2309 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2310 struct ofport_dpif *ofport;
2311
2312 ofport = stp_port_get_aux(sp);
2313 if (!ofport) {
2314 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2315 ofproto->up.name, port_num);
2316 } else {
2317 struct eth_header *eth = dp_packet_eth(pkt);
2318
2319 netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
2320 if (eth_addr_is_zero(eth->eth_src)) {
2321 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2322 "with unknown MAC", ofproto->up.name, port_num);
2323 } else {
2324 ofproto_dpif_send_packet(ofport, false, pkt);
2325 }
2326 }
2327 dp_packet_delete(pkt);
2328 }
2329
2330 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2331 static void
2332 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2333 {
2334 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2335
2336 /* Only revalidate flows if the configuration changed. */
2337 if (!s != !ofproto->rstp) {
2338 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2339 }
2340
2341 if (s) {
2342 if (!ofproto->rstp) {
2343 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2344 rstp_send_bpdu_cb, ofproto);
2345 ofproto->rstp_last_tick = time_msec();
2346 }
2347 rstp_set_bridge_address(ofproto->rstp, s->address);
2348 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2349 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2350 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2351 s->force_protocol_version);
2352 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2353 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2354 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2355 s->transmit_hold_count);
2356 } else {
2357 struct ofport *ofport;
2358 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2359 set_rstp_port(ofport, NULL);
2360 }
2361 rstp_unref(ofproto->rstp);
2362 ofproto->rstp = NULL;
2363 }
2364 }
2365
2366 static void
2367 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2368 {
2369 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2370
2371 if (ofproto->rstp) {
2372 s->enabled = true;
2373 s->root_id = rstp_get_root_id(ofproto->rstp);
2374 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2375 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2376 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2377 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2378 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2379 } else {
2380 s->enabled = false;
2381 }
2382 }
2383
2384 static void
2385 update_rstp_port_state(struct ofport_dpif *ofport)
2386 {
2387 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2388 enum rstp_state state;
2389
2390 /* Figure out new state. */
2391 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2392 : RSTP_DISABLED;
2393
2394 /* Update state. */
2395 if (ofport->rstp_state != state) {
2396 enum ofputil_port_state of_state;
2397 bool fwd_change;
2398
2399 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2400 netdev_get_name(ofport->up.netdev),
2401 rstp_state_name(ofport->rstp_state),
2402 rstp_state_name(state));
2403
2404 if (rstp_learn_in_state(ofport->rstp_state)
2405 != rstp_learn_in_state(state)) {
2406 /* XXX: Learning action flows should also be flushed. */
2407 if (ofport->bundle) {
2408 if (!rstp_shift_root_learned_address(ofproto->rstp)
2409 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2410 bundle_flush_macs(ofport->bundle, false);
2411 }
2412 }
2413 }
2414 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2415 != rstp_forward_in_state(state);
2416
2417 ofproto->backer->need_revalidate = REV_RSTP;
2418 ofport->rstp_state = state;
2419
2420 if (fwd_change && ofport->bundle) {
2421 bundle_update(ofport->bundle);
2422 }
2423
2424 /* Update the RSTP state bits in the OpenFlow port description. */
2425 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2426 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2427 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2428 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2429 : 0);
2430 ofproto_port_set_state(&ofport->up, of_state);
2431 }
2432 }
2433
2434 static void
2435 rstp_run(struct ofproto_dpif *ofproto)
2436 {
2437 if (ofproto->rstp) {
2438 long long int now = time_msec();
2439 long long int elapsed = now - ofproto->rstp_last_tick;
2440 struct rstp_port *rp;
2441 struct ofport_dpif *ofport;
2442
2443 /* Every second, decrease the values of the timers. */
2444 if (elapsed >= 1000) {
2445 rstp_tick_timers(ofproto->rstp);
2446 ofproto->rstp_last_tick = now;
2447 }
2448 rp = NULL;
2449 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2450 update_rstp_port_state(ofport);
2451 }
2452 rp = NULL;
2453 ofport = NULL;
2454 /* FIXME: This check should be done on-event (i.e., when setting
2455 * p->fdb_flush) and not periodically.
2456 */
2457 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2458 if (!rstp_shift_root_learned_address(ofproto->rstp)
2459 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2460 bundle_flush_macs(ofport->bundle, false);
2461 }
2462 }
2463
2464 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2465 struct ofport_dpif *old_root_aux =
2466 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2467 struct ofport_dpif *new_root_aux =
2468 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2469 if (old_root_aux != NULL && new_root_aux != NULL) {
2470 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2471 rstp_reset_root_changed(ofproto->rstp);
2472 }
2473 }
2474 }
2475 }
2476
2477 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2478 static int
2479 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2480 {
2481 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2482
2483 /* Only revalidate flows if the configuration changed. */
2484 if (!s != !ofproto->stp) {
2485 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2486 }
2487
2488 if (s) {
2489 if (!ofproto->stp) {
2490 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2491 send_bpdu_cb, ofproto);
2492 ofproto->stp_last_tick = time_msec();
2493 }
2494
2495 stp_set_bridge_id(ofproto->stp, s->system_id);
2496 stp_set_bridge_priority(ofproto->stp, s->priority);
2497 stp_set_hello_time(ofproto->stp, s->hello_time);
2498 stp_set_max_age(ofproto->stp, s->max_age);
2499 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2500 } else {
2501 struct ofport *ofport;
2502
2503 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2504 set_stp_port(ofport, NULL);
2505 }
2506
2507 stp_unref(ofproto->stp);
2508 ofproto->stp = NULL;
2509 }
2510
2511 return 0;
2512 }
2513
2514 static int
2515 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2516 {
2517 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2518
2519 if (ofproto->stp) {
2520 s->enabled = true;
2521 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2522 s->designated_root = stp_get_designated_root(ofproto->stp);
2523 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2524 } else {
2525 s->enabled = false;
2526 }
2527
2528 return 0;
2529 }
2530
2531 static void
2532 update_stp_port_state(struct ofport_dpif *ofport)
2533 {
2534 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2535 enum stp_state state;
2536
2537 /* Figure out new state. */
2538 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2539 : STP_DISABLED;
2540
2541 /* Update state. */
2542 if (ofport->stp_state != state) {
2543 enum ofputil_port_state of_state;
2544 bool fwd_change;
2545
2546 VLOG_DBG("port %s: STP state changed from %s to %s",
2547 netdev_get_name(ofport->up.netdev),
2548 stp_state_name(ofport->stp_state),
2549 stp_state_name(state));
2550 if (stp_learn_in_state(ofport->stp_state)
2551 != stp_learn_in_state(state)) {
2552 /* xxx Learning action flows should also be flushed. */
2553 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2554 mac_learning_flush(ofproto->ml);
2555 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2556 mcast_snooping_mdb_flush(ofproto->ms);
2557 }
2558 fwd_change = stp_forward_in_state(ofport->stp_state)
2559 != stp_forward_in_state(state);
2560
2561 ofproto->backer->need_revalidate = REV_STP;
2562 ofport->stp_state = state;
2563 ofport->stp_state_entered = time_msec();
2564
2565 if (fwd_change && ofport->bundle) {
2566 bundle_update(ofport->bundle);
2567 }
2568
2569 /* Update the STP state bits in the OpenFlow port description. */
2570 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2571 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2572 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2573 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2574 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2575 : 0);
2576 ofproto_port_set_state(&ofport->up, of_state);
2577 }
2578 }
2579
2580 static void
2581 stp_check_and_update_link_state(struct ofproto_dpif *ofproto)
2582 {
2583 struct ofport_dpif *ofport;
2584
2585 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2586 bool up = netdev_get_carrier(ofport->up.netdev);
2587
2588 if (ofport->stp_port &&
2589 up != (stp_port_get_state(ofport->stp_port) != STP_DISABLED)) {
2590
2591 VLOG_DBG("bridge %s, port %s is %s, %s it.",
2592 ofproto->up.name, netdev_get_name(ofport->up.netdev),
2593 up ? "up" : "down",
2594 up ? "enabling" : "disabling");
2595
2596 if (up) {
2597 stp_port_enable(ofport->stp_port);
2598 stp_port_set_aux(ofport->stp_port, ofport);
2599 } else {
2600 stp_port_disable(ofport->stp_port);
2601 }
2602
2603 update_stp_port_state(ofport);
2604 }
2605 }
2606 }
2607
2608 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2609 * caller is responsible for assigning STP port numbers and ensuring
2610 * there are no duplicates. */
2611 static int
2612 set_stp_port(struct ofport *ofport_,
2613 const struct ofproto_port_stp_settings *s)
2614 {
2615 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2616 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2617 struct stp_port *sp = ofport->stp_port;
2618
2619 if (!s || !s->enable) {
2620 if (sp) {
2621 ofport->stp_port = NULL;
2622 stp_port_disable(sp);
2623 update_stp_port_state(ofport);
2624 }
2625 return 0;
2626 } else if (sp && stp_port_no(sp) != s->port_num
2627 && ofport == stp_port_get_aux(sp)) {
2628 /* The port-id changed, so disable the old one if it's not
2629 * already in use by another port. */
2630 stp_port_disable(sp);
2631 }
2632
2633 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2634
2635 /* Set name before enabling the port so that debugging messages can print
2636 * the name. */
2637 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2638
2639 if (netdev_get_carrier(ofport_->netdev)) {
2640 stp_port_enable(sp);
2641 } else {
2642 stp_port_disable(sp);
2643 }
2644
2645 stp_port_set_aux(sp, ofport);
2646 stp_port_set_priority(sp, s->priority);
2647 stp_port_set_path_cost(sp, s->path_cost);
2648
2649 update_stp_port_state(ofport);
2650
2651 return 0;
2652 }
2653
2654 static int
2655 get_stp_port_status(struct ofport *ofport_,
2656 struct ofproto_port_stp_status *s)
2657 {
2658 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2659 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2660 struct stp_port *sp = ofport->stp_port;
2661
2662 if (!ofproto->stp || !sp) {
2663 s->enabled = false;
2664 return 0;
2665 }
2666
2667 s->enabled = true;
2668 stp_port_get_status(sp, &s->port_id, &s->state, &s->role);
2669 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2670
2671 return 0;
2672 }
2673
2674 static int
2675 get_stp_port_stats(struct ofport *ofport_,
2676 struct ofproto_port_stp_stats *s)
2677 {
2678 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2679 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2680 struct stp_port *sp = ofport->stp_port;
2681
2682 if (!ofproto->stp || !sp) {
2683 s->enabled = false;
2684 return 0;
2685 }
2686
2687 s->enabled = true;
2688 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2689
2690 return 0;
2691 }
2692
2693 static void
2694 stp_run(struct ofproto_dpif *ofproto)
2695 {
2696 if (ofproto->stp) {
2697 long long int now = time_msec();
2698 long long int elapsed = now - ofproto->stp_last_tick;
2699 struct stp_port *sp;
2700
2701 if (elapsed > 0) {
2702 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2703 ofproto->stp_last_tick = now;
2704 }
2705
2706 stp_check_and_update_link_state(ofproto);
2707
2708 while (stp_get_changed_port(ofproto->stp, &sp)) {
2709 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2710
2711 if (ofport) {
2712 update_stp_port_state(ofport);
2713 }
2714 }
2715
2716 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2717 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2718 mac_learning_flush(ofproto->ml);
2719 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2720 mcast_snooping_mdb_flush(ofproto->ms);
2721 }
2722 }
2723 }
2724
2725 static void
2726 stp_wait(struct ofproto_dpif *ofproto)
2727 {
2728 if (ofproto->stp) {
2729 poll_timer_wait(1000);
2730 }
2731 }
2732
2733 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2734 * caller is responsible for assigning RSTP port numbers and ensuring
2735 * there are no duplicates. */
2736 static void
2737 set_rstp_port(struct ofport *ofport_,
2738 const struct ofproto_port_rstp_settings *s)
2739 {
2740 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2741 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2742 struct rstp_port *rp = ofport->rstp_port;
2743
2744 if (!s || !s->enable) {
2745 if (rp) {
2746 rstp_port_set_aux(rp, NULL);
2747 rstp_port_set_state(rp, RSTP_DISABLED);
2748 rstp_port_set_mac_operational(rp, false);
2749 ofport->rstp_port = NULL;
2750 rstp_port_unref(rp);
2751 update_rstp_port_state(ofport);
2752 }
2753 return;
2754 }
2755
2756 /* Check if need to add a new port. */
2757 if (!rp) {
2758 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2759 }
2760
2761 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2762 s->admin_edge_port, s->auto_edge,
2763 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2764 ofport, netdev_get_name(ofport->up.netdev));
2765 update_rstp_port_state(ofport);
2766 /* Synchronize operational status. */
2767 rstp_port_set_mac_operational(rp, ofport->may_enable);
2768 }
2769
2770 static void
2771 get_rstp_port_status(struct ofport *ofport_,
2772 struct ofproto_port_rstp_status *s)
2773 {
2774 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2775 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2776 struct rstp_port *rp = ofport->rstp_port;
2777
2778 if (!ofproto->rstp || !rp) {
2779 s->enabled = false;
2780 return;
2781 }
2782
2783 s->enabled = true;
2784 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2785 &s->designated_bridge_id, &s->designated_port_id,
2786 &s->designated_path_cost, &s->tx_count,
2787 &s->rx_count, &s->error_count, &s->uptime);
2788 }
2789
2790 \f
2791 static int
2792 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2793 size_t n_qdscp)
2794 {
2795 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2796 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2797
2798 if (ofport->n_qdscp != n_qdscp
2799 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2800 n_qdscp * sizeof *qdscp))) {
2801 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2802 free(ofport->qdscp);
2803 ofport->qdscp = n_qdscp
2804 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2805 : NULL;
2806 ofport->n_qdscp = n_qdscp;
2807 }
2808
2809 return 0;
2810 }
2811 \f
2812 /* Bundles. */
2813
2814 /* Expires all MAC learning entries associated with 'bundle' and forces its
2815 * ofproto to revalidate every flow.
2816 *
2817 * Normally MAC learning entries are removed only from the ofproto associated
2818 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2819 * are removed from every ofproto. When patch ports and SLB bonds are in use
2820 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2821 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2822 * with the host from which it migrated. */
2823 static void
2824 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2825 {
2826 struct ofproto_dpif *ofproto = bundle->ofproto;
2827 struct mac_learning *ml = ofproto->ml;
2828 struct mac_entry *mac, *next_mac;
2829
2830 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2831 ovs_rwlock_wrlock(&ml->rwlock);
2832 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2833 if (mac_entry_get_port(ml, mac) == bundle) {
2834 if (all_ofprotos) {
2835 struct ofproto_dpif *o;
2836
2837 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2838 if (o != ofproto) {
2839 struct mac_entry *e;
2840
2841 ovs_rwlock_wrlock(&o->ml->rwlock);
2842 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2843 if (e) {
2844 mac_learning_expire(o->ml, e);
2845 }
2846 ovs_rwlock_unlock(&o->ml->rwlock);
2847 }
2848 }
2849 }
2850
2851 mac_learning_expire(ml, mac);
2852 }
2853 }
2854 ovs_rwlock_unlock(&ml->rwlock);
2855 }
2856
2857 static void
2858 bundle_move(struct ofbundle *old, struct ofbundle *new)
2859 {
2860 struct ofproto_dpif *ofproto = old->ofproto;
2861 struct mac_learning *ml = ofproto->ml;
2862 struct mac_entry *mac, *next_mac;
2863
2864 ovs_assert(new->ofproto == old->ofproto);
2865
2866 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2867 ovs_rwlock_wrlock(&ml->rwlock);
2868 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2869 if (mac_entry_get_port(ml, mac) == old) {
2870 mac_entry_set_port(ml, mac, new);
2871 }
2872 }
2873 ovs_rwlock_unlock(&ml->rwlock);
2874 }
2875
2876 static struct ofbundle *
2877 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2878 {
2879 struct ofbundle *bundle;
2880
2881 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2882 &ofproto->bundles) {
2883 if (bundle->aux == aux) {
2884 return bundle;
2885 }
2886 }
2887 return NULL;
2888 }
2889
2890 static void
2891 bundle_update(struct ofbundle *bundle)
2892 {
2893 struct ofport_dpif *port;
2894
2895 bundle->floodable = true;
2896 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2897 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2898 || netdev_get_pt_mode(port->up.netdev) == NETDEV_PT_LEGACY_L3
2899 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2900 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2901 bundle->floodable = false;
2902 break;
2903 }
2904 }
2905 }
2906
2907 static void
2908 bundle_del_port(struct ofport_dpif *port)
2909 {
2910 struct ofbundle *bundle = port->bundle;
2911
2912 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2913
2914 ovs_list_remove(&port->bundle_node);
2915 port->bundle = NULL;
2916
2917 if (bundle->lacp) {
2918 lacp_slave_unregister(bundle->lacp, port);
2919 }
2920 if (bundle->bond) {
2921 bond_slave_unregister(bundle->bond, port);
2922 }
2923
2924 bundle_update(bundle);
2925 }
2926
2927 static bool
2928 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2929 struct lacp_slave_settings *lacp)
2930 {
2931 struct ofport_dpif *port;
2932
2933 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2934 if (!port) {
2935 return false;
2936 }
2937
2938 if (port->bundle != bundle) {
2939 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2940 if (port->bundle) {
2941 bundle_remove(&port->up);
2942 }
2943
2944 port->bundle = bundle;
2945 ovs_list_push_back(&bundle->ports, &port->bundle_node);
2946 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2947 || netdev_get_pt_mode(port->up.netdev) == NETDEV_PT_LEGACY_L3
2948 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2949 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2950 bundle->floodable = false;
2951 }
2952 }
2953 if (lacp) {
2954 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2955 lacp_slave_register(bundle->lacp, port, lacp);
2956 }
2957
2958 return true;
2959 }
2960
2961 static void
2962 bundle_destroy(struct ofbundle *bundle)
2963 {
2964 struct ofproto_dpif *ofproto;
2965 struct ofport_dpif *port, *next_port;
2966
2967 if (!bundle) {
2968 return;
2969 }
2970
2971 ofproto = bundle->ofproto;
2972 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2973
2974 xlate_txn_start();
2975 xlate_bundle_remove(bundle);
2976 xlate_txn_commit();
2977
2978 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2979 bundle_del_port(port);
2980 }
2981
2982 bundle_flush_macs(bundle, true);
2983 mcast_snooping_flush_bundle(ofproto->ms, bundle);
2984 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2985 free(bundle->name);
2986 free(bundle->trunks);
2987 free(bundle->cvlans);
2988 lacp_unref(bundle->lacp);
2989 bond_unref(bundle->bond);
2990 free(bundle);
2991 }
2992
2993 static int
2994 bundle_set(struct ofproto *ofproto_, void *aux,
2995 const struct ofproto_bundle_settings *s)
2996 {
2997 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2998 bool need_flush = false;
2999 struct ofport_dpif *port;
3000 struct ofbundle *bundle;
3001 unsigned long *trunks = NULL;
3002 unsigned long *cvlans = NULL;
3003 int vlan;
3004 size_t i;
3005 bool ok;
3006
3007 if (!s) {
3008 bundle_destroy(bundle_lookup(ofproto, aux));
3009 return 0;
3010 }
3011
3012 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
3013 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
3014
3015 bundle = bundle_lookup(ofproto, aux);
3016 if (!bundle) {
3017 bundle = xmalloc(sizeof *bundle);
3018
3019 bundle->ofproto = ofproto;
3020 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
3021 hash_pointer(aux, 0));
3022 bundle->aux = aux;
3023 bundle->name = NULL;
3024
3025 ovs_list_init(&bundle->ports);
3026 bundle->vlan_mode = PORT_VLAN_TRUNK;
3027 bundle->qinq_ethtype = ETH_TYPE_VLAN_8021AD;
3028 bundle->vlan = -1;
3029 bundle->trunks = NULL;
3030 bundle->cvlans = NULL;
3031 bundle->use_priority_tags = s->use_priority_tags;
3032 bundle->lacp = NULL;
3033 bundle->bond = NULL;
3034
3035 bundle->floodable = true;
3036 bundle->protected = false;
3037 mbridge_register_bundle(ofproto->mbridge, bundle);
3038 }
3039
3040 if (!bundle->name || strcmp(s->name, bundle->name)) {
3041 free(bundle->name);
3042 bundle->name = xstrdup(s->name);
3043 }
3044
3045 /* LACP. */
3046 if (s->lacp) {
3047 ofproto->lacp_enabled = true;
3048 if (!bundle->lacp) {
3049 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3050 bundle->lacp = lacp_create();
3051 }
3052 lacp_configure(bundle->lacp, s->lacp);
3053 } else {
3054 lacp_unref(bundle->lacp);
3055 bundle->lacp = NULL;
3056 }
3057
3058 /* Update set of ports. */
3059 ok = true;
3060 for (i = 0; i < s->n_slaves; i++) {
3061 if (!bundle_add_port(bundle, s->slaves[i],
3062 s->lacp ? &s->lacp_slaves[i] : NULL)) {
3063 ok = false;
3064 }
3065 }
3066 if (!ok || ovs_list_size(&bundle->ports) != s->n_slaves) {
3067 struct ofport_dpif *next_port;
3068
3069 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
3070 for (i = 0; i < s->n_slaves; i++) {
3071 if (s->slaves[i] == port->up.ofp_port) {
3072 goto found;
3073 }
3074 }
3075
3076 bundle_del_port(port);
3077 found: ;
3078 }
3079 }
3080 ovs_assert(ovs_list_size(&bundle->ports) <= s->n_slaves);
3081
3082 if (ovs_list_is_empty(&bundle->ports)) {
3083 bundle_destroy(bundle);
3084 return EINVAL;
3085 }
3086
3087 /* Set VLAN tagging mode */
3088 if (s->vlan_mode != bundle->vlan_mode
3089 || s->use_priority_tags != bundle->use_priority_tags) {
3090 bundle->vlan_mode = s->vlan_mode;
3091 bundle->use_priority_tags = s->use_priority_tags;
3092 need_flush = true;
3093 }
3094
3095 if (s->qinq_ethtype != bundle->qinq_ethtype) {
3096 bundle->qinq_ethtype = s->qinq_ethtype;
3097 need_flush = true;
3098 }
3099
3100 /* Set VLAN tag. */
3101 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
3102 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
3103 : 0);
3104 if (vlan != bundle->vlan) {
3105 bundle->vlan = vlan;
3106 need_flush = true;
3107 }
3108
3109 /* Get trunked VLANs. */
3110 switch (s->vlan_mode) {
3111 case PORT_VLAN_ACCESS:
3112 trunks = NULL;
3113 break;
3114
3115 case PORT_VLAN_TRUNK:
3116 trunks = CONST_CAST(unsigned long *, s->trunks);
3117 break;
3118
3119 case PORT_VLAN_NATIVE_UNTAGGED:
3120 case PORT_VLAN_NATIVE_TAGGED:
3121 if (vlan != 0 && (!s->trunks
3122 || !bitmap_is_set(s->trunks, vlan)
3123 || bitmap_is_set(s->trunks, 0))) {
3124 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3125 if (s->trunks) {
3126 trunks = bitmap_clone(s->trunks, 4096);
3127 } else {
3128 trunks = bitmap_allocate1(4096);
3129 }
3130 bitmap_set1(trunks, vlan);
3131 bitmap_set0(trunks, 0);
3132 } else {
3133 trunks = CONST_CAST(unsigned long *, s->trunks);
3134 }
3135 break;
3136
3137 case PORT_VLAN_DOT1Q_TUNNEL:
3138 cvlans = CONST_CAST(unsigned long *, s->cvlans);
3139 break;
3140
3141 default:
3142 OVS_NOT_REACHED();
3143 }
3144 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
3145 free(bundle->trunks);
3146 if (trunks == s->trunks) {
3147 bundle->trunks = vlan_bitmap_clone(trunks);
3148 } else {
3149 bundle->trunks = trunks;
3150 trunks = NULL;
3151 }
3152 need_flush = true;
3153 }
3154 if (trunks != s->trunks) {
3155 free(trunks);
3156 }
3157
3158 if (!vlan_bitmap_equal(cvlans, bundle->cvlans)) {
3159 free(bundle->cvlans);
3160 if (cvlans == s->cvlans) {
3161 bundle->cvlans = vlan_bitmap_clone(cvlans);
3162 } else {
3163 bundle->cvlans = cvlans;
3164 cvlans = NULL;
3165 }
3166 need_flush = true;
3167 }
3168 if (cvlans != s->cvlans) {
3169 free(cvlans);
3170 }
3171
3172 /* Bonding. */
3173 if (!ovs_list_is_short(&bundle->ports)) {
3174 bundle->ofproto->has_bonded_bundles = true;
3175 if (bundle->bond) {
3176 if (bond_reconfigure(bundle->bond, s->bond)) {
3177 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3178 }
3179 } else {
3180 bundle->bond = bond_create(s->bond, ofproto);
3181 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3182 }
3183
3184 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3185 bond_slave_register(bundle->bond, port,
3186 port->up.ofp_port, port->up.netdev);
3187 }
3188 } else {
3189 bond_unref(bundle->bond);
3190 bundle->bond = NULL;
3191 }
3192
3193 /* Set proteced port mode */
3194 if (s->protected != bundle->protected) {
3195 bundle->protected = s->protected;
3196 need_flush = true;
3197 }
3198
3199 /* If we changed something that would affect MAC learning, un-learn
3200 * everything on this port and force flow revalidation. */
3201 if (need_flush) {
3202 bundle_flush_macs(bundle, false);
3203 mcast_snooping_flush_bundle(ofproto->ms, bundle);
3204 }
3205
3206 return 0;
3207 }
3208
3209 static void
3210 bundle_remove(struct ofport *port_)
3211 {
3212 struct ofport_dpif *port = ofport_dpif_cast(port_);
3213 struct ofbundle *bundle = port->bundle;
3214
3215 if (bundle) {
3216 bundle_del_port(port);
3217 if (ovs_list_is_empty(&bundle->ports)) {
3218 bundle_destroy(bundle);
3219 } else if (ovs_list_is_short(&bundle->ports)) {
3220 bond_unref(bundle->bond);
3221 bundle->bond = NULL;
3222 }
3223 }
3224 }
3225
3226 static void
3227 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
3228 {
3229 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3230 struct ofport_dpif *port = port_;
3231 struct eth_addr ea;
3232 int error;
3233
3234 error = netdev_get_etheraddr(port->up.netdev, &ea);
3235 if (!error) {
3236 struct dp_packet packet;
3237 void *packet_pdu;
3238
3239 dp_packet_init(&packet, 0);
3240 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3241 pdu_size);
3242 memcpy(packet_pdu, pdu, pdu_size);
3243
3244 ofproto_dpif_send_packet(port, false, &packet);
3245 dp_packet_uninit(&packet);
3246 } else {
3247 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3248 "%s (%s)", port->bundle->name,
3249 netdev_get_name(port->up.netdev), ovs_strerror(error));
3250 }
3251 }
3252
3253 static void
3254 bundle_send_learning_packets(struct ofbundle *bundle)
3255 {
3256 struct ofproto_dpif *ofproto = bundle->ofproto;
3257 int error, n_packets, n_errors;
3258 struct mac_entry *e;
3259 struct pkt_list {
3260 struct ovs_list list_node;
3261 struct ofport_dpif *port;
3262 struct dp_packet *pkt;
3263 } *pkt_node;
3264 struct ovs_list packets;
3265
3266 ovs_list_init(&packets);
3267 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3268 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3269 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3270 pkt_node = xmalloc(sizeof *pkt_node);
3271 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3272 e->mac, e->vlan,
3273 (void **)&pkt_node->port);
3274 ovs_list_push_back(&packets, &pkt_node->list_node);
3275 }
3276 }
3277 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3278
3279 error = n_packets = n_errors = 0;
3280 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
3281 int ret;
3282
3283 ret = ofproto_dpif_send_packet(pkt_node->port, false, pkt_node->pkt);
3284 dp_packet_delete(pkt_node->pkt);
3285 free(pkt_node);
3286 if (ret) {
3287 error = ret;
3288 n_errors++;
3289 }
3290 n_packets++;
3291 }
3292
3293 if (n_errors) {
3294 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3295 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3296 "packets, last error was: %s",
3297 bundle->name, n_errors, n_packets, ovs_strerror(error));
3298 } else {
3299 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3300 bundle->name, n_packets);
3301 }
3302 }
3303
3304 static void
3305 bundle_run(struct ofbundle *bundle)
3306 {
3307 if (bundle->lacp) {
3308 lacp_run(bundle->lacp, send_pdu_cb);
3309 }
3310 if (bundle->bond) {
3311 struct ofport_dpif *port;
3312
3313 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3314 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3315 }
3316
3317 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3318 bundle->ofproto->backer->need_revalidate = REV_BOND;
3319 }
3320
3321 if (bond_should_send_learning_packets(bundle->bond)) {
3322 bundle_send_learning_packets(bundle);
3323 }
3324 }
3325 }
3326
3327 static void
3328 bundle_wait(struct ofbundle *bundle)
3329 {
3330 if (bundle->lacp) {
3331 lacp_wait(bundle->lacp);
3332 }
3333 if (bundle->bond) {
3334 bond_wait(bundle->bond);
3335 }
3336 }
3337 \f
3338 /* Mirrors. */
3339
3340 static int
3341 mirror_set__(struct ofproto *ofproto_, void *aux,
3342 const struct ofproto_mirror_settings *s)
3343 {
3344 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3345 struct ofbundle **srcs, **dsts;
3346 int error;
3347 size_t i;
3348
3349 if (!s) {
3350 mirror_destroy(ofproto->mbridge, aux);
3351 return 0;
3352 }
3353
3354 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3355 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3356
3357 for (i = 0; i < s->n_srcs; i++) {
3358 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3359 }
3360
3361 for (i = 0; i < s->n_dsts; i++) {
3362 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3363 }
3364
3365 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3366 s->n_dsts, s->src_vlans,
3367 bundle_lookup(ofproto, s->out_bundle),
3368 s->snaplen, s->out_vlan);
3369 free(srcs);
3370 free(dsts);
3371 return error;
3372 }
3373
3374 static int
3375 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3376 uint64_t *packets, uint64_t *bytes)
3377 {
3378 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3379 bytes);
3380 }
3381
3382 static int
3383 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3384 {
3385 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3386 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3387 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3388 mac_learning_flush(ofproto->ml);
3389 }
3390 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3391 return 0;
3392 }
3393
3394 static bool
3395 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3396 {
3397 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3398 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3399 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3400 }
3401
3402 static void
3403 forward_bpdu_changed(struct ofproto *ofproto_)
3404 {
3405 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3406 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3407 }
3408
3409 static void
3410 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3411 size_t max_entries)
3412 {
3413 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3414 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3415 mac_learning_set_idle_time(ofproto->ml, idle_time);
3416 mac_learning_set_max_entries(ofproto->ml, max_entries);
3417 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3418 }
3419
3420 /* Configures multicast snooping on 'ofport' using the settings
3421 * defined in 's'. */
3422 static int
3423 set_mcast_snooping(struct ofproto *ofproto_,
3424 const struct ofproto_mcast_snooping_settings *s)
3425 {
3426 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3427
3428 /* Only revalidate flows if the configuration changed. */
3429 if (!s != !ofproto->ms) {
3430 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3431 }
3432
3433 if (s) {
3434 if (!ofproto->ms) {
3435 ofproto->ms = mcast_snooping_create();
3436 }
3437
3438 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3439 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3440 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3441 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3442 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3443 }
3444 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3445 } else {
3446 mcast_snooping_unref(ofproto->ms);
3447 ofproto->ms = NULL;
3448 }
3449
3450 return 0;
3451 }
3452
3453 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3454 static int
3455 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3456 const struct ofproto_mcast_snooping_port_settings *s)
3457 {
3458 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3459 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3460
3461 if (ofproto->ms && s) {
3462 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3463 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3464 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3465 s->flood_reports);
3466 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3467 }
3468 return 0;
3469 }
3470
3471 \f
3472 /* Ports. */
3473
3474 struct ofport_dpif *
3475 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3476 {
3477 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3478 return ofport ? ofport_dpif_cast(ofport) : NULL;
3479 }
3480
3481 static void
3482 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3483 struct ofproto_port *ofproto_port,
3484 struct dpif_port *dpif_port)
3485 {
3486 ofproto_port->name = dpif_port->name;
3487 ofproto_port->type = dpif_port->type;
3488 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3489 }
3490
3491 static void
3492 ofport_update_peer(struct ofport_dpif *ofport)
3493 {
3494 const struct ofproto_dpif *ofproto;
3495 struct dpif_backer *backer;
3496 char *peer_name;
3497
3498 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3499 return;
3500 }
3501
3502 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3503 backer->need_revalidate = REV_RECONFIGURE;
3504
3505 if (ofport->peer) {
3506 ofport->peer->peer = NULL;
3507 ofport->peer = NULL;
3508 }
3509
3510 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3511 if (!peer_name) {
3512 return;
3513 }
3514
3515 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3516 struct ofport *peer_ofport;
3517 struct ofport_dpif *peer;
3518 char *peer_peer;
3519
3520 if (ofproto->backer != backer) {
3521 continue;
3522 }
3523
3524 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3525 if (!peer_ofport) {
3526 continue;
3527 }
3528
3529 peer = ofport_dpif_cast(peer_ofport);
3530 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3531 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3532 peer_peer)) {
3533 ofport->peer = peer;
3534 ofport->peer->peer = ofport;
3535 }
3536 free(peer_peer);
3537
3538 break;
3539 }
3540 free(peer_name);
3541 }
3542
3543 static void
3544 port_run(struct ofport_dpif *ofport)
3545 {
3546 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3547 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3548 bool enable = netdev_get_carrier(ofport->up.netdev);
3549 bool cfm_enable = false;
3550 bool bfd_enable = false;
3551
3552 ofport->carrier_seq = carrier_seq;
3553
3554 if (ofport->cfm) {
3555 int cfm_opup = cfm_get_opup(ofport->cfm);
3556
3557 cfm_enable = !cfm_get_fault(ofport->cfm);
3558
3559 if (cfm_opup >= 0) {
3560 cfm_enable = cfm_enable && cfm_opup;
3561 }
3562 }
3563
3564 if (ofport->bfd) {
3565 bfd_enable = bfd_forwarding(ofport->bfd);
3566 }
3567
3568 if (ofport->bfd || ofport->cfm) {
3569 enable = enable && (cfm_enable || bfd_enable);
3570 }
3571
3572 if (ofport->bundle) {
3573 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3574 if (carrier_changed) {
3575 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3576 }
3577 }
3578
3579 if (ofport->may_enable != enable) {
3580 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3581
3582 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3583
3584 if (ofport->rstp_port) {
3585 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3586 }
3587
3588 /* Propagate liveness, unless the link is administratively or
3589 * operationally down. */
3590 if (!(ofport->up.pp.config & OFPUTIL_PC_PORT_DOWN) &&
3591 !(ofport->up.pp.state & OFPUTIL_PS_LINK_DOWN)) {
3592 enum ofputil_port_state of_state = ofport->up.pp.state;
3593 if (enable) {
3594 of_state |= OFPUTIL_PS_LIVE;
3595 } else {
3596 of_state &= ~OFPUTIL_PS_LIVE;
3597 }
3598 ofproto_port_set_state(&ofport->up, of_state);
3599 }
3600 }
3601
3602 ofport->may_enable = enable;
3603 }
3604
3605 static int
3606 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3607 struct ofproto_port *ofproto_port)
3608 {
3609 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3610 struct dpif_port dpif_port;
3611 int error;
3612
3613 if (sset_contains(&ofproto->ghost_ports, devname)) {
3614 const char *type = netdev_get_type_from_name(devname);
3615
3616 /* We may be called before ofproto->up.port_by_name is populated with
3617 * the appropriate ofport. For this reason, we must get the name and
3618 * type from the netdev layer directly. */
3619 if (type) {
3620 const struct ofport *ofport;
3621
3622 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3623 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3624 ofproto_port->name = xstrdup(devname);
3625 ofproto_port->type = xstrdup(type);
3626 return 0;
3627 }
3628 return ENODEV;
3629 }
3630
3631 if (!sset_contains(&ofproto->ports, devname)) {
3632 return ENODEV;
3633 }
3634 error = dpif_port_query_by_name(ofproto->backer->dpif,
3635 devname, &dpif_port);
3636 if (!error) {
3637 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3638 }
3639 return error;
3640 }
3641
3642 static int
3643 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3644 {
3645 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3646 const char *devname = netdev_get_name(netdev);
3647 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3648 const char *dp_port_name;
3649
3650 if (netdev_vport_is_patch(netdev)) {
3651 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3652 return 0;
3653 }
3654
3655 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3656 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3657 odp_port_t port_no = ODPP_NONE;
3658 int error;
3659
3660 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3661 if (error) {
3662 return error;
3663 }
3664 if (netdev_get_tunnel_config(netdev)) {
3665 simap_put(&ofproto->backer->tnl_backers,
3666 dp_port_name, odp_to_u32(port_no));
3667 }
3668 }
3669
3670 if (netdev_get_tunnel_config(netdev)) {
3671 sset_add(&ofproto->ghost_ports, devname);
3672 } else {
3673 sset_add(&ofproto->ports, devname);
3674 }
3675 return 0;
3676 }
3677
3678 static int
3679 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3680 {
3681 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3682 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3683 int error = 0;
3684
3685 if (!ofport) {
3686 return 0;
3687 }
3688
3689 sset_find_and_delete(&ofproto->ghost_ports,
3690 netdev_get_name(ofport->up.netdev));
3691 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3692 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3693 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3694 if (!error) {
3695 /* The caller is going to close ofport->up.netdev. If this is a
3696 * bonded port, then the bond is using that netdev, so remove it
3697 * from the bond. The client will need to reconfigure everything
3698 * after deleting ports, so then the slave will get re-added. */
3699 bundle_remove(&ofport->up);
3700 }
3701 }
3702 return error;
3703 }
3704
3705 static int
3706 port_set_config(const struct ofport *ofport_, const struct smap *cfg)
3707 {
3708 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3709 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3710
3711 if (sset_contains(&ofproto->ghost_ports,
3712 netdev_get_name(ofport->up.netdev))) {
3713 return 0;
3714 }
3715
3716 return dpif_port_set_config(ofproto->backer->dpif, ofport->odp_port, cfg);
3717 }
3718
3719 static int
3720 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3721 {
3722 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3723 int error;
3724
3725 error = netdev_get_stats(ofport->up.netdev, stats);
3726
3727 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3728 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3729
3730 ovs_mutex_lock(&ofproto->stats_mutex);
3731 /* ofproto->stats.tx_packets represents packets that we created
3732 * internally and sent to some port (e.g. packets sent with
3733 * ofproto_dpif_send_packet()). Account for them as if they had
3734 * come from OFPP_LOCAL and got forwarded. */
3735
3736 if (stats->rx_packets != UINT64_MAX) {
3737 stats->rx_packets += ofproto->stats.tx_packets;
3738 }
3739
3740 if (stats->rx_bytes != UINT64_MAX) {
3741 stats->rx_bytes += ofproto->stats.tx_bytes;
3742 }
3743
3744 /* ofproto->stats.rx_packets represents packets that were received on
3745 * some port and we processed internally and dropped (e.g. STP).
3746 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3747
3748 if (stats->tx_packets != UINT64_MAX) {
3749 stats->tx_packets += ofproto->stats.rx_packets;
3750 }
3751
3752 if (stats->tx_bytes != UINT64_MAX) {
3753 stats->tx_bytes += ofproto->stats.rx_bytes;
3754 }
3755 ovs_mutex_unlock(&ofproto->stats_mutex);
3756 }
3757
3758 return error;
3759 }
3760
3761 static int
3762 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3763 {
3764 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3765 if (ofport->bundle && ofport->bundle->lacp) {
3766 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3767 return 0;
3768 }
3769 }
3770 return -1;
3771 }
3772
3773 struct port_dump_state {
3774 struct sset_position pos;
3775 bool ghost;
3776
3777 struct ofproto_port port;
3778 bool has_port;
3779 };
3780
3781 static int
3782 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3783 {
3784 *statep = xzalloc(sizeof(struct port_dump_state));
3785 return 0;
3786 }
3787
3788 static int
3789 port_dump_next(const struct ofproto *ofproto_, void *state_,
3790 struct ofproto_port *port)
3791 {
3792 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3793 struct port_dump_state *state = state_;
3794 const struct sset *sset;
3795 struct sset_node *node;
3796
3797 if (state->has_port) {
3798 ofproto_port_destroy(&state->port);
3799 state->has_port = false;
3800 }
3801 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3802 while ((node = sset_at_position(sset, &state->pos))) {
3803 int error;
3804
3805 error = port_query_by_name(ofproto_, node->name, &state->port);
3806 if (!error) {
3807 *port = state->port;
3808 state->has_port = true;
3809 return 0;
3810 } else if (error != ENODEV) {
3811 return error;
3812 }
3813 }
3814
3815 if (!state->ghost) {
3816 state->ghost = true;
3817 memset(&state->pos, 0, sizeof state->pos);
3818 return port_dump_next(ofproto_, state_, port);
3819 }
3820
3821 return EOF;
3822 }
3823
3824 static int
3825 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3826 {
3827 struct port_dump_state *state = state_;
3828
3829 if (state->has_port) {
3830 ofproto_port_destroy(&state->port);
3831 }
3832 free(state);
3833 return 0;
3834 }
3835
3836 static int
3837 port_poll(const struct ofproto *ofproto_, char **devnamep)
3838 {
3839 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3840
3841 if (ofproto->port_poll_errno) {
3842 int error = ofproto->port_poll_errno;
3843 ofproto->port_poll_errno = 0;
3844 return error;
3845 }
3846
3847 if (sset_is_empty(&ofproto->port_poll_set)) {
3848 return EAGAIN;
3849 }
3850
3851 *devnamep = sset_pop(&ofproto->port_poll_set);
3852 return 0;
3853 }
3854
3855 static void
3856 port_poll_wait(const struct ofproto *ofproto_)
3857 {
3858 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3859 dpif_port_poll_wait(ofproto->backer->dpif);
3860 }
3861
3862 static int
3863 port_is_lacp_current(const struct ofport *ofport_)
3864 {
3865 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3866 return (ofport->bundle && ofport->bundle->lacp
3867 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3868 : -1);
3869 }
3870 \f
3871 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3872 * then delete it entirely. */
3873 static void
3874 rule_expire(struct rule_dpif *rule, long long now)
3875 OVS_REQUIRES(ofproto_mutex)
3876 {
3877 uint16_t hard_timeout, idle_timeout;
3878 int reason = -1;
3879
3880 hard_timeout = rule->up.hard_timeout;
3881 idle_timeout = rule->up.idle_timeout;
3882
3883 /* Has 'rule' expired? */
3884 if (hard_timeout) {
3885 long long int modified;
3886
3887 ovs_mutex_lock(&rule->up.mutex);
3888 modified = rule->up.modified;
3889 ovs_mutex_unlock(&rule->up.mutex);
3890
3891 if (now > modified + hard_timeout * 1000) {
3892 reason = OFPRR_HARD_TIMEOUT;
3893 }
3894 }
3895
3896 if (reason < 0 && idle_timeout) {
3897 long long int used;
3898
3899 ovs_mutex_lock(&rule->stats_mutex);
3900 used = rule->stats.used;
3901 ovs_mutex_unlock(&rule->stats_mutex);
3902
3903 if (now > used + idle_timeout * 1000) {
3904 reason = OFPRR_IDLE_TIMEOUT;
3905 }
3906 }
3907
3908 if (reason >= 0) {
3909 COVERAGE_INC(ofproto_dpif_expired);
3910 ofproto_rule_expire(&rule->up, reason);
3911 }
3912 }
3913
3914 static void
3915 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif *ofproto,
3916 ofp_port_t in_port, struct dp_packet *packet)
3917 {
3918 if (in_port == OFPP_NONE) {
3919 in_port = OFPP_LOCAL;
3920 }
3921 packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3922 }
3923
3924 int
3925 ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
3926 ovs_version_t version, const struct flow *flow,
3927 struct rule_dpif *rule,
3928 const struct ofpact *ofpacts, size_t ofpacts_len,
3929 int depth, int resubmits,
3930 struct dp_packet *packet)
3931 {
3932 struct dpif_flow_stats stats;
3933 struct xlate_out xout;
3934 struct xlate_in xin;
3935 struct dpif_execute execute;
3936 int error;
3937
3938 ovs_assert((rule != NULL) != (ofpacts != NULL));
3939
3940 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3941
3942 if (rule) {
3943 rule_dpif_credit_stats(rule, &stats);
3944 }
3945
3946 uint64_t odp_actions_stub[1024 / 8];
3947 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
3948 xlate_in_init(&xin, ofproto, version, flow, flow->in_port.ofp_port, rule,
3949 stats.tcp_flags, packet, NULL, &odp_actions);
3950 xin.ofpacts = ofpacts;
3951 xin.ofpacts_len = ofpacts_len;
3952 xin.resubmit_stats = &stats;
3953 xin.depth = depth;
3954 xin.resubmits = resubmits;
3955 if (xlate_actions(&xin, &xout) != XLATE_OK) {
3956 error = EINVAL;
3957 goto out;
3958 }
3959
3960 execute.actions = odp_actions.data;
3961 execute.actions_len = odp_actions.size;
3962
3963 pkt_metadata_from_flow(&packet->md, flow);
3964 execute.packet = packet;
3965 execute.flow = flow;
3966 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3967 execute.probe = false;
3968 execute.mtu = 0;
3969
3970 /* Fix up in_port. */
3971 ofproto_dpif_set_packet_odp_port(ofproto, flow->in_port.ofp_port, packet);
3972
3973 error = dpif_execute(ofproto->backer->dpif, &execute);
3974 out:
3975 xlate_out_uninit(&xout);
3976 ofpbuf_uninit(&odp_actions);
3977
3978 return error;
3979 }
3980
3981 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3982 * 'flow' must reflect the data in 'packet'. */
3983 int
3984 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3985 ovs_version_t version, const struct flow *flow,
3986 struct rule_dpif *rule,
3987 const struct ofpact *ofpacts, size_t ofpacts_len,
3988 struct dp_packet *packet)
3989 {
3990 return ofproto_dpif_execute_actions__(ofproto, version, flow, rule,
3991 ofpacts, ofpacts_len, 0, 0, packet);
3992 }
3993
3994 static void
3995 rule_dpif_credit_stats__(struct rule_dpif *rule,
3996 const struct dpif_flow_stats *stats,
3997 bool credit_counts)
3998 OVS_REQUIRES(rule->stats_mutex)
3999 {
4000 if (credit_counts) {
4001 rule->stats.n_packets += stats->n_packets;
4002 rule->stats.n_bytes += stats->n_bytes;
4003 }
4004 rule->stats.used = MAX(rule->stats.used, stats->used);
4005 }
4006
4007 void
4008 rule_dpif_credit_stats(struct rule_dpif *rule,
4009 const struct dpif_flow_stats *stats)
4010 {
4011 ovs_mutex_lock(&rule->stats_mutex);
4012 if (OVS_UNLIKELY(rule->new_rule)) {
4013 ovs_mutex_lock(&rule->new_rule->stats_mutex);
4014 rule_dpif_credit_stats__(rule->new_rule, stats, rule->forward_counts);
4015 ovs_mutex_unlock(&rule->new_rule->stats_mutex);
4016 } else {
4017 rule_dpif_credit_stats__(rule, stats, true);
4018 }
4019 ovs_mutex_unlock(&rule->stats_mutex);
4020 }
4021
4022 /* Sets 'rule''s recirculation id. */
4023 static void
4024 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
4025 OVS_REQUIRES(rule->up.mutex)
4026 {
4027 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
4028 if (rule->recirc_id == id) {
4029 /* Release the new reference to the same id. */
4030 recirc_free_id(id);
4031 } else {
4032 rule->recirc_id = id;
4033 }
4034 }
4035
4036 /* Sets 'rule''s recirculation id. */
4037 void
4038 rule_set_recirc_id(struct rule *rule_, uint32_t id)
4039 {
4040 struct rule_dpif *rule = rule_dpif_cast(rule_);
4041
4042 ovs_mutex_lock(&rule->up.mutex);
4043 rule_dpif_set_recirc_id(rule, id);
4044 ovs_mutex_unlock(&rule->up.mutex);
4045 }
4046
4047 ovs_version_t
4048 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto)
4049 {
4050 ovs_version_t version;
4051
4052 /* Use memory_order_acquire to signify that any following memory accesses
4053 * can not be reordered to happen before this atomic read. This makes sure
4054 * all following reads relate to this or a newer version, but never to an
4055 * older version. */
4056 atomic_read_explicit(&ofproto->tables_version, &version,
4057 memory_order_acquire);
4058 return version;
4059 }
4060
4061 /* The returned rule (if any) is valid at least until the next RCU quiescent
4062 * period. If the rule needs to stay around longer, the caller should take
4063 * a reference.
4064 *
4065 * 'flow' is non-const to allow for temporary modifications during the lookup.
4066 * Any changes are restored before returning. */
4067 static struct rule_dpif *
4068 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, ovs_version_t version,
4069 uint8_t table_id, struct flow *flow,
4070 struct flow_wildcards *wc)
4071 {
4072 struct classifier *cls = &ofproto->up.tables[table_id].cls;
4073 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
4074 flow, wc)));
4075 }
4076
4077 void
4078 ofproto_dpif_credit_table_stats(struct ofproto_dpif *ofproto, uint8_t table_id,
4079 uint64_t n_matches, uint64_t n_misses)
4080 {
4081 struct oftable *tbl = &ofproto->up.tables[table_id];
4082 unsigned long orig;
4083
4084 if (n_matches) {
4085 atomic_add_relaxed(&tbl->n_matched, n_matches, &orig);
4086 }
4087 if (n_misses) {
4088 atomic_add_relaxed(&tbl->n_missed, n_misses, &orig);
4089 }
4090 }
4091
4092 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
4093 * table '*table_id'. Returns the rule that was found, which may be one of the
4094 * special rules according to packet miss hadling. If 'may_packet_in' is
4095 * false, returning of the miss_rule (which issues packet ins for the
4096 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
4097 * that were used during the lookup.
4098 *
4099 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
4100 * if none is found then the table miss configuration for that table is
4101 * honored, which can result in additional lookups in other OpenFlow tables.
4102 * In this case the function updates '*table_id' to reflect the final OpenFlow
4103 * table that was searched.
4104 *
4105 * If 'honor_table_miss' is false, then only one table lookup occurs, in
4106 * '*table_id'.
4107 *
4108 * The rule is returned in '*rule', which is valid at least until the next
4109 * RCU quiescent period. If the '*rule' needs to stay around longer, the
4110 * caller must take a reference.
4111 *
4112 * 'in_port' allows the lookup to take place as if the in port had the value
4113 * 'in_port'. This is needed for resubmit action support.
4114 *
4115 * 'flow' is non-const to allow for temporary modifications during the lookup.
4116 * Any changes are restored before returning. */
4117 struct rule_dpif *
4118 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
4119 ovs_version_t version, struct flow *flow,
4120 struct flow_wildcards *wc,
4121 const struct dpif_flow_stats *stats,
4122 uint8_t *table_id, ofp_port_t in_port,
4123 bool may_packet_in, bool honor_table_miss,
4124 struct xlate_cache *xcache)
4125 {
4126 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
4127 ofp_port_t old_in_port = flow->in_port.ofp_port;
4128 enum ofputil_table_miss miss_config;
4129 struct rule_dpif *rule;
4130 uint8_t next_id;
4131
4132 /* We always unwildcard nw_frag (for IP), so they
4133 * need not be unwildcarded here. */
4134 if (flow->nw_frag & FLOW_NW_FRAG_ANY
4135 && ofproto->up.frag_handling != OFPUTIL_FRAG_NX_MATCH) {
4136 if (ofproto->up.frag_handling == OFPUTIL_FRAG_NORMAL) {
4137 /* We must pretend that transport ports are unavailable. */
4138 flow->tp_src = htons(0);
4139 flow->tp_dst = htons(0);
4140 } else {
4141 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
4142 * Use the drop_frags_rule (which cannot disappear). */
4143 rule = ofproto->drop_frags_rule;
4144 if (stats) {
4145 struct oftable *tbl = &ofproto->up.tables[*table_id];
4146 unsigned long orig;
4147
4148 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
4149 }
4150 if (xcache) {
4151 struct xc_entry *entry;
4152
4153 entry = xlate_cache_add_entry(xcache, XC_TABLE);
4154 entry->table.ofproto = ofproto;
4155 entry->table.id = *table_id;
4156 entry->table.match = true;
4157 }
4158 return rule;
4159 }
4160 }
4161
4162 /* Look up a flow with 'in_port' as the input port. Then restore the
4163 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
4164 * have surprising behavior). */
4165 flow->in_port.ofp_port = in_port;
4166
4167 /* Our current implementation depends on n_tables == N_TABLES, and
4168 * TBL_INTERNAL being the last table. */
4169 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
4170
4171 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
4172
4173 for (next_id = *table_id;
4174 next_id < ofproto->up.n_tables;
4175 next_id++, next_id += (next_id == TBL_INTERNAL))
4176 {
4177 *table_id = next_id;
4178 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
4179 if (stats) {
4180 struct oftable *tbl = &ofproto->up.tables[next_id];
4181 unsigned long orig;
4182
4183 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
4184 stats->n_packets, &orig);
4185 }
4186 if (xcache) {
4187 struct xc_entry *entry;
4188
4189 entry = xlate_cache_add_entry(xcache, XC_TABLE);
4190 entry->table.ofproto = ofproto;
4191 entry->table.id = next_id;
4192 entry->table.match = (rule != NULL);
4193 }
4194 if (rule) {
4195 goto out; /* Match. */
4196 }
4197 if (honor_table_miss) {
4198 miss_config = ofproto_table_get_miss_config(&ofproto->up,
4199 *table_id);
4200 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
4201 continue;
4202 }
4203 }
4204 break;
4205 }
4206 /* Miss. */
4207 rule = ofproto->no_packet_in_rule;
4208 if (may_packet_in) {
4209 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
4210 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
4211 struct ofport_dpif *port;
4212
4213 port = ofp_port_to_ofport(ofproto, old_in_port);
4214 if (!port) {
4215 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu32,
4216 old_in_port);
4217 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
4218 rule = ofproto->miss_rule;
4219 }
4220 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
4221 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
4222 rule = ofproto->miss_rule;
4223 }
4224 }
4225 out:
4226 /* Restore port numbers, as they may have been modified above. */
4227 flow->tp_src = old_tp_src;
4228 flow->tp_dst = old_tp_dst;
4229 /* Restore the old in port. */
4230 flow->in_port.ofp_port = old_in_port;
4231
4232 return rule;
4233 }
4234
4235 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4236 {
4237 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4238 }
4239
4240 static struct rule *
4241 rule_alloc(void)
4242 {
4243 struct rule_dpif *rule = xzalloc(sizeof *rule);
4244 return &rule->up;
4245 }
4246
4247 static void
4248 rule_dealloc(struct rule *rule_)
4249 {
4250 struct rule_dpif *rule = rule_dpif_cast(rule_);
4251 free(rule);
4252 }
4253
4254 static enum ofperr
4255 check_mask(struct ofproto_dpif *ofproto, const struct miniflow *flow)
4256 {
4257 const struct odp_support *support;
4258 uint16_t ct_state, ct_zone;
4259 ovs_u128 ct_label;
4260 uint32_t ct_mark;
4261
4262 support = &ofproto->backer->support.odp;
4263 ct_state = MINIFLOW_GET_U8(flow, ct_state);
4264
4265 /* Do not bother dissecting the flow further if the datapath supports all
4266 * the features we know of. */
4267 if (support->ct_state && support->ct_zone && support->ct_mark
4268 && support->ct_label && support->ct_state_nat
4269 && support->ct_orig_tuple && support->ct_orig_tuple6) {
4270 return ct_state & CS_UNSUPPORTED_MASK ? OFPERR_OFPBMC_BAD_MASK : 0;
4271 }
4272
4273 ct_zone = MINIFLOW_GET_U16(flow, ct_zone);
4274 ct_mark = MINIFLOW_GET_U32(flow, ct_mark);
4275 ct_label = MINIFLOW_GET_U128(flow, ct_label);
4276
4277 if ((ct_state && !support->ct_state)
4278 || (ct_state & CS_UNSUPPORTED_MASK)
4279 || ((ct_state & (CS_SRC_NAT | CS_DST_NAT)) && !support->ct_state_nat)
4280 || (ct_zone && !support->ct_zone)
4281 || (ct_mark && !support->ct_mark)
4282 || (!ovs_u128_is_zero(ct_label) && !support->ct_label)) {
4283 return OFPERR_OFPBMC_BAD_MASK;
4284 }
4285
4286 if (!support->ct_orig_tuple && !support->ct_orig_tuple6
4287 && (MINIFLOW_GET_U8(flow, ct_nw_proto)
4288 || MINIFLOW_GET_U16(flow, ct_tp_src)
4289 || MINIFLOW_GET_U16(flow, ct_tp_dst))) {
4290 return OFPERR_OFPBMC_BAD_MASK;
4291 }
4292
4293 if (!support->ct_orig_tuple
4294 && (MINIFLOW_GET_U32(flow, ct_nw_src)
4295 || MINIFLOW_GET_U32(flow, ct_nw_dst))) {
4296 return OFPERR_OFPBMC_BAD_MASK;
4297 }
4298
4299 if (!support->ct_orig_tuple6
4300 && (!ovs_u128_is_zero(MINIFLOW_GET_U128(flow, ct_ipv6_src))
4301 || !ovs_u128_is_zero(MINIFLOW_GET_U128(flow, ct_ipv6_dst)))) {
4302 return OFPERR_OFPBMC_BAD_MASK;
4303 }
4304
4305 return 0;
4306 }
4307
4308 static void
4309 report_unsupported_act(const char *action, const char *detail)
4310 {
4311 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4312 VLOG_WARN_RL(&rl, "Rejecting %s action because datapath does not support"
4313 "%s%s (your kernel module may be out of date)",
4314 action, detail ? " " : "", detail ? detail : "");
4315 }
4316
4317 static enum ofperr
4318 check_actions(const struct ofproto_dpif *ofproto,
4319 const struct rule_actions *const actions)
4320 {
4321 const struct ofpact *ofpact;
4322 const struct odp_support *support = &ofproto->backer->support.odp;
4323
4324 OFPACT_FOR_EACH (ofpact, actions->ofpacts, actions->ofpacts_len) {
4325 if (ofpact->type == OFPACT_CT) {
4326 const struct ofpact_conntrack *ct;
4327 const struct ofpact *a;
4328
4329 ct = CONTAINER_OF(ofpact, struct ofpact_conntrack, ofpact);
4330
4331 if (!support->ct_state) {
4332 report_unsupported_act("ct", "ct action");
4333 return OFPERR_OFPBAC_BAD_TYPE;
4334 }
4335 if ((ct->zone_imm || ct->zone_src.field) && !support->ct_zone) {
4336 report_unsupported_act("ct", "ct zones");
4337 return OFPERR_OFPBAC_BAD_ARGUMENT;
4338 }
4339 /* So far the force commit feature is implemented together with the
4340 * original direction tuple feature by all datapaths, so we use the
4341 * support flag for the 'ct_orig_tuple' to indicate support for the
4342 * force commit feature as well. */
4343 if ((ct->flags & NX_CT_F_FORCE) && !support->ct_orig_tuple) {
4344 report_unsupported_act("ct", "force commit");
4345 return OFPERR_OFPBAC_BAD_ARGUMENT;
4346 }
4347
4348 OFPACT_FOR_EACH(a, ct->actions, ofpact_ct_get_action_len(ct)) {
4349 const struct mf_field *dst = ofpact_get_mf_dst(a);
4350
4351 if (a->type == OFPACT_NAT && !support->ct_state_nat) {
4352 /* The backer doesn't seem to support the NAT bits in
4353 * 'ct_state': assume that it doesn't support the NAT
4354 * action. */
4355 report_unsupported_act("ct", "nat");
4356 return OFPERR_OFPBAC_BAD_TYPE;
4357 }
4358 if (dst && ((dst->id == MFF_CT_MARK && !support->ct_mark) ||
4359 (dst->id == MFF_CT_LABEL && !support->ct_label))) {
4360 report_unsupported_act("ct", "setting mark and/or label");
4361 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
4362 }
4363 }
4364 } else if (ofpact->type == OFPACT_RESUBMIT) {
4365 struct ofpact_resubmit *resubmit = ofpact_get_RESUBMIT(ofpact);
4366
4367 if (resubmit->with_ct_orig && !support->ct_orig_tuple) {
4368 report_unsupported_act("resubmit",
4369 "ct original direction tuple");
4370 return OFPERR_OFPBAC_BAD_TYPE;
4371 }
4372 }
4373 }
4374
4375 return 0;
4376 }
4377
4378 static enum ofperr
4379 rule_check(struct rule *rule)
4380 {
4381 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4382 enum ofperr err;
4383
4384 err = check_mask(ofproto, &rule->cr.match.mask->masks);
4385 if (err) {
4386 return err;
4387 }
4388 return check_actions(ofproto, rule->actions);
4389 }
4390
4391 static enum ofperr
4392 rule_construct(struct rule *rule_)
4393 OVS_NO_THREAD_SAFETY_ANALYSIS
4394 {
4395 struct rule_dpif *rule = rule_dpif_cast(rule_);
4396 int error;
4397
4398 error = rule_check(rule_);
4399 if (error) {
4400 return error;
4401 }
4402
4403 ovs_mutex_init_adaptive(&rule->stats_mutex);
4404 rule->stats.n_packets = 0;
4405 rule->stats.n_bytes = 0;
4406 rule->stats.used = rule->up.modified;
4407 rule->recirc_id = 0;
4408 rule->new_rule = NULL;
4409 rule->forward_counts = false;
4410
4411 return 0;
4412 }
4413
4414 static void
4415 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_counts)
4416 OVS_REQUIRES(ofproto_mutex)
4417 {
4418 struct rule_dpif *rule = rule_dpif_cast(rule_);
4419
4420 if (old_rule_) {
4421 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4422
4423 ovs_assert(!old_rule->new_rule);
4424
4425 /* Take a reference to the new rule, and refer all stats updates from
4426 * the old rule to the new rule. */
4427 ofproto_rule_ref(&rule->up);
4428
4429 ovs_mutex_lock(&old_rule->stats_mutex);
4430 ovs_mutex_lock(&rule->stats_mutex);
4431 old_rule->new_rule = rule; /* Forward future stats. */
4432 old_rule->forward_counts = forward_counts;
4433
4434 if (forward_counts) {
4435 rule->stats = old_rule->stats; /* Transfer stats to the new
4436 * rule. */
4437 } else {
4438 /* Used timestamp must be forwarded whenever a rule is modified. */
4439 rule->stats.used = old_rule->stats.used;
4440 }
4441 ovs_mutex_unlock(&rule->stats_mutex);
4442 ovs_mutex_unlock(&old_rule->stats_mutex);
4443 }
4444 }
4445
4446 static void
4447 rule_destruct(struct rule *rule_)
4448 OVS_NO_THREAD_SAFETY_ANALYSIS
4449 {
4450 struct rule_dpif *rule = rule_dpif_cast(rule_);
4451
4452 ovs_mutex_destroy(&rule->stats_mutex);
4453 /* Release reference to the new rule, if any. */
4454 if (rule->new_rule) {
4455 ofproto_rule_unref(&rule->new_rule->up);
4456 }
4457 if (rule->recirc_id) {
4458 recirc_free_id(rule->recirc_id);
4459 }
4460 }
4461
4462 static void
4463 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4464 long long int *used)
4465 {
4466 struct rule_dpif *rule = rule_dpif_cast(rule_);
4467
4468 ovs_mutex_lock(&rule->stats_mutex);
4469 if (OVS_UNLIKELY(rule->new_rule)) {
4470 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4471 } else {
4472 *packets = rule->stats.n_packets;
4473 *bytes = rule->stats.n_bytes;
4474 *used = rule->stats.used;
4475 }
4476 ovs_mutex_unlock(&rule->stats_mutex);
4477 }
4478
4479 struct ofproto_dpif_packet_out {
4480 struct xlate_cache xcache;
4481 struct ofpbuf odp_actions;
4482 struct recirc_refs rr;
4483 bool needs_help;
4484 };
4485
4486
4487 static struct ofproto_dpif_packet_out *
4488 ofproto_dpif_packet_out_new(void)
4489 {
4490 struct ofproto_dpif_packet_out *aux = xmalloc(sizeof *aux);
4491 xlate_cache_init(&aux->xcache);
4492 ofpbuf_init(&aux->odp_actions, 64);
4493 aux->rr = RECIRC_REFS_EMPTY_INITIALIZER;
4494 aux->needs_help = false;
4495
4496 return aux;
4497 }
4498
4499 static void
4500 ofproto_dpif_packet_out_delete(struct ofproto_dpif_packet_out *aux)
4501 {
4502 if (aux) {
4503 xlate_cache_uninit(&aux->xcache);
4504 ofpbuf_uninit(&aux->odp_actions);
4505 recirc_refs_unref(&aux->rr);
4506 free(aux);
4507 }
4508 }
4509
4510 static enum ofperr
4511 packet_xlate(struct ofproto *ofproto_, struct ofproto_packet_out *opo)
4512 OVS_REQUIRES(ofproto_mutex)
4513 {
4514 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4515 struct xlate_out xout;
4516 struct xlate_in xin;
4517 enum ofperr error = 0;
4518
4519 struct ofproto_dpif_packet_out *aux = ofproto_dpif_packet_out_new();
4520
4521 xlate_in_init(&xin, ofproto, opo->version, opo->flow,
4522 opo->flow->in_port.ofp_port, NULL, 0, opo->packet, NULL,
4523 &aux->odp_actions);
4524 xin.ofpacts = opo->ofpacts;
4525 xin.ofpacts_len = opo->ofpacts_len;
4526 /* No learning or stats, but collect side effects to xcache. */
4527 xin.allow_side_effects = false;
4528 xin.resubmit_stats = NULL;
4529 xin.xcache = &aux->xcache;
4530 xin.in_packet_out = true;
4531
4532 if (xlate_actions(&xin, &xout) != XLATE_OK) {
4533 error = OFPERR_OFPFMFC_UNKNOWN; /* Error processing actions. */
4534 goto error_out;
4535 } else {
4536 /* Prepare learn actions. */
4537 struct xc_entry *entry;
4538 struct ofpbuf entries = aux->xcache.entries;
4539
4540 XC_ENTRY_FOR_EACH (entry, &entries) {
4541 if (entry->type == XC_LEARN) {
4542 struct ofproto_flow_mod *ofm = entry->learn.ofm;
4543
4544 error = ofproto_flow_mod_learn_refresh(ofm);
4545 if (error) {
4546 goto error_out;
4547 }
4548 struct rule *rule = ofm->temp_rule;
4549 ofm->learn_adds_rule = (rule->state == RULE_INITIALIZED);
4550 if (ofm->learn_adds_rule) {
4551 /* If learning on a different bridge, must use its next
4552 * version number. */
4553 ofm->version = (rule->ofproto == ofproto_)
4554 ? opo->version : rule->ofproto->tables_version + 1;
4555 error = ofproto_flow_mod_learn_start(ofm);
4556 if (error) {
4557 goto error_out;
4558 }
4559 }
4560 }
4561 }
4562
4563 /* Success. */
4564 aux->needs_help = (xout.slow & SLOW_ACTION) != 0;
4565 recirc_refs_swap(&aux->rr, &xout.recircs); /* Hold recirc refs. */
4566 }
4567 xlate_out_uninit(&xout);
4568 opo->aux = aux;
4569 return 0;
4570
4571 error_out:
4572 xlate_out_uninit(&xout);
4573 ofproto_dpif_packet_out_delete(aux);
4574 opo->aux = NULL;
4575 return error;
4576 }
4577
4578 static void
4579 packet_xlate_revert(struct ofproto *ofproto OVS_UNUSED,
4580 struct ofproto_packet_out *opo)
4581 OVS_REQUIRES(ofproto_mutex)
4582 {
4583 struct ofproto_dpif_packet_out *aux = opo->aux;
4584 ovs_assert(aux);
4585
4586 /* Revert the learned flows. */
4587 struct xc_entry *entry;
4588 struct ofpbuf entries = aux->xcache.entries;
4589
4590 XC_ENTRY_FOR_EACH (entry, &entries) {
4591 if (entry->type == XC_LEARN && entry->learn.ofm->learn_adds_rule) {
4592 ofproto_flow_mod_learn_revert(entry->learn.ofm);
4593 }
4594 }
4595
4596 ofproto_dpif_packet_out_delete(aux);
4597 opo->aux = NULL;
4598 }
4599
4600 /* Push stats and perform side effects of flow translation. */
4601 static void
4602 ofproto_dpif_xcache_execute(struct ofproto_dpif *ofproto,
4603 struct xlate_cache *xcache,
4604 struct dpif_flow_stats *stats)
4605 OVS_REQUIRES(ofproto_mutex)
4606 {
4607 struct xc_entry *entry;
4608 struct ofpbuf entries = xcache->entries;
4609
4610 XC_ENTRY_FOR_EACH (entry, &entries) {
4611 switch (entry->type) {
4612 case XC_LEARN:
4613 /* Finish the learned flows. */
4614 if (entry->learn.ofm->learn_adds_rule) {
4615 ofproto_flow_mod_learn_finish(entry->learn.ofm, &ofproto->up);
4616 }
4617 break;
4618 case XC_FIN_TIMEOUT:
4619 if (stats->tcp_flags & (TCP_FIN | TCP_RST)) {
4620 /* 'ofproto_mutex' already held */
4621 ofproto_rule_reduce_timeouts__(&entry->fin.rule->up,
4622 entry->fin.idle,
4623 entry->fin.hard);
4624 }
4625 break;
4626 /* All the rest can be dealt with by the xlate layer. */
4627 case XC_TABLE:
4628 case XC_RULE:
4629 case XC_BOND:
4630 case XC_NETDEV:
4631 case XC_NETFLOW:
4632 case XC_MIRROR:
4633 case XC_NORMAL:
4634 case XC_GROUP:
4635 case XC_TNL_NEIGH:
4636 case XC_CONTROLLER:
4637 case XC_TUNNEL_HEADER:
4638 xlate_push_stats_entry(entry, stats);
4639 break;
4640 default:
4641 OVS_NOT_REACHED();
4642 }
4643 }
4644 }
4645
4646 static void
4647 packet_execute(struct ofproto *ofproto_, struct ofproto_packet_out *opo)
4648 OVS_REQUIRES(ofproto_mutex)
4649 {
4650 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4651 struct dpif_flow_stats stats;
4652 struct dpif_execute execute;
4653
4654 struct ofproto_dpif_packet_out *aux = opo->aux;
4655 ovs_assert(aux);
4656
4657 /* Run the side effects from the xcache. */
4658 dpif_flow_stats_extract(opo->flow, opo->packet, time_msec(), &stats);
4659 ofproto_dpif_xcache_execute(ofproto, &aux->xcache, &stats);
4660
4661 execute.actions = aux->odp_actions.data;
4662 execute.actions_len = aux->odp_actions.size;
4663
4664 pkt_metadata_from_flow(&opo->packet->md, opo->flow);
4665 execute.packet = opo->packet;
4666 execute.flow = opo->flow;
4667 execute.needs_help = aux->needs_help;
4668 execute.probe = false;
4669 execute.mtu = 0;
4670
4671 /* Fix up in_port. */
4672 ofproto_dpif_set_packet_odp_port(ofproto, opo->flow->in_port.ofp_port,
4673 opo->packet);
4674
4675 dpif_execute(ofproto->backer->dpif, &execute);
4676 ofproto_dpif_packet_out_delete(aux);
4677 opo->aux = NULL;
4678 }
4679
4680 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4681 {
4682 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4683 }
4684
4685 static struct ofgroup *
4686 group_alloc(void)
4687 {
4688 struct group_dpif *group = xzalloc(sizeof *group);
4689 return &group->up;
4690 }
4691
4692 static void
4693 group_dealloc(struct ofgroup *group_)
4694 {
4695 struct group_dpif *group = group_dpif_cast(group_);
4696 free(group);
4697 }
4698
4699 static void
4700 group_construct_stats(struct group_dpif *group)
4701 OVS_REQUIRES(group->stats_mutex)
4702 {
4703 group->packet_count = 0;
4704 group->byte_count = 0;
4705
4706 struct ofputil_bucket *bucket;
4707 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4708 bucket->stats.packet_count = 0;
4709 bucket->stats.byte_count = 0;
4710 }
4711 }
4712
4713 void
4714 group_dpif_credit_stats(struct group_dpif *group,
4715 struct ofputil_bucket *bucket,
4716 const struct dpif_flow_stats *stats)
4717 {
4718 ovs_mutex_lock(&group->stats_mutex);
4719 group->packet_count += stats->n_packets;
4720 group->byte_count += stats->n_bytes;
4721 if (bucket) {
4722 bucket->stats.packet_count += stats->n_packets;
4723 bucket->stats.byte_count += stats->n_bytes;
4724 } else { /* Credit to all buckets */
4725 struct ofputil_bucket *bucket;
4726 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4727 bucket->stats.packet_count += stats->n_packets;
4728 bucket->stats.byte_count += stats->n_bytes;
4729 }
4730 }
4731 ovs_mutex_unlock(&group->stats_mutex);
4732 }
4733
4734 static enum ofperr
4735 group_construct(struct ofgroup *group_)
4736 {
4737 struct group_dpif *group = group_dpif_cast(group_);
4738
4739 ovs_mutex_init_adaptive(&group->stats_mutex);
4740 ovs_mutex_lock(&group->stats_mutex);
4741 group_construct_stats(group);
4742 ovs_mutex_unlock(&group->stats_mutex);
4743 return 0;
4744 }
4745
4746 static void
4747 group_destruct(struct ofgroup *group_)
4748 {
4749 struct group_dpif *group = group_dpif_cast(group_);
4750 ovs_mutex_destroy(&group->stats_mutex);
4751 }
4752
4753 static enum ofperr
4754 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4755 {
4756 struct group_dpif *group = group_dpif_cast(group_);
4757
4758 ovs_mutex_lock(&group->stats_mutex);
4759 ogs->packet_count = group->packet_count;
4760 ogs->byte_count = group->byte_count;
4761
4762 struct bucket_counter *bucket_stats = ogs->bucket_stats;
4763 struct ofputil_bucket *bucket;
4764 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4765 bucket_stats->packet_count = bucket->stats.packet_count;
4766 bucket_stats->byte_count = bucket->stats.byte_count;
4767 bucket_stats++;
4768 }
4769 ovs_mutex_unlock(&group->stats_mutex);
4770
4771 return 0;
4772 }
4773
4774 /* If the group exists, this function increments the groups's reference count.
4775 *
4776 * Make sure to call ofproto_group_unref() after no longer needing to maintain
4777 * a reference to the group. */
4778 struct group_dpif *
4779 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4780 ovs_version_t version, bool take_ref)
4781 {
4782 struct ofgroup *ofgroup = ofproto_group_lookup(&ofproto->up, group_id,
4783 version, take_ref);
4784 return ofgroup ? group_dpif_cast(ofgroup) : NULL;
4785 }
4786 \f
4787 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4788 * supports a notion of an OAM flag, sets it if 'oam' is true.
4789 * May modify 'packet'.
4790 * Returns 0 if successful, otherwise a positive errno value. */
4791 int
4792 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, bool oam,
4793 struct dp_packet *packet)
4794 {
4795 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4796 int error;
4797
4798 error = xlate_send_packet(ofport, oam, packet);
4799
4800 ovs_mutex_lock(&ofproto->stats_mutex);
4801 ofproto->stats.tx_packets++;
4802 ofproto->stats.tx_bytes += dp_packet_size(packet);
4803 ovs_mutex_unlock(&ofproto->stats_mutex);
4804 return error;
4805 }
4806 \f
4807 /* Return the version string of the datapath that backs up
4808 * this 'ofproto'.
4809 */
4810 static const char *
4811 get_datapath_version(const struct ofproto *ofproto_)
4812 {
4813 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4814
4815 return ofproto->backer->dp_version_string;
4816 }
4817
4818 static void
4819 type_set_config(const char *type, const struct smap *other_config)
4820 {
4821 struct dpif_backer *backer;
4822
4823 backer = shash_find_data(&all_dpif_backers, type);
4824 if (!backer) {
4825 /* This is not necessarily a problem, since backers are only
4826 * created on demand. */
4827 return;
4828 }
4829
4830 dpif_set_config(backer->dpif, other_config);
4831 }
4832
4833 static void
4834 ct_flush(const struct ofproto *ofproto_, const uint16_t *zone)
4835 {
4836 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4837
4838 ct_dpif_flush(ofproto->backer->dpif, zone);
4839 }
4840
4841 static bool
4842 set_frag_handling(struct ofproto *ofproto_,
4843 enum ofputil_frag_handling frag_handling)
4844 {
4845 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4846 if (frag_handling != OFPUTIL_FRAG_REASM) {
4847 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4848 return true;
4849 } else {
4850 return false;
4851 }
4852 }
4853
4854 static enum ofperr
4855 nxt_resume(struct ofproto *ofproto_,
4856 const struct ofputil_packet_in_private *pin)
4857 {
4858 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4859
4860 /* Translate pin into datapath actions. */
4861 uint64_t odp_actions_stub[1024 / 8];
4862 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
4863 enum slow_path_reason slow;
4864 enum ofperr error = xlate_resume(ofproto, pin, &odp_actions, &slow);
4865
4866 /* Steal 'pin->packet' and put it into a dp_packet. */
4867 struct dp_packet packet;
4868 dp_packet_init(&packet, pin->public.packet_len);
4869 dp_packet_put(&packet, pin->public.packet, pin->public.packet_len);
4870
4871 pkt_metadata_from_flow(&packet.md, &pin->public.flow_metadata.flow);
4872
4873 /* Fix up in_port. */
4874 ofproto_dpif_set_packet_odp_port(ofproto,
4875 pin->public.flow_metadata.flow.in_port.ofp_port,
4876 &packet);
4877
4878 struct flow headers;
4879 flow_extract(&packet, &headers);
4880
4881 /* Execute the datapath actions on the packet. */
4882 struct dpif_execute execute = {
4883 .actions = odp_actions.data,
4884 .actions_len = odp_actions.size,
4885 .needs_help = (slow & SLOW_ACTION) != 0,
4886 .packet = &packet,
4887 .flow = &headers,
4888 };
4889 dpif_execute(ofproto->backer->dpif, &execute);
4890
4891 /* Clean up. */
4892 ofpbuf_uninit(&odp_actions);
4893 dp_packet_uninit(&packet);
4894
4895 return error;
4896 }
4897 \f
4898 /* NetFlow. */
4899
4900 static int
4901 set_netflow(struct ofproto *ofproto_,
4902 const struct netflow_options *netflow_options)
4903 {
4904 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4905
4906 if (netflow_options) {
4907 if (!ofproto->netflow) {
4908 ofproto->netflow = netflow_create();
4909 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4910 }
4911 return netflow_set_options(ofproto->netflow, netflow_options);
4912 } else if (ofproto->netflow) {
4913 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4914 netflow_unref(ofproto->netflow);
4915 ofproto->netflow = NULL;
4916 }
4917
4918 return 0;
4919 }
4920
4921 static void
4922 get_netflow_ids(const struct ofproto *ofproto_,
4923 uint8_t *engine_type, uint8_t *engine_id)
4924 {
4925 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4926
4927 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4928 }
4929 \f
4930 struct ofproto_dpif *
4931 ofproto_dpif_lookup(const char *name)
4932 {
4933 struct ofproto_dpif *ofproto;
4934
4935 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4936 hash_string(name, 0), &all_ofproto_dpifs) {
4937 if (!strcmp(ofproto->up.name, name)) {
4938 return ofproto;
4939 }
4940 }
4941 return NULL;
4942 }
4943
4944 static void
4945 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4946 const char *argv[], void *aux OVS_UNUSED)
4947 {
4948 struct ofproto_dpif *ofproto;
4949
4950 if (argc > 1) {
4951 ofproto = ofproto_dpif_lookup(argv[1]);
4952 if (!ofproto) {
4953 unixctl_command_reply_error(conn, "no such bridge");
4954 return;
4955 }
4956 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4957 mac_learning_flush(ofproto->ml);
4958 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4959 } else {
4960 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4961 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4962 mac_learning_flush(ofproto->ml);
4963 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4964 }
4965 }
4966
4967 unixctl_command_reply(conn, "table successfully flushed");
4968 }
4969
4970 static void
4971 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4972 const char *argv[], void *aux OVS_UNUSED)
4973 {
4974 struct ofproto_dpif *ofproto;
4975
4976 if (argc > 1) {
4977 ofproto = ofproto_dpif_lookup(argv[1]);
4978 if (!ofproto) {
4979 unixctl_command_reply_error(conn, "no such bridge");
4980 return;
4981 }
4982
4983 if (!mcast_snooping_enabled(ofproto->ms)) {
4984 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4985 return;
4986 }
4987 mcast_snooping_mdb_flush(ofproto->ms);
4988 } else {
4989 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4990 if (!mcast_snooping_enabled(ofproto->ms)) {
4991 continue;
4992 }
4993 mcast_snooping_mdb_flush(ofproto->ms);
4994 }
4995 }
4996
4997 unixctl_command_reply(conn, "table successfully flushed");
4998 }
4999
5000 static struct ofport_dpif *
5001 ofbundle_get_a_port(const struct ofbundle *bundle)
5002 {
5003 return CONTAINER_OF(ovs_list_front(&bundle->ports), struct ofport_dpif,
5004 bundle_node);
5005 }
5006
5007 static void
5008 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5009 const char *argv[], void *aux OVS_UNUSED)
5010 {
5011 struct ds ds = DS_EMPTY_INITIALIZER;
5012 const struct ofproto_dpif *ofproto;
5013 const struct mac_entry *e;
5014
5015 ofproto = ofproto_dpif_lookup(argv[1]);
5016 if (!ofproto) {
5017 unixctl_command_reply_error(conn, "no such bridge");
5018 return;
5019 }
5020
5021 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5022 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
5023 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5024 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
5025 char name[OFP10_MAX_PORT_NAME_LEN];
5026
5027 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5028 NULL, name, sizeof name);
5029 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
5030 name, e->vlan, ETH_ADDR_ARGS(e->mac),
5031 mac_entry_age(ofproto->ml, e));
5032 }
5033 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5034 unixctl_command_reply(conn, ds_cstr(&ds));
5035 ds_destroy(&ds);
5036 }
5037
5038 static void
5039 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
5040 int argc OVS_UNUSED,
5041 const char *argv[],
5042 void *aux OVS_UNUSED)
5043 {
5044 struct ds ds = DS_EMPTY_INITIALIZER;
5045 const struct ofproto_dpif *ofproto;
5046 const struct ofbundle *bundle;
5047 const struct mcast_group *grp;
5048 struct mcast_group_bundle *b;
5049 struct mcast_mrouter_bundle *mrouter;
5050
5051 ofproto = ofproto_dpif_lookup(argv[1]);
5052 if (!ofproto) {
5053 unixctl_command_reply_error(conn, "no such bridge");
5054 return;
5055 }
5056
5057 if (!mcast_snooping_enabled(ofproto->ms)) {
5058 unixctl_command_reply_error(conn, "multicast snooping is disabled");
5059 return;
5060 }
5061
5062 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
5063 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
5064 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
5065 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
5066 char name[OFP10_MAX_PORT_NAME_LEN];
5067
5068 bundle = b->port;
5069 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5070 NULL, name, sizeof name);
5071 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
5072 ipv6_format_mapped(&grp->addr, &ds);
5073 ds_put_format(&ds, " %3d\n",
5074 mcast_bundle_age(ofproto->ms, b));
5075 }
5076 }
5077
5078 /* ports connected to multicast routers */
5079 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
5080 char name[OFP10_MAX_PORT_NAME_LEN];
5081
5082 bundle = mrouter->port;
5083 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5084 NULL, name, sizeof name);
5085 ds_put_format(&ds, "%5s %4d querier %3d\n",
5086 name, mrouter->vlan,
5087 mcast_mrouter_age(ofproto->ms, mrouter));
5088 }
5089 ovs_rwlock_unlock(&ofproto->ms->rwlock);
5090 unixctl_command_reply(conn, ds_cstr(&ds));
5091 ds_destroy(&ds);
5092 }
5093
5094 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5095 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5096 * to destroy 'ofproto_shash' and free the returned value. */
5097 static const struct shash_node **
5098 get_ofprotos(struct shash *ofproto_shash)
5099 {
5100 const struct ofproto_dpif *ofproto;
5101
5102 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5103 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5104 shash_add_nocopy(ofproto_shash, name, ofproto);
5105 }
5106
5107 return shash_sort(ofproto_shash);
5108 }
5109
5110 static void
5111 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5112 const char *argv[] OVS_UNUSED,
5113 void *aux OVS_UNUSED)
5114 {
5115 struct ds ds = DS_EMPTY_INITIALIZER;
5116 struct shash ofproto_shash;
5117 const struct shash_node **sorted_ofprotos;
5118 int i;
5119
5120 shash_init(&ofproto_shash);
5121 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5122 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5123 const struct shash_node *node = sorted_ofprotos[i];
5124 ds_put_format(&ds, "%s\n", node->name);
5125 }
5126
5127 shash_destroy(&ofproto_shash);
5128 free(sorted_ofprotos);
5129
5130 unixctl_command_reply(conn, ds_cstr(&ds));
5131 ds_destroy(&ds);
5132 }
5133
5134 static void
5135 show_dp_feature_bool(struct ds *ds, const char *feature, bool b)
5136 {
5137 ds_put_format(ds, "%s: %s\n", feature, b ? "Yes" : "No");
5138 }
5139
5140 static void
5141 show_dp_feature_size_t(struct ds *ds, const char *feature, size_t s)
5142 {
5143 ds_put_format(ds, "%s: %"PRIuSIZE"\n", feature, s);
5144 }
5145
5146 static void
5147 dpif_show_support(const struct dpif_backer_support *support, struct ds *ds)
5148 {
5149 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5150 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5151 DPIF_SUPPORT_FIELDS
5152 #undef DPIF_SUPPORT_FIELD
5153
5154 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5155 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5156 ODP_SUPPORT_FIELDS
5157 #undef ODP_SUPPORT_FIELD
5158 }
5159
5160 static void
5161 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5162 {
5163 const struct shash_node **ofprotos;
5164 struct dpif_dp_stats dp_stats;
5165 struct shash ofproto_shash;
5166 size_t i;
5167
5168 dpif_get_dp_stats(backer->dpif, &dp_stats);
5169 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5170 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5171
5172 shash_init(&ofproto_shash);
5173 ofprotos = get_ofprotos(&ofproto_shash);
5174 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5175 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5176 const struct shash_node **ports;
5177 size_t j;
5178
5179 if (ofproto->backer != backer) {
5180 continue;
5181 }
5182
5183 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5184
5185 ports = shash_sort(&ofproto->up.port_by_name);
5186 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5187 const struct shash_node *node = ports[j];
5188 struct ofport *ofport = node->data;
5189 struct smap config;
5190 odp_port_t odp_port;
5191
5192 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5193 ofport->ofp_port);
5194
5195 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5196 if (odp_port != ODPP_NONE) {
5197 ds_put_format(ds, "%"PRIu32":", odp_port);
5198 } else {
5199 ds_put_cstr(ds, "none:");
5200 }
5201
5202 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5203
5204 smap_init(&config);
5205 if (!netdev_get_config(ofport->netdev, &config)) {
5206 const struct smap_node **nodes;
5207 size_t i;
5208
5209 nodes = smap_sort(&config);
5210 for (i = 0; i < smap_count(&config); i++) {
5211 const struct smap_node *node = nodes[i];
5212 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5213 node->key, node->value);
5214 }
5215 free(nodes);
5216 }
5217 smap_destroy(&config);
5218
5219 ds_put_char(ds, ')');
5220 ds_put_char(ds, '\n');
5221 }
5222 free(ports);
5223 }
5224 shash_destroy(&ofproto_shash);
5225 free(ofprotos);
5226 }
5227
5228 static void
5229 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5230 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5231 {
5232 struct ds ds = DS_EMPTY_INITIALIZER;
5233 const struct shash_node **backers;
5234 int i;
5235
5236 backers = shash_sort(&all_dpif_backers);
5237 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5238 dpif_show_backer(backers[i]->data, &ds);
5239 }
5240 free(backers);
5241
5242 unixctl_command_reply(conn, ds_cstr(&ds));
5243 ds_destroy(&ds);
5244 }
5245
5246 static void
5247 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5248 int argc OVS_UNUSED, const char *argv[],
5249 void *aux OVS_UNUSED)
5250 {
5251 const struct ofproto_dpif *ofproto;
5252
5253 struct ds ds = DS_EMPTY_INITIALIZER;
5254
5255 struct dpif_flow_dump *flow_dump;
5256 struct dpif_flow_dump_thread *flow_dump_thread;
5257 struct dpif_flow f;
5258 int error;
5259
5260 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5261 if (!ofproto) {
5262 unixctl_command_reply_error(conn, "no such bridge");
5263 return;
5264 }
5265
5266 bool verbosity = false;
5267 bool names = false;
5268 bool set_names = false;
5269 for (int i = 1; i < argc - 1; i++) {
5270 if (!strcmp(argv[i], "-m")) {
5271 verbosity = true;
5272 } else if (!strcmp(argv[i], "--names")) {
5273 names = true;
5274 set_names = true;
5275 } else if (!strcmp(argv[i], "--no-names")) {
5276 names = false;
5277 set_names = true;
5278 }
5279 }
5280 if (!set_names) {
5281 names = verbosity;
5282 }
5283
5284 struct hmap *portno_names = NULL;
5285 if (names) {
5286 portno_names = xmalloc(sizeof *portno_names);
5287 hmap_init(portno_names);
5288
5289 struct dpif_port dpif_port;
5290 struct dpif_port_dump port_dump;
5291 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5292 odp_portno_names_set(portno_names, dpif_port.port_no,
5293 dpif_port.name);
5294 }
5295 }
5296
5297 ds_init(&ds);
5298 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false, NULL);
5299 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5300 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5301 struct flow flow;
5302
5303 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5304 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5305 continue;
5306 }
5307
5308 if (verbosity) {
5309 odp_format_ufid(&f.ufid, &ds);
5310 ds_put_cstr(&ds, " ");
5311 }
5312 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5313 portno_names, &ds, verbosity);
5314 ds_put_cstr(&ds, ", ");
5315 dpif_flow_stats_format(&f.stats, &ds);
5316 ds_put_cstr(&ds, ", actions:");
5317 format_odp_actions(&ds, f.actions, f.actions_len, portno_names);
5318 ds_put_char(&ds, '\n');
5319 }
5320 dpif_flow_dump_thread_destroy(flow_dump_thread);
5321 error = dpif_flow_dump_destroy(flow_dump);
5322
5323 if (error) {
5324 ds_clear(&ds);
5325 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5326 unixctl_command_reply_error(conn, ds_cstr(&ds));
5327 } else {
5328 unixctl_command_reply(conn, ds_cstr(&ds));
5329 }
5330 if (portno_names) {
5331 odp_portno_names_destroy(portno_names);
5332 hmap_destroy(portno_names);
5333 free(portno_names);
5334 }
5335 ds_destroy(&ds);
5336 }
5337
5338 static void
5339 ofproto_revalidate_all_backers(void)
5340 {
5341 const struct shash_node **backers;
5342 int i;
5343
5344 backers = shash_sort(&all_dpif_backers);
5345 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5346 struct dpif_backer *backer = backers[i]->data;
5347 backer->need_revalidate = REV_RECONFIGURE;
5348 }
5349 free(backers);
5350 }
5351
5352 static void
5353 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5354 const char *argv[], void *aux OVS_UNUSED)
5355 {
5356 if (!strcasecmp(argv[1], "off")) {
5357 ofproto_use_tnl_push_pop = false;
5358 unixctl_command_reply(conn, "Tunnel push-pop off");
5359 ofproto_revalidate_all_backers();
5360 } else if (!strcasecmp(argv[1], "on")) {
5361 ofproto_use_tnl_push_pop = true;
5362 unixctl_command_reply(conn, "Tunnel push-pop on");
5363 ofproto_revalidate_all_backers();
5364 } else {
5365 unixctl_command_reply_error(conn, "Invalid argument");
5366 }
5367 }
5368
5369 static void
5370 disable_datapath_truncate(struct unixctl_conn *conn OVS_UNUSED,
5371 int argc OVS_UNUSED,
5372 const char *argv[] OVS_UNUSED,
5373 void *aux OVS_UNUSED)
5374 {
5375 const struct shash_node **backers;
5376 int i;
5377
5378 backers = shash_sort(&all_dpif_backers);
5379 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5380 struct dpif_backer *backer = backers[i]->data;
5381 backer->support.trunc = false;
5382 }
5383 free(backers);
5384 unixctl_command_reply(conn, "Datapath truncate action diabled");
5385 }
5386
5387 static void
5388 disable_datapath_clone(struct unixctl_conn *conn OVS_UNUSED,
5389 int argc, const char *argv[],
5390 void *aux OVS_UNUSED)
5391 {
5392 struct ds ds = DS_EMPTY_INITIALIZER;
5393 const char *br = argv[argc -1];
5394 struct ofproto_dpif *ofproto;
5395
5396 ofproto = ofproto_dpif_lookup(br);
5397 if (!ofproto) {
5398 unixctl_command_reply_error(conn, "no such bridge");
5399 return;
5400 }
5401 xlate_disable_dp_clone(ofproto);
5402 udpif_flush(ofproto->backer->udpif);
5403 ds_put_format(&ds, "Datapath clone action disabled for bridge %s", br);
5404 unixctl_command_reply(conn, ds_cstr(&ds));
5405 ds_destroy(&ds);
5406 }
5407
5408 static void
5409 ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn *conn,
5410 int argc, const char *argv[],
5411 void *aux OVS_UNUSED)
5412 {
5413 struct ds ds = DS_EMPTY_INITIALIZER;
5414 const char *br = argv[argc -1];
5415 struct ofproto_dpif *ofproto = ofproto_dpif_lookup(br);
5416
5417 if (!ofproto) {
5418 unixctl_command_reply_error(conn, "no such bridge");
5419 return;
5420 }
5421
5422 dpif_show_support(&ofproto->backer->support, &ds);
5423 unixctl_command_reply(conn, ds_cstr(&ds));
5424 }
5425
5426 static void
5427 ofproto_unixctl_init(void)
5428 {
5429 static bool registered;
5430 if (registered) {
5431 return;
5432 }
5433 registered = true;
5434
5435 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5436 ofproto_unixctl_fdb_flush, NULL);
5437 unixctl_command_register("fdb/show", "bridge", 1, 1,
5438 ofproto_unixctl_fdb_show, NULL);
5439 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5440 ofproto_unixctl_mcast_snooping_flush, NULL);
5441 unixctl_command_register("mdb/show", "bridge", 1, 1,
5442 ofproto_unixctl_mcast_snooping_show, NULL);
5443 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5444 ofproto_unixctl_dpif_dump_dps, NULL);
5445 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5446 NULL);
5447 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
5448 ofproto_unixctl_dpif_show_dp_features, NULL);
5449 unixctl_command_register("dpif/dump-flows", "[-m] [--names | --no-nmaes] bridge", 1, INT_MAX,
5450 ofproto_unixctl_dpif_dump_flows, NULL);
5451
5452 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5453 disable_tnl_push_pop, NULL);
5454
5455 unixctl_command_register("dpif/disable-truncate", "", 0, 0,
5456 disable_datapath_truncate, NULL);
5457
5458 unixctl_command_register("dpif/disable-dp-clone", "bridge", 1, 1,
5459 disable_datapath_clone, NULL);
5460 }
5461 \f
5462 static odp_port_t
5463 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5464 {
5465 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5466 return ofport ? ofport->odp_port : ODPP_NONE;
5467 }
5468
5469 struct ofport_dpif *
5470 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5471 {
5472 struct ofport_dpif *port;
5473
5474 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5475 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5476 &backer->odp_to_ofport_map) {
5477 if (port->odp_port == odp_port) {
5478 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5479 return port;
5480 }
5481 }
5482
5483 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5484 return NULL;
5485 }
5486
5487 static ofp_port_t
5488 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5489 {
5490 struct ofport_dpif *port;
5491
5492 port = odp_port_to_ofport(ofproto->backer, odp_port);
5493 if (port && &ofproto->up == port->up.ofproto) {
5494 return port->up.ofp_port;
5495 } else {
5496 return OFPP_NONE;
5497 }
5498 }
5499
5500 int
5501 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5502 const struct match *match, int priority,
5503 uint16_t idle_timeout,
5504 const struct ofpbuf *ofpacts,
5505 struct rule **rulep)
5506 {
5507 struct ofputil_flow_mod fm;
5508 struct rule_dpif *rule;
5509 int error;
5510
5511 fm = (struct ofputil_flow_mod) {
5512 .buffer_id = UINT32_MAX,
5513 .match = *match,
5514 .priority = priority,
5515 .table_id = TBL_INTERNAL,
5516 .command = OFPFC_ADD,
5517 .idle_timeout = idle_timeout,
5518 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5519 .ofpacts = ofpacts->data,
5520 .ofpacts_len = ofpacts->size,
5521 };
5522
5523 error = ofproto_flow_mod(&ofproto->up, &fm);
5524 if (error) {
5525 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5526 ofperr_to_string(error));
5527 *rulep = NULL;
5528 return error;
5529 }
5530
5531 rule = rule_dpif_lookup_in_table(ofproto,
5532 ofproto_dpif_get_tables_version(ofproto),
5533 TBL_INTERNAL, &fm.match.flow,
5534 &fm.match.wc);
5535 if (rule) {
5536 *rulep = &rule->up;
5537 } else {
5538 OVS_NOT_REACHED();
5539 }
5540 return 0;
5541 }
5542
5543 int
5544 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5545 struct match *match, int priority)
5546 {
5547 struct ofputil_flow_mod fm;
5548 int error;
5549
5550 fm = (struct ofputil_flow_mod) {
5551 .buffer_id = UINT32_MAX,
5552 .match = *match,
5553 .priority = priority,
5554 .table_id = TBL_INTERNAL,
5555 .out_port = OFPP_ANY,
5556 .out_group = OFPG_ANY,
5557 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5558 .command = OFPFC_DELETE_STRICT,
5559 };
5560
5561 error = ofproto_flow_mod(&ofproto->up, &fm);
5562 if (error) {
5563 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5564 ofperr_to_string(error));
5565 return error;
5566 }
5567
5568 return 0;
5569 }
5570
5571 static void
5572 meter_get_features(const struct ofproto *ofproto_,
5573 struct ofputil_meter_features *features)
5574 {
5575 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5576
5577 dpif_meter_get_features(ofproto->backer->dpif, features);
5578 }
5579
5580 static enum ofperr
5581 meter_set(struct ofproto *ofproto_, ofproto_meter_id *meter_id,
5582 struct ofputil_meter_config *config)
5583 {
5584 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5585
5586 /* Provider ID unknown. Use backer to allocate a new DP meter */
5587 if (meter_id->uint32 == UINT32_MAX) {
5588 if (!ofproto->backer->meter_ids) {
5589 return EFBIG; /* Datapath does not support meter. */
5590 }
5591
5592 if(!id_pool_alloc_id(ofproto->backer->meter_ids, &meter_id->uint32)) {
5593 return ENOMEM; /* Can't allocate a DP meter. */
5594 }
5595 }
5596
5597 switch (dpif_meter_set(ofproto->backer->dpif, meter_id, config)) {
5598 case 0:
5599 return 0;
5600 case EFBIG: /* meter_id out of range */
5601 case ENOMEM: /* Cannot allocate meter */
5602 return OFPERR_OFPMMFC_OUT_OF_METERS;
5603 case EBADF: /* Unsupported flags */
5604 return OFPERR_OFPMMFC_BAD_FLAGS;
5605 case EINVAL: /* Too many bands */
5606 return OFPERR_OFPMMFC_OUT_OF_BANDS;
5607 case ENODEV: /* Unsupported band type */
5608 return OFPERR_OFPMMFC_BAD_BAND;
5609 default:
5610 return OFPERR_OFPMMFC_UNKNOWN;
5611 }
5612 }
5613
5614 static enum ofperr
5615 meter_get(const struct ofproto *ofproto_, ofproto_meter_id meter_id,
5616 struct ofputil_meter_stats *stats, uint16_t n_bands)
5617 {
5618 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5619
5620 if (!dpif_meter_get(ofproto->backer->dpif, meter_id, stats, n_bands)) {
5621 return 0;
5622 }
5623 return OFPERR_OFPMMFC_UNKNOWN_METER;
5624 }
5625
5626 struct free_meter_id_args {
5627 struct ofproto_dpif *ofproto;
5628 ofproto_meter_id meter_id;
5629 };
5630
5631 static void
5632 free_meter_id(struct free_meter_id_args *args)
5633 {
5634 struct ofproto_dpif *ofproto = args->ofproto;
5635
5636 dpif_meter_del(ofproto->backer->dpif, args->meter_id, NULL, 0);
5637 id_pool_free_id(ofproto->backer->meter_ids, args->meter_id.uint32);
5638 free(args);
5639 }
5640
5641 static void
5642 meter_del(struct ofproto *ofproto_, ofproto_meter_id meter_id)
5643 {
5644 struct free_meter_id_args *arg = xmalloc(sizeof *arg);
5645
5646 /* Before a meter can be deleted, Openflow spec requires all rules
5647 * referring to the meter to be (automatically) removed before the
5648 * meter is deleted. However, since vswitchd is multi-threaded,
5649 * those rules and their actions remain accessible by other threads,
5650 * especially by the handler and revalidator threads.
5651 * Postpone meter deletion after RCU grace period, so that ongoing
5652 * upcall translation or flow revalidation can complete. */
5653 arg->ofproto = ofproto_dpif_cast(ofproto_);
5654 arg->meter_id = meter_id;
5655 ovsrcu_postpone(free_meter_id, arg);
5656 }
5657
5658 const struct ofproto_class ofproto_dpif_class = {
5659 init,
5660 enumerate_types,
5661 enumerate_names,
5662 del,
5663 port_open_type,
5664 type_run,
5665 type_wait,
5666 alloc,
5667 construct,
5668 destruct,
5669 dealloc,
5670 run,
5671 ofproto_dpif_wait,
5672 NULL, /* get_memory_usage. */
5673 type_get_memory_usage,
5674 flush,
5675 query_tables,
5676 set_tables_version,
5677 port_alloc,
5678 port_construct,
5679 port_destruct,
5680 port_dealloc,
5681 port_modified,
5682 port_reconfigured,
5683 port_query_by_name,
5684 port_add,
5685 port_del,
5686 port_set_config,
5687 port_get_stats,
5688 port_dump_start,
5689 port_dump_next,
5690 port_dump_done,
5691 port_poll,
5692 port_poll_wait,
5693 port_is_lacp_current,
5694 port_get_lacp_stats,
5695 NULL, /* rule_choose_table */
5696 rule_alloc,
5697 rule_construct,
5698 rule_insert,
5699 NULL, /* rule_delete */
5700 rule_destruct,
5701 rule_dealloc,
5702 rule_get_stats,
5703 packet_xlate,
5704 packet_xlate_revert,
5705 packet_execute,
5706 set_frag_handling,
5707 nxt_resume,
5708 set_netflow,
5709 get_netflow_ids,
5710 set_sflow,
5711 set_ipfix,
5712 get_ipfix_stats,
5713 set_cfm,
5714 cfm_status_changed,
5715 get_cfm_status,
5716 set_lldp,
5717 get_lldp_status,
5718 set_aa,
5719 aa_mapping_set,
5720 aa_mapping_unset,
5721 aa_vlan_get_queued,
5722 aa_vlan_get_queue_size,
5723 set_bfd,
5724 bfd_status_changed,
5725 get_bfd_status,
5726 set_stp,
5727 get_stp_status,
5728 set_stp_port,
5729 get_stp_port_status,
5730 get_stp_port_stats,
5731 set_rstp,
5732 get_rstp_status,
5733 set_rstp_port,
5734 get_rstp_port_status,
5735 set_queues,
5736 bundle_set,
5737 bundle_remove,
5738 mirror_set__,
5739 mirror_get_stats__,
5740 set_flood_vlans,
5741 is_mirror_output_bundle,
5742 forward_bpdu_changed,
5743 set_mac_table_config,
5744 set_mcast_snooping,
5745 set_mcast_snooping_port,
5746 meter_get_features,
5747 meter_set,
5748 meter_get,
5749 meter_del,
5750 group_alloc, /* group_alloc */
5751 group_construct, /* group_construct */
5752 group_destruct, /* group_destruct */
5753 group_dealloc, /* group_dealloc */
5754 NULL, /* group_modify */
5755 group_get_stats, /* group_get_stats */
5756 get_datapath_version, /* get_datapath_version */
5757 type_set_config,
5758 ct_flush, /* ct_flush */
5759 };