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