2010-04-11 09:38:49 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Nicira Networks.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "netdev-provider.h"
|
2010-05-17 15:04:10 -07:00
|
|
|
#include "netdev-vport.h"
|
2010-04-11 09:38:49 -04:00
|
|
|
#include "openflow/openflow.h"
|
|
|
|
#include "openvswitch/datapath-protocol.h"
|
2010-08-10 20:11:48 -04:00
|
|
|
#include "openvswitch/tunnel.h"
|
2010-04-11 09:38:49 -04:00
|
|
|
#include "packets.h"
|
|
|
|
#include "socket-util.h"
|
|
|
|
#include "vlog.h"
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
VLOG_DEFINE_THIS_MODULE(netdev_tunnel)
|
2010-07-16 11:02:49 -07:00
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_dev_tunnel {
|
2010-04-11 09:38:49 -04:00
|
|
|
struct netdev_dev netdev_dev;
|
|
|
|
};
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_tunnel {
|
2010-04-11 09:38:49 -04:00
|
|
|
struct netdev netdev;
|
|
|
|
};
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
static int netdev_tunnel_create(const char *name, const char *type,
|
|
|
|
const struct shash *args, struct netdev_dev **);
|
|
|
|
|
|
|
|
static struct netdev_dev_tunnel *
|
|
|
|
netdev_dev_tunnel_cast(const struct netdev_dev *netdev_dev)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
2010-08-11 18:29:48 -04:00
|
|
|
assert(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
|
|
|
|
return CONTAINER_OF(netdev_dev, struct netdev_dev_tunnel, netdev_dev);
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
static struct netdev_tunnel *
|
|
|
|
netdev_tunnel_cast(const struct netdev *netdev)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
|
|
|
|
assert(netdev_dev_get_class(netdev_dev)->create == netdev_tunnel_create);
|
|
|
|
return CONTAINER_OF(netdev, struct netdev_tunnel, netdev);
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 18:29:48 -04:00
|
|
|
parse_config(const char *name, const char *type, const struct shash *args,
|
2010-08-10 20:11:48 -04:00
|
|
|
struct tnl_port_config *config)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
|
|
|
struct shash_node *node;
|
|
|
|
|
|
|
|
memset(config, 0, sizeof *config);
|
|
|
|
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_PMTUD;
|
2010-04-11 09:38:49 -04:00
|
|
|
|
|
|
|
SHASH_FOR_EACH (node, args) {
|
|
|
|
if (!strcmp(node->name, "remote_ip")) {
|
|
|
|
struct in_addr in_addr;
|
|
|
|
if (lookup_ip(node->data, &in_addr)) {
|
2010-08-11 18:29:48 -04:00
|
|
|
VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->daddr = in_addr.s_addr;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "local_ip")) {
|
|
|
|
struct in_addr in_addr;
|
|
|
|
if (lookup_ip(node->data, &in_addr)) {
|
2010-08-11 18:29:48 -04:00
|
|
|
VLOG_WARN("%s: bad %s 'local_ip'", name, type);
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->saddr = in_addr.s_addr;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "key")) {
|
|
|
|
if (!strcmp(node->data, "flow")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_IN_KEY_MATCH;
|
|
|
|
config->flags |= TNL_F_OUT_KEY_ACTION;
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->out_key = config->in_key = htonl(atoi(node->data));
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "in_key")) {
|
|
|
|
if (!strcmp(node->data, "flow")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_IN_KEY_MATCH;
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->in_key = htonl(atoi(node->data));
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "out_key")) {
|
|
|
|
if (!strcmp(node->data, "flow")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_OUT_KEY_ACTION;
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->out_key = htonl(atoi(node->data));
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "tos")) {
|
|
|
|
if (!strcmp(node->data, "inherit")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_TOS_INHERIT;
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->tos = atoi(node->data);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "ttl")) {
|
|
|
|
if (!strcmp(node->data, "inherit")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_TTL_INHERIT;
|
2010-04-11 09:38:49 -04:00
|
|
|
} else {
|
|
|
|
config->ttl = atoi(node->data);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "csum")) {
|
2010-08-12 23:31:03 -04:00
|
|
|
if (!strcmp(node->data, "true")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags |= TNL_F_CSUM;
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
} else if (!strcmp(node->name, "pmtud")) {
|
|
|
|
if (!strcmp(node->data, "false")) {
|
2010-08-10 20:11:48 -04:00
|
|
|
config->flags &= ~TNL_F_PMTUD;
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-11 18:29:48 -04:00
|
|
|
VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config->daddr) {
|
2010-08-11 18:29:48 -04:00
|
|
|
VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
|
2010-04-11 09:38:49 -04:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_create(const char *name, const char *type,
|
|
|
|
const struct shash *args, struct netdev_dev **netdev_devp)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct odp_vport_add ova;
|
2010-08-10 20:11:48 -04:00
|
|
|
struct tnl_port_config port_config;
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_dev_tunnel *netdev_dev;
|
2010-04-11 09:38:49 -04:00
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
ovs_strlcpy(ova.port_type, type, sizeof ova.port_type);
|
2010-04-11 09:38:49 -04:00
|
|
|
ovs_strlcpy(ova.devname, name, sizeof ova.devname);
|
|
|
|
ova.config = &port_config;
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
err = parse_config(name, type, args, &port_config);
|
2010-04-11 09:38:49 -04:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
|
2010-07-14 19:02:10 -07:00
|
|
|
if (err == EBUSY) {
|
2010-04-11 09:38:49 -04:00
|
|
|
VLOG_WARN("%s: destroying existing device", name);
|
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
|
2010-04-11 09:38:49 -04:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
netdev_dev = xmalloc(sizeof *netdev_dev);
|
2010-04-08 10:22:35 -04:00
|
|
|
netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_gre_class);
|
2010-04-11 09:38:49 -04:00
|
|
|
|
|
|
|
*netdev_devp = &netdev_dev->netdev_dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
|
|
|
const char *name = netdev_dev_get_name(netdev_dev_);
|
|
|
|
struct odp_vport_mod ovm;
|
2010-08-10 20:11:48 -04:00
|
|
|
struct tnl_port_config port_config;
|
2010-04-11 09:38:49 -04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
|
|
|
|
ovm.config = &port_config;
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
err = parse_config(name, netdev_dev_get_class(netdev_dev_)->type, args,
|
|
|
|
&port_config);
|
2010-04-11 09:38:49 -04:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
|
2010-04-11 09:38:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_destroy(struct netdev_dev *netdev_dev_)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
|
2010-04-11 09:38:49 -04:00
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
|
2010-04-11 09:38:49 -04:00
|
|
|
free(netdev_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
|
2010-04-11 09:38:49 -04:00
|
|
|
struct netdev **netdevp)
|
|
|
|
{
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_tunnel *netdev;
|
2010-04-11 09:38:49 -04:00
|
|
|
|
|
|
|
netdev = xmalloc(sizeof *netdev);
|
|
|
|
netdev_init(&netdev->netdev, netdev_dev_);
|
|
|
|
|
|
|
|
*netdevp = &netdev->netdev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_close(struct netdev *netdev_)
|
2010-04-11 09:38:49 -04:00
|
|
|
{
|
2010-08-11 18:29:48 -04:00
|
|
|
struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_);
|
2010-04-11 09:38:49 -04:00
|
|
|
free(netdev);
|
|
|
|
}
|
|
|
|
|
2010-04-08 10:22:35 -04:00
|
|
|
const struct netdev_class netdev_gre_class = {
|
|
|
|
"gre",
|
2010-04-11 09:38:49 -04:00
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
NULL, /* init */
|
2010-04-11 09:38:49 -04:00
|
|
|
NULL, /* run */
|
|
|
|
NULL, /* wait */
|
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_create,
|
|
|
|
netdev_tunnel_destroy,
|
|
|
|
netdev_tunnel_reconfigure,
|
2010-04-11 09:38:49 -04:00
|
|
|
|
2010-08-11 18:29:48 -04:00
|
|
|
netdev_tunnel_open,
|
|
|
|
netdev_tunnel_close,
|
2010-04-11 09:38:49 -04:00
|
|
|
|
|
|
|
NULL, /* enumerate */
|
|
|
|
|
|
|
|
NULL, /* recv */
|
|
|
|
NULL, /* recv_wait */
|
|
|
|
NULL, /* drain */
|
|
|
|
|
|
|
|
NULL, /* send */
|
|
|
|
NULL, /* send_wait */
|
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
netdev_vport_set_etheraddr,
|
|
|
|
netdev_vport_get_etheraddr,
|
|
|
|
netdev_vport_get_mtu,
|
2010-04-11 09:38:49 -04:00
|
|
|
NULL, /* get_ifindex */
|
2010-05-17 15:04:10 -07:00
|
|
|
netdev_vport_get_carrier,
|
|
|
|
netdev_vport_get_stats,
|
2010-06-09 12:54:34 -07:00
|
|
|
netdev_vport_set_stats,
|
2010-04-11 09:38:49 -04:00
|
|
|
|
|
|
|
NULL, /* get_features */
|
|
|
|
NULL, /* set_advertisements */
|
|
|
|
NULL, /* get_vlan_vid */
|
2010-06-17 15:04:12 -07:00
|
|
|
|
2010-04-11 09:38:49 -04:00
|
|
|
NULL, /* set_policing */
|
2010-06-17 15:04:12 -07:00
|
|
|
NULL, /* get_qos_types */
|
|
|
|
NULL, /* get_qos_capabilities */
|
|
|
|
NULL, /* get_qos */
|
|
|
|
NULL, /* set_qos */
|
|
|
|
NULL, /* get_queue */
|
|
|
|
NULL, /* set_queue */
|
|
|
|
NULL, /* delete_queue */
|
|
|
|
NULL, /* get_queue_stats */
|
|
|
|
NULL, /* dump_queues */
|
|
|
|
NULL, /* dump_queue_stats */
|
2010-04-11 09:38:49 -04:00
|
|
|
|
|
|
|
NULL, /* get_in4 */
|
|
|
|
NULL, /* set_in4 */
|
|
|
|
NULL, /* get_in6 */
|
|
|
|
NULL, /* add_router */
|
|
|
|
NULL, /* get_next_hop */
|
|
|
|
NULL, /* arp_lookup */
|
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
netdev_vport_update_flags,
|
2010-04-11 09:38:49 -04:00
|
|
|
|
2010-05-17 15:04:10 -07:00
|
|
|
netdev_vport_poll_add,
|
|
|
|
netdev_vport_poll_remove,
|
2010-04-11 09:38:49 -04:00
|
|
|
};
|