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