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