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