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