sFlow: Genericize/simplify kernel sFlow implementation
[sliver-openvswitch.git] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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
19 #include "dpif-linux.h"
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <net/if.h>
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "bitmap.h"
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
38 #include "flow.h"
39 #include "netdev.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
44 #include "netlink.h"
45 #include "odp-util.h"
46 #include "ofpbuf.h"
47 #include "openvswitch/datapath-compat.h"
48 #include "openvswitch/tunnel.h"
49 #include "packets.h"
50 #include "poll-loop.h"
51 #include "random.h"
52 #include "shash.h"
53 #include "sset.h"
54 #include "unaligned.h"
55 #include "util.h"
56 #include "vlog.h"
57
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
59
60 enum { LRU_MAX_PORTS = 1024 };
61 enum { LRU_MASK = LRU_MAX_PORTS - 1};
62 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
63
64 enum { N_UPCALL_SOCKS = 16 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
66
67 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
68  * missing if we have old headers. */
69 #define ETH_FLAG_LRO      (1 << 15)    /* LRO is enabled */
70
71 struct dpif_linux_dp {
72     /* Generic Netlink header. */
73     uint8_t cmd;
74
75     /* struct ovs_header. */
76     int dp_ifindex;
77
78     /* Attributes. */
79     const char *name;                  /* OVS_DP_ATTR_NAME. */
80     uint32_t upcall_pid;               /* OVS_DP_UPCALL_PID. */
81     struct ovs_dp_stats stats;         /* OVS_DP_ATTR_STATS. */
82     enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
83 };
84
85 static void dpif_linux_dp_init(struct dpif_linux_dp *);
86 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
87                                      const struct ofpbuf *);
88 static void dpif_linux_dp_dump_start(struct nl_dump *);
89 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
90                                   struct dpif_linux_dp *reply,
91                                   struct ofpbuf **bufp);
92 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
93                              struct ofpbuf **bufp);
94
95 struct dpif_linux_flow {
96     /* Generic Netlink header. */
97     uint8_t cmd;
98
99     /* struct ovs_header. */
100     unsigned int nlmsg_flags;
101     int dp_ifindex;
102
103     /* Attributes.
104      *
105      * The 'stats' and 'used' members point to 64-bit data that might only be
106      * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
107      * access their values.
108      *
109      * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
110      * the Netlink version of the command, even if actions_len is zero. */
111     const struct nlattr *key;           /* OVS_FLOW_ATTR_KEY. */
112     size_t key_len;
113     const struct nlattr *actions;       /* OVS_FLOW_ATTR_ACTIONS. */
114     size_t actions_len;
115     uint32_t upcall_pid;                /* OVS_FLOW_ATTR_UPCALL_PID. */
116     const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
117     const uint8_t *tcp_flags;           /* OVS_FLOW_ATTR_TCP_FLAGS. */
118     const uint64_t *used;               /* OVS_FLOW_ATTR_USED. */
119     bool clear;                         /* OVS_FLOW_ATTR_CLEAR. */
120 };
121
122 static void dpif_linux_flow_init(struct dpif_linux_flow *);
123 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
124                                        const struct ofpbuf *);
125 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
126                                       struct ofpbuf *);
127 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
128                                     struct dpif_linux_flow *reply,
129                                     struct ofpbuf **bufp);
130 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
131                                       struct dpif_flow_stats *);
132
133 /* Datapath interface for the openvswitch Linux kernel module. */
134 struct dpif_linux {
135     struct dpif dpif;
136     int dp_ifindex;
137
138     /* Upcall messages. */
139     struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
140     int last_read_upcall;
141     unsigned int listen_mask;
142
143     /* Change notification. */
144     struct sset changed_ports;  /* Ports that have changed. */
145     struct nln_notifier *port_notifier;
146     bool change_error;
147
148     /* Queue of unused ports. */
149     unsigned long *lru_bitmap;
150     uint16_t lru_ports[LRU_MAX_PORTS];
151     size_t lru_head;
152     size_t lru_tail;
153 };
154
155 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156
157 /* Generic Netlink family numbers for OVS. */
158 static int ovs_datapath_family;
159 static int ovs_vport_family;
160 static int ovs_flow_family;
161 static int ovs_packet_family;
162
163 /* Generic Netlink socket. */
164 static struct nl_sock *genl_sock;
165 static struct nln *nln = NULL;
166
167 static int dpif_linux_init(void);
168 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
169 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
170 static void dpif_linux_port_changed(const void *vport, void *dpif);
171 static uint32_t get_upcall_pid_port(struct dpif_linux *, uint32_t port);
172 static uint32_t get_upcall_pid_flow(struct dpif_linux *,
173                                     const struct nlattr *key, size_t key_len);
174
175 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176                                        struct ofpbuf *);
177 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
178                                         const struct ofpbuf *);
179
180 static struct dpif_linux *
181 dpif_linux_cast(const struct dpif *dpif)
182 {
183     dpif_assert_class(dpif, &dpif_linux_class);
184     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
185 }
186
187 static void
188 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
189 {
190     if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
191         bitmap_set1(dp->lru_bitmap, port);
192         dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
193     }
194 }
195
196 static uint32_t
197 dpif_linux_pop_port(struct dpif_linux *dp)
198 {
199     uint16_t port;
200
201     if (dp->lru_head == dp->lru_tail) {
202         return UINT32_MAX;
203     }
204
205     port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
206     bitmap_set0(dp->lru_bitmap, port);
207     return port;
208 }
209
210 static int
211 dpif_linux_enumerate(struct sset *all_dps)
212 {
213     struct nl_dump dump;
214     struct ofpbuf msg;
215     int error;
216
217     error = dpif_linux_init();
218     if (error) {
219         return error;
220     }
221
222     dpif_linux_dp_dump_start(&dump);
223     while (nl_dump_next(&dump, &msg)) {
224         struct dpif_linux_dp dp;
225
226         if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
227             sset_add(all_dps, dp.name);
228         }
229     }
230     return nl_dump_done(&dump);
231 }
232
233 static int
234 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
235                 bool create, struct dpif **dpifp)
236 {
237     struct dpif_linux_dp dp_request, dp;
238     struct ofpbuf *buf;
239     int error;
240
241     error = dpif_linux_init();
242     if (error) {
243         return error;
244     }
245
246     /* Create or look up datapath. */
247     dpif_linux_dp_init(&dp_request);
248     dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
249     dp_request.name = name;
250     error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
251     if (error) {
252         return error;
253     }
254
255     open_dpif(&dp, dpifp);
256     ofpbuf_delete(buf);
257     return 0;
258 }
259
260 static void
261 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
262 {
263     struct dpif_linux *dpif;
264     int i;
265
266     dpif = xzalloc(sizeof *dpif);
267     dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
268                                               dpif);
269
270     dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
271               dp->dp_ifindex, dp->dp_ifindex);
272
273     dpif->dp_ifindex = dp->dp_ifindex;
274     sset_init(&dpif->changed_ports);
275     *dpifp = &dpif->dpif;
276
277     dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
278     bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
279     for (i = 1; i < LRU_MAX_PORTS; i++) {
280         dpif_linux_push_port(dpif, i);
281     }
282 }
283
284 static void
285 destroy_upcall_socks(struct dpif_linux *dpif)
286 {
287     int i;
288
289     for (i = 0; i < N_UPCALL_SOCKS; i++) {
290         nl_sock_destroy(dpif->upcall_socks[i]);
291         dpif->upcall_socks[i] = NULL;
292     }
293 }
294
295 static void
296 dpif_linux_close(struct dpif *dpif_)
297 {
298     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
299
300     nln_notifier_destroy(dpif->port_notifier);
301     destroy_upcall_socks(dpif);
302     sset_destroy(&dpif->changed_ports);
303     free(dpif->lru_bitmap);
304     free(dpif);
305 }
306
307 static int
308 dpif_linux_destroy(struct dpif *dpif_)
309 {
310     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
311     struct dpif_linux_dp dp;
312
313     dpif_linux_dp_init(&dp);
314     dp.cmd = OVS_DP_CMD_DEL;
315     dp.dp_ifindex = dpif->dp_ifindex;
316     return dpif_linux_dp_transact(&dp, NULL, NULL);
317 }
318
319 static void
320 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
321 {
322     if (nln) {
323         nln_run(nln);
324     }
325 }
326
327 static void
328 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
329 {
330     if (nln) {
331         nln_wait(nln);
332     }
333 }
334
335 static int
336 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
337 {
338     struct dpif_linux_dp dp;
339     struct ofpbuf *buf;
340     int error;
341
342     error = dpif_linux_dp_get(dpif_, &dp, &buf);
343     if (!error) {
344         *stats = dp.stats;
345         ofpbuf_delete(buf);
346     }
347     return error;
348 }
349
350 static int
351 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
352 {
353     struct dpif_linux_dp dp;
354     struct ofpbuf *buf;
355     int error;
356
357     error = dpif_linux_dp_get(dpif_, &dp, &buf);
358     if (!error) {
359         *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
360         ofpbuf_delete(buf);
361     }
362     return error;
363 }
364
365 static int
366 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
367 {
368     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
369     struct dpif_linux_dp dp;
370
371     dpif_linux_dp_init(&dp);
372     dp.cmd = OVS_DP_CMD_SET;
373     dp.dp_ifindex = dpif->dp_ifindex;
374     dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
375     return dpif_linux_dp_transact(&dp, NULL, NULL);
376 }
377
378 static int
379 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
380                     uint16_t *port_nop)
381 {
382     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
383     const char *name = netdev_get_name(netdev);
384     const char *type = netdev_get_type(netdev);
385     struct dpif_linux_vport request, reply;
386     const struct ofpbuf *options;
387     struct ofpbuf *buf;
388     int error;
389
390     dpif_linux_vport_init(&request);
391     request.cmd = OVS_VPORT_CMD_NEW;
392     request.dp_ifindex = dpif->dp_ifindex;
393     request.type = netdev_vport_get_vport_type(netdev);
394     if (request.type == OVS_VPORT_TYPE_UNSPEC) {
395         VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
396                      "unsupported type `%s'",
397                      dpif_name(dpif_), name, type);
398         return EINVAL;
399     }
400     request.name = name;
401
402     options = netdev_vport_get_options(netdev);
403     if (options && options->size) {
404         request.options = options->data;
405         request.options_len = options->size;
406     }
407
408     if (request.type == OVS_VPORT_TYPE_NETDEV) {
409         netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
410     }
411
412     /* Loop until we find a port that isn't used. */
413     do {
414         request.port_no = dpif_linux_pop_port(dpif);
415         request.upcall_pid = get_upcall_pid_port(dpif, request.port_no);
416         error = dpif_linux_vport_transact(&request, &reply, &buf);
417
418         if (!error) {
419             *port_nop = reply.port_no;
420             VLOG_DBG("%s: assigning port %"PRIu32" to netlink "
421                      "pid %"PRIu32,
422                      dpif_name(dpif_), request.port_no,
423                      request.upcall_pid);
424         }
425         ofpbuf_delete(buf);
426     } while (request.port_no != UINT32_MAX
427              && (error == EBUSY || error == EFBIG));
428
429     return error;
430 }
431
432 static int
433 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
434 {
435     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
436     struct dpif_linux_vport vport;
437     int error;
438
439     dpif_linux_vport_init(&vport);
440     vport.cmd = OVS_VPORT_CMD_DEL;
441     vport.dp_ifindex = dpif->dp_ifindex;
442     vport.port_no = port_no;
443     error = dpif_linux_vport_transact(&vport, NULL, NULL);
444
445     if (!error) {
446         dpif_linux_push_port(dpif, port_no);
447     }
448     return error;
449 }
450
451 static int
452 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
453                         const char *port_name, struct dpif_port *dpif_port)
454 {
455     struct dpif_linux_vport request;
456     struct dpif_linux_vport reply;
457     struct ofpbuf *buf;
458     int error;
459
460     dpif_linux_vport_init(&request);
461     request.cmd = OVS_VPORT_CMD_GET;
462     request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
463     request.port_no = port_no;
464     request.name = port_name;
465
466     error = dpif_linux_vport_transact(&request, &reply, &buf);
467     if (!error) {
468         dpif_port->name = xstrdup(reply.name);
469         dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
470         dpif_port->port_no = reply.port_no;
471         ofpbuf_delete(buf);
472     }
473     return error;
474 }
475
476 static int
477 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
478                                 struct dpif_port *dpif_port)
479 {
480     return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
481 }
482
483 static int
484 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
485                               struct dpif_port *dpif_port)
486 {
487     return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
488 }
489
490 static int
491 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
492 {
493     /* If the datapath increases its range of supported ports, then it should
494      * start reporting that. */
495     return 1024;
496 }
497
498 static int
499 dpif_linux_flow_flush(struct dpif *dpif_)
500 {
501     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
502     struct dpif_linux_flow flow;
503
504     dpif_linux_flow_init(&flow);
505     flow.cmd = OVS_FLOW_CMD_DEL;
506     flow.dp_ifindex = dpif->dp_ifindex;
507     return dpif_linux_flow_transact(&flow, NULL, NULL);
508 }
509
510 struct dpif_linux_port_state {
511     struct nl_dump dump;
512     unsigned long *port_bitmap; /* Ports in the datapath. */
513     bool complete;              /* Dump completed without error. */
514 };
515
516 static int
517 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
518 {
519     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
520     struct dpif_linux_port_state *state;
521     struct dpif_linux_vport request;
522     struct ofpbuf *buf;
523
524     *statep = state = xmalloc(sizeof *state);
525     state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
526     state->complete = false;
527
528     dpif_linux_vport_init(&request);
529     request.cmd = OVS_DP_CMD_GET;
530     request.dp_ifindex = dpif->dp_ifindex;
531
532     buf = ofpbuf_new(1024);
533     dpif_linux_vport_to_ofpbuf(&request, buf);
534     nl_dump_start(&state->dump, genl_sock, buf);
535     ofpbuf_delete(buf);
536
537     return 0;
538 }
539
540 static int
541 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
542                           struct dpif_port *dpif_port)
543 {
544     struct dpif_linux_port_state *state = state_;
545     struct dpif_linux_vport vport;
546     struct ofpbuf buf;
547     int error;
548
549     if (!nl_dump_next(&state->dump, &buf)) {
550         state->complete = true;
551         return EOF;
552     }
553
554     error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
555     if (error) {
556         return error;
557     }
558
559     if (vport.port_no < LRU_MAX_PORTS) {
560         bitmap_set1(state->port_bitmap, vport.port_no);
561     }
562
563     dpif_port->name = (char *) vport.name;
564     dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
565     dpif_port->port_no = vport.port_no;
566     return 0;
567 }
568
569 static int
570 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
571 {
572     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
573     struct dpif_linux_port_state *state = state_;
574     int error = nl_dump_done(&state->dump);
575
576     if (state->complete) {
577         uint16_t i;
578
579         for (i = 0; i < LRU_MAX_PORTS; i++) {
580             if (!bitmap_is_set(state->port_bitmap, i)) {
581                 dpif_linux_push_port(dpif, i);
582             }
583         }
584     }
585
586     free(state->port_bitmap);
587     free(state);
588     return error;
589 }
590
591 static int
592 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
593 {
594     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
595
596     if (dpif->change_error) {
597         dpif->change_error = false;
598         sset_clear(&dpif->changed_ports);
599         return ENOBUFS;
600     } else if (!sset_is_empty(&dpif->changed_ports)) {
601         *devnamep = sset_pop(&dpif->changed_ports);
602         return 0;
603     } else {
604         return EAGAIN;
605     }
606 }
607
608 static void
609 dpif_linux_port_poll_wait(const struct dpif *dpif_)
610 {
611     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
612     if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
613         poll_immediate_wake();
614     }
615 }
616
617 static int
618 dpif_linux_flow_get__(const struct dpif *dpif_,
619                       const struct nlattr *key, size_t key_len,
620                       struct dpif_linux_flow *reply, struct ofpbuf **bufp)
621 {
622     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
623     struct dpif_linux_flow request;
624
625     dpif_linux_flow_init(&request);
626     request.cmd = OVS_FLOW_CMD_GET;
627     request.dp_ifindex = dpif->dp_ifindex;
628     request.key = key;
629     request.key_len = key_len;
630     return dpif_linux_flow_transact(&request, reply, bufp);
631 }
632
633 static int
634 dpif_linux_flow_get(const struct dpif *dpif_,
635                     const struct nlattr *key, size_t key_len,
636                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
637 {
638     struct dpif_linux_flow reply;
639     struct ofpbuf *buf;
640     int error;
641
642     error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
643     if (!error) {
644         if (stats) {
645             dpif_linux_flow_get_stats(&reply, stats);
646         }
647         if (actionsp) {
648             buf->data = (void *) reply.actions;
649             buf->size = reply.actions_len;
650             *actionsp = buf;
651         } else {
652             ofpbuf_delete(buf);
653         }
654     }
655     return error;
656 }
657
658 static int
659 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
660                     const struct nlattr *key, size_t key_len,
661                     const struct nlattr *actions, size_t actions_len,
662                     struct dpif_flow_stats *stats)
663 {
664     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
665     struct dpif_linux_flow request, reply;
666     struct nlattr dummy_action;
667     struct ofpbuf *buf;
668     int error;
669
670     dpif_linux_flow_init(&request);
671     request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
672     request.dp_ifindex = dpif->dp_ifindex;
673     request.key = key;
674     request.key_len = key_len;
675     /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
676     request.actions = actions ? actions : &dummy_action;
677     request.actions_len = actions_len;
678     request.upcall_pid = get_upcall_pid_flow(dpif, key, key_len);
679     if (flags & DPIF_FP_ZERO_STATS) {
680         request.clear = true;
681     }
682     request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
683     error = dpif_linux_flow_transact(&request,
684                                      stats ? &reply : NULL,
685                                      stats ? &buf : NULL);
686     if (!error && stats) {
687         dpif_linux_flow_get_stats(&reply, stats);
688         ofpbuf_delete(buf);
689     }
690     return error;
691 }
692
693 static int
694 dpif_linux_flow_del(struct dpif *dpif_,
695                     const struct nlattr *key, size_t key_len,
696                     struct dpif_flow_stats *stats)
697 {
698     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
699     struct dpif_linux_flow request, reply;
700     struct ofpbuf *buf;
701     int error;
702
703     dpif_linux_flow_init(&request);
704     request.cmd = OVS_FLOW_CMD_DEL;
705     request.dp_ifindex = dpif->dp_ifindex;
706     request.key = key;
707     request.key_len = key_len;
708     error = dpif_linux_flow_transact(&request,
709                                      stats ? &reply : NULL,
710                                      stats ? &buf : NULL);
711     if (!error && stats) {
712         dpif_linux_flow_get_stats(&reply, stats);
713         ofpbuf_delete(buf);
714     }
715     return error;
716 }
717
718 struct dpif_linux_flow_state {
719     struct nl_dump dump;
720     struct dpif_linux_flow flow;
721     struct dpif_flow_stats stats;
722     struct ofpbuf *buf;
723 };
724
725 static int
726 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
727 {
728     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
729     struct dpif_linux_flow_state *state;
730     struct dpif_linux_flow request;
731     struct ofpbuf *buf;
732
733     *statep = state = xmalloc(sizeof *state);
734
735     dpif_linux_flow_init(&request);
736     request.cmd = OVS_DP_CMD_GET;
737     request.dp_ifindex = dpif->dp_ifindex;
738
739     buf = ofpbuf_new(1024);
740     dpif_linux_flow_to_ofpbuf(&request, buf);
741     nl_dump_start(&state->dump, genl_sock, buf);
742     ofpbuf_delete(buf);
743
744     state->buf = NULL;
745
746     return 0;
747 }
748
749 static int
750 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
751                           const struct nlattr **key, size_t *key_len,
752                           const struct nlattr **actions, size_t *actions_len,
753                           const struct dpif_flow_stats **stats)
754 {
755     struct dpif_linux_flow_state *state = state_;
756     struct ofpbuf buf;
757     int error;
758
759     do {
760         ofpbuf_delete(state->buf);
761         state->buf = NULL;
762
763         if (!nl_dump_next(&state->dump, &buf)) {
764             return EOF;
765         }
766
767         error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
768         if (error) {
769             return error;
770         }
771
772         if (actions && !state->flow.actions) {
773             error = dpif_linux_flow_get__(dpif_, state->flow.key,
774                                           state->flow.key_len,
775                                           &state->flow, &state->buf);
776             if (error == ENOENT) {
777                 VLOG_DBG("dumped flow disappeared on get");
778             } else if (error) {
779                 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
780             }
781         }
782     } while (error);
783
784     if (actions) {
785         *actions = state->flow.actions;
786         *actions_len = state->flow.actions_len;
787     }
788     if (key) {
789         *key = state->flow.key;
790         *key_len = state->flow.key_len;
791     }
792     if (stats) {
793         dpif_linux_flow_get_stats(&state->flow, &state->stats);
794         *stats = &state->stats;
795     }
796     return error;
797 }
798
799 static int
800 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
801 {
802     struct dpif_linux_flow_state *state = state_;
803     int error = nl_dump_done(&state->dump);
804     ofpbuf_delete(state->buf);
805     free(state);
806     return error;
807 }
808
809 static int
810 dpif_linux_execute__(int dp_ifindex, uint32_t upcall_pid,
811                      const struct nlattr *key, size_t key_len,
812                      const struct nlattr *actions, size_t actions_len,
813                      const struct ofpbuf *packet)
814 {
815     struct ovs_header *execute;
816     struct ofpbuf *buf;
817     int error;
818
819     buf = ofpbuf_new(128 + actions_len + packet->size);
820
821     nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
822                           OVS_PACKET_CMD_EXECUTE, 1);
823
824     execute = ofpbuf_put_uninit(buf, sizeof *execute);
825     execute->dp_ifindex = dp_ifindex;
826
827     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
828     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
829     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
830     nl_msg_put_u32(buf, OVS_PACKET_ATTR_UPCALL_PID, upcall_pid);
831
832     error = nl_sock_transact(genl_sock, buf, NULL);
833     ofpbuf_delete(buf);
834     return error;
835 }
836
837 static int
838 dpif_linux_execute(struct dpif *dpif_,
839                    const struct nlattr *key, size_t key_len,
840                    const struct nlattr *actions, size_t actions_len,
841                    const struct ofpbuf *packet)
842 {
843     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
844     uint32_t upcall_pid = get_upcall_pid_flow(dpif, key, key_len);
845
846     return dpif_linux_execute__(dpif->dp_ifindex, upcall_pid, key, key_len,
847                                 actions, actions_len, packet);
848 }
849
850 static int
851 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
852 {
853     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
854     *listen_mask = dpif->listen_mask;
855     return 0;
856 }
857
858 static uint32_t
859 get_upcall_pid_port__(struct dpif_linux *dpif, uint32_t port)
860 {
861     int idx = port & (N_UPCALL_SOCKS - 1);
862     return nl_sock_pid(dpif->upcall_socks[idx]);
863 }
864
865 static uint32_t
866 get_upcall_pid_port(struct dpif_linux *dpif, uint32_t port)
867 {
868     if (!(dpif->listen_mask & (1u << DPIF_UC_MISS))) {
869         return 0;
870     }
871
872     return get_upcall_pid_port__(dpif, port);
873 }
874
875 static uint32_t
876 get_upcall_pid_flow(struct dpif_linux *dpif,
877                     const struct nlattr *key, size_t key_len)
878 {
879     const struct nlattr *nla;
880     uint32_t port;
881
882     if (!(dpif->listen_mask & (1u << DPIF_UC_ACTION))) {
883         return 0;
884     }
885
886     nla = nl_attr_find__(key, key_len, OVS_KEY_ATTR_IN_PORT);
887     if (nla) {
888         port = nl_attr_get_u32(nla);
889     } else {
890         port = random_uint32();
891     }
892
893     return get_upcall_pid_port__(dpif, port);
894 }
895
896 static void
897 set_upcall_pids(struct dpif_linux *dpif)
898 {
899     struct dpif_port port;
900     struct dpif_port_dump port_dump;
901     struct dpif_flow_dump flow_dump;
902     const struct nlattr *key;
903     size_t key_len;
904     int error;
905
906     DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
907         struct dpif_linux_vport vport_request;
908
909         dpif_linux_vport_init(&vport_request);
910         vport_request.cmd = OVS_VPORT_CMD_SET;
911         vport_request.dp_ifindex = dpif->dp_ifindex;
912         vport_request.port_no = port.port_no;
913         vport_request.upcall_pid = get_upcall_pid_port(dpif,
914                                                        vport_request.port_no);
915         error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
916         if (!error) {
917             VLOG_DBG("%s: assigning port %"PRIu32" to netlink "
918                      "pid %"PRIu32,
919                      dpif_name(&dpif->dpif), vport_request.port_no,
920                      vport_request.upcall_pid);
921         } else {
922             VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
923                          dpif_name(&dpif->dpif), strerror(error));
924         }
925     }
926
927     dpif_flow_dump_start(&flow_dump, &dpif->dpif);
928     while (dpif_flow_dump_next(&flow_dump, &key, &key_len,
929                                NULL, NULL, NULL)) {
930         struct dpif_linux_flow flow_request;
931
932         dpif_linux_flow_init(&flow_request);
933         flow_request.cmd = OVS_FLOW_CMD_SET;
934         flow_request.dp_ifindex = dpif->dp_ifindex;
935         flow_request.key = key;
936         flow_request.key_len = key_len;
937         flow_request.upcall_pid = get_upcall_pid_flow(dpif, key, key_len);
938         error = dpif_linux_flow_transact(&flow_request, NULL, NULL);
939         if (error) {
940             VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on flow: %s",
941                          dpif_name(&dpif->dpif), strerror(error));
942         }
943     }
944     dpif_flow_dump_done(&flow_dump);
945 }
946
947 static int
948 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
949 {
950     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
951
952     if (listen_mask == dpif->listen_mask) {
953         return 0;
954     }
955
956     if (!listen_mask) {
957         destroy_upcall_socks(dpif);
958     } else if (!dpif->listen_mask) {
959         int i;
960         int error;
961
962         for (i = 0; i < N_UPCALL_SOCKS; i++) {
963             error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
964             if (error) {
965                 destroy_upcall_socks(dpif);
966                 return error;
967             }
968         }
969     }
970
971     dpif->listen_mask = listen_mask;
972     set_upcall_pids(dpif);
973
974     return 0;
975 }
976
977 static int
978 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
979                              uint32_t queue_id, uint32_t *priority)
980 {
981     if (queue_id < 0xf000) {
982         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
983         return 0;
984     } else {
985         return EINVAL;
986     }
987 }
988
989 static int
990 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
991                  int *dp_ifindex)
992 {
993     static const struct nl_policy ovs_packet_policy[] = {
994         /* Always present. */
995         [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
996                                      .min_len = ETH_HEADER_LEN },
997         [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
998
999         /* OVS_PACKET_CMD_ACTION only. */
1000         [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1001     };
1002
1003     struct ovs_header *ovs_header;
1004     struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1005     struct nlmsghdr *nlmsg;
1006     struct genlmsghdr *genl;
1007     struct ofpbuf b;
1008     int type;
1009
1010     ofpbuf_use_const(&b, buf->data, buf->size);
1011
1012     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1013     genl = ofpbuf_try_pull(&b, sizeof *genl);
1014     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1015     if (!nlmsg || !genl || !ovs_header
1016         || nlmsg->nlmsg_type != ovs_packet_family
1017         || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1018                             ARRAY_SIZE(ovs_packet_policy))) {
1019         return EINVAL;
1020     }
1021
1022     type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1023             : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1024             : -1);
1025     if (type < 0) {
1026         return EINVAL;
1027     }
1028
1029     memset(upcall, 0, sizeof *upcall);
1030     upcall->type = type;
1031     upcall->packet = buf;
1032     upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1033     upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1034     upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1035     upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1036     upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1037                         ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1038                         : 0);
1039     *dp_ifindex = ovs_header->dp_ifindex;
1040
1041     return 0;
1042 }
1043
1044 static int
1045 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1046 {
1047     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1048     int i;
1049     int read_tries = 0;
1050
1051     if (!dpif->listen_mask) {
1052        return EAGAIN;
1053     }
1054
1055     for (i = 0; i < N_UPCALL_SOCKS; i++) {
1056         struct nl_sock *upcall_sock;
1057         dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1058                                  (N_UPCALL_SOCKS - 1);
1059         upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1060
1061         if (nl_sock_woke(upcall_sock)) {
1062             int dp_ifindex;
1063
1064             for (;;) {
1065                 struct ofpbuf *buf;
1066                 int error;
1067
1068                 if (++read_tries > 50) {
1069                     return EAGAIN;
1070                 }
1071
1072                 error = nl_sock_recv(upcall_sock, &buf, false);
1073                 if (error == EAGAIN) {
1074                     break;
1075                 } else if (error) {
1076                     return error;
1077                 }
1078
1079                 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1080                 if (!error
1081                     && dp_ifindex == dpif->dp_ifindex
1082                     && dpif->listen_mask & (1u << upcall->type)) {
1083                     return 0;
1084                 }
1085
1086                 ofpbuf_delete(buf);
1087                 if (error) {
1088                     return error;
1089                 }
1090             }
1091         }
1092     }
1093
1094     return EAGAIN;
1095 }
1096
1097 static void
1098 dpif_linux_recv_wait(struct dpif *dpif_)
1099 {
1100     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1101     int i;
1102
1103     if (!dpif->listen_mask) {
1104        return;
1105     }
1106
1107     for (i = 0; i < N_UPCALL_SOCKS; i++) {
1108         nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1109     }
1110 }
1111
1112 static void
1113 dpif_linux_recv_purge(struct dpif *dpif_)
1114 {
1115     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1116     int i;
1117
1118     if (!dpif->listen_mask) {
1119        return;
1120     }
1121
1122     for (i = 0; i < N_UPCALL_SOCKS; i++) {
1123         nl_sock_drain(dpif->upcall_socks[i]);
1124     }
1125 }
1126
1127 const struct dpif_class dpif_linux_class = {
1128     "system",
1129     dpif_linux_enumerate,
1130     dpif_linux_open,
1131     dpif_linux_close,
1132     dpif_linux_destroy,
1133     dpif_linux_run,
1134     dpif_linux_wait,
1135     dpif_linux_get_stats,
1136     dpif_linux_get_drop_frags,
1137     dpif_linux_set_drop_frags,
1138     dpif_linux_port_add,
1139     dpif_linux_port_del,
1140     dpif_linux_port_query_by_number,
1141     dpif_linux_port_query_by_name,
1142     dpif_linux_get_max_ports,
1143     dpif_linux_port_dump_start,
1144     dpif_linux_port_dump_next,
1145     dpif_linux_port_dump_done,
1146     dpif_linux_port_poll,
1147     dpif_linux_port_poll_wait,
1148     dpif_linux_flow_get,
1149     dpif_linux_flow_put,
1150     dpif_linux_flow_del,
1151     dpif_linux_flow_flush,
1152     dpif_linux_flow_dump_start,
1153     dpif_linux_flow_dump_next,
1154     dpif_linux_flow_dump_done,
1155     dpif_linux_execute,
1156     dpif_linux_recv_get_mask,
1157     dpif_linux_recv_set_mask,
1158     dpif_linux_queue_to_priority,
1159     dpif_linux_recv,
1160     dpif_linux_recv_wait,
1161     dpif_linux_recv_purge,
1162 };
1163 \f
1164 static int
1165 dpif_linux_init(void)
1166 {
1167     static int error = -1;
1168
1169     if (error < 0) {
1170         unsigned int ovs_vport_mcgroup;
1171
1172         error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1173                                       &ovs_datapath_family);
1174         if (error) {
1175             VLOG_ERR("Generic Netlink family '%s' does not exist. "
1176                      "The Open vSwitch kernel module is probably not loaded.",
1177                      OVS_DATAPATH_FAMILY);
1178         }
1179         if (!error) {
1180             error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1181         }
1182         if (!error) {
1183             error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1184         }
1185         if (!error) {
1186             error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1187                                           &ovs_packet_family);
1188         }
1189         if (!error) {
1190             error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1191         }
1192         if (!error) {
1193             error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1194                                            &ovs_vport_mcgroup,
1195                                            OVS_VPORT_MCGROUP_FALLBACK_ID);
1196         }
1197         if (!error) {
1198             static struct dpif_linux_vport vport;
1199             nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1200                              dpif_linux_nln_parse, &vport);
1201         }
1202     }
1203
1204     return error;
1205 }
1206
1207 bool
1208 dpif_linux_is_internal_device(const char *name)
1209 {
1210     struct dpif_linux_vport reply;
1211     struct ofpbuf *buf;
1212     int error;
1213
1214     error = dpif_linux_vport_get(name, &reply, &buf);
1215     if (!error) {
1216         ofpbuf_delete(buf);
1217     } else if (error != ENODEV && error != ENOENT) {
1218         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1219                      name, strerror(error));
1220     }
1221
1222     return reply.type == OVS_VPORT_TYPE_INTERNAL;
1223 }
1224
1225 int
1226 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1227                       const void *data, size_t size)
1228 {
1229     struct ofpbuf actions, key, packet;
1230     struct odputil_keybuf keybuf;
1231     struct flow flow;
1232     uint64_t action;
1233
1234     ofpbuf_use_const(&packet, data, size);
1235     flow_extract(&packet, htonll(0), 0, &flow);
1236
1237     ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1238     odp_flow_key_from_flow(&key, &flow);
1239
1240     ofpbuf_use_stack(&actions, &action, sizeof action);
1241     nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1242
1243     return dpif_linux_execute__(dp_ifindex, 0, key.data, key.size,
1244                                 actions.data, actions.size, &packet);
1245 }
1246
1247 static bool
1248 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1249 {
1250     struct dpif_linux_vport *vport = vport_;
1251     return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1252 }
1253
1254 static void
1255 dpif_linux_port_changed(const void *vport_, void *dpif_)
1256 {
1257     const struct dpif_linux_vport *vport = vport_;
1258     struct dpif_linux *dpif = dpif_;
1259
1260     if (vport) {
1261         if (vport->dp_ifindex == dpif->dp_ifindex
1262             && (vport->cmd == OVS_VPORT_CMD_NEW
1263                 || vport->cmd == OVS_VPORT_CMD_DEL
1264                 || vport->cmd == OVS_VPORT_CMD_SET)) {
1265             VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1266                      dpif->dpif.full_name, vport->name, vport->cmd);
1267             sset_add(&dpif->changed_ports, vport->name);
1268         }
1269     } else {
1270         dpif->change_error = true;
1271     }
1272 }
1273 \f
1274 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1275  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1276  * positive errno value.
1277  *
1278  * 'vport' will contain pointers into 'buf', so the caller should not free
1279  * 'buf' while 'vport' is still in use. */
1280 static int
1281 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1282                              const struct ofpbuf *buf)
1283 {
1284     static const struct nl_policy ovs_vport_policy[] = {
1285         [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1286         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1287         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1288         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1289         [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1290                                    .min_len = sizeof(struct ovs_vport_stats),
1291                                    .max_len = sizeof(struct ovs_vport_stats),
1292                                    .optional = true },
1293         [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1294                                      .min_len = ETH_ADDR_LEN,
1295                                      .max_len = ETH_ADDR_LEN,
1296                                      .optional = true },
1297         [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1298         [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1299     };
1300
1301     struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1302     struct ovs_header *ovs_header;
1303     struct nlmsghdr *nlmsg;
1304     struct genlmsghdr *genl;
1305     struct ofpbuf b;
1306
1307     dpif_linux_vport_init(vport);
1308
1309     ofpbuf_use_const(&b, buf->data, buf->size);
1310     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1311     genl = ofpbuf_try_pull(&b, sizeof *genl);
1312     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1313     if (!nlmsg || !genl || !ovs_header
1314         || nlmsg->nlmsg_type != ovs_vport_family
1315         || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1316                             ARRAY_SIZE(ovs_vport_policy))) {
1317         return EINVAL;
1318     }
1319
1320     vport->cmd = genl->cmd;
1321     vport->dp_ifindex = ovs_header->dp_ifindex;
1322     vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1323     vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1324     vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1325     if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1326         vport->upcall_pid = nl_attr_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1327     }
1328     if (a[OVS_VPORT_ATTR_STATS]) {
1329         vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1330     }
1331     if (a[OVS_VPORT_ATTR_ADDRESS]) {
1332         vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1333     }
1334     if (a[OVS_VPORT_ATTR_OPTIONS]) {
1335         vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1336         vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1337     }
1338     if (a[OVS_VPORT_ATTR_IFINDEX]) {
1339         vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1340     }
1341     return 0;
1342 }
1343
1344 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1345  * followed by Netlink attributes corresponding to 'vport'. */
1346 static void
1347 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1348                            struct ofpbuf *buf)
1349 {
1350     struct ovs_header *ovs_header;
1351
1352     nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1353                           vport->cmd, 1);
1354
1355     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1356     ovs_header->dp_ifindex = vport->dp_ifindex;
1357
1358     if (vport->port_no != UINT32_MAX) {
1359         nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1360     }
1361
1362     if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1363         nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1364     }
1365
1366     if (vport->name) {
1367         nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1368     }
1369
1370     nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1371
1372     if (vport->stats) {
1373         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1374                           vport->stats, sizeof *vport->stats);
1375     }
1376
1377     if (vport->address) {
1378         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1379                           vport->address, ETH_ADDR_LEN);
1380     }
1381
1382     if (vport->options) {
1383         nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1384                           vport->options, vport->options_len);
1385     }
1386
1387     if (vport->ifindex) {
1388         nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1389     }
1390 }
1391
1392 /* Clears 'vport' to "empty" values. */
1393 void
1394 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1395 {
1396     memset(vport, 0, sizeof *vport);
1397     vport->port_no = UINT32_MAX;
1398 }
1399
1400 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1401  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1402  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1403  * result of the command is expected to be an ovs_vport also, which is decoded
1404  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1405  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1406 int
1407 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1408                           struct dpif_linux_vport *reply,
1409                           struct ofpbuf **bufp)
1410 {
1411     struct ofpbuf *request_buf;
1412     int error;
1413
1414     assert((reply != NULL) == (bufp != NULL));
1415
1416     error = dpif_linux_init();
1417     if (error) {
1418         if (reply) {
1419             *bufp = NULL;
1420             dpif_linux_vport_init(reply);
1421         }
1422         return error;
1423     }
1424
1425     request_buf = ofpbuf_new(1024);
1426     dpif_linux_vport_to_ofpbuf(request, request_buf);
1427     error = nl_sock_transact(genl_sock, request_buf, bufp);
1428     ofpbuf_delete(request_buf);
1429
1430     if (reply) {
1431         if (!error) {
1432             error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1433         }
1434         if (error) {
1435             dpif_linux_vport_init(reply);
1436             ofpbuf_delete(*bufp);
1437             *bufp = NULL;
1438         }
1439     }
1440     return error;
1441 }
1442
1443 /* Obtains information about the kernel vport named 'name' and stores it into
1444  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1445  * longer needed ('reply' will contain pointers into '*bufp').  */
1446 int
1447 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1448                      struct ofpbuf **bufp)
1449 {
1450     struct dpif_linux_vport request;
1451
1452     dpif_linux_vport_init(&request);
1453     request.cmd = OVS_VPORT_CMD_GET;
1454     request.name = name;
1455
1456     return dpif_linux_vport_transact(&request, reply, bufp);
1457 }
1458 \f
1459 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1460  * by Netlink attributes, into 'dp'.  Returns 0 if successful, otherwise a
1461  * positive errno value.
1462  *
1463  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1464  * while 'dp' is still in use. */
1465 static int
1466 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1467 {
1468     static const struct nl_policy ovs_datapath_policy[] = {
1469         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1470         [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1471                                 .min_len = sizeof(struct ovs_dp_stats),
1472                                 .max_len = sizeof(struct ovs_dp_stats),
1473                                 .optional = true },
1474         [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1475     };
1476
1477     struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1478     struct ovs_header *ovs_header;
1479     struct nlmsghdr *nlmsg;
1480     struct genlmsghdr *genl;
1481     struct ofpbuf b;
1482
1483     dpif_linux_dp_init(dp);
1484
1485     ofpbuf_use_const(&b, buf->data, buf->size);
1486     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1487     genl = ofpbuf_try_pull(&b, sizeof *genl);
1488     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1489     if (!nlmsg || !genl || !ovs_header
1490         || nlmsg->nlmsg_type != ovs_datapath_family
1491         || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1492                             ARRAY_SIZE(ovs_datapath_policy))) {
1493         return EINVAL;
1494     }
1495
1496     dp->cmd = genl->cmd;
1497     dp->dp_ifindex = ovs_header->dp_ifindex;
1498     dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1499     if (a[OVS_DP_ATTR_STATS]) {
1500         /* Can't use structure assignment because Netlink doesn't ensure
1501          * sufficient alignment for 64-bit members. */
1502         memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1503                sizeof dp->stats);
1504     }
1505     if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1506         dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1507     }
1508
1509     return 0;
1510 }
1511
1512 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1513 static void
1514 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1515 {
1516     struct ovs_header *ovs_header;
1517
1518     nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1519                           NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1520
1521     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1522     ovs_header->dp_ifindex = dp->dp_ifindex;
1523
1524     if (dp->name) {
1525         nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1526     }
1527
1528     nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, dp->upcall_pid);
1529
1530     /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1531
1532     if (dp->ipv4_frags) {
1533         nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1534     }
1535 }
1536
1537 /* Clears 'dp' to "empty" values. */
1538 static void
1539 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1540 {
1541     memset(dp, 0, sizeof *dp);
1542 }
1543
1544 static void
1545 dpif_linux_dp_dump_start(struct nl_dump *dump)
1546 {
1547     struct dpif_linux_dp request;
1548     struct ofpbuf *buf;
1549
1550     dpif_linux_dp_init(&request);
1551     request.cmd = OVS_DP_CMD_GET;
1552
1553     buf = ofpbuf_new(1024);
1554     dpif_linux_dp_to_ofpbuf(&request, buf);
1555     nl_dump_start(dump, genl_sock, buf);
1556     ofpbuf_delete(buf);
1557 }
1558
1559 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1560  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1561  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1562  * result of the command is expected to be of the same form, which is decoded
1563  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1564  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1565 static int
1566 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1567                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1568 {
1569     struct ofpbuf *request_buf;
1570     int error;
1571
1572     assert((reply != NULL) == (bufp != NULL));
1573
1574     request_buf = ofpbuf_new(1024);
1575     dpif_linux_dp_to_ofpbuf(request, request_buf);
1576     error = nl_sock_transact(genl_sock, request_buf, bufp);
1577     ofpbuf_delete(request_buf);
1578
1579     if (reply) {
1580         if (!error) {
1581             error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1582         }
1583         if (error) {
1584             dpif_linux_dp_init(reply);
1585             ofpbuf_delete(*bufp);
1586             *bufp = NULL;
1587         }
1588     }
1589     return error;
1590 }
1591
1592 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1593  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1594  * will contain pointers into '*bufp').  */
1595 static int
1596 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1597                   struct ofpbuf **bufp)
1598 {
1599     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1600     struct dpif_linux_dp request;
1601
1602     dpif_linux_dp_init(&request);
1603     request.cmd = OVS_DP_CMD_GET;
1604     request.dp_ifindex = dpif->dp_ifindex;
1605
1606     return dpif_linux_dp_transact(&request, reply, bufp);
1607 }
1608 \f
1609 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1610  * by Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1611  * positive errno value.
1612  *
1613  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1614  * while 'flow' is still in use. */
1615 static int
1616 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1617                             const struct ofpbuf *buf)
1618 {
1619     static const struct nl_policy ovs_flow_policy[] = {
1620         [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1621         [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1622         [OVS_FLOW_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1623         [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1624                                   .min_len = sizeof(struct ovs_flow_stats),
1625                                   .max_len = sizeof(struct ovs_flow_stats),
1626                                   .optional = true },
1627         [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1628         [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1629         /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1630     };
1631
1632     struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1633     struct ovs_header *ovs_header;
1634     struct nlmsghdr *nlmsg;
1635     struct genlmsghdr *genl;
1636     struct ofpbuf b;
1637
1638     dpif_linux_flow_init(flow);
1639
1640     ofpbuf_use_const(&b, buf->data, buf->size);
1641     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1642     genl = ofpbuf_try_pull(&b, sizeof *genl);
1643     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1644     if (!nlmsg || !genl || !ovs_header
1645         || nlmsg->nlmsg_type != ovs_flow_family
1646         || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1647                             ARRAY_SIZE(ovs_flow_policy))) {
1648         return EINVAL;
1649     }
1650
1651     flow->nlmsg_flags = nlmsg->nlmsg_flags;
1652     flow->dp_ifindex = ovs_header->dp_ifindex;
1653     flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1654     flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1655     if (a[OVS_FLOW_ATTR_ACTIONS]) {
1656         flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1657         flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1658     }
1659     if (a[OVS_FLOW_ATTR_UPCALL_PID]) {
1660         flow->upcall_pid = nl_attr_get_u32(a[OVS_FLOW_ATTR_UPCALL_PID]);
1661     }
1662     if (a[OVS_FLOW_ATTR_STATS]) {
1663         flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1664     }
1665     if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1666         flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1667     }
1668     if (a[OVS_FLOW_ATTR_USED]) {
1669         flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1670     }
1671     return 0;
1672 }
1673
1674 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1675  * followed by Netlink attributes corresponding to 'flow'. */
1676 static void
1677 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1678                           struct ofpbuf *buf)
1679 {
1680     struct ovs_header *ovs_header;
1681
1682     nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1683                           NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1684                           flow->cmd, 1);
1685
1686     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1687     ovs_header->dp_ifindex = flow->dp_ifindex;
1688
1689     if (flow->key_len) {
1690         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1691     }
1692
1693     if (flow->actions || flow->actions_len) {
1694         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1695                           flow->actions, flow->actions_len);
1696     }
1697
1698     nl_msg_put_u32(buf, OVS_FLOW_ATTR_UPCALL_PID, flow->upcall_pid);
1699
1700     /* We never need to send these to the kernel. */
1701     assert(!flow->stats);
1702     assert(!flow->tcp_flags);
1703     assert(!flow->used);
1704
1705     if (flow->clear) {
1706         nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1707     }
1708 }
1709
1710 /* Clears 'flow' to "empty" values. */
1711 static void
1712 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1713 {
1714     memset(flow, 0, sizeof *flow);
1715 }
1716
1717 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1718  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1719  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1720  * result of the command is expected to be a flow also, which is decoded and
1721  * stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the reply
1722  * is no longer needed ('reply' will contain pointers into '*bufp'). */
1723 static int
1724 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1725                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1726 {
1727     struct ofpbuf *request_buf;
1728     int error;
1729
1730     assert((reply != NULL) == (bufp != NULL));
1731
1732     request_buf = ofpbuf_new(1024);
1733     dpif_linux_flow_to_ofpbuf(request, request_buf);
1734     error = nl_sock_transact(genl_sock, request_buf, bufp);
1735     ofpbuf_delete(request_buf);
1736
1737     if (reply) {
1738         if (!error) {
1739             error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1740         }
1741         if (error) {
1742             dpif_linux_flow_init(reply);
1743             ofpbuf_delete(*bufp);
1744             *bufp = NULL;
1745         }
1746     }
1747     return error;
1748 }
1749
1750 static void
1751 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1752                           struct dpif_flow_stats *stats)
1753 {
1754     if (flow->stats) {
1755         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1756         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1757     } else {
1758         stats->n_packets = 0;
1759         stats->n_bytes = 0;
1760     }
1761     stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1762     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1763 }
1764