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