dpif-netdev: Introduce new mutex to protect queues.
[sliver-openvswitch.git] / lib / dpif-netdev.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "dpif.h"
19
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <net/if.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include "classifier.h"
35 #include "csum.h"
36 #include "dpif.h"
37 #include "dpif-provider.h"
38 #include "dummy.h"
39 #include "dynamic-string.h"
40 #include "flow.h"
41 #include "hmap.h"
42 #include "list.h"
43 #include "meta-flow.h"
44 #include "netdev.h"
45 #include "netdev-vport.h"
46 #include "netlink.h"
47 #include "odp-execute.h"
48 #include "odp-util.h"
49 #include "ofp-print.h"
50 #include "ofpbuf.h"
51 #include "packets.h"
52 #include "poll-loop.h"
53 #include "random.h"
54 #include "seq.h"
55 #include "shash.h"
56 #include "sset.h"
57 #include "timeval.h"
58 #include "unixctl.h"
59 #include "util.h"
60 #include "vlog.h"
61
62 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
63
64 /* By default, choose a priority in the middle. */
65 #define NETDEV_RULE_PRIORITY 0x8000
66
67 /* Configuration parameters. */
68 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
69
70 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
71  * headers to be aligned on a 4-byte boundary.  */
72 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
73
74 /* Queues. */
75 enum { N_QUEUES = 2 };          /* Number of queues for dpif_recv(). */
76 enum { MAX_QUEUE_LEN = 128 };   /* Maximum number of packets per queue. */
77 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
78 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
79
80 struct dp_netdev_upcall {
81     struct dpif_upcall upcall;  /* Queued upcall information. */
82     struct ofpbuf buf;          /* ofpbuf instance for upcall.packet. */
83 };
84
85 struct dp_netdev_queue {
86     struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
87     unsigned int head OVS_GUARDED;
88     unsigned int tail OVS_GUARDED;
89 };
90
91 /* Datapath based on the network device interface from netdev.h. */
92 struct dp_netdev {
93     const struct dpif_class *class;
94     char *name;
95     struct ovs_refcount ref_cnt;
96     atomic_flag destroyed;
97
98     struct classifier cls;      /* Classifier. */
99     struct hmap flow_table;     /* Flow table. */
100
101     /* Queues. */
102     struct ovs_mutex queue_mutex;
103     struct dp_netdev_queue queues[N_QUEUES];
104     struct seq *queue_seq;      /* Incremented whenever a packet is queued. */
105
106     /* Statistics. */
107     struct ovsthread_counter *n_hit;    /* Number of flow table matches. */
108     struct ovsthread_counter *n_missed; /* Number of flow table misses. */
109     struct ovsthread_counter *n_lost;   /* Number of misses not passed up. */
110
111     /* Ports. */
112     struct hmap ports;
113     struct seq *port_seq;       /* Incremented whenever a port changes. */
114 };
115
116 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *,
117                                                     odp_port_t);
118
119 /* A port in a netdev-based datapath. */
120 struct dp_netdev_port {
121     struct hmap_node node;      /* Node in dp_netdev's 'ports'. */
122     odp_port_t port_no;
123     struct netdev *netdev;
124     struct netdev_saved_flags *sf;
125     struct netdev_rx *rx;
126     char *type;                 /* Port type as requested by user. */
127 };
128
129 /* A flow in dp_netdev's 'flow_table'. */
130 struct dp_netdev_flow {
131     /* Packet classification. */
132     struct cls_rule cr;         /* In owning dp_netdev's 'cls'. */
133
134     /* Hash table index by unmasked flow.*/
135     struct hmap_node node;      /* In owning dp_netdev's 'flow_table'. */
136     struct flow flow;           /* The flow that created this entry. */
137
138     /* Statistics. */
139     long long int used;         /* Last used time, in monotonic msecs. */
140     long long int packet_count; /* Number of packets matched. */
141     long long int byte_count;   /* Number of bytes matched. */
142     uint16_t tcp_flags;         /* Bitwise-OR of seen tcp_flags values. */
143
144     /* Actions. */
145     struct dp_netdev_actions *actions;
146 };
147
148 /* A set of datapath actions within a "struct dp_netdev_flow".
149  *
150  *
151  * Thread-safety
152  * =============
153  *
154  * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
155  * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
156  * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
157  * owns a reference to 'actions->ref_cnt' (or both). */
158 struct dp_netdev_actions {
159     struct ovs_refcount ref_cnt;
160
161     /* These members are immutable: they do not change during the struct's
162      * lifetime.  */
163     struct nlattr *actions;     /* Sequence of OVS_ACTION_ATTR_* attributes. */
164     unsigned int size;          /* Size of 'actions', in bytes. */
165 };
166
167 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
168                                                    size_t);
169 struct dp_netdev_actions *dp_netdev_actions_ref(
170     const struct dp_netdev_actions *);
171 void dp_netdev_actions_unref(struct dp_netdev_actions *);
172
173 /* Interface to netdev-based datapath. */
174 struct dpif_netdev {
175     struct dpif dpif;
176     struct dp_netdev *dp;
177     uint64_t last_port_seq;
178 };
179
180 /* All netdev-based datapaths. */
181 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
182
183 /* Global lock for all data. */
184 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
185
186 static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
187                               struct dp_netdev_port **portp);
188 static int get_port_by_name(struct dp_netdev *, const char *devname,
189                             struct dp_netdev_port **portp);
190 static void dp_netdev_free(struct dp_netdev *);
191 static void dp_netdev_flow_flush(struct dp_netdev *);
192 static int do_add_port(struct dp_netdev *, const char *devname,
193                        const char *type, odp_port_t port_no);
194 static int do_del_port(struct dp_netdev *, odp_port_t port_no);
195 static int dpif_netdev_open(const struct dpif_class *, const char *name,
196                             bool create, struct dpif **);
197 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
198                                     int queue_no, const struct flow *,
199                                     const struct nlattr *userdata)
200     OVS_EXCLUDED(dp->queue_mutex);
201 static void dp_netdev_execute_actions(struct dp_netdev *, const struct flow *,
202                                       struct ofpbuf *, struct pkt_metadata *,
203                                       const struct nlattr *actions,
204                                       size_t actions_len);
205 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
206                                  struct pkt_metadata *md);
207
208 static struct dpif_netdev *
209 dpif_netdev_cast(const struct dpif *dpif)
210 {
211     ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
212     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
213 }
214
215 static struct dp_netdev *
216 get_dp_netdev(const struct dpif *dpif)
217 {
218     return dpif_netdev_cast(dpif)->dp;
219 }
220
221 static int
222 dpif_netdev_enumerate(struct sset *all_dps)
223 {
224     struct shash_node *node;
225
226     ovs_mutex_lock(&dp_netdev_mutex);
227     SHASH_FOR_EACH(node, &dp_netdevs) {
228         sset_add(all_dps, node->name);
229     }
230     ovs_mutex_unlock(&dp_netdev_mutex);
231
232     return 0;
233 }
234
235 static bool
236 dpif_netdev_class_is_dummy(const struct dpif_class *class)
237 {
238     return class != &dpif_netdev_class;
239 }
240
241 static const char *
242 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
243 {
244     return strcmp(type, "internal") ? type
245                   : dpif_netdev_class_is_dummy(class) ? "dummy"
246                   : "tap";
247 }
248
249 static struct dpif *
250 create_dpif_netdev(struct dp_netdev *dp)
251 {
252     uint16_t netflow_id = hash_string(dp->name, 0);
253     struct dpif_netdev *dpif;
254
255     ovs_refcount_ref(&dp->ref_cnt);
256
257     dpif = xmalloc(sizeof *dpif);
258     dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
259     dpif->dp = dp;
260     dpif->last_port_seq = seq_read(dp->port_seq);
261
262     return &dpif->dpif;
263 }
264
265 /* Choose an unused, non-zero port number and return it on success.
266  * Return ODPP_NONE on failure. */
267 static odp_port_t
268 choose_port(struct dp_netdev *dp, const char *name)
269 {
270     uint32_t port_no;
271
272     if (dp->class != &dpif_netdev_class) {
273         const char *p;
274         int start_no = 0;
275
276         /* If the port name begins with "br", start the number search at
277          * 100 to make writing tests easier. */
278         if (!strncmp(name, "br", 2)) {
279             start_no = 100;
280         }
281
282         /* If the port name contains a number, try to assign that port number.
283          * This can make writing unit tests easier because port numbers are
284          * predictable. */
285         for (p = name; *p != '\0'; p++) {
286             if (isdigit((unsigned char) *p)) {
287                 port_no = start_no + strtol(p, NULL, 10);
288                 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
289                     && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
290                     return u32_to_odp(port_no);
291                 }
292                 break;
293             }
294         }
295     }
296
297     for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
298         if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
299             return u32_to_odp(port_no);
300         }
301     }
302
303     return ODPP_NONE;
304 }
305
306 static int
307 create_dp_netdev(const char *name, const struct dpif_class *class,
308                  struct dp_netdev **dpp)
309 {
310     struct dp_netdev *dp;
311     int error;
312     int i;
313
314     dp = xzalloc(sizeof *dp);
315     dp->class = class;
316     dp->name = xstrdup(name);
317     ovs_refcount_init(&dp->ref_cnt);
318     atomic_flag_init(&dp->destroyed);
319     ovs_mutex_init(&dp->queue_mutex);
320     ovs_mutex_lock(&dp->queue_mutex);
321     for (i = 0; i < N_QUEUES; i++) {
322         dp->queues[i].head = dp->queues[i].tail = 0;
323     }
324     ovs_mutex_unlock(&dp->queue_mutex);
325     dp->queue_seq = seq_create();
326     classifier_init(&dp->cls, NULL);
327     hmap_init(&dp->flow_table);
328
329     dp->n_hit = ovsthread_counter_create();
330     dp->n_missed = ovsthread_counter_create();
331     dp->n_lost = ovsthread_counter_create();
332
333     hmap_init(&dp->ports);
334     dp->port_seq = seq_create();
335
336     error = do_add_port(dp, name, "internal", ODPP_LOCAL);
337     if (error) {
338         dp_netdev_free(dp);
339         return error;
340     }
341
342     shash_add(&dp_netdevs, name, dp);
343
344     *dpp = dp;
345     return 0;
346 }
347
348 static int
349 dpif_netdev_open(const struct dpif_class *class, const char *name,
350                  bool create, struct dpif **dpifp)
351 {
352     struct dp_netdev *dp;
353     int error;
354
355     ovs_mutex_lock(&dp_netdev_mutex);
356     dp = shash_find_data(&dp_netdevs, name);
357     if (!dp) {
358         error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
359     } else {
360         error = (dp->class != class ? EINVAL
361                  : create ? EEXIST
362                  : 0);
363     }
364     if (!error) {
365         *dpifp = create_dpif_netdev(dp);
366     }
367     ovs_mutex_unlock(&dp_netdev_mutex);
368
369     return error;
370 }
371
372 static void
373 dp_netdev_purge_queues(struct dp_netdev *dp)
374 {
375     int i;
376
377     ovs_mutex_lock(&dp->queue_mutex);
378     for (i = 0; i < N_QUEUES; i++) {
379         struct dp_netdev_queue *q = &dp->queues[i];
380
381         while (q->tail != q->head) {
382             struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
383             ofpbuf_uninit(&u->upcall.packet);
384             ofpbuf_uninit(&u->buf);
385         }
386     }
387     ovs_mutex_unlock(&dp->queue_mutex);
388 }
389
390 static void
391 dp_netdev_free(struct dp_netdev *dp)
392 {
393     struct dp_netdev_port *port, *next;
394
395     dp_netdev_flow_flush(dp);
396     HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
397         do_del_port(dp, port->port_no);
398     }
399     ovsthread_counter_destroy(dp->n_hit);
400     ovsthread_counter_destroy(dp->n_missed);
401     ovsthread_counter_destroy(dp->n_lost);
402
403     dp_netdev_purge_queues(dp);
404     seq_destroy(dp->queue_seq);
405     ovs_mutex_destroy(&dp->queue_mutex);
406
407     classifier_destroy(&dp->cls);
408     hmap_destroy(&dp->flow_table);
409     seq_destroy(dp->port_seq);
410     hmap_destroy(&dp->ports);
411     atomic_flag_destroy(&dp->destroyed);
412     ovs_refcount_destroy(&dp->ref_cnt);
413     free(dp->name);
414     free(dp);
415 }
416
417 static void
418 dpif_netdev_close(struct dpif *dpif)
419 {
420     struct dp_netdev *dp = get_dp_netdev(dpif);
421
422     ovs_mutex_lock(&dp_netdev_mutex);
423
424     if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
425         shash_find_and_delete(&dp_netdevs, dp->name);
426         dp_netdev_free(dp);
427     }
428     free(dpif);
429
430     ovs_mutex_unlock(&dp_netdev_mutex);
431 }
432
433 static int
434 dpif_netdev_destroy(struct dpif *dpif)
435 {
436     struct dp_netdev *dp = get_dp_netdev(dpif);
437
438     if (!atomic_flag_test_and_set(&dp->destroyed)) {
439         if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
440             /* Can't happen: 'dpif' still owns a reference to 'dp'. */
441             OVS_NOT_REACHED();
442         }
443     }
444
445     return 0;
446 }
447
448 static int
449 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
450 {
451     struct dp_netdev *dp = get_dp_netdev(dpif);
452
453     ovs_mutex_lock(&dp_netdev_mutex);
454     stats->n_flows = hmap_count(&dp->flow_table);
455     stats->n_hit = ovsthread_counter_read(dp->n_hit);
456     stats->n_missed = ovsthread_counter_read(dp->n_missed);
457     stats->n_lost = ovsthread_counter_read(dp->n_lost);
458     stats->n_masks = UINT32_MAX;
459     stats->n_mask_hit = UINT64_MAX;
460     ovs_mutex_unlock(&dp_netdev_mutex);
461
462     return 0;
463 }
464
465 static int
466 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
467             odp_port_t port_no)
468 {
469     struct netdev_saved_flags *sf;
470     struct dp_netdev_port *port;
471     struct netdev *netdev;
472     struct netdev_rx *rx;
473     enum netdev_flags flags;
474     const char *open_type;
475     int error;
476
477     /* XXX reject devices already in some dp_netdev. */
478
479     /* Open and validate network device. */
480     open_type = dpif_netdev_port_open_type(dp->class, type);
481     error = netdev_open(devname, open_type, &netdev);
482     if (error) {
483         return error;
484     }
485     /* XXX reject non-Ethernet devices */
486
487     netdev_get_flags(netdev, &flags);
488     if (flags & NETDEV_LOOPBACK) {
489         VLOG_ERR("%s: cannot add a loopback device", devname);
490         netdev_close(netdev);
491         return EINVAL;
492     }
493
494     error = netdev_rx_open(netdev, &rx);
495     if (error
496         && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
497         VLOG_ERR("%s: cannot receive packets on this network device (%s)",
498                  devname, ovs_strerror(errno));
499         netdev_close(netdev);
500         return error;
501     }
502
503     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
504     if (error) {
505         netdev_rx_close(rx);
506         netdev_close(netdev);
507         return error;
508     }
509
510     port = xmalloc(sizeof *port);
511     port->port_no = port_no;
512     port->netdev = netdev;
513     port->sf = sf;
514     port->rx = rx;
515     port->type = xstrdup(type);
516
517     hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
518     seq_change(dp->port_seq);
519
520     return 0;
521 }
522
523 static int
524 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
525                      odp_port_t *port_nop)
526 {
527     struct dp_netdev *dp = get_dp_netdev(dpif);
528     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
529     const char *dpif_port;
530     odp_port_t port_no;
531     int error;
532
533     ovs_mutex_lock(&dp_netdev_mutex);
534     dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
535     if (*port_nop != ODPP_NONE) {
536         port_no = *port_nop;
537         error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
538     } else {
539         port_no = choose_port(dp, dpif_port);
540         error = port_no == ODPP_NONE ? EFBIG : 0;
541     }
542     if (!error) {
543         *port_nop = port_no;
544         error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
545     }
546     ovs_mutex_unlock(&dp_netdev_mutex);
547
548     return error;
549 }
550
551 static int
552 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
553 {
554     struct dp_netdev *dp = get_dp_netdev(dpif);
555     int error;
556
557     ovs_mutex_lock(&dp_netdev_mutex);
558     error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
559     ovs_mutex_unlock(&dp_netdev_mutex);
560
561     return error;
562 }
563
564 static bool
565 is_valid_port_number(odp_port_t port_no)
566 {
567     return port_no != ODPP_NONE;
568 }
569
570 static struct dp_netdev_port *
571 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
572 {
573     struct dp_netdev_port *port;
574
575     HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
576                              &dp->ports) {
577         if (port->port_no == port_no) {
578             return port;
579         }
580     }
581     return NULL;
582 }
583
584 static int
585 get_port_by_number(struct dp_netdev *dp,
586                    odp_port_t port_no, struct dp_netdev_port **portp)
587 {
588     if (!is_valid_port_number(port_no)) {
589         *portp = NULL;
590         return EINVAL;
591     } else {
592         *portp = dp_netdev_lookup_port(dp, port_no);
593         return *portp ? 0 : ENOENT;
594     }
595 }
596
597 static int
598 get_port_by_name(struct dp_netdev *dp,
599                  const char *devname, struct dp_netdev_port **portp)
600 {
601     struct dp_netdev_port *port;
602
603     HMAP_FOR_EACH (port, node, &dp->ports) {
604         if (!strcmp(netdev_get_name(port->netdev), devname)) {
605             *portp = port;
606             return 0;
607         }
608     }
609     return ENOENT;
610 }
611
612 static int
613 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
614 {
615     struct dp_netdev_port *port;
616     int error;
617
618     error = get_port_by_number(dp, port_no, &port);
619     if (error) {
620         return error;
621     }
622
623     hmap_remove(&dp->ports, &port->node);
624     seq_change(dp->port_seq);
625
626     netdev_close(port->netdev);
627     netdev_restore_flags(port->sf);
628     netdev_rx_close(port->rx);
629     free(port->type);
630     free(port);
631
632     return 0;
633 }
634
635 static void
636 answer_port_query(const struct dp_netdev_port *port,
637                   struct dpif_port *dpif_port)
638 {
639     dpif_port->name = xstrdup(netdev_get_name(port->netdev));
640     dpif_port->type = xstrdup(port->type);
641     dpif_port->port_no = port->port_no;
642 }
643
644 static int
645 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
646                                  struct dpif_port *dpif_port)
647 {
648     struct dp_netdev *dp = get_dp_netdev(dpif);
649     struct dp_netdev_port *port;
650     int error;
651
652     ovs_mutex_lock(&dp_netdev_mutex);
653     error = get_port_by_number(dp, port_no, &port);
654     if (!error && dpif_port) {
655         answer_port_query(port, dpif_port);
656     }
657     ovs_mutex_unlock(&dp_netdev_mutex);
658
659     return error;
660 }
661
662 static int
663 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
664                                struct dpif_port *dpif_port)
665 {
666     struct dp_netdev *dp = get_dp_netdev(dpif);
667     struct dp_netdev_port *port;
668     int error;
669
670     ovs_mutex_lock(&dp_netdev_mutex);
671     error = get_port_by_name(dp, devname, &port);
672     if (!error && dpif_port) {
673         answer_port_query(port, dpif_port);
674     }
675     ovs_mutex_unlock(&dp_netdev_mutex);
676
677     return error;
678 }
679
680 static void
681 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *netdev_flow)
682 {
683     ovs_rwlock_wrlock(&dp->cls.rwlock);
684     classifier_remove(&dp->cls, &netdev_flow->cr);
685     ovs_rwlock_unlock(&dp->cls.rwlock);
686     cls_rule_destroy(&netdev_flow->cr);
687
688     hmap_remove(&dp->flow_table, &netdev_flow->node);
689     free(netdev_flow->actions);
690     free(netdev_flow);
691 }
692
693 static void
694 dp_netdev_flow_flush(struct dp_netdev *dp)
695 {
696     struct dp_netdev_flow *netdev_flow, *next;
697
698     HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
699         dp_netdev_free_flow(dp, netdev_flow);
700     }
701 }
702
703 static int
704 dpif_netdev_flow_flush(struct dpif *dpif)
705 {
706     struct dp_netdev *dp = get_dp_netdev(dpif);
707
708     ovs_mutex_lock(&dp_netdev_mutex);
709     dp_netdev_flow_flush(dp);
710     ovs_mutex_unlock(&dp_netdev_mutex);
711
712     return 0;
713 }
714
715 struct dp_netdev_port_state {
716     uint32_t bucket;
717     uint32_t offset;
718     char *name;
719 };
720
721 static int
722 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
723 {
724     *statep = xzalloc(sizeof(struct dp_netdev_port_state));
725     return 0;
726 }
727
728 static int
729 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
730                            struct dpif_port *dpif_port)
731 {
732     struct dp_netdev_port_state *state = state_;
733     struct dp_netdev *dp = get_dp_netdev(dpif);
734     struct hmap_node *node;
735     int retval;
736
737     ovs_mutex_lock(&dp_netdev_mutex);
738     node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
739     if (node) {
740         struct dp_netdev_port *port;
741
742         port = CONTAINER_OF(node, struct dp_netdev_port, node);
743
744         free(state->name);
745         state->name = xstrdup(netdev_get_name(port->netdev));
746         dpif_port->name = state->name;
747         dpif_port->type = port->type;
748         dpif_port->port_no = port->port_no;
749
750         retval = 0;
751     } else {
752         retval = EOF;
753     }
754     ovs_mutex_unlock(&dp_netdev_mutex);
755
756     return retval;
757 }
758
759 static int
760 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
761 {
762     struct dp_netdev_port_state *state = state_;
763     free(state->name);
764     free(state);
765     return 0;
766 }
767
768 static int
769 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
770 {
771     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
772     uint64_t new_port_seq;
773     int error;
774
775     ovs_mutex_lock(&dp_netdev_mutex);
776     new_port_seq = seq_read(dpif->dp->port_seq);
777     if (dpif->last_port_seq != new_port_seq) {
778         dpif->last_port_seq = new_port_seq;
779         error = ENOBUFS;
780     } else {
781         error = EAGAIN;
782     }
783     ovs_mutex_unlock(&dp_netdev_mutex);
784
785     return error;
786 }
787
788 static void
789 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
790 {
791     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
792
793     ovs_mutex_lock(&dp_netdev_mutex);
794     seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
795     ovs_mutex_unlock(&dp_netdev_mutex);
796 }
797
798 static struct dp_netdev_flow *
799 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
800 {
801     struct cls_rule *cr;
802
803     ovs_rwlock_wrlock(&dp->cls.rwlock);
804     cr = classifier_lookup(&dp->cls, flow, NULL);
805     ovs_rwlock_unlock(&dp->cls.rwlock);
806
807     return (cr
808             ? CONTAINER_OF(cr, struct dp_netdev_flow, cr)
809             : NULL);
810 }
811
812 static struct dp_netdev_flow *
813 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
814 {
815     struct dp_netdev_flow *netdev_flow;
816
817     HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
818                              &dp->flow_table) {
819         if (flow_equal(&netdev_flow->flow, flow)) {
820             return netdev_flow;
821         }
822     }
823     return NULL;
824 }
825
826 static void
827 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
828                     struct dpif_flow_stats *stats)
829 {
830     stats->n_packets = netdev_flow->packet_count;
831     stats->n_bytes = netdev_flow->byte_count;
832     stats->used = netdev_flow->used;
833     stats->tcp_flags = netdev_flow->tcp_flags;
834 }
835
836 static int
837 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
838                               const struct nlattr *mask_key,
839                               uint32_t mask_key_len, const struct flow *flow,
840                               struct flow *mask)
841 {
842     if (mask_key_len) {
843         if (odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow)) {
844             /* This should not happen: it indicates that
845              * odp_flow_key_from_mask() and odp_flow_key_to_mask()
846              * disagree on the acceptable form of a mask.  Log the problem
847              * as an error, with enough details to enable debugging. */
848             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
849
850             if (!VLOG_DROP_ERR(&rl)) {
851                 struct ds s;
852
853                 ds_init(&s);
854                 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
855                                 true);
856                 VLOG_ERR("internal error parsing flow mask %s", ds_cstr(&s));
857                 ds_destroy(&s);
858             }
859
860             return EINVAL;
861         }
862         /* Force unwildcard the in_port. */
863         mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
864     } else {
865         enum mf_field_id id;
866         /* No mask key, unwildcard everything except fields whose
867          * prerequisities are not met. */
868         memset(mask, 0x0, sizeof *mask);
869
870         for (id = 0; id < MFF_N_IDS; ++id) {
871             /* Skip registers and metadata. */
872             if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
873                 && id != MFF_METADATA) {
874                 const struct mf_field *mf = mf_from_id(id);
875                 if (mf_are_prereqs_ok(mf, flow)) {
876                     mf_mask_field(mf, mask);
877                 }
878             }
879         }
880     }
881
882     return 0;
883 }
884
885 static int
886 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
887                               struct flow *flow)
888 {
889     odp_port_t in_port;
890
891     if (odp_flow_key_to_flow(key, key_len, flow)) {
892         /* This should not happen: it indicates that odp_flow_key_from_flow()
893          * and odp_flow_key_to_flow() disagree on the acceptable form of a
894          * flow.  Log the problem as an error, with enough details to enable
895          * debugging. */
896         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
897
898         if (!VLOG_DROP_ERR(&rl)) {
899             struct ds s;
900
901             ds_init(&s);
902             odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
903             VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
904             ds_destroy(&s);
905         }
906
907         return EINVAL;
908     }
909
910     in_port = flow->in_port.odp_port;
911     if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
912         return EINVAL;
913     }
914
915     return 0;
916 }
917
918 static int
919 dpif_netdev_flow_get(const struct dpif *dpif,
920                      const struct nlattr *nl_key, size_t nl_key_len,
921                      struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
922 {
923     struct dp_netdev *dp = get_dp_netdev(dpif);
924     struct dp_netdev_flow *netdev_flow;
925     struct flow key;
926     int error;
927
928     error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
929     if (error) {
930         return error;
931     }
932
933     ovs_mutex_lock(&dp_netdev_mutex);
934     netdev_flow = dp_netdev_find_flow(dp, &key);
935     if (netdev_flow) {
936         if (stats) {
937             get_dpif_flow_stats(netdev_flow, stats);
938         }
939         if (actionsp) {
940             *actionsp = ofpbuf_clone_data(netdev_flow->actions->actions,
941                                           netdev_flow->actions->size);
942         }
943     } else {
944         error = ENOENT;
945     }
946     ovs_mutex_unlock(&dp_netdev_mutex);
947
948     return error;
949 }
950
951 static int
952 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
953                    const struct flow_wildcards *wc,
954                    const struct nlattr *actions,
955                    size_t actions_len)
956 {
957     struct dp_netdev_flow *netdev_flow;
958     struct match match;
959
960     netdev_flow = xzalloc(sizeof *netdev_flow);
961     netdev_flow->flow = *flow;
962     netdev_flow->actions = dp_netdev_actions_create(actions, actions_len);
963
964     match_init(&match, flow, wc);
965     cls_rule_init(&netdev_flow->cr, &match, NETDEV_RULE_PRIORITY);
966     ovs_rwlock_wrlock(&dp->cls.rwlock);
967     classifier_insert(&dp->cls, &netdev_flow->cr);
968     ovs_rwlock_unlock(&dp->cls.rwlock);
969
970     hmap_insert(&dp->flow_table, &netdev_flow->node, flow_hash(flow, 0));
971     return 0;
972 }
973
974 static void
975 clear_stats(struct dp_netdev_flow *netdev_flow)
976 {
977     netdev_flow->used = 0;
978     netdev_flow->packet_count = 0;
979     netdev_flow->byte_count = 0;
980     netdev_flow->tcp_flags = 0;
981 }
982
983 static int
984 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
985 {
986     struct dp_netdev *dp = get_dp_netdev(dpif);
987     struct dp_netdev_flow *netdev_flow;
988     struct flow flow;
989     struct flow_wildcards wc;
990     int error;
991
992     error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
993     if (error) {
994         return error;
995     }
996     error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
997                                           put->mask, put->mask_len,
998                                           &flow, &wc.masks);
999     if (error) {
1000         return error;
1001     }
1002
1003     ovs_mutex_lock(&dp_netdev_mutex);
1004     netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1005     if (!netdev_flow) {
1006         if (put->flags & DPIF_FP_CREATE) {
1007             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1008                 if (put->stats) {
1009                     memset(put->stats, 0, sizeof *put->stats);
1010                 }
1011                 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1012                                            put->actions_len);
1013             } else {
1014                 error = EFBIG;
1015             }
1016         } else {
1017             error = ENOENT;
1018         }
1019     } else {
1020         if (put->flags & DPIF_FP_MODIFY
1021             && flow_equal(&flow, &netdev_flow->flow)) {
1022             dp_netdev_actions_unref(netdev_flow->actions);
1023             netdev_flow->actions = dp_netdev_actions_create(put->actions,
1024                                                             put->actions_len);
1025             if (put->stats) {
1026                 get_dpif_flow_stats(netdev_flow, put->stats);
1027             }
1028             if (put->flags & DPIF_FP_ZERO_STATS) {
1029                 clear_stats(netdev_flow);
1030             }
1031         } else if (put->flags & DPIF_FP_CREATE) {
1032             error = EEXIST;
1033         } else {
1034             /* Overlapping flow. */
1035             error = EINVAL;
1036         }
1037     }
1038     ovs_mutex_unlock(&dp_netdev_mutex);
1039
1040     return error;
1041 }
1042
1043 static int
1044 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1045 {
1046     struct dp_netdev *dp = get_dp_netdev(dpif);
1047     struct dp_netdev_flow *netdev_flow;
1048     struct flow key;
1049     int error;
1050
1051     error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1052     if (error) {
1053         return error;
1054     }
1055
1056     ovs_mutex_lock(&dp_netdev_mutex);
1057     netdev_flow = dp_netdev_find_flow(dp, &key);
1058     if (netdev_flow) {
1059         if (del->stats) {
1060             get_dpif_flow_stats(netdev_flow, del->stats);
1061         }
1062         dp_netdev_free_flow(dp, netdev_flow);
1063     } else {
1064         error = ENOENT;
1065     }
1066     ovs_mutex_unlock(&dp_netdev_mutex);
1067
1068     return error;
1069 }
1070
1071 struct dp_netdev_flow_state {
1072     uint32_t bucket;
1073     uint32_t offset;
1074     struct dp_netdev_actions *actions;
1075     struct odputil_keybuf keybuf;
1076     struct odputil_keybuf maskbuf;
1077     struct dpif_flow_stats stats;
1078 };
1079
1080 static int
1081 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
1082 {
1083     struct dp_netdev_flow_state *state;
1084
1085     *statep = state = xmalloc(sizeof *state);
1086     state->bucket = 0;
1087     state->offset = 0;
1088     state->actions = NULL;
1089     return 0;
1090 }
1091
1092 static int
1093 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
1094                            const struct nlattr **key, size_t *key_len,
1095                            const struct nlattr **mask, size_t *mask_len,
1096                            const struct nlattr **actions, size_t *actions_len,
1097                            const struct dpif_flow_stats **stats)
1098 {
1099     struct dp_netdev_flow_state *state = state_;
1100     struct dp_netdev *dp = get_dp_netdev(dpif);
1101     struct dp_netdev_flow *netdev_flow;
1102     struct hmap_node *node;
1103
1104     ovs_mutex_lock(&dp_netdev_mutex);
1105     node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
1106     if (!node) {
1107         ovs_mutex_unlock(&dp_netdev_mutex);
1108         return EOF;
1109     }
1110
1111     netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1112
1113     if (key) {
1114         struct ofpbuf buf;
1115
1116         ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1117         odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1118                                netdev_flow->flow.in_port.odp_port);
1119
1120         *key = buf.data;
1121         *key_len = buf.size;
1122     }
1123
1124     if (key && mask) {
1125         struct ofpbuf buf;
1126         struct flow_wildcards wc;
1127
1128         ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1129         minimask_expand(&netdev_flow->cr.match.mask, &wc);
1130         odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1131                                odp_to_u32(wc.masks.in_port.odp_port));
1132
1133         *mask = buf.data;
1134         *mask_len = buf.size;
1135     }
1136
1137     if (actions) {
1138         dp_netdev_actions_unref(state->actions);
1139         state->actions = dp_netdev_actions_ref(netdev_flow->actions);
1140         *actions = state->actions->actions;
1141         *actions_len = state->actions->size;
1142     }
1143
1144     if (stats) {
1145         get_dpif_flow_stats(netdev_flow, &state->stats);
1146         *stats = &state->stats;
1147     }
1148
1149     ovs_mutex_unlock(&dp_netdev_mutex);
1150     return 0;
1151 }
1152
1153 static int
1154 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1155 {
1156     struct dp_netdev_flow_state *state = state_;
1157
1158     dp_netdev_actions_unref(state->actions);
1159     free(state);
1160     return 0;
1161 }
1162
1163 static int
1164 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1165 {
1166     struct dp_netdev *dp = get_dp_netdev(dpif);
1167     struct pkt_metadata *md = &execute->md;
1168     struct flow key;
1169
1170     if (execute->packet->size < ETH_HEADER_LEN ||
1171         execute->packet->size > UINT16_MAX) {
1172         return EINVAL;
1173     }
1174
1175     /* Extract flow key. */
1176     flow_extract(execute->packet, md->skb_priority, md->pkt_mark, &md->tunnel,
1177                  (union flow_in_port *)&md->in_port, &key);
1178     ovs_mutex_lock(&dp_netdev_mutex);
1179     dp_netdev_execute_actions(dp, &key, execute->packet, md, execute->actions,
1180                               execute->actions_len);
1181     ovs_mutex_unlock(&dp_netdev_mutex);
1182     return 0;
1183 }
1184
1185 static int
1186 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1187 {
1188     return 0;
1189 }
1190
1191 static int
1192 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1193                               uint32_t queue_id, uint32_t *priority)
1194 {
1195     *priority = queue_id;
1196     return 0;
1197 }
1198
1199 static struct dp_netdev_queue *
1200 find_nonempty_queue(struct dp_netdev *dp)
1201     OVS_REQUIRES(dp->queue_mutex)
1202 {
1203     int i;
1204
1205     for (i = 0; i < N_QUEUES; i++) {
1206         struct dp_netdev_queue *q = &dp->queues[i];
1207         if (q->head != q->tail) {
1208             return q;
1209         }
1210     }
1211     return NULL;
1212 }
1213
1214 static int
1215 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1216                  struct ofpbuf *buf)
1217 {
1218     struct dp_netdev *dp = get_dp_netdev(dpif);
1219     struct dp_netdev_queue *q;
1220     int error;
1221
1222     ovs_mutex_lock(&dp->queue_mutex);
1223     q = find_nonempty_queue(dp);
1224     if (q) {
1225         struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1226
1227         *upcall = u->upcall;
1228
1229         ofpbuf_uninit(buf);
1230         *buf = u->buf;
1231
1232         error = 0;
1233     } else {
1234         error = EAGAIN;
1235     }
1236     ovs_mutex_unlock(&dp->queue_mutex);
1237
1238     return error;
1239 }
1240
1241 static void
1242 dpif_netdev_recv_wait(struct dpif *dpif)
1243 {
1244     struct dp_netdev *dp = get_dp_netdev(dpif);
1245     uint64_t seq;
1246
1247     ovs_mutex_lock(&dp->queue_mutex);
1248     seq = seq_read(dp->queue_seq);
1249     if (find_nonempty_queue(dp)) {
1250         poll_immediate_wake();
1251     } else {
1252         seq_wait(dp->queue_seq, seq);
1253     }
1254     ovs_mutex_unlock(&dp->queue_mutex);
1255 }
1256
1257 static void
1258 dpif_netdev_recv_purge(struct dpif *dpif)
1259 {
1260     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1261
1262     dp_netdev_purge_queues(dpif_netdev->dp);
1263 }
1264 \f
1265 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1266  * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1267  * 'ofpacts'. */
1268 struct dp_netdev_actions *
1269 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1270 {
1271     struct dp_netdev_actions *netdev_actions;
1272
1273     netdev_actions = xmalloc(sizeof *netdev_actions);
1274     ovs_refcount_init(&netdev_actions->ref_cnt);
1275     netdev_actions->actions = xmemdup(actions, size);
1276     netdev_actions->size = size;
1277
1278     return netdev_actions;
1279 }
1280
1281 /* Increments 'actions''s refcount. */
1282 struct dp_netdev_actions *
1283 dp_netdev_actions_ref(const struct dp_netdev_actions *actions_)
1284 {
1285     struct dp_netdev_actions *actions;
1286
1287     actions = CONST_CAST(struct dp_netdev_actions *, actions_);
1288     if (actions) {
1289         ovs_refcount_ref(&actions->ref_cnt);
1290     }
1291     return actions;
1292 }
1293
1294 /* Decrements 'actions''s refcount and frees 'actions' if the refcount reaches
1295  * 0. */
1296 void
1297 dp_netdev_actions_unref(struct dp_netdev_actions *actions)
1298 {
1299     if (actions && ovs_refcount_unref(&actions->ref_cnt) == 1) {
1300         free(actions->actions);
1301         free(actions);
1302     }
1303 }
1304 \f
1305 static void
1306 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1307                     const struct ofpbuf *packet)
1308 {
1309     netdev_flow->used = time_msec();
1310     netdev_flow->packet_count++;
1311     netdev_flow->byte_count += packet->size;
1312     netdev_flow->tcp_flags |= packet_get_tcp_flags(packet, &netdev_flow->flow);
1313 }
1314
1315 static void
1316 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1317                      struct pkt_metadata *md)
1318 {
1319     struct dp_netdev_flow *netdev_flow;
1320     struct flow key;
1321
1322     if (packet->size < ETH_HEADER_LEN) {
1323         return;
1324     }
1325     flow_extract(packet, md->skb_priority, md->pkt_mark, &md->tunnel,
1326                  (union flow_in_port *)&md->in_port, &key);
1327     netdev_flow = dp_netdev_lookup_flow(dp, &key);
1328     if (netdev_flow) {
1329         struct dp_netdev_actions *actions;
1330
1331         dp_netdev_flow_used(netdev_flow, packet);
1332         actions = dp_netdev_actions_ref(netdev_flow->actions);
1333         dp_netdev_execute_actions(dp, &key, packet, md,
1334                                   actions->actions, actions->size);
1335         dp_netdev_actions_unref(actions);
1336         ovsthread_counter_inc(dp->n_hit, 1);
1337     } else {
1338         ovsthread_counter_inc(dp->n_missed, 1);
1339         dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1340     }
1341 }
1342
1343 static void
1344 dpif_netdev_run(struct dpif *dpif)
1345 {
1346     struct dp_netdev_port *port;
1347     struct dp_netdev *dp;
1348     struct ofpbuf packet;
1349
1350     ovs_mutex_lock(&dp_netdev_mutex);
1351     dp = get_dp_netdev(dpif);
1352     ofpbuf_init(&packet, 0);
1353
1354     HMAP_FOR_EACH (port, node, &dp->ports) {
1355         int buf_size;
1356         int error;
1357         int mtu;
1358
1359         error = netdev_get_mtu(port->netdev, &mtu);
1360         if (error) {
1361             mtu = ETH_PAYLOAD_MAX;
1362         }
1363         buf_size = DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + mtu;
1364
1365         ofpbuf_clear(&packet);
1366         ofpbuf_reserve_with_tailroom(&packet, DP_NETDEV_HEADROOM, buf_size);
1367
1368         error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1369         if (!error) {
1370             struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1371             dp_netdev_port_input(dp, &packet, &md);
1372         } else if (error != EAGAIN && error != EOPNOTSUPP) {
1373             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1374
1375             VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1376                         netdev_get_name(port->netdev), ovs_strerror(error));
1377         }
1378     }
1379     ofpbuf_uninit(&packet);
1380     ovs_mutex_unlock(&dp_netdev_mutex);
1381 }
1382
1383 static void
1384 dpif_netdev_wait(struct dpif *dpif)
1385 {
1386     struct dp_netdev_port *port;
1387
1388     /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
1389      * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
1390      * A makes it to poll_block().
1391      *
1392      * But I think it doesn't matter:
1393      *
1394      *     - In the dpif_port_add() case, A will not wake up when a packet
1395      *       arrives on the new port, but this would also happen if the
1396      *       ordering were reversed.
1397      *
1398      *     - In the dpif_port_remove() case, A might wake up spuriously, but
1399      *       that is harmless. */
1400
1401     ovs_mutex_lock(&dp_netdev_mutex);
1402     HMAP_FOR_EACH (port, node, &get_dp_netdev(dpif)->ports) {
1403         if (port->rx) {
1404             netdev_rx_wait(port->rx);
1405         }
1406     }
1407     ovs_mutex_unlock(&dp_netdev_mutex);
1408 }
1409
1410 static void
1411 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1412                       odp_port_t out_port)
1413 {
1414     struct dp_netdev_port *p = dp_netdev_lookup_port(dp, out_port);
1415     if (p) {
1416         netdev_send(p->netdev, packet);
1417     }
1418 }
1419
1420 static int
1421 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
1422                            int queue_no, const struct flow *flow,
1423                            const struct nlattr *userdata)
1424     OVS_EXCLUDED(dp->queue_mutex)
1425 {
1426     struct dp_netdev_queue *q = &dp->queues[queue_no];
1427     int error;
1428
1429     ovs_mutex_lock(&dp->queue_mutex);
1430     if (q->head - q->tail < MAX_QUEUE_LEN) {
1431         struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1432         struct dpif_upcall *upcall = &u->upcall;
1433         struct ofpbuf *buf = &u->buf;
1434         size_t buf_size;
1435
1436         upcall->type = queue_no;
1437
1438         /* Allocate buffer big enough for everything. */
1439         buf_size = ODPUTIL_FLOW_KEY_BYTES;
1440         if (userdata) {
1441             buf_size += NLA_ALIGN(userdata->nla_len);
1442         }
1443         ofpbuf_init(buf, buf_size);
1444
1445         /* Put ODP flow. */
1446         odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1447         upcall->key = buf->data;
1448         upcall->key_len = buf->size;
1449
1450         /* Put userdata. */
1451         if (userdata) {
1452             upcall->userdata = ofpbuf_put(buf, userdata,
1453                                           NLA_ALIGN(userdata->nla_len));
1454         }
1455
1456         /* Steal packet data. */
1457         ovs_assert(packet->source == OFPBUF_MALLOC);
1458         upcall->packet = *packet;
1459         ofpbuf_use(packet, NULL, 0);
1460
1461         seq_change(dp->queue_seq);
1462
1463         error = 0;
1464     } else {
1465         ovsthread_counter_inc(dp->n_lost, 1);
1466         error = ENOBUFS;
1467     }
1468     ovs_mutex_unlock(&dp->queue_mutex);
1469
1470     return error;
1471 }
1472
1473 struct dp_netdev_execute_aux {
1474     struct dp_netdev *dp;
1475     const struct flow *key;
1476 };
1477
1478 static void
1479 dp_execute_cb(void *aux_, struct ofpbuf *packet,
1480               const struct pkt_metadata *md OVS_UNUSED,
1481               const struct nlattr *a, bool may_steal)
1482 {
1483     struct dp_netdev_execute_aux *aux = aux_;
1484     int type = nl_attr_type(a);
1485
1486     switch ((enum ovs_action_attr)type) {
1487     case OVS_ACTION_ATTR_OUTPUT:
1488         dp_netdev_output_port(aux->dp, packet, u32_to_odp(nl_attr_get_u32(a)));
1489         break;
1490
1491     case OVS_ACTION_ATTR_USERSPACE: {
1492         const struct nlattr *userdata;
1493
1494         userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1495
1496         /* Make a copy if we are not allowed to steal the packet's data. */
1497         if (!may_steal) {
1498             packet = ofpbuf_clone_with_headroom(packet, DP_NETDEV_HEADROOM);
1499         }
1500         dp_netdev_output_userspace(aux->dp, packet, DPIF_UC_ACTION, aux->key,
1501                                    userdata);
1502         if (!may_steal) {
1503             ofpbuf_uninit(packet);
1504         }
1505         break;
1506     }
1507     case OVS_ACTION_ATTR_PUSH_VLAN:
1508     case OVS_ACTION_ATTR_POP_VLAN:
1509     case OVS_ACTION_ATTR_PUSH_MPLS:
1510     case OVS_ACTION_ATTR_POP_MPLS:
1511     case OVS_ACTION_ATTR_SET:
1512     case OVS_ACTION_ATTR_SAMPLE:
1513     case OVS_ACTION_ATTR_UNSPEC:
1514     case __OVS_ACTION_ATTR_MAX:
1515         OVS_NOT_REACHED();
1516     }
1517 }
1518
1519 static void
1520 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
1521                           struct ofpbuf *packet, struct pkt_metadata *md,
1522                           const struct nlattr *actions, size_t actions_len)
1523 {
1524     struct dp_netdev_execute_aux aux = {dp, key};
1525
1526     odp_execute_actions(&aux, packet, md, actions, actions_len, dp_execute_cb);
1527 }
1528
1529 const struct dpif_class dpif_netdev_class = {
1530     "netdev",
1531     dpif_netdev_enumerate,
1532     dpif_netdev_port_open_type,
1533     dpif_netdev_open,
1534     dpif_netdev_close,
1535     dpif_netdev_destroy,
1536     dpif_netdev_run,
1537     dpif_netdev_wait,
1538     dpif_netdev_get_stats,
1539     dpif_netdev_port_add,
1540     dpif_netdev_port_del,
1541     dpif_netdev_port_query_by_number,
1542     dpif_netdev_port_query_by_name,
1543     NULL,                       /* port_get_pid */
1544     dpif_netdev_port_dump_start,
1545     dpif_netdev_port_dump_next,
1546     dpif_netdev_port_dump_done,
1547     dpif_netdev_port_poll,
1548     dpif_netdev_port_poll_wait,
1549     dpif_netdev_flow_get,
1550     dpif_netdev_flow_put,
1551     dpif_netdev_flow_del,
1552     dpif_netdev_flow_flush,
1553     dpif_netdev_flow_dump_start,
1554     dpif_netdev_flow_dump_next,
1555     dpif_netdev_flow_dump_done,
1556     dpif_netdev_execute,
1557     NULL,                       /* operate */
1558     dpif_netdev_recv_set,
1559     dpif_netdev_queue_to_priority,
1560     dpif_netdev_recv,
1561     dpif_netdev_recv_wait,
1562     dpif_netdev_recv_purge,
1563 };
1564
1565 static void
1566 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1567                               const char *argv[], void *aux OVS_UNUSED)
1568 {
1569     struct dp_netdev_port *port;
1570     struct dp_netdev *dp;
1571     odp_port_t port_no;
1572
1573     dp = shash_find_data(&dp_netdevs, argv[1]);
1574     if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1575         unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1576         return;
1577     }
1578
1579     if (get_port_by_name(dp, argv[2], &port)) {
1580         unixctl_command_reply_error(conn, "unknown port");
1581         return;
1582     }
1583
1584     port_no = u32_to_odp(atoi(argv[3]));
1585     if (!port_no || port_no == ODPP_NONE) {
1586         unixctl_command_reply_error(conn, "bad port number");
1587         return;
1588     }
1589     if (dp_netdev_lookup_port(dp, port_no)) {
1590         unixctl_command_reply_error(conn, "port number already in use");
1591         return;
1592     }
1593     hmap_remove(&dp->ports, &port->node);
1594     port->port_no = port_no;
1595     hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
1596     seq_change(dp->port_seq);
1597     unixctl_command_reply(conn, NULL);
1598 }
1599
1600 static void
1601 dpif_dummy_register__(const char *type)
1602 {
1603     struct dpif_class *class;
1604
1605     class = xmalloc(sizeof *class);
1606     *class = dpif_netdev_class;
1607     class->type = xstrdup(type);
1608     dp_register_provider(class);
1609 }
1610
1611 void
1612 dpif_dummy_register(bool override)
1613 {
1614     if (override) {
1615         struct sset types;
1616         const char *type;
1617
1618         sset_init(&types);
1619         dp_enumerate_types(&types);
1620         SSET_FOR_EACH (type, &types) {
1621             if (!dp_unregister_provider(type)) {
1622                 dpif_dummy_register__(type);
1623             }
1624         }
1625         sset_destroy(&types);
1626     }
1627
1628     dpif_dummy_register__("dummy");
1629
1630     unixctl_command_register("dpif-dummy/change-port-number",
1631                              "DP PORT NEW-NUMBER",
1632                              3, 3, dpif_dummy_change_port_number, NULL);
1633 }