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