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