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