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