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