netdev-vport: Checks tunnel status change when route-table is reset.
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013 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 <net/if.h>
25 #include <sys/ioctl.h>
26
27 #include "byte-order.h"
28 #include "daemon.h"
29 #include "dirs.h"
30 #include "dpif.h"
31 #include "hash.h"
32 #include "hmap.h"
33 #include "list.h"
34 #include "netdev-provider.h"
35 #include "ofpbuf.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "route-table.h"
39 #include "shash.h"
40 #include "socket-util.h"
41 #include "vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(netdev_vport);
44
45 #define VXLAN_DST_PORT 4789
46 #define LISP_DST_PORT 4341
47
48 #define DEFAULT_TTL 64
49
50 struct netdev_vport {
51     struct netdev up;
52
53     /* Protects all members below. */
54     struct ovs_mutex mutex;
55
56     uint8_t etheraddr[ETH_ADDR_LEN];
57     struct netdev_stats stats;
58
59     /* Tunnels. */
60     struct netdev_tunnel_config tnl_cfg;
61     char egress_iface[IFNAMSIZ];
62     bool carrier_status;
63
64     /* Patch Ports. */
65     char *peer;
66 };
67
68 struct vport_class {
69     const char *dpif_port;
70     struct netdev_class netdev_class;
71 };
72
73 /* Last read of the route-table's change number. */
74 static uint64_t rt_change_seqno;
75
76 static int netdev_vport_construct(struct netdev *);
77 static int get_patch_config(const struct netdev *netdev, struct smap *args);
78 static int get_tunnel_config(const struct netdev *, struct smap *args);
79 static bool tunnel_check_status_change__(struct netdev_vport *);
80
81 static bool
82 is_vport_class(const struct netdev_class *class)
83 {
84     return class->construct == netdev_vport_construct;
85 }
86
87 bool
88 netdev_vport_is_vport_class(const struct netdev_class *class)
89 {
90     return is_vport_class(class);
91 }
92
93 static const struct vport_class *
94 vport_class_cast(const struct netdev_class *class)
95 {
96     ovs_assert(is_vport_class(class));
97     return CONTAINER_OF(class, struct vport_class, netdev_class);
98 }
99
100 static struct netdev_vport *
101 netdev_vport_cast(const struct netdev *netdev)
102 {
103     ovs_assert(is_vport_class(netdev_get_class(netdev)));
104     return CONTAINER_OF(netdev, struct netdev_vport, up);
105 }
106
107 static const struct netdev_tunnel_config *
108 get_netdev_tunnel_config(const struct netdev *netdev)
109 {
110     return &netdev_vport_cast(netdev)->tnl_cfg;
111 }
112
113 bool
114 netdev_vport_is_patch(const struct netdev *netdev)
115 {
116     const struct netdev_class *class = netdev_get_class(netdev);
117
118     return class->get_config == get_patch_config;
119 }
120
121 bool
122 netdev_vport_is_layer3(const struct netdev *dev)
123 {
124     const char *type = netdev_get_type(dev);
125
126     return (!strcmp("lisp", type));
127 }
128
129 static bool
130 netdev_vport_needs_dst_port(const struct netdev *dev)
131 {
132     const struct netdev_class *class = netdev_get_class(dev);
133     const char *type = netdev_get_type(dev);
134
135     return (class->get_config == get_tunnel_config &&
136             (!strcmp("vxlan", type) || !strcmp("lisp", type)));
137 }
138
139 const char *
140 netdev_vport_class_get_dpif_port(const struct netdev_class *class)
141 {
142     return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
143 }
144
145 const char *
146 netdev_vport_get_dpif_port(const struct netdev *netdev,
147                            char namebuf[], size_t bufsize)
148 {
149     if (netdev_vport_needs_dst_port(netdev)) {
150         const struct netdev_vport *vport = netdev_vport_cast(netdev);
151         const char *type = netdev_get_type(netdev);
152
153         /*
154          * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
155          * or LISP port name below is 15 or 14 bytes respectively. Still,
156          * assert here on the size of strlen(type) in case that changes
157          * in the future.
158          */
159         BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
160         ovs_assert(strlen(type) + 10 < IFNAMSIZ);
161         snprintf(namebuf, bufsize, "%s_sys_%d", type,
162                  ntohs(vport->tnl_cfg.dst_port));
163         return namebuf;
164     } else {
165         const struct netdev_class *class = netdev_get_class(netdev);
166         const char *dpif_port = netdev_vport_class_get_dpif_port(class);
167         return dpif_port ? dpif_port : netdev_get_name(netdev);
168     }
169 }
170
171 char *
172 netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
173 {
174     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
175
176     return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
177                                               sizeof namebuf));
178 }
179
180 /* Whenever the route-table change number is incremented,
181  * netdev_vport_route_changed() should be called to update
182  * the corresponding tunnel interface status. */
183 static void
184 netdev_vport_route_changed(void)
185 {
186     struct netdev **vports;
187     size_t i, n_vports;
188
189     vports = netdev_get_vports(&n_vports);
190     for (i = 0; i < n_vports; i++) {
191         struct netdev *netdev_ = vports[i];
192         struct netdev_vport *netdev = netdev_vport_cast(netdev_);
193
194         ovs_mutex_lock(&netdev->mutex);
195         /* Finds all tunnel vports. */
196         if (netdev->tnl_cfg.ip_dst) {
197             if (tunnel_check_status_change__(netdev)) {
198                 netdev_change_seq_changed(netdev_);
199             }
200         }
201         netdev_close(netdev_);
202         ovs_mutex_unlock(&netdev->mutex);
203     }
204
205     free(vports);
206 }
207
208 static struct netdev *
209 netdev_vport_alloc(void)
210 {
211     struct netdev_vport *netdev = xzalloc(sizeof *netdev);
212     return &netdev->up;
213 }
214
215 static int
216 netdev_vport_construct(struct netdev *netdev_)
217 {
218     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
219
220     ovs_mutex_init(&netdev->mutex);
221     eth_addr_random(netdev->etheraddr);
222
223     route_table_register();
224
225     return 0;
226 }
227
228 static void
229 netdev_vport_destruct(struct netdev *netdev_)
230 {
231     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
232
233     route_table_unregister();
234     free(netdev->peer);
235     ovs_mutex_destroy(&netdev->mutex);
236 }
237
238 static void
239 netdev_vport_dealloc(struct netdev *netdev_)
240 {
241     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
242     free(netdev);
243 }
244
245 static int
246 netdev_vport_set_etheraddr(struct netdev *netdev_,
247                            const uint8_t mac[ETH_ADDR_LEN])
248 {
249     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
250
251     ovs_mutex_lock(&netdev->mutex);
252     memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
253     ovs_mutex_unlock(&netdev->mutex);
254     netdev_change_seq_changed(netdev_);
255
256     return 0;
257 }
258
259 static int
260 netdev_vport_get_etheraddr(const struct netdev *netdev_,
261                            uint8_t mac[ETH_ADDR_LEN])
262 {
263     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
264
265     ovs_mutex_lock(&netdev->mutex);
266     memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
267     ovs_mutex_unlock(&netdev->mutex);
268
269     return 0;
270 }
271
272 /* Checks if the tunnel status has changed and returns a boolean.
273  * Updates the tunnel status if it has changed. */
274 static bool
275 tunnel_check_status_change__(struct netdev_vport *netdev)
276     OVS_REQUIRES(netdev->mutex)
277 {
278     char iface[IFNAMSIZ];
279     bool status = false;
280     ovs_be32 route;
281
282     iface[0] = '\0';
283     route = netdev->tnl_cfg.ip_dst;
284     if (route_table_get_name(route, iface)) {
285         struct netdev *egress_netdev;
286
287         if (!netdev_open(iface, "system", &egress_netdev)) {
288             status = netdev_get_carrier(egress_netdev);
289             netdev_close(egress_netdev);
290         }
291     }
292
293     if (strcmp(netdev->egress_iface, iface)
294         || netdev->carrier_status != status) {
295         ovs_strlcpy(netdev->egress_iface, iface, IFNAMSIZ);
296         netdev->carrier_status = status;
297
298         return true;
299     }
300
301     return false;
302 }
303
304 static int
305 tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
306 {
307     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
308
309     if (netdev->egress_iface[0]) {
310         smap_add(smap, "tunnel_egress_iface", netdev->egress_iface);
311
312         smap_add(smap, "tunnel_egress_iface_carrier",
313                  netdev->carrier_status ? "up" : "down");
314     }
315
316     return 0;
317 }
318
319 static int
320 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
321                           enum netdev_flags off,
322                           enum netdev_flags on OVS_UNUSED,
323                           enum netdev_flags *old_flagsp)
324 {
325     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
326         return EOPNOTSUPP;
327     }
328
329     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
330     return 0;
331 }
332
333 static void
334 netdev_vport_run(void)
335 {
336     uint64_t seq;
337
338     route_table_run();
339     seq = route_table_get_change_seq();
340     if (rt_change_seqno != seq) {
341         rt_change_seqno = seq;
342         netdev_vport_route_changed();
343     }
344 }
345
346 static void
347 netdev_vport_wait(void)
348 {
349     uint64_t seq;
350
351     route_table_wait();
352     seq = route_table_get_change_seq();
353     if (rt_change_seqno != seq) {
354         poll_immediate_wake();
355     }
356 }
357 \f
358 /* Code specific to tunnel types. */
359
360 static ovs_be64
361 parse_key(const struct smap *args, const char *name,
362           bool *present, bool *flow)
363 {
364     const char *s;
365
366     *present = false;
367     *flow = false;
368
369     s = smap_get(args, name);
370     if (!s) {
371         s = smap_get(args, "key");
372         if (!s) {
373             return 0;
374         }
375     }
376
377     *present = true;
378
379     if (!strcmp(s, "flow")) {
380         *flow = true;
381         return 0;
382     } else {
383         return htonll(strtoull(s, NULL, 0));
384     }
385 }
386
387 static int
388 set_tunnel_config(struct netdev *dev_, const struct smap *args)
389 {
390     struct netdev_vport *dev = netdev_vport_cast(dev_);
391     const char *name = netdev_get_name(dev_);
392     const char *type = netdev_get_type(dev_);
393     bool ipsec_mech_set, needs_dst_port, has_csum;
394     struct netdev_tunnel_config tnl_cfg;
395     struct smap_node *node;
396
397     has_csum = strstr(type, "gre");
398     ipsec_mech_set = false;
399     memset(&tnl_cfg, 0, sizeof tnl_cfg);
400
401     needs_dst_port = netdev_vport_needs_dst_port(dev_);
402     tnl_cfg.ipsec = strstr(type, "ipsec");
403     tnl_cfg.dont_fragment = true;
404
405     SMAP_FOR_EACH (node, args) {
406         if (!strcmp(node->key, "remote_ip")) {
407             struct in_addr in_addr;
408             if (!strcmp(node->value, "flow")) {
409                 tnl_cfg.ip_dst_flow = true;
410                 tnl_cfg.ip_dst = htonl(0);
411             } else if (lookup_ip(node->value, &in_addr)) {
412                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
413             } else if (ip_is_multicast(in_addr.s_addr)) {
414                 VLOG_WARN("%s: multicast remote_ip="IP_FMT" not allowed",
415                           name, IP_ARGS(in_addr.s_addr));
416                 return EINVAL;
417             } else {
418                 tnl_cfg.ip_dst = in_addr.s_addr;
419             }
420         } else if (!strcmp(node->key, "local_ip")) {
421             struct in_addr in_addr;
422             if (!strcmp(node->value, "flow")) {
423                 tnl_cfg.ip_src_flow = true;
424                 tnl_cfg.ip_src = htonl(0);
425             } else if (lookup_ip(node->value, &in_addr)) {
426                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
427             } else {
428                 tnl_cfg.ip_src = in_addr.s_addr;
429             }
430         } else if (!strcmp(node->key, "tos")) {
431             if (!strcmp(node->value, "inherit")) {
432                 tnl_cfg.tos_inherit = true;
433             } else {
434                 char *endptr;
435                 int tos;
436                 tos = strtol(node->value, &endptr, 0);
437                 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
438                     tnl_cfg.tos = tos;
439                 } else {
440                     VLOG_WARN("%s: invalid TOS %s", name, node->value);
441                 }
442             }
443         } else if (!strcmp(node->key, "ttl")) {
444             if (!strcmp(node->value, "inherit")) {
445                 tnl_cfg.ttl_inherit = true;
446             } else {
447                 tnl_cfg.ttl = atoi(node->value);
448             }
449         } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
450             tnl_cfg.dst_port = htons(atoi(node->value));
451         } else if (!strcmp(node->key, "csum") && has_csum) {
452             if (!strcmp(node->value, "true")) {
453                 tnl_cfg.csum = true;
454             }
455         } else if (!strcmp(node->key, "df_default")) {
456             if (!strcmp(node->value, "false")) {
457                 tnl_cfg.dont_fragment = false;
458             }
459         } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
460             if (smap_get(args, "certificate")) {
461                 ipsec_mech_set = true;
462             } else {
463                 const char *use_ssl_cert;
464
465                 /* If the "use_ssl_cert" is true, then "certificate" and
466                  * "private_key" will be pulled from the SSL table.  The
467                  * use of this option is strongly discouraged, since it
468                  * will like be removed when multiple SSL configurations
469                  * are supported by OVS.
470                  */
471                 use_ssl_cert = smap_get(args, "use_ssl_cert");
472                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
473                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
474                              name);
475                     return EINVAL;
476                 }
477                 ipsec_mech_set = true;
478             }
479         } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
480             ipsec_mech_set = true;
481         } else if (tnl_cfg.ipsec
482                 && (!strcmp(node->key, "certificate")
483                     || !strcmp(node->key, "private_key")
484                     || !strcmp(node->key, "use_ssl_cert"))) {
485             /* Ignore options not used by the netdev. */
486         } else if (!strcmp(node->key, "key") ||
487                    !strcmp(node->key, "in_key") ||
488                    !strcmp(node->key, "out_key")) {
489             /* Handled separately below. */
490         } else {
491             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
492         }
493     }
494
495     /* Add a default destination port for VXLAN if none specified. */
496     if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) {
497         tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
498     }
499
500     /* Add a default destination port for LISP if none specified. */
501     if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) {
502         tnl_cfg.dst_port = htons(LISP_DST_PORT);
503     }
504
505     if (tnl_cfg.ipsec) {
506         static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
507         static pid_t pid = 0;
508
509 #ifndef _WIN32
510         ovs_mutex_lock(&mutex);
511         if (pid <= 0) {
512             char *file_name = xasprintf("%s/%s", ovs_rundir(),
513                                         "ovs-monitor-ipsec.pid");
514             pid = read_pidfile(file_name);
515             free(file_name);
516         }
517         ovs_mutex_unlock(&mutex);
518 #endif
519
520         if (pid < 0) {
521             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
522                      name);
523             return EINVAL;
524         }
525
526         if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
527             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
528             return EINVAL;
529         }
530
531         if (!ipsec_mech_set) {
532             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
533                      name);
534             return EINVAL;
535         }
536     }
537
538     if (!tnl_cfg.ip_dst && !tnl_cfg.ip_dst_flow) {
539         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
540                  name, type);
541         return EINVAL;
542     }
543     if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
544         VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
545                  name, type);
546         return EINVAL;
547     }
548     if (!tnl_cfg.ttl) {
549         tnl_cfg.ttl = DEFAULT_TTL;
550     }
551
552     tnl_cfg.in_key = parse_key(args, "in_key",
553                                &tnl_cfg.in_key_present,
554                                &tnl_cfg.in_key_flow);
555
556     tnl_cfg.out_key = parse_key(args, "out_key",
557                                &tnl_cfg.out_key_present,
558                                &tnl_cfg.out_key_flow);
559
560     ovs_mutex_lock(&dev->mutex);
561     dev->tnl_cfg = tnl_cfg;
562     tunnel_check_status_change__(dev);
563     netdev_change_seq_changed(dev_);
564     ovs_mutex_unlock(&dev->mutex);
565
566     return 0;
567 }
568
569 static int
570 get_tunnel_config(const struct netdev *dev, struct smap *args)
571 {
572     struct netdev_vport *netdev = netdev_vport_cast(dev);
573     struct netdev_tunnel_config tnl_cfg;
574
575     ovs_mutex_lock(&netdev->mutex);
576     tnl_cfg = netdev->tnl_cfg;
577     ovs_mutex_unlock(&netdev->mutex);
578
579     if (tnl_cfg.ip_dst) {
580         smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_dst));
581     } else if (tnl_cfg.ip_dst_flow) {
582         smap_add(args, "remote_ip", "flow");
583     }
584
585     if (tnl_cfg.ip_src) {
586         smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_src));
587     } else if (tnl_cfg.ip_src_flow) {
588         smap_add(args, "local_ip", "flow");
589     }
590
591     if (tnl_cfg.in_key_flow && tnl_cfg.out_key_flow) {
592         smap_add(args, "key", "flow");
593     } else if (tnl_cfg.in_key_present && tnl_cfg.out_key_present
594                && tnl_cfg.in_key == tnl_cfg.out_key) {
595         smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg.in_key));
596     } else {
597         if (tnl_cfg.in_key_flow) {
598             smap_add(args, "in_key", "flow");
599         } else if (tnl_cfg.in_key_present) {
600             smap_add_format(args, "in_key", "%"PRIu64,
601                             ntohll(tnl_cfg.in_key));
602         }
603
604         if (tnl_cfg.out_key_flow) {
605             smap_add(args, "out_key", "flow");
606         } else if (tnl_cfg.out_key_present) {
607             smap_add_format(args, "out_key", "%"PRIu64,
608                             ntohll(tnl_cfg.out_key));
609         }
610     }
611
612     if (tnl_cfg.ttl_inherit) {
613         smap_add(args, "ttl", "inherit");
614     } else if (tnl_cfg.ttl != DEFAULT_TTL) {
615         smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg.ttl);
616     }
617
618     if (tnl_cfg.tos_inherit) {
619         smap_add(args, "tos", "inherit");
620     } else if (tnl_cfg.tos) {
621         smap_add_format(args, "tos", "0x%x", tnl_cfg.tos);
622     }
623
624     if (tnl_cfg.dst_port) {
625         uint16_t dst_port = ntohs(tnl_cfg.dst_port);
626         const char *type = netdev_get_type(dev);
627
628         if ((!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
629             (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
630             smap_add_format(args, "dst_port", "%d", dst_port);
631         }
632     }
633
634     if (tnl_cfg.csum) {
635         smap_add(args, "csum", "true");
636     }
637
638     if (!tnl_cfg.dont_fragment) {
639         smap_add(args, "df_default", "false");
640     }
641
642     return 0;
643 }
644 \f
645 /* Code specific to patch ports. */
646
647 /* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
648  * string that the caller must free.
649  *
650  * If 'netdev' is not a patch port, returns NULL. */
651 char *
652 netdev_vport_patch_peer(const struct netdev *netdev_)
653 {
654     char *peer = NULL;
655
656     if (netdev_vport_is_patch(netdev_)) {
657         struct netdev_vport *netdev = netdev_vport_cast(netdev_);
658
659         ovs_mutex_lock(&netdev->mutex);
660         if (netdev->peer) {
661             peer = xstrdup(netdev->peer);
662         }
663         ovs_mutex_unlock(&netdev->mutex);
664     }
665
666     return peer;
667 }
668
669 void
670 netdev_vport_inc_rx(const struct netdev *netdev,
671                     const struct dpif_flow_stats *stats)
672 {
673     if (is_vport_class(netdev_get_class(netdev))) {
674         struct netdev_vport *dev = netdev_vport_cast(netdev);
675
676         ovs_mutex_lock(&dev->mutex);
677         dev->stats.rx_packets += stats->n_packets;
678         dev->stats.rx_bytes += stats->n_bytes;
679         ovs_mutex_unlock(&dev->mutex);
680     }
681 }
682
683 void
684 netdev_vport_inc_tx(const struct netdev *netdev,
685                     const struct dpif_flow_stats *stats)
686 {
687     if (is_vport_class(netdev_get_class(netdev))) {
688         struct netdev_vport *dev = netdev_vport_cast(netdev);
689
690         ovs_mutex_lock(&dev->mutex);
691         dev->stats.tx_packets += stats->n_packets;
692         dev->stats.tx_bytes += stats->n_bytes;
693         ovs_mutex_unlock(&dev->mutex);
694     }
695 }
696
697 static int
698 get_patch_config(const struct netdev *dev_, struct smap *args)
699 {
700     struct netdev_vport *dev = netdev_vport_cast(dev_);
701
702     ovs_mutex_lock(&dev->mutex);
703     if (dev->peer) {
704         smap_add(args, "peer", dev->peer);
705     }
706     ovs_mutex_unlock(&dev->mutex);
707
708     return 0;
709 }
710
711 static int
712 set_patch_config(struct netdev *dev_, const struct smap *args)
713 {
714     struct netdev_vport *dev = netdev_vport_cast(dev_);
715     const char *name = netdev_get_name(dev_);
716     const char *peer;
717
718     peer = smap_get(args, "peer");
719     if (!peer) {
720         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
721         return EINVAL;
722     }
723
724     if (smap_count(args) > 1) {
725         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
726         return EINVAL;
727     }
728
729     if (!strcmp(name, peer)) {
730         VLOG_ERR("%s: patch peer must not be self", name);
731         return EINVAL;
732     }
733
734     ovs_mutex_lock(&dev->mutex);
735     free(dev->peer);
736     dev->peer = xstrdup(peer);
737     netdev_change_seq_changed(dev_);
738     ovs_mutex_unlock(&dev->mutex);
739
740     return 0;
741 }
742
743 static int
744 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
745 {
746     struct netdev_vport *dev = netdev_vport_cast(netdev);
747
748     ovs_mutex_lock(&dev->mutex);
749     *stats = dev->stats;
750     ovs_mutex_unlock(&dev->mutex);
751
752     return 0;
753 }
754 \f
755 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG,             \
756                         GET_TUNNEL_CONFIG, GET_STATUS)      \
757     NULL,                                                   \
758     netdev_vport_run,                                       \
759     netdev_vport_wait,                                      \
760                                                             \
761     netdev_vport_alloc,                                     \
762     netdev_vport_construct,                                 \
763     netdev_vport_destruct,                                  \
764     netdev_vport_dealloc,                                   \
765     GET_CONFIG,                                             \
766     SET_CONFIG,                                             \
767     GET_TUNNEL_CONFIG,                                      \
768                                                             \
769     NULL,                       /* send */                  \
770     NULL,                       /* send_wait */             \
771                                                             \
772     netdev_vport_set_etheraddr,                             \
773     netdev_vport_get_etheraddr,                             \
774     NULL,                       /* get_mtu */               \
775     NULL,                       /* set_mtu */               \
776     NULL,                       /* get_ifindex */           \
777     NULL,                       /* get_carrier */           \
778     NULL,                       /* get_carrier_resets */    \
779     NULL,                       /* get_miimon */            \
780     get_stats,                                              \
781     NULL,                       /* set_stats */             \
782                                                             \
783     NULL,                       /* get_features */          \
784     NULL,                       /* set_advertisements */    \
785                                                             \
786     NULL,                       /* set_policing */          \
787     NULL,                       /* get_qos_types */         \
788     NULL,                       /* get_qos_capabilities */  \
789     NULL,                       /* get_qos */               \
790     NULL,                       /* set_qos */               \
791     NULL,                       /* get_queue */             \
792     NULL,                       /* set_queue */             \
793     NULL,                       /* delete_queue */          \
794     NULL,                       /* get_queue_stats */       \
795     NULL,                       /* queue_dump_start */      \
796     NULL,                       /* queue_dump_next */       \
797     NULL,                       /* queue_dump_done */       \
798     NULL,                       /* dump_queue_stats */      \
799                                                             \
800     NULL,                       /* get_in4 */               \
801     NULL,                       /* set_in4 */               \
802     NULL,                       /* get_in6 */               \
803     NULL,                       /* add_router */            \
804     NULL,                       /* get_next_hop */          \
805     GET_STATUS,                                             \
806     NULL,                       /* arp_lookup */            \
807                                                             \
808     netdev_vport_update_flags,                              \
809                                                             \
810     NULL,                   /* rx_alloc */                  \
811     NULL,                   /* rx_construct */              \
812     NULL,                   /* rx_destruct */               \
813     NULL,                   /* rx_dealloc */                \
814     NULL,                   /* rx_recv */                   \
815     NULL,                   /* rx_wait */                   \
816     NULL,                   /* rx_drain */
817
818 #define TUNNEL_CLASS(NAME, DPIF_PORT)                       \
819     { DPIF_PORT,                                            \
820         { NAME, VPORT_FUNCTIONS(get_tunnel_config,          \
821                                 set_tunnel_config,          \
822                                 get_netdev_tunnel_config,   \
823                                 tunnel_get_status) }}
824
825 void
826 netdev_vport_tunnel_register(void)
827 {
828     static const struct vport_class vport_classes[] = {
829         TUNNEL_CLASS("gre", "gre_system"),
830         TUNNEL_CLASS("ipsec_gre", "gre_system"),
831         TUNNEL_CLASS("gre64", "gre64_system"),
832         TUNNEL_CLASS("ipsec_gre64", "gre64_system"),
833         TUNNEL_CLASS("vxlan", "vxlan_system"),
834         TUNNEL_CLASS("lisp", "lisp_system")
835     };
836     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
837
838     if (ovsthread_once_start(&once)) {
839         int i;
840
841         for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
842             netdev_register_provider(&vport_classes[i].netdev_class);
843         }
844         ovsthread_once_done(&once);
845     }
846 }
847
848 void
849 netdev_vport_patch_register(void)
850 {
851     static const struct vport_class patch_class =
852         { NULL,
853             { "patch", VPORT_FUNCTIONS(get_patch_config,
854                                        set_patch_config,
855                                        NULL,
856                                        NULL) }};
857     netdev_register_provider(&patch_class.netdev_class);
858 }