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