datapath: Always use generic stats for devices (vports)
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 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 "netdev-vport.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <linux/rtnetlink.h>
25 #include <net/if.h>
26 #include <sys/ioctl.h>
27
28 #include "byte-order.h"
29 #include "daemon.h"
30 #include "dirs.h"
31 #include "dpif-linux.h"
32 #include "hash.h"
33 #include "hmap.h"
34 #include "list.h"
35 #include "netdev-linux.h"
36 #include "netdev-provider.h"
37 #include "netlink.h"
38 #include "netlink-notifier.h"
39 #include "netlink-socket.h"
40 #include "ofpbuf.h"
41 #include "openvswitch/datapath-protocol.h"
42 #include "openvswitch/tunnel.h"
43 #include "packets.h"
44 #include "route-table.h"
45 #include "shash.h"
46 #include "socket-util.h"
47 #include "vlog.h"
48
49 VLOG_DEFINE_THIS_MODULE(netdev_vport);
50
51 struct netdev_dev_vport {
52     struct netdev_dev netdev_dev;
53     struct ofpbuf *options;
54     int dp_ifindex;             /* -1 if unknown. */
55     uint32_t port_no;           /* UINT32_MAX if unknown. */
56     unsigned int change_seq;
57 };
58
59 struct netdev_vport {
60     struct netdev netdev;
61 };
62
63 struct vport_class {
64     enum ovs_vport_type type;
65     struct netdev_class netdev_class;
66     int (*parse_config)(const char *name, const char *type,
67                         const struct shash *args, struct ofpbuf *options);
68     int (*unparse_config)(const char *name, const char *type,
69                           const struct nlattr *options, size_t options_len,
70                           struct shash *args);
71 };
72
73 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
74
75 static int netdev_vport_create(const struct netdev_class *, const char *,
76                                struct netdev_dev **);
77 static void netdev_vport_poll_notify(const struct netdev *);
78 static int tnl_port_config_from_nlattr(const struct nlattr *options,
79                                        size_t options_len,
80                                        struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1]);
81
82 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
83
84 static bool
85 is_vport_class(const struct netdev_class *class)
86 {
87     return class->create == netdev_vport_create;
88 }
89
90 static const struct vport_class *
91 vport_class_cast(const struct netdev_class *class)
92 {
93     assert(is_vport_class(class));
94     return CONTAINER_OF(class, struct vport_class, netdev_class);
95 }
96
97 static struct netdev_dev_vport *
98 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
99 {
100     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
101     return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
102 }
103
104 static struct netdev_vport *
105 netdev_vport_cast(const struct netdev *netdev)
106 {
107     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
108     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
109     return CONTAINER_OF(netdev, struct netdev_vport, netdev);
110 }
111
112 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
113  * options to include in OVS_VPORT_ATTR_OPTIONS for configuring that vport.
114  * Otherwise returns NULL. */
115 const struct ofpbuf *
116 netdev_vport_get_options(const struct netdev *netdev)
117 {
118     const struct netdev_dev *dev = netdev_get_dev(netdev);
119
120     return (is_vport_class(netdev_dev_get_class(dev))
121             ? netdev_dev_vport_cast(dev)->options
122             : NULL);
123 }
124
125 enum ovs_vport_type
126 netdev_vport_get_vport_type(const struct netdev *netdev)
127 {
128     const struct netdev_dev *dev = netdev_get_dev(netdev);
129     const struct netdev_class *class = netdev_dev_get_class(dev);
130
131     return (is_vport_class(class) ? vport_class_cast(class)->type
132             : class == &netdev_internal_class ? OVS_VPORT_TYPE_INTERNAL
133             : class == &netdev_linux_class ? OVS_VPORT_TYPE_NETDEV
134             : OVS_VPORT_TYPE_UNSPEC);
135 }
136
137 const char *
138 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
139 {
140     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
141
142     switch (vport->type) {
143     case OVS_VPORT_TYPE_UNSPEC:
144         break;
145
146     case OVS_VPORT_TYPE_NETDEV:
147         return "system";
148
149     case OVS_VPORT_TYPE_INTERNAL:
150         return "internal";
151
152     case OVS_VPORT_TYPE_PATCH:
153         return "patch";
154
155     case OVS_VPORT_TYPE_GRE:
156         if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
157                                         a)) {
158             break;
159         }
160         return (nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
161                 ? "ipsec_gre" : "gre");
162
163     case OVS_VPORT_TYPE_CAPWAP:
164         return "capwap";
165
166     case __OVS_VPORT_TYPE_MAX:
167         break;
168     }
169
170     VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
171                  vport->dp_ifindex, vport->name, (unsigned int) vport->type);
172     return "unknown";
173 }
174
175 static int
176 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
177                     struct netdev_dev **netdev_devp)
178 {
179     struct netdev_dev_vport *dev;
180
181     dev = xmalloc(sizeof *dev);
182     netdev_dev_init(&dev->netdev_dev, name, netdev_class);
183     dev->options = NULL;
184     dev->dp_ifindex = -1;
185     dev->port_no = UINT32_MAX;
186     dev->change_seq = 1;
187
188     *netdev_devp = &dev->netdev_dev;
189     route_table_register();
190
191     return 0;
192 }
193
194 static void
195 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
196 {
197     struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
198
199     route_table_unregister();
200     free(netdev_dev);
201 }
202
203 static int
204 netdev_vport_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
205 {
206     struct netdev_vport *netdev;
207
208     netdev = xmalloc(sizeof *netdev);
209     netdev_init(&netdev->netdev, netdev_dev_);
210
211     *netdevp = &netdev->netdev;
212     return 0;
213 }
214
215 static void
216 netdev_vport_close(struct netdev *netdev_)
217 {
218     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
219     free(netdev);
220 }
221
222 static int
223 netdev_vport_get_config(struct netdev_dev *dev_, struct shash *args)
224 {
225     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
226     const struct vport_class *vport_class = vport_class_cast(netdev_class);
227     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
228     const char *name = netdev_dev_get_name(dev_);
229     int error;
230
231     if (!dev->options) {
232         struct dpif_linux_vport reply;
233         struct ofpbuf *buf;
234
235         error = dpif_linux_vport_get(name, &reply, &buf);
236         if (error) {
237             VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
238                         name, strerror(error));
239             return error;
240         }
241
242         dev->options = ofpbuf_clone_data(reply.options, reply.options_len);
243         dev->dp_ifindex = reply.dp_ifindex;
244         dev->port_no = reply.port_no;
245         ofpbuf_delete(buf);
246     }
247
248     error = vport_class->unparse_config(name, netdev_class->type,
249                                         dev->options->data,
250                                         dev->options->size,
251                                         args);
252     if (error) {
253         VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
254                     name, strerror(error));
255     }
256     return error;
257 }
258
259 static int
260 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
261 {
262     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
263     const struct vport_class *vport_class = vport_class_cast(netdev_class);
264     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
265     const char *name = netdev_dev_get_name(dev_);
266     struct ofpbuf *options;
267     int error;
268
269     options = ofpbuf_new(64);
270     error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
271                                       args, options);
272     if (!error
273         && (!dev->options
274             || options->size != dev->options->size
275             || memcmp(options->data, dev->options->data, options->size))) {
276         struct dpif_linux_vport vport;
277
278         dpif_linux_vport_init(&vport);
279         vport.cmd = OVS_VPORT_CMD_SET;
280         vport.name = name;
281         vport.options = options->data;
282         vport.options_len = options->size;
283         error = dpif_linux_vport_transact(&vport, NULL, NULL);
284         if (!error || error == ENODEV) {
285             /* Either reconfiguration succeeded or this vport is not installed
286              * in the kernel (e.g. it hasn't been added to a dpif yet with
287              * dpif_port_add()). */
288             ofpbuf_delete(dev->options);
289             dev->options = options;
290             options = NULL;
291             error = 0;
292         }
293     }
294     ofpbuf_delete(options);
295
296     return error;
297 }
298
299 static int
300 netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
301 {
302     struct netdev_dev *dev_ = netdev_get_dev(netdev);
303     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
304
305     if (dev->dp_ifindex == -1) {
306         const char *name = netdev_get_name(netdev);
307         struct dpif_linux_vport reply;
308         struct ofpbuf *buf;
309         int error;
310
311         error = dpif_linux_vport_get(name, &reply, &buf);
312         if (error) {
313             VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
314                         name, strerror(error));
315             return error;
316         }
317         dev->dp_ifindex = reply.dp_ifindex;
318         dev->port_no = reply.port_no;
319         ofpbuf_delete(buf);
320     }
321
322     return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
323 }
324
325 static int
326 netdev_vport_set_etheraddr(struct netdev *netdev,
327                            const uint8_t mac[ETH_ADDR_LEN])
328 {
329     struct dpif_linux_vport vport;
330     int error;
331
332     dpif_linux_vport_init(&vport);
333     vport.cmd = OVS_VPORT_CMD_SET;
334     vport.name = netdev_get_name(netdev);
335     vport.address = mac;
336
337     error = dpif_linux_vport_transact(&vport, NULL, NULL);
338     if (!error) {
339         netdev_vport_poll_notify(netdev);
340     }
341     return error;
342 }
343
344 static int
345 netdev_vport_get_etheraddr(const struct netdev *netdev,
346                            uint8_t mac[ETH_ADDR_LEN])
347 {
348     struct dpif_linux_vport reply;
349     struct ofpbuf *buf;
350     int error;
351
352     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
353     if (!error) {
354         if (reply.address) {
355             memcpy(mac, reply.address, ETH_ADDR_LEN);
356         } else {
357             error = EOPNOTSUPP;
358         }
359         ofpbuf_delete(buf);
360     }
361     return error;
362 }
363
364 #define COPY_OVS_STATS                                      \
365     dst->rx_packets = src->rx_packets;                      \
366     dst->tx_packets = src->tx_packets;                      \
367     dst->rx_bytes = src->rx_bytes;                          \
368     dst->tx_bytes = src->tx_bytes;                          \
369     dst->rx_errors = src->rx_errors;                        \
370     dst->tx_errors = src->tx_errors;                        \
371     dst->rx_dropped = src->rx_dropped;                      \
372     dst->tx_dropped = src->tx_dropped;
373
374 /* Copies 'src' into 'dst', performing format conversion in the process. */
375 static void
376 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
377                                   const struct ovs_vport_stats *src)
378 {
379     COPY_OVS_STATS
380     dst->multicast = 0;
381     dst->collisions = 0;
382     dst->rx_length_errors = 0;
383     dst->rx_over_errors = 0;
384     dst->rx_crc_errors = 0;
385     dst->rx_frame_errors = 0;
386     dst->rx_fifo_errors = 0;
387     dst->rx_missed_errors = 0;
388     dst->tx_aborted_errors = 0;
389     dst->tx_carrier_errors = 0;
390     dst->tx_fifo_errors = 0;
391     dst->tx_heartbeat_errors = 0;
392     dst->tx_window_errors = 0;
393 }
394
395 /* Copies 'src' into 'dst', performing format conversion in the process. */
396 static void
397 netdev_stats_to_ovs_vport_stats(struct ovs_vport_stats *dst,
398                                 const struct netdev_stats *src)
399 {
400     COPY_OVS_STATS
401 }
402
403 int
404 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
405 {
406     struct dpif_linux_vport reply;
407     struct ofpbuf *buf;
408     int error;
409
410     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
411     if (error) {
412         return error;
413     } else if (!reply.stats) {
414         ofpbuf_delete(buf);
415         return EOPNOTSUPP;
416     }
417
418     netdev_stats_from_ovs_vport_stats(stats, reply.stats);
419
420     ofpbuf_delete(buf);
421
422     return 0;
423 }
424
425 int
426 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
427 {
428     struct ovs_vport_stats rtnl_stats;
429     struct dpif_linux_vport vport;
430     int err;
431
432     netdev_stats_to_ovs_vport_stats(&rtnl_stats, stats);
433
434     dpif_linux_vport_init(&vport);
435     vport.cmd = OVS_VPORT_CMD_SET;
436     vport.name = netdev_get_name(netdev);
437     vport.stats = &rtnl_stats;
438
439     err = dpif_linux_vport_transact(&vport, NULL, NULL);
440
441     /* If the vport layer doesn't know about the device, that doesn't mean it
442      * doesn't exist (after all were able to open it when netdev_open() was
443      * called), it just means that it isn't attached and we'll be getting
444      * stats a different way. */
445     if (err == ENODEV) {
446         err = EOPNOTSUPP;
447     }
448
449     return err;
450 }
451
452 static int
453 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
454 {
455     const char *iface = netdev_vport_get_tnl_iface(netdev);
456
457     if (iface) {
458         struct netdev *egress_netdev;
459
460         shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
461
462         if (!netdev_open(iface, "system", &egress_netdev)) {
463             shash_add(sh, "tunnel_egress_iface_carrier",
464                       xstrdup(netdev_get_carrier(egress_netdev)
465                               ? "up" : "down"));
466             netdev_close(egress_netdev);
467         }
468     }
469
470     return 0;
471 }
472
473 static int
474 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
475                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
476                         enum netdev_flags *old_flagsp)
477 {
478     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
479         return EOPNOTSUPP;
480     }
481
482     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
483     return 0;
484 }
485
486 static unsigned int
487 netdev_vport_change_seq(const struct netdev *netdev)
488 {
489     return netdev_dev_vport_cast(netdev_get_dev(netdev))->change_seq;
490 }
491
492 static void
493 netdev_vport_run(void)
494 {
495     route_table_run();
496 }
497
498 static void
499 netdev_vport_wait(void)
500 {
501     route_table_wait();
502 }
503 \f
504 /* get_tnl_iface() implementation. */
505 static const char *
506 netdev_vport_get_tnl_iface(const struct netdev *netdev)
507 {
508     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
509     ovs_be32 route;
510     struct netdev_dev_vport *ndv;
511     static char name[IFNAMSIZ];
512
513     ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
514     if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
515                                     a)) {
516         return NULL;
517     }
518     route = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
519
520     if (route_table_get_name(route, name)) {
521         return name;
522     }
523
524     return NULL;
525 }
526 \f
527 /* Helper functions. */
528
529 static void
530 netdev_vport_poll_notify(const struct netdev *netdev)
531 {
532     struct netdev_dev_vport *ndv;
533
534     ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
535
536     ndv->change_seq++;
537     if (!ndv->change_seq) {
538         ndv->change_seq++;
539     }
540 }
541 \f
542 /* Code specific to individual vport types. */
543
544 static void
545 set_key(const struct shash *args, const char *name, uint16_t type,
546         struct ofpbuf *options)
547 {
548     const char *s;
549
550     s = shash_find_data(args, name);
551     if (!s) {
552         s = shash_find_data(args, "key");
553         if (!s) {
554             s = "0";
555         }
556     }
557
558     if (!strcmp(s, "flow")) {
559         /* This is the default if no attribute is present. */
560     } else {
561         nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
562     }
563 }
564
565 static int
566 parse_tunnel_config(const char *name, const char *type,
567                     const struct shash *args, struct ofpbuf *options)
568 {
569     bool is_gre = false;
570     bool is_ipsec = false;
571     struct shash_node *node;
572     bool ipsec_mech_set = false;
573     ovs_be32 daddr = htonl(0);
574     uint32_t flags;
575
576     flags = TNL_F_DF_DEFAULT | TNL_F_PMTUD | TNL_F_HDR_CACHE;
577     if (!strcmp(type, "gre")) {
578         is_gre = true;
579     } else if (!strcmp(type, "ipsec_gre")) {
580         is_gre = true;
581         is_ipsec = true;
582         flags |= TNL_F_IPSEC;
583         flags &= ~TNL_F_HDR_CACHE;
584     }
585
586     SHASH_FOR_EACH (node, args) {
587         if (!strcmp(node->name, "remote_ip")) {
588             struct in_addr in_addr;
589             if (lookup_ip(node->data, &in_addr)) {
590                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
591             } else {
592                 daddr = in_addr.s_addr;
593             }
594         } else if (!strcmp(node->name, "local_ip")) {
595             struct in_addr in_addr;
596             if (lookup_ip(node->data, &in_addr)) {
597                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
598             } else {
599                 nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4,
600                                 in_addr.s_addr);
601             }
602         } else if (!strcmp(node->name, "tos")) {
603             if (!strcmp(node->data, "inherit")) {
604                 flags |= TNL_F_TOS_INHERIT;
605             } else {
606                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, atoi(node->data));
607             }
608         } else if (!strcmp(node->name, "ttl")) {
609             if (!strcmp(node->data, "inherit")) {
610                 flags |= TNL_F_TTL_INHERIT;
611             } else {
612                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->data));
613             }
614         } else if (!strcmp(node->name, "csum") && is_gre) {
615             if (!strcmp(node->data, "true")) {
616                 flags |= TNL_F_CSUM;
617             }
618         } else if (!strcmp(node->name, "df_inherit")) {
619             if (!strcmp(node->data, "true")) {
620                 flags |= TNL_F_DF_INHERIT;
621             }
622         } else if (!strcmp(node->name, "df_default")) {
623             if (!strcmp(node->data, "false")) {
624                 flags &= ~TNL_F_DF_DEFAULT;
625             }
626         } else if (!strcmp(node->name, "pmtud")) {
627             if (!strcmp(node->data, "false")) {
628                 flags &= ~TNL_F_PMTUD;
629             }
630         } else if (!strcmp(node->name, "header_cache")) {
631             if (!strcmp(node->data, "false")) {
632                 flags &= ~TNL_F_HDR_CACHE;
633             }
634         } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
635             if (shash_find(args, "certificate")) {
636                 ipsec_mech_set = true;
637             } else {
638                 const char *use_ssl_cert;
639
640                 /* If the "use_ssl_cert" is true, then "certificate" and
641                  * "private_key" will be pulled from the SSL table.  The
642                  * use of this option is strongly discouraged, since it
643                  * will like be removed when multiple SSL configurations
644                  * are supported by OVS.
645                  */
646                 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
647                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
648                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
649                              name);
650                     return EINVAL;
651                 }
652                 ipsec_mech_set = true;
653             }
654         } else if (!strcmp(node->name, "psk") && is_ipsec) {
655             ipsec_mech_set = true;
656         } else if (is_ipsec
657                 && (!strcmp(node->name, "certificate")
658                     || !strcmp(node->name, "private_key")
659                     || !strcmp(node->name, "use_ssl_cert"))) {
660             /* Ignore options not used by the netdev. */
661         } else if (!strcmp(node->name, "key") ||
662                    !strcmp(node->name, "in_key") ||
663                    !strcmp(node->name, "out_key")) {
664             /* Handled separately below. */
665         } else {
666             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
667         }
668     }
669
670     if (is_ipsec) {
671         char *file_name = xasprintf("%s/%s", ovs_rundir(),
672                 "ovs-monitor-ipsec.pid");
673         pid_t pid = read_pidfile(file_name);
674         free(file_name);
675         if (pid < 0) {
676             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
677                      name);
678             return EINVAL;
679         }
680
681         if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
682             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
683             return EINVAL;
684         }
685
686         if (!ipsec_mech_set) {
687             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
688                      name);
689             return EINVAL;
690         }
691     }
692
693     set_key(args, "in_key", OVS_TUNNEL_ATTR_IN_KEY, options);
694     set_key(args, "out_key", OVS_TUNNEL_ATTR_OUT_KEY, options);
695
696     if (!daddr) {
697         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
698                  name, type);
699         return EINVAL;
700     }
701     nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, daddr);
702
703     nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
704
705     return 0;
706 }
707
708 static int
709 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
710                             struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
711 {
712     static const struct nl_policy ovs_tunnel_policy[] = {
713         [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
714         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
715         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
716         [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
717         [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
718         [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
719         [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
720     };
721     struct ofpbuf buf;
722
723     ofpbuf_use_const(&buf, options, options_len);
724     if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
725                          a, ARRAY_SIZE(ovs_tunnel_policy))) {
726         return EINVAL;
727     }
728     return 0;
729 }
730
731 static uint64_t
732 get_be64_or_zero(const struct nlattr *a)
733 {
734     return a ? ntohll(nl_attr_get_be64(a)) : 0;
735 }
736
737 static int
738 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
739                       const struct nlattr *options, size_t options_len,
740                       struct shash *args)
741 {
742     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
743     ovs_be32 daddr;
744     uint32_t flags;
745     int error;
746
747     error = tnl_port_config_from_nlattr(options, options_len, a);
748     if (error) {
749         return error;
750     }
751
752     flags = nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]);
753     if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
754         smap_add(args, "header_cache",
755                  flags & TNL_F_HDR_CACHE ? "true" : "false");
756     }
757
758     daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
759     shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&daddr)));
760
761     if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
762         ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
763         shash_add(args, "local_ip", xasprintf(IP_FMT, IP_ARGS(&saddr)));
764     }
765
766     if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
767         smap_add(args, "key", "flow");
768     } else {
769         uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
770         uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
771
772         if (in_key && in_key == out_key) {
773             shash_add(args, "key", xasprintf("%"PRIu64, in_key));
774         } else {
775             if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
776                 smap_add(args, "in_key", "flow");
777             } else if (in_key) {
778                 shash_add(args, "in_key", xasprintf("%"PRIu64, in_key));
779             }
780
781             if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
782                 smap_add(args, "out_key", "flow");
783             } else if (out_key) {
784                 shash_add(args, "out_key", xasprintf("%"PRIu64, out_key));
785             }
786         }
787     }
788
789     if (flags & TNL_F_TTL_INHERIT) {
790         smap_add(args, "tos", "inherit");
791     } else if (a[OVS_TUNNEL_ATTR_TTL]) {
792         int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
793         shash_add(args, "tos", xasprintf("%d", ttl));
794     }
795
796     if (flags & TNL_F_TOS_INHERIT) {
797         smap_add(args, "tos", "inherit");
798     } else if (a[OVS_TUNNEL_ATTR_TOS]) {
799         int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
800         shash_add(args, "tos", xasprintf("%d", tos));
801     }
802
803     if (flags & TNL_F_CSUM) {
804         smap_add(args, "csum", "true");
805     }
806     if (flags & TNL_F_DF_INHERIT) {
807         smap_add(args, "df_inherit", "true");
808     }
809     if (!(flags & TNL_F_DF_DEFAULT)) {
810         smap_add(args, "df_default", "false");
811     }
812     if (!(flags & TNL_F_PMTUD)) {
813         smap_add(args, "pmtud", "false");
814     }
815
816     return 0;
817 }
818
819 static int
820 parse_patch_config(const char *name, const char *type OVS_UNUSED,
821                    const struct shash *args, struct ofpbuf *options)
822 {
823     const char *peer;
824
825     peer = shash_find_data(args, "peer");
826     if (!peer) {
827         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
828         return EINVAL;
829     }
830
831     if (shash_count(args) > 1) {
832         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
833         return EINVAL;
834     }
835
836     if (strlen(peer) >= IFNAMSIZ) {
837         VLOG_ERR("%s: patch 'peer' arg too long", name);
838         return EINVAL;
839     }
840
841     if (!strcmp(name, peer)) {
842         VLOG_ERR("%s: patch peer must not be self", name);
843         return EINVAL;
844     }
845
846     nl_msg_put_string(options, OVS_PATCH_ATTR_PEER, peer);
847
848     return 0;
849 }
850
851 static int
852 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
853                      const struct nlattr *options, size_t options_len,
854                      struct shash *args)
855 {
856     static const struct nl_policy ovs_patch_policy[] = {
857         [OVS_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
858                                .max_len = IFNAMSIZ,
859                                .optional = false }
860     };
861
862     struct nlattr *a[ARRAY_SIZE(ovs_patch_policy)];
863     struct ofpbuf buf;
864
865     ofpbuf_use_const(&buf, options, options_len);
866     if (!nl_policy_parse(&buf, 0, ovs_patch_policy,
867                          a, ARRAY_SIZE(ovs_patch_policy))) {
868         return EINVAL;
869     }
870
871     smap_add(args, "peer", nl_attr_get_string(a[OVS_PATCH_ATTR_PEER]));
872     return 0;
873 }
874 \f
875 #define VPORT_FUNCTIONS(GET_STATUS)                         \
876     NULL,                                                   \
877     netdev_vport_run,                                       \
878     netdev_vport_wait,                                      \
879                                                             \
880     netdev_vport_create,                                    \
881     netdev_vport_destroy,                                   \
882     netdev_vport_get_config,                                \
883     netdev_vport_set_config,                                \
884                                                             \
885     netdev_vport_open,                                      \
886     netdev_vport_close,                                     \
887                                                             \
888     NULL,                       /* enumerate */             \
889                                                             \
890     NULL,                       /* listen */                \
891     NULL,                       /* recv */                  \
892     NULL,                       /* recv_wait */             \
893     NULL,                       /* drain */                 \
894                                                             \
895     netdev_vport_send,          /* send */                  \
896     NULL,                       /* send_wait */             \
897                                                             \
898     netdev_vport_set_etheraddr,                             \
899     netdev_vport_get_etheraddr,                             \
900     NULL,                       /* get_mtu */               \
901     NULL,                       /* set_mtu */               \
902     NULL,                       /* get_ifindex */           \
903     NULL,                       /* get_carrier */           \
904     NULL,                       /* get_miimon */            \
905     netdev_vport_get_stats,                                 \
906     netdev_vport_set_stats,                                 \
907                                                             \
908     NULL,                       /* get_features */          \
909     NULL,                       /* set_advertisements */    \
910     NULL,                       /* get_vlan_vid */          \
911                                                             \
912     NULL,                       /* set_policing */          \
913     NULL,                       /* get_qos_types */         \
914     NULL,                       /* get_qos_capabilities */  \
915     NULL,                       /* get_qos */               \
916     NULL,                       /* set_qos */               \
917     NULL,                       /* get_queue */             \
918     NULL,                       /* set_queue */             \
919     NULL,                       /* delete_queue */          \
920     NULL,                       /* get_queue_stats */       \
921     NULL,                       /* dump_queues */           \
922     NULL,                       /* dump_queue_stats */      \
923                                                             \
924     NULL,                       /* get_in4 */               \
925     NULL,                       /* set_in4 */               \
926     NULL,                       /* get_in6 */               \
927     NULL,                       /* add_router */            \
928     NULL,                       /* get_next_hop */          \
929     GET_STATUS,                                             \
930     NULL,                       /* arp_lookup */            \
931                                                             \
932     netdev_vport_update_flags,                              \
933                                                             \
934     netdev_vport_change_seq
935
936 void
937 netdev_vport_register(void)
938 {
939     static const struct vport_class vport_classes[] = {
940         { OVS_VPORT_TYPE_GRE,
941           { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
942           parse_tunnel_config, unparse_tunnel_config },
943
944         { OVS_VPORT_TYPE_GRE,
945           { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
946           parse_tunnel_config, unparse_tunnel_config },
947
948         { OVS_VPORT_TYPE_CAPWAP,
949           { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
950           parse_tunnel_config, unparse_tunnel_config },
951
952         { OVS_VPORT_TYPE_PATCH,
953           { "patch", VPORT_FUNCTIONS(NULL) },
954           parse_patch_config, unparse_patch_config }
955     };
956
957     int i;
958
959     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
960         netdev_register_provider(&vport_classes[i].netdev_class);
961     }
962 }