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