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