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"
34 #include "netdev-provider.h"
37 #include "route-table.h"
39 #include "socket-util.h"
42 VLOG_DEFINE_THIS_MODULE(netdev_vport);
44 /* Default to the OTV port, per the VXLAN IETF draft. */
45 #define VXLAN_DST_PORT 8472
47 #define LISP_DST_PORT 4341
49 #define DEFAULT_TTL 64
51 struct netdev_dev_vport {
52 struct netdev_dev netdev_dev;
53 unsigned int change_seq;
54 uint8_t etheraddr[ETH_ADDR_LEN];
55 struct netdev_stats stats;
58 struct netdev_tunnel_config tnl_cfg;
65 const char *dpif_port;
66 struct netdev_class netdev_class;
69 static int netdev_vport_create(const struct netdev_class *, const char *,
70 struct netdev_dev **);
71 static int get_patch_config(struct netdev_dev *, struct smap *args);
72 static int get_tunnel_config(struct netdev_dev *, struct smap *args);
73 static void netdev_vport_poll_notify(struct netdev_dev_vport *);
76 is_vport_class(const struct netdev_class *class)
78 return class->create == netdev_vport_create;
81 static const struct vport_class *
82 vport_class_cast(const struct netdev_class *class)
84 ovs_assert(is_vport_class(class));
85 return CONTAINER_OF(class, struct vport_class, netdev_class);
88 static struct netdev_dev_vport *
89 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
91 ovs_assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
92 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
95 static struct netdev_dev_vport *
96 netdev_vport_get_dev(const struct netdev *netdev)
98 return netdev_dev_vport_cast(netdev_get_dev(netdev));
101 static const struct netdev_tunnel_config *
102 get_netdev_tunnel_config(const struct netdev_dev *netdev_dev)
104 return &netdev_dev_vport_cast(netdev_dev)->tnl_cfg;
108 netdev_vport_is_patch(const struct netdev *netdev)
110 const struct netdev_dev *dev = netdev_get_dev(netdev);
111 const struct netdev_class *class = netdev_dev_get_class(dev);
113 return class->get_config == get_patch_config;
117 netdev_vport_needs_dst_port(const struct netdev_dev *dev)
119 const struct netdev_class *class = netdev_dev_get_class(dev);
120 const char *type = netdev_dev_get_type(dev);
122 return (class->get_config == get_tunnel_config &&
123 (!strcmp("vxlan", type) || !strcmp("lisp", type)));
127 netdev_vport_get_dpif_port(const struct netdev *netdev)
129 const struct netdev_dev *dev = netdev_get_dev(netdev);
130 const struct netdev_class *class = netdev_dev_get_class(dev);
131 const char *dpif_port;
133 if (netdev_vport_needs_dst_port(dev)) {
134 const struct netdev_dev_vport *vport = netdev_vport_get_dev(netdev);
135 const char *type = netdev_dev_get_type(dev);
136 static char dpif_port_combined[IFNAMSIZ];
139 * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
140 * or LISP port name below is 15 or 14 bytes respectively. Still,
141 * assert here on the size of strlen(type) in case that changes
144 ovs_assert(strlen(type) + 10 < IFNAMSIZ);
145 snprintf(dpif_port_combined, IFNAMSIZ, "%s_sys_%d", type,
146 ntohs(vport->tnl_cfg.dst_port));
147 return dpif_port_combined;
149 dpif_port = (is_vport_class(class)
150 ? vport_class_cast(class)->dpif_port
154 return dpif_port ? dpif_port : netdev_get_name(netdev);
158 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
159 struct netdev_dev **netdev_devp)
161 struct netdev_dev_vport *dev;
163 dev = xzalloc(sizeof *dev);
164 netdev_dev_init(&dev->netdev_dev, name, netdev_class);
166 eth_addr_random(dev->etheraddr);
168 *netdev_devp = &dev->netdev_dev;
169 route_table_register();
175 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
177 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
179 route_table_unregister();
180 free(netdev_dev->peer);
185 netdev_vport_open(struct netdev_dev *netdev_dev, struct netdev **netdevp)
187 *netdevp = xmalloc(sizeof **netdevp);
188 netdev_init(*netdevp, netdev_dev);
193 netdev_vport_close(struct netdev *netdev)
199 netdev_vport_set_etheraddr(struct netdev *netdev,
200 const uint8_t mac[ETH_ADDR_LEN])
202 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
203 memcpy(dev->etheraddr, mac, ETH_ADDR_LEN);
204 netdev_vport_poll_notify(dev);
209 netdev_vport_get_etheraddr(const struct netdev *netdev,
210 uint8_t mac[ETH_ADDR_LEN])
212 memcpy(mac, netdev_vport_get_dev(netdev)->etheraddr, ETH_ADDR_LEN);
217 tunnel_get_status(const struct netdev *netdev, struct smap *smap)
219 static char iface[IFNAMSIZ];
222 route = netdev_vport_get_dev(netdev)->tnl_cfg.ip_dst;
223 if (route_table_get_name(route, iface)) {
224 struct netdev *egress_netdev;
226 smap_add(smap, "tunnel_egress_iface", iface);
228 if (!netdev_open(iface, "system", &egress_netdev)) {
229 smap_add(smap, "tunnel_egress_iface_carrier",
230 netdev_get_carrier(egress_netdev) ? "up" : "down");
231 netdev_close(egress_netdev);
239 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
240 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
241 enum netdev_flags *old_flagsp)
243 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
247 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
252 netdev_vport_change_seq(const struct netdev *netdev)
254 return netdev_vport_get_dev(netdev)->change_seq;
258 netdev_vport_run(void)
264 netdev_vport_wait(void)
269 /* Helper functions. */
272 netdev_vport_poll_notify(struct netdev_dev_vport *ndv)
275 if (!ndv->change_seq) {
280 /* Code specific to tunnel types. */
283 parse_key(const struct smap *args, const char *name,
284 bool *present, bool *flow)
291 s = smap_get(args, name);
293 s = smap_get(args, "key");
301 if (!strcmp(s, "flow")) {
305 return htonll(strtoull(s, NULL, 0));
310 set_tunnel_config(struct netdev_dev *dev_, const struct smap *args)
312 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
313 const char *name = netdev_dev_get_name(dev_);
314 const char *type = netdev_dev_get_type(dev_);
315 bool ipsec_mech_set, needs_dst_port, has_csum;
316 struct netdev_tunnel_config tnl_cfg;
317 struct smap_node *node;
319 has_csum = strstr(type, "gre");
320 ipsec_mech_set = false;
321 memset(&tnl_cfg, 0, sizeof tnl_cfg);
323 needs_dst_port = netdev_vport_needs_dst_port(dev_);
324 tnl_cfg.ipsec = strstr(type, "ipsec");
325 tnl_cfg.dont_fragment = true;
327 SMAP_FOR_EACH (node, args) {
328 if (!strcmp(node->key, "remote_ip")) {
329 struct in_addr in_addr;
330 if (lookup_ip(node->value, &in_addr)) {
331 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
332 } else if (ip_is_multicast(in_addr.s_addr)) {
333 VLOG_WARN("%s: multicast remote_ip="IP_FMT" not allowed",
334 name, IP_ARGS(in_addr.s_addr));
337 tnl_cfg.ip_dst = in_addr.s_addr;
339 } else if (!strcmp(node->key, "local_ip")) {
340 struct in_addr in_addr;
341 if (lookup_ip(node->value, &in_addr)) {
342 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
344 tnl_cfg.ip_src = in_addr.s_addr;
346 } else if (!strcmp(node->key, "tos")) {
347 if (!strcmp(node->value, "inherit")) {
348 tnl_cfg.tos_inherit = true;
352 tos = strtol(node->value, &endptr, 0);
353 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
356 VLOG_WARN("%s: invalid TOS %s", name, node->value);
359 } else if (!strcmp(node->key, "ttl")) {
360 if (!strcmp(node->value, "inherit")) {
361 tnl_cfg.ttl_inherit = true;
363 tnl_cfg.ttl = atoi(node->value);
365 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
366 tnl_cfg.dst_port = htons(atoi(node->value));
367 } else if (!strcmp(node->key, "csum") && has_csum) {
368 if (!strcmp(node->value, "true")) {
371 } else if (!strcmp(node->key, "df_default")) {
372 if (!strcmp(node->value, "false")) {
373 tnl_cfg.dont_fragment = false;
375 } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
376 if (smap_get(args, "certificate")) {
377 ipsec_mech_set = true;
379 const char *use_ssl_cert;
381 /* If the "use_ssl_cert" is true, then "certificate" and
382 * "private_key" will be pulled from the SSL table. The
383 * use of this option is strongly discouraged, since it
384 * will like be removed when multiple SSL configurations
385 * are supported by OVS.
387 use_ssl_cert = smap_get(args, "use_ssl_cert");
388 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
389 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
393 ipsec_mech_set = true;
395 } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
396 ipsec_mech_set = true;
397 } else if (tnl_cfg.ipsec
398 && (!strcmp(node->key, "certificate")
399 || !strcmp(node->key, "private_key")
400 || !strcmp(node->key, "use_ssl_cert"))) {
401 /* Ignore options not used by the netdev. */
402 } else if (!strcmp(node->key, "key") ||
403 !strcmp(node->key, "in_key") ||
404 !strcmp(node->key, "out_key")) {
405 /* Handled separately below. */
407 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
411 /* Add a default destination port for VXLAN if none specified. */
412 if (!strcmp(type, "vxlan") && !tnl_cfg.dst_port) {
413 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
416 /* Add a default destination port for LISP if none specified. */
417 if (!strcmp(type, "lisp") && !tnl_cfg.dst_port) {
418 tnl_cfg.dst_port = htons(LISP_DST_PORT);
422 static pid_t pid = 0;
424 char *file_name = xasprintf("%s/%s", ovs_rundir(),
425 "ovs-monitor-ipsec.pid");
426 pid = read_pidfile(file_name);
431 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
436 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
437 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
441 if (!ipsec_mech_set) {
442 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
448 if (!tnl_cfg.ip_dst) {
449 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
454 tnl_cfg.ttl = DEFAULT_TTL;
457 tnl_cfg.in_key = parse_key(args, "in_key",
458 &tnl_cfg.in_key_present,
459 &tnl_cfg.in_key_flow);
461 tnl_cfg.out_key = parse_key(args, "out_key",
462 &tnl_cfg.out_key_present,
463 &tnl_cfg.out_key_flow);
465 dev->tnl_cfg = tnl_cfg;
466 netdev_vport_poll_notify(dev);
472 get_tunnel_config(struct netdev_dev *dev, struct smap *args)
474 const struct netdev_tunnel_config *tnl_cfg =
475 &netdev_dev_vport_cast(dev)->tnl_cfg;
477 if (tnl_cfg->ip_dst) {
478 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_dst));
481 if (tnl_cfg->ip_src) {
482 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg->ip_src));
485 if (tnl_cfg->in_key_flow && tnl_cfg->out_key_flow) {
486 smap_add(args, "key", "flow");
487 } else if (tnl_cfg->in_key_present && tnl_cfg->out_key_present
488 && tnl_cfg->in_key == tnl_cfg->out_key) {
489 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg->in_key));
491 if (tnl_cfg->in_key_flow) {
492 smap_add(args, "in_key", "flow");
493 } else if (tnl_cfg->in_key_present) {
494 smap_add_format(args, "in_key", "%"PRIu64,
495 ntohll(tnl_cfg->in_key));
498 if (tnl_cfg->out_key_flow) {
499 smap_add(args, "out_key", "flow");
500 } else if (tnl_cfg->out_key_present) {
501 smap_add_format(args, "out_key", "%"PRIu64,
502 ntohll(tnl_cfg->out_key));
506 if (tnl_cfg->ttl_inherit) {
507 smap_add(args, "ttl", "inherit");
508 } else if (tnl_cfg->ttl != DEFAULT_TTL) {
509 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg->ttl);
512 if (tnl_cfg->tos_inherit) {
513 smap_add(args, "tos", "inherit");
514 } else if (tnl_cfg->tos) {
515 smap_add_format(args, "tos", "0x%x", tnl_cfg->tos);
518 if (tnl_cfg->dst_port) {
519 uint16_t dst_port = ntohs(tnl_cfg->dst_port);
520 const char *type = netdev_dev_get_type(dev);
522 if ((!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
523 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT)) {
524 smap_add_format(args, "dst_port", "%d", dst_port);
529 smap_add(args, "csum", "true");
532 if (!tnl_cfg->dont_fragment) {
533 smap_add(args, "df_default", "false");
539 /* Code specific to patch ports. */
542 netdev_vport_patch_peer(const struct netdev *netdev)
544 return netdev_vport_is_patch(netdev)
545 ? netdev_vport_get_dev(netdev)->peer
550 netdev_vport_inc_rx(const struct netdev *netdev,
551 const struct dpif_flow_stats *stats)
553 if (is_vport_class(netdev_dev_get_class(netdev_get_dev(netdev)))) {
554 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
555 dev->stats.rx_packets += stats->n_packets;
556 dev->stats.rx_bytes += stats->n_bytes;
561 netdev_vport_inc_tx(const struct netdev *netdev,
562 const struct dpif_flow_stats *stats)
564 if (is_vport_class(netdev_dev_get_class(netdev_get_dev(netdev)))) {
565 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
566 dev->stats.tx_packets += stats->n_packets;
567 dev->stats.tx_bytes += stats->n_bytes;
572 get_patch_config(struct netdev_dev *dev_, struct smap *args)
574 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
577 smap_add(args, "peer", dev->peer);
583 set_patch_config(struct netdev_dev *dev_, const struct smap *args)
585 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
586 const char *name = netdev_dev_get_name(dev_);
589 peer = smap_get(args, "peer");
591 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
595 if (smap_count(args) > 1) {
596 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
600 if (!strcmp(name, peer)) {
601 VLOG_ERR("%s: patch peer must not be self", name);
606 dev->peer = xstrdup(peer);
612 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
614 struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
615 memcpy(stats, &dev->stats, sizeof *stats);
619 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
620 GET_TUNNEL_CONFIG, GET_STATUS) \
625 netdev_vport_create, \
626 netdev_vport_destroy, \
632 netdev_vport_close, \
636 NULL, /* recv_wait */ \
640 NULL, /* send_wait */ \
642 netdev_vport_set_etheraddr, \
643 netdev_vport_get_etheraddr, \
644 NULL, /* get_mtu */ \
645 NULL, /* set_mtu */ \
646 NULL, /* get_ifindex */ \
647 NULL, /* get_carrier */ \
648 NULL, /* get_carrier_resets */ \
649 NULL, /* get_miimon */ \
651 NULL, /* set_stats */ \
653 NULL, /* get_features */ \
654 NULL, /* set_advertisements */ \
656 NULL, /* set_policing */ \
657 NULL, /* get_qos_types */ \
658 NULL, /* get_qos_capabilities */ \
659 NULL, /* get_qos */ \
660 NULL, /* set_qos */ \
661 NULL, /* get_queue */ \
662 NULL, /* set_queue */ \
663 NULL, /* delete_queue */ \
664 NULL, /* get_queue_stats */ \
665 NULL, /* dump_queues */ \
666 NULL, /* dump_queue_stats */ \
668 NULL, /* get_in4 */ \
669 NULL, /* set_in4 */ \
670 NULL, /* get_in6 */ \
671 NULL, /* add_router */ \
672 NULL, /* get_next_hop */ \
674 NULL, /* arp_lookup */ \
676 netdev_vport_update_flags, \
678 netdev_vport_change_seq
680 #define TUNNEL_CLASS(NAME, DPIF_PORT) \
682 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
684 get_netdev_tunnel_config, \
685 tunnel_get_status) }}
688 netdev_vport_tunnel_register(void)
690 static const struct vport_class vport_classes[] = {
691 TUNNEL_CLASS("gre", "gre_system"),
692 TUNNEL_CLASS("ipsec_gre", "gre_system"),
693 TUNNEL_CLASS("gre64", "gre64_system"),
694 TUNNEL_CLASS("ipsec_gre64", "gre64_system"),
695 TUNNEL_CLASS("vxlan", "vxlan_system"),
696 TUNNEL_CLASS("lisp", "lisp_system")
701 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
702 netdev_register_provider(&vport_classes[i].netdev_class);
707 netdev_vport_patch_register(void)
709 static const struct vport_class patch_class =
711 { "patch", VPORT_FUNCTIONS(get_patch_config,
715 netdev_register_provider(&patch_class.netdev_class);