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