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