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