]> git.proxmox.com Git - ovs.git/blob - ofproto/ofproto.c
bridge: Move packet processing functionality into ofproto.
[ovs.git] / ofproto / ofproto.c
1 /*
2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3 * Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <config.h>
19 #include "ofproto.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include "autopath.h"
28 #include "bitmap.h"
29 #include "bond.h"
30 #include "byte-order.h"
31 #include "cfm.h"
32 #include "classifier.h"
33 #include "connmgr.h"
34 #include "coverage.h"
35 #include "dpif.h"
36 #include "dynamic-string.h"
37 #include "fail-open.h"
38 #include "hash.h"
39 #include "hmap.h"
40 #include "hmapx.h"
41 #include "in-band.h"
42 #include "lacp.h"
43 #include "mac-learning.h"
44 #include "multipath.h"
45 #include "netdev.h"
46 #include "netflow.h"
47 #include "netlink.h"
48 #include "nx-match.h"
49 #include "odp-util.h"
50 #include "ofp-print.h"
51 #include "ofp-util.h"
52 #include "ofproto-sflow.h"
53 #include "ofpbuf.h"
54 #include "openflow/nicira-ext.h"
55 #include "openflow/openflow.h"
56 #include "openvswitch/datapath-protocol.h"
57 #include "packets.h"
58 #include "pinsched.h"
59 #include "pktbuf.h"
60 #include "poll-loop.h"
61 #include "private.h"
62 #include "rconn.h"
63 #include "shash.h"
64 #include "sset.h"
65 #include "stream-ssl.h"
66 #include "tag.h"
67 #include "timer.h"
68 #include "timeval.h"
69 #include "unaligned.h"
70 #include "unixctl.h"
71 #include "vconn.h"
72 #include "vlan-bitmap.h"
73 #include "vlog.h"
74
75 VLOG_DEFINE_THIS_MODULE(ofproto);
76
77 COVERAGE_DEFINE(facet_changed_rule);
78 COVERAGE_DEFINE(facet_revalidate);
79 COVERAGE_DEFINE(odp_overflow);
80 COVERAGE_DEFINE(ofproto_agg_request);
81 COVERAGE_DEFINE(ofproto_costly_flags);
82 COVERAGE_DEFINE(ofproto_ctlr_action);
83 COVERAGE_DEFINE(ofproto_del_rule);
84 COVERAGE_DEFINE(ofproto_error);
85 COVERAGE_DEFINE(ofproto_expiration);
86 COVERAGE_DEFINE(ofproto_expired);
87 COVERAGE_DEFINE(ofproto_flows_req);
88 COVERAGE_DEFINE(ofproto_flush);
89 COVERAGE_DEFINE(ofproto_invalidated);
90 COVERAGE_DEFINE(ofproto_no_packet_in);
91 COVERAGE_DEFINE(ofproto_ofp2odp);
92 COVERAGE_DEFINE(ofproto_packet_in);
93 COVERAGE_DEFINE(ofproto_packet_out);
94 COVERAGE_DEFINE(ofproto_queue_req);
95 COVERAGE_DEFINE(ofproto_recv_openflow);
96 COVERAGE_DEFINE(ofproto_reinit_ports);
97 COVERAGE_DEFINE(ofproto_unexpected_rule);
98 COVERAGE_DEFINE(ofproto_uninstallable);
99 COVERAGE_DEFINE(ofproto_update_port);
100
101 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
102 * flow translation. */
103 #define MAX_RESUBMIT_RECURSION 16
104
105 struct rule;
106
107 #define MAX_MIRRORS 32
108 typedef uint32_t mirror_mask_t;
109 #define MIRROR_MASK_C(X) UINT32_C(X)
110 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
111 struct ofmirror {
112 struct ofproto *ofproto; /* Owning ofproto. */
113 size_t idx; /* In ofproto's "mirrors" array. */
114 void *aux; /* Key supplied by ofproto's client. */
115 char *name; /* Identifier for log messages. */
116
117 /* Selection criteria. */
118 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
119 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
120 unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */
121
122 /* Output (mutually exclusive). */
123 struct ofbundle *out; /* Output port or NULL. */
124 int out_vlan; /* Output VLAN or -1. */
125 };
126
127 static void ofproto_mirror_destroy(struct ofmirror *);
128
129 /* A group of one or more OpenFlow ports. */
130 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
131 struct ofbundle {
132 struct ofproto *ofproto; /* Owning ofproto. */
133 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
134 void *aux; /* Key supplied by ofproto's client. */
135 char *name; /* Identifier for log messages. */
136
137 /* Configuration. */
138 struct list ports; /* Contains "struct ofport"s. */
139 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
140 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
141 * NULL if all VLANs are trunked. */
142 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
143 struct bond *bond; /* Bonding setup if more than one port,
144 * otherwise NULL. */
145
146 /* Status. */
147 bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */
148
149 /* Port mirroring info. */
150 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
151 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
152 mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */
153 };
154
155 /* An OpenFlow port. */
156 struct ofport {
157 struct ofproto *ofproto; /* Owning ofproto. */
158 struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
159 struct netdev *netdev;
160 struct ofp_phy_port opp;
161 uint16_t odp_port;
162
163 /* Bridging. */
164 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
165 struct list bundle_node; /* In struct ofbundle's "ports" list. */
166 struct cfm *cfm; /* Connectivity Fault Management, if any. */
167 tag_type tag; /* Tag associated with this port. */
168 };
169
170 static void ofport_free(struct ofport *);
171 static void ofport_run(struct ofport *);
172 static void ofport_wait(struct ofport *);
173
174 struct action_xlate_ctx {
175 /* action_xlate_ctx_init() initializes these members. */
176
177 /* The ofproto. */
178 struct ofproto *ofproto;
179
180 /* Flow to which the OpenFlow actions apply. xlate_actions() will modify
181 * this flow when actions change header fields. */
182 struct flow flow;
183
184 /* The packet corresponding to 'flow', or a null pointer if we are
185 * revalidating without a packet to refer to. */
186 const struct ofpbuf *packet;
187
188 /* If nonnull, called just before executing a resubmit action.
189 *
190 * This is normally null so the client has to set it manually after
191 * calling action_xlate_ctx_init(). */
192 void (*resubmit_hook)(struct action_xlate_ctx *, struct rule *);
193
194 /* If true, the speciality of 'flow' should be checked before executing
195 * its actions. If special_cb returns false on 'flow' rendered
196 * uninstallable and no actions will be executed. */
197 bool check_special;
198
199 /* xlate_actions() initializes and uses these members. The client might want
200 * to look at them after it returns. */
201
202 struct ofpbuf *odp_actions; /* Datapath actions. */
203 tag_type tags; /* Tags associated with OFPP_NORMAL actions. */
204 bool may_set_up_flow; /* True ordinarily; false if the actions must
205 * be reassessed for every packet. */
206 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
207
208 /* xlate_actions() initializes and uses these members, but the client has no
209 * reason to look at them. */
210
211 int recurse; /* Recursion level, via xlate_table_action. */
212 int last_pop_priority; /* Offset in 'odp_actions' just past most
213 * recent ODP_ACTION_ATTR_SET_PRIORITY. */
214 };
215
216 static void action_xlate_ctx_init(struct action_xlate_ctx *,
217 struct ofproto *, const struct flow *,
218 const struct ofpbuf *);
219 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
220 const union ofp_action *in, size_t n_in);
221
222 /* An OpenFlow flow. */
223 struct rule {
224 long long int used; /* Time last used; time created if not used. */
225 long long int created; /* Creation time. */
226
227 /* These statistics:
228 *
229 * - Do include packets and bytes from facets that have been deleted or
230 * whose own statistics have been folded into the rule.
231 *
232 * - Do include packets and bytes sent "by hand" that were accounted to
233 * the rule without any facet being involved (this is a rare corner
234 * case in rule_execute()).
235 *
236 * - Do not include packet or bytes that can be obtained from any facet's
237 * packet_count or byte_count member or that can be obtained from the
238 * datapath by, e.g., dpif_flow_get() for any facet.
239 */
240 uint64_t packet_count; /* Number of packets received. */
241 uint64_t byte_count; /* Number of bytes received. */
242
243 ovs_be64 flow_cookie; /* Controller-issued identifier. */
244
245 struct cls_rule cr; /* In owning ofproto's classifier. */
246 uint16_t idle_timeout; /* In seconds from time of last use. */
247 uint16_t hard_timeout; /* In seconds from time of creation. */
248 bool send_flow_removed; /* Send a flow removed message? */
249 int n_actions; /* Number of elements in actions[]. */
250 union ofp_action *actions; /* OpenFlow actions. */
251 struct list facets; /* List of "struct facet"s. */
252 };
253
254 static struct rule *rule_from_cls_rule(const struct cls_rule *);
255 static bool rule_is_hidden(const struct rule *);
256
257 static struct rule *rule_create(const struct cls_rule *,
258 const union ofp_action *, size_t n_actions,
259 uint16_t idle_timeout, uint16_t hard_timeout,
260 ovs_be64 flow_cookie, bool send_flow_removed);
261 static void rule_destroy(struct ofproto *, struct rule *);
262 static void rule_free(struct rule *);
263
264 static struct rule *rule_lookup(struct ofproto *, const struct flow *);
265 static void rule_insert(struct ofproto *, struct rule *);
266 static void rule_remove(struct ofproto *, struct rule *);
267
268 static void rule_send_removed(struct ofproto *, struct rule *, uint8_t reason);
269 static void rule_get_stats(const struct rule *, uint64_t *packets,
270 uint64_t *bytes);
271
272 /* An exact-match instantiation of an OpenFlow flow. */
273 struct facet {
274 long long int used; /* Time last used; time created if not used. */
275
276 /* These statistics:
277 *
278 * - Do include packets and bytes sent "by hand", e.g. with
279 * dpif_execute().
280 *
281 * - Do include packets and bytes that were obtained from the datapath
282 * when a flow was deleted (e.g. dpif_flow_del()) or when its
283 * statistics were reset (e.g. dpif_flow_put() with
284 * DPIF_FP_ZERO_STATS).
285 *
286 * - Do not include any packets or bytes that can currently be obtained
287 * from the datapath by, e.g., dpif_flow_get().
288 */
289 uint64_t packet_count; /* Number of packets received. */
290 uint64_t byte_count; /* Number of bytes received. */
291
292 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
293 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
294
295 uint64_t rs_packet_count; /* Packets pushed to resubmit children. */
296 uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */
297 long long int rs_used; /* Used time pushed to resubmit children. */
298
299 /* Number of bytes passed to account_cb. This may include bytes that can
300 * currently obtained from the datapath (thus, it can be greater than
301 * byte_count). */
302 uint64_t accounted_bytes;
303
304 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
305 struct list list_node; /* In owning rule's 'facets' list. */
306 struct rule *rule; /* Owning rule. */
307 struct flow flow; /* Exact-match flow. */
308 bool installed; /* Installed in datapath? */
309 bool may_install; /* True ordinarily; false if actions must
310 * be reassessed for every packet. */
311 size_t actions_len; /* Number of bytes in actions[]. */
312 struct nlattr *actions; /* Datapath actions. */
313 tag_type tags; /* Tags. */
314 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
315 };
316
317 static struct facet *facet_create(struct ofproto *, struct rule *,
318 const struct flow *,
319 const struct ofpbuf *packet);
320 static void facet_remove(struct ofproto *, struct facet *);
321 static void facet_free(struct facet *);
322
323 static struct facet *facet_lookup_valid(struct ofproto *, const struct flow *);
324 static bool facet_revalidate(struct ofproto *, struct facet *);
325
326 static void facet_install(struct ofproto *, struct facet *, bool zero_stats);
327 static void facet_uninstall(struct ofproto *, struct facet *);
328 static void facet_flush_stats(struct ofproto *, struct facet *);
329
330 static void facet_make_actions(struct ofproto *, struct facet *,
331 const struct ofpbuf *packet);
332 static void facet_update_stats(struct ofproto *, struct facet *,
333 const struct dpif_flow_stats *);
334 static void facet_push_stats(struct ofproto *, struct facet *);
335
336 static void send_packet_in(struct ofproto *, struct dpif_upcall *,
337 const struct flow *, bool clone);
338
339 struct ofproto {
340 char *name; /* Datapath name. */
341 struct hmap_node hmap_node; /* In global 'all_ofprotos' hmap. */
342
343 /* Settings. */
344 uint64_t datapath_id; /* Datapath ID. */
345 uint64_t fallback_dpid; /* Datapath ID if no better choice found. */
346 char *mfr_desc; /* Manufacturer. */
347 char *hw_desc; /* Hardware. */
348 char *sw_desc; /* Software version. */
349 char *serial_desc; /* Serial number. */
350 char *dp_desc; /* Datapath description. */
351
352 /* Datapath. */
353 struct dpif *dpif;
354 struct netdev_monitor *netdev_monitor;
355 struct hmap ports; /* Contains "struct ofport"s. */
356 struct shash port_by_name;
357 uint32_t max_ports;
358
359 /* Bridging. */
360 struct netflow *netflow;
361 struct ofproto_sflow *sflow;
362 struct hmap bundles; /* Contains "struct ofbundle"s. */
363 struct mac_learning *ml;
364 struct ofmirror *mirrors[MAX_MIRRORS];
365 bool has_bonded_bundles;
366
367 /* Flow table. */
368 struct classifier cls;
369 struct timer next_expiration;
370
371 /* Facets. */
372 struct hmap facets;
373 bool need_revalidate;
374 struct tag_set revalidate_set;
375
376 /* OpenFlow connections. */
377 struct connmgr *connmgr;
378 };
379
380 /* Map from dpif name to struct ofproto, for use by unixctl commands. */
381 static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos);
382
383 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
384
385 static uint64_t pick_datapath_id(const struct ofproto *);
386 static uint64_t pick_fallback_dpid(void);
387
388 static void ofproto_flush_flows__(struct ofproto *);
389 static int ofproto_expire(struct ofproto *);
390 static void flow_push_stats(struct ofproto *, const struct rule *,
391 struct flow *, uint64_t packets, uint64_t bytes,
392 long long int used);
393
394 static void handle_upcall(struct ofproto *, struct dpif_upcall *);
395
396 static void handle_openflow(struct ofconn *, struct ofpbuf *);
397
398 static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
399 static void update_port(struct ofproto *, const char *devname);
400 static int init_ports(struct ofproto *);
401 static void reinit_ports(struct ofproto *);
402
403 static void update_learning_table(struct ofproto *,
404 const struct flow *, int vlan,
405 struct ofbundle *);
406 static bool is_admissible(struct ofproto *, const struct flow *,
407 bool have_packet, tag_type *, int *vlanp,
408 struct ofbundle **in_bundlep);
409
410 static void ofproto_unixctl_init(void);
411
412 int
413 ofproto_create(const char *datapath, const char *datapath_type,
414 struct ofproto **ofprotop)
415 {
416 char local_name[IF_NAMESIZE];
417 struct ofproto *p;
418 struct dpif *dpif;
419 int error;
420 int i;
421
422 *ofprotop = NULL;
423
424 ofproto_unixctl_init();
425
426 /* Connect to datapath and start listening for messages. */
427 error = dpif_create_and_open(datapath, datapath_type, &dpif);
428 if (error) {
429 VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
430 return error;
431 }
432 error = dpif_recv_set_mask(dpif,
433 ((1u << DPIF_UC_MISS) |
434 (1u << DPIF_UC_ACTION) |
435 (1u << DPIF_UC_SAMPLE)));
436 if (error) {
437 VLOG_ERR("failed to listen on datapath %s: %s",
438 datapath, strerror(error));
439 dpif_close(dpif);
440 return error;
441 }
442 dpif_flow_flush(dpif);
443 dpif_recv_purge(dpif);
444
445 error = dpif_port_get_name(dpif, ODPP_LOCAL,
446 local_name, sizeof local_name);
447 if (error) {
448 VLOG_ERR("%s: cannot get name of datapath local port (%s)",
449 datapath, strerror(error));
450 return error;
451 }
452
453 /* Initialize settings. */
454 p = xzalloc(sizeof *p);
455 p->name = xstrdup(dpif_name(dpif));
456 hmap_insert(&all_ofprotos, &p->hmap_node, hash_string(p->name, 0));
457 p->fallback_dpid = pick_fallback_dpid();
458 p->datapath_id = p->fallback_dpid;
459 p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
460 p->hw_desc = xstrdup(DEFAULT_HW_DESC);
461 p->sw_desc = xstrdup(DEFAULT_SW_DESC);
462 p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
463 p->dp_desc = xstrdup(DEFAULT_DP_DESC);
464
465 /* Initialize datapath. */
466 p->dpif = dpif;
467 p->netdev_monitor = netdev_monitor_create();
468 hmap_init(&p->ports);
469 shash_init(&p->port_by_name);
470 p->max_ports = dpif_get_max_ports(dpif);
471
472 /* Initialize bridging. */
473 p->netflow = NULL;
474 p->sflow = NULL;
475 hmap_init(&p->bundles);
476 p->ml = mac_learning_create();
477 for (i = 0; i < MAX_MIRRORS; i++) {
478 p->mirrors[i] = NULL;
479 }
480 p->has_bonded_bundles = false;
481
482 /* Initialize flow table. */
483 classifier_init(&p->cls);
484 timer_set_duration(&p->next_expiration, 1000);
485
486 /* Initialize facet table. */
487 hmap_init(&p->facets);
488 p->need_revalidate = false;
489 tag_set_init(&p->revalidate_set);
490
491 /* Pick final datapath ID. */
492 p->datapath_id = pick_datapath_id(p);
493 VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
494
495 /* Initialize OpenFlow connections. */
496 p->connmgr = connmgr_create(p, datapath, local_name);
497
498 init_ports(p);
499
500 *ofprotop = p;
501 return 0;
502 }
503
504 void
505 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
506 {
507 uint64_t old_dpid = p->datapath_id;
508 p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
509 if (p->datapath_id != old_dpid) {
510 VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
511
512 /* Force all active connections to reconnect, since there is no way to
513 * notify a controller that the datapath ID has changed. */
514 ofproto_reconnect_controllers(p);
515 }
516 }
517
518 void
519 ofproto_set_controllers(struct ofproto *p,
520 const struct ofproto_controller *controllers,
521 size_t n_controllers)
522 {
523 connmgr_set_controllers(p->connmgr, controllers, n_controllers);
524 }
525
526 void
527 ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode)
528 {
529 connmgr_set_fail_mode(p->connmgr, fail_mode);
530 }
531
532 /* Drops the connections between 'ofproto' and all of its controllers, forcing
533 * them to reconnect. */
534 void
535 ofproto_reconnect_controllers(struct ofproto *ofproto)
536 {
537 connmgr_reconnect(ofproto->connmgr);
538 }
539
540 /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s
541 * in-band control should guarantee access, in the same way that in-band
542 * control guarantees access to OpenFlow controllers. */
543 void
544 ofproto_set_extra_in_band_remotes(struct ofproto *ofproto,
545 const struct sockaddr_in *extras, size_t n)
546 {
547 connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n);
548 }
549
550 /* Sets the OpenFlow queue used by flows set up by in-band control on
551 * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control
552 * flows will use the default queue. */
553 void
554 ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id)
555 {
556 connmgr_set_in_band_queue(ofproto->connmgr, queue_id);
557 }
558
559 void
560 ofproto_set_desc(struct ofproto *p,
561 const char *mfr_desc, const char *hw_desc,
562 const char *sw_desc, const char *serial_desc,
563 const char *dp_desc)
564 {
565 struct ofp_desc_stats *ods;
566
567 if (mfr_desc) {
568 if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
569 VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
570 sizeof ods->mfr_desc);
571 }
572 free(p->mfr_desc);
573 p->mfr_desc = xstrdup(mfr_desc);
574 }
575 if (hw_desc) {
576 if (strlen(hw_desc) >= sizeof ods->hw_desc) {
577 VLOG_WARN("truncating hw_desc, must be less than %zu characters",
578 sizeof ods->hw_desc);
579 }
580 free(p->hw_desc);
581 p->hw_desc = xstrdup(hw_desc);
582 }
583 if (sw_desc) {
584 if (strlen(sw_desc) >= sizeof ods->sw_desc) {
585 VLOG_WARN("truncating sw_desc, must be less than %zu characters",
586 sizeof ods->sw_desc);
587 }
588 free(p->sw_desc);
589 p->sw_desc = xstrdup(sw_desc);
590 }
591 if (serial_desc) {
592 if (strlen(serial_desc) >= sizeof ods->serial_num) {
593 VLOG_WARN("truncating serial_desc, must be less than %zu "
594 "characters",
595 sizeof ods->serial_num);
596 }
597 free(p->serial_desc);
598 p->serial_desc = xstrdup(serial_desc);
599 }
600 if (dp_desc) {
601 if (strlen(dp_desc) >= sizeof ods->dp_desc) {
602 VLOG_WARN("truncating dp_desc, must be less than %zu characters",
603 sizeof ods->dp_desc);
604 }
605 free(p->dp_desc);
606 p->dp_desc = xstrdup(dp_desc);
607 }
608 }
609
610 int
611 ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops)
612 {
613 return connmgr_set_snoops(ofproto->connmgr, snoops);
614 }
615
616 int
617 ofproto_set_netflow(struct ofproto *ofproto,
618 const struct netflow_options *nf_options)
619 {
620 if (nf_options && !sset_is_empty(&nf_options->collectors)) {
621 if (!ofproto->netflow) {
622 ofproto->netflow = netflow_create();
623 }
624 return netflow_set_options(ofproto->netflow, nf_options);
625 } else {
626 netflow_destroy(ofproto->netflow);
627 ofproto->netflow = NULL;
628 return 0;
629 }
630 }
631
632 void
633 ofproto_set_sflow(struct ofproto *ofproto,
634 const struct ofproto_sflow_options *oso)
635 {
636 struct ofproto_sflow *os = ofproto->sflow;
637 if (oso) {
638 if (!os) {
639 struct ofport *ofport;
640
641 os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
642 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
643 ofproto_sflow_add_port(os, ofport->odp_port,
644 netdev_get_name(ofport->netdev));
645 }
646 }
647 ofproto_sflow_set_options(os, oso);
648 } else {
649 ofproto_sflow_destroy(os);
650 ofproto->sflow = NULL;
651 }
652 }
653 \f
654 /* Connectivity Fault Management configuration. */
655
656 /* Clears the CFM configuration from 'port_no' on 'ofproto'. */
657 void
658 ofproto_port_clear_cfm(struct ofproto *ofproto, uint32_t port_no)
659 {
660 struct ofport *ofport = get_port(ofproto, port_no);
661 if (ofport && ofport->cfm){
662 cfm_destroy(ofport->cfm);
663 ofport->cfm = NULL;
664 }
665 }
666
667 /* Configures connectivity fault management on 'port_no' in 'ofproto'. Takes
668 * basic configuration from the configuration members in 'cfm', and the set of
669 * remote maintenance points from the 'n_remote_mps' elements in 'remote_mps'.
670 * Ignores the statistics members of 'cfm'.
671 *
672 * This function has no effect if 'ofproto' does not have a port 'port_no'. */
673 void
674 ofproto_port_set_cfm(struct ofproto *ofproto, uint32_t port_no,
675 const struct cfm *cfm,
676 const uint16_t *remote_mps, size_t n_remote_mps)
677 {
678 struct ofport *ofport;
679
680 ofport = get_port(ofproto, port_no);
681 if (!ofport) {
682 VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32,
683 ofproto->name, port_no);
684 return;
685 }
686
687 if (!ofport->cfm) {
688 ofport->cfm = cfm_create();
689 }
690
691 ofport->cfm->mpid = cfm->mpid;
692 ofport->cfm->interval = cfm->interval;
693 memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
694
695 cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
696
697 if (!cfm_configure(ofport->cfm)) {
698 VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed",
699 ofproto->name, port_no,
700 netdev_get_name(ofport->netdev));
701 cfm_destroy(ofport->cfm);
702 ofport->cfm = NULL;
703 }
704 }
705
706 /* Returns the connectivity fault management object associated with 'port_no'
707 * within 'ofproto', or a null pointer if 'ofproto' does not have a port
708 * 'port_no' or if that port does not have CFM configured. The caller must not
709 * modify or destroy the returned object. */
710 const struct cfm *
711 ofproto_port_get_cfm(struct ofproto *ofproto, uint32_t port_no)
712 {
713 struct ofport *ofport = get_port(ofproto, port_no);
714 return ofport ? ofport->cfm : NULL;
715 }
716
717 /* Checks the status of LACP negotiation for 'ofp_port' within ofproto.
718 * Returns 1 if LACP partner information for 'ofp_port' is up-to-date,
719 * 0 if LACP partner information is not current (generally indicating a
720 * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */
721 int
722 ofproto_port_is_lacp_current(struct ofproto *ofproto, uint16_t ofp_port)
723 {
724 struct ofport *ofport = get_port(ofproto, ofp_port_to_odp_port(ofp_port));
725 return (ofport && ofport->bundle && ofport->bundle->lacp
726 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
727 : -1);
728 }
729 \f
730 /* Bundles. */
731
732 /* Expires all MAC learning entries associated with 'port' and forces ofproto
733 * to revalidate every flow. */
734 static void
735 ofproto_bundle_flush_macs(struct ofbundle *bundle)
736 {
737 struct ofproto *ofproto = bundle->ofproto;
738 struct mac_learning *ml = ofproto->ml;
739 struct mac_entry *mac, *next_mac;
740
741 ofproto->need_revalidate = true;
742 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
743 if (mac->port.p == bundle) {
744 mac_learning_expire(ml, mac);
745 }
746 }
747 }
748
749 static struct ofbundle *
750 ofproto_bundle_lookup(const struct ofproto *ofproto, void *aux)
751 {
752 struct ofbundle *bundle;
753
754 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
755 &ofproto->bundles) {
756 if (bundle->aux == aux) {
757 return bundle;
758 }
759 }
760 return NULL;
761 }
762
763 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
764 * ones that are found to 'bundles'. */
765 static void
766 ofproto_bundle_lookup_multiple(struct ofproto *ofproto,
767 void **auxes, size_t n_auxes,
768 struct hmapx *bundles)
769 {
770 size_t i;
771
772 hmapx_init(bundles);
773 for (i = 0; i < n_auxes; i++) {
774 struct ofbundle *bundle = ofproto_bundle_lookup(ofproto, auxes[i]);
775 if (bundle) {
776 hmapx_add(bundles, bundle);
777 }
778 }
779 }
780
781 static void
782 ofproto_bundle_del_port(struct ofport *port)
783 {
784 struct ofbundle *bundle = port->bundle;
785
786 list_remove(&port->bundle_node);
787 port->bundle = NULL;
788
789 if (bundle->lacp) {
790 lacp_slave_unregister(bundle->lacp, port);
791 }
792 if (bundle->bond) {
793 bond_slave_unregister(bundle->bond, port);
794 }
795
796 bundle->floodable = true;
797 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
798 if (port->opp.config & htonl(OFPPC_NO_FLOOD)) {
799 bundle->floodable = false;
800 }
801 }
802 }
803
804 static bool
805 ofproto_bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
806 struct lacp_slave_settings *lacp)
807 {
808 struct ofport *port;
809
810 port = get_port(bundle->ofproto, ofp_port_to_odp_port(ofp_port));
811 if (!port) {
812 return false;
813 }
814
815 if (port->bundle != bundle) {
816 if (port->bundle) {
817 ofproto_bundle_del_port(port);
818 }
819
820 port->bundle = bundle;
821 list_push_back(&bundle->ports, &port->bundle_node);
822 if (port->opp.config & htonl(OFPPC_NO_FLOOD)) {
823 bundle->floodable = false;
824 }
825 }
826
827 if (lacp) {
828 lacp_slave_register(bundle->lacp, port, lacp);
829 }
830
831 return true;
832 }
833
834 void
835 ofproto_bundle_register(struct ofproto *ofproto, void *aux,
836 const struct ofproto_bundle_settings *s)
837 {
838 bool need_flush = false;
839 const unsigned long *trunks;
840 struct ofbundle *bundle;
841 struct ofport *port;
842 size_t i;
843 bool ok;
844
845 assert(s->n_slaves == 1 || s->bond != NULL);
846 assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
847
848 bundle = ofproto_bundle_lookup(ofproto, aux);
849 if (!bundle) {
850 bundle = xmalloc(sizeof *bundle);
851
852 bundle->ofproto = ofproto;
853 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
854 hash_pointer(aux, 0));
855 bundle->aux = aux;
856 bundle->name = NULL;
857
858 list_init(&bundle->ports);
859 bundle->vlan = -1;
860 bundle->trunks = NULL;
861 bundle->bond = NULL;
862 bundle->lacp = NULL;
863
864 bundle->floodable = true;
865
866 bundle->src_mirrors = 0;
867 bundle->dst_mirrors = 0;
868 bundle->mirror_out = 0;
869 }
870
871 if (!bundle->name || strcmp(s->name, bundle->name)) {
872 free(bundle->name);
873 bundle->name = xstrdup(s->name);
874 }
875
876 /* LACP. */
877 if (s->lacp) {
878 if (!bundle->lacp) {
879 bundle->lacp = lacp_create();
880 }
881 lacp_configure(bundle->lacp, s->lacp);
882 } else {
883 lacp_destroy(bundle->lacp);
884 bundle->lacp = NULL;
885 }
886
887 /* Update set of ports. */
888 ok = true;
889 for (i = 0; i < s->n_slaves; i++) {
890 if (!ofproto_bundle_add_port(bundle, s->slaves[i],
891 s->lacp ? &s->lacp_slaves[i] : NULL)) {
892 ok = false;
893 }
894 }
895 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
896 struct ofport *next_port;
897
898 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
899 for (i = 0; i < s->n_slaves; i++) {
900 if (s->slaves[i] == odp_port_to_ofp_port(port->odp_port)) {
901 goto found;
902 }
903 }
904
905 ofproto_bundle_del_port(port);
906 found: ;
907 }
908 }
909 assert(list_size(&bundle->ports) <= s->n_slaves);
910
911 if (list_is_empty(&bundle->ports)) {
912 ofproto_bundle_unregister(ofproto, aux);
913 return;
914 }
915
916 /* Set VLAN tag. */
917 if (s->vlan != bundle->vlan) {
918 bundle->vlan = s->vlan;
919 need_flush = true;
920 }
921
922 /* Get trunked VLANs. */
923 trunks = s->vlan == -1 ? NULL : s->trunks;
924 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
925 free(bundle->trunks);
926 bundle->trunks = vlan_bitmap_clone(trunks);
927 need_flush = true;
928 }
929
930 /* Bonding. */
931 if (!list_is_short(&bundle->ports)) {
932 bundle->ofproto->has_bonded_bundles = true;
933 if (bundle->bond) {
934 if (bond_reconfigure(bundle->bond, s->bond)) {
935 ofproto->need_revalidate = true;
936 }
937 } else {
938 bundle->bond = bond_create(s->bond);
939 }
940
941 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
942 uint16_t stable_id = (bundle->lacp
943 ? lacp_slave_get_port_id(bundle->lacp, port)
944 : port->odp_port);
945 bond_slave_register(bundle->bond, port, stable_id, port->netdev);
946 }
947 } else {
948 bond_destroy(bundle->bond);
949 bundle->bond = NULL;
950 }
951
952 /* If we changed something that would affect MAC learning, un-learn
953 * everything on this port and force flow revalidation. */
954 if (need_flush) {
955 ofproto_bundle_flush_macs(bundle);
956 }
957 }
958
959 static void
960 ofproto_bundle_destroy(struct ofbundle *bundle)
961 {
962 struct ofproto *ofproto;
963 struct ofport *port, *next_port;
964 int i;
965
966 if (!bundle) {
967 return;
968 }
969
970 ofproto = bundle->ofproto;
971 for (i = 0; i < MAX_MIRRORS; i++) {
972 struct ofmirror *m = ofproto->mirrors[i];
973 if (m) {
974 if (m->out == bundle) {
975 ofproto_mirror_destroy(m);
976 } else if (hmapx_find_and_delete(&m->srcs, bundle)
977 || hmapx_find_and_delete(&m->dsts, bundle)) {
978 ofproto->need_revalidate = true;
979 }
980 }
981 }
982
983 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
984 ofproto_bundle_del_port(port);
985 }
986
987 ofproto_bundle_flush_macs(bundle);
988 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
989 free(bundle->name);
990 free(bundle->trunks);
991 bond_destroy(bundle->bond);
992 lacp_destroy(bundle->lacp);
993 free(bundle);
994 }
995
996 void
997 ofproto_bundle_unregister(struct ofproto *ofproto, void *aux)
998 {
999 ofproto_bundle_destroy(ofproto_bundle_lookup(ofproto, aux));
1000 }
1001
1002 static void
1003 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1004 {
1005 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1006 struct ofport *port = port_;
1007 uint8_t ea[ETH_ADDR_LEN];
1008 int error;
1009
1010 error = netdev_get_etheraddr(port->netdev, ea);
1011 if (!error) {
1012 struct lacp_pdu *packet_pdu;
1013 struct ofpbuf packet;
1014
1015 ofpbuf_init(&packet, 0);
1016 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1017 sizeof *packet_pdu);
1018 *packet_pdu = *pdu;
1019 error = netdev_send(port->netdev, &packet);
1020 if (error) {
1021 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1022 "(%s)", port->bundle->name,
1023 netdev_get_name(port->netdev), strerror(error));
1024 }
1025 ofpbuf_uninit(&packet);
1026 } else {
1027 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1028 "%s (%s)", port->bundle->name,
1029 netdev_get_name(port->netdev), strerror(error));
1030 }
1031 }
1032
1033 static void
1034 ofproto_bundle_send_learning_packets(struct ofbundle *bundle)
1035 {
1036 struct ofproto *ofproto = bundle->ofproto;
1037 int error, n_packets, n_errors;
1038 struct mac_entry *e;
1039
1040 error = n_packets = n_errors = 0;
1041 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1042 if (e->port.p != bundle) {
1043 int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1044 if (ret) {
1045 error = ret;
1046 n_errors++;
1047 }
1048 n_packets++;
1049 }
1050 }
1051
1052 if (n_errors) {
1053 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1054 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1055 "packets, last error was: %s",
1056 bundle->name, n_errors, n_packets, strerror(error));
1057 } else {
1058 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1059 bundle->name, n_packets);
1060 }
1061 }
1062
1063 static void
1064 ofproto_bundle_run(struct ofbundle *bundle)
1065 {
1066 if (bundle->lacp) {
1067 lacp_run(bundle->lacp, send_pdu_cb);
1068 }
1069 if (bundle->bond) {
1070 struct ofport *port;
1071
1072 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1073 bool may_enable = lacp_slave_may_enable(bundle->lacp, port);
1074 bond_slave_set_lacp_may_enable(bundle->bond, port, may_enable);
1075 }
1076
1077 bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1078 lacp_negotiated(bundle->lacp));
1079 if (bond_should_send_learning_packets(bundle->bond)) {
1080 ofproto_bundle_send_learning_packets(bundle);
1081 }
1082 }
1083 }
1084
1085 static void
1086 ofproto_bundle_wait(struct ofbundle *bundle)
1087 {
1088 if (bundle->lacp) {
1089 lacp_wait(bundle->lacp);
1090 }
1091 if (bundle->bond) {
1092 bond_wait(bundle->bond);
1093 }
1094 }
1095 \f
1096 static int
1097 ofproto_mirror_scan(struct ofproto *ofproto)
1098 {
1099 int idx;
1100
1101 for (idx = 0; idx < MAX_MIRRORS; idx++) {
1102 if (!ofproto->mirrors[idx]) {
1103 return idx;
1104 }
1105 }
1106 return -1;
1107 }
1108
1109 static struct ofmirror *
1110 ofproto_mirror_lookup(struct ofproto *ofproto, void *aux)
1111 {
1112 int i;
1113
1114 for (i = 0; i < MAX_MIRRORS; i++) {
1115 struct ofmirror *mirror = ofproto->mirrors[i];
1116 if (mirror && mirror->aux == aux) {
1117 return mirror;
1118 }
1119 }
1120
1121 return NULL;
1122 }
1123
1124 void
1125 ofproto_mirror_register(struct ofproto *ofproto, void *aux,
1126 const struct ofproto_mirror_settings *s)
1127 {
1128 mirror_mask_t mirror_bit;
1129 struct ofbundle *bundle;
1130 struct ofmirror *mirror;
1131 struct ofbundle *out;
1132 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
1133 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
1134 int out_vlan;
1135
1136 mirror = ofproto_mirror_lookup(ofproto, aux);
1137 if (!mirror) {
1138 int idx;
1139
1140 idx = ofproto_mirror_scan(ofproto);
1141 if (idx < 0) {
1142 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1143 "cannot create %s",
1144 ofproto->name, MAX_MIRRORS, s->name);
1145 return;
1146 }
1147
1148 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1149 mirror->ofproto = ofproto;
1150 mirror->idx = idx;
1151 mirror->out_vlan = -1;
1152 mirror->name = NULL;
1153 }
1154
1155 if (!mirror->name || strcmp(s->name, mirror->name)) {
1156 free(mirror->name);
1157 mirror->name = xstrdup(s->name);
1158 }
1159
1160 /* Get the new configuration. */
1161 if (s->out_bundle) {
1162 out = ofproto_bundle_lookup(ofproto, s->out_bundle);
1163 if (!out) {
1164 ofproto_mirror_destroy(mirror);
1165 return;
1166 }
1167 out_vlan = -1;
1168 } else {
1169 out = NULL;
1170 out_vlan = s->out_vlan;
1171 }
1172 ofproto_bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1173 ofproto_bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1174
1175 /* If the configuration has not changed, do nothing. */
1176 if (hmapx_equals(&srcs, &mirror->srcs)
1177 && hmapx_equals(&dsts, &mirror->dsts)
1178 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1179 && mirror->out == out
1180 && mirror->out_vlan == out_vlan)
1181 {
1182 hmapx_destroy(&srcs);
1183 hmapx_destroy(&dsts);
1184 return;
1185 }
1186
1187 hmapx_swap(&srcs, &mirror->srcs);
1188 hmapx_destroy(&srcs);
1189
1190 hmapx_swap(&dsts, &mirror->dsts);
1191 hmapx_destroy(&dsts);
1192
1193 free(mirror->vlans);
1194 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1195
1196 mirror->out = out;
1197 mirror->out_vlan = out_vlan;
1198
1199 /* Update bundles. */
1200 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1201 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1202 if (hmapx_contains(&mirror->srcs, bundle)) {
1203 bundle->src_mirrors |= mirror_bit;
1204 } else {
1205 bundle->src_mirrors &= ~mirror_bit;
1206 }
1207
1208 if (hmapx_contains(&mirror->dsts, bundle)) {
1209 bundle->dst_mirrors |= mirror_bit;
1210 } else {
1211 bundle->dst_mirrors &= ~mirror_bit;
1212 }
1213
1214 if (mirror->out == bundle) {
1215 bundle->mirror_out |= mirror_bit;
1216 } else {
1217 bundle->mirror_out &= ~mirror_bit;
1218 }
1219 }
1220
1221 ofproto->need_revalidate = true;
1222 mac_learning_flush(ofproto->ml);
1223 }
1224
1225 static void
1226 ofproto_mirror_destroy(struct ofmirror *mirror)
1227 {
1228 mirror_mask_t mirror_bit;
1229 struct ofbundle *bundle;
1230 struct ofproto *ofproto;
1231
1232 if (!mirror) {
1233 return;
1234 }
1235
1236 ofproto = mirror->ofproto;
1237 ofproto->need_revalidate = true;
1238 mac_learning_flush(ofproto->ml);
1239
1240 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1241 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1242 bundle->src_mirrors &= ~mirror_bit;
1243 bundle->dst_mirrors &= ~mirror_bit;
1244 bundle->mirror_out &= ~mirror_bit;
1245 }
1246
1247 hmapx_destroy(&mirror->srcs);
1248 hmapx_destroy(&mirror->dsts);
1249 free(mirror->vlans);
1250
1251 ofproto->mirrors[mirror->idx] = NULL;
1252 free(mirror->name);
1253 free(mirror);
1254 }
1255
1256 void
1257 ofproto_mirror_unregister(struct ofproto *ofproto, void *aux)
1258 {
1259 ofproto_mirror_destroy(ofproto_mirror_lookup(ofproto, aux));
1260 }
1261
1262 void
1263 ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans)
1264 {
1265 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1266 ofproto->need_revalidate = true;
1267 mac_learning_flush(ofproto->ml);
1268 }
1269 }
1270
1271 bool
1272 ofproto_is_mirror_output_bundle(struct ofproto *ofproto, void *aux)
1273 {
1274 struct ofbundle *bundle = ofproto_bundle_lookup(ofproto, aux);
1275 return bundle && bundle->mirror_out != 0;
1276 }
1277 \f
1278 bool
1279 ofproto_has_snoops(const struct ofproto *ofproto)
1280 {
1281 return connmgr_has_snoops(ofproto->connmgr);
1282 }
1283
1284 void
1285 ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops)
1286 {
1287 connmgr_get_snoops(ofproto->connmgr, snoops);
1288 }
1289
1290 void
1291 ofproto_destroy(struct ofproto *p)
1292 {
1293 struct ofport *ofport, *next_ofport;
1294 int i;
1295
1296 if (!p) {
1297 return;
1298 }
1299
1300 hmap_remove(&all_ofprotos, &p->hmap_node);
1301
1302 for (i = 0; i < MAX_MIRRORS; i++) {
1303 ofproto_mirror_destroy(p->mirrors[i]);
1304 }
1305 ofproto_flush_flows__(p);
1306 connmgr_destroy(p->connmgr);
1307 classifier_destroy(&p->cls);
1308 hmap_destroy(&p->facets);
1309
1310 dpif_close(p->dpif);
1311
1312 netdev_monitor_destroy(p->netdev_monitor);
1313 HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
1314 hmap_remove(&p->ports, &ofport->hmap_node);
1315 ofport_free(ofport);
1316 }
1317 shash_destroy(&p->port_by_name);
1318
1319 netflow_destroy(p->netflow);
1320 ofproto_sflow_destroy(p->sflow);
1321
1322 free(p->mfr_desc);
1323 free(p->hw_desc);
1324 free(p->sw_desc);
1325 free(p->serial_desc);
1326 free(p->dp_desc);
1327
1328 hmap_destroy(&p->ports);
1329
1330 free(p->name);
1331 free(p);
1332 }
1333
1334 static void
1335 process_port_change(struct ofproto *ofproto, int error, char *devname)
1336 {
1337 if (error == ENOBUFS) {
1338 reinit_ports(ofproto);
1339 } else if (!error) {
1340 update_port(ofproto, devname);
1341 free(devname);
1342 }
1343 }
1344
1345 int
1346 ofproto_run(struct ofproto *p)
1347 {
1348 struct ofbundle *bundle;
1349 struct ofport *ofport;
1350 char *devname;
1351 int error;
1352 int i;
1353
1354 for (i = 0; i < 50; i++) {
1355 struct dpif_upcall packet;
1356
1357 error = dpif_recv(p->dpif, &packet);
1358 if (error) {
1359 if (error == ENODEV) {
1360 /* Someone destroyed the datapath behind our back. The caller
1361 * better destroy us and give up, because we're just going to
1362 * spin from here on out. */
1363 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
1364 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
1365 p->name);
1366 return ENODEV;
1367 }
1368 break;
1369 }
1370
1371 handle_upcall(p, &packet);
1372 }
1373
1374 while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
1375 process_port_change(p, error, devname);
1376 }
1377 while ((error = netdev_monitor_poll(p->netdev_monitor,
1378 &devname)) != EAGAIN) {
1379 process_port_change(p, error, devname);
1380 }
1381
1382 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1383 ofport_run(ofport);
1384 }
1385
1386 HMAP_FOR_EACH (bundle, hmap_node, &p->bundles) {
1387 ofproto_bundle_run(bundle);
1388 }
1389
1390 connmgr_run(p->connmgr, handle_openflow);
1391
1392 if (timer_expired(&p->next_expiration)) {
1393 int delay = ofproto_expire(p);
1394 timer_set_duration(&p->next_expiration, delay);
1395 COVERAGE_INC(ofproto_expiration);
1396 }
1397
1398 if (p->netflow) {
1399 netflow_run(p->netflow);
1400 }
1401 if (p->sflow) {
1402 ofproto_sflow_run(p->sflow);
1403 }
1404
1405 /* Now revalidate if there's anything to do. */
1406 if (p->need_revalidate || !tag_set_is_empty(&p->revalidate_set)) {
1407 struct tag_set revalidate_set = p->revalidate_set;
1408 bool revalidate_all = p->need_revalidate;
1409 struct facet *facet, *next;
1410
1411 /* Clear the revalidation flags. */
1412 tag_set_init(&p->revalidate_set);
1413 p->need_revalidate = false;
1414
1415 HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &p->facets) {
1416 if (revalidate_all
1417 || tag_set_intersects(&revalidate_set, facet->tags)) {
1418 facet_revalidate(p, facet);
1419 }
1420 }
1421 }
1422
1423 return 0;
1424 }
1425
1426 void
1427 ofproto_wait(struct ofproto *p)
1428 {
1429 struct ofbundle *bundle;
1430 struct ofport *ofport;
1431
1432 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1433 ofport_wait(ofport);
1434 }
1435 HMAP_FOR_EACH (bundle, hmap_node, &p->bundles) {
1436 ofproto_bundle_wait(bundle);
1437 }
1438 dpif_recv_wait(p->dpif);
1439 dpif_port_poll_wait(p->dpif);
1440 netdev_monitor_poll_wait(p->netdev_monitor);
1441 if (p->sflow) {
1442 ofproto_sflow_wait(p->sflow);
1443 }
1444 if (!tag_set_is_empty(&p->revalidate_set)) {
1445 poll_immediate_wake();
1446 }
1447 if (p->need_revalidate) {
1448 /* Shouldn't happen, but if it does just go around again. */
1449 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1450 poll_immediate_wake();
1451 } else {
1452 timer_wait(&p->next_expiration);
1453 }
1454 connmgr_wait(p->connmgr);
1455 }
1456
1457 bool
1458 ofproto_is_alive(const struct ofproto *p)
1459 {
1460 return connmgr_has_controllers(p->connmgr);
1461 }
1462
1463 void
1464 ofproto_get_ofproto_controller_info(const struct ofproto *ofproto,
1465 struct shash *info)
1466 {
1467 connmgr_get_controller_info(ofproto->connmgr, info);
1468 }
1469
1470 void
1471 ofproto_free_ofproto_controller_info(struct shash *info)
1472 {
1473 struct shash_node *node;
1474
1475 SHASH_FOR_EACH (node, info) {
1476 struct ofproto_controller_info *cinfo = node->data;
1477 while (cinfo->pairs.n) {
1478 free((char *) cinfo->pairs.values[--cinfo->pairs.n]);
1479 }
1480 free(cinfo);
1481 }
1482 shash_destroy(info);
1483 }
1484
1485 /* Makes a deep copy of 'old' into 'port'. */
1486 void
1487 ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old)
1488 {
1489 port->name = xstrdup(old->name);
1490 port->type = xstrdup(old->type);
1491 port->ofp_port = old->ofp_port;
1492 }
1493
1494 /* Frees memory allocated to members of 'ofproto_port'.
1495 *
1496 * Do not call this function on an ofproto_port obtained from
1497 * ofproto_port_dump_next(): that function retains ownership of the data in the
1498 * ofproto_port. */
1499 void
1500 ofproto_port_destroy(struct ofproto_port *ofproto_port)
1501 {
1502 free(ofproto_port->name);
1503 free(ofproto_port->type);
1504 }
1505
1506 /* Converts a dpif_port into an ofproto_port.
1507 *
1508 * This only makes a shallow copy, so make sure that the dpif_port doesn't get
1509 * freed while the ofproto_port is still in use. You can choose to free the
1510 * ofproto_port instead of the dpif_port. */
1511 static void
1512 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1513 struct dpif_port *dpif_port)
1514 {
1515 ofproto_port->name = dpif_port->name;
1516 ofproto_port->type = dpif_port->type;
1517 ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1518 }
1519
1520 /* Initializes 'dump' to begin dumping the ports in an ofproto.
1521 *
1522 * This function provides no status indication. An error status for the entire
1523 * dump operation is provided when it is completed by calling
1524 * ofproto_port_dump_done().
1525 */
1526 void
1527 ofproto_port_dump_start(struct ofproto_port_dump *dump,
1528 const struct ofproto *ofproto)
1529 {
1530 struct dpif_port_dump *dpif_dump;
1531
1532 dump->state = dpif_dump = xmalloc(sizeof *dpif_dump);
1533 dpif_port_dump_start(dpif_dump, ofproto->dpif);
1534 }
1535
1536 /* Attempts to retrieve another port from 'dump', which must have been created
1537 * with ofproto_port_dump_start(). On success, stores a new ofproto_port into
1538 * 'port' and returns true. On failure, returns false.
1539 *
1540 * Failure might indicate an actual error or merely that the last port has been
1541 * dumped. An error status for the entire dump operation is provided when it
1542 * is completed by calling ofproto_port_dump_done().
1543 *
1544 * The ofproto owns the data stored in 'port'. It will remain valid until at
1545 * least the next time 'dump' is passed to ofproto_port_dump_next() or
1546 * ofproto_port_dump_done(). */
1547 bool
1548 ofproto_port_dump_next(struct ofproto_port_dump *dump,
1549 struct ofproto_port *port)
1550 {
1551 struct dpif_port_dump *dpif_dump = dump->state;
1552 struct dpif_port dpif_port;
1553 bool ok;
1554
1555 ok = dpif_port_dump_next(dpif_dump, &dpif_port);
1556 if (ok) {
1557 ofproto_port_from_dpif_port(port, &dpif_port);
1558 }
1559 return ok;
1560 }
1561
1562 /* Completes port table dump operation 'dump', which must have been created
1563 * with ofproto_port_dump_start(). Returns 0 if the dump operation was
1564 * error-free, otherwise a positive errno value describing the problem. */
1565 int
1566 ofproto_port_dump_done(struct ofproto_port_dump *dump)
1567 {
1568 struct dpif_port_dump *dpif_dump = dump->state;
1569 int error = dpif_port_dump_done(dpif_dump);
1570 free(dpif_dump);
1571 return error;
1572 }
1573
1574 /* Attempts to add 'netdev' as a port on 'ofproto'. If successful, returns 0
1575 * and sets '*ofp_portp' to the new port's port OpenFlow number (if 'ofp_portp'
1576 * is non-null). On failure, returns a positive errno value and sets
1577 * '*ofp_portp' to OFPP_NONE (if 'ofp_portp' is non-null). */
1578 int
1579 ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev,
1580 uint16_t *ofp_portp)
1581 {
1582 uint16_t odp_port;
1583 int error;
1584
1585 error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1586 if (!error) {
1587 update_port(ofproto, netdev_get_name(netdev));
1588 }
1589 if (ofp_portp) {
1590 *ofp_portp = error ? OFPP_NONE : odp_port_to_ofp_port(odp_port);
1591 }
1592 return error;
1593 }
1594
1595 /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and
1596 * initializes '*port' appropriately; on failure, returns a positive errno
1597 * value.
1598 *
1599 * The caller owns the data in 'port' and must free it with
1600 * ofproto_port_destroy() when it is no longer needed. */
1601 int
1602 ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname,
1603 struct ofproto_port *port)
1604 {
1605 struct dpif_port dpif_port;
1606 int error;
1607
1608 error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1609 if (!error) {
1610 ofproto_port_from_dpif_port(port, &dpif_port);
1611 }
1612 return error;
1613 }
1614
1615 /* Deletes port number 'ofp_port' from the datapath for 'ofproto'.
1616 *
1617 * This is almost the same as calling dpif_port_del() directly on the
1618 * datapath, but it also makes 'ofproto' close its open netdev for the port
1619 * (if any). This makes it possible to create a new netdev of a different
1620 * type under the same name, which otherwise the netdev library would refuse
1621 * to do because of the conflict. (The netdev would eventually get closed on
1622 * the next trip through ofproto_run(), but this interface is more direct.)
1623 *
1624 * Returns 0 if successful, otherwise a positive errno. */
1625 int
1626 ofproto_port_del(struct ofproto *ofproto, uint16_t ofp_port)
1627 {
1628 uint32_t odp_port = ofp_port_to_odp_port(ofp_port);
1629 struct ofport *ofport = get_port(ofproto, odp_port);
1630 const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>";
1631 int error;
1632
1633 error = dpif_port_del(ofproto->dpif, odp_port);
1634 if (error) {
1635 VLOG_ERR("%s: failed to remove port %"PRIu16" (%s) interface (%s)",
1636 ofproto->name, odp_port, name, strerror(error));
1637 } else if (ofport) {
1638 /* 'name' is the netdev's name and update_port() is going to close the
1639 * netdev. Just in case update_port() refers to 'name' after it
1640 * destroys 'ofport', make a copy of it around the update_port()
1641 * call. */
1642 char *devname = xstrdup(name);
1643 update_port(ofproto, devname);
1644 free(devname);
1645 }
1646 return error;
1647 }
1648
1649 /* Sends 'packet' out of port 'port_no' within 'p'. If 'vlan_tci' is zero the
1650 * packet will not have any 802.1Q hader; if it is nonzero, then the packet
1651 * will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
1652 *
1653 * Returns 0 if successful, otherwise a positive errno value. */
1654 static int
1655 ofproto_send_packet(struct ofproto *ofproto,
1656 uint32_t port_no, uint16_t vlan_tci,
1657 const struct ofpbuf *packet)
1658 {
1659 struct ofpbuf odp_actions;
1660 int error;
1661
1662 ofpbuf_init(&odp_actions, 32);
1663 if (vlan_tci != 0) {
1664 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
1665 ntohs(vlan_tci & ~VLAN_CFI));
1666 }
1667 nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, port_no);
1668 error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
1669 packet);
1670 ofpbuf_uninit(&odp_actions);
1671
1672 if (error) {
1673 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
1674 ofproto->name, port_no, strerror(error));
1675 }
1676 return error;
1677 }
1678
1679 /* Adds a flow to the OpenFlow flow table in 'p' that matches 'cls_rule' and
1680 * performs the 'n_actions' actions in 'actions'. The new flow will not
1681 * timeout.
1682 *
1683 * If cls_rule->priority is in the range of priorities supported by OpenFlow
1684 * (0...65535, inclusive) then the flow will be visible to OpenFlow
1685 * controllers; otherwise, it will be hidden.
1686 *
1687 * The caller retains ownership of 'cls_rule' and 'actions'. */
1688 void
1689 ofproto_add_flow(struct ofproto *p, const struct cls_rule *cls_rule,
1690 const union ofp_action *actions, size_t n_actions)
1691 {
1692 struct rule *rule;
1693 rule = rule_create(cls_rule, actions, n_actions, 0, 0, 0, false);
1694 rule_insert(p, rule);
1695 }
1696
1697 void
1698 ofproto_delete_flow(struct ofproto *ofproto, const struct cls_rule *target)
1699 {
1700 struct rule *rule;
1701
1702 rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
1703 target));
1704 if (rule) {
1705 rule_remove(ofproto, rule);
1706 }
1707 }
1708
1709 static void
1710 ofproto_flush_flows__(struct ofproto *ofproto)
1711 {
1712 struct facet *facet, *next_facet;
1713 struct rule *rule, *next_rule;
1714 struct cls_cursor cursor;
1715
1716 COVERAGE_INC(ofproto_flush);
1717
1718 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1719 /* Mark the facet as not installed so that facet_remove() doesn't
1720 * bother trying to uninstall it. There is no point in uninstalling it
1721 * individually since we are about to blow away all the facets with
1722 * dpif_flow_flush(). */
1723 facet->installed = false;
1724 facet->dp_packet_count = 0;
1725 facet->dp_byte_count = 0;
1726 facet_remove(ofproto, facet);
1727 }
1728
1729 cls_cursor_init(&cursor, &ofproto->cls, NULL);
1730 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
1731 rule_remove(ofproto, rule);
1732 }
1733
1734 dpif_flow_flush(ofproto->dpif);
1735 }
1736
1737 void
1738 ofproto_flush_flows(struct ofproto *ofproto)
1739 {
1740 ofproto_flush_flows__(ofproto);
1741 connmgr_flushed(ofproto->connmgr);
1742 }
1743 \f
1744 static void
1745 reinit_ports(struct ofproto *p)
1746 {
1747 struct dpif_port_dump dump;
1748 struct sset devnames;
1749 struct ofport *ofport;
1750 struct dpif_port dpif_port;
1751 const char *devname;
1752
1753 COVERAGE_INC(ofproto_reinit_ports);
1754
1755 sset_init(&devnames);
1756 HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
1757 sset_add(&devnames, netdev_get_name(ofport->netdev));
1758 }
1759 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
1760 sset_add(&devnames, dpif_port.name);
1761 }
1762
1763 SSET_FOR_EACH (devname, &devnames) {
1764 update_port(p, devname);
1765 }
1766 sset_destroy(&devnames);
1767 }
1768
1769 /* Opens and returns a netdev for 'dpif_port', or a null pointer if the netdev
1770 * cannot be opened. On success, also fills in 'opp'. */
1771 static struct netdev *
1772 ofport_open(const struct dpif_port *dpif_port, struct ofp_phy_port *opp)
1773 {
1774 uint32_t curr, advertised, supported, peer;
1775 struct netdev_options netdev_options;
1776 enum netdev_flags flags;
1777 struct netdev *netdev;
1778 int error;
1779
1780 memset(&netdev_options, 0, sizeof netdev_options);
1781 netdev_options.name = dpif_port->name;
1782 netdev_options.type = dpif_port->type;
1783 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
1784
1785 error = netdev_open(&netdev_options, &netdev);
1786 if (error) {
1787 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
1788 "cannot be opened (%s)",
1789 dpif_port->name, dpif_port->port_no,
1790 dpif_port->name, strerror(error));
1791 return NULL;
1792 }
1793
1794 netdev_get_flags(netdev, &flags);
1795 netdev_get_features(netdev, &curr, &advertised, &supported, &peer);
1796
1797 opp->port_no = htons(odp_port_to_ofp_port(dpif_port->port_no));
1798 netdev_get_etheraddr(netdev, opp->hw_addr);
1799 ovs_strzcpy(opp->name, dpif_port->name, sizeof opp->name);
1800 opp->config = flags & NETDEV_UP ? 0 : htonl(OFPPC_PORT_DOWN);
1801 opp->state = netdev_get_carrier(netdev) ? 0 : htonl(OFPPS_LINK_DOWN);
1802 opp->curr = htonl(curr);
1803 opp->advertised = htonl(advertised);
1804 opp->supported = htonl(supported);
1805 opp->peer = htonl(peer);
1806
1807 return netdev;
1808 }
1809
1810 static bool
1811 ofport_conflicts(const struct ofproto *p, const struct dpif_port *dpif_port)
1812 {
1813 if (get_port(p, dpif_port->port_no)) {
1814 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
1815 dpif_port->port_no);
1816 return true;
1817 } else if (shash_find(&p->port_by_name, dpif_port->name)) {
1818 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
1819 dpif_port->name);
1820 return true;
1821 } else {
1822 return false;
1823 }
1824 }
1825
1826 /* Returns true if most fields of 'a' and 'b' are equal. Differences in name,
1827 * port number, and 'config' bits other than OFPPC_PORT_DOWN are
1828 * disregarded. */
1829 static bool
1830 ofport_equal(const struct ofp_phy_port *a, const struct ofp_phy_port *b)
1831 {
1832 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
1833 return (!memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
1834 && a->state == b->state
1835 && !((a->config ^ b->config) & htonl(OFPPC_PORT_DOWN))
1836 && a->curr == b->curr
1837 && a->advertised == b->advertised
1838 && a->supported == b->supported
1839 && a->peer == b->peer);
1840 }
1841
1842 /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'.
1843 * The caller must ensure that 'p' does not have a conflicting ofport (that is,
1844 * one with the same name or port number). */
1845 static void
1846 ofport_install(struct ofproto *p,
1847 struct netdev *netdev, const struct ofp_phy_port *opp)
1848 {
1849 const char *netdev_name = netdev_get_name(netdev);
1850 struct ofport *ofport;
1851
1852 connmgr_send_port_status(p->connmgr, opp, OFPPR_ADD);
1853
1854 /* Create ofport. */
1855 ofport = xmalloc(sizeof *ofport);
1856 ofport->ofproto = p;
1857 ofport->netdev = netdev;
1858 ofport->opp = *opp;
1859 ofport->odp_port = ofp_port_to_odp_port(ntohs(opp->port_no));
1860 ofport->bundle = NULL;
1861 ofport->cfm = NULL;
1862 ofport->tag = tag_create_random();
1863
1864 /* Add port to 'p'. */
1865 netdev_monitor_add(p->netdev_monitor, ofport->netdev);
1866 hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
1867 shash_add(&p->port_by_name, netdev_name, ofport);
1868 if (p->sflow) {
1869 ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
1870 }
1871 }
1872
1873 /* Removes 'ofport' from 'p' and destroys it. */
1874 static void
1875 ofport_remove(struct ofport *ofport)
1876 {
1877 connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->opp,
1878 OFPPR_DELETE);
1879 ofport_free(ofport);
1880 }
1881
1882 /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and
1883 * destroys it. */
1884 static void
1885 ofport_remove_with_name(struct ofproto *ofproto, const char *name)
1886 {
1887 struct ofport *port = shash_find_data(&ofproto->port_by_name, name);
1888 if (port) {
1889 ofport_remove(port);
1890 }
1891 }
1892
1893 /* Updates 'port' within 'ofproto' with the new 'netdev' and 'opp'.
1894 *
1895 * Does not handle a name or port number change. The caller must implement
1896 * such a change as a delete followed by an add. */
1897 static void
1898 ofport_modified(struct ofport *port,
1899 struct netdev *netdev, struct ofp_phy_port *opp)
1900 {
1901 struct ofproto *ofproto = port->ofproto;
1902
1903 if (port->bundle && port->bundle->bond) {
1904 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1905 }
1906
1907 memcpy(port->opp.hw_addr, opp->hw_addr, ETH_ADDR_LEN);
1908 port->opp.config = ((port->opp.config & ~htonl(OFPPC_PORT_DOWN))
1909 | (opp->config & htonl(OFPPC_PORT_DOWN)));
1910 port->opp.state = opp->state;
1911 port->opp.curr = opp->curr;
1912 port->opp.advertised = opp->advertised;
1913 port->opp.supported = opp->supported;
1914 port->opp.peer = opp->peer;
1915
1916 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1917 netdev_monitor_add(ofproto->netdev_monitor, netdev);
1918
1919 netdev_close(port->netdev);
1920 port->netdev = netdev;
1921
1922 connmgr_send_port_status(ofproto->connmgr, &port->opp, OFPPR_MODIFY);
1923 }
1924
1925 static void
1926 ofport_run(struct ofport *ofport)
1927 {
1928 if (ofport->cfm) {
1929 cfm_run(ofport->cfm);
1930
1931 if (cfm_should_send_ccm(ofport->cfm)) {
1932 struct ofpbuf packet;
1933 struct ccm *ccm;
1934
1935 ofpbuf_init(&packet, 0);
1936 ccm = eth_compose(&packet, eth_addr_ccm, ofport->opp.hw_addr,
1937 ETH_TYPE_CFM, sizeof *ccm);
1938 cfm_compose_ccm(ofport->cfm, ccm);
1939 ofproto_send_packet(ofport->ofproto, ofport->odp_port, 0, &packet);
1940 ofpbuf_uninit(&packet);
1941 }
1942 }
1943 }
1944
1945 static void
1946 ofport_wait(struct ofport *ofport)
1947 {
1948 if (ofport->cfm) {
1949 cfm_wait(ofport->cfm);
1950 }
1951 }
1952
1953 static void
1954 ofport_unregister(struct ofport *port)
1955 {
1956 struct ofbundle *bundle = port->bundle;
1957
1958 if (bundle) {
1959 ofproto_bundle_del_port(port);
1960 if (list_is_empty(&bundle->ports)) {
1961 ofproto_bundle_destroy(bundle);
1962 } else if (list_is_short(&bundle->ports)) {
1963 bond_destroy(bundle->bond);
1964 bundle->bond = NULL;
1965 }
1966 }
1967
1968 cfm_destroy(port->cfm);
1969 port->cfm = NULL;
1970 }
1971
1972 void
1973 ofproto_port_unregister(struct ofproto *ofproto, uint32_t ofp_port)
1974 {
1975 struct ofport *port = get_port(ofproto, ofp_port_to_odp_port(ofp_port));
1976 if (port) {
1977 ofport_unregister(port);
1978 }
1979 }
1980
1981 static void
1982 ofport_free(struct ofport *port)
1983 {
1984 if (port) {
1985 struct ofproto *ofproto = port->ofproto;
1986 const char *name = netdev_get_name(port->netdev);
1987
1988 ofport_unregister(port);
1989
1990 netdev_monitor_remove(ofproto->netdev_monitor, port->netdev);
1991 hmap_remove(&ofproto->ports, &port->hmap_node);
1992 shash_delete(&ofproto->port_by_name,
1993 shash_find(&ofproto->port_by_name, name));
1994 if (ofproto->sflow) {
1995 ofproto_sflow_del_port(ofproto->sflow, port->odp_port);
1996 }
1997
1998 netdev_close(port->netdev);
1999 free(port);
2000 }
2001 }
2002
2003 static struct ofport *
2004 get_port(const struct ofproto *ofproto, uint16_t odp_port)
2005 {
2006 struct ofport *port;
2007
2008 HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
2009 hash_int(odp_port, 0), &ofproto->ports) {
2010 if (port->odp_port == odp_port) {
2011 return port;
2012 }
2013 }
2014 return NULL;
2015 }
2016
2017 static void
2018 update_port(struct ofproto *ofproto, const char *name)
2019 {
2020 struct dpif_port dpif_port;
2021 struct ofp_phy_port opp;
2022 struct netdev *netdev;
2023 struct ofport *port;
2024
2025 COVERAGE_INC(ofproto_update_port);
2026
2027 /* Fetch 'name''s location and properties from the datapath. */
2028 netdev = (!dpif_port_query_by_name(ofproto->dpif, name, &dpif_port)
2029 ? ofport_open(&dpif_port, &opp)
2030 : NULL);
2031 if (netdev) {
2032 port = get_port(ofproto, dpif_port.port_no);
2033 if (port && !strcmp(netdev_get_name(port->netdev), name)) {
2034 /* 'name' hasn't changed location. Any properties changed? */
2035 if (!ofport_equal(&port->opp, &opp)) {
2036 ofport_modified(port, netdev, &opp);
2037 } else {
2038 netdev_close(netdev);
2039 }
2040 } else {
2041 /* If 'port' is nonnull then its name differs from 'name' and thus
2042 * we should delete it. If we think there's a port named 'name'
2043 * then its port number must be wrong now so delete it too. */
2044 if (port) {
2045 ofport_remove(port);
2046 }
2047 ofport_remove_with_name(ofproto, name);
2048 ofport_install(ofproto, netdev, &opp);
2049 }
2050 } else {
2051 /* Any port named 'name' is gone now. */
2052 ofport_remove_with_name(ofproto, name);
2053 }
2054 dpif_port_destroy(&dpif_port);
2055 }
2056
2057 static int
2058 init_ports(struct ofproto *p)
2059 {
2060 struct dpif_port_dump dump;
2061 struct dpif_port dpif_port;
2062
2063 DPIF_PORT_FOR_EACH (&dpif_port, &dump, p->dpif) {
2064 if (!ofport_conflicts(p, &dpif_port)) {
2065 struct ofp_phy_port opp;
2066 struct netdev *netdev;
2067
2068 netdev = ofport_open(&dpif_port, &opp);
2069 if (netdev) {
2070 ofport_install(p, netdev, &opp);
2071 }
2072 }
2073 }
2074
2075 return 0;
2076 }
2077 \f
2078 /* Returns true if 'rule' should be hidden from the controller.
2079 *
2080 * Rules with priority higher than UINT16_MAX are set up by ofproto itself
2081 * (e.g. by in-band control) and are intentionally hidden from the
2082 * controller. */
2083 static bool
2084 rule_is_hidden(const struct rule *rule)
2085 {
2086 return rule->cr.priority > UINT16_MAX;
2087 }
2088
2089 /* Creates and returns a new rule initialized as specified.
2090 *
2091 * The caller is responsible for inserting the rule into the classifier (with
2092 * rule_insert()). */
2093 static struct rule *
2094 rule_create(const struct cls_rule *cls_rule,
2095 const union ofp_action *actions, size_t n_actions,
2096 uint16_t idle_timeout, uint16_t hard_timeout,
2097 ovs_be64 flow_cookie, bool send_flow_removed)
2098 {
2099 struct rule *rule = xzalloc(sizeof *rule);
2100 rule->cr = *cls_rule;
2101 rule->idle_timeout = idle_timeout;
2102 rule->hard_timeout = hard_timeout;
2103 rule->flow_cookie = flow_cookie;
2104 rule->used = rule->created = time_msec();
2105 rule->send_flow_removed = send_flow_removed;
2106 list_init(&rule->facets);
2107 if (n_actions > 0) {
2108 rule->n_actions = n_actions;
2109 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
2110 }
2111
2112 return rule;
2113 }
2114
2115 static struct rule *
2116 rule_from_cls_rule(const struct cls_rule *cls_rule)
2117 {
2118 return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
2119 }
2120
2121 static void
2122 rule_free(struct rule *rule)
2123 {
2124 free(rule->actions);
2125 free(rule);
2126 }
2127
2128 /* Destroys 'rule' and iterates through all of its facets and revalidates them,
2129 * destroying any that no longer has a rule (which is probably all of them).
2130 *
2131 * The caller must have already removed 'rule' from the classifier. */
2132 static void
2133 rule_destroy(struct ofproto *ofproto, struct rule *rule)
2134 {
2135 struct facet *facet, *next_facet;
2136 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2137 facet_revalidate(ofproto, facet);
2138 }
2139 rule_free(rule);
2140 }
2141
2142 /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
2143 * that outputs to 'out_port' (output to OFPP_FLOOD and OFPP_ALL doesn't
2144 * count). */
2145 static bool
2146 rule_has_out_port(const struct rule *rule, ovs_be16 out_port)
2147 {
2148 const union ofp_action *oa;
2149 struct actions_iterator i;
2150
2151 if (out_port == htons(OFPP_NONE)) {
2152 return true;
2153 }
2154 for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
2155 oa = actions_next(&i)) {
2156 if (action_outputs_to_port(oa, out_port)) {
2157 return true;
2158 }
2159 }
2160 return false;
2161 }
2162
2163 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
2164 * 'packet', which arrived on 'in_port'.
2165 *
2166 * Takes ownership of 'packet'. */
2167 static bool
2168 execute_odp_actions(struct ofproto *ofproto, const struct flow *flow,
2169 const struct nlattr *odp_actions, size_t actions_len,
2170 struct ofpbuf *packet)
2171 {
2172 if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
2173 && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
2174 /* As an optimization, avoid a round-trip from userspace to kernel to
2175 * userspace. This also avoids possibly filling up kernel packet
2176 * buffers along the way. */
2177 struct dpif_upcall upcall;
2178
2179 upcall.type = DPIF_UC_ACTION;
2180 upcall.packet = packet;
2181 upcall.key = NULL;
2182 upcall.key_len = 0;
2183 upcall.userdata = nl_attr_get_u64(odp_actions);
2184 upcall.sample_pool = 0;
2185 upcall.actions = NULL;
2186 upcall.actions_len = 0;
2187
2188 send_packet_in(ofproto, &upcall, flow, false);
2189
2190 return true;
2191 } else {
2192 int error;
2193
2194 error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
2195 ofpbuf_delete(packet);
2196 return !error;
2197 }
2198 }
2199
2200 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
2201 * statistics appropriately. 'packet' must have at least sizeof(struct
2202 * ofp_packet_in) bytes of headroom.
2203 *
2204 * For correct results, 'packet' must actually be in 'facet''s flow; that is,
2205 * applying flow_extract() to 'packet' would yield the same flow as
2206 * 'facet->flow'.
2207 *
2208 * 'facet' must have accurately composed ODP actions; that is, it must not be
2209 * in need of revalidation.
2210 *
2211 * Takes ownership of 'packet'. */
2212 static void
2213 facet_execute(struct ofproto *ofproto, struct facet *facet,
2214 struct ofpbuf *packet)
2215 {
2216 struct dpif_flow_stats stats;
2217
2218 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2219
2220 flow_extract_stats(&facet->flow, packet, &stats);
2221 stats.used = time_msec();
2222 if (execute_odp_actions(ofproto, &facet->flow,
2223 facet->actions, facet->actions_len, packet)) {
2224 facet_update_stats(ofproto, facet, &stats);
2225 }
2226 }
2227
2228 /* Executes the actions indicated by 'rule' on 'packet' and credits 'rule''s
2229 * statistics (or the statistics for one of its facets) appropriately.
2230 * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of headroom.
2231 *
2232 * 'packet' doesn't necessarily have to match 'rule'. 'rule' will be credited
2233 * with statistics for 'packet' either way.
2234 *
2235 * Takes ownership of 'packet'. */
2236 static void
2237 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
2238 struct ofpbuf *packet)
2239 {
2240 struct action_xlate_ctx ctx;
2241 struct ofpbuf *odp_actions;
2242 struct facet *facet;
2243 struct flow flow;
2244 size_t size;
2245
2246 assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
2247
2248 flow_extract(packet, 0, in_port, &flow);
2249
2250 /* First look for a related facet. If we find one, account it to that. */
2251 facet = facet_lookup_valid(ofproto, &flow);
2252 if (facet && facet->rule == rule) {
2253 facet_execute(ofproto, facet, packet);
2254 return;
2255 }
2256
2257 /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2258 * create a new facet for it and use that. */
2259 if (rule_lookup(ofproto, &flow) == rule) {
2260 facet = facet_create(ofproto, rule, &flow, packet);
2261 facet_execute(ofproto, facet, packet);
2262 facet_install(ofproto, facet, true);
2263 return;
2264 }
2265
2266 /* We can't account anything to a facet. If we were to try, then that
2267 * facet would have a non-matching rule, busting our invariants. */
2268 action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
2269 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
2270 size = packet->size;
2271 if (execute_odp_actions(ofproto, &flow, odp_actions->data,
2272 odp_actions->size, packet)) {
2273 rule->used = time_msec();
2274 rule->packet_count++;
2275 rule->byte_count += size;
2276 flow_push_stats(ofproto, rule, &flow, 1, size, rule->used);
2277 }
2278 ofpbuf_delete(odp_actions);
2279 }
2280
2281 /* Inserts 'rule' into 'p''s flow table. */
2282 static void
2283 rule_insert(struct ofproto *p, struct rule *rule)
2284 {
2285 struct rule *displaced_rule;
2286
2287 displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
2288 if (displaced_rule) {
2289 rule_destroy(p, displaced_rule);
2290 }
2291 p->need_revalidate = true;
2292 }
2293
2294 /* Creates and returns a new facet within 'ofproto' owned by 'rule', given a
2295 * 'flow' and an example 'packet' within that flow.
2296 *
2297 * The caller must already have determined that no facet with an identical
2298 * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
2299 * 'ofproto''s classifier table. */
2300 static struct facet *
2301 facet_create(struct ofproto *ofproto, struct rule *rule,
2302 const struct flow *flow, const struct ofpbuf *packet)
2303 {
2304 struct facet *facet;
2305
2306 facet = xzalloc(sizeof *facet);
2307 facet->used = time_msec();
2308 hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
2309 list_push_back(&rule->facets, &facet->list_node);
2310 facet->rule = rule;
2311 facet->flow = *flow;
2312 netflow_flow_init(&facet->nf_flow);
2313 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
2314
2315 facet_make_actions(ofproto, facet, packet);
2316
2317 return facet;
2318 }
2319
2320 static void
2321 facet_free(struct facet *facet)
2322 {
2323 free(facet->actions);
2324 free(facet);
2325 }
2326
2327 /* Remove 'rule' from 'ofproto' and free up the associated memory:
2328 *
2329 * - Removes 'rule' from the classifier.
2330 *
2331 * - If 'rule' has facets, revalidates them (and possibly uninstalls and
2332 * destroys them), via rule_destroy().
2333 */
2334 static void
2335 rule_remove(struct ofproto *ofproto, struct rule *rule)
2336 {
2337 COVERAGE_INC(ofproto_del_rule);
2338 ofproto->need_revalidate = true;
2339 classifier_remove(&ofproto->cls, &rule->cr);
2340 rule_destroy(ofproto, rule);
2341 }
2342
2343 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2344 *
2345 * - If 'facet' was installed in the datapath, uninstalls it and updates its
2346 * rule's statistics, via facet_uninstall().
2347 *
2348 * - Removes 'facet' from its rule and from ofproto->facets.
2349 */
2350 static void
2351 facet_remove(struct ofproto *ofproto, struct facet *facet)
2352 {
2353 facet_uninstall(ofproto, facet);
2354 facet_flush_stats(ofproto, facet);
2355 hmap_remove(&ofproto->facets, &facet->hmap_node);
2356 list_remove(&facet->list_node);
2357 facet_free(facet);
2358 }
2359
2360 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2361 static void
2362 facet_make_actions(struct ofproto *p, struct facet *facet,
2363 const struct ofpbuf *packet)
2364 {
2365 const struct rule *rule = facet->rule;
2366 struct ofpbuf *odp_actions;
2367 struct action_xlate_ctx ctx;
2368
2369 action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2370 odp_actions = xlate_actions(&ctx, rule->actions, rule->n_actions);
2371 facet->tags = ctx.tags;
2372 facet->may_install = ctx.may_set_up_flow;
2373 facet->nf_flow.output_iface = ctx.nf_output_iface;
2374
2375 if (facet->actions_len != odp_actions->size
2376 || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2377 free(facet->actions);
2378 facet->actions_len = odp_actions->size;
2379 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2380 }
2381
2382 ofpbuf_delete(odp_actions);
2383 }
2384
2385 static int
2386 facet_put__(struct ofproto *ofproto, struct facet *facet,
2387 const struct nlattr *actions, size_t actions_len,
2388 struct dpif_flow_stats *stats)
2389 {
2390 struct odputil_keybuf keybuf;
2391 enum dpif_flow_put_flags flags;
2392 struct ofpbuf key;
2393
2394 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2395 if (stats) {
2396 flags |= DPIF_FP_ZERO_STATS;
2397 facet->dp_packet_count = 0;
2398 facet->dp_byte_count = 0;
2399 }
2400
2401 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2402 odp_flow_key_from_flow(&key, &facet->flow);
2403
2404 return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2405 actions, actions_len, stats);
2406 }
2407
2408 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If
2409 * 'zero_stats' is true, clears any existing statistics from the datapath for
2410 * 'facet'. */
2411 static void
2412 facet_install(struct ofproto *p, struct facet *facet, bool zero_stats)
2413 {
2414 struct dpif_flow_stats stats;
2415
2416 if (facet->may_install
2417 && !facet_put__(p, facet, facet->actions, facet->actions_len,
2418 zero_stats ? &stats : NULL)) {
2419 facet->installed = true;
2420 }
2421 }
2422
2423 static void
2424 facet_account(struct ofproto *ofproto,
2425 struct facet *facet, uint64_t extra_bytes)
2426 {
2427 uint64_t total_bytes, n_bytes;
2428 struct ofbundle *in_bundle;
2429 const struct nlattr *a;
2430 tag_type dummy = 0;
2431 unsigned int left;
2432 int vlan;
2433
2434 total_bytes = facet->byte_count + extra_bytes;
2435 if (total_bytes <= facet->accounted_bytes) {
2436 return;
2437 }
2438 n_bytes = total_bytes - facet->accounted_bytes;
2439 facet->accounted_bytes = total_bytes;
2440
2441 /* Test that 'tags' is nonzero to ensure that only flows that include an
2442 * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2443 * This works because OFPP_NORMAL always sets a nonzero tag value.
2444 *
2445 * Feed information from the active flows back into the learning table to
2446 * ensure that table is always in sync with what is actually flowing
2447 * through the datapath. */
2448 if (!facet->tags
2449 || !is_admissible(ofproto, &facet->flow, false, &dummy,
2450 &vlan, &in_bundle)) {
2451 return;
2452 }
2453
2454 update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2455
2456 if (!ofproto->has_bonded_bundles) {
2457 return;
2458 }
2459 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2460 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2461 struct ofport *port = get_port(ofproto, nl_attr_get_u32(a));
2462 if (port && port->bundle && port->bundle->bond) {
2463 bond_account(port->bundle->bond, &facet->flow, vlan, n_bytes);
2464 }
2465 }
2466 }
2467 }
2468
2469 /* If 'rule' is installed in the datapath, uninstalls it. */
2470 static void
2471 facet_uninstall(struct ofproto *p, struct facet *facet)
2472 {
2473 if (facet->installed) {
2474 struct odputil_keybuf keybuf;
2475 struct dpif_flow_stats stats;
2476 struct ofpbuf key;
2477
2478 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2479 odp_flow_key_from_flow(&key, &facet->flow);
2480
2481 if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
2482 facet_update_stats(p, facet, &stats);
2483 }
2484 facet->installed = false;
2485 facet->dp_packet_count = 0;
2486 facet->dp_byte_count = 0;
2487 } else {
2488 assert(facet->dp_packet_count == 0);
2489 assert(facet->dp_byte_count == 0);
2490 }
2491 }
2492
2493 /* Returns true if the only action for 'facet' is to send to the controller.
2494 * (We don't report NetFlow expiration messages for such facets because they
2495 * are just part of the control logic for the network, not real traffic). */
2496 static bool
2497 facet_is_controller_flow(struct facet *facet)
2498 {
2499 return (facet
2500 && facet->rule->n_actions == 1
2501 && action_outputs_to_port(&facet->rule->actions[0],
2502 htons(OFPP_CONTROLLER)));
2503 }
2504
2505 /* Folds all of 'facet''s statistics into its rule. Also updates the
2506 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
2507 * 'facet''s statistics in the datapath should have been zeroed and folded into
2508 * its packet and byte counts before this function is called. */
2509 static void
2510 facet_flush_stats(struct ofproto *ofproto, struct facet *facet)
2511 {
2512 assert(!facet->dp_byte_count);
2513 assert(!facet->dp_packet_count);
2514
2515 facet_push_stats(ofproto, facet);
2516 facet_account(ofproto, facet, 0);
2517
2518 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2519 struct ofexpired expired;
2520 expired.flow = facet->flow;
2521 expired.packet_count = facet->packet_count;
2522 expired.byte_count = facet->byte_count;
2523 expired.used = facet->used;
2524 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2525 }
2526
2527 facet->rule->packet_count += facet->packet_count;
2528 facet->rule->byte_count += facet->byte_count;
2529
2530 /* Reset counters to prevent double counting if 'facet' ever gets
2531 * reinstalled. */
2532 facet->packet_count = 0;
2533 facet->byte_count = 0;
2534 facet->rs_packet_count = 0;
2535 facet->rs_byte_count = 0;
2536 facet->accounted_bytes = 0;
2537
2538 netflow_flow_clear(&facet->nf_flow);
2539 }
2540
2541 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2542 * Returns it if found, otherwise a null pointer.
2543 *
2544 * The returned facet might need revalidation; use facet_lookup_valid()
2545 * instead if that is important. */
2546 static struct facet *
2547 facet_find(struct ofproto *ofproto, const struct flow *flow)
2548 {
2549 struct facet *facet;
2550
2551 HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2552 &ofproto->facets) {
2553 if (flow_equal(flow, &facet->flow)) {
2554 return facet;
2555 }
2556 }
2557
2558 return NULL;
2559 }
2560
2561 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2562 * Returns it if found, otherwise a null pointer.
2563 *
2564 * The returned facet is guaranteed to be valid. */
2565 static struct facet *
2566 facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
2567 {
2568 struct facet *facet = facet_find(ofproto, flow);
2569
2570 /* The facet we found might not be valid, since we could be in need of
2571 * revalidation. If it is not valid, don't return it. */
2572 if (facet
2573 && ofproto->need_revalidate
2574 && !facet_revalidate(ofproto, facet)) {
2575 COVERAGE_INC(ofproto_invalidated);
2576 return NULL;
2577 }
2578
2579 return facet;
2580 }
2581
2582 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2583 *
2584 * - If the rule found is different from 'facet''s current rule, moves
2585 * 'facet' to the new rule and recompiles its actions.
2586 *
2587 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2588 * where it is and recompiles its actions anyway.
2589 *
2590 * - If there is none, destroys 'facet'.
2591 *
2592 * Returns true if 'facet' still exists, false if it has been destroyed. */
2593 static bool
2594 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
2595 {
2596 struct action_xlate_ctx ctx;
2597 struct ofpbuf *odp_actions;
2598 struct rule *new_rule;
2599 bool actions_changed;
2600
2601 COVERAGE_INC(facet_revalidate);
2602
2603 /* Determine the new rule. */
2604 new_rule = rule_lookup(ofproto, &facet->flow);
2605 if (!new_rule) {
2606 /* No new rule, so delete the facet. */
2607 facet_remove(ofproto, facet);
2608 return false;
2609 }
2610
2611 /* Calculate new ODP actions.
2612 *
2613 * We do not modify any 'facet' state yet, because we might need to, e.g.,
2614 * emit a NetFlow expiration and, if so, we need to have the old state
2615 * around to properly compose it. */
2616 action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2617 odp_actions = xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
2618 actions_changed = (facet->actions_len != odp_actions->size
2619 || memcmp(facet->actions, odp_actions->data,
2620 facet->actions_len));
2621
2622 /* If the ODP actions changed or the installability changed, then we need
2623 * to talk to the datapath. */
2624 if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2625 if (ctx.may_set_up_flow) {
2626 struct dpif_flow_stats stats;
2627
2628 facet_put__(ofproto, facet,
2629 odp_actions->data, odp_actions->size, &stats);
2630 facet_update_stats(ofproto, facet, &stats);
2631 } else {
2632 facet_uninstall(ofproto, facet);
2633 }
2634
2635 /* The datapath flow is gone or has zeroed stats, so push stats out of
2636 * 'facet' into 'rule'. */
2637 facet_flush_stats(ofproto, facet);
2638 }
2639
2640 /* Update 'facet' now that we've taken care of all the old state. */
2641 facet->tags = ctx.tags;
2642 facet->nf_flow.output_iface = ctx.nf_output_iface;
2643 facet->may_install = ctx.may_set_up_flow;
2644 if (actions_changed) {
2645 free(facet->actions);
2646 facet->actions_len = odp_actions->size;
2647 facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2648 }
2649 if (facet->rule != new_rule) {
2650 COVERAGE_INC(facet_changed_rule);
2651 list_remove(&facet->list_node);
2652 list_push_back(&new_rule->facets, &facet->list_node);
2653 facet->rule = new_rule;
2654 facet->used = new_rule->created;
2655 facet->rs_used = facet->used;
2656 }
2657
2658 ofpbuf_delete(odp_actions);
2659
2660 return true;
2661 }
2662 \f
2663 /* Bridge packet processing functions. */
2664
2665 struct dst {
2666 struct ofport *port;
2667 uint16_t vlan;
2668 };
2669
2670 struct dst_set {
2671 struct dst builtin[32];
2672 struct dst *dsts;
2673 size_t n, allocated;
2674 };
2675
2676 static void dst_set_init(struct dst_set *);
2677 static void dst_set_add(struct dst_set *, const struct dst *);
2678 static void dst_set_free(struct dst_set *);
2679
2680 static struct ofport *ofbundle_get_a_port(const struct ofbundle *);
2681
2682 static bool
2683 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
2684 const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
2685 {
2686 dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
2687 : in_bundle->vlan >= 0 ? in_bundle->vlan
2688 : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
2689 : vlan_tci_to_vid(ctx->flow.vlan_tci));
2690
2691 dst->port = (!out_bundle->bond
2692 ? ofbundle_get_a_port(out_bundle)
2693 : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
2694 dst->vlan, &ctx->tags));
2695
2696 return dst->port != NULL;
2697 }
2698
2699 static int
2700 mirror_mask_ffs(mirror_mask_t mask)
2701 {
2702 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2703 return ffs(mask);
2704 }
2705
2706 static void
2707 dst_set_init(struct dst_set *set)
2708 {
2709 set->dsts = set->builtin;
2710 set->n = 0;
2711 set->allocated = ARRAY_SIZE(set->builtin);
2712 }
2713
2714 static void
2715 dst_set_add(struct dst_set *set, const struct dst *dst)
2716 {
2717 if (set->n >= set->allocated) {
2718 size_t new_allocated;
2719 struct dst *new_dsts;
2720
2721 new_allocated = set->allocated * 2;
2722 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2723 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2724
2725 dst_set_free(set);
2726
2727 set->dsts = new_dsts;
2728 set->allocated = new_allocated;
2729 }
2730 set->dsts[set->n++] = *dst;
2731 }
2732
2733 static void
2734 dst_set_free(struct dst_set *set)
2735 {
2736 if (set->dsts != set->builtin) {
2737 free(set->dsts);
2738 }
2739 }
2740
2741 static bool
2742 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
2743 {
2744 size_t i;
2745 for (i = 0; i < set->n; i++) {
2746 if (set->dsts[i].vlan == test->vlan
2747 && set->dsts[i].port == test->port) {
2748 return true;
2749 }
2750 }
2751 return false;
2752 }
2753
2754 static bool
2755 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
2756 {
2757 return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan);
2758 }
2759
2760 static bool
2761 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
2762 {
2763 return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
2764 }
2765
2766 /* Returns an arbitrary interface within 'bundle'. */
2767 static struct ofport *
2768 ofbundle_get_a_port(const struct ofbundle *bundle)
2769 {
2770 return CONTAINER_OF(list_front(&bundle->ports),
2771 struct ofport, bundle_node);
2772 }
2773
2774 static void
2775 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
2776 const struct ofbundle *in_bundle,
2777 const struct ofbundle *out_bundle, struct dst_set *set)
2778 {
2779 struct dst dst;
2780
2781 if (out_bundle == OFBUNDLE_FLOOD) {
2782 struct ofbundle *bundle;
2783
2784 HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
2785 if (bundle != in_bundle
2786 && ofbundle_includes_vlan(bundle, vlan)
2787 && bundle->floodable
2788 && !bundle->mirror_out
2789 && set_dst(ctx, &dst, in_bundle, bundle)) {
2790 dst_set_add(set, &dst);
2791 }
2792 }
2793 ctx->nf_output_iface = NF_OUT_FLOOD;
2794 } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
2795 dst_set_add(set, &dst);
2796 ctx->nf_output_iface = dst.port->odp_port;
2797 }
2798 }
2799
2800 static bool
2801 vlan_is_mirrored(const struct ofmirror *m, int vlan)
2802 {
2803 return vlan_bitmap_contains(m->vlans, vlan);
2804 }
2805
2806 static void
2807 compose_mirror_dsts(struct action_xlate_ctx *ctx,
2808 uint16_t vlan, const struct ofbundle *in_bundle,
2809 struct dst_set *set)
2810 {
2811 struct ofproto *ofproto = ctx->ofproto;
2812 mirror_mask_t mirrors;
2813 int flow_vlan;
2814 size_t i;
2815
2816 mirrors = in_bundle->src_mirrors;
2817 for (i = 0; i < set->n; i++) {
2818 mirrors |= set->dsts[i].port->bundle->dst_mirrors;
2819 }
2820
2821 if (!mirrors) {
2822 return;
2823 }
2824
2825 flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
2826 if (flow_vlan == 0) {
2827 flow_vlan = OFP_VLAN_NONE;
2828 }
2829
2830 while (mirrors) {
2831 struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
2832 if (vlan_is_mirrored(m, vlan)) {
2833 struct dst dst;
2834
2835 if (m->out) {
2836 if (set_dst(ctx, &dst, in_bundle, m->out)
2837 && !dst_is_duplicate(set, &dst)) {
2838 dst_set_add(set, &dst);
2839 }
2840 } else {
2841 struct ofbundle *bundle;
2842
2843 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2844 if (ofbundle_includes_vlan(bundle, m->out_vlan)
2845 && set_dst(ctx, &dst, in_bundle, bundle))
2846 {
2847 if (bundle->vlan < 0) {
2848 dst.vlan = m->out_vlan;
2849 }
2850 if (dst_is_duplicate(set, &dst)) {
2851 continue;
2852 }
2853
2854 /* Use the vlan tag on the original flow instead of
2855 * the one passed in the vlan parameter. This ensures
2856 * that we compare the vlan from before any implicit
2857 * tagging tags place. This is necessary because
2858 * dst->vlan is the final vlan, after removing implicit
2859 * tags. */
2860 if (bundle == in_bundle && dst.vlan == flow_vlan) {
2861 /* Don't send out input port on same VLAN. */
2862 continue;
2863 }
2864 dst_set_add(set, &dst);
2865 }
2866 }
2867 }
2868 }
2869 mirrors &= mirrors - 1;
2870 }
2871 }
2872
2873 static void
2874 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
2875 const struct ofbundle *in_bundle,
2876 const struct ofbundle *out_bundle)
2877 {
2878 uint16_t initial_vlan, cur_vlan;
2879 const struct dst *dst;
2880 struct dst_set set;
2881
2882 dst_set_init(&set);
2883 compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
2884 compose_mirror_dsts(ctx, vlan, in_bundle, &set);
2885
2886 /* Output all the packets we can without having to change the VLAN. */
2887 initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
2888 if (initial_vlan == 0) {
2889 initial_vlan = OFP_VLAN_NONE;
2890 }
2891 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2892 if (dst->vlan != initial_vlan) {
2893 continue;
2894 }
2895 nl_msg_put_u32(ctx->odp_actions,
2896 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
2897 }
2898
2899 /* Then output the rest. */
2900 cur_vlan = initial_vlan;
2901 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2902 if (dst->vlan == initial_vlan) {
2903 continue;
2904 }
2905 if (dst->vlan != cur_vlan) {
2906 if (dst->vlan == OFP_VLAN_NONE) {
2907 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2908 } else {
2909 ovs_be16 tci;
2910 tci = htons(dst->vlan & VLAN_VID_MASK);
2911 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
2912 nl_msg_put_be16(ctx->odp_actions,
2913 ODP_ACTION_ATTR_SET_DL_TCI, tci);
2914 }
2915 cur_vlan = dst->vlan;
2916 }
2917 nl_msg_put_u32(ctx->odp_actions,
2918 ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
2919 }
2920
2921 dst_set_free(&set);
2922 }
2923
2924 /* Returns the effective vlan of a packet, taking into account both the
2925 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2926 * the packet is untagged and -1 indicates it has an invalid header and
2927 * should be dropped. */
2928 static int
2929 flow_get_vlan(struct ofproto *ofproto, const struct flow *flow,
2930 struct ofbundle *in_bundle, bool have_packet)
2931 {
2932 int vlan = vlan_tci_to_vid(flow->vlan_tci);
2933 if (in_bundle->vlan >= 0) {
2934 if (vlan) {
2935 if (have_packet) {
2936 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2937 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2938 "packet received on port %s configured with "
2939 "implicit VLAN %"PRIu16,
2940 ofproto->name, vlan,
2941 in_bundle->name, in_bundle->vlan);
2942 }
2943 return -1;
2944 }
2945 vlan = in_bundle->vlan;
2946 } else {
2947 if (!ofbundle_includes_vlan(in_bundle, vlan)) {
2948 if (have_packet) {
2949 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2950 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2951 "packet received on port %s not configured for "
2952 "trunking VLAN %d",
2953 ofproto->name, vlan, in_bundle->name, vlan);
2954 }
2955 return -1;
2956 }
2957 }
2958
2959 return vlan;
2960 }
2961
2962 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2963 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2964 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2965 static bool
2966 is_gratuitous_arp(const struct flow *flow)
2967 {
2968 return (flow->dl_type == htons(ETH_TYPE_ARP)
2969 && eth_addr_is_broadcast(flow->dl_dst)
2970 && (flow->nw_proto == ARP_OP_REPLY
2971 || (flow->nw_proto == ARP_OP_REQUEST
2972 && flow->nw_src == flow->nw_dst)));
2973 }
2974
2975 static void
2976 update_learning_table(struct ofproto *ofproto,
2977 const struct flow *flow, int vlan,
2978 struct ofbundle *in_bundle)
2979 {
2980 struct mac_entry *mac;
2981
2982 if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
2983 return;
2984 }
2985
2986 mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
2987 if (is_gratuitous_arp(flow)) {
2988 /* We don't want to learn from gratuitous ARP packets that are
2989 * reflected back over bond slaves so we lock the learning table. */
2990 if (!in_bundle->bond) {
2991 mac_entry_set_grat_arp_lock(mac);
2992 } else if (mac_entry_is_grat_arp_locked(mac)) {
2993 return;
2994 }
2995 }
2996
2997 if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
2998 /* The log messages here could actually be useful in debugging,
2999 * so keep the rate limit relatively high. */
3000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3001 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3002 "on port %s in VLAN %d",
3003 ofproto->name, ETH_ADDR_ARGS(flow->dl_src),
3004 in_bundle->name, vlan);
3005
3006 mac->port.p = in_bundle;
3007 tag_set_add(&ofproto->revalidate_set,
3008 mac_learning_changed(ofproto->ml, mac));
3009 }
3010 }
3011
3012 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3013 * dropped. Returns true if they may be forwarded, false if they should be
3014 * dropped.
3015 *
3016 * If 'have_packet' is true, it indicates that the caller is processing a
3017 * received packet. If 'have_packet' is false, then the caller is just
3018 * revalidating an existing flow because configuration has changed. Either
3019 * way, 'have_packet' only affects logging (there is no point in logging errors
3020 * during revalidation).
3021 *
3022 * Sets '*in_portp' to the input port. This will be a null pointer if
3023 * flow->in_port does not designate a known input port (in which case
3024 * is_admissible() returns false).
3025 *
3026 * When returning true, sets '*vlanp' to the effective VLAN of the input
3027 * packet, as returned by flow_get_vlan().
3028 *
3029 * May also add tags to '*tags', although the current implementation only does
3030 * so in one special case.
3031 */
3032 static bool
3033 is_admissible(struct ofproto *ofproto, const struct flow *flow,
3034 bool have_packet,
3035 tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3036 {
3037 struct ofport *in_port;
3038 struct ofbundle *in_bundle;
3039 int vlan;
3040
3041 /* Find the port and bundle for the received packet. */
3042 in_port = get_port(ofproto, flow->in_port);
3043 *in_bundlep = in_bundle = in_port->bundle;
3044 if (!in_port || !in_bundle) {
3045 /* No interface? Something fishy... */
3046 if (have_packet) {
3047 /* Odd. A few possible reasons here:
3048 *
3049 * - We deleted a port but there are still a few packets queued up
3050 * from it.
3051 *
3052 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3053 * we don't know about.
3054 *
3055 * - Packet arrived on the local port but the local port is not
3056 * part of a bundle.
3057 */
3058 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3059
3060 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3061 "port %"PRIu16,
3062 ofproto->name, flow->in_port);
3063 }
3064 return false;
3065 }
3066 *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3067 if (vlan < 0) {
3068 return false;
3069 }
3070
3071 /* Drop frames for reserved multicast addresses. */
3072 if (eth_addr_is_reserved(flow->dl_dst)) {
3073 return false;
3074 }
3075
3076 /* Drop frames on bundles reserved for mirroring. */
3077 if (in_bundle->mirror_out) {
3078 if (have_packet) {
3079 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3080 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3081 "%s, which is reserved exclusively for mirroring",
3082 ofproto->name, in_bundle->name);
3083 }
3084 return false;
3085 }
3086
3087 if (in_bundle->bond) {
3088 struct mac_entry *mac;
3089
3090 switch (bond_check_admissibility(in_bundle->bond, in_port,
3091 flow->dl_dst, tags)) {
3092 case BV_ACCEPT:
3093 break;
3094
3095 case BV_DROP:
3096 return false;
3097
3098 case BV_DROP_IF_MOVED:
3099 mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3100 if (mac && mac->port.p != in_bundle &&
3101 (!is_gratuitous_arp(flow)
3102 || mac_entry_is_grat_arp_locked(mac))) {
3103 return false;
3104 }
3105 break;
3106 }
3107 }
3108
3109 return true;
3110 }
3111
3112 /* If the composed actions may be applied to any packet in the given 'flow',
3113 * returns true. Otherwise, the actions should only be applied to 'packet', or
3114 * not at all, if 'packet' was NULL. */
3115 static bool
3116 xlate_normal(struct action_xlate_ctx *ctx)
3117 {
3118 struct ofbundle *in_bundle;
3119 struct ofbundle *out_bundle;
3120 struct mac_entry *mac;
3121 int vlan;
3122
3123 /* Check whether we should drop packets in this flow. */
3124 if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3125 &ctx->tags, &vlan, &in_bundle)) {
3126 out_bundle = NULL;
3127 goto done;
3128 }
3129
3130 /* Learn source MAC (but don't try to learn from revalidation). */
3131 if (ctx->packet) {
3132 update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3133 }
3134
3135 /* Determine output bundle. */
3136 mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3137 &ctx->tags);
3138 if (mac) {
3139 out_bundle = mac->port.p;
3140 } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3141 /* If we are revalidating but don't have a learning entry then eject
3142 * the flow. Installing a flow that floods packets opens up a window
3143 * of time where we could learn from a packet reflected on a bond and
3144 * blackhole packets before the learning table is updated to reflect
3145 * the correct port. */
3146 return false;
3147 } else {
3148 out_bundle = OFBUNDLE_FLOOD;
3149 }
3150
3151 /* Don't send packets out their input bundles. */
3152 if (in_bundle == out_bundle) {
3153 out_bundle = NULL;
3154 }
3155
3156 done:
3157 if (in_bundle) {
3158 compose_actions(ctx, vlan, in_bundle, out_bundle);
3159 }
3160
3161 return true;
3162 }
3163 \f
3164 static void
3165 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
3166 int error)
3167 {
3168 struct ofpbuf *buf = ofputil_encode_error_msg(error, oh);
3169 if (buf) {
3170 COVERAGE_INC(ofproto_error);
3171 ofconn_send_reply(ofconn, buf);
3172 }
3173 }
3174
3175 static int
3176 handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
3177 {
3178 ofconn_send_reply(ofconn, make_echo_reply(oh));
3179 return 0;
3180 }
3181
3182 static int
3183 handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
3184 {
3185 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3186 struct ofp_switch_features *osf;
3187 struct ofpbuf *buf;
3188 struct ofport *port;
3189
3190 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
3191 osf->datapath_id = htonll(ofproto->datapath_id);
3192 osf->n_buffers = htonl(pktbuf_capacity());
3193 osf->n_tables = 2;
3194 osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
3195 OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
3196 osf->actions = htonl((1u << OFPAT_OUTPUT) |
3197 (1u << OFPAT_SET_VLAN_VID) |
3198 (1u << OFPAT_SET_VLAN_PCP) |
3199 (1u << OFPAT_STRIP_VLAN) |
3200 (1u << OFPAT_SET_DL_SRC) |
3201 (1u << OFPAT_SET_DL_DST) |
3202 (1u << OFPAT_SET_NW_SRC) |
3203 (1u << OFPAT_SET_NW_DST) |
3204 (1u << OFPAT_SET_NW_TOS) |
3205 (1u << OFPAT_SET_TP_SRC) |
3206 (1u << OFPAT_SET_TP_DST) |
3207 (1u << OFPAT_ENQUEUE));
3208
3209 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
3210 ofpbuf_put(buf, &port->opp, sizeof port->opp);
3211 }
3212
3213 ofconn_send_reply(ofconn, buf);
3214 return 0;
3215 }
3216
3217 static int
3218 handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
3219 {
3220 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3221 struct ofpbuf *buf;
3222 struct ofp_switch_config *osc;
3223 uint16_t flags;
3224 bool drop_frags;
3225
3226 /* Figure out flags. */
3227 dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3228 flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
3229
3230 /* Send reply. */
3231 osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
3232 osc->flags = htons(flags);
3233 osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn));
3234 ofconn_send_reply(ofconn, buf);
3235
3236 return 0;
3237 }
3238
3239 static int
3240 handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
3241 {
3242 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
3243 uint16_t flags = ntohs(osc->flags);
3244
3245 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
3246 && ofconn_get_role(ofconn) != NX_ROLE_SLAVE) {
3247 switch (flags & OFPC_FRAG_MASK) {
3248 case OFPC_FRAG_NORMAL:
3249 dpif_set_drop_frags(ofproto->dpif, false);
3250 break;
3251 case OFPC_FRAG_DROP:
3252 dpif_set_drop_frags(ofproto->dpif, true);
3253 break;
3254 default:
3255 VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
3256 osc->flags);
3257 break;
3258 }
3259 }
3260
3261 ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len));
3262
3263 return 0;
3264 }
3265
3266 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
3267 struct action_xlate_ctx *ctx);
3268
3269 static void
3270 add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
3271 {
3272 const struct ofport *ofport = get_port(ctx->ofproto, port);
3273
3274 if (ofport) {
3275 if (ofport->opp.config & htonl(OFPPC_NO_FWD)) {
3276 /* Forwarding disabled on port. */
3277 return;
3278 }
3279 } else {
3280 /*
3281 * We don't have an ofport record for this port, but it doesn't hurt to
3282 * allow forwarding to it anyhow. Maybe such a port will appear later
3283 * and we're pre-populating the flow table.
3284 */
3285 }
3286
3287 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, port);
3288 ctx->nf_output_iface = port;
3289 }
3290
3291 static struct rule *
3292 rule_lookup(struct ofproto *ofproto, const struct flow *flow)
3293 {
3294 return rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
3295 }
3296
3297 static void
3298 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
3299 {
3300 if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
3301 uint16_t old_in_port;
3302 struct rule *rule;
3303
3304 /* Look up a flow with 'in_port' as the input port. Then restore the
3305 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3306 * have surprising behavior). */
3307 old_in_port = ctx->flow.in_port;
3308 ctx->flow.in_port = in_port;
3309 rule = rule_lookup(ctx->ofproto, &ctx->flow);
3310 ctx->flow.in_port = old_in_port;
3311
3312 if (ctx->resubmit_hook) {
3313 ctx->resubmit_hook(ctx, rule);
3314 }
3315
3316 if (rule) {
3317 ctx->recurse++;
3318 do_xlate_actions(rule->actions, rule->n_actions, ctx);
3319 ctx->recurse--;
3320 }
3321 } else {
3322 static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
3323
3324 VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
3325 MAX_RESUBMIT_RECURSION);
3326 }
3327 }
3328
3329 static void
3330 flood_packets(struct ofproto *ofproto, uint16_t odp_in_port, ovs_be32 mask,
3331 uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
3332 {
3333 struct ofport *ofport;
3334
3335 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
3336 uint16_t odp_port = ofport->odp_port;
3337 if (odp_port != odp_in_port && !(ofport->opp.config & mask)) {
3338 nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
3339 }
3340 }
3341 *nf_output_iface = NF_OUT_FLOOD;
3342 }
3343
3344 static void
3345 xlate_output_action__(struct action_xlate_ctx *ctx,
3346 uint16_t port, uint16_t max_len)
3347 {
3348 uint16_t odp_port;
3349 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
3350
3351 ctx->nf_output_iface = NF_OUT_DROP;
3352
3353 switch (port) {
3354 case OFPP_IN_PORT:
3355 add_output_action(ctx, ctx->flow.in_port);
3356 break;
3357 case OFPP_TABLE:
3358 xlate_table_action(ctx, ctx->flow.in_port);
3359 break;
3360 case OFPP_NORMAL:
3361 xlate_normal(ctx);
3362 break;
3363 case OFPP_FLOOD:
3364 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD),
3365 &ctx->nf_output_iface, ctx->odp_actions);
3366 break;
3367 case OFPP_ALL:
3368 flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0),
3369 &ctx->nf_output_iface, ctx->odp_actions);
3370 break;
3371 case OFPP_CONTROLLER:
3372 nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
3373 break;
3374 case OFPP_LOCAL:
3375 add_output_action(ctx, ODPP_LOCAL);
3376 break;
3377 default:
3378 odp_port = ofp_port_to_odp_port(port);
3379 if (odp_port != ctx->flow.in_port) {
3380 add_output_action(ctx, odp_port);
3381 }
3382 break;
3383 }
3384
3385 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3386 ctx->nf_output_iface = NF_OUT_FLOOD;
3387 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3388 ctx->nf_output_iface = prev_nf_output_iface;
3389 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3390 ctx->nf_output_iface != NF_OUT_FLOOD) {
3391 ctx->nf_output_iface = NF_OUT_MULTI;
3392 }
3393 }
3394
3395 static void
3396 xlate_output_action(struct action_xlate_ctx *ctx,
3397 const struct ofp_action_output *oao)
3398 {
3399 xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
3400 }
3401
3402 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
3403 * optimization, because we're going to add another action that sets the
3404 * priority immediately after, or because there are no actions following the
3405 * pop. */
3406 static void
3407 remove_pop_action(struct action_xlate_ctx *ctx)
3408 {
3409 if (ctx->odp_actions->size == ctx->last_pop_priority) {
3410 ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
3411 ctx->last_pop_priority = -1;
3412 }
3413 }
3414
3415 static void
3416 add_pop_action(struct action_xlate_ctx *ctx)
3417 {
3418 if (ctx->odp_actions->size != ctx->last_pop_priority) {
3419 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
3420 ctx->last_pop_priority = ctx->odp_actions->size;
3421 }
3422 }
3423
3424 static void
3425 xlate_enqueue_action(struct action_xlate_ctx *ctx,
3426 const struct ofp_action_enqueue *oae)
3427 {
3428 uint16_t ofp_port, odp_port;
3429 uint32_t priority;
3430 int error;
3431
3432 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
3433 &priority);
3434 if (error) {
3435 /* Fall back to ordinary output action. */
3436 xlate_output_action__(ctx, ntohs(oae->port), 0);
3437 return;
3438 }
3439
3440 /* Figure out ODP output port. */
3441 ofp_port = ntohs(oae->port);
3442 if (ofp_port != OFPP_IN_PORT) {
3443 odp_port = ofp_port_to_odp_port(ofp_port);
3444 } else {
3445 odp_port = ctx->flow.in_port;
3446 }
3447
3448 /* Add ODP actions. */
3449 remove_pop_action(ctx);
3450 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
3451 add_output_action(ctx, odp_port);
3452 add_pop_action(ctx);
3453
3454 /* Update NetFlow output port. */
3455 if (ctx->nf_output_iface == NF_OUT_DROP) {
3456 ctx->nf_output_iface = odp_port;
3457 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3458 ctx->nf_output_iface = NF_OUT_MULTI;
3459 }
3460 }
3461
3462 static void
3463 xlate_set_queue_action(struct action_xlate_ctx *ctx,
3464 const struct nx_action_set_queue *nasq)
3465 {
3466 uint32_t priority;
3467 int error;
3468
3469 error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
3470 &priority);
3471 if (error) {
3472 /* Couldn't translate queue to a priority, so ignore. A warning
3473 * has already been logged. */
3474 return;
3475 }
3476
3477 remove_pop_action(ctx);
3478 nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
3479 }
3480
3481 static void
3482 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
3483 {
3484 ovs_be16 tci = ctx->flow.vlan_tci;
3485 if (!(tci & htons(VLAN_CFI))) {
3486 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3487 } else {
3488 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
3489 tci & ~htons(VLAN_CFI));
3490 }
3491 }
3492
3493 struct xlate_reg_state {
3494 ovs_be16 vlan_tci;
3495 ovs_be64 tun_id;
3496 };
3497
3498 static void
3499 save_reg_state(const struct action_xlate_ctx *ctx,
3500 struct xlate_reg_state *state)
3501 {
3502 state->vlan_tci = ctx->flow.vlan_tci;
3503 state->tun_id = ctx->flow.tun_id;
3504 }
3505
3506 static void
3507 update_reg_state(struct action_xlate_ctx *ctx,
3508 const struct xlate_reg_state *state)
3509 {
3510 if (ctx->flow.vlan_tci != state->vlan_tci) {
3511 xlate_set_dl_tci(ctx);
3512 }
3513 if (ctx->flow.tun_id != state->tun_id) {
3514 nl_msg_put_be64(ctx->odp_actions,
3515 ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
3516 }
3517 }
3518
3519 static void
3520 xlate_autopath(struct action_xlate_ctx *ctx,
3521 const struct nx_action_autopath *naa)
3522 {
3523 uint16_t ofp_port = ntohl(naa->id);
3524 struct ofport *port;
3525
3526 port = get_port(ctx->ofproto, ofp_port_to_odp_port(ofp_port));
3527 if (!port || !port->bundle) {
3528 ofp_port = OFPP_NONE;
3529 } else if (port->bundle->bond) {
3530 /* Autopath does not support VLAN hashing. */
3531 struct ofport *slave = bond_choose_output_slave(
3532 port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
3533 if (slave) {
3534 ofp_port = odp_port_to_ofp_port(slave->odp_port);
3535 }
3536 }
3537 autopath_execute(naa, &ctx->flow, ofp_port);
3538 }
3539
3540 static void
3541 xlate_nicira_action(struct action_xlate_ctx *ctx,
3542 const struct nx_action_header *nah)
3543 {
3544 const struct nx_action_resubmit *nar;
3545 const struct nx_action_set_tunnel *nast;
3546 const struct nx_action_set_queue *nasq;
3547 const struct nx_action_multipath *nam;
3548 const struct nx_action_autopath *naa;
3549 enum nx_action_subtype subtype = ntohs(nah->subtype);
3550 struct xlate_reg_state state;
3551 ovs_be64 tun_id;
3552
3553 assert(nah->vendor == htonl(NX_VENDOR_ID));
3554 switch (subtype) {
3555 case NXAST_RESUBMIT:
3556 nar = (const struct nx_action_resubmit *) nah;
3557 xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
3558 break;
3559
3560 case NXAST_SET_TUNNEL:
3561 nast = (const struct nx_action_set_tunnel *) nah;
3562 tun_id = htonll(ntohl(nast->tun_id));
3563 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
3564 ctx->flow.tun_id = tun_id;
3565 break;
3566
3567 case NXAST_DROP_SPOOFED_ARP:
3568 if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
3569 nl_msg_put_flag(ctx->odp_actions,
3570 ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
3571 }
3572 break;
3573
3574 case NXAST_SET_QUEUE:
3575 nasq = (const struct nx_action_set_queue *) nah;
3576 xlate_set_queue_action(ctx, nasq);
3577 break;
3578
3579 case NXAST_POP_QUEUE:
3580 add_pop_action(ctx);
3581 break;
3582
3583 case NXAST_REG_MOVE:
3584 save_reg_state(ctx, &state);
3585 nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
3586 &ctx->flow);
3587 update_reg_state(ctx, &state);
3588 break;
3589
3590 case NXAST_REG_LOAD:
3591 save_reg_state(ctx, &state);
3592 nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
3593 &ctx->flow);
3594 update_reg_state(ctx, &state);
3595 break;
3596
3597 case NXAST_NOTE:
3598 /* Nothing to do. */
3599 break;
3600
3601 case NXAST_SET_TUNNEL64:
3602 tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
3603 nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
3604 ctx->flow.tun_id = tun_id;
3605 break;
3606
3607 case NXAST_MULTIPATH:
3608 nam = (const struct nx_action_multipath *) nah;
3609 multipath_execute(nam, &ctx->flow);
3610 break;
3611
3612 case NXAST_AUTOPATH:
3613 naa = (const struct nx_action_autopath *) nah;
3614 xlate_autopath(ctx, naa);
3615 break;
3616
3617 /* If you add a new action here that modifies flow data, don't forget to
3618 * update the flow key in ctx->flow at the same time. */
3619
3620 case NXAST_SNAT__OBSOLETE:
3621 default:
3622 VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
3623 break;
3624 }
3625 }
3626
3627 static void
3628 do_xlate_actions(const union ofp_action *in, size_t n_in,
3629 struct action_xlate_ctx *ctx)
3630 {
3631 struct actions_iterator iter;
3632 const union ofp_action *ia;
3633 const struct ofport *port;
3634
3635 port = get_port(ctx->ofproto, ctx->flow.in_port);
3636 if (port && port->opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
3637 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
3638 ? htonl(OFPPC_NO_RECV_STP)
3639 : htonl(OFPPC_NO_RECV))) {
3640 /* Drop this flow. */
3641 return;
3642 }
3643
3644 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
3645 enum ofp_action_type type = ntohs(ia->type);
3646 const struct ofp_action_dl_addr *oada;
3647
3648 switch (type) {
3649 case OFPAT_OUTPUT:
3650 xlate_output_action(ctx, &ia->output);
3651 break;
3652
3653 case OFPAT_SET_VLAN_VID:
3654 ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
3655 ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
3656 xlate_set_dl_tci(ctx);
3657 break;
3658
3659 case OFPAT_SET_VLAN_PCP:
3660 ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
3661 ctx->flow.vlan_tci |= htons(
3662 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3663 xlate_set_dl_tci(ctx);
3664 break;
3665
3666 case OFPAT_STRIP_VLAN:
3667 ctx->flow.vlan_tci = htons(0);
3668 xlate_set_dl_tci(ctx);
3669 break;
3670
3671 case OFPAT_SET_DL_SRC:
3672 oada = ((struct ofp_action_dl_addr *) ia);
3673 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
3674 oada->dl_addr, ETH_ADDR_LEN);
3675 memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3676 break;
3677
3678 case OFPAT_SET_DL_DST:
3679 oada = ((struct ofp_action_dl_addr *) ia);
3680 nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
3681 oada->dl_addr, ETH_ADDR_LEN);
3682 memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3683 break;
3684
3685 case OFPAT_SET_NW_SRC:
3686 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
3687 ia->nw_addr.nw_addr);
3688 ctx->flow.nw_src = ia->nw_addr.nw_addr;
3689 break;
3690
3691 case OFPAT_SET_NW_DST:
3692 nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
3693 ia->nw_addr.nw_addr);
3694 ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3695 break;
3696
3697 case OFPAT_SET_NW_TOS:
3698 nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
3699 ia->nw_tos.nw_tos);
3700 ctx->flow.nw_tos = ia->nw_tos.nw_tos;
3701 break;
3702
3703 case OFPAT_SET_TP_SRC:
3704 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
3705 ia->tp_port.tp_port);
3706 ctx->flow.tp_src = ia->tp_port.tp_port;
3707 break;
3708
3709 case OFPAT_SET_TP_DST:
3710 nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
3711 ia->tp_port.tp_port);
3712 ctx->flow.tp_dst = ia->tp_port.tp_port;
3713 break;
3714
3715 case OFPAT_VENDOR:
3716 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
3717 break;
3718
3719 case OFPAT_ENQUEUE:
3720 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3721 break;
3722
3723 default:
3724 VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
3725 break;
3726 }
3727 }
3728 }
3729
3730 static void
3731 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3732 struct ofproto *ofproto, const struct flow *flow,
3733 const struct ofpbuf *packet)
3734 {
3735 ctx->ofproto = ofproto;
3736 ctx->flow = *flow;
3737 ctx->packet = packet;
3738 ctx->resubmit_hook = NULL;
3739 ctx->check_special = true;
3740 }
3741
3742 static bool
3743 ofproto_process_special(struct ofproto *ofproto, const struct flow *flow,
3744 const struct ofpbuf *packet)
3745 {
3746 if (cfm_should_process_flow(flow)) {
3747 struct ofport *ofport = get_port(ofproto, flow->in_port);
3748 if (ofport && ofport->cfm) {
3749 cfm_process_heartbeat(ofport->cfm, packet);
3750 }
3751 return true;
3752 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
3753 struct ofport *port = get_port(ofproto, flow->in_port);
3754 if (port && port->bundle && port->bundle->lacp) {
3755 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
3756 if (pdu) {
3757 lacp_process_pdu(port->bundle->lacp, port, pdu);
3758 }
3759 return true;
3760 }
3761 }
3762 return false;
3763 }
3764
3765 static struct ofpbuf *
3766 xlate_actions(struct action_xlate_ctx *ctx,
3767 const union ofp_action *in, size_t n_in)
3768 {
3769 COVERAGE_INC(ofproto_ofp2odp);
3770
3771 ctx->odp_actions = ofpbuf_new(512);
3772 ctx->tags = 0;
3773 ctx->may_set_up_flow = true;
3774 ctx->nf_output_iface = NF_OUT_DROP;
3775 ctx->recurse = 0;
3776 ctx->last_pop_priority = -1;
3777
3778 if (ctx->check_special
3779 && ofproto_process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3780 ctx->may_set_up_flow = false;
3781 } else {
3782 do_xlate_actions(in, n_in, ctx);
3783 }
3784
3785 remove_pop_action(ctx);
3786
3787 /* Check with in-band control to see if we're allowed to set up this
3788 * flow. */
3789 if (!connmgr_may_set_up_flow(ctx->ofproto->connmgr, &ctx->flow,
3790 ctx->odp_actions->data,
3791 ctx->odp_actions->size)) {
3792 ctx->may_set_up_flow = false;
3793 }
3794
3795 return ctx->odp_actions;
3796 }
3797
3798 /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
3799 * error message code (composed with ofp_mkerr()) for the caller to propagate
3800 * upward. Otherwise, returns 0.
3801 *
3802 * The log message mentions 'msg_type'. */
3803 static int
3804 reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
3805 {
3806 if (ofconn_get_type(ofconn) == OFCONN_PRIMARY
3807 && ofconn_get_role(ofconn) == NX_ROLE_SLAVE) {
3808 static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
3809 VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
3810 msg_type);
3811
3812 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
3813 } else {
3814 return 0;
3815 }
3816 }
3817
3818 static int
3819 handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
3820 {
3821 struct ofproto *p = ofconn_get_ofproto(ofconn);
3822 struct ofp_packet_out *opo;
3823 struct ofpbuf payload, *buffer;
3824 union ofp_action *ofp_actions;
3825 struct action_xlate_ctx ctx;
3826 struct ofpbuf *odp_actions;
3827 struct ofpbuf request;
3828 struct flow flow;
3829 size_t n_ofp_actions;
3830 uint16_t in_port;
3831 int error;
3832
3833 COVERAGE_INC(ofproto_packet_out);
3834
3835 error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
3836 if (error) {
3837 return error;
3838 }
3839
3840 /* Get ofp_packet_out. */
3841 ofpbuf_use_const(&request, oh, ntohs(oh->length));
3842 opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
3843
3844 /* Get actions. */
3845 error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
3846 &ofp_actions, &n_ofp_actions);
3847 if (error) {
3848 return error;
3849 }
3850
3851 /* Get payload. */
3852 if (opo->buffer_id != htonl(UINT32_MAX)) {
3853 error = ofconn_pktbuf_retrieve(ofconn, ntohl(opo->buffer_id),
3854 &buffer, &in_port);
3855 if (error || !buffer) {
3856 return error;
3857 }
3858 payload = *buffer;
3859 } else {
3860 payload = request;
3861 buffer = NULL;
3862 }
3863
3864 /* Extract flow, check actions. */
3865 flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
3866 &flow);
3867 error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
3868 if (error) {
3869 goto exit;
3870 }
3871
3872 /* Send. */
3873 action_xlate_ctx_init(&ctx, p, &flow, &payload);
3874 odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3875 dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
3876 ofpbuf_delete(odp_actions);
3877
3878 exit:
3879 ofpbuf_delete(buffer);
3880 return 0;
3881 }
3882
3883 static void
3884 update_port_config(struct ofproto *p, struct ofport *port,
3885 ovs_be32 config, ovs_be32 mask)
3886 {
3887 mask &= config ^ port->opp.config;
3888 if (mask & htonl(OFPPC_PORT_DOWN)) {
3889 if (config & htonl(OFPPC_PORT_DOWN)) {
3890 netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
3891 } else {
3892 netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
3893 }
3894 }
3895 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
3896 OFPPC_NO_FWD | OFPPC_NO_FLOOD)
3897 if (mask & htonl(REVALIDATE_BITS)) {
3898 COVERAGE_INC(ofproto_costly_flags);
3899 port->opp.config ^= mask & htonl(REVALIDATE_BITS);
3900 p->need_revalidate = true;
3901 }
3902 #undef REVALIDATE_BITS
3903 if (mask & htonl(OFPPC_NO_PACKET_IN)) {
3904 port->opp.config ^= htonl(OFPPC_NO_PACKET_IN);
3905 }
3906 }
3907
3908 static int
3909 handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
3910 {
3911 struct ofproto *p = ofconn_get_ofproto(ofconn);
3912 const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
3913 struct ofport *port;
3914 int error;
3915
3916 error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
3917 if (error) {
3918 return error;
3919 }
3920
3921 port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
3922 if (!port) {
3923 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
3924 } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
3925 return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
3926 } else {
3927 update_port_config(p, port, opm->config, opm->mask);
3928 if (opm->advertise) {
3929 netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
3930 }
3931 }
3932 return 0;
3933 }
3934
3935 static struct ofpbuf *
3936 make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
3937 {
3938 struct ofp_stats_reply *osr;
3939 struct ofpbuf *msg;
3940
3941 msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
3942 osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
3943 osr->type = type;
3944 osr->flags = htons(0);
3945 return msg;
3946 }
3947
3948 static struct ofpbuf *
3949 start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
3950 {
3951 const struct ofp_stats_request *osr
3952 = (const struct ofp_stats_request *) request;
3953 return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
3954 }
3955
3956 static void *
3957 append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
3958 struct ofpbuf **msgp)
3959 {
3960 struct ofpbuf *msg = *msgp;
3961 assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
3962 if (nbytes + msg->size > UINT16_MAX) {
3963 struct ofp_stats_reply *reply = msg->data;
3964 reply->flags = htons(OFPSF_REPLY_MORE);
3965 *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
3966 ofconn_send_reply(ofconn, msg);
3967 }
3968 return ofpbuf_put_uninit(*msgp, nbytes);
3969 }
3970
3971 static struct ofpbuf *
3972 make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
3973 {
3974 struct nicira_stats_msg *nsm;
3975 struct ofpbuf *msg;
3976
3977 msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
3978 nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
3979 nsm->type = htons(OFPST_VENDOR);
3980 nsm->flags = htons(0);
3981 nsm->vendor = htonl(NX_VENDOR_ID);
3982 nsm->subtype = subtype;
3983 return msg;
3984 }
3985
3986 static struct ofpbuf *
3987 start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
3988 {
3989 return make_nxstats_reply(request->header.xid, request->subtype, body_len);
3990 }
3991
3992 static void
3993 append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
3994 struct ofpbuf **msgp)
3995 {
3996 struct ofpbuf *msg = *msgp;
3997 assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
3998 if (nbytes + msg->size > UINT16_MAX) {
3999 struct nicira_stats_msg *reply = msg->data;
4000 reply->flags = htons(OFPSF_REPLY_MORE);
4001 *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
4002 ofconn_send_reply(ofconn, msg);
4003 }
4004 ofpbuf_prealloc_tailroom(*msgp, nbytes);
4005 }
4006
4007 static int
4008 handle_desc_stats_request(struct ofconn *ofconn,
4009 const struct ofp_header *request)
4010 {
4011 struct ofproto *p = ofconn_get_ofproto(ofconn);
4012 struct ofp_desc_stats *ods;
4013 struct ofpbuf *msg;
4014
4015 msg = start_ofp_stats_reply(request, sizeof *ods);
4016 ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
4017 memset(ods, 0, sizeof *ods);
4018 ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
4019 ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
4020 ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
4021 ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
4022 ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
4023 ofconn_send_reply(ofconn, msg);
4024
4025 return 0;
4026 }
4027
4028 static int
4029 handle_table_stats_request(struct ofconn *ofconn,
4030 const struct ofp_header *request)
4031 {
4032 struct ofproto *p = ofconn_get_ofproto(ofconn);
4033 struct ofp_table_stats *ots;
4034 struct ofpbuf *msg;
4035
4036 msg = start_ofp_stats_reply(request, sizeof *ots * 2);
4037
4038 /* Classifier table. */
4039 ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
4040 memset(ots, 0, sizeof *ots);
4041 strcpy(ots->name, "classifier");
4042 ots->wildcards = (ofconn_get_flow_format(ofconn) == NXFF_OPENFLOW10
4043 ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
4044 ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
4045 ots->active_count = htonl(classifier_count(&p->cls));
4046 put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
4047 put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
4048
4049 ofconn_send_reply(ofconn, msg);
4050 return 0;
4051 }
4052
4053 static void
4054 append_port_stat(struct ofport *port, struct ofconn *ofconn,
4055 struct ofpbuf **msgp)
4056 {
4057 struct netdev_stats stats;
4058 struct ofp_port_stats *ops;
4059
4060 /* Intentionally ignore return value, since errors will set
4061 * 'stats' to all-1s, which is correct for OpenFlow, and
4062 * netdev_get_stats() will log errors. */
4063 netdev_get_stats(port->netdev, &stats);
4064
4065 ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
4066 ops->port_no = port->opp.port_no;
4067 memset(ops->pad, 0, sizeof ops->pad);
4068 put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
4069 put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
4070 put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
4071 put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
4072 put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
4073 put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
4074 put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
4075 put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
4076 put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
4077 put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
4078 put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
4079 put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
4080 }
4081
4082 static int
4083 handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
4084 {
4085 struct ofproto *p = ofconn_get_ofproto(ofconn);
4086 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
4087 struct ofp_port_stats *ops;
4088 struct ofpbuf *msg;
4089 struct ofport *port;
4090
4091 msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
4092 if (psr->port_no != htons(OFPP_NONE)) {
4093 port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
4094 if (port) {
4095 append_port_stat(port, ofconn, &msg);
4096 }
4097 } else {
4098 HMAP_FOR_EACH (port, hmap_node, &p->ports) {
4099 append_port_stat(port, ofconn, &msg);
4100 }
4101 }
4102
4103 ofconn_send_reply(ofconn, msg);
4104 return 0;
4105 }
4106
4107 static void
4108 calc_flow_duration__(long long int start, uint32_t *sec, uint32_t *nsec)
4109 {
4110 long long int msecs = time_msec() - start;
4111 *sec = msecs / 1000;
4112 *nsec = (msecs % 1000) * (1000 * 1000);
4113 }
4114
4115 static void
4116 calc_flow_duration(long long int start, ovs_be32 *sec_be, ovs_be32 *nsec_be)
4117 {
4118 uint32_t sec, nsec;
4119
4120 calc_flow_duration__(start, &sec, &nsec);
4121 *sec_be = htonl(sec);
4122 *nsec_be = htonl(nsec);
4123 }
4124
4125 static void
4126 put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
4127 ovs_be16 out_port, struct ofpbuf **replyp)
4128 {
4129 struct ofp_flow_stats *ofs;
4130 uint64_t packet_count, byte_count;
4131 ovs_be64 cookie;
4132 size_t act_len, len;
4133
4134 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
4135 return;
4136 }
4137
4138 act_len = sizeof *rule->actions * rule->n_actions;
4139 len = offsetof(struct ofp_flow_stats, actions) + act_len;
4140
4141 rule_get_stats(rule, &packet_count, &byte_count);
4142
4143 ofs = append_ofp_stats_reply(len, ofconn, replyp);
4144 ofs->length = htons(len);
4145 ofs->table_id = 0;
4146 ofs->pad = 0;
4147 ofputil_cls_rule_to_match(&rule->cr, ofconn_get_flow_format(ofconn),
4148 &ofs->match, rule->flow_cookie, &cookie);
4149 put_32aligned_be64(&ofs->cookie, cookie);
4150 calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
4151 ofs->priority = htons(rule->cr.priority);
4152 ofs->idle_timeout = htons(rule->idle_timeout);
4153 ofs->hard_timeout = htons(rule->hard_timeout);
4154 memset(ofs->pad2, 0, sizeof ofs->pad2);
4155 put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
4156 put_32aligned_be64(&ofs->byte_count, htonll(byte_count));
4157 if (rule->n_actions > 0) {
4158 memcpy(ofs->actions, rule->actions, act_len);
4159 }
4160 }
4161
4162 static bool
4163 is_valid_table(uint8_t table_id)
4164 {
4165 if (table_id == 0 || table_id == 0xff) {
4166 return true;
4167 } else {
4168 /* It would probably be better to reply with an error but there doesn't
4169 * seem to be any appropriate value, so that might just be
4170 * confusing. */
4171 VLOG_WARN_RL(&rl, "controller asked for invalid table %"PRIu8,
4172 table_id);
4173 return false;
4174 }
4175 }
4176
4177 static int
4178 handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
4179 {
4180 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
4181 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4182 struct ofpbuf *reply;
4183
4184 COVERAGE_INC(ofproto_flows_req);
4185 reply = start_ofp_stats_reply(oh, 1024);
4186 if (is_valid_table(fsr->table_id)) {
4187 struct cls_cursor cursor;
4188 struct cls_rule target;
4189 struct rule *rule;
4190
4191 ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
4192 &target);
4193 cls_cursor_init(&cursor, &ofproto->cls, &target);
4194 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4195 put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
4196 }
4197 }
4198 ofconn_send_reply(ofconn, reply);
4199
4200 return 0;
4201 }
4202
4203 static void
4204 put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
4205 ovs_be16 out_port, struct ofpbuf **replyp)
4206 {
4207 struct nx_flow_stats *nfs;
4208 uint64_t packet_count, byte_count;
4209 size_t act_len, start_len;
4210 struct ofpbuf *reply;
4211
4212 if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
4213 return;
4214 }
4215
4216 rule_get_stats(rule, &packet_count, &byte_count);
4217
4218 act_len = sizeof *rule->actions * rule->n_actions;
4219
4220 append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
4221 start_len = (*replyp)->size;
4222 reply = *replyp;
4223
4224 nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
4225 nfs->table_id = 0;
4226 nfs->pad = 0;
4227 calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
4228 nfs->cookie = rule->flow_cookie;
4229 nfs->priority = htons(rule->cr.priority);
4230 nfs->idle_timeout = htons(rule->idle_timeout);
4231 nfs->hard_timeout = htons(rule->hard_timeout);
4232 nfs->match_len = htons(nx_put_match(reply, &rule->cr));
4233 memset(nfs->pad2, 0, sizeof nfs->pad2);
4234 nfs->packet_count = htonll(packet_count);
4235 nfs->byte_count = htonll(byte_count);
4236 if (rule->n_actions > 0) {
4237 ofpbuf_put(reply, rule->actions, act_len);
4238 }
4239 nfs->length = htons(reply->size - start_len);
4240 }
4241
4242 static int
4243 handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
4244 {
4245 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4246 struct nx_flow_stats_request *nfsr;
4247 struct cls_rule target;
4248 struct ofpbuf *reply;
4249 struct ofpbuf b;
4250 int error;
4251
4252 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4253
4254 /* Dissect the message. */
4255 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
4256 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
4257 if (error) {
4258 return error;
4259 }
4260 if (b.size) {
4261 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
4262 }
4263
4264 COVERAGE_INC(ofproto_flows_req);
4265 reply = start_nxstats_reply(&nfsr->nsm, 1024);
4266 if (is_valid_table(nfsr->table_id)) {
4267 struct cls_cursor cursor;
4268 struct rule *rule;
4269
4270 cls_cursor_init(&cursor, &ofproto->cls, &target);
4271 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4272 put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
4273 }
4274 }
4275 ofconn_send_reply(ofconn, reply);
4276
4277 return 0;
4278 }
4279
4280 static void
4281 flow_stats_ds(struct rule *rule, struct ds *results)
4282 {
4283 uint64_t packet_count, byte_count;
4284 size_t act_len = sizeof *rule->actions * rule->n_actions;
4285
4286 rule_get_stats(rule, &packet_count, &byte_count);
4287
4288 ds_put_format(results, "duration=%llds, ",
4289 (time_msec() - rule->created) / 1000);
4290 ds_put_format(results, "idle=%.3fs, ", (time_msec() - rule->used) / 1000.0);
4291 ds_put_format(results, "priority=%u, ", rule->cr.priority);
4292 ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
4293 ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
4294 cls_rule_format(&rule->cr, results);
4295 ds_put_char(results, ',');
4296 if (act_len > 0) {
4297 ofp_print_actions(results, &rule->actions->header, act_len);
4298 } else {
4299 ds_put_cstr(results, "drop");
4300 }
4301 ds_put_cstr(results, "\n");
4302 }
4303
4304 /* Adds a pretty-printed description of all flows to 'results', including
4305 * hidden flows (e.g., set up by in-band control). */
4306 void
4307 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
4308 {
4309 struct cls_cursor cursor;
4310 struct rule *rule;
4311
4312 cls_cursor_init(&cursor, &p->cls, NULL);
4313 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4314 flow_stats_ds(rule, results);
4315 }
4316 }
4317
4318 /* Obtains the NetFlow engine type and engine ID for 'ofproto' into
4319 * '*engine_type' and '*engine_id', respectively. */
4320 void
4321 ofproto_get_netflow_ids(const struct ofproto *ofproto,
4322 uint8_t *engine_type, uint8_t *engine_id)
4323 {
4324 dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
4325 }
4326
4327 static void
4328 query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
4329 ovs_be16 out_port, uint8_t table_id,
4330 struct ofp_aggregate_stats_reply *oasr)
4331 {
4332 uint64_t total_packets = 0;
4333 uint64_t total_bytes = 0;
4334 int n_flows = 0;
4335
4336 COVERAGE_INC(ofproto_agg_request);
4337
4338 if (is_valid_table(table_id)) {
4339 struct cls_cursor cursor;
4340 struct rule *rule;
4341
4342 cls_cursor_init(&cursor, &ofproto->cls, target);
4343 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4344 if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
4345 uint64_t packet_count;
4346 uint64_t byte_count;
4347
4348 rule_get_stats(rule, &packet_count, &byte_count);
4349
4350 total_packets += packet_count;
4351 total_bytes += byte_count;
4352 n_flows++;
4353 }
4354 }
4355 }
4356
4357 oasr->flow_count = htonl(n_flows);
4358 put_32aligned_be64(&oasr->packet_count, htonll(total_packets));
4359 put_32aligned_be64(&oasr->byte_count, htonll(total_bytes));
4360 memset(oasr->pad, 0, sizeof oasr->pad);
4361 }
4362
4363 static int
4364 handle_aggregate_stats_request(struct ofconn *ofconn,
4365 const struct ofp_header *oh)
4366 {
4367 const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
4368 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4369 struct ofp_aggregate_stats_reply *reply;
4370 struct cls_rule target;
4371 struct ofpbuf *msg;
4372
4373 ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
4374 &target);
4375
4376 msg = start_ofp_stats_reply(oh, sizeof *reply);
4377 reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
4378 query_aggregate_stats(ofproto, &target, request->out_port,
4379 request->table_id, reply);
4380 ofconn_send_reply(ofconn, msg);
4381 return 0;
4382 }
4383
4384 static int
4385 handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
4386 {
4387 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4388 struct nx_aggregate_stats_request *request;
4389 struct ofp_aggregate_stats_reply *reply;
4390 struct cls_rule target;
4391 struct ofpbuf b;
4392 struct ofpbuf *buf;
4393 int error;
4394
4395 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4396
4397 /* Dissect the message. */
4398 request = ofpbuf_pull(&b, sizeof *request);
4399 error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
4400 if (error) {
4401 return error;
4402 }
4403 if (b.size) {
4404 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
4405 }
4406
4407 /* Reply. */
4408 COVERAGE_INC(ofproto_flows_req);
4409 buf = start_nxstats_reply(&request->nsm, sizeof *reply);
4410 reply = ofpbuf_put_uninit(buf, sizeof *reply);
4411 query_aggregate_stats(ofproto, &target, request->out_port,
4412 request->table_id, reply);
4413 ofconn_send_reply(ofconn, buf);
4414
4415 return 0;
4416 }
4417
4418 struct queue_stats_cbdata {
4419 struct ofconn *ofconn;
4420 struct ofport *ofport;
4421 struct ofpbuf *msg;
4422 };
4423
4424 static void
4425 put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
4426 const struct netdev_queue_stats *stats)
4427 {
4428 struct ofp_queue_stats *reply;
4429
4430 reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
4431 reply->port_no = cbdata->ofport->opp.port_no;
4432 memset(reply->pad, 0, sizeof reply->pad);
4433 reply->queue_id = htonl(queue_id);
4434 put_32aligned_be64(&reply->tx_bytes, htonll(stats->tx_bytes));
4435 put_32aligned_be64(&reply->tx_packets, htonll(stats->tx_packets));
4436 put_32aligned_be64(&reply->tx_errors, htonll(stats->tx_errors));
4437 }
4438
4439 static void
4440 handle_queue_stats_dump_cb(uint32_t queue_id,
4441 struct netdev_queue_stats *stats,
4442 void *cbdata_)
4443 {
4444 struct queue_stats_cbdata *cbdata = cbdata_;
4445
4446 put_queue_stats(cbdata, queue_id, stats);
4447 }
4448
4449 static void
4450 handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
4451 struct queue_stats_cbdata *cbdata)
4452 {
4453 cbdata->ofport = port;
4454 if (queue_id == OFPQ_ALL) {
4455 netdev_dump_queue_stats(port->netdev,
4456 handle_queue_stats_dump_cb, cbdata);
4457 } else {
4458 struct netdev_queue_stats stats;
4459
4460 if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
4461 put_queue_stats(cbdata, queue_id, &stats);
4462 }
4463 }
4464 }
4465
4466 static int
4467 handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
4468 {
4469 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4470 const struct ofp_queue_stats_request *qsr;
4471 struct queue_stats_cbdata cbdata;
4472 struct ofport *port;
4473 unsigned int port_no;
4474 uint32_t queue_id;
4475
4476 qsr = ofputil_stats_body(oh);
4477 if (!qsr) {
4478 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
4479 }
4480
4481 COVERAGE_INC(ofproto_queue_req);
4482
4483 cbdata.ofconn = ofconn;
4484 cbdata.msg = start_ofp_stats_reply(oh, 128);
4485
4486 port_no = ntohs(qsr->port_no);
4487 queue_id = ntohl(qsr->queue_id);
4488 if (port_no == OFPP_ALL) {
4489 HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
4490 handle_queue_stats_for_port(port, queue_id, &cbdata);
4491 }
4492 } else if (port_no < ofproto->max_ports) {
4493 port = get_port(ofproto, ofp_port_to_odp_port(port_no));
4494 if (port) {
4495 handle_queue_stats_for_port(port, queue_id, &cbdata);
4496 }
4497 } else {
4498 ofpbuf_delete(cbdata.msg);
4499 return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
4500 }
4501 ofconn_send_reply(ofconn, cbdata.msg);
4502
4503 return 0;
4504 }
4505
4506 /* Updates 'facet''s used time. Caller is responsible for calling
4507 * facet_push_stats() to update the flows which 'facet' resubmits into. */
4508 static void
4509 facet_update_time(struct ofproto *ofproto, struct facet *facet,
4510 long long int used)
4511 {
4512 if (used > facet->used) {
4513 facet->used = used;
4514 if (used > facet->rule->used) {
4515 facet->rule->used = used;
4516 }
4517 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
4518 }
4519 }
4520
4521 /* Folds the statistics from 'stats' into the counters in 'facet'.
4522 *
4523 * Because of the meaning of a facet's counters, it only makes sense to do this
4524 * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
4525 * packet that was sent by hand or if it represents statistics that have been
4526 * cleared out of the datapath. */
4527 static void
4528 facet_update_stats(struct ofproto *ofproto, struct facet *facet,
4529 const struct dpif_flow_stats *stats)
4530 {
4531 if (stats->n_packets || stats->used > facet->used) {
4532 facet_update_time(ofproto, facet, stats->used);
4533 facet->packet_count += stats->n_packets;
4534 facet->byte_count += stats->n_bytes;
4535 facet_push_stats(ofproto, facet);
4536 netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
4537 }
4538 }
4539
4540 static void
4541 facet_push_stats(struct ofproto *ofproto, struct facet *facet)
4542 {
4543 uint64_t rs_packets, rs_bytes;
4544
4545 assert(facet->packet_count >= facet->rs_packet_count);
4546 assert(facet->byte_count >= facet->rs_byte_count);
4547 assert(facet->used >= facet->rs_used);
4548
4549 rs_packets = facet->packet_count - facet->rs_packet_count;
4550 rs_bytes = facet->byte_count - facet->rs_byte_count;
4551
4552 if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
4553 facet->rs_packet_count = facet->packet_count;
4554 facet->rs_byte_count = facet->byte_count;
4555 facet->rs_used = facet->used;
4556
4557 flow_push_stats(ofproto, facet->rule, &facet->flow,
4558 rs_packets, rs_bytes, facet->used);
4559 }
4560 }
4561
4562 struct ofproto_push {
4563 struct action_xlate_ctx ctx;
4564 uint64_t packets;
4565 uint64_t bytes;
4566 long long int used;
4567 };
4568
4569 static void
4570 push_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
4571 {
4572 struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
4573
4574 if (rule) {
4575 rule->packet_count += push->packets;
4576 rule->byte_count += push->bytes;
4577 rule->used = MAX(push->used, rule->used);
4578 }
4579 }
4580
4581 /* Pushes flow statistics to the rules which 'flow' resubmits into given
4582 * 'rule''s actions. */
4583 static void
4584 flow_push_stats(struct ofproto *ofproto, const struct rule *rule,
4585 struct flow *flow, uint64_t packets, uint64_t bytes,
4586 long long int used)
4587 {
4588 struct ofproto_push push;
4589
4590 push.packets = packets;
4591 push.bytes = bytes;
4592 push.used = used;
4593
4594 action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
4595 push.ctx.resubmit_hook = push_resubmit;
4596 ofpbuf_delete(xlate_actions(&push.ctx, rule->actions, rule->n_actions));
4597 }
4598
4599 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
4600 * in which no matching flow already exists in the flow table.
4601 *
4602 * Adds the flow specified by 'ofm', which is followed by 'n_actions'
4603 * ofp_actions, to the ofproto's flow table. Returns 0 on success or an
4604 * OpenFlow error code as encoded by ofp_mkerr() on failure.
4605 *
4606 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
4607 * if any. */
4608 static int
4609 add_flow(struct ofconn *ofconn, struct flow_mod *fm)
4610 {
4611 struct ofproto *p = ofconn_get_ofproto(ofconn);
4612 struct ofpbuf *packet;
4613 struct rule *rule;
4614 uint16_t in_port;
4615 int error;
4616
4617 if (fm->flags & OFPFF_CHECK_OVERLAP
4618 && classifier_rule_overlaps(&p->cls, &fm->cr)) {
4619 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
4620 }
4621
4622 error = 0;
4623 if (fm->buffer_id != UINT32_MAX) {
4624 error = ofconn_pktbuf_retrieve(ofconn, fm->buffer_id,
4625 &packet, &in_port);
4626 } else {
4627 packet = NULL;
4628 in_port = UINT16_MAX;
4629 }
4630
4631 rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
4632 fm->idle_timeout, fm->hard_timeout, fm->cookie,
4633 fm->flags & OFPFF_SEND_FLOW_REM);
4634 rule_insert(p, rule);
4635 if (packet) {
4636 rule_execute(p, rule, in_port, packet);
4637 }
4638 return error;
4639 }
4640
4641 static struct rule *
4642 find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
4643 {
4644 return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
4645 }
4646
4647 static int
4648 send_buffered_packet(struct ofconn *ofconn,
4649 struct rule *rule, uint32_t buffer_id)
4650 {
4651 struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
4652 struct ofpbuf *packet;
4653 uint16_t in_port;
4654 int error;
4655
4656 if (buffer_id == UINT32_MAX) {
4657 return 0;
4658 }
4659
4660 error = ofconn_pktbuf_retrieve(ofconn, buffer_id, &packet, &in_port);
4661 if (error) {
4662 return error;
4663 }
4664
4665 rule_execute(ofproto, rule, in_port, packet);
4666
4667 return 0;
4668 }
4669 \f
4670 /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
4671
4672 struct modify_flows_cbdata {
4673 struct ofproto *ofproto;
4674 const struct flow_mod *fm;
4675 struct rule *match;
4676 };
4677
4678 static int modify_flow(struct ofproto *, const struct flow_mod *,
4679 struct rule *);
4680
4681 /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
4682 * encoded by ofp_mkerr() on failure.
4683 *
4684 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
4685 * if any. */
4686 static int
4687 modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
4688 {
4689 struct ofproto *p = ofconn_get_ofproto(ofconn);
4690 struct rule *match = NULL;
4691 struct cls_cursor cursor;
4692 struct rule *rule;
4693
4694 cls_cursor_init(&cursor, &p->cls, &fm->cr);
4695 CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
4696 if (!rule_is_hidden(rule)) {
4697 match = rule;
4698 modify_flow(p, fm, rule);
4699 }
4700 }
4701
4702 if (match) {
4703 /* This credits the packet to whichever flow happened to match last.
4704 * That's weird. Maybe we should do a lookup for the flow that
4705 * actually matches the packet? Who knows. */
4706 send_buffered_packet(ofconn, match, fm->buffer_id);
4707 return 0;
4708 } else {
4709 return add_flow(ofconn, fm);
4710 }
4711 }
4712
4713 /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
4714 * code as encoded by ofp_mkerr() on failure.
4715 *
4716 * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
4717 * if any. */
4718 static int
4719 modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
4720 {
4721 struct ofproto *p = ofconn_get_ofproto(ofconn);
4722 struct rule *rule = find_flow_strict(p, fm);
4723 if (rule && !rule_is_hidden(rule)) {
4724 modify_flow(p, fm, rule);
4725 return send_buffered_packet(ofconn, rule, fm->buffer_id);
4726 } else {
4727 return add_flow(ofconn, fm);
4728 }
4729 }
4730
4731 /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
4732 * been identified as a flow in 'p''s flow table to be modified, by changing
4733 * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
4734 * ofp_action[] structures). */
4735 static int
4736 modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
4737 {
4738 size_t actions_len = fm->n_actions * sizeof *rule->actions;
4739
4740 rule->flow_cookie = fm->cookie;
4741
4742 /* If the actions are the same, do nothing. */
4743 if (fm->n_actions == rule->n_actions
4744 && (!fm->n_actions
4745 || !memcmp(fm->actions, rule->actions, actions_len))) {
4746 return 0;
4747 }
4748
4749 /* Replace actions. */
4750 free(rule->actions);
4751 rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
4752 rule->n_actions = fm->n_actions;
4753
4754 p->need_revalidate = true;
4755
4756 return 0;
4757 }
4758 \f
4759 /* OFPFC_DELETE implementation. */
4760
4761 static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
4762
4763 /* Implements OFPFC_DELETE. */
4764 static void
4765 delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
4766 {
4767 struct rule *rule, *next_rule;
4768 struct cls_cursor cursor;
4769
4770 cls_cursor_init(&cursor, &p->cls, &fm->cr);
4771 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
4772 delete_flow(p, rule, htons(fm->out_port));
4773 }
4774 }
4775
4776 /* Implements OFPFC_DELETE_STRICT. */
4777 static void
4778 delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
4779 {
4780 struct rule *rule = find_flow_strict(p, fm);
4781 if (rule) {
4782 delete_flow(p, rule, htons(fm->out_port));
4783 }
4784 }
4785
4786 /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
4787 * been identified as a flow to delete from 'p''s flow table, by deleting the
4788 * flow and sending out a OFPT_FLOW_REMOVED message to any interested
4789 * controller.
4790 *
4791 * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
4792 * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
4793 * specified 'out_port'. */
4794 static void
4795 delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
4796 {
4797 if (rule_is_hidden(rule)) {
4798 return;
4799 }
4800
4801 if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
4802 return;
4803 }
4804
4805 rule_send_removed(p, rule, OFPRR_DELETE);
4806 rule_remove(p, rule);
4807 }
4808 \f
4809 static int
4810 handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
4811 {
4812 struct ofproto *p = ofconn_get_ofproto(ofconn);
4813 struct flow_mod fm;
4814 int error;
4815
4816 error = reject_slave_controller(ofconn, "flow_mod");
4817 if (error) {
4818 return error;
4819 }
4820
4821 error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_flow_format(ofconn));
4822 if (error) {
4823 return error;
4824 }
4825
4826 /* We do not support the emergency flow cache. It will hopefully get
4827 * dropped from OpenFlow in the near future. */
4828 if (fm.flags & OFPFF_EMERG) {
4829 /* There isn't a good fit for an error code, so just state that the
4830 * flow table is full. */
4831 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
4832 }
4833
4834 error = validate_actions(fm.actions, fm.n_actions,
4835 &fm.cr.flow, p->max_ports);
4836 if (error) {
4837 return error;
4838 }
4839
4840 switch (fm.command) {
4841 case OFPFC_ADD:
4842 return add_flow(ofconn, &fm);
4843
4844 case OFPFC_MODIFY:
4845 return modify_flows_loose(ofconn, &fm);
4846
4847 case OFPFC_MODIFY_STRICT:
4848 return modify_flow_strict(ofconn, &fm);
4849
4850 case OFPFC_DELETE:
4851 delete_flows_loose(p, &fm);
4852 return 0;
4853
4854 case OFPFC_DELETE_STRICT:
4855 delete_flow_strict(p, &fm);
4856 return 0;
4857
4858 default:
4859 return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
4860 }
4861 }
4862
4863 static int
4864 handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
4865 {
4866 const struct nxt_tun_id_cookie *msg
4867 = (const struct nxt_tun_id_cookie *) oh;
4868 enum nx_flow_format flow_format;
4869
4870 flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
4871 ofconn_set_flow_format(ofconn, flow_format);
4872
4873 return 0;
4874 }
4875
4876 static int
4877 handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
4878 {
4879 struct nx_role_request *nrr = (struct nx_role_request *) oh;
4880 struct nx_role_request *reply;
4881 struct ofpbuf *buf;
4882 uint32_t role;
4883
4884 if (ofconn_get_type(ofconn) != OFCONN_PRIMARY) {
4885 VLOG_WARN_RL(&rl, "ignoring role request on service connection");
4886 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
4887 }
4888
4889 role = ntohl(nrr->role);
4890 if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
4891 && role != NX_ROLE_SLAVE) {
4892 VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
4893
4894 /* There's no good error code for this. */
4895 return ofp_mkerr(OFPET_BAD_REQUEST, -1);
4896 }
4897
4898 ofconn_set_role(ofconn, role);
4899
4900 reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
4901 reply->role = htonl(role);
4902 ofconn_send_reply(ofconn, buf);
4903
4904 return 0;
4905 }
4906
4907 static int
4908 handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
4909 {
4910 const struct nxt_set_flow_format *msg
4911 = (const struct nxt_set_flow_format *) oh;
4912 uint32_t format;
4913
4914 format = ntohl(msg->format);
4915 if (format == NXFF_OPENFLOW10
4916 || format == NXFF_TUN_ID_FROM_COOKIE
4917 || format == NXFF_NXM) {
4918 ofconn_set_flow_format(ofconn, format);
4919 return 0;
4920 } else {
4921 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
4922 }
4923 }
4924
4925 static int
4926 handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
4927 {
4928 struct ofp_header *ob;
4929 struct ofpbuf *buf;
4930
4931 /* Currently, everything executes synchronously, so we can just
4932 * immediately send the barrier reply. */
4933 ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
4934 ofconn_send_reply(ofconn, buf);
4935 return 0;
4936 }
4937
4938 static int
4939 handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
4940 {
4941 const struct ofp_header *oh = msg->data;
4942 const struct ofputil_msg_type *type;
4943 int error;
4944
4945 error = ofputil_decode_msg_type(oh, &type);
4946 if (error) {
4947 return error;
4948 }
4949
4950 switch (ofputil_msg_type_code(type)) {
4951 /* OpenFlow requests. */
4952 case OFPUTIL_OFPT_ECHO_REQUEST:
4953 return handle_echo_request(ofconn, oh);
4954
4955 case OFPUTIL_OFPT_FEATURES_REQUEST:
4956 return handle_features_request(ofconn, oh);
4957
4958 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
4959 return handle_get_config_request(ofconn, oh);
4960
4961 case OFPUTIL_OFPT_SET_CONFIG:
4962 return handle_set_config(ofconn, msg->data);
4963
4964 case OFPUTIL_OFPT_PACKET_OUT:
4965 return handle_packet_out(ofconn, oh);
4966
4967 case OFPUTIL_OFPT_PORT_MOD:
4968 return handle_port_mod(ofconn, oh);
4969
4970 case OFPUTIL_OFPT_FLOW_MOD:
4971 return handle_flow_mod(ofconn, oh);
4972
4973 case OFPUTIL_OFPT_BARRIER_REQUEST:
4974 return handle_barrier_request(ofconn, oh);
4975
4976 /* OpenFlow replies. */
4977 case OFPUTIL_OFPT_ECHO_REPLY:
4978 return 0;
4979
4980 /* Nicira extension requests. */
4981 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
4982 return handle_tun_id_from_cookie(ofconn, oh);
4983
4984 case OFPUTIL_NXT_ROLE_REQUEST:
4985 return handle_role_request(ofconn, oh);
4986
4987 case OFPUTIL_NXT_SET_FLOW_FORMAT:
4988 return handle_nxt_set_flow_format(ofconn, oh);
4989
4990 case OFPUTIL_NXT_FLOW_MOD:
4991 return handle_flow_mod(ofconn, oh);
4992
4993 /* OpenFlow statistics requests. */
4994 case OFPUTIL_OFPST_DESC_REQUEST:
4995 return handle_desc_stats_request(ofconn, oh);
4996
4997 case OFPUTIL_OFPST_FLOW_REQUEST:
4998 return handle_flow_stats_request(ofconn, oh);
4999
5000 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
5001 return handle_aggregate_stats_request(ofconn, oh);
5002
5003 case OFPUTIL_OFPST_TABLE_REQUEST:
5004 return handle_table_stats_request(ofconn, oh);
5005
5006 case OFPUTIL_OFPST_PORT_REQUEST:
5007 return handle_port_stats_request(ofconn, oh);
5008
5009 case OFPUTIL_OFPST_QUEUE_REQUEST:
5010 return handle_queue_stats_request(ofconn, oh);
5011
5012 /* Nicira extension statistics requests. */
5013 case OFPUTIL_NXST_FLOW_REQUEST:
5014 return handle_nxst_flow(ofconn, oh);
5015
5016 case OFPUTIL_NXST_AGGREGATE_REQUEST:
5017 return handle_nxst_aggregate(ofconn, oh);
5018
5019 case OFPUTIL_INVALID:
5020 case OFPUTIL_OFPT_HELLO:
5021 case OFPUTIL_OFPT_ERROR:
5022 case OFPUTIL_OFPT_FEATURES_REPLY:
5023 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
5024 case OFPUTIL_OFPT_PACKET_IN:
5025 case OFPUTIL_OFPT_FLOW_REMOVED:
5026 case OFPUTIL_OFPT_PORT_STATUS:
5027 case OFPUTIL_OFPT_BARRIER_REPLY:
5028 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
5029 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
5030 case OFPUTIL_OFPST_DESC_REPLY:
5031 case OFPUTIL_OFPST_FLOW_REPLY:
5032 case OFPUTIL_OFPST_QUEUE_REPLY:
5033 case OFPUTIL_OFPST_PORT_REPLY:
5034 case OFPUTIL_OFPST_TABLE_REPLY:
5035 case OFPUTIL_OFPST_AGGREGATE_REPLY:
5036 case OFPUTIL_NXT_ROLE_REPLY:
5037 case OFPUTIL_NXT_FLOW_REMOVED:
5038 case OFPUTIL_NXST_FLOW_REPLY:
5039 case OFPUTIL_NXST_AGGREGATE_REPLY:
5040 default:
5041 if (VLOG_IS_WARN_ENABLED()) {
5042 char *s = ofp_to_string(oh, ntohs(oh->length), 2);
5043 VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
5044 free(s);
5045 }
5046 if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
5047 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
5048 } else {
5049 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
5050 }
5051 }
5052 }
5053
5054 static void
5055 handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
5056 {
5057 int error = handle_openflow__(ofconn, ofp_msg);
5058 if (error) {
5059 send_error_oh(ofconn, ofp_msg->data, error);
5060 }
5061 COVERAGE_INC(ofproto_recv_openflow);
5062 }
5063 \f
5064 static void
5065 handle_miss_upcall(struct ofproto *p, struct dpif_upcall *upcall)
5066 {
5067 struct facet *facet;
5068 struct flow flow;
5069
5070 /* Obtain in_port and tun_id, at least. */
5071 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
5072
5073 /* Set header pointers in 'flow'. */
5074 flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
5075
5076 /* Handle 802.1ag and LACP. */
5077 if (ofproto_process_special(p, &flow, upcall->packet)) {
5078 ofpbuf_delete(upcall->packet);
5079 return;
5080 }
5081
5082 /* Check with in-band control to see if this packet should be sent
5083 * to the local port regardless of the flow table. */
5084 if (connmgr_msg_in_hook(p->connmgr, &flow, upcall->packet)) {
5085 ofproto_send_packet(p, ODPP_LOCAL, 0, upcall->packet);
5086 }
5087
5088 facet = facet_lookup_valid(p, &flow);
5089 if (!facet) {
5090 struct rule *rule = rule_lookup(p, &flow);
5091 if (!rule) {
5092 /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
5093 struct ofport *port = get_port(p, flow.in_port);
5094 if (port) {
5095 if (port->opp.config & htonl(OFPPC_NO_PACKET_IN)) {
5096 COVERAGE_INC(ofproto_no_packet_in);
5097 /* XXX install 'drop' flow entry */
5098 ofpbuf_delete(upcall->packet);
5099 return;
5100 }
5101 } else {
5102 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
5103 flow.in_port);
5104 }
5105
5106 COVERAGE_INC(ofproto_packet_in);
5107 send_packet_in(p, upcall, &flow, false);
5108 return;
5109 }
5110
5111 facet = facet_create(p, rule, &flow, upcall->packet);
5112 } else if (!facet->may_install) {
5113 /* The facet is not installable, that is, we need to process every
5114 * packet, so process the current packet's actions into 'facet'. */
5115 facet_make_actions(p, facet, upcall->packet);
5116 }
5117
5118 if (facet->rule->cr.priority == FAIL_OPEN_PRIORITY) {
5119 /*
5120 * Extra-special case for fail-open mode.
5121 *
5122 * We are in fail-open mode and the packet matched the fail-open rule,
5123 * but we are connected to a controller too. We should send the packet
5124 * up to the controller in the hope that it will try to set up a flow
5125 * and thereby allow us to exit fail-open.
5126 *
5127 * See the top-level comment in fail-open.c for more information.
5128 */
5129 send_packet_in(p, upcall, &flow, true);
5130 }
5131
5132 facet_execute(p, facet, upcall->packet);
5133 facet_install(p, facet, false);
5134 }
5135
5136 static void
5137 handle_upcall(struct ofproto *p, struct dpif_upcall *upcall)
5138 {
5139 struct flow flow;
5140
5141 switch (upcall->type) {
5142 case DPIF_UC_ACTION:
5143 COVERAGE_INC(ofproto_ctlr_action);
5144 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
5145 send_packet_in(p, upcall, &flow, false);
5146 break;
5147
5148 case DPIF_UC_SAMPLE:
5149 if (p->sflow) {
5150 odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
5151 ofproto_sflow_received(p->sflow, upcall, &flow);
5152 }
5153 ofpbuf_delete(upcall->packet);
5154 break;
5155
5156 case DPIF_UC_MISS:
5157 handle_miss_upcall(p, upcall);
5158 break;
5159
5160 case DPIF_N_UC_TYPES:
5161 default:
5162 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
5163 break;
5164 }
5165 }
5166 \f
5167 /* Flow expiration. */
5168
5169 static int ofproto_dp_max_idle(const struct ofproto *);
5170 static void ofproto_update_stats(struct ofproto *);
5171 static void rule_expire(struct ofproto *, struct rule *);
5172 static void ofproto_expire_facets(struct ofproto *, int dp_max_idle);
5173
5174 /* This function is called periodically by ofproto_run(). Its job is to
5175 * collect updates for the flows that have been installed into the datapath,
5176 * most importantly when they last were used, and then use that information to
5177 * expire flows that have not been used recently.
5178 *
5179 * Returns the number of milliseconds after which it should be called again. */
5180 static int
5181 ofproto_expire(struct ofproto *ofproto)
5182 {
5183 struct rule *rule, *next_rule;
5184 struct cls_cursor cursor;
5185 int dp_max_idle;
5186
5187 /* Update stats for each flow in the datapath. */
5188 ofproto_update_stats(ofproto);
5189
5190 /* Expire facets that have been idle too long. */
5191 dp_max_idle = ofproto_dp_max_idle(ofproto);
5192 ofproto_expire_facets(ofproto, dp_max_idle);
5193
5194 /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
5195 cls_cursor_init(&cursor, &ofproto->cls, NULL);
5196 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
5197 rule_expire(ofproto, rule);
5198 }
5199
5200 /* All outstanding data in existing flows has been accounted, so it's a
5201 * good time to do bond rebalancing. */
5202 if (ofproto->has_bonded_bundles) {
5203 struct ofbundle *bundle;
5204
5205 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
5206 if (bundle->bond) {
5207 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
5208 }
5209 }
5210 }
5211
5212 return MIN(dp_max_idle, 1000);
5213 }
5214
5215 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
5216 *
5217 * This function also pushes statistics updates to rules which each facet
5218 * resubmits into. Generally these statistics will be accurate. However, if a
5219 * facet changes the rule it resubmits into at some time in between
5220 * ofproto_update_stats() runs, it is possible that statistics accrued to the
5221 * old rule will be incorrectly attributed to the new rule. This could be
5222 * avoided by calling ofproto_update_stats() whenever rules are created or
5223 * deleted. However, the performance impact of making so many calls to the
5224 * datapath do not justify the benefit of having perfectly accurate statistics.
5225 */
5226 static void
5227 ofproto_update_stats(struct ofproto *p)
5228 {
5229 const struct dpif_flow_stats *stats;
5230 struct dpif_flow_dump dump;
5231 const struct nlattr *key;
5232 size_t key_len;
5233
5234 dpif_flow_dump_start(&dump, p->dpif);
5235 while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
5236 struct facet *facet;
5237 struct flow flow;
5238
5239 if (odp_flow_key_to_flow(key, key_len, &flow)) {
5240 struct ds s;
5241
5242 ds_init(&s);
5243 odp_flow_key_format(key, key_len, &s);
5244 VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
5245 ds_cstr(&s));
5246 ds_destroy(&s);
5247
5248 continue;
5249 }
5250 facet = facet_find(p, &flow);
5251
5252 if (facet && facet->installed) {
5253
5254 if (stats->n_packets >= facet->dp_packet_count) {
5255 facet->packet_count += stats->n_packets - facet->dp_packet_count;
5256 } else {
5257 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
5258 }
5259
5260 if (stats->n_bytes >= facet->dp_byte_count) {
5261 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
5262 } else {
5263 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
5264 }
5265
5266 facet->dp_packet_count = stats->n_packets;
5267 facet->dp_byte_count = stats->n_bytes;
5268
5269 facet_update_time(p, facet, stats->used);
5270 facet_account(p, facet, stats->n_bytes);
5271 facet_push_stats(p, facet);
5272 } else {
5273 /* There's a flow in the datapath that we know nothing about.
5274 * Delete it. */
5275 COVERAGE_INC(ofproto_unexpected_rule);
5276 dpif_flow_del(p->dpif, key, key_len, NULL);
5277 }
5278 }
5279 dpif_flow_dump_done(&dump);
5280 }
5281
5282 /* Calculates and returns the number of milliseconds of idle time after which
5283 * facets should expire from the datapath and we should fold their statistics
5284 * into their parent rules in userspace. */
5285 static int
5286 ofproto_dp_max_idle(const struct ofproto *ofproto)
5287 {
5288 /*
5289 * Idle time histogram.
5290 *
5291 * Most of the time a switch has a relatively small number of facets. When
5292 * this is the case we might as well keep statistics for all of them in
5293 * userspace and to cache them in the kernel datapath for performance as
5294 * well.
5295 *
5296 * As the number of facets increases, the memory required to maintain
5297 * statistics about them in userspace and in the kernel becomes
5298 * significant. However, with a large number of facets it is likely that
5299 * only a few of them are "heavy hitters" that consume a large amount of
5300 * bandwidth. At this point, only heavy hitters are worth caching in the
5301 * kernel and maintaining in userspaces; other facets we can discard.
5302 *
5303 * The technique used to compute the idle time is to build a histogram with
5304 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet
5305 * that is installed in the kernel gets dropped in the appropriate bucket.
5306 * After the histogram has been built, we compute the cutoff so that only
5307 * the most-recently-used 1% of facets (but at least 1000 flows) are kept
5308 * cached. At least the most-recently-used bucket of facets is kept, so
5309 * actually an arbitrary number of facets can be kept in any given
5310 * expiration run (though the next run will delete most of those unless
5311 * they receive additional data).
5312 *
5313 * This requires a second pass through the facets, in addition to the pass
5314 * made by ofproto_update_stats(), because the former function never looks
5315 * at uninstallable facets.
5316 */
5317 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
5318 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
5319 int buckets[N_BUCKETS] = { 0 };
5320 struct facet *facet;
5321 int total, bucket;
5322 long long int now;
5323 int i;
5324
5325 total = hmap_count(&ofproto->facets);
5326 if (total <= 1000) {
5327 return N_BUCKETS * BUCKET_WIDTH;
5328 }
5329
5330 /* Build histogram. */
5331 now = time_msec();
5332 HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
5333 long long int idle = now - facet->used;
5334 int bucket = (idle <= 0 ? 0
5335 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
5336 : (unsigned int) idle / BUCKET_WIDTH);
5337 buckets[bucket]++;
5338 }
5339
5340 /* Find the first bucket whose flows should be expired. */
5341 for (bucket = 0; bucket < N_BUCKETS; bucket++) {
5342 if (buckets[bucket]) {
5343 int subtotal = 0;
5344 do {
5345 subtotal += buckets[bucket++];
5346 } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
5347 break;
5348 }
5349 }
5350
5351 if (VLOG_IS_DBG_ENABLED()) {
5352 struct ds s;
5353
5354 ds_init(&s);
5355 ds_put_cstr(&s, "keep");
5356 for (i = 0; i < N_BUCKETS; i++) {
5357 if (i == bucket) {
5358 ds_put_cstr(&s, ", drop");
5359 }
5360 if (buckets[i]) {
5361 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
5362 }
5363 }
5364 VLOG_INFO("%s: %s (msec:count)", ofproto->name, ds_cstr(&s));
5365 ds_destroy(&s);
5366 }
5367
5368 return bucket * BUCKET_WIDTH;
5369 }
5370
5371 static void
5372 facet_active_timeout(struct ofproto *ofproto, struct facet *facet)
5373 {
5374 if (ofproto->netflow && !facet_is_controller_flow(facet) &&
5375 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5376 struct ofexpired expired;
5377
5378 if (facet->installed) {
5379 struct dpif_flow_stats stats;
5380
5381 facet_put__(ofproto, facet, facet->actions, facet->actions_len,
5382 &stats);
5383 facet_update_stats(ofproto, facet, &stats);
5384 }
5385
5386 expired.flow = facet->flow;
5387 expired.packet_count = facet->packet_count;
5388 expired.byte_count = facet->byte_count;
5389 expired.used = facet->used;
5390 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5391 }
5392 }
5393
5394 static void
5395 ofproto_expire_facets(struct ofproto *ofproto, int dp_max_idle)
5396 {
5397 long long int cutoff = time_msec() - dp_max_idle;
5398 struct facet *facet, *next_facet;
5399
5400 HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
5401 facet_active_timeout(ofproto, facet);
5402 if (facet->used < cutoff) {
5403 facet_remove(ofproto, facet);
5404 }
5405 }
5406 }
5407
5408 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
5409 * then delete it entirely. */
5410 static void
5411 rule_expire(struct ofproto *ofproto, struct rule *rule)
5412 {
5413 struct facet *facet, *next_facet;
5414 long long int now;
5415 uint8_t reason;
5416
5417 /* Has 'rule' expired? */
5418 now = time_msec();
5419 if (rule->hard_timeout
5420 && now > rule->created + rule->hard_timeout * 1000) {
5421 reason = OFPRR_HARD_TIMEOUT;
5422 } else if (rule->idle_timeout && list_is_empty(&rule->facets)
5423 && now >rule->used + rule->idle_timeout * 1000) {
5424 reason = OFPRR_IDLE_TIMEOUT;
5425 } else {
5426 return;
5427 }
5428
5429 COVERAGE_INC(ofproto_expired);
5430
5431 /* Update stats. (This is a no-op if the rule expired due to an idle
5432 * timeout, because that only happens when the rule has no facets left.) */
5433 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
5434 facet_remove(ofproto, facet);
5435 }
5436
5437 /* Get rid of the rule. */
5438 if (!rule_is_hidden(rule)) {
5439 rule_send_removed(ofproto, rule, reason);
5440 }
5441 rule_remove(ofproto, rule);
5442 }
5443 \f
5444 static void
5445 rule_send_removed(struct ofproto *p, struct rule *rule, uint8_t reason)
5446 {
5447 struct ofputil_flow_removed fr;
5448
5449 if (!rule->send_flow_removed) {
5450 return;
5451 }
5452
5453 fr.rule = rule->cr;
5454 fr.cookie = rule->flow_cookie;
5455 fr.reason = reason;
5456 calc_flow_duration__(rule->created, &fr.duration_sec, &fr.duration_nsec);
5457 fr.idle_timeout = rule->idle_timeout;
5458 fr.packet_count = rule->packet_count;
5459 fr.byte_count = rule->byte_count;
5460
5461 connmgr_send_flow_removed(p->connmgr, &fr);
5462 }
5463
5464 /* Obtains statistics for 'rule' and stores them in '*packets' and '*bytes'.
5465 * The returned statistics include statistics for all of 'rule''s facets. */
5466 static void
5467 rule_get_stats(const struct rule *rule, uint64_t *packets, uint64_t *bytes)
5468 {
5469 uint64_t p, b;
5470 struct facet *facet;
5471
5472 /* Start from historical data for 'rule' itself that are no longer tracked
5473 * in facets. This counts, for example, facets that have expired. */
5474 p = rule->packet_count;
5475 b = rule->byte_count;
5476
5477 /* Add any statistics that are tracked by facets. This includes
5478 * statistical data recently updated by ofproto_update_stats() as well as
5479 * stats for packets that were executed "by hand" via dpif_execute(). */
5480 LIST_FOR_EACH (facet, list_node, &rule->facets) {
5481 p += facet->packet_count;
5482 b += facet->byte_count;
5483 }
5484
5485 *packets = p;
5486 *bytes = b;
5487 }
5488
5489 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
5490 * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
5491 * their individual configurations.
5492 *
5493 * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
5494 * Otherwise, ownership is transferred to this function. */
5495 static void
5496 send_packet_in(struct ofproto *ofproto, struct dpif_upcall *upcall,
5497 const struct flow *flow, bool clone)
5498 {
5499 struct ofputil_packet_in pin;
5500
5501 pin.packet = upcall->packet;
5502 pin.in_port = odp_port_to_ofp_port(flow->in_port);
5503 pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
5504 pin.buffer_id = 0; /* not yet known */
5505 pin.send_len = upcall->userdata;
5506 connmgr_send_packet_in(ofproto->connmgr, upcall, flow,
5507 clone ? NULL : upcall->packet);
5508 }
5509
5510 static uint64_t
5511 pick_datapath_id(const struct ofproto *ofproto)
5512 {
5513 const struct ofport *port;
5514
5515 port = get_port(ofproto, ODPP_LOCAL);
5516 if (port) {
5517 uint8_t ea[ETH_ADDR_LEN];
5518 int error;
5519
5520 error = netdev_get_etheraddr(port->netdev, ea);
5521 if (!error) {
5522 return eth_addr_to_uint64(ea);
5523 }
5524 VLOG_WARN("could not get MAC address for %s (%s)",
5525 netdev_get_name(port->netdev), strerror(error));
5526 }
5527 return ofproto->fallback_dpid;
5528 }
5529
5530 static uint64_t
5531 pick_fallback_dpid(void)
5532 {
5533 uint8_t ea[ETH_ADDR_LEN];
5534 eth_addr_nicira_random(ea);
5535 return eth_addr_to_uint64(ea);
5536 }
5537 \f
5538 static struct ofproto *
5539 ofproto_lookup(const char *name)
5540 {
5541 struct ofproto *ofproto;
5542
5543 HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0),
5544 &all_ofprotos) {
5545 if (!strcmp(ofproto->name, name)) {
5546 return ofproto;
5547 }
5548 }
5549 return NULL;
5550 }
5551
5552 static void
5553 ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
5554 void *aux OVS_UNUSED)
5555 {
5556 struct ofproto *ofproto;
5557 struct ds results;
5558
5559 ds_init(&results);
5560 HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) {
5561 ds_put_format(&results, "%s\n", ofproto->name);
5562 }
5563 unixctl_command_reply(conn, 200, ds_cstr(&results));
5564 ds_destroy(&results);
5565 }
5566
5567 struct ofproto_trace {
5568 struct action_xlate_ctx ctx;
5569 struct flow flow;
5570 struct ds *result;
5571 };
5572
5573 static void
5574 trace_format_rule(struct ds *result, int level, const struct rule *rule)
5575 {
5576 ds_put_char_multiple(result, '\t', level);
5577 if (!rule) {
5578 ds_put_cstr(result, "No match\n");
5579 return;
5580 }
5581
5582 ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
5583 ntohll(rule->flow_cookie));
5584 cls_rule_format(&rule->cr, result);
5585 ds_put_char(result, '\n');
5586
5587 ds_put_char_multiple(result, '\t', level);
5588 ds_put_cstr(result, "OpenFlow ");
5589 ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
5590 rule->n_actions * sizeof *rule->actions);
5591 ds_put_char(result, '\n');
5592 }
5593
5594 static void
5595 trace_format_flow(struct ds *result, int level, const char *title,
5596 struct ofproto_trace *trace)
5597 {
5598 ds_put_char_multiple(result, '\t', level);
5599 ds_put_format(result, "%s: ", title);
5600 if (flow_equal(&trace->ctx.flow, &trace->flow)) {
5601 ds_put_cstr(result, "unchanged");
5602 } else {
5603 flow_format(result, &trace->ctx.flow);
5604 trace->flow = trace->ctx.flow;
5605 }
5606 ds_put_char(result, '\n');
5607 }
5608
5609 static void
5610 trace_resubmit(struct action_xlate_ctx *ctx, struct rule *rule)
5611 {
5612 struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
5613 struct ds *result = trace->result;
5614
5615 ds_put_char(result, '\n');
5616 trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
5617 trace_format_rule(result, ctx->recurse + 1, rule);
5618 }
5619
5620 static void
5621 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
5622 void *aux OVS_UNUSED)
5623 {
5624 char *dpname, *in_port_s, *tun_id_s, *packet_s;
5625 char *args = xstrdup(args_);
5626 char *save_ptr = NULL;
5627 struct ofproto *ofproto;
5628 struct ofpbuf packet;
5629 struct rule *rule;
5630 struct ds result;
5631 struct flow flow;
5632 uint16_t in_port;
5633 ovs_be64 tun_id;
5634 char *s;
5635
5636 ofpbuf_init(&packet, strlen(args) / 2);
5637 ds_init(&result);
5638
5639 dpname = strtok_r(args, " ", &save_ptr);
5640 tun_id_s = strtok_r(NULL, " ", &save_ptr);
5641 in_port_s = strtok_r(NULL, " ", &save_ptr);
5642 packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
5643 if (!dpname || !in_port_s || !packet_s) {
5644 unixctl_command_reply(conn, 501, "Bad command syntax");
5645 goto exit;
5646 }
5647
5648 ofproto = ofproto_lookup(dpname);
5649 if (!ofproto) {
5650 unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
5651 "for help)");
5652 goto exit;
5653 }
5654
5655 tun_id = htonll(strtoull(tun_id_s, NULL, 0));
5656 in_port = ofp_port_to_odp_port(atoi(in_port_s));
5657
5658 packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
5659 packet_s += strspn(packet_s, " ");
5660 if (*packet_s != '\0') {
5661 unixctl_command_reply(conn, 501, "Trailing garbage in command");
5662 goto exit;
5663 }
5664 if (packet.size < ETH_HEADER_LEN) {
5665 unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
5666 goto exit;
5667 }
5668
5669 ds_put_cstr(&result, "Packet: ");
5670 s = ofp_packet_to_string(packet.data, packet.size, packet.size);
5671 ds_put_cstr(&result, s);
5672 free(s);
5673
5674 flow_extract(&packet, tun_id, in_port, &flow);
5675 ds_put_cstr(&result, "Flow: ");
5676 flow_format(&result, &flow);
5677 ds_put_char(&result, '\n');
5678
5679 rule = rule_lookup(ofproto, &flow);
5680 trace_format_rule(&result, 0, rule);
5681 if (rule) {
5682 struct ofproto_trace trace;
5683 struct ofpbuf *odp_actions;
5684
5685 trace.result = &result;
5686 trace.flow = flow;
5687 action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
5688 trace.ctx.resubmit_hook = trace_resubmit;
5689 odp_actions = xlate_actions(&trace.ctx,
5690 rule->actions, rule->n_actions);
5691
5692 ds_put_char(&result, '\n');
5693 trace_format_flow(&result, 0, "Final flow", &trace);
5694 ds_put_cstr(&result, "Datapath actions: ");
5695 format_odp_actions(&result, odp_actions->data, odp_actions->size);
5696 ofpbuf_delete(odp_actions);
5697 }
5698
5699 unixctl_command_reply(conn, 200, ds_cstr(&result));
5700
5701 exit:
5702 ds_destroy(&result);
5703 ofpbuf_uninit(&packet);
5704 free(args);
5705 }
5706
5707 static void
5708 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
5709 const char *args, void *aux OVS_UNUSED)
5710 {
5711 struct ds ds = DS_EMPTY_INITIALIZER;
5712 const struct ofproto *ofproto;
5713 const struct mac_entry *e;
5714
5715 ofproto = ofproto_lookup(args);
5716 if (!ofproto) {
5717 unixctl_command_reply(conn, 501, "no such bridge");
5718 return;
5719 }
5720
5721 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5722 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5723 struct ofbundle *bundle = e->port.p;
5724 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
5725 ofbundle_get_a_port(bundle)->odp_port,
5726 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
5727 }
5728 unixctl_command_reply(conn, 200, ds_cstr(&ds));
5729 ds_destroy(&ds);
5730 }
5731
5732 static void
5733 ofproto_unixctl_init(void)
5734 {
5735 static bool registered;
5736 if (registered) {
5737 return;
5738 }
5739 registered = true;
5740
5741 unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
5742 unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
5743 unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
5744 }