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