2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "netdev-vport.h"
23 #include <sys/socket.h>
25 #include <sys/ioctl.h>
27 #include "byte-order.h"
28 #include "connectivity.h"
35 #include "netdev-provider.h"
38 #include "route-table.h"
41 #include "socket-util.h"
44 VLOG_DEFINE_THIS_MODULE(netdev_vport);
46 #define VXLAN_DST_PORT 4789
47 #define LISP_DST_PORT 4341
49 #define DEFAULT_TTL 64
54 /* Protects all members below. */
55 struct ovs_mutex mutex;
57 uint8_t etheraddr[ETH_ADDR_LEN];
58 struct netdev_stats stats;
61 struct netdev_tunnel_config tnl_cfg;
68 const char *dpif_port;
69 struct netdev_class netdev_class;
72 static int netdev_vport_construct(struct netdev *);
73 static int get_patch_config(const struct netdev *netdev, struct smap *args);
74 static int get_tunnel_config(const struct netdev *, struct smap *args);
77 is_vport_class(const struct netdev_class *class)
79 return class->construct == netdev_vport_construct;
82 static const struct vport_class *
83 vport_class_cast(const struct netdev_class *class)
85 ovs_assert(is_vport_class(class));
86 return CONTAINER_OF(class, struct vport_class, netdev_class);
89 static struct netdev_vport *
90 netdev_vport_cast(const struct netdev *netdev)
92 ovs_assert(is_vport_class(netdev_get_class(netdev)));
93 return CONTAINER_OF(netdev, struct netdev_vport, up);
96 static const struct netdev_tunnel_config *
97 get_netdev_tunnel_config(const struct netdev *netdev)
99 return &netdev_vport_cast(netdev)->tnl_cfg;
103 netdev_vport_is_patch(const struct netdev *netdev)
105 const struct netdev_class *class = netdev_get_class(netdev);
107 return class->get_config == get_patch_config;
111 netdev_vport_is_layer3(const struct netdev *dev)
113 const char *type = netdev_get_type(dev);
115 return (!strcmp("lisp", type));
119 netdev_vport_needs_dst_port(const struct netdev *dev)
121 const struct netdev_class *class = netdev_get_class(dev);
122 const char *type = netdev_get_type(dev);
124 return (class->get_config == get_tunnel_config &&
125 (!strcmp("vxlan", type) || !strcmp("lisp", type)));
129 netdev_vport_class_get_dpif_port(const struct netdev_class *class)
131 return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
135 netdev_vport_get_dpif_port(const struct netdev *netdev,
136 char namebuf[], size_t bufsize)
138 if (netdev_vport_needs_dst_port(netdev)) {
139 const struct netdev_vport *vport = netdev_vport_cast(netdev);
140 const char *type = netdev_get_type(netdev);
143 * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
144 * or LISP port name below is 15 or 14 bytes respectively. Still,
145 * assert here on the size of strlen(type) in case that changes
148 BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
149 ovs_assert(strlen(type) + 10 < IFNAMSIZ);
150 snprintf(namebuf, bufsize, "%s_sys_%d", type,
151 ntohs(vport->tnl_cfg.dst_port));
154 const struct netdev_class *class = netdev_get_class(netdev);
155 const char *dpif_port = netdev_vport_class_get_dpif_port(class);
156 return dpif_port ? dpif_port : netdev_get_name(netdev);
161 netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
163 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
165 return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
169 static struct netdev *
170 netdev_vport_alloc(void)
172 struct netdev_vport *netdev = xzalloc(sizeof *netdev);
177 netdev_vport_construct(struct netdev *netdev_)
179 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
181 ovs_mutex_init(&netdev->mutex);
182 eth_addr_random(netdev->etheraddr);
184 route_table_register();
190 netdev_vport_destruct(struct netdev *netdev_)
192 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
194 route_table_unregister();
196 ovs_mutex_destroy(&netdev->mutex);
200 netdev_vport_dealloc(struct netdev *netdev_)
202 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
207 netdev_vport_set_etheraddr(struct netdev *netdev_,
208 const uint8_t mac[ETH_ADDR_LEN])
210 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
212 ovs_mutex_lock(&netdev->mutex);
213 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
214 ovs_mutex_unlock(&netdev->mutex);
215 seq_change(connectivity_seq_get());
221 netdev_vport_get_etheraddr(const struct netdev *netdev_,
222 uint8_t mac[ETH_ADDR_LEN])
224 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
226 ovs_mutex_lock(&netdev->mutex);
227 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
228 ovs_mutex_unlock(&netdev->mutex);
234 tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
236 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
237 char iface[IFNAMSIZ];
240 ovs_mutex_lock(&netdev->mutex);
241 route = netdev->tnl_cfg.ip_dst;
242 ovs_mutex_unlock(&netdev->mutex);
244 if (route_table_get_name(route, iface)) {
245 struct netdev *egress_netdev;
247 smap_add(smap, "tunnel_egress_iface", iface);
249 if (!netdev_open(iface, "system", &egress_netdev)) {
250 smap_add(smap, "tunnel_egress_iface_carrier",
251 netdev_get_carrier(egress_netdev) ? "up" : "down");
252 netdev_close(egress_netdev);
260 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
261 enum netdev_flags off,
262 enum netdev_flags on OVS_UNUSED,
263 enum netdev_flags *old_flagsp)
265 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
269 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
274 netdev_vport_run(void)
280 netdev_vport_wait(void)
285 /* Code specific to tunnel types. */
288 parse_key(const struct smap *args, const char *name,
289 bool *present, bool *flow)
296 s = smap_get(args, name);
298 s = smap_get(args, "key");
306 if (!strcmp(s, "flow")) {
310 return htonll(strtoull(s, NULL, 0));
315 set_tunnel_config(struct netdev *dev_, const struct smap *args)
317 struct netdev_vport *dev = netdev_vport_cast(dev_);
318 const char *name = netdev_get_name(dev_);
319 const char *type = netdev_get_type(dev_);
320 bool ipsec_mech_set, needs_dst_port, has_csum;
321 struct netdev_tunnel_config tnl_cfg;
322 struct smap_node *node;
324 has_csum = strstr(type, "gre");
325 ipsec_mech_set = false;
326 memset(&tnl_cfg, 0, sizeof tnl_cfg);
328 needs_dst_port = netdev_vport_needs_dst_port(dev_);
329 tnl_cfg.ipsec = strstr(type, "ipsec");
330 tnl_cfg.dont_fragment = true;
332 SMAP_FOR_EACH (node, args) {
333 if (!strcmp(node->key, "remote_ip")) {
334 struct in_addr in_addr;
335 if (!strcmp(node->value, "flow")) {
336 tnl_cfg.ip_dst_flow = true;
337 tnl_cfg.ip_dst = htonl(0);
338 } else if (lookup_ip(node->value, &in_addr)) {
339 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
340 } else if (ip_is_multicast(in_addr.s_addr)) {
341 VLOG_WARN("%s: multicast remote_ip="IP_FMT" not allowed",
342 name, IP_ARGS(in_addr.s_addr));
345 tnl_cfg.ip_dst = in_addr.s_addr;
347 } else if (!strcmp(node->key, "local_ip")) {
348 struct in_addr in_addr;
349 if (!strcmp(node->value, "flow")) {
350 tnl_cfg.ip_src_flow = true;
351 tnl_cfg.ip_src = htonl(0);
352 } else if (lookup_ip(node->value, &in_addr)) {
353 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
355 tnl_cfg.ip_src = in_addr.s_addr;
357 } else if (!strcmp(node->key, "tos")) {
358 if (!strcmp(node->value, "inherit")) {
359 tnl_cfg.tos_inherit = true;
363 tos = strtol(node->value, &endptr, 0);
364 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
367 VLOG_WARN("%s: invalid TOS %s", name, node->value);
370 } else if (!strcmp(node->key, "ttl")) {
371 if (!strcmp(node->value, "inherit")) {
372 tnl_cfg.ttl_inherit = true;
374 tnl_cfg.ttl = atoi(node->value);
376 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
377 tnl_cfg.dst_port = htons(atoi(node->value));
378 } else if (!strcmp(node->key, "csum") && has_csum) {
379 if (!strcmp(node->value, "true")) {
382 } else if (!strcmp(node->key, "df_default")) {
383 if (!strcmp(node->value, "false")) {
384 tnl_cfg.dont_fragment = false;
386 } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
387 if (smap_get(args, "certificate")) {
388 ipsec_mech_set = true;
390 const char *use_ssl_cert;
392 /* If the "use_ssl_cert" is true, then "certificate" and
393 * "private_key" will be pulled from the SSL table. The
394 * use of this option is strongly discouraged, since it
395 * will like be removed when multiple SSL configurations
396 * are supported by OVS.
398 use_ssl_cert = smap_get(args, "use_ssl_cert");
399 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
400 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
404 ipsec_mech_set = true;
406 } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
407 ipsec_mech_set = true;
408 } else if (tnl_cfg.ipsec
409 && (!strcmp(node->key, "certificate")
410 || !strcmp(node->key, "private_key")
411 || !strcmp(node->key, "use_ssl_cert"))) {
412 /* Ignore options not used by the netdev. */
413 } else if (!strcmp(node->key, "key") ||
414 !strcmp(node->key, "in_key") ||
415 !strcmp(node->key, "out_key")) {
416 /* Handled separately below. */
418 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
422 /* Add a default destination port for VXLAN if none specified. */
423 if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) {
424 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
427 /* Add a default destination port for LISP if none specified. */
428 if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) {
429 tnl_cfg.dst_port = htons(LISP_DST_PORT);
433 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
434 static pid_t pid = 0;
436 ovs_mutex_lock(&mutex);
438 char *file_name = xasprintf("%s/%s", ovs_rundir(),
439 "ovs-monitor-ipsec.pid");
440 pid = read_pidfile(file_name);
443 ovs_mutex_unlock(&mutex);
446 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
451 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
452 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
456 if (!ipsec_mech_set) {
457 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
463 if (!tnl_cfg.ip_dst && !tnl_cfg.ip_dst_flow) {
464 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
468 if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
469 VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
474 tnl_cfg.ttl = DEFAULT_TTL;
477 tnl_cfg.in_key = parse_key(args, "in_key",
478 &tnl_cfg.in_key_present,
479 &tnl_cfg.in_key_flow);
481 tnl_cfg.out_key = parse_key(args, "out_key",
482 &tnl_cfg.out_key_present,
483 &tnl_cfg.out_key_flow);
485 ovs_mutex_lock(&dev->mutex);
486 dev->tnl_cfg = tnl_cfg;
487 seq_change(connectivity_seq_get());
488 ovs_mutex_unlock(&dev->mutex);
494 get_tunnel_config(const struct netdev *dev, struct smap *args)
496 struct netdev_vport *netdev = netdev_vport_cast(dev);
497 struct netdev_tunnel_config tnl_cfg;
499 ovs_mutex_lock(&netdev->mutex);
500 tnl_cfg = netdev->tnl_cfg;
501 ovs_mutex_unlock(&netdev->mutex);
503 if (tnl_cfg.ip_dst) {
504 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_dst));
505 } else if (tnl_cfg.ip_dst_flow) {
506 smap_add(args, "remote_ip", "flow");
509 if (tnl_cfg.ip_src) {
510 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_src));
511 } else if (tnl_cfg.ip_src_flow) {
512 smap_add(args, "local_ip", "flow");
515 if (tnl_cfg.in_key_flow && tnl_cfg.out_key_flow) {
516 smap_add(args, "key", "flow");
517 } else if (tnl_cfg.in_key_present && tnl_cfg.out_key_present
518 && tnl_cfg.in_key == tnl_cfg.out_key) {
519 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg.in_key));
521 if (tnl_cfg.in_key_flow) {
522 smap_add(args, "in_key", "flow");
523 } else if (tnl_cfg.in_key_present) {
524 smap_add_format(args, "in_key", "%"PRIu64,
525 ntohll(tnl_cfg.in_key));
528 if (tnl_cfg.out_key_flow) {
529 smap_add(args, "out_key", "flow");
530 } else if (tnl_cfg.out_key_present) {
531 smap_add_format(args, "out_key", "%"PRIu64,
532 ntohll(tnl_cfg.out_key));
536 if (tnl_cfg.ttl_inherit) {
537 smap_add(args, "ttl", "inherit");
538 } else if (tnl_cfg.ttl != DEFAULT_TTL) {
539 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg.ttl);
542 if (tnl_cfg.tos_inherit) {
543 smap_add(args, "tos", "inherit");
544 } else if (tnl_cfg.tos) {
545 smap_add_format(args, "tos", "0x%x", tnl_cfg.tos);
548 if (tnl_cfg.dst_port) {
549 uint16_t dst_port = ntohs(tnl_cfg.dst_port);
550 const char *type = netdev_get_type(dev);
552 if ((!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
553 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
554 smap_add_format(args, "dst_port", "%d", dst_port);
559 smap_add(args, "csum", "true");
562 if (!tnl_cfg.dont_fragment) {
563 smap_add(args, "df_default", "false");
569 /* Code specific to patch ports. */
571 /* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
572 * string that the caller must free.
574 * If 'netdev' is not a patch port, returns NULL. */
576 netdev_vport_patch_peer(const struct netdev *netdev_)
580 if (netdev_vport_is_patch(netdev_)) {
581 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
583 ovs_mutex_lock(&netdev->mutex);
585 peer = xstrdup(netdev->peer);
587 ovs_mutex_unlock(&netdev->mutex);
594 netdev_vport_inc_rx(const struct netdev *netdev,
595 const struct dpif_flow_stats *stats)
597 if (is_vport_class(netdev_get_class(netdev))) {
598 struct netdev_vport *dev = netdev_vport_cast(netdev);
600 ovs_mutex_lock(&dev->mutex);
601 dev->stats.rx_packets += stats->n_packets;
602 dev->stats.rx_bytes += stats->n_bytes;
603 ovs_mutex_unlock(&dev->mutex);
608 netdev_vport_inc_tx(const struct netdev *netdev,
609 const struct dpif_flow_stats *stats)
611 if (is_vport_class(netdev_get_class(netdev))) {
612 struct netdev_vport *dev = netdev_vport_cast(netdev);
614 ovs_mutex_lock(&dev->mutex);
615 dev->stats.tx_packets += stats->n_packets;
616 dev->stats.tx_bytes += stats->n_bytes;
617 ovs_mutex_unlock(&dev->mutex);
622 get_patch_config(const struct netdev *dev_, struct smap *args)
624 struct netdev_vport *dev = netdev_vport_cast(dev_);
626 ovs_mutex_lock(&dev->mutex);
628 smap_add(args, "peer", dev->peer);
630 ovs_mutex_unlock(&dev->mutex);
636 set_patch_config(struct netdev *dev_, const struct smap *args)
638 struct netdev_vport *dev = netdev_vport_cast(dev_);
639 const char *name = netdev_get_name(dev_);
642 peer = smap_get(args, "peer");
644 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
648 if (smap_count(args) > 1) {
649 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
653 if (!strcmp(name, peer)) {
654 VLOG_ERR("%s: patch peer must not be self", name);
658 ovs_mutex_lock(&dev->mutex);
660 dev->peer = xstrdup(peer);
661 seq_change(connectivity_seq_get());
662 ovs_mutex_unlock(&dev->mutex);
668 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
670 struct netdev_vport *dev = netdev_vport_cast(netdev);
672 ovs_mutex_lock(&dev->mutex);
674 ovs_mutex_unlock(&dev->mutex);
679 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
680 GET_TUNNEL_CONFIG, GET_STATUS) \
685 netdev_vport_alloc, \
686 netdev_vport_construct, \
687 netdev_vport_destruct, \
688 netdev_vport_dealloc, \
694 NULL, /* send_wait */ \
696 netdev_vport_set_etheraddr, \
697 netdev_vport_get_etheraddr, \
698 NULL, /* get_mtu */ \
699 NULL, /* set_mtu */ \
700 NULL, /* get_ifindex */ \
701 NULL, /* get_carrier */ \
702 NULL, /* get_carrier_resets */ \
703 NULL, /* get_miimon */ \
705 NULL, /* set_stats */ \
707 NULL, /* get_features */ \
708 NULL, /* set_advertisements */ \
710 NULL, /* set_policing */ \
711 NULL, /* get_qos_types */ \
712 NULL, /* get_qos_capabilities */ \
713 NULL, /* get_qos */ \
714 NULL, /* set_qos */ \
715 NULL, /* get_queue */ \
716 NULL, /* set_queue */ \
717 NULL, /* delete_queue */ \
718 NULL, /* get_queue_stats */ \
719 NULL, /* queue_dump_start */ \
720 NULL, /* queue_dump_next */ \
721 NULL, /* queue_dump_done */ \
722 NULL, /* dump_queue_stats */ \
724 NULL, /* get_in4 */ \
725 NULL, /* set_in4 */ \
726 NULL, /* get_in6 */ \
727 NULL, /* add_router */ \
728 NULL, /* get_next_hop */ \
730 NULL, /* arp_lookup */ \
732 netdev_vport_update_flags, \
734 NULL, /* rx_alloc */ \
735 NULL, /* rx_construct */ \
736 NULL, /* rx_destruct */ \
737 NULL, /* rx_dealloc */ \
738 NULL, /* rx_recv */ \
739 NULL, /* rx_wait */ \
742 #define TUNNEL_CLASS(NAME, DPIF_PORT) \
744 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
746 get_netdev_tunnel_config, \
747 tunnel_get_status) }}
750 netdev_vport_tunnel_register(void)
752 static const struct vport_class vport_classes[] = {
753 TUNNEL_CLASS("gre", "gre_system"),
754 TUNNEL_CLASS("ipsec_gre", "gre_system"),
755 TUNNEL_CLASS("gre64", "gre64_system"),
756 TUNNEL_CLASS("ipsec_gre64", "gre64_system"),
757 TUNNEL_CLASS("vxlan", "vxlan_system"),
758 TUNNEL_CLASS("lisp", "lisp_system")
760 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
762 if (ovsthread_once_start(&once)) {
765 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
766 netdev_register_provider(&vport_classes[i].netdev_class);
768 ovsthread_once_done(&once);
773 netdev_vport_patch_register(void)
775 static const struct vport_class patch_class =
777 { "patch", VPORT_FUNCTIONS(get_patch_config,
781 netdev_register_provider(&patch_class.netdev_class);