ofproto: Start work to enable datapaths with built-in wildcard support.
[sliver-openvswitch.git] / ofproto / ofproto.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofproto.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include "classifier.h"
26 #include "coverage.h"
27 #include "discovery.h"
28 #include "dynamic-string.h"
29 #include "fail-open.h"
30 #include "in-band.h"
31 #include "mac-learning.h"
32 #include "netdev.h"
33 #include "netflow.h"
34 #include "ofp-print.h"
35 #include "ofproto-sflow.h"
36 #include "ofpbuf.h"
37 #include "openflow/nicira-ext.h"
38 #include "openflow/openflow.h"
39 #include "openvswitch/xflow.h"
40 #include "packets.h"
41 #include "pinsched.h"
42 #include "pktbuf.h"
43 #include "poll-loop.h"
44 #include "port-array.h"
45 #include "rconn.h"
46 #include "shash.h"
47 #include "status.h"
48 #include "stp.h"
49 #include "stream-ssl.h"
50 #include "svec.h"
51 #include "tag.h"
52 #include "timeval.h"
53 #include "unixctl.h"
54 #include "vconn.h"
55 #include "wdp.h"
56 #include "xfif.h"
57 #include "xtoxll.h"
58
59 #define THIS_MODULE VLM_ofproto
60 #include "vlog.h"
61
62 #include "sflow_api.h"
63
64 enum {
65     TABLEID_HASH = 0,
66     TABLEID_CLASSIFIER = 1
67 };
68
69 struct ofproto_rule {
70     uint64_t flow_cookie;       /* Controller-issued identifier. 
71                                    (Kept in network-byte order.) */
72     bool send_flow_removed;     /* Send a flow removed message? */
73     tag_type tags;              /* Tags (set only by hooks). */
74 };
75
76 static struct ofproto_rule *
77 ofproto_rule_cast(const struct wdp_rule *wdp_rule)
78 {
79     return wdp_rule->client_data;
80 }
81
82 static void
83 ofproto_rule_init(struct wdp_rule *wdp_rule)
84 {
85     wdp_rule->client_data = xzalloc(sizeof(struct ofproto_rule));
86 }
87
88
89 static inline bool
90 rule_is_hidden(const struct wdp_rule *rule)
91 {
92     /* Rules with priority higher than UINT16_MAX are set up by ofproto itself
93      * (e.g. by in-band control) and are intentionally hidden from the
94      * controller. */
95     if (rule->cr.flow.priority > UINT16_MAX) {
96         return true;
97     }
98
99     return false;
100 }
101
102 static void delete_flow(struct ofproto *, struct wdp_rule *, uint8_t reason);
103
104 struct ofconn {
105     struct list node;
106     struct rconn *rconn;
107     struct pktbuf *pktbuf;
108     int miss_send_len;
109
110     struct rconn_packet_counter *packet_in_counter;
111
112     /* Number of OpenFlow messages queued as replies to OpenFlow requests, and
113      * the maximum number before we stop reading OpenFlow requests.  */
114 #define OFCONN_REPLY_MAX 100
115     struct rconn_packet_counter *reply_counter;
116 };
117
118 static struct ofconn *ofconn_create(struct ofproto *, struct rconn *);
119 static void ofconn_destroy(struct ofconn *);
120 static void ofconn_run(struct ofconn *, struct ofproto *);
121 static void ofconn_wait(struct ofconn *);
122 static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
123                      struct rconn_packet_counter *counter);
124
125 struct ofproto {
126     /* Settings. */
127     uint64_t datapath_id;       /* Datapath ID. */
128     uint64_t fallback_dpid;     /* Datapath ID if no better choice found. */
129     char *mfr_desc;             /* Manufacturer. */
130     char *hw_desc;              /* Hardware. */
131     char *sw_desc;              /* Software version. */
132     char *serial_desc;          /* Serial number. */
133     char *dp_desc;              /* Datapath description. */
134
135     /* Datapath. */
136     struct wdp *wdp;
137     uint32_t max_ports;
138
139     /* Configuration. */
140     struct switch_status *switch_status;
141     struct status_category *ss_cat;
142     struct in_band *in_band;
143     struct discovery *discovery;
144     struct fail_open *fail_open;
145     struct pinsched *miss_sched, *action_sched;
146     struct netflow *netflow;
147     struct ofproto_sflow *sflow;
148
149     /* OpenFlow connections. */
150     struct list all_conns;
151     struct ofconn *controller;
152     struct pvconn **listeners;
153     size_t n_listeners;
154     struct pvconn **snoops;
155     size_t n_snoops;
156
157     /* Hooks for ovs-vswitchd. */
158     const struct ofhooks *ofhooks;
159     void *aux;
160
161     /* Used by default ofhooks. */
162     struct mac_learning *ml;
163 };
164
165 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
166
167 static const struct ofhooks default_ofhooks;
168
169 static uint64_t pick_datapath_id(const struct ofproto *);
170 static uint64_t pick_fallback_dpid(void);
171 static void send_packet_in_miss(struct wdp_packet *, void *ofproto);
172 static void send_packet_in_action(struct wdp_packet *, void *ofproto);
173
174 static void handle_wdp_packet(struct ofproto *, struct wdp_packet *);
175
176 static void handle_openflow(struct ofconn *, struct ofproto *,
177                             struct ofpbuf *);
178
179 int
180 ofproto_create(const char *datapath, const char *datapath_type,
181                const struct ofhooks *ofhooks, void *aux,
182                struct ofproto **ofprotop)
183 {
184     struct wdp_stats stats;
185     struct ofproto *p;
186     struct wdp *wdp;
187     int error;
188
189     *ofprotop = NULL;
190
191     /* Connect to datapath and start listening for messages. */
192     error = wdp_open(datapath, datapath_type, &wdp);
193     if (error) {
194         VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
195         return error;
196     }
197     error = wdp_get_wdp_stats(wdp, &stats);
198     if (error) {
199         VLOG_ERR("failed to obtain stats for datapath %s: %s",
200                  datapath, strerror(error));
201         wdp_close(wdp);
202         return error;
203     }
204     error = wdp_recv_set_mask(wdp, ((1 << WDP_CHAN_MISS)
205                                     | (1 << WDP_CHAN_ACTION)
206                                     | (1 << WDP_CHAN_SFLOW)));
207     if (error) {
208         VLOG_ERR("failed to listen on datapath %s: %s",
209                  datapath, strerror(error));
210         wdp_close(wdp);
211         return error;
212     }
213     wdp_flow_flush(wdp);
214     wdp_recv_purge(wdp);
215
216     /* Initialize settings. */
217     p = xzalloc(sizeof *p);
218     p->fallback_dpid = pick_fallback_dpid();
219     p->datapath_id = p->fallback_dpid;
220     p->mfr_desc = xstrdup(DEFAULT_MFR_DESC);
221     p->hw_desc = xstrdup(DEFAULT_HW_DESC);
222     p->sw_desc = xstrdup(DEFAULT_SW_DESC);
223     p->serial_desc = xstrdup(DEFAULT_SERIAL_DESC);
224     p->dp_desc = xstrdup(DEFAULT_DP_DESC);
225
226     /* Initialize datapath. */
227     p->wdp = wdp;
228     p->max_ports = stats.max_ports;
229
230     /* Initialize submodules. */
231     p->switch_status = switch_status_create(p);
232     p->in_band = NULL;
233     p->discovery = NULL;
234     p->fail_open = NULL;
235     p->miss_sched = p->action_sched = NULL;
236     p->netflow = NULL;
237     p->sflow = NULL;
238
239     /* Initialize OpenFlow connections. */
240     list_init(&p->all_conns);
241     p->controller = ofconn_create(p, rconn_create(5, 8));
242     p->controller->pktbuf = pktbuf_create();
243     p->controller->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
244     p->listeners = NULL;
245     p->n_listeners = 0;
246     p->snoops = NULL;
247     p->n_snoops = 0;
248
249     /* Initialize hooks. */
250     if (ofhooks) {
251         p->ofhooks = ofhooks;
252         p->aux = aux;
253         p->ml = NULL;
254     } else {
255         p->ofhooks = &default_ofhooks;
256         p->aux = p;
257         p->ml = mac_learning_create();
258     }
259
260     /* Register switch status category. */
261     p->ss_cat = switch_status_register(p->switch_status, "remote",
262                                        rconn_status_cb, p->controller->rconn);
263
264     /* Pick final datapath ID. */
265     p->datapath_id = pick_datapath_id(p);
266     VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
267
268     *ofprotop = p;
269     return 0;
270 }
271
272 void
273 ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id)
274 {
275     uint64_t old_dpid = p->datapath_id;
276     p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p);
277     if (p->datapath_id != old_dpid) {
278         VLOG_INFO("datapath ID changed to %016"PRIx64, p->datapath_id);
279         rconn_reconnect(p->controller->rconn);
280     }
281 }
282
283 void
284 ofproto_set_probe_interval(struct ofproto *p, int probe_interval)
285 {
286     probe_interval = probe_interval ? MAX(probe_interval, 5) : 0;
287     rconn_set_probe_interval(p->controller->rconn, probe_interval);
288     if (p->fail_open) {
289         int trigger_duration = probe_interval ? probe_interval * 3 : 15;
290         fail_open_set_trigger_duration(p->fail_open, trigger_duration);
291     }
292 }
293
294 void
295 ofproto_set_max_backoff(struct ofproto *p, int max_backoff)
296 {
297     rconn_set_max_backoff(p->controller->rconn, max_backoff);
298 }
299
300 void
301 ofproto_set_desc(struct ofproto *p,
302                  const char *mfr_desc, const char *hw_desc,
303                  const char *sw_desc, const char *serial_desc,
304                  const char *dp_desc)
305 {
306     struct ofp_desc_stats *ods;
307
308     if (mfr_desc) {
309         if (strlen(mfr_desc) >= sizeof ods->mfr_desc) {
310             VLOG_WARN("truncating mfr_desc, must be less than %zu characters",
311                     sizeof ods->mfr_desc);
312         }
313         free(p->mfr_desc);
314         p->mfr_desc = xstrdup(mfr_desc);
315     }
316     if (hw_desc) {
317         if (strlen(hw_desc) >= sizeof ods->hw_desc) {
318             VLOG_WARN("truncating hw_desc, must be less than %zu characters",
319                     sizeof ods->hw_desc);
320         }
321         free(p->hw_desc);
322         p->hw_desc = xstrdup(hw_desc);
323     }
324     if (sw_desc) {
325         if (strlen(sw_desc) >= sizeof ods->sw_desc) {
326             VLOG_WARN("truncating sw_desc, must be less than %zu characters",
327                     sizeof ods->sw_desc);
328         }
329         free(p->sw_desc);
330         p->sw_desc = xstrdup(sw_desc);
331     }
332     if (serial_desc) {
333         if (strlen(serial_desc) >= sizeof ods->serial_num) {
334             VLOG_WARN("truncating serial_desc, must be less than %zu "
335                     "characters",
336                     sizeof ods->serial_num);
337         }
338         free(p->serial_desc);
339         p->serial_desc = xstrdup(serial_desc);
340     }
341     if (dp_desc) {
342         if (strlen(dp_desc) >= sizeof ods->dp_desc) {
343             VLOG_WARN("truncating dp_desc, must be less than %zu characters",
344                     sizeof ods->dp_desc);
345         }
346         free(p->dp_desc);
347         p->dp_desc = xstrdup(dp_desc);
348     }
349 }
350
351 int
352 ofproto_set_in_band(struct ofproto *p, bool in_band)
353 {
354     if (in_band != (p->in_band != NULL)) {
355         if (in_band) {
356             return in_band_create(p, p->wdp, p->switch_status,
357                                   p->controller->rconn, &p->in_band);
358         } else {
359             ofproto_set_discovery(p, false, NULL, true);
360             in_band_destroy(p->in_band);
361             p->in_band = NULL;
362         }
363         rconn_reconnect(p->controller->rconn);
364     }
365     return 0;
366 }
367
368 int
369 ofproto_set_discovery(struct ofproto *p, bool discovery,
370                       const char *re, bool update_resolv_conf)
371 {
372     if (discovery != (p->discovery != NULL)) {
373         if (discovery) {
374             int error = ofproto_set_in_band(p, true);
375             if (error) {
376                 return error;
377             }
378             error = discovery_create(re, update_resolv_conf,
379                                      p->wdp, p->switch_status,
380                                      &p->discovery);
381             if (error) {
382                 return error;
383             }
384         } else {
385             discovery_destroy(p->discovery);
386             p->discovery = NULL;
387         }
388         rconn_disconnect(p->controller->rconn);
389     } else if (discovery) {
390         discovery_set_update_resolv_conf(p->discovery, update_resolv_conf);
391         return discovery_set_accept_controller_re(p->discovery, re);
392     }
393     return 0;
394 }
395
396 int
397 ofproto_set_controller(struct ofproto *ofproto, const char *controller)
398 {
399     if (ofproto->discovery) {
400         return EINVAL;
401     } else if (controller) {
402         if (strcmp(rconn_get_name(ofproto->controller->rconn), controller)) {
403             return rconn_connect(ofproto->controller->rconn, controller);
404         } else {
405             return 0;
406         }
407     } else {
408         rconn_disconnect(ofproto->controller->rconn);
409         return 0;
410     }
411 }
412
413 static int
414 set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp,
415             const struct svec *svec)
416 {
417     struct pvconn **pvconns = *pvconnsp;
418     size_t n_pvconns = *n_pvconnsp;
419     int retval = 0;
420     size_t i;
421
422     for (i = 0; i < n_pvconns; i++) {
423         pvconn_close(pvconns[i]);
424     }
425     free(pvconns);
426
427     pvconns = xmalloc(svec->n * sizeof *pvconns);
428     n_pvconns = 0;
429     for (i = 0; i < svec->n; i++) {
430         const char *name = svec->names[i];
431         struct pvconn *pvconn;
432         int error;
433
434         error = pvconn_open(name, &pvconn);
435         if (!error) {
436             pvconns[n_pvconns++] = pvconn;
437         } else {
438             VLOG_ERR("failed to listen on %s: %s", name, strerror(error));
439             if (!retval) {
440                 retval = error;
441             }
442         }
443     }
444
445     *pvconnsp = pvconns;
446     *n_pvconnsp = n_pvconns;
447
448     return retval;
449 }
450
451 int
452 ofproto_set_listeners(struct ofproto *ofproto, const struct svec *listeners)
453 {
454     return set_pvconns(&ofproto->listeners, &ofproto->n_listeners, listeners);
455 }
456
457 int
458 ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops)
459 {
460     return set_pvconns(&ofproto->snoops, &ofproto->n_snoops, snoops);
461 }
462
463 int
464 ofproto_set_netflow(struct ofproto *ofproto,
465                     const struct netflow_options *nf_options)
466 {
467     if (nf_options && nf_options->collectors.n) {
468         if (!ofproto->netflow) {
469             ofproto->netflow = netflow_create();
470         }
471         return netflow_set_options(ofproto->netflow, nf_options);
472     } else {
473         netflow_destroy(ofproto->netflow);
474         ofproto->netflow = NULL;
475         return 0;
476     }
477 }
478
479 void
480 ofproto_set_sflow(struct ofproto *ofproto,
481                   const struct ofproto_sflow_options *oso)
482 {
483     struct ofproto_sflow *os = ofproto->sflow;
484     if (oso) {
485         if (!os) {
486             os = ofproto->sflow = ofproto_sflow_create(ofproto->wdp);
487             /* XXX ofport */
488         }
489         ofproto_sflow_set_options(os, oso);
490     } else {
491         ofproto_sflow_destroy(os);
492         ofproto->sflow = NULL;
493     }
494 }
495
496 void
497 ofproto_set_failure(struct ofproto *ofproto, bool fail_open)
498 {
499     if (fail_open) {
500         struct rconn *rconn = ofproto->controller->rconn;
501         int trigger_duration = rconn_get_probe_interval(rconn) * 3;
502         if (!ofproto->fail_open) {
503             ofproto->fail_open = fail_open_create(ofproto, trigger_duration,
504                                                   ofproto->switch_status,
505                                                   rconn);
506         } else {
507             fail_open_set_trigger_duration(ofproto->fail_open,
508                                            trigger_duration);
509         }
510     } else {
511         fail_open_destroy(ofproto->fail_open);
512         ofproto->fail_open = NULL;
513     }
514 }
515
516 void
517 ofproto_set_rate_limit(struct ofproto *ofproto,
518                        int rate_limit, int burst_limit)
519 {
520     if (rate_limit > 0) {
521         if (!ofproto->miss_sched) {
522             ofproto->miss_sched = pinsched_create(rate_limit, burst_limit,
523                                                   ofproto->switch_status);
524             ofproto->action_sched = pinsched_create(rate_limit, burst_limit,
525                                                     NULL);
526         } else {
527             pinsched_set_limits(ofproto->miss_sched, rate_limit, burst_limit);
528             pinsched_set_limits(ofproto->action_sched,
529                                 rate_limit, burst_limit);
530         }
531     } else {
532         pinsched_destroy(ofproto->miss_sched);
533         ofproto->miss_sched = NULL;
534         pinsched_destroy(ofproto->action_sched);
535         ofproto->action_sched = NULL;
536     }
537 }
538
539 int
540 ofproto_set_stp(struct ofproto *ofproto OVS_UNUSED, bool enable_stp)
541 {
542     /* XXX */
543     if (enable_stp) {
544         VLOG_WARN("STP is not yet implemented");
545         return EINVAL;
546     } else {
547         return 0;
548     }
549 }
550
551 uint64_t
552 ofproto_get_datapath_id(const struct ofproto *ofproto)
553 {
554     return ofproto->datapath_id;
555 }
556
557 int
558 ofproto_get_probe_interval(const struct ofproto *ofproto)
559 {
560     return rconn_get_probe_interval(ofproto->controller->rconn);
561 }
562
563 int
564 ofproto_get_max_backoff(const struct ofproto *ofproto)
565 {
566     return rconn_get_max_backoff(ofproto->controller->rconn);
567 }
568
569 bool
570 ofproto_get_in_band(const struct ofproto *ofproto)
571 {
572     return ofproto->in_band != NULL;
573 }
574
575 bool
576 ofproto_get_discovery(const struct ofproto *ofproto)
577 {
578     return ofproto->discovery != NULL;
579 }
580
581 const char *
582 ofproto_get_controller(const struct ofproto *ofproto)
583 {
584     return rconn_get_name(ofproto->controller->rconn);
585 }
586
587 void
588 ofproto_get_listeners(const struct ofproto *ofproto, struct svec *listeners)
589 {
590     size_t i;
591
592     for (i = 0; i < ofproto->n_listeners; i++) {
593         svec_add(listeners, pvconn_get_name(ofproto->listeners[i]));
594     }
595 }
596
597 void
598 ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops)
599 {
600     size_t i;
601
602     for (i = 0; i < ofproto->n_snoops; i++) {
603         svec_add(snoops, pvconn_get_name(ofproto->snoops[i]));
604     }
605 }
606
607 void
608 ofproto_destroy(struct ofproto *p)
609 {
610     struct ofconn *ofconn, *next_ofconn;
611     size_t i;
612
613     if (!p) {
614         return;
615     }
616
617     /* Destroy fail-open early, because it touches the classifier. */
618     ofproto_set_failure(p, false);
619
620     ofproto_flush_flows(p);
621
622     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
623                         &p->all_conns) {
624         ofconn_destroy(ofconn);
625     }
626
627     wdp_close(p->wdp);
628
629     switch_status_destroy(p->switch_status);
630     in_band_destroy(p->in_band);
631     discovery_destroy(p->discovery);
632     pinsched_destroy(p->miss_sched);
633     pinsched_destroy(p->action_sched);
634     netflow_destroy(p->netflow);
635     ofproto_sflow_destroy(p->sflow);
636
637     switch_status_unregister(p->ss_cat);
638
639     for (i = 0; i < p->n_listeners; i++) {
640         pvconn_close(p->listeners[i]);
641     }
642     free(p->listeners);
643
644     for (i = 0; i < p->n_snoops; i++) {
645         pvconn_close(p->snoops[i]);
646     }
647     free(p->snoops);
648
649     mac_learning_destroy(p->ml);
650
651     free(p->mfr_desc);
652     free(p->hw_desc);
653     free(p->sw_desc);
654     free(p->serial_desc);
655     free(p->dp_desc);
656
657     free(p);
658 }
659
660 int
661 ofproto_run(struct ofproto *p)
662 {
663     int error = ofproto_run1(p);
664     if (!error) {
665         error = ofproto_run2(p, false);
666     }
667     return error;
668 }
669
670 int
671 ofproto_run1(struct ofproto *p)
672 {
673     struct ofconn *ofconn, *next_ofconn;
674     int i;
675
676     for (i = 0; i < 50; i++) {
677         struct wdp_packet packet;
678         int error;
679
680         error = wdp_recv(p->wdp, &packet);
681         if (error) {
682             if (error == ENODEV) {
683                 /* Someone destroyed the datapath behind our back.  The caller
684                  * better destroy us and give up, because we're just going to
685                  * spin from here on out. */
686                 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
687                 VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
688                             wdp_name(p->wdp));
689                 return ENODEV;
690             }
691             break;
692         }
693
694         handle_wdp_packet(p, xmemdup(&packet, sizeof packet));
695     }
696
697     if (p->in_band) {
698         in_band_run(p->in_band);
699     }
700     if (p->discovery) {
701         char *controller_name;
702         if (rconn_is_connectivity_questionable(p->controller->rconn)) {
703             discovery_question_connectivity(p->discovery);
704         }
705         if (discovery_run(p->discovery, &controller_name)) {
706             if (controller_name) {
707                 rconn_connect(p->controller->rconn, controller_name);
708             } else {
709                 rconn_disconnect(p->controller->rconn);
710             }
711         }
712     }
713     pinsched_run(p->miss_sched, send_packet_in_miss, p);
714     pinsched_run(p->action_sched, send_packet_in_action, p);
715
716     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
717                         &p->all_conns) {
718         ofconn_run(ofconn, p);
719     }
720
721     /* Fail-open maintenance.  Do this after processing the ofconns since
722      * fail-open checks the status of the controller rconn. */
723     if (p->fail_open) {
724         fail_open_run(p->fail_open);
725     }
726
727     for (i = 0; i < p->n_listeners; i++) {
728         struct vconn *vconn;
729         int retval;
730
731         retval = pvconn_accept(p->listeners[i], OFP_VERSION, &vconn);
732         if (!retval) {
733             ofconn_create(p, rconn_new_from_vconn("passive", vconn));
734         } else if (retval != EAGAIN) {
735             VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
736         }
737     }
738
739     for (i = 0; i < p->n_snoops; i++) {
740         struct vconn *vconn;
741         int retval;
742
743         retval = pvconn_accept(p->snoops[i], OFP_VERSION, &vconn);
744         if (!retval) {
745             rconn_add_monitor(p->controller->rconn, vconn);
746         } else if (retval != EAGAIN) {
747             VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
748         }
749     }
750
751     if (p->netflow) {
752         netflow_run(p->netflow);
753     }
754     if (p->sflow) {
755         ofproto_sflow_run(p->sflow);
756     }
757
758     return 0;
759 }
760
761 struct revalidate_cbdata {
762     struct ofproto *ofproto;
763     bool revalidate_all;        /* Revalidate all exact-match rules? */
764     bool revalidate_subrules;   /* Revalidate all exact-match subrules? */
765     struct tag_set revalidate_set; /* Set of tags to revalidate. */
766 };
767
768 int
769 ofproto_run2(struct ofproto *p OVS_UNUSED, bool revalidate_all OVS_UNUSED)
770 {
771     return 0;
772 }
773
774 void
775 ofproto_wait(struct ofproto *p)
776 {
777     struct ofconn *ofconn;
778     size_t i;
779
780     wdp_recv_wait(p->wdp);
781     wdp_port_poll_wait(p->wdp);
782     LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
783         ofconn_wait(ofconn);
784     }
785     if (p->in_band) {
786         in_band_wait(p->in_band);
787     }
788     if (p->discovery) {
789         discovery_wait(p->discovery);
790     }
791     if (p->fail_open) {
792         fail_open_wait(p->fail_open);
793     }
794     pinsched_wait(p->miss_sched);
795     pinsched_wait(p->action_sched);
796     if (p->sflow) {
797         ofproto_sflow_wait(p->sflow);
798     }
799     for (i = 0; i < p->n_listeners; i++) {
800         pvconn_wait(p->listeners[i]);
801     }
802     for (i = 0; i < p->n_snoops; i++) {
803         pvconn_wait(p->snoops[i]);
804     }
805 }
806
807 void
808 ofproto_revalidate(struct ofproto *ofproto OVS_UNUSED, tag_type tag OVS_UNUSED)
809 {
810     //XXX tag_set_add(&ofproto->revalidate_set, tag);
811 }
812
813 bool
814 ofproto_is_alive(const struct ofproto *p)
815 {
816     return p->discovery || rconn_is_alive(p->controller->rconn);
817 }
818
819 int
820 ofproto_send_packet(struct ofproto *p, const flow_t *flow,
821                     const union ofp_action *actions, size_t n_actions,
822                     const struct ofpbuf *packet)
823 {
824     /* XXX Should we translate the wdp_execute() errno value into an OpenFlow
825      * error code? */
826     wdp_execute(p->wdp, flow->in_port, actions, n_actions, packet);
827     return 0;
828 }
829
830 void
831 ofproto_add_flow(struct ofproto *p, const flow_t *flow,
832                  const union ofp_action *actions, size_t n_actions,
833                  int idle_timeout)
834 {
835     struct wdp_flow_put put;
836     struct wdp_rule *rule;
837
838     put.flags = WDP_PUT_CREATE | WDP_PUT_MODIFY | WDP_PUT_ALL;
839     put.flow = flow;
840     put.actions = actions;
841     put.n_actions = n_actions;
842     put.idle_timeout = idle_timeout;
843     put.hard_timeout = 0;
844
845     if (!wdp_flow_put(p->wdp, &put, NULL, &rule)) {
846         ofproto_rule_init(rule);
847     }
848 }
849
850 void
851 ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow)
852 {
853     struct wdp_rule *rule = wdp_flow_get(ofproto->wdp, flow);
854     if (rule) {
855         delete_flow(ofproto, rule, OFPRR_DELETE);
856     }
857 }
858
859 void
860 ofproto_flush_flows(struct ofproto *ofproto)
861 {
862     COVERAGE_INC(ofproto_flush);
863     wdp_flow_flush(ofproto->wdp);
864     if (ofproto->in_band) {
865         in_band_flushed(ofproto->in_band);
866     }
867     if (ofproto->fail_open) {
868         fail_open_flushed(ofproto->fail_open);
869     }
870 }
871 \f
872 static struct ofconn *
873 ofconn_create(struct ofproto *p, struct rconn *rconn)
874 {
875     struct ofconn *ofconn = xmalloc(sizeof *ofconn);
876     list_push_back(&p->all_conns, &ofconn->node);
877     ofconn->rconn = rconn;
878     ofconn->pktbuf = NULL;
879     ofconn->miss_send_len = 0;
880     ofconn->packet_in_counter = rconn_packet_counter_create ();
881     ofconn->reply_counter = rconn_packet_counter_create ();
882     return ofconn;
883 }
884
885 static void
886 ofconn_destroy(struct ofconn *ofconn)
887 {
888     list_remove(&ofconn->node);
889     rconn_destroy(ofconn->rconn);
890     rconn_packet_counter_destroy(ofconn->packet_in_counter);
891     rconn_packet_counter_destroy(ofconn->reply_counter);
892     pktbuf_destroy(ofconn->pktbuf);
893     free(ofconn);
894 }
895
896 static void
897 ofconn_run(struct ofconn *ofconn, struct ofproto *p)
898 {
899     int iteration;
900
901     rconn_run(ofconn->rconn);
902
903     if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
904         /* Limit the number of iterations to prevent other tasks from
905          * starving. */
906         for (iteration = 0; iteration < 50; iteration++) {
907             struct ofpbuf *of_msg = rconn_recv(ofconn->rconn);
908             if (!of_msg) {
909                 break;
910             }
911             if (p->fail_open) {
912                 fail_open_maybe_recover(p->fail_open);
913             }
914             handle_openflow(ofconn, p, of_msg);
915             ofpbuf_delete(of_msg);
916         }
917     }
918
919     if (ofconn != p->controller && !rconn_is_alive(ofconn->rconn)) {
920         ofconn_destroy(ofconn);
921     }
922 }
923
924 static void
925 ofconn_wait(struct ofconn *ofconn)
926 {
927     rconn_run_wait(ofconn->rconn);
928     if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) {
929         rconn_recv_wait(ofconn->rconn);
930     } else {
931         COVERAGE_INC(ofproto_ofconn_stuck);
932     }
933 }
934 \f
935 static bool
936 rule_has_out_port(const struct wdp_rule *rule, uint16_t out_port)
937 {
938     const union ofp_action *oa;
939     struct actions_iterator i;
940
941     if (out_port == htons(OFPP_NONE)) {
942         return true;
943     }
944     for (oa = actions_first(&i, rule->actions, rule->n_actions); oa;
945          oa = actions_next(&i)) {
946         if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
947             return true;
948         }
949     }
950     return false;
951 }
952 \f
953 static void
954 queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
955          struct rconn_packet_counter *counter)
956 {
957     update_openflow_length(msg);
958     if (rconn_send(ofconn->rconn, msg, counter)) {
959         ofpbuf_delete(msg);
960     }
961 }
962
963 static void
964 send_error(const struct ofconn *ofconn, const struct ofp_header *oh,
965            int error, const void *data, size_t len)
966 {
967     struct ofpbuf *buf;
968     struct ofp_error_msg *oem;
969
970     if (!(error >> 16)) {
971         VLOG_WARN_RL(&rl, "not sending bad error code %d to controller",
972                      error);
973         return;
974     }
975
976     COVERAGE_INC(ofproto_error);
977     oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR,
978                             oh ? oh->xid : 0, &buf);
979     oem->type = htons((unsigned int) error >> 16);
980     oem->code = htons(error & 0xffff);
981     memcpy(oem->data, data, len);
982     queue_tx(buf, ofconn, ofconn->reply_counter);
983 }
984
985 static void
986 send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
987               int error)
988 {
989     size_t oh_length = ntohs(oh->length);
990     send_error(ofconn, oh, error, oh, MIN(oh_length, 64));
991 }
992
993 static int
994 handle_echo_request(struct ofconn *ofconn, struct ofp_header *oh)
995 {
996     struct ofp_header *rq = oh;
997     queue_tx(make_echo_reply(rq), ofconn, ofconn->reply_counter);
998     return 0;
999 }
1000
1001 static int
1002 handle_features_request(struct ofproto *p, struct ofconn *ofconn,
1003                         struct ofp_header *oh)
1004 {
1005     struct ofpbuf *features;
1006     int error;
1007
1008     error = wdp_get_features(p->wdp, &features);
1009     if (!error) {
1010         struct ofp_switch_features *osf = features->data;
1011
1012         osf->header.xid = oh->xid;
1013         osf->datapath_id = htonll(p->datapath_id);
1014         osf->n_buffers = htonl(pktbuf_capacity());
1015         osf->capabilities |= htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
1016                                    OFPC_PORT_STATS);
1017
1018         queue_tx(features, ofconn, ofconn->reply_counter);
1019     }
1020     return error;
1021 }
1022
1023 static int
1024 handle_get_config_request(struct ofproto *p, struct ofconn *ofconn,
1025                           struct ofp_header *oh)
1026 {
1027     struct ofpbuf *buf;
1028     struct ofp_switch_config *osc;
1029     uint16_t flags;
1030     bool drop_frags;
1031
1032     /* Figure out flags. */
1033     wdp_get_drop_frags(p->wdp, &drop_frags);
1034     flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
1035
1036     /* Send reply. */
1037     osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf);
1038     osc->flags = htons(flags);
1039     osc->miss_send_len = htons(ofconn->miss_send_len);
1040     queue_tx(buf, ofconn, ofconn->reply_counter);
1041
1042     return 0;
1043 }
1044
1045 static int
1046 handle_set_config(struct ofproto *p, struct ofconn *ofconn,
1047                   struct ofp_switch_config *osc)
1048 {
1049     uint16_t flags;
1050     int error;
1051
1052     error = check_ofp_message(&osc->header, OFPT_SET_CONFIG, sizeof *osc);
1053     if (error) {
1054         return error;
1055     }
1056     flags = ntohs(osc->flags);
1057
1058     if (ofconn == p->controller) {
1059         switch (flags & OFPC_FRAG_MASK) {
1060         case OFPC_FRAG_NORMAL:
1061             wdp_set_drop_frags(p->wdp, false);
1062             break;
1063         case OFPC_FRAG_DROP:
1064             wdp_set_drop_frags(p->wdp, true);
1065             break;
1066         default:
1067             VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
1068                          osc->flags);
1069             break;
1070         }
1071     }
1072
1073     if ((ntohs(osc->miss_send_len) != 0) != (ofconn->miss_send_len != 0)) {
1074         if (ntohs(osc->miss_send_len) != 0) {
1075             ofconn->pktbuf = pktbuf_create();
1076         } else {
1077             pktbuf_destroy(ofconn->pktbuf);
1078         }
1079     }
1080
1081     ofconn->miss_send_len = ntohs(osc->miss_send_len);
1082
1083     return 0;
1084 }
1085
1086 static int
1087 handle_packet_out(struct ofproto *p, struct ofconn *ofconn,
1088                   struct ofp_header *oh)
1089 {
1090     struct ofp_packet_out *opo;
1091     struct ofpbuf payload, *buffer;
1092     struct ofp_action_header *actions;
1093     int n_actions;
1094     uint16_t in_port;
1095     flow_t flow;
1096     int error;
1097
1098     error = check_ofp_packet_out(oh, &payload, &n_actions, p->max_ports);
1099     if (error) {
1100         return error;
1101     }
1102     opo = (struct ofp_packet_out *) oh;
1103     actions = opo->actions;
1104
1105     COVERAGE_INC(ofproto_packet_out);
1106     if (opo->buffer_id != htonl(UINT32_MAX)) {
1107         error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id),
1108                                 &buffer, &in_port);
1109         if (error || !buffer) {
1110             return error;
1111         }
1112         payload = *buffer;
1113     } else {
1114         buffer = NULL;
1115     }
1116
1117     flow_extract(&payload, ntohs(opo->in_port), &flow);
1118     wdp_execute(p->wdp, flow.in_port, (const union ofp_action *) actions,
1119                 n_actions, &payload);
1120     ofpbuf_delete(buffer);
1121
1122     return 0;
1123 }
1124
1125 static int
1126 handle_port_mod(struct ofproto *p, struct ofp_header *oh)
1127 {
1128     const struct ofp_port_mod *opm;
1129     struct wdp_port *port;
1130     int error;
1131
1132     error = check_ofp_message(oh, OFPT_PORT_MOD, sizeof *opm);
1133     if (error) {
1134         return error;
1135     }
1136     opm = (struct ofp_port_mod *) oh;
1137
1138     wdp_port_query_by_number(p->wdp, ntohs(opm->port_no), &port);
1139     if (!port) {
1140         return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
1141     } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
1142         return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
1143     } else {
1144         uint32_t mask, new_config;
1145
1146         mask = ntohl(opm->mask) & (OFPPC_PORT_DOWN | OFPPC_NO_STP
1147                                    | OFPPC_NO_RECV | OFPPC_NO_RECV_STP
1148                                    | OFPPC_NO_FLOOD | OFPPC_NO_FWD
1149                                    | OFPPC_NO_PACKET_IN);
1150         new_config = (port->opp.config & ~mask) | (ntohl(opm->config) & mask);
1151         if (new_config != port->opp.config) {
1152             wdp_port_set_config(p->wdp, ntohs(opm->port_no), new_config);
1153         }
1154         if (opm->advertise) {
1155             netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
1156         }
1157     }
1158
1159     return 0;
1160 }
1161
1162 static struct ofpbuf *
1163 make_stats_reply(uint32_t xid, uint16_t type, size_t body_len)
1164 {
1165     struct ofp_stats_reply *osr;
1166     struct ofpbuf *msg;
1167
1168     msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
1169     osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
1170     osr->type = type;
1171     osr->flags = htons(0);
1172     return msg;
1173 }
1174
1175 static struct ofpbuf *
1176 start_stats_reply(const struct ofp_stats_request *request, size_t body_len)
1177 {
1178     return make_stats_reply(request->header.xid, request->type, body_len);
1179 }
1180
1181 static void *
1182 append_stats_reply(size_t nbytes, struct ofconn *ofconn, struct ofpbuf **msgp)
1183 {
1184     struct ofpbuf *msg = *msgp;
1185     assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
1186     if (nbytes + msg->size > UINT16_MAX) {
1187         struct ofp_stats_reply *reply = msg->data;
1188         reply->flags = htons(OFPSF_REPLY_MORE);
1189         *msgp = make_stats_reply(reply->header.xid, reply->type, nbytes);
1190         queue_tx(msg, ofconn, ofconn->reply_counter);
1191     }
1192     return ofpbuf_put_uninit(*msgp, nbytes);
1193 }
1194
1195 static int
1196 handle_desc_stats_request(struct ofproto *p, struct ofconn *ofconn,
1197                            struct ofp_stats_request *request)
1198 {
1199     struct ofp_desc_stats *ods;
1200     struct ofpbuf *msg;
1201
1202     msg = start_stats_reply(request, sizeof *ods);
1203     ods = append_stats_reply(sizeof *ods, ofconn, &msg);
1204     memset(ods, 0, sizeof *ods);
1205     ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
1206     ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
1207     ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
1208     ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
1209     ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
1210     queue_tx(msg, ofconn, ofconn->reply_counter);
1211
1212     return 0;
1213 }
1214
1215 static int
1216 handle_table_stats_request(struct ofproto *p, struct ofconn *ofconn,
1217                            struct ofp_stats_request *request)
1218 {
1219     struct ofp_table_stats *ots;
1220     struct ofpbuf *msg;
1221     struct wdp_stats dpstats;
1222
1223     msg = start_stats_reply(request, sizeof *ots * 2);
1224
1225     wdp_get_wdp_stats(p->wdp, &dpstats);
1226
1227     /* Hash table. */
1228     ots = append_stats_reply(sizeof *ots, ofconn, &msg);
1229     memset(ots, 0, sizeof *ots);
1230     ots->table_id = TABLEID_HASH;
1231     strcpy(ots->name, "hash");
1232     ots->wildcards = htonl(0);
1233     ots->max_entries = htonl(dpstats.exact.max_capacity);
1234     ots->active_count = htonl(dpstats.exact.n_flows);
1235     ots->lookup_count = htonll(dpstats.exact.n_hit + dpstats.exact.n_missed);
1236     ots->matched_count = htonll(dpstats.exact.n_hit);
1237
1238     /* Classifier table. */
1239     ots = append_stats_reply(sizeof *ots, ofconn, &msg);
1240     memset(ots, 0, sizeof *ots);
1241     ots->table_id = TABLEID_CLASSIFIER;
1242     strcpy(ots->name, "classifier");
1243     ots->wildcards = htonl(OFPFW_ALL);
1244     ots->max_entries = htonl(dpstats.wild.max_capacity);
1245     ots->active_count = htonl(dpstats.wild.n_flows);
1246     ots->lookup_count = htonll(dpstats.wild.n_hit + dpstats.wild.n_missed);
1247     ots->matched_count = htonll(dpstats.wild.n_hit);
1248
1249     queue_tx(msg, ofconn, ofconn->reply_counter);
1250     return 0;
1251 }
1252
1253 static void
1254 append_port_stat(struct wdp_port *port, struct ofconn *ofconn,
1255                  struct ofpbuf *msg)
1256 {
1257     struct netdev_stats stats;
1258     struct ofp_port_stats *ops;
1259
1260     /* Intentionally ignore return value, since errors will set 
1261      * 'stats' to all-1s, which is correct for OpenFlow, and 
1262      * netdev_get_stats() will log errors. */
1263     netdev_get_stats(port->netdev, &stats);
1264
1265     ops = append_stats_reply(sizeof *ops, ofconn, &msg);
1266     ops->port_no = htons(port->opp.port_no);
1267     memset(ops->pad, 0, sizeof ops->pad);
1268     ops->rx_packets = htonll(stats.rx_packets);
1269     ops->tx_packets = htonll(stats.tx_packets);
1270     ops->rx_bytes = htonll(stats.rx_bytes);
1271     ops->tx_bytes = htonll(stats.tx_bytes);
1272     ops->rx_dropped = htonll(stats.rx_dropped);
1273     ops->tx_dropped = htonll(stats.tx_dropped);
1274     ops->rx_errors = htonll(stats.rx_errors);
1275     ops->tx_errors = htonll(stats.tx_errors);
1276     ops->rx_frame_err = htonll(stats.rx_frame_errors);
1277     ops->rx_over_err = htonll(stats.rx_over_errors);
1278     ops->rx_crc_err = htonll(stats.rx_crc_errors);
1279     ops->collisions = htonll(stats.collisions);
1280 }
1281
1282 static int
1283 handle_port_stats_request(struct ofproto *p, struct ofconn *ofconn,
1284                           struct ofp_stats_request *osr,
1285                           size_t arg_size)
1286 {
1287     struct ofp_port_stats_request *psr;
1288     struct ofp_port_stats *ops;
1289     struct ofpbuf *msg;
1290
1291     if (arg_size != sizeof *psr) {
1292         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1293     }
1294     psr = (struct ofp_port_stats_request *) osr->body;
1295
1296     msg = start_stats_reply(osr, sizeof *ops * 16);
1297     if (psr->port_no != htons(OFPP_NONE)) {
1298         struct wdp_port *port;
1299
1300         wdp_port_query_by_number(p->wdp, ntohs(psr->port_no), &port);
1301         if (port) {
1302             append_port_stat(port, ofconn, msg);
1303         }
1304     } else {
1305         struct wdp_port **ports;
1306         size_t n_ports;
1307         size_t i;
1308
1309         wdp_port_list(p->wdp, &ports, &n_ports);
1310         for (i = 0; i < n_ports; i++) {
1311             append_port_stat(ports[i], ofconn, msg);
1312         }
1313         free(ports);
1314     }
1315
1316     queue_tx(msg, ofconn, ofconn->reply_counter);
1317     return 0;
1318 }
1319
1320 struct flow_stats_cbdata {
1321     struct ofproto *ofproto;
1322     struct ofconn *ofconn;
1323     uint16_t out_port;
1324     struct ofpbuf *msg;
1325 };
1326
1327 /* Obtains statistic counters for 'rule' within 'p' and stores them into
1328  * '*packet_countp' and '*byte_countp'.  If 'rule' is a wildcarded rule, the
1329  * returned statistic include statistics for all of 'rule''s subrules. */
1330 static void
1331 query_stats(struct ofproto *p, struct wdp_rule *rule,
1332             uint64_t *packet_countp, uint64_t *byte_countp)
1333 {
1334     struct wdp_flow_stats stats;
1335
1336     if (!wdp_flow_get_stats(p->wdp, rule, &stats)) {
1337         *packet_countp = stats.n_packets;
1338         *byte_countp = stats.n_bytes;
1339     } else {
1340         *packet_countp = 0;
1341         *byte_countp = 0;
1342     }
1343 }
1344
1345 static void
1346 flow_stats_cb(struct wdp_rule *rule, void *cbdata_)
1347 {
1348     struct flow_stats_cbdata *cbdata = cbdata_;
1349     struct ofp_flow_stats *ofs;
1350     uint64_t packet_count, byte_count;
1351     size_t act_len, len;
1352     long long int tdiff = time_msec() - rule->created;
1353     uint32_t sec = tdiff / 1000;
1354     uint32_t msec = tdiff - (sec * 1000);
1355
1356     if (rule_is_hidden(rule)
1357         || !rule_has_out_port(rule, cbdata->out_port)) {
1358         return;
1359     }
1360
1361     act_len = sizeof *rule->actions * rule->n_actions;
1362     len = offsetof(struct ofp_flow_stats, actions) + act_len;
1363
1364     query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
1365
1366     ofs = append_stats_reply(len, cbdata->ofconn, &cbdata->msg);
1367     ofs->length = htons(len);
1368     ofs->table_id = rule->cr.flow.wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
1369     ofs->pad = 0;
1370     flow_to_match(&rule->cr.flow, &ofs->match);
1371     ofs->duration_sec = htonl(sec);
1372     ofs->duration_nsec = htonl(msec * 1000000);
1373     ofs->cookie = ofproto_rule_cast(rule)->flow_cookie;
1374     ofs->priority = htons(rule->cr.flow.priority);
1375     ofs->idle_timeout = htons(rule->idle_timeout);
1376     ofs->hard_timeout = htons(rule->hard_timeout);
1377     memset(ofs->pad2, 0, sizeof ofs->pad2);
1378     ofs->packet_count = htonll(packet_count);
1379     ofs->byte_count = htonll(byte_count);
1380     memcpy(ofs->actions, rule->actions, act_len);
1381 }
1382
1383 static int
1384 table_id_to_include(uint8_t table_id)
1385 {
1386     return (table_id == TABLEID_HASH ? CLS_INC_EXACT
1387             : table_id == TABLEID_CLASSIFIER ? CLS_INC_WILD
1388             : table_id == 0xff ? CLS_INC_ALL
1389             : 0);
1390 }
1391
1392 static int
1393 handle_flow_stats_request(struct ofproto *p, struct ofconn *ofconn,
1394                           const struct ofp_stats_request *osr,
1395                           size_t arg_size)
1396 {
1397     struct ofp_flow_stats_request *fsr;
1398     struct flow_stats_cbdata cbdata;
1399     flow_t target;
1400
1401     if (arg_size != sizeof *fsr) {
1402         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1403     }
1404     fsr = (struct ofp_flow_stats_request *) osr->body;
1405
1406     COVERAGE_INC(ofproto_flows_req);
1407     cbdata.ofproto = p;
1408     cbdata.ofconn = ofconn;
1409     cbdata.out_port = fsr->out_port;
1410     cbdata.msg = start_stats_reply(osr, 1024);
1411     flow_from_match(&target, 0, &fsr->match);
1412     wdp_flow_for_each_match(p->wdp, &target,
1413                             table_id_to_include(fsr->table_id),
1414                             flow_stats_cb, &cbdata);
1415     queue_tx(cbdata.msg, ofconn, ofconn->reply_counter);
1416     return 0;
1417 }
1418
1419 struct flow_stats_ds_cbdata {
1420     struct ofproto *ofproto;
1421     struct ds *results;
1422 };
1423
1424 static void
1425 flow_stats_ds_cb(struct wdp_rule *rule, void *cbdata_)
1426 {
1427     struct flow_stats_ds_cbdata *cbdata = cbdata_;
1428     struct ds *results = cbdata->results;
1429     struct ofp_match match;
1430     uint64_t packet_count, byte_count;
1431     size_t act_len = sizeof *rule->actions * rule->n_actions;
1432
1433     query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
1434     flow_to_match(&rule->cr.flow, &match);
1435
1436     ds_put_format(results, "duration=%llds, ",
1437                   (time_msec() - rule->created) / 1000);
1438     ds_put_format(results, "priority=%u, ", rule->cr.flow.priority);
1439     ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
1440     ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
1441     ofp_print_match(results, &match, true);
1442     ofp_print_actions(results, &rule->actions->header, act_len);
1443     ds_put_cstr(results, "\n");
1444 }
1445
1446 /* Adds a pretty-printed description of all flows to 'results', including 
1447  * those marked hidden by secchan (e.g., by in-band control). */
1448 void
1449 ofproto_get_all_flows(struct ofproto *p, struct ds *results)
1450 {
1451     struct flow_stats_ds_cbdata cbdata;
1452     struct ofp_match match;
1453     flow_t target;
1454
1455     memset(&match, 0, sizeof match);
1456     match.wildcards = htonl(OFPFW_ALL);
1457
1458     cbdata.ofproto = p;
1459     cbdata.results = results;
1460
1461     flow_from_match(&target, 0, &match);
1462     wdp_flow_for_each_match(p->wdp, &target, CLS_INC_ALL,
1463                             flow_stats_ds_cb, &cbdata);
1464 }
1465
1466 struct aggregate_stats_cbdata {
1467     struct ofproto *ofproto;
1468     uint16_t out_port;
1469     uint64_t packet_count;
1470     uint64_t byte_count;
1471     uint32_t n_flows;
1472 };
1473
1474 static void
1475 aggregate_stats_cb(struct wdp_rule *rule, void *cbdata_)
1476 {
1477     struct aggregate_stats_cbdata *cbdata = cbdata_;
1478     uint64_t packet_count, byte_count;
1479
1480     if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
1481         return;
1482     }
1483
1484     query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
1485
1486     cbdata->packet_count += packet_count;
1487     cbdata->byte_count += byte_count;
1488     cbdata->n_flows++;
1489 }
1490
1491 static int
1492 handle_aggregate_stats_request(struct ofproto *p, struct ofconn *ofconn,
1493                                const struct ofp_stats_request *osr,
1494                                size_t arg_size)
1495 {
1496     struct ofp_aggregate_stats_request *asr;
1497     struct ofp_aggregate_stats_reply *reply;
1498     struct aggregate_stats_cbdata cbdata;
1499     struct ofpbuf *msg;
1500     flow_t target;
1501
1502     if (arg_size != sizeof *asr) {
1503         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1504     }
1505     asr = (struct ofp_aggregate_stats_request *) osr->body;
1506
1507     COVERAGE_INC(ofproto_agg_request);
1508     cbdata.ofproto = p;
1509     cbdata.out_port = asr->out_port;
1510     cbdata.packet_count = 0;
1511     cbdata.byte_count = 0;
1512     cbdata.n_flows = 0;
1513     flow_from_match(&target, 0, &asr->match);
1514     wdp_flow_for_each_match(p->wdp, &target,
1515                             table_id_to_include(asr->table_id),
1516                             aggregate_stats_cb, &cbdata);
1517
1518     msg = start_stats_reply(osr, sizeof *reply);
1519     reply = append_stats_reply(sizeof *reply, ofconn, &msg);
1520     reply->flow_count = htonl(cbdata.n_flows);
1521     reply->packet_count = htonll(cbdata.packet_count);
1522     reply->byte_count = htonll(cbdata.byte_count);
1523     queue_tx(msg, ofconn, ofconn->reply_counter);
1524     return 0;
1525 }
1526
1527 static int
1528 handle_stats_request(struct ofproto *p, struct ofconn *ofconn,
1529                      struct ofp_header *oh)
1530 {
1531     struct ofp_stats_request *osr;
1532     size_t arg_size;
1533     int error;
1534
1535     error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr,
1536                                     1, &arg_size);
1537     if (error) {
1538         return error;
1539     }
1540     osr = (struct ofp_stats_request *) oh;
1541
1542     switch (ntohs(osr->type)) {
1543     case OFPST_DESC:
1544         return handle_desc_stats_request(p, ofconn, osr);
1545
1546     case OFPST_FLOW:
1547         return handle_flow_stats_request(p, ofconn, osr, arg_size);
1548
1549     case OFPST_AGGREGATE:
1550         return handle_aggregate_stats_request(p, ofconn, osr, arg_size);
1551
1552     case OFPST_TABLE:
1553         return handle_table_stats_request(p, ofconn, osr);
1554
1555     case OFPST_PORT:
1556         return handle_port_stats_request(p, ofconn, osr, arg_size);
1557
1558     case OFPST_VENDOR:
1559         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
1560
1561     default:
1562         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
1563     }
1564 }
1565
1566 static int
1567 add_flow(struct ofproto *p, struct ofconn *ofconn,
1568          struct ofp_flow_mod *ofm, size_t n_actions)
1569 {
1570     struct wdp_rule *rule;
1571     struct wdp_flow_put put;
1572     struct ofpbuf *packet;
1573     uint16_t in_port;
1574     flow_t flow;
1575     int error;
1576
1577     flow_from_match(&flow, ntohs(ofm->priority), &ofm->match);
1578     if (ofm->flags & htons(OFPFF_CHECK_OVERLAP)
1579         && wdp_flow_overlaps(p->wdp, &flow)) {
1580         return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
1581     }
1582
1583     put.flags = WDP_PUT_CREATE | WDP_PUT_MODIFY | WDP_PUT_ALL;
1584     put.flow = &flow;
1585     put.actions = (const union ofp_action *) ofm->actions;
1586     put.n_actions = n_actions;
1587     put.idle_timeout = ntohs(ofm->idle_timeout);
1588     put.hard_timeout = ntohs(ofm->hard_timeout);
1589     error = wdp_flow_put(p->wdp, &put, NULL, &rule);
1590     if (error) {
1591         /* XXX wdp_flow_put should return OpenFlow error code. */
1592         return error;
1593     }
1594     ofproto_rule_init(rule);
1595
1596     if (ofm->buffer_id != htonl(UINT32_MAX)) {
1597         error = pktbuf_retrieve(ofconn->pktbuf, ntohl(ofm->buffer_id),
1598                                 &packet, &in_port);
1599         if (!error) {
1600             wdp_flow_inject(p->wdp, rule, in_port, packet);
1601             ofpbuf_delete(packet);
1602         }
1603     }
1604
1605     return 0;
1606 }
1607
1608 static int
1609 modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm,
1610             size_t n_actions, uint16_t command, struct wdp_rule *rule)
1611 {
1612     if (rule_is_hidden(rule)) {
1613         return 0;
1614     }
1615
1616     if (command == OFPFC_DELETE) {
1617         delete_flow(p, rule, OFPPR_DELETE);
1618     } else {
1619         const struct ofp_action_header *actions = ofm->actions;
1620         struct wdp_flow_put put;
1621
1622         ofproto_rule_cast(rule)->flow_cookie = ofm->cookie;
1623
1624         put.flags = WDP_PUT_MODIFY | WDP_PUT_ACTIONS;
1625         put.flow = &rule->cr.flow;
1626         put.actions = (const union ofp_action *) actions;
1627         put.n_actions = n_actions;
1628         put.idle_timeout = put.hard_timeout = 0;
1629         wdp_flow_put(p->wdp, &put, NULL, NULL);
1630     }
1631
1632     return 0;
1633 }
1634
1635 static int
1636 modify_flows_strict(struct ofproto *p, const struct ofp_flow_mod *ofm,
1637                     size_t n_actions, uint16_t command)
1638 {
1639     struct wdp_rule *rule;
1640     flow_t flow;
1641
1642     flow_from_match(&flow, ntohs(ofm->priority), &ofm->match);
1643     rule = wdp_flow_get(p->wdp, &flow);
1644
1645     if (rule) {
1646         if (command == OFPFC_DELETE
1647             && ofm->out_port != htons(OFPP_NONE)
1648             && !rule_has_out_port(rule, ofm->out_port)) {
1649             return 0;
1650         }
1651
1652         modify_flow(p, ofm, n_actions, command, rule);
1653     }
1654     return 0;
1655 }
1656
1657 struct modify_flows_cbdata {
1658     struct ofproto *ofproto;
1659     const struct ofp_flow_mod *ofm;
1660     uint16_t out_port;
1661     size_t n_actions;
1662     uint16_t command;
1663 };
1664
1665 static void
1666 modify_flows_cb(struct wdp_rule *rule, void *cbdata_)
1667 {
1668     struct modify_flows_cbdata *cbdata = cbdata_;
1669
1670     if (cbdata->out_port != htons(OFPP_NONE)
1671         && !rule_has_out_port(rule, cbdata->out_port)) {
1672         return;
1673     }
1674
1675     modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions,
1676                 cbdata->command, rule);
1677 }
1678
1679 static int
1680 modify_flows_loose(struct ofproto *p, const struct ofp_flow_mod *ofm,
1681                    size_t n_actions, uint16_t command)
1682 {
1683     struct modify_flows_cbdata cbdata;
1684     flow_t target;
1685
1686     cbdata.ofproto = p;
1687     cbdata.ofm = ofm;
1688     cbdata.out_port = (command == OFPFC_DELETE ? ofm->out_port
1689                        : htons(OFPP_NONE));
1690     cbdata.n_actions = n_actions;
1691     cbdata.command = command;
1692
1693     flow_from_match(&target, 0, &ofm->match);
1694     wdp_flow_for_each_match(p->wdp, &target, CLS_INC_ALL,
1695                             modify_flows_cb, &cbdata);
1696     return 0;
1697 }
1698
1699 static int
1700 handle_flow_mod(struct ofproto *p, struct ofconn *ofconn,
1701                 struct ofp_flow_mod *ofm)
1702 {
1703     size_t n_actions;
1704     int error;
1705
1706     error = check_ofp_message_array(&ofm->header, OFPT_FLOW_MOD, sizeof *ofm,
1707                                     sizeof *ofm->actions, &n_actions);
1708     if (error) {
1709         return error;
1710     }
1711
1712     /* We do not support the emergency flow cache.  It will hopefully
1713      * get dropped from OpenFlow in the near future. */
1714     if (ofm->flags & htons(OFPFF_EMERG)) {
1715         /* There isn't a good fit for an error code, so just state that the
1716          * flow table is full. */
1717         return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
1718     }
1719
1720     normalize_match(&ofm->match);
1721     if (!ofm->match.wildcards) {
1722         ofm->priority = htons(UINT16_MAX);
1723     }
1724
1725     error = validate_actions((const union ofp_action *) ofm->actions,
1726                              n_actions, p->max_ports);
1727     if (error) {
1728         return error;
1729     }
1730
1731     switch (ntohs(ofm->command)) {
1732     case OFPFC_ADD:
1733         return add_flow(p, ofconn, ofm, n_actions);
1734
1735     case OFPFC_MODIFY:
1736         return modify_flows_loose(p, ofm, n_actions, OFPFC_MODIFY);
1737
1738     case OFPFC_MODIFY_STRICT:
1739         return modify_flows_strict(p, ofm, n_actions, OFPFC_MODIFY);
1740
1741     case OFPFC_DELETE:
1742         return modify_flows_loose(p, ofm, n_actions, OFPFC_DELETE);
1743
1744     case OFPFC_DELETE_STRICT:
1745         return modify_flows_strict(p, ofm, n_actions, OFPFC_DELETE);
1746
1747     default:
1748         return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
1749     }
1750 }
1751
1752 static int
1753 handle_vendor(struct ofproto *p, struct ofconn *ofconn, void *msg)
1754 {
1755     struct ofp_vendor_header *ovh = msg;
1756     struct nicira_header *nh;
1757
1758     if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) {
1759         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1760     }
1761     if (ovh->vendor != htonl(NX_VENDOR_ID)) {
1762         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
1763     }
1764     if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
1765         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1766     }
1767
1768     nh = msg;
1769     switch (ntohl(nh->subtype)) {
1770     case NXT_STATUS_REQUEST:
1771         return switch_status_handle_request(p->switch_status, ofconn->rconn,
1772                                             msg);
1773     }
1774
1775     return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
1776 }
1777
1778 static int
1779 handle_barrier_request(struct ofconn *ofconn, struct ofp_header *oh)
1780 {
1781     struct ofp_header *ob;
1782     struct ofpbuf *buf;
1783
1784     /* Currently, everything executes synchronously, so we can just
1785      * immediately send the barrier reply. */
1786     ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
1787     queue_tx(buf, ofconn, ofconn->reply_counter);
1788     return 0;
1789 }
1790
1791 static void
1792 handle_openflow(struct ofconn *ofconn, struct ofproto *p,
1793                 struct ofpbuf *ofp_msg)
1794 {
1795     struct ofp_header *oh = ofp_msg->data;
1796     int error;
1797
1798     COVERAGE_INC(ofproto_recv_openflow);
1799     switch (oh->type) {
1800     case OFPT_ECHO_REQUEST:
1801         error = handle_echo_request(ofconn, oh);
1802         break;
1803
1804     case OFPT_ECHO_REPLY:
1805         error = 0;
1806         break;
1807
1808     case OFPT_FEATURES_REQUEST:
1809         error = handle_features_request(p, ofconn, oh);
1810         break;
1811
1812     case OFPT_GET_CONFIG_REQUEST:
1813         error = handle_get_config_request(p, ofconn, oh);
1814         break;
1815
1816     case OFPT_SET_CONFIG:
1817         error = handle_set_config(p, ofconn, ofp_msg->data);
1818         break;
1819
1820     case OFPT_PACKET_OUT:
1821         error = handle_packet_out(p, ofconn, ofp_msg->data);
1822         break;
1823
1824     case OFPT_PORT_MOD:
1825         error = handle_port_mod(p, oh);
1826         break;
1827
1828     case OFPT_FLOW_MOD:
1829         error = handle_flow_mod(p, ofconn, ofp_msg->data);
1830         break;
1831
1832     case OFPT_STATS_REQUEST:
1833         error = handle_stats_request(p, ofconn, oh);
1834         break;
1835
1836     case OFPT_VENDOR:
1837         error = handle_vendor(p, ofconn, ofp_msg->data);
1838         break;
1839
1840     case OFPT_BARRIER_REQUEST:
1841         error = handle_barrier_request(ofconn, oh);
1842         break;
1843
1844     default:
1845         if (VLOG_IS_WARN_ENABLED()) {
1846             char *s = ofp_to_string(oh, ntohs(oh->length), 2);
1847             VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
1848             free(s);
1849         }
1850         error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
1851         break;
1852     }
1853
1854     if (error) {
1855         send_error_oh(ofconn, ofp_msg->data, error);
1856     }
1857 }
1858 \f
1859 static void
1860 handle_flow_miss(struct ofproto *p, struct wdp_packet *packet)
1861 {
1862     struct wdp_rule *rule;
1863     flow_t flow;
1864
1865     flow_extract(packet->payload, packet->in_port, &flow);
1866     rule = wdp_flow_match(p->wdp, &flow);
1867     if (!rule) {
1868         /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1869         struct wdp_port *port;
1870
1871         wdp_port_query_by_number(p->wdp, packet->in_port, &port);
1872         if (port) {
1873             if (port->opp.config & OFPPC_NO_PACKET_IN) {
1874                 COVERAGE_INC(ofproto_no_packet_in);
1875                 wdp_packet_destroy(packet);
1876                 return;
1877             }
1878         } else {
1879             VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1880                          packet->in_port);
1881         }
1882
1883         COVERAGE_INC(ofproto_packet_in);
1884         pinsched_send(p->miss_sched, packet->in_port, packet,
1885                       send_packet_in_miss, p);
1886         return;
1887     }
1888
1889     wdp_flow_inject(p->wdp, rule, packet->in_port, packet->payload);
1890
1891     if (rule->cr.flow.priority == FAIL_OPEN_PRIORITY
1892         && rconn_is_connected(p->controller->rconn)) {
1893         /*
1894          * Extra-special case for fail-open mode.
1895          *
1896          * We are in fail-open mode and the packet matched the fail-open rule,
1897          * but we are connected to a controller too.  We should send the packet
1898          * up to the controller in the hope that it will try to set up a flow
1899          * and thereby allow us to exit fail-open.
1900          *
1901          * See the top-level comment in fail-open.c for more information.
1902          */
1903         pinsched_send(p->miss_sched, packet->in_port, packet,
1904                       send_packet_in_miss, p);
1905     } else {
1906         wdp_packet_destroy(packet);
1907     }
1908 }
1909
1910 static void
1911 handle_wdp_packet(struct ofproto *p, struct wdp_packet *packet)
1912 {
1913     switch (packet->channel) {
1914     case WDP_CHAN_ACTION:
1915         COVERAGE_INC(ofproto_ctlr_action);
1916         pinsched_send(p->action_sched, packet->in_port, packet,
1917                       send_packet_in_action, p);
1918         break;
1919
1920     case WDP_CHAN_SFLOW:
1921         /* XXX */
1922         wdp_packet_destroy(packet);
1923         break;
1924
1925     case WDP_CHAN_MISS:
1926         handle_flow_miss(p, packet);
1927         break;
1928
1929     case WDP_N_CHANS:
1930     default:
1931         wdp_packet_destroy(packet);
1932         VLOG_WARN_RL(&rl, "received message on unexpected channel %d",
1933                      (int) packet->channel);
1934         break;
1935     }
1936 }
1937 \f
1938 static struct ofpbuf *
1939 compose_flow_removed(const struct wdp_rule *rule, uint8_t reason)
1940 {
1941     long long int tdiff = time_msec() - rule->created;
1942     uint32_t sec = tdiff / 1000;
1943     uint32_t msec = tdiff - (sec * 1000);
1944     struct ofp_flow_removed *ofr;
1945     struct ofpbuf *buf;
1946
1947     ofr = make_openflow(sizeof *ofr, OFPT_FLOW_REMOVED, &buf);
1948     flow_to_match(&rule->cr.flow, &ofr->match);
1949     ofr->cookie = ofproto_rule_cast(rule)->flow_cookie;
1950     ofr->priority = htons(rule->cr.flow.priority);
1951     ofr->reason = reason;
1952     ofr->duration_sec = htonl(sec);
1953     ofr->duration_nsec = htonl(msec * 1000000);
1954     ofr->idle_timeout = htons(rule->idle_timeout);
1955
1956     return buf;
1957 }
1958
1959 static void
1960 delete_flow(struct ofproto *p, struct wdp_rule *rule, uint8_t reason)
1961 {
1962     /* We limit the maximum number of queued flow expirations it by accounting
1963      * them under the counter for replies.  That works because preventing
1964      * OpenFlow requests from being processed also prevents new flows from
1965      * being added (and expiring).  (It also prevents processing OpenFlow
1966      * requests that would not add new flows, so it is imperfect.) */
1967
1968     struct ofproto_rule *ofproto_rule = ofproto_rule_cast(rule);
1969     struct wdp_flow_stats stats;
1970     struct ofpbuf *buf;
1971
1972     if (ofproto_rule->send_flow_removed) {
1973         /* Compose most of the ofp_flow_removed before 'rule' is destroyed. */
1974         buf = compose_flow_removed(rule, reason);
1975     } else {
1976         buf = NULL;
1977     }
1978
1979     if (wdp_flow_delete(p->wdp, rule, &stats)) {
1980         return;
1981     }
1982
1983     if (buf) {
1984         struct ofp_flow_removed *ofr;
1985         struct ofconn *prev = NULL;
1986         struct ofconn *ofconn;
1987
1988         /* Compose the parts of the ofp_flow_removed that require stats. */
1989         ofr = buf->data;
1990         ofr->packet_count = htonll(stats.n_packets);
1991         ofr->byte_count = htonll(stats.n_bytes);
1992
1993         LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
1994             if (rconn_is_connected(ofconn->rconn)) {
1995                 if (prev) {
1996                     queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter);
1997                 }
1998                 prev = ofconn;
1999             }
2000         }
2001         if (prev) {
2002             queue_tx(buf, prev, prev->reply_counter);
2003         } else {
2004             ofpbuf_delete(buf);
2005         }
2006     }
2007     free(ofproto_rule);
2008 }
2009
2010 static void
2011 do_send_packet_in(struct ofconn *ofconn, uint32_t buffer_id,
2012                   const struct wdp_packet *packet, int send_len)
2013 {
2014     struct ofpbuf *opi;
2015     uint8_t reason;
2016
2017     reason = packet->channel == WDP_CHAN_ACTION ? OFPR_ACTION : OFPR_NO_MATCH;
2018     opi = make_packet_in(buffer_id, packet->in_port, reason,
2019                          packet->payload, send_len);
2020     rconn_send_with_limit(ofconn->rconn, opi, ofconn->packet_in_counter, 100);
2021 }
2022
2023 static void
2024 send_packet_in_action(struct wdp_packet *packet, void *p_)
2025 {
2026     struct ofproto *p = p_;
2027     struct ofconn *ofconn;
2028
2029     LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
2030         if (ofconn == p->controller || ofconn->miss_send_len) {
2031             do_send_packet_in(ofconn, UINT32_MAX, packet, packet->send_len);
2032         }
2033     }
2034     wdp_packet_destroy(packet);
2035 }
2036
2037 static void
2038 send_packet_in_miss(struct wdp_packet *packet, void *p_)
2039 {
2040     struct ofproto *p = p_;
2041     bool in_fail_open = p->fail_open && fail_open_is_active(p->fail_open);
2042     struct ofconn *ofconn;
2043
2044     LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
2045         if (ofconn->miss_send_len) {
2046             struct pktbuf *pb = ofconn->pktbuf;
2047             uint32_t buffer_id = (in_fail_open
2048                                   ? pktbuf_get_null()
2049                                   : pktbuf_save(pb, packet->payload,
2050                                                 packet->in_port));
2051             int send_len = (buffer_id != UINT32_MAX ? ofconn->miss_send_len
2052                             : UINT32_MAX);
2053             do_send_packet_in(ofconn, buffer_id, packet, send_len);
2054         }
2055     }
2056     wdp_packet_destroy(packet);
2057 }
2058
2059 static uint64_t
2060 pick_datapath_id(const struct ofproto *ofproto)
2061 {
2062     struct wdp_port *port;
2063
2064     wdp_port_query_by_number(ofproto->wdp, OFPP_LOCAL, &port);
2065     if (port) {
2066         uint8_t ea[ETH_ADDR_LEN];
2067         int error;
2068
2069         error = netdev_get_etheraddr(port->netdev, ea);
2070         if (!error) {
2071             return eth_addr_to_uint64(ea);
2072         }
2073         VLOG_WARN("could not get MAC address for %s (%s)",
2074                   netdev_get_name(port->netdev), strerror(error));
2075     }
2076
2077     return ofproto->fallback_dpid;
2078 }
2079
2080 static uint64_t
2081 pick_fallback_dpid(void)
2082 {
2083     uint8_t ea[ETH_ADDR_LEN];
2084     eth_addr_nicira_random(ea);
2085     return eth_addr_to_uint64(ea);
2086 }