]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif.c
ofproto: Fix for ovs-vswitchd crash on flow-mod with unsupported action
[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 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3666 odp_port_t port_no = ODPP_NONE;
3667 int error;
3668
3669 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3670 if (error) {
3671 return error;
3672 }
3673 if (netdev_get_tunnel_config(netdev)) {
3674 simap_put(&ofproto->backer->tnl_backers,
3675 dp_port_name, odp_to_u32(port_no));
3676 }
3677 }
3678
3679 if (netdev_get_tunnel_config(netdev)) {
3680 sset_add(&ofproto->ghost_ports, devname);
3681 } else {
3682 sset_add(&ofproto->ports, devname);
3683 }
3684 return 0;
3685 }
3686
3687 static int
3688 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3689 {
3690 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3691 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3692 int error = 0;
3693
3694 if (!ofport) {
3695 return 0;
3696 }
3697
3698 sset_find_and_delete(&ofproto->ghost_ports,
3699 netdev_get_name(ofport->up.netdev));
3700 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3701 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3702 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port, false);
3703 if (!error) {
3704 /* The caller is going to close ofport->up.netdev. If this is a
3705 * bonded port, then the bond is using that netdev, so remove it
3706 * from the bond. The client will need to reconfigure everything
3707 * after deleting ports, so then the slave will get re-added. */
3708 bundle_remove(&ofport->up);
3709 }
3710 }
3711 return error;
3712 }
3713
3714 static int
3715 port_set_config(const struct ofport *ofport_, const struct smap *cfg)
3716 {
3717 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3718 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3719
3720 if (sset_contains(&ofproto->ghost_ports,
3721 netdev_get_name(ofport->up.netdev))) {
3722 return 0;
3723 }
3724
3725 return dpif_port_set_config(ofproto->backer->dpif, ofport->odp_port, cfg);
3726 }
3727
3728 static int
3729 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3730 {
3731 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3732 int error;
3733
3734 error = netdev_get_stats(ofport->up.netdev, stats);
3735
3736 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3737 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3738
3739 ovs_mutex_lock(&ofproto->stats_mutex);
3740 /* ofproto->stats.tx_packets represents packets that we created
3741 * internally and sent to some port (e.g. packets sent with
3742 * ofproto_dpif_send_packet()). Account for them as if they had
3743 * come from OFPP_LOCAL and got forwarded. */
3744
3745 if (stats->rx_packets != UINT64_MAX) {
3746 stats->rx_packets += ofproto->stats.tx_packets;
3747 }
3748
3749 if (stats->rx_bytes != UINT64_MAX) {
3750 stats->rx_bytes += ofproto->stats.tx_bytes;
3751 }
3752
3753 /* ofproto->stats.rx_packets represents packets that were received on
3754 * some port and we processed internally and dropped (e.g. STP).
3755 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3756
3757 if (stats->tx_packets != UINT64_MAX) {
3758 stats->tx_packets += ofproto->stats.rx_packets;
3759 }
3760
3761 if (stats->tx_bytes != UINT64_MAX) {
3762 stats->tx_bytes += ofproto->stats.rx_bytes;
3763 }
3764 ovs_mutex_unlock(&ofproto->stats_mutex);
3765 }
3766
3767 return error;
3768 }
3769
3770 static int
3771 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3772 {
3773 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3774 if (ofport->bundle && ofport->bundle->lacp) {
3775 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3776 return 0;
3777 }
3778 }
3779 return -1;
3780 }
3781
3782 struct port_dump_state {
3783 struct sset_position pos;
3784 bool ghost;
3785
3786 struct ofproto_port port;
3787 bool has_port;
3788 };
3789
3790 static int
3791 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3792 {
3793 *statep = xzalloc(sizeof(struct port_dump_state));
3794 return 0;
3795 }
3796
3797 static int
3798 port_dump_next(const struct ofproto *ofproto_, void *state_,
3799 struct ofproto_port *port)
3800 {
3801 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3802 struct port_dump_state *state = state_;
3803 const struct sset *sset;
3804 struct sset_node *node;
3805
3806 if (state->has_port) {
3807 ofproto_port_destroy(&state->port);
3808 state->has_port = false;
3809 }
3810 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3811 while ((node = sset_at_position(sset, &state->pos))) {
3812 int error;
3813
3814 error = port_query_by_name(ofproto_, node->name, &state->port);
3815 if (!error) {
3816 *port = state->port;
3817 state->has_port = true;
3818 return 0;
3819 } else if (error != ENODEV) {
3820 return error;
3821 }
3822 }
3823
3824 if (!state->ghost) {
3825 state->ghost = true;
3826 memset(&state->pos, 0, sizeof state->pos);
3827 return port_dump_next(ofproto_, state_, port);
3828 }
3829
3830 return EOF;
3831 }
3832
3833 static int
3834 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3835 {
3836 struct port_dump_state *state = state_;
3837
3838 if (state->has_port) {
3839 ofproto_port_destroy(&state->port);
3840 }
3841 free(state);
3842 return 0;
3843 }
3844
3845 static int
3846 port_poll(const struct ofproto *ofproto_, char **devnamep)
3847 {
3848 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3849
3850 if (ofproto->port_poll_errno) {
3851 int error = ofproto->port_poll_errno;
3852 ofproto->port_poll_errno = 0;
3853 return error;
3854 }
3855
3856 if (sset_is_empty(&ofproto->port_poll_set)) {
3857 return EAGAIN;
3858 }
3859
3860 *devnamep = sset_pop(&ofproto->port_poll_set);
3861 return 0;
3862 }
3863
3864 static void
3865 port_poll_wait(const struct ofproto *ofproto_)
3866 {
3867 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3868 dpif_port_poll_wait(ofproto->backer->dpif);
3869 }
3870
3871 static int
3872 port_is_lacp_current(const struct ofport *ofport_)
3873 {
3874 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3875 return (ofport->bundle && ofport->bundle->lacp
3876 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3877 : -1);
3878 }
3879 \f
3880 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3881 * then delete it entirely. */
3882 static void
3883 rule_expire(struct rule_dpif *rule, long long now)
3884 OVS_REQUIRES(ofproto_mutex)
3885 {
3886 uint16_t hard_timeout, idle_timeout;
3887 int reason = -1;
3888
3889 hard_timeout = rule->up.hard_timeout;
3890 idle_timeout = rule->up.idle_timeout;
3891
3892 /* Has 'rule' expired? */
3893 if (hard_timeout) {
3894 long long int modified;
3895
3896 ovs_mutex_lock(&rule->up.mutex);
3897 modified = rule->up.modified;
3898 ovs_mutex_unlock(&rule->up.mutex);
3899
3900 if (now > modified + hard_timeout * 1000) {
3901 reason = OFPRR_HARD_TIMEOUT;
3902 }
3903 }
3904
3905 if (reason < 0 && idle_timeout) {
3906 long long int used;
3907
3908 ovs_mutex_lock(&rule->stats_mutex);
3909 used = rule->stats.used;
3910 ovs_mutex_unlock(&rule->stats_mutex);
3911
3912 if (now > used + idle_timeout * 1000) {
3913 reason = OFPRR_IDLE_TIMEOUT;
3914 }
3915 }
3916
3917 if (reason >= 0) {
3918 COVERAGE_INC(ofproto_dpif_expired);
3919 ofproto_rule_expire(&rule->up, reason);
3920 }
3921 }
3922
3923 static void
3924 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif *ofproto,
3925 ofp_port_t in_port, struct dp_packet *packet)
3926 {
3927 if (in_port == OFPP_NONE) {
3928 in_port = OFPP_LOCAL;
3929 }
3930 packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3931 }
3932
3933 int
3934 ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
3935 ovs_version_t version, const struct flow *flow,
3936 struct rule_dpif *rule,
3937 const struct ofpact *ofpacts, size_t ofpacts_len,
3938 int depth, int resubmits,
3939 struct dp_packet *packet)
3940 {
3941 struct dpif_flow_stats stats;
3942 struct xlate_out xout;
3943 struct xlate_in xin;
3944 struct dpif_execute execute;
3945 int error;
3946
3947 ovs_assert((rule != NULL) != (ofpacts != NULL));
3948
3949 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3950
3951 if (rule) {
3952 rule_dpif_credit_stats(rule, &stats);
3953 }
3954
3955 uint64_t odp_actions_stub[1024 / 8];
3956 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
3957 xlate_in_init(&xin, ofproto, version, flow, flow->in_port.ofp_port, rule,
3958 stats.tcp_flags, packet, NULL, &odp_actions);
3959 xin.ofpacts = ofpacts;
3960 xin.ofpacts_len = ofpacts_len;
3961 xin.resubmit_stats = &stats;
3962 xin.depth = depth;
3963 xin.resubmits = resubmits;
3964 if (xlate_actions(&xin, &xout) != XLATE_OK) {
3965 error = EINVAL;
3966 goto out;
3967 }
3968
3969 execute.actions = odp_actions.data;
3970 execute.actions_len = odp_actions.size;
3971
3972 pkt_metadata_from_flow(&packet->md, flow);
3973 execute.packet = packet;
3974 execute.flow = flow;
3975 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3976 execute.probe = false;
3977 execute.mtu = 0;
3978
3979 /* Fix up in_port. */
3980 ofproto_dpif_set_packet_odp_port(ofproto, flow->in_port.ofp_port, packet);
3981
3982 error = dpif_execute(ofproto->backer->dpif, &execute);
3983 out:
3984 xlate_out_uninit(&xout);
3985 ofpbuf_uninit(&odp_actions);
3986
3987 return error;
3988 }
3989
3990 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3991 * 'flow' must reflect the data in 'packet'. */
3992 int
3993 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3994 ovs_version_t version, const struct flow *flow,
3995 struct rule_dpif *rule,
3996 const struct ofpact *ofpacts, size_t ofpacts_len,
3997 struct dp_packet *packet)
3998 {
3999 return ofproto_dpif_execute_actions__(ofproto, version, flow, rule,
4000 ofpacts, ofpacts_len, 0, 0, packet);
4001 }
4002
4003 static void
4004 rule_dpif_credit_stats__(struct rule_dpif *rule,
4005 const struct dpif_flow_stats *stats,
4006 bool credit_counts)
4007 OVS_REQUIRES(rule->stats_mutex)
4008 {
4009 if (credit_counts) {
4010 rule->stats.n_packets += stats->n_packets;
4011 rule->stats.n_bytes += stats->n_bytes;
4012 }
4013 rule->stats.used = MAX(rule->stats.used, stats->used);
4014 }
4015
4016 void
4017 rule_dpif_credit_stats(struct rule_dpif *rule,
4018 const struct dpif_flow_stats *stats)
4019 {
4020 ovs_mutex_lock(&rule->stats_mutex);
4021 if (OVS_UNLIKELY(rule->new_rule)) {
4022 ovs_mutex_lock(&rule->new_rule->stats_mutex);
4023 rule_dpif_credit_stats__(rule->new_rule, stats, rule->forward_counts);
4024 ovs_mutex_unlock(&rule->new_rule->stats_mutex);
4025 } else {
4026 rule_dpif_credit_stats__(rule, stats, true);
4027 }
4028 ovs_mutex_unlock(&rule->stats_mutex);
4029 }
4030
4031 /* Sets 'rule''s recirculation id. */
4032 static void
4033 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
4034 OVS_REQUIRES(rule->up.mutex)
4035 {
4036 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
4037 if (rule->recirc_id == id) {
4038 /* Release the new reference to the same id. */
4039 recirc_free_id(id);
4040 } else {
4041 rule->recirc_id = id;
4042 }
4043 }
4044
4045 /* Sets 'rule''s recirculation id. */
4046 void
4047 rule_set_recirc_id(struct rule *rule_, uint32_t id)
4048 {
4049 struct rule_dpif *rule = rule_dpif_cast(rule_);
4050
4051 ovs_mutex_lock(&rule->up.mutex);
4052 rule_dpif_set_recirc_id(rule, id);
4053 ovs_mutex_unlock(&rule->up.mutex);
4054 }
4055
4056 ovs_version_t
4057 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto)
4058 {
4059 ovs_version_t version;
4060
4061 /* Use memory_order_acquire to signify that any following memory accesses
4062 * can not be reordered to happen before this atomic read. This makes sure
4063 * all following reads relate to this or a newer version, but never to an
4064 * older version. */
4065 atomic_read_explicit(&ofproto->tables_version, &version,
4066 memory_order_acquire);
4067 return version;
4068 }
4069
4070 /* The returned rule (if any) is valid at least until the next RCU quiescent
4071 * period. If the rule needs to stay around longer, the caller should take
4072 * a reference.
4073 *
4074 * 'flow' is non-const to allow for temporary modifications during the lookup.
4075 * Any changes are restored before returning. */
4076 static struct rule_dpif *
4077 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, ovs_version_t version,
4078 uint8_t table_id, struct flow *flow,
4079 struct flow_wildcards *wc)
4080 {
4081 struct classifier *cls = &ofproto->up.tables[table_id].cls;
4082 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
4083 flow, wc)));
4084 }
4085
4086 void
4087 ofproto_dpif_credit_table_stats(struct ofproto_dpif *ofproto, uint8_t table_id,
4088 uint64_t n_matches, uint64_t n_misses)
4089 {
4090 struct oftable *tbl = &ofproto->up.tables[table_id];
4091 unsigned long orig;
4092
4093 if (n_matches) {
4094 atomic_add_relaxed(&tbl->n_matched, n_matches, &orig);
4095 }
4096 if (n_misses) {
4097 atomic_add_relaxed(&tbl->n_missed, n_misses, &orig);
4098 }
4099 }
4100
4101 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
4102 * table '*table_id'. Returns the rule that was found, which may be one of the
4103 * special rules according to packet miss hadling. If 'may_packet_in' is
4104 * false, returning of the miss_rule (which issues packet ins for the
4105 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
4106 * that were used during the lookup.
4107 *
4108 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
4109 * if none is found then the table miss configuration for that table is
4110 * honored, which can result in additional lookups in other OpenFlow tables.
4111 * In this case the function updates '*table_id' to reflect the final OpenFlow
4112 * table that was searched.
4113 *
4114 * If 'honor_table_miss' is false, then only one table lookup occurs, in
4115 * '*table_id'.
4116 *
4117 * The rule is returned in '*rule', which is valid at least until the next
4118 * RCU quiescent period. If the '*rule' needs to stay around longer, the
4119 * caller must take a reference.
4120 *
4121 * 'in_port' allows the lookup to take place as if the in port had the value
4122 * 'in_port'. This is needed for resubmit action support.
4123 *
4124 * 'flow' is non-const to allow for temporary modifications during the lookup.
4125 * Any changes are restored before returning. */
4126 struct rule_dpif *
4127 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
4128 ovs_version_t version, struct flow *flow,
4129 struct flow_wildcards *wc,
4130 const struct dpif_flow_stats *stats,
4131 uint8_t *table_id, ofp_port_t in_port,
4132 bool may_packet_in, bool honor_table_miss,
4133 struct xlate_cache *xcache)
4134 {
4135 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
4136 ofp_port_t old_in_port = flow->in_port.ofp_port;
4137 enum ofputil_table_miss miss_config;
4138 struct rule_dpif *rule;
4139 uint8_t next_id;
4140
4141 /* We always unwildcard nw_frag (for IP), so they
4142 * need not be unwildcarded here. */
4143 if (flow->nw_frag & FLOW_NW_FRAG_ANY
4144 && ofproto->up.frag_handling != OFPUTIL_FRAG_NX_MATCH) {
4145 if (ofproto->up.frag_handling == OFPUTIL_FRAG_NORMAL) {
4146 /* We must pretend that transport ports are unavailable. */
4147 flow->tp_src = htons(0);
4148 flow->tp_dst = htons(0);
4149 } else {
4150 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
4151 * Use the drop_frags_rule (which cannot disappear). */
4152 rule = ofproto->drop_frags_rule;
4153 if (stats) {
4154 struct oftable *tbl = &ofproto->up.tables[*table_id];
4155 unsigned long orig;
4156
4157 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
4158 }
4159 if (xcache) {
4160 struct xc_entry *entry;
4161
4162 entry = xlate_cache_add_entry(xcache, XC_TABLE);
4163 entry->table.ofproto = ofproto;
4164 entry->table.id = *table_id;
4165 entry->table.match = true;
4166 }
4167 return rule;
4168 }
4169 }
4170
4171 /* Look up a flow with 'in_port' as the input port. Then restore the
4172 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
4173 * have surprising behavior). */
4174 flow->in_port.ofp_port = in_port;
4175
4176 /* Our current implementation depends on n_tables == N_TABLES, and
4177 * TBL_INTERNAL being the last table. */
4178 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
4179
4180 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
4181
4182 for (next_id = *table_id;
4183 next_id < ofproto->up.n_tables;
4184 next_id++, next_id += (next_id == TBL_INTERNAL))
4185 {
4186 *table_id = next_id;
4187 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
4188 if (stats) {
4189 struct oftable *tbl = &ofproto->up.tables[next_id];
4190 unsigned long orig;
4191
4192 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
4193 stats->n_packets, &orig);
4194 }
4195 if (xcache) {
4196 struct xc_entry *entry;
4197
4198 entry = xlate_cache_add_entry(xcache, XC_TABLE);
4199 entry->table.ofproto = ofproto;
4200 entry->table.id = next_id;
4201 entry->table.match = (rule != NULL);
4202 }
4203 if (rule) {
4204 goto out; /* Match. */
4205 }
4206 if (honor_table_miss) {
4207 miss_config = ofproto_table_get_miss_config(&ofproto->up,
4208 *table_id);
4209 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
4210 continue;
4211 }
4212 }
4213 break;
4214 }
4215 /* Miss. */
4216 rule = ofproto->no_packet_in_rule;
4217 if (may_packet_in) {
4218 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
4219 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
4220 struct ofport_dpif *port;
4221
4222 port = ofp_port_to_ofport(ofproto, old_in_port);
4223 if (!port) {
4224 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu32,
4225 old_in_port);
4226 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
4227 rule = ofproto->miss_rule;
4228 }
4229 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
4230 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
4231 rule = ofproto->miss_rule;
4232 }
4233 }
4234 out:
4235 /* Restore port numbers, as they may have been modified above. */
4236 flow->tp_src = old_tp_src;
4237 flow->tp_dst = old_tp_dst;
4238 /* Restore the old in port. */
4239 flow->in_port.ofp_port = old_in_port;
4240
4241 return rule;
4242 }
4243
4244 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4245 {
4246 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4247 }
4248
4249 static struct rule *
4250 rule_alloc(void)
4251 {
4252 struct rule_dpif *rule = xzalloc(sizeof *rule);
4253 return &rule->up;
4254 }
4255
4256 static void
4257 rule_dealloc(struct rule *rule_)
4258 {
4259 struct rule_dpif *rule = rule_dpif_cast(rule_);
4260 free(rule);
4261 }
4262
4263 static enum ofperr
4264 check_mask(struct ofproto_dpif *ofproto, const struct miniflow *flow)
4265 {
4266 const struct odp_support *support;
4267 uint16_t ct_state, ct_zone;
4268 ovs_u128 ct_label;
4269 uint32_t ct_mark;
4270
4271 support = &ofproto->backer->rt_support.odp;
4272 ct_state = MINIFLOW_GET_U8(flow, ct_state);
4273
4274 if (ct_state & CS_UNSUPPORTED_MASK) {
4275 return OFPERR_OFPBMC_BAD_MASK;
4276 }
4277
4278 /* Do not bother dissecting the flow further if the datapath supports all
4279 * the features we know of. */
4280 if (support->ct_state && support->ct_zone && support->ct_mark
4281 && support->ct_label && support->ct_state_nat
4282 && support->ct_orig_tuple && support->ct_orig_tuple6) {
4283 return 0;
4284 }
4285
4286 ct_zone = MINIFLOW_GET_U16(flow, ct_zone);
4287 ct_mark = MINIFLOW_GET_U32(flow, ct_mark);
4288 ct_label = MINIFLOW_GET_U128(flow, ct_label);
4289
4290 if ((ct_state && !support->ct_state)
4291 || ((ct_state & (CS_SRC_NAT | CS_DST_NAT)) && !support->ct_state_nat)
4292 || (ct_zone && !support->ct_zone)
4293 || (ct_mark && !support->ct_mark)
4294 || (!ovs_u128_is_zero(ct_label) && !support->ct_label)) {
4295 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT;
4296 }
4297
4298 if (!support->ct_orig_tuple && !support->ct_orig_tuple6
4299 && (MINIFLOW_GET_U8(flow, ct_nw_proto)
4300 || MINIFLOW_GET_U16(flow, ct_tp_src)
4301 || MINIFLOW_GET_U16(flow, ct_tp_dst))) {
4302 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT;
4303 }
4304
4305 if (!support->ct_orig_tuple
4306 && (MINIFLOW_GET_U32(flow, ct_nw_src)
4307 || MINIFLOW_GET_U32(flow, ct_nw_dst))) {
4308 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT;
4309 }
4310
4311 if (!support->ct_orig_tuple6
4312 && (!ovs_u128_is_zero(MINIFLOW_GET_U128(flow, ct_ipv6_src))
4313 || !ovs_u128_is_zero(MINIFLOW_GET_U128(flow, ct_ipv6_dst)))) {
4314 return OFPERR_NXBMC_CT_DATAPATH_SUPPORT;
4315 }
4316
4317 return 0;
4318 }
4319
4320 static void
4321 report_unsupported_act(const char *action, const char *detail)
4322 {
4323 static struct vlog_rate_limit rll = VLOG_RATE_LIMIT_INIT(1, 5);
4324 VLOG_WARN_RL(&rll, "Rejecting %s action because datapath does not support"
4325 "%s%s (your kernel module may be out of date)",
4326 action, detail ? " " : "", detail ? detail : "");
4327 }
4328
4329 static enum ofperr
4330 check_actions(const struct ofproto_dpif *ofproto,
4331 const struct rule_actions *const actions)
4332 {
4333 const struct ofpact *ofpact;
4334 const struct odp_support *support = &ofproto->backer->rt_support.odp;
4335
4336 OFPACT_FOR_EACH (ofpact, actions->ofpacts, actions->ofpacts_len) {
4337 if (ofpact->type == OFPACT_CT) {
4338 const struct ofpact_conntrack *ct;
4339 const struct ofpact *a;
4340
4341 ct = CONTAINER_OF(ofpact, struct ofpact_conntrack, ofpact);
4342
4343 if (!support->ct_state) {
4344 report_unsupported_act("ct", "ct action");
4345 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT;
4346 }
4347 if ((ct->zone_imm || ct->zone_src.field) && !support->ct_zone) {
4348 report_unsupported_act("ct", "ct zones");
4349 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT;
4350 }
4351 /* So far the force commit feature is implemented together with the
4352 * original direction tuple feature by all datapaths, so we use the
4353 * support flag for the 'ct_orig_tuple' to indicate support for the
4354 * force commit feature as well. */
4355 if ((ct->flags & NX_CT_F_FORCE) && !support->ct_orig_tuple) {
4356 report_unsupported_act("ct", "force commit");
4357 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT;
4358 }
4359
4360 OFPACT_FOR_EACH(a, ct->actions, ofpact_ct_get_action_len(ct)) {
4361 const struct mf_field *dst = ofpact_get_mf_dst(a);
4362
4363 if (a->type == OFPACT_NAT && !support->ct_state_nat) {
4364 /* The backer doesn't seem to support the NAT bits in
4365 * 'ct_state': assume that it doesn't support the NAT
4366 * action. */
4367 report_unsupported_act("ct", "nat");
4368 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT;
4369 }
4370 if (dst && ((dst->id == MFF_CT_MARK && !support->ct_mark) ||
4371 (dst->id == MFF_CT_LABEL && !support->ct_label))) {
4372 report_unsupported_act("ct", "setting mark and/or label");
4373 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT;
4374 }
4375 }
4376 } else if (ofpact->type == OFPACT_RESUBMIT) {
4377 struct ofpact_resubmit *resubmit = ofpact_get_RESUBMIT(ofpact);
4378
4379 if (resubmit->with_ct_orig && !support->ct_orig_tuple) {
4380 report_unsupported_act("resubmit",
4381 "ct original direction tuple");
4382 return OFPERR_NXBAC_CT_DATAPATH_SUPPORT;
4383 }
4384 }
4385 }
4386
4387 return 0;
4388 }
4389
4390 static enum ofperr
4391 rule_check(struct rule *rule)
4392 {
4393 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4394 enum ofperr err;
4395
4396 err = check_mask(ofproto, &rule->cr.match.mask->masks);
4397 if (err) {
4398 return err;
4399 }
4400 return check_actions(ofproto, rule->actions);
4401 }
4402
4403 static enum ofperr
4404 rule_construct(struct rule *rule_)
4405 OVS_NO_THREAD_SAFETY_ANALYSIS
4406 {
4407 struct rule_dpif *rule = rule_dpif_cast(rule_);
4408 int error;
4409
4410 error = rule_check(rule_);
4411 if (error) {
4412 return error;
4413 }
4414
4415 ovs_mutex_init_adaptive(&rule->stats_mutex);
4416 rule->stats.n_packets = 0;
4417 rule->stats.n_bytes = 0;
4418 rule->stats.used = rule->up.modified;
4419 rule->recirc_id = 0;
4420 rule->new_rule = NULL;
4421 rule->forward_counts = false;
4422
4423 return 0;
4424 }
4425
4426 static void
4427 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_counts)
4428 OVS_REQUIRES(ofproto_mutex)
4429 {
4430 struct rule_dpif *rule = rule_dpif_cast(rule_);
4431
4432 if (old_rule_) {
4433 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4434
4435 ovs_assert(!old_rule->new_rule);
4436
4437 /* Take a reference to the new rule, and refer all stats updates from
4438 * the old rule to the new rule. */
4439 ofproto_rule_ref(&rule->up);
4440
4441 ovs_mutex_lock(&old_rule->stats_mutex);
4442 ovs_mutex_lock(&rule->stats_mutex);
4443 old_rule->new_rule = rule; /* Forward future stats. */
4444 old_rule->forward_counts = forward_counts;
4445
4446 if (forward_counts) {
4447 rule->stats = old_rule->stats; /* Transfer stats to the new
4448 * rule. */
4449 } else {
4450 /* Used timestamp must be forwarded whenever a rule is modified. */
4451 rule->stats.used = old_rule->stats.used;
4452 }
4453 ovs_mutex_unlock(&rule->stats_mutex);
4454 ovs_mutex_unlock(&old_rule->stats_mutex);
4455 }
4456 }
4457
4458 static void
4459 rule_destruct(struct rule *rule_)
4460 OVS_NO_THREAD_SAFETY_ANALYSIS
4461 {
4462 struct rule_dpif *rule = rule_dpif_cast(rule_);
4463
4464 ovs_mutex_destroy(&rule->stats_mutex);
4465 /* Release reference to the new rule, if any. */
4466 if (rule->new_rule) {
4467 ofproto_rule_unref(&rule->new_rule->up);
4468 }
4469 if (rule->recirc_id) {
4470 recirc_free_id(rule->recirc_id);
4471 }
4472 }
4473
4474 static void
4475 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4476 long long int *used)
4477 {
4478 struct rule_dpif *rule = rule_dpif_cast(rule_);
4479
4480 ovs_mutex_lock(&rule->stats_mutex);
4481 if (OVS_UNLIKELY(rule->new_rule)) {
4482 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4483 } else {
4484 *packets = rule->stats.n_packets;
4485 *bytes = rule->stats.n_bytes;
4486 *used = rule->stats.used;
4487 }
4488 ovs_mutex_unlock(&rule->stats_mutex);
4489 }
4490
4491 struct ofproto_dpif_packet_out {
4492 struct xlate_cache xcache;
4493 struct ofpbuf odp_actions;
4494 struct recirc_refs rr;
4495 bool needs_help;
4496 };
4497
4498
4499 static struct ofproto_dpif_packet_out *
4500 ofproto_dpif_packet_out_new(void)
4501 {
4502 struct ofproto_dpif_packet_out *aux = xmalloc(sizeof *aux);
4503 xlate_cache_init(&aux->xcache);
4504 ofpbuf_init(&aux->odp_actions, 64);
4505 aux->rr = RECIRC_REFS_EMPTY_INITIALIZER;
4506 aux->needs_help = false;
4507
4508 return aux;
4509 }
4510
4511 static void
4512 ofproto_dpif_packet_out_delete(struct ofproto_dpif_packet_out *aux)
4513 {
4514 if (aux) {
4515 xlate_cache_uninit(&aux->xcache);
4516 ofpbuf_uninit(&aux->odp_actions);
4517 recirc_refs_unref(&aux->rr);
4518 free(aux);
4519 }
4520 }
4521
4522 static enum ofperr
4523 packet_xlate(struct ofproto *ofproto_, struct ofproto_packet_out *opo)
4524 OVS_REQUIRES(ofproto_mutex)
4525 {
4526 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4527 struct xlate_out xout;
4528 struct xlate_in xin;
4529 enum ofperr error = 0;
4530
4531 struct ofproto_dpif_packet_out *aux = ofproto_dpif_packet_out_new();
4532
4533 xlate_in_init(&xin, ofproto, opo->version, opo->flow,
4534 opo->flow->in_port.ofp_port, NULL, 0, opo->packet, NULL,
4535 &aux->odp_actions);
4536 xin.ofpacts = opo->ofpacts;
4537 xin.ofpacts_len = opo->ofpacts_len;
4538 /* No learning or stats, but collect side effects to xcache. */
4539 xin.allow_side_effects = false;
4540 xin.resubmit_stats = NULL;
4541 xin.xcache = &aux->xcache;
4542 xin.in_packet_out = true;
4543
4544 if (xlate_actions(&xin, &xout) != XLATE_OK) {
4545 error = OFPERR_OFPFMFC_UNKNOWN; /* Error processing actions. */
4546 goto error_out;
4547 } else {
4548 /* Prepare learn actions. */
4549 struct xc_entry *entry;
4550 struct ofpbuf entries = aux->xcache.entries;
4551
4552 XC_ENTRY_FOR_EACH (entry, &entries) {
4553 if (entry->type == XC_LEARN) {
4554 struct ofproto_flow_mod *ofm = entry->learn.ofm;
4555
4556 error = ofproto_flow_mod_learn_refresh(ofm);
4557 if (error) {
4558 goto error_out;
4559 }
4560 struct rule *rule = ofm->temp_rule;
4561 ofm->learn_adds_rule = (rule->state == RULE_INITIALIZED);
4562 if (ofm->learn_adds_rule) {
4563 /* If learning on a different bridge, must use its next
4564 * version number. */
4565 ofm->version = (rule->ofproto == ofproto_)
4566 ? opo->version : rule->ofproto->tables_version + 1;
4567 error = ofproto_flow_mod_learn_start(ofm);
4568 if (error) {
4569 goto error_out;
4570 }
4571 }
4572 }
4573 }
4574
4575 /* Success. */
4576 aux->needs_help = (xout.slow & SLOW_ACTION) != 0;
4577 recirc_refs_swap(&aux->rr, &xout.recircs); /* Hold recirc refs. */
4578 }
4579 xlate_out_uninit(&xout);
4580 opo->aux = aux;
4581 return 0;
4582
4583 error_out:
4584 xlate_out_uninit(&xout);
4585 ofproto_dpif_packet_out_delete(aux);
4586 opo->aux = NULL;
4587 return error;
4588 }
4589
4590 static void
4591 packet_xlate_revert(struct ofproto *ofproto OVS_UNUSED,
4592 struct ofproto_packet_out *opo)
4593 OVS_REQUIRES(ofproto_mutex)
4594 {
4595 struct ofproto_dpif_packet_out *aux = opo->aux;
4596 ovs_assert(aux);
4597
4598 /* Revert the learned flows. */
4599 struct xc_entry *entry;
4600 struct ofpbuf entries = aux->xcache.entries;
4601
4602 XC_ENTRY_FOR_EACH (entry, &entries) {
4603 if (entry->type == XC_LEARN && entry->learn.ofm->learn_adds_rule) {
4604 ofproto_flow_mod_learn_revert(entry->learn.ofm);
4605 }
4606 }
4607
4608 ofproto_dpif_packet_out_delete(aux);
4609 opo->aux = NULL;
4610 }
4611
4612 /* Push stats and perform side effects of flow translation. */
4613 static void
4614 ofproto_dpif_xcache_execute(struct ofproto_dpif *ofproto,
4615 struct xlate_cache *xcache,
4616 struct dpif_flow_stats *stats)
4617 OVS_REQUIRES(ofproto_mutex)
4618 {
4619 struct xc_entry *entry;
4620 struct ofpbuf entries = xcache->entries;
4621
4622 XC_ENTRY_FOR_EACH (entry, &entries) {
4623 switch (entry->type) {
4624 case XC_LEARN:
4625 /* Finish the learned flows. */
4626 if (entry->learn.ofm->learn_adds_rule) {
4627 ofproto_flow_mod_learn_finish(entry->learn.ofm, &ofproto->up);
4628 }
4629 break;
4630 case XC_FIN_TIMEOUT:
4631 if (stats->tcp_flags & (TCP_FIN | TCP_RST)) {
4632 /* 'ofproto_mutex' already held */
4633 ofproto_rule_reduce_timeouts__(&entry->fin.rule->up,
4634 entry->fin.idle,
4635 entry->fin.hard);
4636 }
4637 break;
4638 /* All the rest can be dealt with by the xlate layer. */
4639 case XC_TABLE:
4640 case XC_RULE:
4641 case XC_BOND:
4642 case XC_NETDEV:
4643 case XC_NETFLOW:
4644 case XC_MIRROR:
4645 case XC_NORMAL:
4646 case XC_GROUP:
4647 case XC_TNL_NEIGH:
4648 case XC_TUNNEL_HEADER:
4649 xlate_push_stats_entry(entry, stats);
4650 break;
4651 default:
4652 OVS_NOT_REACHED();
4653 }
4654 }
4655 }
4656
4657 static void
4658 packet_execute(struct ofproto *ofproto_, struct ofproto_packet_out *opo)
4659 OVS_REQUIRES(ofproto_mutex)
4660 {
4661 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4662 struct dpif_flow_stats stats;
4663 struct dpif_execute execute;
4664
4665 struct ofproto_dpif_packet_out *aux = opo->aux;
4666 ovs_assert(aux);
4667
4668 /* Run the side effects from the xcache. */
4669 dpif_flow_stats_extract(opo->flow, opo->packet, time_msec(), &stats);
4670 ofproto_dpif_xcache_execute(ofproto, &aux->xcache, &stats);
4671
4672 execute.actions = aux->odp_actions.data;
4673 execute.actions_len = aux->odp_actions.size;
4674
4675 pkt_metadata_from_flow(&opo->packet->md, opo->flow);
4676 execute.packet = opo->packet;
4677 execute.flow = opo->flow;
4678 execute.needs_help = aux->needs_help;
4679 execute.probe = false;
4680 execute.mtu = 0;
4681
4682 /* Fix up in_port. */
4683 ofproto_dpif_set_packet_odp_port(ofproto, opo->flow->in_port.ofp_port,
4684 opo->packet);
4685
4686 dpif_execute(ofproto->backer->dpif, &execute);
4687 ofproto_dpif_packet_out_delete(aux);
4688 opo->aux = NULL;
4689 }
4690
4691 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4692 {
4693 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4694 }
4695
4696 static struct ofgroup *
4697 group_alloc(void)
4698 {
4699 struct group_dpif *group = xzalloc(sizeof *group);
4700 return &group->up;
4701 }
4702
4703 static void
4704 group_dealloc(struct ofgroup *group_)
4705 {
4706 struct group_dpif *group = group_dpif_cast(group_);
4707 free(group);
4708 }
4709
4710 static void
4711 group_construct_stats(struct group_dpif *group)
4712 OVS_REQUIRES(group->stats_mutex)
4713 {
4714 group->packet_count = 0;
4715 group->byte_count = 0;
4716
4717 struct ofputil_bucket *bucket;
4718 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4719 bucket->stats.packet_count = 0;
4720 bucket->stats.byte_count = 0;
4721 }
4722 }
4723
4724 void
4725 group_dpif_credit_stats(struct group_dpif *group,
4726 struct ofputil_bucket *bucket,
4727 const struct dpif_flow_stats *stats)
4728 {
4729 ovs_mutex_lock(&group->stats_mutex);
4730 group->packet_count += stats->n_packets;
4731 group->byte_count += stats->n_bytes;
4732 if (bucket) {
4733 bucket->stats.packet_count += stats->n_packets;
4734 bucket->stats.byte_count += stats->n_bytes;
4735 } else { /* Credit to all buckets */
4736 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4737 bucket->stats.packet_count += stats->n_packets;
4738 bucket->stats.byte_count += stats->n_bytes;
4739 }
4740 }
4741 ovs_mutex_unlock(&group->stats_mutex);
4742 }
4743
4744 /* Calculate the dp_hash mask needed to provide the least weighted bucket
4745 * with at least one hash value and construct a mapping table from masked
4746 * dp_hash value to group bucket using the Webster method.
4747 * If the caller specifies a non-zero max_hash value, abort and return false
4748 * if more hash values would be required. The absolute maximum number of
4749 * hash values supported is 256. */
4750
4751 #define MAX_SELECT_GROUP_HASH_VALUES 256
4752
4753 static bool
4754 group_setup_dp_hash_table(struct group_dpif *group, size_t max_hash)
4755 {
4756 struct ofputil_bucket *bucket;
4757 uint32_t n_buckets = group->up.n_buckets;
4758 uint64_t total_weight = 0;
4759 uint16_t min_weight = UINT16_MAX;
4760 struct webster {
4761 struct ofputil_bucket *bucket;
4762 uint32_t divisor;
4763 double value;
4764 int hits;
4765 } *webster;
4766
4767 if (n_buckets == 0) {
4768 VLOG_DBG(" Don't apply dp_hash method without buckets.");
4769 return false;
4770 }
4771
4772 webster = xcalloc(n_buckets, sizeof(struct webster));
4773 int i = 0;
4774 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4775 if (bucket->weight > 0 && bucket->weight < min_weight) {
4776 min_weight = bucket->weight;
4777 }
4778 total_weight += bucket->weight;
4779 webster[i].bucket = bucket;
4780 webster[i].divisor = 1;
4781 webster[i].value = bucket->weight;
4782 webster[i].hits = 0;
4783 i++;
4784 }
4785
4786 if (total_weight == 0) {
4787 VLOG_DBG(" Total weight is zero. No active buckets.");
4788 free(webster);
4789 return false;
4790 }
4791 VLOG_DBG(" Minimum weight: %d, total weight: %"PRIu64,
4792 min_weight, total_weight);
4793
4794 uint64_t min_slots = DIV_ROUND_UP(total_weight, min_weight);
4795 uint64_t min_slots2 = ROUND_UP_POW2(min_slots);
4796 uint64_t n_hash = MAX(16, min_slots2);
4797 if (n_hash > MAX_SELECT_GROUP_HASH_VALUES ||
4798 (max_hash != 0 && n_hash > max_hash)) {
4799 VLOG_DBG(" Too many hash values required: %"PRIu64, n_hash);
4800 return false;
4801 }
4802
4803 VLOG_DBG(" Using %"PRIu64" hash values:", n_hash);
4804 group->hash_mask = n_hash - 1;
4805 if (group->hash_map) {
4806 free(group->hash_map);
4807 }
4808 group->hash_map = xcalloc(n_hash, sizeof(struct ofputil_bucket *));
4809
4810 /* Use Webster method to distribute hash values over buckets. */
4811 for (int hash = 0; hash < n_hash; hash++) {
4812 struct webster *winner = &webster[0];
4813 for (i = 1; i < n_buckets; i++) {
4814 if (webster[i].value > winner->value) {
4815 winner = &webster[i];
4816 }
4817 }
4818 winner->hits++;
4819 winner->divisor += 2;
4820 winner->value = (double) winner->bucket->weight / winner->divisor;
4821 group->hash_map[hash] = winner->bucket;
4822 }
4823
4824 i = 0;
4825 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4826 double target = (n_hash * bucket->weight) / (double) total_weight;
4827 VLOG_DBG(" Bucket %d: weight=%d, target=%.2f hits=%d",
4828 bucket->bucket_id, bucket->weight,
4829 target, webster[i].hits);
4830 i++;
4831 }
4832
4833 free(webster);
4834 return true;
4835 }
4836
4837 static void
4838 group_set_selection_method(struct group_dpif *group)
4839 {
4840 const struct ofputil_group_props *props = &group->up.props;
4841 const char *selection_method = props->selection_method;
4842
4843 VLOG_DBG("Constructing select group %"PRIu32, group->up.group_id);
4844 if (selection_method[0] == '\0') {
4845 VLOG_DBG("No selection method specified. Trying dp_hash.");
4846 /* If the controller has not specified a selection method, check if
4847 * the dp_hash selection method with max 64 hash values is appropriate
4848 * for the given bucket configuration. */
4849 if (group_setup_dp_hash_table(group, 64)) {
4850 /* Use dp_hash selection method with symmetric L4 hash. */
4851 group->selection_method = SEL_METHOD_DP_HASH;
4852 group->hash_alg = OVS_HASH_ALG_SYM_L4;
4853 group->hash_basis = 0;
4854 VLOG_DBG("Use dp_hash with %d hash values using algorithm %d.",
4855 group->hash_mask + 1, group->hash_alg);
4856 } else {
4857 /* Fall back to original default hashing in slow path. */
4858 VLOG_DBG("Falling back to default hash method.");
4859 group->selection_method = SEL_METHOD_DEFAULT;
4860 }
4861 } else if (!strcmp(selection_method, "dp_hash")) {
4862 VLOG_DBG("Selection method specified: dp_hash.");
4863 /* Try to use dp_hash if possible at all. */
4864 if (group_setup_dp_hash_table(group, 0)) {
4865 group->selection_method = SEL_METHOD_DP_HASH;
4866 group->hash_alg = props->selection_method_param >> 32;
4867 if (group->hash_alg >= __OVS_HASH_MAX) {
4868 VLOG_DBG("Invalid dp_hash algorithm %d. "
4869 "Defaulting to OVS_HASH_ALG_L4", group->hash_alg);
4870 group->hash_alg = OVS_HASH_ALG_L4;
4871 }
4872 group->hash_basis = (uint32_t) props->selection_method_param;
4873 VLOG_DBG("Use dp_hash with %d hash values using algorithm %d.",
4874 group->hash_mask + 1, group->hash_alg);
4875 } else {
4876 /* Fall back to original default hashing in slow path. */
4877 VLOG_DBG("Falling back to default hash method.");
4878 group->selection_method = SEL_METHOD_DEFAULT;
4879 }
4880 } else if (!strcmp(selection_method, "hash")) {
4881 VLOG_DBG("Selection method specified: hash.");
4882 if (props->fields.values_size > 0) {
4883 /* Controller has specified hash fields. */
4884 struct ds s = DS_EMPTY_INITIALIZER;
4885 oxm_format_field_array(&s, &props->fields);
4886 VLOG_DBG("Hash fields: %s", ds_cstr(&s));
4887 ds_destroy(&s);
4888 group->selection_method = SEL_METHOD_HASH;
4889 } else {
4890 /* No hash fields. Fall back to original default hashing. */
4891 VLOG_DBG("No hash fields. Falling back to default hash method.");
4892 group->selection_method = SEL_METHOD_DEFAULT;
4893 }
4894 } else {
4895 /* Parsing of groups should ensure this never happens */
4896 OVS_NOT_REACHED();
4897 }
4898 }
4899
4900 static enum ofperr
4901 group_construct(struct ofgroup *group_)
4902 {
4903 struct group_dpif *group = group_dpif_cast(group_);
4904
4905 ovs_mutex_init_adaptive(&group->stats_mutex);
4906 ovs_mutex_lock(&group->stats_mutex);
4907 group_construct_stats(group);
4908 group->hash_map = NULL;
4909 if (group->up.type == OFPGT11_SELECT) {
4910 group_set_selection_method(group);
4911 }
4912 ovs_mutex_unlock(&group->stats_mutex);
4913 return 0;
4914 }
4915
4916 static void
4917 group_destruct(struct ofgroup *group_)
4918 {
4919 struct group_dpif *group = group_dpif_cast(group_);
4920 ovs_mutex_destroy(&group->stats_mutex);
4921 if (group->hash_map) {
4922 free(group->hash_map);
4923 group->hash_map = NULL;
4924 }
4925 }
4926
4927 static enum ofperr
4928 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4929 {
4930 struct group_dpif *group = group_dpif_cast(group_);
4931
4932 ovs_mutex_lock(&group->stats_mutex);
4933 ogs->packet_count = group->packet_count;
4934 ogs->byte_count = group->byte_count;
4935
4936 struct bucket_counter *bucket_stats = ogs->bucket_stats;
4937 struct ofputil_bucket *bucket;
4938 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4939 bucket_stats->packet_count = bucket->stats.packet_count;
4940 bucket_stats->byte_count = bucket->stats.byte_count;
4941 bucket_stats++;
4942 }
4943 ovs_mutex_unlock(&group->stats_mutex);
4944
4945 return 0;
4946 }
4947
4948 /* If the group exists, this function increments the groups's reference count.
4949 *
4950 * Make sure to call ofproto_group_unref() after no longer needing to maintain
4951 * a reference to the group. */
4952 struct group_dpif *
4953 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4954 ovs_version_t version, bool take_ref)
4955 {
4956 struct ofgroup *ofgroup = ofproto_group_lookup(&ofproto->up, group_id,
4957 version, take_ref);
4958 return ofgroup ? group_dpif_cast(ofgroup) : NULL;
4959 }
4960 \f
4961 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4962 * supports a notion of an OAM flag, sets it if 'oam' is true.
4963 * May modify 'packet'.
4964 * Returns 0 if successful, otherwise a positive errno value. */
4965 int
4966 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, bool oam,
4967 struct dp_packet *packet)
4968 {
4969 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4970 int error;
4971
4972 error = xlate_send_packet(ofport, oam, packet);
4973
4974 ovs_mutex_lock(&ofproto->stats_mutex);
4975 ofproto->stats.tx_packets++;
4976 ofproto->stats.tx_bytes += dp_packet_size(packet);
4977 ovs_mutex_unlock(&ofproto->stats_mutex);
4978 return error;
4979 }
4980 \f
4981 /* Return the version string of the datapath that backs up
4982 * this 'ofproto'.
4983 */
4984 static const char *
4985 get_datapath_version(const struct ofproto *ofproto_)
4986 {
4987 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4988
4989 return ofproto->backer->dp_version_string;
4990 }
4991
4992 static void
4993 type_set_config(const char *type, const struct smap *other_config)
4994 {
4995 struct dpif_backer *backer;
4996
4997 backer = shash_find_data(&all_dpif_backers, type);
4998 if (!backer) {
4999 /* This is not necessarily a problem, since backers are only
5000 * created on demand. */
5001 return;
5002 }
5003
5004 dpif_set_config(backer->dpif, other_config);
5005 }
5006
5007 static void
5008 ct_flush(const struct ofproto *ofproto_, const uint16_t *zone)
5009 {
5010 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5011
5012 ct_dpif_flush(ofproto->backer->dpif, zone, NULL);
5013 }
5014
5015 static bool
5016 set_frag_handling(struct ofproto *ofproto_,
5017 enum ofputil_frag_handling frag_handling)
5018 {
5019 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5020 if (frag_handling != OFPUTIL_FRAG_REASM) {
5021 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5022 return true;
5023 } else {
5024 return false;
5025 }
5026 }
5027
5028 static enum ofperr
5029 nxt_resume(struct ofproto *ofproto_,
5030 const struct ofputil_packet_in_private *pin)
5031 {
5032 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5033 struct dpif_flow_stats stats;
5034 struct xlate_cache xcache;
5035 struct flow flow;
5036 xlate_cache_init(&xcache);
5037
5038 /* Translate pin into datapath actions. */
5039 uint64_t odp_actions_stub[1024 / 8];
5040 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
5041 enum slow_path_reason slow;
5042 enum ofperr error = xlate_resume(ofproto, pin, &odp_actions, &slow,
5043 &flow, &xcache);
5044
5045 /* Steal 'pin->packet' and put it into a dp_packet. */
5046 struct dp_packet packet;
5047 dp_packet_init(&packet, pin->base.packet_len);
5048 dp_packet_put(&packet, pin->base.packet, pin->base.packet_len);
5049
5050 /* Run the side effects from the xcache. */
5051 dpif_flow_stats_extract(&flow, &packet, time_msec(), &stats);
5052 ovs_mutex_lock(&ofproto_mutex);
5053 ofproto_dpif_xcache_execute(ofproto, &xcache, &stats);
5054 ovs_mutex_unlock(&ofproto_mutex);
5055
5056 pkt_metadata_from_flow(&packet.md, &pin->base.flow_metadata.flow);
5057
5058 /* Fix up in_port. */
5059 ofproto_dpif_set_packet_odp_port(ofproto,
5060 pin->base.flow_metadata.flow.in_port.ofp_port,
5061 &packet);
5062
5063 struct flow headers;
5064 flow_extract(&packet, &headers);
5065
5066 /* Execute the datapath actions on the packet. */
5067 struct dpif_execute execute = {
5068 .actions = odp_actions.data,
5069 .actions_len = odp_actions.size,
5070 .needs_help = (slow & SLOW_ACTION) != 0,
5071 .packet = &packet,
5072 .flow = &headers,
5073 };
5074 dpif_execute(ofproto->backer->dpif, &execute);
5075
5076 /* Clean up. */
5077 ofpbuf_uninit(&odp_actions);
5078 dp_packet_uninit(&packet);
5079
5080 return error;
5081 }
5082 \f
5083 /* NetFlow. */
5084
5085 static int
5086 set_netflow(struct ofproto *ofproto_,
5087 const struct netflow_options *netflow_options)
5088 {
5089 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5090
5091 if (netflow_options) {
5092 if (!ofproto->netflow) {
5093 ofproto->netflow = netflow_create();
5094 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5095 }
5096 return netflow_set_options(ofproto->netflow, netflow_options);
5097 } else if (ofproto->netflow) {
5098 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5099 netflow_unref(ofproto->netflow);
5100 ofproto->netflow = NULL;
5101 }
5102
5103 return 0;
5104 }
5105
5106 static void
5107 get_netflow_ids(const struct ofproto *ofproto_,
5108 uint8_t *engine_type, uint8_t *engine_id)
5109 {
5110 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5111
5112 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
5113 }
5114 \f
5115 struct ofproto_dpif *
5116 ofproto_dpif_lookup_by_name(const char *name)
5117 {
5118 struct ofproto_dpif *ofproto;
5119
5120 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_by_name_node,
5121 hash_string(name, 0),
5122 &all_ofproto_dpifs_by_name) {
5123 if (!strcmp(ofproto->up.name, name)) {
5124 return ofproto;
5125 }
5126 }
5127 return NULL;
5128 }
5129
5130 struct ofproto_dpif *
5131 ofproto_dpif_lookup_by_uuid(const struct uuid *uuid)
5132 {
5133 struct ofproto_dpif *ofproto;
5134
5135 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_by_uuid_node,
5136 uuid_hash(uuid), &all_ofproto_dpifs_by_uuid) {
5137 if (uuid_equals(&ofproto->uuid, uuid)) {
5138 return ofproto;
5139 }
5140 }
5141 return NULL;
5142 }
5143
5144 static void
5145 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
5146 const char *argv[], void *aux OVS_UNUSED)
5147 {
5148 struct ofproto_dpif *ofproto;
5149
5150 if (argc > 1) {
5151 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
5152 if (!ofproto) {
5153 unixctl_command_reply_error(conn, "no such bridge");
5154 return;
5155 }
5156 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
5157 mac_learning_flush(ofproto->ml);
5158 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5159 } else {
5160 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_by_name_node,
5161 &all_ofproto_dpifs_by_name) {
5162 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
5163 mac_learning_flush(ofproto->ml);
5164 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5165 }
5166 }
5167
5168 unixctl_command_reply(conn, "table successfully flushed");
5169 }
5170
5171 static void
5172 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
5173 const char *argv[], void *aux OVS_UNUSED)
5174 {
5175 struct ofproto_dpif *ofproto;
5176
5177 if (argc > 1) {
5178 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
5179 if (!ofproto) {
5180 unixctl_command_reply_error(conn, "no such bridge");
5181 return;
5182 }
5183
5184 if (!mcast_snooping_enabled(ofproto->ms)) {
5185 unixctl_command_reply_error(conn, "multicast snooping is disabled");
5186 return;
5187 }
5188 mcast_snooping_mdb_flush(ofproto->ms);
5189 } else {
5190 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_by_name_node,
5191 &all_ofproto_dpifs_by_name) {
5192 if (!mcast_snooping_enabled(ofproto->ms)) {
5193 continue;
5194 }
5195 mcast_snooping_mdb_flush(ofproto->ms);
5196 }
5197 }
5198
5199 unixctl_command_reply(conn, "table successfully flushed");
5200 }
5201
5202 static struct ofport_dpif *
5203 ofbundle_get_a_port(const struct ofbundle *bundle)
5204 {
5205 return CONTAINER_OF(ovs_list_front(&bundle->ports), struct ofport_dpif,
5206 bundle_node);
5207 }
5208
5209 static void
5210 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5211 const char *argv[], void *aux OVS_UNUSED)
5212 {
5213 struct ds ds = DS_EMPTY_INITIALIZER;
5214 const struct ofproto_dpif *ofproto;
5215 const struct mac_entry *e;
5216
5217 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
5218 if (!ofproto) {
5219 unixctl_command_reply_error(conn, "no such bridge");
5220 return;
5221 }
5222
5223 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5224 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
5225 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5226 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
5227 char name[OFP_MAX_PORT_NAME_LEN];
5228
5229 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5230 NULL, name, sizeof name);
5231 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
5232 name, e->vlan, ETH_ADDR_ARGS(e->mac),
5233 mac_entry_age(ofproto->ml, e));
5234 }
5235 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5236 unixctl_command_reply(conn, ds_cstr(&ds));
5237 ds_destroy(&ds);
5238 }
5239
5240 static void
5241 ofproto_unixctl_fdb_stats_clear(struct unixctl_conn *conn, int argc,
5242 const char *argv[], void *aux OVS_UNUSED)
5243 {
5244 struct ofproto_dpif *ofproto;
5245
5246 if (argc > 1) {
5247 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
5248 if (!ofproto) {
5249 unixctl_command_reply_error(conn, "no such bridge");
5250 return;
5251 }
5252 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
5253 mac_learning_clear_statistics(ofproto->ml);
5254 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5255 } else {
5256 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_by_name_node,
5257 &all_ofproto_dpifs_by_name) {
5258 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
5259 mac_learning_clear_statistics(ofproto->ml);
5260 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5261 }
5262 }
5263
5264 unixctl_command_reply(conn, "statistics successfully cleared");
5265 }
5266
5267 static void
5268 ofproto_unixctl_fdb_stats_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5269 const char *argv[], void *aux OVS_UNUSED)
5270 {
5271 struct ds ds = DS_EMPTY_INITIALIZER;
5272 const struct ofproto_dpif *ofproto;
5273 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
5274 if (!ofproto) {
5275 unixctl_command_reply_error(conn, "no such bridge");
5276 return;
5277 }
5278
5279 ds_put_format(&ds, "Statistics for bridge \"%s\":\n", argv[1]);
5280 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
5281
5282 ds_put_format(&ds, " Current/maximum MAC entries in the table: %"
5283 PRIuSIZE"/%"PRIuSIZE"\n",
5284 hmap_count(&ofproto->ml->table), ofproto->ml->max_entries);
5285 ds_put_format(&ds,
5286 " Total number of learned MAC entries : %"PRIu64"\n",
5287 ofproto->ml->total_learned);
5288 ds_put_format(&ds,
5289 " Total number of expired MAC entries : %"PRIu64"\n",
5290 ofproto->ml->total_expired);
5291 ds_put_format(&ds,
5292 " Total number of evicted MAC entries : %"PRIu64"\n",
5293 ofproto->ml->total_evicted);
5294 ds_put_format(&ds,
5295 " Total number of port moved MAC entries : %"PRIu64"\n",
5296 ofproto->ml->total_moved);
5297
5298 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5299 unixctl_command_reply(conn, ds_cstr(&ds));
5300 ds_destroy(&ds);
5301 }
5302
5303 static void
5304 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
5305 int argc OVS_UNUSED,
5306 const char *argv[],
5307 void *aux OVS_UNUSED)
5308 {
5309 struct ds ds = DS_EMPTY_INITIALIZER;
5310 const struct ofproto_dpif *ofproto;
5311 const struct ofbundle *bundle;
5312 const struct mcast_group *grp;
5313 struct mcast_group_bundle *b;
5314 struct mcast_mrouter_bundle *mrouter;
5315
5316 ofproto = ofproto_dpif_lookup_by_name(argv[1]);
5317 if (!ofproto) {
5318 unixctl_command_reply_error(conn, "no such bridge");
5319 return;
5320 }
5321
5322 if (!mcast_snooping_enabled(ofproto->ms)) {
5323 unixctl_command_reply_error(conn, "multicast snooping is disabled");
5324 return;
5325 }
5326
5327 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
5328 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
5329 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
5330 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
5331 char name[OFP_MAX_PORT_NAME_LEN];
5332
5333 bundle = b->port;
5334 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5335 NULL, name, sizeof name);
5336 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
5337 ipv6_format_mapped(&grp->addr, &ds);
5338 ds_put_format(&ds, " %3d\n",
5339 mcast_bundle_age(ofproto->ms, b));
5340 }
5341 }
5342
5343 /* ports connected to multicast routers */
5344 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
5345 char name[OFP_MAX_PORT_NAME_LEN];
5346
5347 bundle = mrouter->port;
5348 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5349 NULL, name, sizeof name);
5350 ds_put_format(&ds, "%5s %4d querier %3d\n",
5351 name, mrouter->vlan,
5352 mcast_mrouter_age(ofproto->ms, mrouter));
5353 }
5354 ovs_rwlock_unlock(&ofproto->ms->rwlock);
5355 unixctl_command_reply(conn, ds_cstr(&ds));
5356 ds_destroy(&ds);
5357 }
5358
5359 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5360 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5361 * to destroy 'ofproto_shash' and free the returned value. */
5362 static const struct shash_node **
5363 get_ofprotos(struct shash *ofproto_shash)
5364 {
5365 const struct ofproto_dpif *ofproto;
5366
5367 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_by_name_node,
5368 &all_ofproto_dpifs_by_name) {
5369 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5370 shash_add_nocopy(ofproto_shash, name, ofproto);
5371 }
5372
5373 return shash_sort(ofproto_shash);
5374 }
5375
5376 static void
5377 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5378 const char *argv[] OVS_UNUSED,
5379 void *aux OVS_UNUSED)
5380 {
5381 struct ds ds = DS_EMPTY_INITIALIZER;
5382 struct shash ofproto_shash;
5383 const struct shash_node **sorted_ofprotos;
5384 int i;
5385
5386 shash_init(&ofproto_shash);
5387 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5388 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5389 const struct shash_node *node = sorted_ofprotos[i];
5390 ds_put_format(&ds, "%s\n", node->name);
5391 }
5392
5393 shash_destroy(&ofproto_shash);
5394 free(sorted_ofprotos);
5395
5396 unixctl_command_reply(conn, ds_cstr(&ds));
5397 ds_destroy(&ds);
5398 }
5399
5400 static void
5401 show_dp_feature_bool(struct ds *ds, const char *feature, bool b)
5402 {
5403 ds_put_format(ds, "%s: %s\n", feature, b ? "Yes" : "No");
5404 }
5405
5406 static void
5407 show_dp_feature_size_t(struct ds *ds, const char *feature, size_t s)
5408 {
5409 ds_put_format(ds, "%s: %"PRIuSIZE"\n", feature, s);
5410 }
5411
5412 enum dpif_support_field_type {
5413 DPIF_SUPPORT_FIELD_bool,
5414 DPIF_SUPPORT_FIELD_size_t,
5415 };
5416
5417 struct dpif_support_field {
5418 void *rt_ptr; /* Points to the 'rt_support' field. */
5419 const void *bt_ptr; /* Points to the 'bt_support' field. */
5420 const char *title;
5421 enum dpif_support_field_type type;
5422 };
5423
5424 #define DPIF_SUPPORT_FIELD_INTIALIZER(RT_PTR, BT_PTR, TITLE, TYPE) \
5425 (struct dpif_support_field) {RT_PTR, BT_PTR, TITLE, TYPE}
5426
5427 static void
5428 dpif_show_support(const struct dpif_backer_support *support, struct ds *ds)
5429 {
5430 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5431 show_dp_feature_##TYPE (ds, TITLE, support->NAME);
5432 DPIF_SUPPORT_FIELDS
5433 #undef DPIF_SUPPORT_FIELD
5434
5435 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5436 show_dp_feature_##TYPE (ds, TITLE, support->odp.NAME );
5437 ODP_SUPPORT_FIELDS
5438 #undef ODP_SUPPORT_FIELD
5439 }
5440
5441 static void
5442 display_support_field(const char *name,
5443 const struct dpif_support_field *field,
5444 struct ds *ds)
5445 {
5446 switch (field->type) {
5447 case DPIF_SUPPORT_FIELD_bool: {
5448 bool v = *(bool *)field->rt_ptr;
5449 bool b = *(bool *)field->bt_ptr;
5450 ds_put_format(ds, "%s (%s) : [run time]:%s, [boot time]:%s\n", name,
5451 field->title, v ? "true" : "false",
5452 b ? "true" : "false");
5453 break;
5454 }
5455 case DPIF_SUPPORT_FIELD_size_t:
5456 ds_put_format(ds, "%s (%s) : [run time]:%"PRIuSIZE
5457 ", [boot time]:%"PRIuSIZE"\n", name,
5458 field->title, *(size_t *)field->rt_ptr,
5459 *(size_t *)field->bt_ptr);
5460 break;
5461 default:
5462 OVS_NOT_REACHED();
5463 }
5464 }
5465
5466 /* Set a field of 'rt_support' to a new value.
5467 *
5468 * Returns 'true' if the value is actually set. */
5469 static bool
5470 dpif_set_support(struct dpif_backer_support *rt_support,
5471 struct dpif_backer_support *bt_support,
5472 const char *name, const char *value, struct ds *ds)
5473 {
5474 struct shash all_fields = SHASH_INITIALIZER(&all_fields);
5475 struct dpif_support_field *field;
5476 struct shash_node *node;
5477 bool changed = false;
5478
5479 #define DPIF_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5480 {\
5481 struct dpif_support_field *f = xmalloc(sizeof *f); \
5482 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->NAME, \
5483 &bt_support->NAME, \
5484 TITLE, \
5485 DPIF_SUPPORT_FIELD_##TYPE);\
5486 shash_add_once(&all_fields, #NAME, f); \
5487 }
5488 DPIF_SUPPORT_FIELDS;
5489 #undef DPIF_SUPPORT_FIELD
5490
5491 #define ODP_SUPPORT_FIELD(TYPE, NAME, TITLE) \
5492 {\
5493 struct dpif_support_field *f = xmalloc(sizeof *f); \
5494 *f = DPIF_SUPPORT_FIELD_INTIALIZER(&rt_support->odp.NAME, \
5495 &bt_support->odp.NAME, \
5496 TITLE, \
5497 DPIF_SUPPORT_FIELD_##TYPE);\
5498 shash_add_once(&all_fields, #NAME, f); \
5499 }
5500 ODP_SUPPORT_FIELDS;
5501 #undef ODP_SUPPORT_FIELD
5502
5503 if (!name) {
5504 SHASH_FOR_EACH (node, &all_fields) {
5505 display_support_field(node->name, node->data, ds);
5506 }
5507 goto done;
5508 }
5509
5510 node = shash_find(&all_fields, name);
5511 if (!node) {
5512 ds_put_cstr(ds, "Unexpected support field");
5513 goto done;
5514 }
5515 field = node->data;
5516
5517 if (!value) {
5518 display_support_field(node->name, field, ds);
5519 goto done;
5520 }
5521
5522 if (field->type == DPIF_SUPPORT_FIELD_bool) {
5523 if (!strcasecmp(value, "true")) {
5524 if (*(bool *)field->bt_ptr) {
5525 *(bool *)field->rt_ptr = true;
5526 changed = true;
5527 } else {
5528 ds_put_cstr(ds, "Can not enable features not supported by the datapth");
5529 }
5530 } else if (!strcasecmp(value, "false")) {
5531 *(bool *)field->rt_ptr = false;
5532 changed = true;
5533 } else {
5534 ds_put_cstr(ds, "Boolean value expected");
5535 }
5536 } else if (field->type == DPIF_SUPPORT_FIELD_size_t) {
5537 int v;
5538 if (str_to_int(value, 10, &v)) {
5539 if (v >= 0) {
5540 if (v <= *(size_t *)field->bt_ptr) {
5541 *(size_t *)field->rt_ptr = v;
5542 changed = true;
5543 } else {
5544 ds_put_cstr(ds, "Can not set value beyond the datapath capability");
5545 }
5546 } else {
5547 ds_put_format(ds, "Negative number not expected");
5548 }
5549 } else {
5550 ds_put_cstr(ds, "Integer number expected");
5551 }
5552 }
5553
5554 done:
5555 shash_destroy_free_data(&all_fields);
5556 return changed;
5557 }
5558
5559 static void
5560 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5561 {
5562 const struct shash_node **ofprotos;
5563 struct dpif_dp_stats dp_stats;
5564 struct shash ofproto_shash;
5565 size_t i;
5566
5567 dpif_get_dp_stats(backer->dpif, &dp_stats);
5568 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5569 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5570
5571 shash_init(&ofproto_shash);
5572 ofprotos = get_ofprotos(&ofproto_shash);
5573 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5574 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5575 const struct shash_node **ports;
5576 size_t j;
5577
5578 if (ofproto->backer != backer) {
5579 continue;
5580 }
5581
5582 ds_put_format(ds, " %s:\n", ofproto->up.name);
5583
5584 ports = shash_sort(&ofproto->up.port_by_name);
5585 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5586 const struct shash_node *node = ports[j];
5587 struct ofport *ofport = node->data;
5588 struct smap config;
5589 odp_port_t odp_port;
5590
5591 ds_put_format(ds, " %s %u/", netdev_get_name(ofport->netdev),
5592 ofport->ofp_port);
5593
5594 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5595 if (odp_port != ODPP_NONE) {
5596 ds_put_format(ds, "%"PRIu32":", odp_port);
5597 } else {
5598 ds_put_cstr(ds, "none:");
5599 }
5600
5601 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5602
5603 smap_init(&config);
5604 if (!netdev_get_config(ofport->netdev, &config)) {
5605 const struct smap_node **nodes = smap_sort(&config);
5606 for (size_t k = 0; k < smap_count(&config); k++) {
5607 ds_put_format(ds, "%c %s=%s", k ? ',' : ':',
5608 nodes[k]->key, nodes[k]->value);
5609 }
5610 free(nodes);
5611 }
5612 smap_destroy(&config);
5613
5614 ds_put_char(ds, ')');
5615 ds_put_char(ds, '\n');
5616 }
5617 free(ports);
5618 }
5619 shash_destroy(&ofproto_shash);
5620 free(ofprotos);
5621 }
5622
5623 static void
5624 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5625 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5626 {
5627 struct ds ds = DS_EMPTY_INITIALIZER;
5628 const struct shash_node **backers;
5629 int i;
5630
5631 backers = shash_sort(&all_dpif_backers);
5632 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5633 dpif_show_backer(backers[i]->data, &ds);
5634 }
5635 free(backers);
5636
5637 unixctl_command_reply(conn, ds_cstr(&ds));
5638 ds_destroy(&ds);
5639 }
5640
5641 static void
5642 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5643 int argc OVS_UNUSED, const char *argv[],
5644 void *aux OVS_UNUSED)
5645 {
5646 const struct ofproto_dpif *ofproto;
5647
5648 struct ds ds = DS_EMPTY_INITIALIZER;
5649
5650 struct dpif_flow_dump *flow_dump;
5651 struct dpif_flow_dump_thread *flow_dump_thread;
5652 struct dpif_flow f;
5653 int error;
5654
5655 ofproto = ofproto_dpif_lookup_by_name(argv[argc - 1]);
5656 if (!ofproto) {
5657 unixctl_command_reply_error(conn, "no such bridge");
5658 return;
5659 }
5660
5661 bool verbosity = false;
5662 bool names = false;
5663 bool set_names = false;
5664 for (int i = 1; i < argc - 1; i++) {
5665 if (!strcmp(argv[i], "-m")) {
5666 verbosity = true;
5667 } else if (!strcmp(argv[i], "--names")) {
5668 names = true;
5669 set_names = true;
5670 } else if (!strcmp(argv[i], "--no-names")) {
5671 names = false;
5672 set_names = true;
5673 }
5674 }
5675 if (!set_names) {
5676 names = verbosity;
5677 }
5678
5679 struct hmap *portno_names = NULL;
5680 if (names) {
5681 portno_names = xmalloc(sizeof *portno_names);
5682 hmap_init(portno_names);
5683
5684 struct dpif_port dpif_port;
5685 struct dpif_port_dump port_dump;
5686 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5687 odp_portno_names_set(portno_names, dpif_port.port_no,
5688 dpif_port.name);
5689 }
5690 }
5691
5692 ds_init(&ds);
5693 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false, NULL);
5694 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5695 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5696 struct flow flow;
5697
5698 if ((odp_flow_key_to_flow(f.key, f.key_len, &flow, NULL)
5699 == ODP_FIT_ERROR)
5700 || (xlate_lookup_ofproto(ofproto->backer, &flow, NULL, NULL)
5701 != ofproto)) {
5702 continue;
5703 }
5704
5705 if (verbosity) {
5706 odp_format_ufid(&f.ufid, &ds);
5707 ds_put_cstr(&ds, " ");
5708 }
5709 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5710 portno_names, &ds, verbosity);
5711 ds_put_cstr(&ds, ", ");
5712 dpif_flow_stats_format(&f.stats, &ds);
5713 ds_put_cstr(&ds, ", actions:");
5714 format_odp_actions(&ds, f.actions, f.actions_len, portno_names);
5715 ds_put_char(&ds, '\n');
5716 }
5717 dpif_flow_dump_thread_destroy(flow_dump_thread);
5718 error = dpif_flow_dump_destroy(flow_dump);
5719
5720 if (error) {
5721 ds_clear(&ds);
5722 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5723 unixctl_command_reply_error(conn, ds_cstr(&ds));
5724 } else {
5725 unixctl_command_reply(conn, ds_cstr(&ds));
5726 }
5727 if (portno_names) {
5728 odp_portno_names_destroy(portno_names);
5729 hmap_destroy(portno_names);
5730 free(portno_names);
5731 }
5732 ds_destroy(&ds);
5733 }
5734
5735 static void
5736 ofproto_unixctl_dpif_show_dp_features(struct unixctl_conn *conn,
5737 int argc, const char *argv[],
5738 void *aux OVS_UNUSED)
5739 {
5740 struct ds ds = DS_EMPTY_INITIALIZER;
5741 const char *br = argv[argc -1];
5742 struct ofproto_dpif *ofproto = ofproto_dpif_lookup_by_name(br);
5743
5744 if (!ofproto) {
5745 unixctl_command_reply_error(conn, "no such bridge");
5746 return;
5747 }
5748
5749 dpif_show_support(&ofproto->backer->bt_support, &ds);
5750 unixctl_command_reply(conn, ds_cstr(&ds));
5751 }
5752
5753 static void
5754 ofproto_unixctl_dpif_set_dp_features(struct unixctl_conn *conn,
5755 int argc, const char *argv[],
5756 void *aux OVS_UNUSED)
5757 {
5758 struct ds ds = DS_EMPTY_INITIALIZER;
5759 const char *br = argv[1];
5760 const char *name, *value;
5761 struct ofproto_dpif *ofproto = ofproto_dpif_lookup_by_name(br);
5762 bool changed;
5763
5764 if (!ofproto) {
5765 unixctl_command_reply_error(conn, "no such bridge");
5766 return;
5767 }
5768
5769 name = argc > 2 ? argv[2] : NULL;
5770 value = argc > 3 ? argv[3] : NULL;
5771 changed = dpif_set_support(&ofproto->backer->rt_support,
5772 &ofproto->backer->bt_support,
5773 name, value, &ds);
5774 if (changed) {
5775 xlate_set_support(ofproto, &ofproto->backer->rt_support);
5776 udpif_flush(ofproto->backer->udpif);
5777 }
5778 unixctl_command_reply(conn, ds_cstr(&ds));
5779 ds_destroy(&ds);
5780 }
5781
5782 static void
5783 ofproto_unixctl_init(void)
5784 {
5785 static bool registered;
5786 if (registered) {
5787 return;
5788 }
5789 registered = true;
5790
5791 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5792 ofproto_unixctl_fdb_flush, NULL);
5793 unixctl_command_register("fdb/show", "bridge", 1, 1,
5794 ofproto_unixctl_fdb_show, NULL);
5795 unixctl_command_register("fdb/stats-clear", "[bridge]", 0, 1,
5796 ofproto_unixctl_fdb_stats_clear, NULL);
5797 unixctl_command_register("fdb/stats-show", "bridge", 1, 1,
5798 ofproto_unixctl_fdb_stats_show, NULL);
5799 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5800 ofproto_unixctl_mcast_snooping_flush, NULL);
5801 unixctl_command_register("mdb/show", "bridge", 1, 1,
5802 ofproto_unixctl_mcast_snooping_show, NULL);
5803 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5804 ofproto_unixctl_dpif_dump_dps, NULL);
5805 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5806 NULL);
5807 unixctl_command_register("dpif/show-dp-features", "bridge", 1, 1,
5808 ofproto_unixctl_dpif_show_dp_features, NULL);
5809 unixctl_command_register("dpif/dump-flows",
5810 "[-m] [--names | --no-names] bridge", 1, INT_MAX,
5811 ofproto_unixctl_dpif_dump_flows, NULL);
5812 unixctl_command_register("dpif/set-dp-features", "bridge", 1, 3 ,
5813 ofproto_unixctl_dpif_set_dp_features, NULL);
5814 }
5815 \f
5816 static odp_port_t
5817 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5818 {
5819 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5820 return ofport ? ofport->odp_port : ODPP_NONE;
5821 }
5822
5823 struct ofport_dpif *
5824 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5825 {
5826 struct ofport_dpif *port;
5827
5828 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5829 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5830 &backer->odp_to_ofport_map) {
5831 if (port->odp_port == odp_port) {
5832 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5833 return port;
5834 }
5835 }
5836
5837 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5838 return NULL;
5839 }
5840
5841 static ofp_port_t
5842 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5843 {
5844 struct ofport_dpif *port;
5845
5846 port = odp_port_to_ofport(ofproto->backer, odp_port);
5847 if (port && &ofproto->up == port->up.ofproto) {
5848 return port->up.ofp_port;
5849 } else {
5850 return OFPP_NONE;
5851 }
5852 }
5853
5854 /* 'match' is non-const to allow for temporary modifications. Any changes are
5855 * restored before returning. */
5856 int
5857 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5858 struct match *match, int priority,
5859 uint16_t idle_timeout,
5860 const struct ofpbuf *ofpacts,
5861 struct rule **rulep)
5862 {
5863 struct ofputil_flow_mod fm;
5864 struct rule_dpif *rule;
5865 int error;
5866
5867 fm = (struct ofputil_flow_mod) {
5868 .buffer_id = UINT32_MAX,
5869 .priority = priority,
5870 .table_id = TBL_INTERNAL,
5871 .command = OFPFC_ADD,
5872 .idle_timeout = idle_timeout,
5873 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5874 .ofpacts = ofpacts->data,
5875 .ofpacts_len = ofpacts->size,
5876 };
5877 minimatch_init(&fm.match, match);
5878 error = ofproto_flow_mod(&ofproto->up, &fm);
5879 minimatch_destroy(&fm.match);
5880
5881 if (error) {
5882 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5883 ofperr_to_string(error));
5884 *rulep = NULL;
5885 return error;
5886 }
5887
5888 rule = rule_dpif_lookup_in_table(ofproto,
5889 ofproto_dpif_get_tables_version(ofproto),
5890 TBL_INTERNAL, &match->flow, &match->wc);
5891 if (rule) {
5892 *rulep = &rule->up;
5893 } else {
5894 OVS_NOT_REACHED();
5895 }
5896 return 0;
5897 }
5898
5899 int
5900 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5901 struct match *match, int priority)
5902 {
5903 struct ofputil_flow_mod fm;
5904 int error;
5905
5906 fm = (struct ofputil_flow_mod) {
5907 .buffer_id = UINT32_MAX,
5908 .priority = priority,
5909 .table_id = TBL_INTERNAL,
5910 .out_port = OFPP_ANY,
5911 .out_group = OFPG_ANY,
5912 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5913 .command = OFPFC_DELETE_STRICT,
5914 };
5915 minimatch_init(&fm.match, match);
5916 error = ofproto_flow_mod(&ofproto->up, &fm);
5917 minimatch_destroy(&fm.match);
5918
5919 if (error) {
5920 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5921 ofperr_to_string(error));
5922 return error;
5923 }
5924
5925 return 0;
5926 }
5927
5928 static void
5929 meter_get_features(const struct ofproto *ofproto_,
5930 struct ofputil_meter_features *features)
5931 {
5932 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5933
5934 dpif_meter_get_features(ofproto->backer->dpif, features);
5935 }
5936
5937 static enum ofperr
5938 meter_set(struct ofproto *ofproto_, ofproto_meter_id *meter_id,
5939 struct ofputil_meter_config *config)
5940 {
5941 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5942
5943 /* Provider ID unknown. Use backer to allocate a new DP meter */
5944 if (meter_id->uint32 == UINT32_MAX) {
5945 if (!ofproto->backer->meter_ids) {
5946 return OFPERR_OFPMMFC_OUT_OF_METERS; /* Meters not supported. */
5947 }
5948
5949 if(!id_pool_alloc_id(ofproto->backer->meter_ids, &meter_id->uint32)) {
5950 return OFPERR_OFPMMFC_OUT_OF_METERS; /* Can't allocate meter. */
5951 }
5952 }
5953
5954 switch (dpif_meter_set(ofproto->backer->dpif, *meter_id, config)) {
5955 case 0:
5956 return 0;
5957 case EFBIG: /* meter_id out of range */
5958 case ENOMEM: /* Cannot allocate meter */
5959 return OFPERR_OFPMMFC_OUT_OF_METERS;
5960 case EBADF: /* Unsupported flags */
5961 return OFPERR_OFPMMFC_BAD_FLAGS;
5962 case EINVAL: /* Too many bands */
5963 return OFPERR_OFPMMFC_OUT_OF_BANDS;
5964 case ENODEV: /* Unsupported band type */
5965 return OFPERR_OFPMMFC_BAD_BAND;
5966 case EDOM: /* Rate must be non-zero */
5967 return OFPERR_OFPMMFC_BAD_RATE;
5968 default:
5969 return OFPERR_OFPMMFC_UNKNOWN;
5970 }
5971 }
5972
5973 static enum ofperr
5974 meter_get(const struct ofproto *ofproto_, ofproto_meter_id meter_id,
5975 struct ofputil_meter_stats *stats, uint16_t n_bands)
5976 {
5977 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5978
5979 if (!dpif_meter_get(ofproto->backer->dpif, meter_id, stats, n_bands)) {
5980 return 0;
5981 }
5982 return OFPERR_OFPMMFC_UNKNOWN_METER;
5983 }
5984
5985 struct free_meter_id_args {
5986 struct ofproto_dpif *ofproto;
5987 ofproto_meter_id meter_id;
5988 };
5989
5990 static void
5991 free_meter_id(struct free_meter_id_args *args)
5992 {
5993 struct ofproto_dpif *ofproto = args->ofproto;
5994
5995 dpif_meter_del(ofproto->backer->dpif, args->meter_id, NULL, 0);
5996 id_pool_free_id(ofproto->backer->meter_ids, args->meter_id.uint32);
5997 free(args);
5998 }
5999
6000 static void
6001 meter_del(struct ofproto *ofproto_, ofproto_meter_id meter_id)
6002 {
6003 struct free_meter_id_args *arg = xmalloc(sizeof *arg);
6004
6005 /* Before a meter can be deleted, Openflow spec requires all rules
6006 * referring to the meter to be (automatically) removed before the
6007 * meter is deleted. However, since vswitchd is multi-threaded,
6008 * those rules and their actions remain accessible by other threads,
6009 * especially by the handler and revalidator threads.
6010 * Postpone meter deletion after RCU grace period, so that ongoing
6011 * upcall translation or flow revalidation can complete. */
6012 arg->ofproto = ofproto_dpif_cast(ofproto_);
6013 arg->meter_id = meter_id;
6014 ovsrcu_postpone(free_meter_id, arg);
6015 }
6016
6017 const struct ofproto_class ofproto_dpif_class = {
6018 init,
6019 enumerate_types,
6020 enumerate_names,
6021 del,
6022 port_open_type,
6023 type_run,
6024 type_wait,
6025 alloc,
6026 construct,
6027 destruct,
6028 dealloc,
6029 run,
6030 ofproto_dpif_wait,
6031 NULL, /* get_memory_usage. */
6032 type_get_memory_usage,
6033 flush,
6034 query_tables,
6035 NULL, /* modify_tables */
6036 set_tables_version,
6037 port_alloc,
6038 port_construct,
6039 port_destruct,
6040 port_dealloc,
6041 port_modified,
6042 port_reconfigured,
6043 port_query_by_name,
6044 port_add,
6045 port_del,
6046 port_set_config,
6047 port_get_stats,
6048 port_dump_start,
6049 port_dump_next,
6050 port_dump_done,
6051 port_poll,
6052 port_poll_wait,
6053 port_is_lacp_current,
6054 port_get_lacp_stats,
6055 NULL, /* rule_choose_table */
6056 rule_alloc,
6057 rule_construct,
6058 rule_insert,
6059 NULL, /* rule_delete */
6060 rule_destruct,
6061 rule_dealloc,
6062 rule_get_stats,
6063 packet_xlate,
6064 packet_xlate_revert,
6065 packet_execute,
6066 set_frag_handling,
6067 nxt_resume,
6068 set_netflow,
6069 get_netflow_ids,
6070 set_sflow,
6071 set_ipfix,
6072 get_ipfix_stats,
6073 set_cfm,
6074 cfm_status_changed,
6075 get_cfm_status,
6076 set_lldp,
6077 get_lldp_status,
6078 set_aa,
6079 aa_mapping_set,
6080 aa_mapping_unset,
6081 aa_vlan_get_queued,
6082 aa_vlan_get_queue_size,
6083 set_bfd,
6084 bfd_status_changed,
6085 get_bfd_status,
6086 set_stp,
6087 get_stp_status,
6088 set_stp_port,
6089 get_stp_port_status,
6090 get_stp_port_stats,
6091 set_rstp,
6092 get_rstp_status,
6093 set_rstp_port,
6094 get_rstp_port_status,
6095 set_queues,
6096 bundle_set,
6097 bundle_remove,
6098 mirror_set__,
6099 mirror_get_stats__,
6100 set_flood_vlans,
6101 is_mirror_output_bundle,
6102 forward_bpdu_changed,
6103 set_mac_table_config,
6104 set_mcast_snooping,
6105 set_mcast_snooping_port,
6106 meter_get_features,
6107 meter_set,
6108 meter_get,
6109 meter_del,
6110 group_alloc, /* group_alloc */
6111 group_construct, /* group_construct */
6112 group_destruct, /* group_destruct */
6113 group_dealloc, /* group_dealloc */
6114 NULL, /* group_modify */
6115 group_get_stats, /* group_get_stats */
6116 get_datapath_version, /* get_datapath_version */
6117 type_set_config,
6118 ct_flush, /* ct_flush */
6119 };