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