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