dpif: New function dpif_operate() and dpif-linux implementation.
[sliver-openvswitch.git] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "dpif-linux.h"
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <net/if.h>
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
31 #include <stdlib.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "bitmap.h"
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
38 #include "flow.h"
39 #include "netdev.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
44 #include "netlink.h"
45 #include "odp-util.h"
46 #include "ofpbuf.h"
47 #include "openvswitch/datapath-compat.h"
48 #include "openvswitch/tunnel.h"
49 #include "packets.h"
50 #include "poll-loop.h"
51 #include "random.h"
52 #include "shash.h"
53 #include "sset.h"
54 #include "unaligned.h"
55 #include "util.h"
56 #include "vlog.h"
57
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
59
60 enum { LRU_MAX_PORTS = 1024 };
61 enum { LRU_MASK = LRU_MAX_PORTS - 1};
62 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
63
64 enum { N_UPCALL_SOCKS = 16 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
66
67 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
68  * missing if we have old headers. */
69 #define ETH_FLAG_LRO      (1 << 15)    /* LRO is enabled */
70
71 struct dpif_linux_dp {
72     /* Generic Netlink header. */
73     uint8_t cmd;
74
75     /* struct ovs_header. */
76     int dp_ifindex;
77
78     /* Attributes. */
79     const char *name;                  /* OVS_DP_ATTR_NAME. */
80     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 void
691 dpif_linux_init_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_linux_flow *request)
695 {
696     static struct nlattr dummy_action;
697
698     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
699
700     dpif_linux_flow_init(request);
701     request->cmd = (flags & DPIF_FP_CREATE
702                     ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
703     request->dp_ifindex = dpif->dp_ifindex;
704     request->key = key;
705     request->key_len = key_len;
706     /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
707     request->actions = actions ? actions : &dummy_action;
708     request->actions_len = actions_len;
709     if (flags & DPIF_FP_ZERO_STATS) {
710         request->clear = true;
711     }
712     request->nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
713 }
714
715 static int
716 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
717                     const struct nlattr *key, size_t key_len,
718                     const struct nlattr *actions, size_t actions_len,
719                     struct dpif_flow_stats *stats)
720 {
721     struct dpif_linux_flow request, reply;
722     struct ofpbuf *buf;
723     int error;
724
725     dpif_linux_init_flow_put(dpif_, flags, key, key_len, actions, actions_len,
726                              &request);
727     error = dpif_linux_flow_transact(&request,
728                                      stats ? &reply : NULL,
729                                      stats ? &buf : NULL);
730     if (!error && stats) {
731         dpif_linux_flow_get_stats(&reply, stats);
732         ofpbuf_delete(buf);
733     }
734     return error;
735 }
736
737 static int
738 dpif_linux_flow_del(struct dpif *dpif_,
739                     const struct nlattr *key, size_t key_len,
740                     struct dpif_flow_stats *stats)
741 {
742     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
743     struct dpif_linux_flow request, reply;
744     struct ofpbuf *buf;
745     int error;
746
747     dpif_linux_flow_init(&request);
748     request.cmd = OVS_FLOW_CMD_DEL;
749     request.dp_ifindex = dpif->dp_ifindex;
750     request.key = key;
751     request.key_len = key_len;
752     error = dpif_linux_flow_transact(&request,
753                                      stats ? &reply : NULL,
754                                      stats ? &buf : NULL);
755     if (!error && stats) {
756         dpif_linux_flow_get_stats(&reply, stats);
757         ofpbuf_delete(buf);
758     }
759     return error;
760 }
761
762 struct dpif_linux_flow_state {
763     struct nl_dump dump;
764     struct dpif_linux_flow flow;
765     struct dpif_flow_stats stats;
766     struct ofpbuf *buf;
767 };
768
769 static int
770 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
771 {
772     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
773     struct dpif_linux_flow_state *state;
774     struct dpif_linux_flow request;
775     struct ofpbuf *buf;
776
777     *statep = state = xmalloc(sizeof *state);
778
779     dpif_linux_flow_init(&request);
780     request.cmd = OVS_DP_CMD_GET;
781     request.dp_ifindex = dpif->dp_ifindex;
782
783     buf = ofpbuf_new(1024);
784     dpif_linux_flow_to_ofpbuf(&request, buf);
785     nl_dump_start(&state->dump, genl_sock, buf);
786     ofpbuf_delete(buf);
787
788     state->buf = NULL;
789
790     return 0;
791 }
792
793 static int
794 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
795                           const struct nlattr **key, size_t *key_len,
796                           const struct nlattr **actions, size_t *actions_len,
797                           const struct dpif_flow_stats **stats)
798 {
799     struct dpif_linux_flow_state *state = state_;
800     struct ofpbuf buf;
801     int error;
802
803     do {
804         ofpbuf_delete(state->buf);
805         state->buf = NULL;
806
807         if (!nl_dump_next(&state->dump, &buf)) {
808             return EOF;
809         }
810
811         error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
812         if (error) {
813             return error;
814         }
815
816         if (actions && !state->flow.actions) {
817             error = dpif_linux_flow_get__(dpif_, state->flow.key,
818                                           state->flow.key_len,
819                                           &state->flow, &state->buf);
820             if (error == ENOENT) {
821                 VLOG_DBG("dumped flow disappeared on get");
822             } else if (error) {
823                 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
824             }
825         }
826     } while (error);
827
828     if (actions) {
829         *actions = state->flow.actions;
830         *actions_len = state->flow.actions_len;
831     }
832     if (key) {
833         *key = state->flow.key;
834         *key_len = state->flow.key_len;
835     }
836     if (stats) {
837         dpif_linux_flow_get_stats(&state->flow, &state->stats);
838         *stats = &state->stats;
839     }
840     return error;
841 }
842
843 static int
844 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
845 {
846     struct dpif_linux_flow_state *state = state_;
847     int error = nl_dump_done(&state->dump);
848     ofpbuf_delete(state->buf);
849     free(state);
850     return error;
851 }
852
853 static struct ofpbuf *
854 dpif_linux_encode_execute(int dp_ifindex,
855                           const struct nlattr *key, size_t key_len,
856                           const struct nlattr *actions, size_t actions_len,
857                           const struct ofpbuf *packet)
858 {
859     struct ovs_header *execute;
860     struct ofpbuf *buf;
861
862     buf = ofpbuf_new(128 + actions_len + packet->size);
863
864     nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
865                           OVS_PACKET_CMD_EXECUTE, 1);
866
867     execute = ofpbuf_put_uninit(buf, sizeof *execute);
868     execute->dp_ifindex = dp_ifindex;
869
870     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
871     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
872     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
873
874     return buf;
875 }
876
877 static int
878 dpif_linux_execute__(int dp_ifindex, const struct nlattr *key, size_t key_len,
879                      const struct nlattr *actions, size_t actions_len,
880                      const struct ofpbuf *packet)
881 {
882     struct ofpbuf *request;
883     int error;
884
885     request = dpif_linux_encode_execute(dp_ifindex,
886                                         key, key_len, actions, actions_len,
887                                         packet);
888     error = nl_sock_transact(genl_sock, request, NULL);
889     ofpbuf_delete(request);
890
891     return error;
892 }
893
894 static int
895 dpif_linux_execute(struct dpif *dpif_,
896                    const struct nlattr *key, size_t key_len,
897                    const struct nlattr *actions, size_t actions_len,
898                    const struct ofpbuf *packet)
899 {
900     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
901
902     return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
903                                 actions, actions_len, packet);
904 }
905
906 static void
907 dpif_linux_operate(struct dpif *dpif_, union dpif_op **ops, size_t n_ops)
908 {
909     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
910     struct nl_transaction **txnsp;
911     struct nl_transaction *txns;
912     size_t i;
913
914     txns = xmalloc(n_ops * sizeof *txns);
915     for (i = 0; i < n_ops; i++) {
916         struct nl_transaction *txn = &txns[i];
917         union dpif_op *op = ops[i];
918
919         if (op->type == DPIF_OP_FLOW_PUT) {
920             struct dpif_flow_put *put = &op->flow_put;
921             struct dpif_linux_flow request;
922
923             dpif_linux_init_flow_put(dpif_, put->flags, put->key, put->key_len,
924                                      put->actions, put->actions_len,
925                                      &request);
926             if (put->stats) {
927                 request.nlmsg_flags |= NLM_F_ECHO;
928             }
929             txn->request = ofpbuf_new(1024);
930             dpif_linux_flow_to_ofpbuf(&request, txn->request);
931         } else if (op->type == DPIF_OP_EXECUTE) {
932             struct dpif_execute *execute = &op->execute;
933
934             txn->request = dpif_linux_encode_execute(
935                 dpif->dp_ifindex, execute->key, execute->key_len,
936                 execute->actions, execute->actions_len, execute->packet);
937         } else {
938             NOT_REACHED();
939         }
940     }
941
942     txnsp = xmalloc(n_ops * sizeof *txnsp);
943     for (i = 0; i < n_ops; i++) {
944         txnsp[i] = &txns[i];
945     }
946
947     nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
948
949     free(txnsp);
950
951     for (i = 0; i < n_ops; i++) {
952         struct nl_transaction *txn = &txns[i];
953         union dpif_op *op = ops[i];
954
955         if (op->type == DPIF_OP_FLOW_PUT) {
956             struct dpif_flow_put *put = &op->flow_put;
957             int error = txn->error;
958
959             if (!error && put->stats) {
960                 struct dpif_linux_flow reply;
961
962                 error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
963                 if (!error) {
964                     dpif_linux_flow_get_stats(&reply, put->stats);
965                 }
966             }
967             put->error = error;
968         } else if (op->type == DPIF_OP_EXECUTE) {
969             struct dpif_execute *execute = &op->execute;
970
971             execute->error = txn->error;
972         } else {
973             NOT_REACHED();
974         }
975
976         ofpbuf_delete(txn->request);
977         ofpbuf_delete(txn->reply);
978     }
979     free(txns);
980 }
981
982 static int
983 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
984 {
985     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
986     *listen_mask = dpif->listen_mask;
987     return 0;
988 }
989
990 static void
991 set_upcall_pids(struct dpif *dpif_)
992 {
993     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
994     struct dpif_port_dump port_dump;
995     struct dpif_port port;
996     int error;
997
998     DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
999         uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
1000                                                         DPIF_UC_MISS);
1001         struct dpif_linux_vport vport_request;
1002
1003         dpif_linux_vport_init(&vport_request);
1004         vport_request.cmd = OVS_VPORT_CMD_SET;
1005         vport_request.dp_ifindex = dpif->dp_ifindex;
1006         vport_request.port_no = port.port_no;
1007         vport_request.upcall_pid = &upcall_pid;
1008         error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1009         if (!error) {
1010             VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1011                      dpif_name(&dpif->dpif), vport_request.port_no,
1012                      upcall_pid);
1013         } else {
1014             VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
1015                          dpif_name(&dpif->dpif), strerror(error));
1016         }
1017     }
1018 }
1019
1020 static int
1021 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
1022 {
1023     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1024
1025     if (listen_mask == dpif->listen_mask) {
1026         return 0;
1027     }
1028
1029     if (!listen_mask) {
1030         destroy_upcall_socks(dpif);
1031     } else if (!dpif->listen_mask) {
1032         int i;
1033         int error;
1034
1035         for (i = 0; i < N_UPCALL_SOCKS; i++) {
1036             error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
1037             if (error) {
1038                 destroy_upcall_socks(dpif);
1039                 return error;
1040             }
1041         }
1042     }
1043
1044     dpif->listen_mask = listen_mask;
1045     set_upcall_pids(dpif_);
1046
1047     return 0;
1048 }
1049
1050 static int
1051 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1052                              uint32_t queue_id, uint32_t *priority)
1053 {
1054     if (queue_id < 0xf000) {
1055         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1056         return 0;
1057     } else {
1058         return EINVAL;
1059     }
1060 }
1061
1062 static int
1063 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1064                  int *dp_ifindex)
1065 {
1066     static const struct nl_policy ovs_packet_policy[] = {
1067         /* Always present. */
1068         [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1069                                      .min_len = ETH_HEADER_LEN },
1070         [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1071
1072         /* OVS_PACKET_CMD_ACTION only. */
1073         [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1074     };
1075
1076     struct ovs_header *ovs_header;
1077     struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1078     struct nlmsghdr *nlmsg;
1079     struct genlmsghdr *genl;
1080     struct ofpbuf b;
1081     int type;
1082
1083     ofpbuf_use_const(&b, buf->data, buf->size);
1084
1085     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1086     genl = ofpbuf_try_pull(&b, sizeof *genl);
1087     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1088     if (!nlmsg || !genl || !ovs_header
1089         || nlmsg->nlmsg_type != ovs_packet_family
1090         || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1091                             ARRAY_SIZE(ovs_packet_policy))) {
1092         return EINVAL;
1093     }
1094
1095     type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1096             : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1097             : -1);
1098     if (type < 0) {
1099         return EINVAL;
1100     }
1101
1102     memset(upcall, 0, sizeof *upcall);
1103     upcall->type = type;
1104     upcall->packet = buf;
1105     upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1106     upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1107     upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1108     upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1109     upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1110                         ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1111                         : 0);
1112     *dp_ifindex = ovs_header->dp_ifindex;
1113
1114     return 0;
1115 }
1116
1117 static int
1118 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1119 {
1120     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1121     int i;
1122     int read_tries = 0;
1123
1124     if (!dpif->listen_mask) {
1125        return EAGAIN;
1126     }
1127
1128     for (i = 0; i < N_UPCALL_SOCKS; i++) {
1129         struct nl_sock *upcall_sock;
1130         dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1131                                  (N_UPCALL_SOCKS - 1);
1132         upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1133
1134         if (nl_sock_woke(upcall_sock)) {
1135             int dp_ifindex;
1136
1137             for (;;) {
1138                 struct ofpbuf *buf;
1139                 int error;
1140
1141                 if (++read_tries > 50) {
1142                     return EAGAIN;
1143                 }
1144
1145                 error = nl_sock_recv(upcall_sock, &buf, false);
1146                 if (error == EAGAIN) {
1147                     break;
1148                 } else if (error) {
1149                     return error;
1150                 }
1151
1152                 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1153                 if (!error
1154                     && dp_ifindex == dpif->dp_ifindex
1155                     && dpif->listen_mask & (1u << upcall->type)) {
1156                     return 0;
1157                 }
1158
1159                 ofpbuf_delete(buf);
1160                 if (error) {
1161                     return error;
1162                 }
1163             }
1164         }
1165     }
1166
1167     return EAGAIN;
1168 }
1169
1170 static void
1171 dpif_linux_recv_wait(struct dpif *dpif_)
1172 {
1173     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1174     int i;
1175
1176     if (!dpif->listen_mask) {
1177        return;
1178     }
1179
1180     for (i = 0; i < N_UPCALL_SOCKS; i++) {
1181         nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1182     }
1183 }
1184
1185 static void
1186 dpif_linux_recv_purge(struct dpif *dpif_)
1187 {
1188     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1189     int i;
1190
1191     if (!dpif->listen_mask) {
1192        return;
1193     }
1194
1195     for (i = 0; i < N_UPCALL_SOCKS; i++) {
1196         nl_sock_drain(dpif->upcall_socks[i]);
1197     }
1198 }
1199
1200 const struct dpif_class dpif_linux_class = {
1201     "system",
1202     dpif_linux_enumerate,
1203     dpif_linux_open,
1204     dpif_linux_close,
1205     dpif_linux_destroy,
1206     dpif_linux_run,
1207     dpif_linux_wait,
1208     dpif_linux_get_stats,
1209     dpif_linux_get_drop_frags,
1210     dpif_linux_set_drop_frags,
1211     dpif_linux_port_add,
1212     dpif_linux_port_del,
1213     dpif_linux_port_query_by_number,
1214     dpif_linux_port_query_by_name,
1215     dpif_linux_get_max_ports,
1216     dpif_linux_port_get_pid,
1217     dpif_linux_port_dump_start,
1218     dpif_linux_port_dump_next,
1219     dpif_linux_port_dump_done,
1220     dpif_linux_port_poll,
1221     dpif_linux_port_poll_wait,
1222     dpif_linux_flow_get,
1223     dpif_linux_flow_put,
1224     dpif_linux_flow_del,
1225     dpif_linux_flow_flush,
1226     dpif_linux_flow_dump_start,
1227     dpif_linux_flow_dump_next,
1228     dpif_linux_flow_dump_done,
1229     dpif_linux_execute,
1230     dpif_linux_operate,
1231     dpif_linux_recv_get_mask,
1232     dpif_linux_recv_set_mask,
1233     dpif_linux_queue_to_priority,
1234     dpif_linux_recv,
1235     dpif_linux_recv_wait,
1236     dpif_linux_recv_purge,
1237 };
1238 \f
1239 static int
1240 dpif_linux_init(void)
1241 {
1242     static int error = -1;
1243
1244     if (error < 0) {
1245         unsigned int ovs_vport_mcgroup;
1246
1247         error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1248                                       &ovs_datapath_family);
1249         if (error) {
1250             VLOG_ERR("Generic Netlink family '%s' does not exist. "
1251                      "The Open vSwitch kernel module is probably not loaded.",
1252                      OVS_DATAPATH_FAMILY);
1253         }
1254         if (!error) {
1255             error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1256         }
1257         if (!error) {
1258             error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1259         }
1260         if (!error) {
1261             error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1262                                           &ovs_packet_family);
1263         }
1264         if (!error) {
1265             error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1266         }
1267         if (!error) {
1268             error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1269                                            &ovs_vport_mcgroup,
1270                                            OVS_VPORT_MCGROUP_FALLBACK_ID);
1271         }
1272         if (!error) {
1273             static struct dpif_linux_vport vport;
1274             nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1275                              dpif_linux_nln_parse, &vport);
1276         }
1277     }
1278
1279     return error;
1280 }
1281
1282 bool
1283 dpif_linux_is_internal_device(const char *name)
1284 {
1285     struct dpif_linux_vport reply;
1286     struct ofpbuf *buf;
1287     int error;
1288
1289     error = dpif_linux_vport_get(name, &reply, &buf);
1290     if (!error) {
1291         ofpbuf_delete(buf);
1292     } else if (error != ENODEV && error != ENOENT) {
1293         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1294                      name, strerror(error));
1295     }
1296
1297     return reply.type == OVS_VPORT_TYPE_INTERNAL;
1298 }
1299
1300 int
1301 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1302                       const void *data, size_t size)
1303 {
1304     struct ofpbuf actions, key, packet;
1305     struct odputil_keybuf keybuf;
1306     struct flow flow;
1307     uint64_t action;
1308
1309     ofpbuf_use_const(&packet, data, size);
1310     flow_extract(&packet, htonll(0), 0, &flow);
1311
1312     ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1313     odp_flow_key_from_flow(&key, &flow);
1314
1315     ofpbuf_use_stack(&actions, &action, sizeof action);
1316     nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1317
1318     return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1319                                 actions.data, actions.size, &packet);
1320 }
1321
1322 static bool
1323 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1324 {
1325     struct dpif_linux_vport *vport = vport_;
1326     return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1327 }
1328
1329 static void
1330 dpif_linux_port_changed(const void *vport_, void *dpif_)
1331 {
1332     const struct dpif_linux_vport *vport = vport_;
1333     struct dpif_linux *dpif = dpif_;
1334
1335     if (vport) {
1336         if (vport->dp_ifindex == dpif->dp_ifindex
1337             && (vport->cmd == OVS_VPORT_CMD_NEW
1338                 || vport->cmd == OVS_VPORT_CMD_DEL
1339                 || vport->cmd == OVS_VPORT_CMD_SET)) {
1340             VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1341                      dpif->dpif.full_name, vport->name, vport->cmd);
1342             sset_add(&dpif->changed_ports, vport->name);
1343         }
1344     } else {
1345         dpif->change_error = true;
1346     }
1347 }
1348 \f
1349 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1350  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1351  * positive errno value.
1352  *
1353  * 'vport' will contain pointers into 'buf', so the caller should not free
1354  * 'buf' while 'vport' is still in use. */
1355 static int
1356 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1357                              const struct ofpbuf *buf)
1358 {
1359     static const struct nl_policy ovs_vport_policy[] = {
1360         [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1361         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1362         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1363         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1364         [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1365                                    .min_len = sizeof(struct ovs_vport_stats),
1366                                    .max_len = sizeof(struct ovs_vport_stats),
1367                                    .optional = true },
1368         [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1369                                      .min_len = ETH_ADDR_LEN,
1370                                      .max_len = ETH_ADDR_LEN,
1371                                      .optional = true },
1372         [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1373     };
1374
1375     struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1376     struct ovs_header *ovs_header;
1377     struct nlmsghdr *nlmsg;
1378     struct genlmsghdr *genl;
1379     struct ofpbuf b;
1380
1381     dpif_linux_vport_init(vport);
1382
1383     ofpbuf_use_const(&b, buf->data, buf->size);
1384     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1385     genl = ofpbuf_try_pull(&b, sizeof *genl);
1386     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1387     if (!nlmsg || !genl || !ovs_header
1388         || nlmsg->nlmsg_type != ovs_vport_family
1389         || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1390                             ARRAY_SIZE(ovs_vport_policy))) {
1391         return EINVAL;
1392     }
1393
1394     vport->cmd = genl->cmd;
1395     vport->dp_ifindex = ovs_header->dp_ifindex;
1396     vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1397     vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1398     vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1399     if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1400         vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1401     }
1402     if (a[OVS_VPORT_ATTR_STATS]) {
1403         vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1404     }
1405     if (a[OVS_VPORT_ATTR_ADDRESS]) {
1406         vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1407     }
1408     if (a[OVS_VPORT_ATTR_OPTIONS]) {
1409         vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1410         vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1411     }
1412     return 0;
1413 }
1414
1415 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1416  * followed by Netlink attributes corresponding to 'vport'. */
1417 static void
1418 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1419                            struct ofpbuf *buf)
1420 {
1421     struct ovs_header *ovs_header;
1422
1423     nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1424                           vport->cmd, 1);
1425
1426     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1427     ovs_header->dp_ifindex = vport->dp_ifindex;
1428
1429     if (vport->port_no != UINT32_MAX) {
1430         nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1431     }
1432
1433     if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1434         nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1435     }
1436
1437     if (vport->name) {
1438         nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1439     }
1440
1441     if (vport->upcall_pid) {
1442         nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1443     }
1444
1445     if (vport->stats) {
1446         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1447                           vport->stats, sizeof *vport->stats);
1448     }
1449
1450     if (vport->address) {
1451         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1452                           vport->address, ETH_ADDR_LEN);
1453     }
1454
1455     if (vport->options) {
1456         nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1457                           vport->options, vport->options_len);
1458     }
1459 }
1460
1461 /* Clears 'vport' to "empty" values. */
1462 void
1463 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1464 {
1465     memset(vport, 0, sizeof *vport);
1466     vport->port_no = UINT32_MAX;
1467 }
1468
1469 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1470  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1471  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1472  * result of the command is expected to be an ovs_vport also, which is decoded
1473  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1474  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1475 int
1476 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1477                           struct dpif_linux_vport *reply,
1478                           struct ofpbuf **bufp)
1479 {
1480     struct ofpbuf *request_buf;
1481     int error;
1482
1483     assert((reply != NULL) == (bufp != NULL));
1484
1485     error = dpif_linux_init();
1486     if (error) {
1487         if (reply) {
1488             *bufp = NULL;
1489             dpif_linux_vport_init(reply);
1490         }
1491         return error;
1492     }
1493
1494     request_buf = ofpbuf_new(1024);
1495     dpif_linux_vport_to_ofpbuf(request, request_buf);
1496     error = nl_sock_transact(genl_sock, request_buf, bufp);
1497     ofpbuf_delete(request_buf);
1498
1499     if (reply) {
1500         if (!error) {
1501             error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1502         }
1503         if (error) {
1504             dpif_linux_vport_init(reply);
1505             ofpbuf_delete(*bufp);
1506             *bufp = NULL;
1507         }
1508     }
1509     return error;
1510 }
1511
1512 /* Obtains information about the kernel vport named 'name' and stores it into
1513  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1514  * longer needed ('reply' will contain pointers into '*bufp').  */
1515 int
1516 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1517                      struct ofpbuf **bufp)
1518 {
1519     struct dpif_linux_vport request;
1520
1521     dpif_linux_vport_init(&request);
1522     request.cmd = OVS_VPORT_CMD_GET;
1523     request.name = name;
1524
1525     return dpif_linux_vport_transact(&request, reply, bufp);
1526 }
1527 \f
1528 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1529  * by Netlink attributes, into 'dp'.  Returns 0 if successful, otherwise a
1530  * positive errno value.
1531  *
1532  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1533  * while 'dp' is still in use. */
1534 static int
1535 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1536 {
1537     static const struct nl_policy ovs_datapath_policy[] = {
1538         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1539         [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1540                                 .min_len = sizeof(struct ovs_dp_stats),
1541                                 .max_len = sizeof(struct ovs_dp_stats),
1542                                 .optional = true },
1543         [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1544     };
1545
1546     struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1547     struct ovs_header *ovs_header;
1548     struct nlmsghdr *nlmsg;
1549     struct genlmsghdr *genl;
1550     struct ofpbuf b;
1551
1552     dpif_linux_dp_init(dp);
1553
1554     ofpbuf_use_const(&b, buf->data, buf->size);
1555     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1556     genl = ofpbuf_try_pull(&b, sizeof *genl);
1557     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1558     if (!nlmsg || !genl || !ovs_header
1559         || nlmsg->nlmsg_type != ovs_datapath_family
1560         || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1561                             ARRAY_SIZE(ovs_datapath_policy))) {
1562         return EINVAL;
1563     }
1564
1565     dp->cmd = genl->cmd;
1566     dp->dp_ifindex = ovs_header->dp_ifindex;
1567     dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1568     if (a[OVS_DP_ATTR_STATS]) {
1569         /* Can't use structure assignment because Netlink doesn't ensure
1570          * sufficient alignment for 64-bit members. */
1571         memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1572                sizeof dp->stats);
1573     }
1574     if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1575         dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1576     }
1577
1578     return 0;
1579 }
1580
1581 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1582 static void
1583 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1584 {
1585     struct ovs_header *ovs_header;
1586
1587     nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1588                           NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1589
1590     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1591     ovs_header->dp_ifindex = dp->dp_ifindex;
1592
1593     if (dp->name) {
1594         nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1595     }
1596
1597     if (dp->upcall_pid) {
1598         nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1599     }
1600
1601     /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1602
1603     if (dp->ipv4_frags) {
1604         nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1605     }
1606 }
1607
1608 /* Clears 'dp' to "empty" values. */
1609 static void
1610 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1611 {
1612     memset(dp, 0, sizeof *dp);
1613 }
1614
1615 static void
1616 dpif_linux_dp_dump_start(struct nl_dump *dump)
1617 {
1618     struct dpif_linux_dp request;
1619     struct ofpbuf *buf;
1620
1621     dpif_linux_dp_init(&request);
1622     request.cmd = OVS_DP_CMD_GET;
1623
1624     buf = ofpbuf_new(1024);
1625     dpif_linux_dp_to_ofpbuf(&request, buf);
1626     nl_dump_start(dump, genl_sock, buf);
1627     ofpbuf_delete(buf);
1628 }
1629
1630 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1631  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1632  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1633  * result of the command is expected to be of the same form, which is decoded
1634  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1635  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1636 static int
1637 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1638                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1639 {
1640     struct ofpbuf *request_buf;
1641     int error;
1642
1643     assert((reply != NULL) == (bufp != NULL));
1644
1645     request_buf = ofpbuf_new(1024);
1646     dpif_linux_dp_to_ofpbuf(request, request_buf);
1647     error = nl_sock_transact(genl_sock, request_buf, bufp);
1648     ofpbuf_delete(request_buf);
1649
1650     if (reply) {
1651         if (!error) {
1652             error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1653         }
1654         if (error) {
1655             dpif_linux_dp_init(reply);
1656             ofpbuf_delete(*bufp);
1657             *bufp = NULL;
1658         }
1659     }
1660     return error;
1661 }
1662
1663 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1664  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1665  * will contain pointers into '*bufp').  */
1666 static int
1667 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1668                   struct ofpbuf **bufp)
1669 {
1670     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1671     struct dpif_linux_dp request;
1672
1673     dpif_linux_dp_init(&request);
1674     request.cmd = OVS_DP_CMD_GET;
1675     request.dp_ifindex = dpif->dp_ifindex;
1676
1677     return dpif_linux_dp_transact(&request, reply, bufp);
1678 }
1679 \f
1680 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1681  * by Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1682  * positive errno value.
1683  *
1684  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1685  * while 'flow' is still in use. */
1686 static int
1687 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1688                             const struct ofpbuf *buf)
1689 {
1690     static const struct nl_policy ovs_flow_policy[] = {
1691         [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1692         [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1693         [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1694                                   .min_len = sizeof(struct ovs_flow_stats),
1695                                   .max_len = sizeof(struct ovs_flow_stats),
1696                                   .optional = true },
1697         [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1698         [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1699         /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1700     };
1701
1702     struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1703     struct ovs_header *ovs_header;
1704     struct nlmsghdr *nlmsg;
1705     struct genlmsghdr *genl;
1706     struct ofpbuf b;
1707
1708     dpif_linux_flow_init(flow);
1709
1710     ofpbuf_use_const(&b, buf->data, buf->size);
1711     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1712     genl = ofpbuf_try_pull(&b, sizeof *genl);
1713     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1714     if (!nlmsg || !genl || !ovs_header
1715         || nlmsg->nlmsg_type != ovs_flow_family
1716         || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1717                             ARRAY_SIZE(ovs_flow_policy))) {
1718         return EINVAL;
1719     }
1720
1721     flow->nlmsg_flags = nlmsg->nlmsg_flags;
1722     flow->dp_ifindex = ovs_header->dp_ifindex;
1723     flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1724     flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1725     if (a[OVS_FLOW_ATTR_ACTIONS]) {
1726         flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1727         flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1728     }
1729     if (a[OVS_FLOW_ATTR_STATS]) {
1730         flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1731     }
1732     if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1733         flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1734     }
1735     if (a[OVS_FLOW_ATTR_USED]) {
1736         flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1737     }
1738     return 0;
1739 }
1740
1741 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1742  * followed by Netlink attributes corresponding to 'flow'. */
1743 static void
1744 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1745                           struct ofpbuf *buf)
1746 {
1747     struct ovs_header *ovs_header;
1748
1749     nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1750                           NLM_F_REQUEST | flow->nlmsg_flags,
1751                           flow->cmd, 1);
1752
1753     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1754     ovs_header->dp_ifindex = flow->dp_ifindex;
1755
1756     if (flow->key_len) {
1757         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1758     }
1759
1760     if (flow->actions || flow->actions_len) {
1761         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1762                           flow->actions, flow->actions_len);
1763     }
1764
1765     /* We never need to send these to the kernel. */
1766     assert(!flow->stats);
1767     assert(!flow->tcp_flags);
1768     assert(!flow->used);
1769
1770     if (flow->clear) {
1771         nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1772     }
1773 }
1774
1775 /* Clears 'flow' to "empty" values. */
1776 static void
1777 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1778 {
1779     memset(flow, 0, sizeof *flow);
1780 }
1781
1782 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1783  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1784  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1785  * result of the command is expected to be a flow also, which is decoded and
1786  * stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the reply
1787  * is no longer needed ('reply' will contain pointers into '*bufp'). */
1788 static int
1789 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1790                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1791 {
1792     struct ofpbuf *request_buf;
1793     int error;
1794
1795     assert((reply != NULL) == (bufp != NULL));
1796
1797     if (reply) {
1798         request->nlmsg_flags |= NLM_F_ECHO;
1799     }
1800
1801     request_buf = ofpbuf_new(1024);
1802     dpif_linux_flow_to_ofpbuf(request, request_buf);
1803     error = nl_sock_transact(genl_sock, request_buf, bufp);
1804     ofpbuf_delete(request_buf);
1805
1806     if (reply) {
1807         if (!error) {
1808             error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1809         }
1810         if (error) {
1811             dpif_linux_flow_init(reply);
1812             ofpbuf_delete(*bufp);
1813             *bufp = NULL;
1814         }
1815     }
1816     return error;
1817 }
1818
1819 static void
1820 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1821                           struct dpif_flow_stats *stats)
1822 {
1823     if (flow->stats) {
1824         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1825         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1826     } else {
1827         stats->n_packets = 0;
1828         stats->n_bytes = 0;
1829     }
1830     stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1831     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1832 }