2010-04-12 15:53:39 -04:00
|
|
|
/*
|
2015-12-03 11:40:53 -08:00
|
|
|
* Copyright (c) 2007-2015 Nicira, Inc.
|
2010-04-12 15:53:39 -04:00
|
|
|
*
|
2011-11-16 13:39:40 -08:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA
|
2010-04-12 15:53:39 -04:00
|
|
|
*/
|
|
|
|
|
2011-06-23 12:54:48 -07:00
|
|
|
#include <linux/hardirq.h>
|
2010-12-29 22:13:15 -08:00
|
|
|
#include <linux/if_vlan.h>
|
2010-04-12 15:53:39 -04:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include <linux/ethtool.h>
|
|
|
|
#include <linux/skbuff.h>
|
2015-12-03 11:40:53 -08:00
|
|
|
#include <linux/percpu.h>
|
|
|
|
#include <linux/u64_stats_sync.h>
|
|
|
|
#include <linux/netdev_features.h>
|
2010-04-12 15:53:39 -04:00
|
|
|
|
2012-05-17 11:43:15 -07:00
|
|
|
#include <net/dst.h>
|
|
|
|
#include <net/xfrm.h>
|
2015-12-03 11:40:53 -08:00
|
|
|
#include <net/rtnetlink.h>
|
2012-05-17 11:43:15 -07:00
|
|
|
|
2010-04-12 15:53:39 -04:00
|
|
|
#include "datapath.h"
|
|
|
|
#include "vport-internal_dev.h"
|
|
|
|
#include "vport-netdev.h"
|
|
|
|
|
|
|
|
struct internal_dev {
|
2010-12-03 15:44:51 -08:00
|
|
|
struct vport *vport;
|
2010-04-12 15:53:39 -04:00
|
|
|
};
|
|
|
|
|
2015-04-04 08:24:13 +02:00
|
|
|
static struct vport_ops ovs_internal_vport_ops;
|
|
|
|
|
2011-11-07 15:53:01 -08:00
|
|
|
static struct internal_dev *internal_dev_priv(struct net_device *netdev)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
|
|
|
return netdev_priv(netdev);
|
|
|
|
}
|
|
|
|
|
2011-10-06 19:45:09 -07:00
|
|
|
/* Called with rcu_read_lock_bh. */
|
2010-04-12 15:53:39 -04:00
|
|
|
static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
|
|
|
|
{
|
2015-12-03 11:40:53 -08:00
|
|
|
int len, err;
|
|
|
|
|
|
|
|
len = skb->len;
|
2011-10-06 19:45:09 -07:00
|
|
|
rcu_read_lock();
|
2015-12-03 11:40:53 -08:00
|
|
|
err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL);
|
2011-10-06 19:45:09 -07:00
|
|
|
rcu_read_unlock();
|
2015-12-03 11:40:53 -08:00
|
|
|
|
|
|
|
if (likely(!err)) {
|
|
|
|
#ifdef HAVE_DEV_TSTATS
|
|
|
|
struct pcpu_sw_netstats *tstats;
|
|
|
|
|
|
|
|
tstats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)netdev->tstats);
|
|
|
|
|
|
|
|
u64_stats_update_begin(&tstats->syncp);
|
|
|
|
tstats->tx_bytes += len;
|
|
|
|
tstats->tx_packets++;
|
|
|
|
u64_stats_update_end(&tstats->syncp);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
netdev->stats.tx_errors++;
|
|
|
|
}
|
2010-04-12 15:53:39 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int internal_dev_open(struct net_device *netdev)
|
|
|
|
{
|
|
|
|
netif_start_queue(netdev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int internal_dev_stop(struct net_device *netdev)
|
|
|
|
{
|
|
|
|
netif_stop_queue(netdev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void internal_dev_getinfo(struct net_device *netdev,
|
|
|
|
struct ethtool_drvinfo *info)
|
|
|
|
{
|
2013-03-14 17:36:37 -07:00
|
|
|
strlcpy(info->driver, "openvswitch", sizeof(info->driver));
|
2010-04-12 15:53:39 -04:00
|
|
|
}
|
|
|
|
|
2010-11-23 22:08:27 -08:00
|
|
|
static const struct ethtool_ops internal_dev_ethtool_ops = {
|
2010-04-22 08:11:50 -04:00
|
|
|
.get_drvinfo = internal_dev_getinfo,
|
|
|
|
.get_link = ethtool_op_get_link,
|
2011-11-04 13:31:45 -07:00
|
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
|
2010-04-22 08:11:50 -04:00
|
|
|
.get_sg = ethtool_op_get_sg,
|
|
|
|
.set_sg = ethtool_op_set_sg,
|
|
|
|
.get_tx_csum = ethtool_op_get_tx_csum,
|
|
|
|
.set_tx_csum = ethtool_op_set_tx_hw_csum,
|
|
|
|
.get_tso = ethtool_op_get_tso,
|
|
|
|
.set_tso = ethtool_op_set_tso,
|
2011-11-04 13:31:45 -07:00
|
|
|
#endif
|
2010-04-12 15:53:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu)
|
|
|
|
{
|
|
|
|
if (new_mtu < 68)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
netdev->mtu = new_mtu;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-24 14:16:24 -08:00
|
|
|
static void internal_dev_destructor(struct net_device *dev)
|
|
|
|
{
|
2011-11-21 17:15:20 -08:00
|
|
|
struct vport *vport = ovs_internal_dev_get_vport(dev);
|
2011-02-24 14:16:24 -08:00
|
|
|
|
2011-11-21 17:15:20 -08:00
|
|
|
ovs_vport_free(vport);
|
2011-02-24 14:16:24 -08:00
|
|
|
free_netdev(dev);
|
|
|
|
}
|
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
#ifdef HAVE_DEV_TSTATS
|
|
|
|
static int internal_dev_init(struct net_device *dev)
|
|
|
|
{
|
|
|
|
dev->tstats = (typeof(dev->tstats)) netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
|
|
|
|
if (!dev->tstats)
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void internal_dev_uninit(struct net_device *dev)
|
|
|
|
{
|
|
|
|
free_percpu(dev->tstats);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-12 15:53:39 -04:00
|
|
|
static const struct net_device_ops internal_dev_netdev_ops = {
|
2015-12-03 11:40:53 -08:00
|
|
|
#ifdef HAVE_DEV_TSTATS
|
|
|
|
.ndo_init = internal_dev_init,
|
|
|
|
.ndo_uninit = internal_dev_uninit,
|
|
|
|
.ndo_get_stats64 = ip_tunnel_get_stats64,
|
|
|
|
#endif
|
2010-04-12 15:53:39 -04:00
|
|
|
.ndo_open = internal_dev_open,
|
|
|
|
.ndo_stop = internal_dev_stop,
|
|
|
|
.ndo_start_xmit = internal_dev_xmit,
|
2012-12-04 09:33:47 -08:00
|
|
|
.ndo_set_mac_address = eth_mac_addr,
|
2010-04-12 15:53:39 -04:00
|
|
|
.ndo_change_mtu = internal_dev_change_mtu,
|
|
|
|
};
|
|
|
|
|
2015-01-07 12:55:49 +01:00
|
|
|
static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
|
|
|
|
.kind = "openvswitch",
|
|
|
|
};
|
|
|
|
|
2010-07-14 19:27:18 -07:00
|
|
|
static void do_setup(struct net_device *netdev)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
|
|
|
ether_setup(netdev);
|
|
|
|
|
|
|
|
netdev->netdev_ops = &internal_dev_netdev_ops;
|
|
|
|
|
2011-09-20 19:09:50 -07:00
|
|
|
netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
|
2015-12-03 11:40:53 -08:00
|
|
|
netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH;
|
2011-02-24 14:16:24 -08:00
|
|
|
netdev->destructor = internal_dev_destructor;
|
2014-09-18 14:48:56 +02:00
|
|
|
netdev->ethtool_ops = &internal_dev_ethtool_ops;
|
2015-01-07 12:55:49 +01:00
|
|
|
netdev->rtnl_link_ops = &internal_dev_link_ops;
|
2010-04-12 15:53:39 -04:00
|
|
|
netdev->tx_queue_len = 0;
|
|
|
|
|
2010-11-23 17:03:16 -08:00
|
|
|
netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
|
2014-07-03 11:38:29 -07:00
|
|
|
NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
|
|
|
|
NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
|
2010-04-12 15:53:39 -04:00
|
|
|
|
2011-03-04 15:52:43 -08:00
|
|
|
netdev->vlan_features = netdev->features;
|
2013-08-02 11:38:51 -07:00
|
|
|
netdev->features |= NETIF_F_HW_VLAN_CTAG_TX;
|
2011-03-04 15:52:43 -08:00
|
|
|
|
2014-07-03 11:38:29 -07:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
|
|
|
|
netdev->hw_enc_features = netdev->features;
|
|
|
|
#endif
|
2015-12-03 11:40:53 -08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
|
|
|
|
netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
|
|
|
|
#endif
|
2012-02-15 15:13:19 -08:00
|
|
|
eth_hw_addr_random(netdev);
|
2010-04-12 15:53:39 -04:00
|
|
|
}
|
|
|
|
|
2010-11-04 16:32:57 -07:00
|
|
|
static struct vport *internal_dev_create(const struct vport_parms *parms)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
|
|
|
struct vport *vport;
|
|
|
|
struct internal_dev *internal_dev;
|
|
|
|
int err;
|
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms);
|
2010-04-12 15:53:39 -04:00
|
|
|
if (IS_ERR(vport)) {
|
|
|
|
err = PTR_ERR(vport);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
vport->dev = alloc_netdev(sizeof(struct internal_dev),
|
|
|
|
parms->name, NET_NAME_UNKNOWN, do_setup);
|
|
|
|
if (!vport->dev) {
|
2010-04-12 15:53:39 -04:00
|
|
|
err = -ENOMEM;
|
|
|
|
goto error_free_vport;
|
|
|
|
}
|
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
dev_net_set(vport->dev, ovs_dp_get_net(vport->dp));
|
|
|
|
internal_dev = internal_dev_priv(vport->dev);
|
2010-12-08 19:21:40 -08:00
|
|
|
internal_dev->vport = vport;
|
2010-04-12 15:53:39 -04:00
|
|
|
|
2012-01-30 06:56:54 -08:00
|
|
|
/* Restrict bridge port to current netns. */
|
|
|
|
if (vport->port_no == OVSP_LOCAL)
|
2015-12-03 11:40:53 -08:00
|
|
|
vport->dev->features |= NETIF_F_NETNS_LOCAL;
|
2012-01-30 06:56:54 -08:00
|
|
|
|
2013-04-12 15:57:09 -07:00
|
|
|
rtnl_lock();
|
2015-12-03 11:40:53 -08:00
|
|
|
err = register_netdevice(vport->dev);
|
2010-04-12 15:53:39 -04:00
|
|
|
if (err)
|
|
|
|
goto error_free_netdev;
|
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
dev_set_promiscuity(vport->dev, 1);
|
2013-04-12 15:57:09 -07:00
|
|
|
rtnl_unlock();
|
2015-12-03 11:40:53 -08:00
|
|
|
netif_start_queue(vport->dev);
|
2010-12-03 15:44:51 -08:00
|
|
|
|
2010-04-12 15:53:39 -04:00
|
|
|
return vport;
|
|
|
|
|
|
|
|
error_free_netdev:
|
2013-04-12 15:57:09 -07:00
|
|
|
rtnl_unlock();
|
2015-12-03 11:40:53 -08:00
|
|
|
free_netdev(vport->dev);
|
2010-04-12 15:53:39 -04:00
|
|
|
error_free_vport:
|
2011-11-21 17:15:20 -08:00
|
|
|
ovs_vport_free(vport);
|
2010-04-12 15:53:39 -04:00
|
|
|
error:
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2011-09-09 19:09:47 -07:00
|
|
|
static void internal_dev_destroy(struct vport *vport)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
2015-12-03 11:40:53 -08:00
|
|
|
netif_stop_queue(vport->dev);
|
2013-04-12 15:57:09 -07:00
|
|
|
rtnl_lock();
|
2015-12-03 11:40:53 -08:00
|
|
|
dev_set_promiscuity(vport->dev, -1);
|
2010-12-03 15:44:51 -08:00
|
|
|
|
2011-01-26 09:42:28 -08:00
|
|
|
/* unregister_netdevice() waits for an RCU grace period. */
|
2015-12-03 11:40:53 -08:00
|
|
|
unregister_netdevice(vport->dev);
|
2013-04-12 15:57:09 -07:00
|
|
|
|
|
|
|
rtnl_unlock();
|
2010-04-12 15:53:39 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
static netdev_tx_t internal_dev_recv(struct sk_buff *skb)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
2015-12-03 11:40:53 -08:00
|
|
|
struct net_device *netdev = skb->dev;
|
|
|
|
#ifdef HAVE_DEV_TSTATS
|
|
|
|
struct pcpu_sw_netstats *stats;
|
|
|
|
#endif
|
2010-04-12 15:53:39 -04:00
|
|
|
|
2014-07-30 09:49:01 +08:00
|
|
|
if (unlikely(!(netdev->flags & IFF_UP))) {
|
|
|
|
kfree_skb(skb);
|
2015-12-03 11:40:53 -08:00
|
|
|
netdev->stats.rx_dropped++;
|
|
|
|
return NETDEV_TX_OK;
|
2014-07-30 09:49:01 +08:00
|
|
|
}
|
|
|
|
|
2010-12-29 22:13:15 -08:00
|
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
|
2015-02-03 21:53:35 +01:00
|
|
|
if (skb_vlan_tag_present(skb)) {
|
2015-01-07 12:55:49 +01:00
|
|
|
if (unlikely(!vlan_insert_tag_set_proto(skb,
|
|
|
|
skb->vlan_proto,
|
2015-02-03 21:53:35 +01:00
|
|
|
skb_vlan_tag_get(skb))))
|
2015-12-03 11:40:53 -08:00
|
|
|
return NETDEV_TX_OK;
|
2013-09-04 11:35:13 -07:00
|
|
|
|
|
|
|
if (skb->ip_summed == CHECKSUM_COMPLETE)
|
|
|
|
skb->csum = csum_add(skb->csum,
|
|
|
|
csum_partial(skb->data + (2 * ETH_ALEN),
|
|
|
|
VLAN_HLEN, 0));
|
|
|
|
|
|
|
|
vlan_set_tci(skb, 0);
|
|
|
|
}
|
2010-12-29 22:13:15 -08:00
|
|
|
#endif
|
|
|
|
|
2012-05-17 11:43:15 -07:00
|
|
|
skb_dst_drop(skb);
|
|
|
|
nf_reset(skb);
|
|
|
|
secpath_reset(skb);
|
|
|
|
|
2010-04-12 15:53:39 -04:00
|
|
|
skb->pkt_type = PACKET_HOST;
|
|
|
|
skb->protocol = eth_type_trans(skb, netdev);
|
2013-06-12 16:59:28 -07:00
|
|
|
skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
|
2010-04-12 15:53:39 -04:00
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
#ifdef HAVE_DEV_TSTATS
|
|
|
|
stats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)netdev->tstats);
|
|
|
|
u64_stats_update_begin(&stats->syncp);
|
|
|
|
stats->rx_packets++;
|
|
|
|
stats->rx_bytes += skb->len;
|
|
|
|
u64_stats_update_end(&stats->syncp);
|
|
|
|
#endif
|
2010-11-23 16:46:47 -08:00
|
|
|
|
2015-12-03 11:40:53 -08:00
|
|
|
netif_rx(skb);
|
|
|
|
return NETDEV_TX_OK;
|
2010-04-12 15:53:39 -04:00
|
|
|
}
|
|
|
|
|
2015-04-04 08:24:13 +02:00
|
|
|
static struct vport_ops ovs_internal_vport_ops = {
|
2011-08-18 10:35:40 -07:00
|
|
|
.type = OVS_VPORT_TYPE_INTERNAL,
|
2010-04-12 15:53:39 -04:00
|
|
|
.create = internal_dev_create,
|
|
|
|
.destroy = internal_dev_destroy,
|
|
|
|
.send = internal_dev_recv,
|
|
|
|
};
|
|
|
|
|
2011-11-21 17:15:20 -08:00
|
|
|
int ovs_is_internal_dev(const struct net_device *netdev)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
|
|
|
return netdev->netdev_ops == &internal_dev_netdev_ops;
|
|
|
|
}
|
|
|
|
|
2011-11-21 17:15:20 -08:00
|
|
|
struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
|
2010-04-12 15:53:39 -04:00
|
|
|
{
|
2011-11-21 17:15:20 -08:00
|
|
|
if (!ovs_is_internal_dev(netdev))
|
2010-10-05 19:14:21 -07:00
|
|
|
return NULL;
|
2010-12-03 15:44:51 -08:00
|
|
|
|
2010-12-08 19:21:40 -08:00
|
|
|
return internal_dev_priv(netdev)->vport;
|
2010-04-12 15:53:39 -04:00
|
|
|
}
|
2015-01-07 12:55:49 +01:00
|
|
|
|
|
|
|
int ovs_internal_dev_rtnl_link_register(void)
|
|
|
|
{
|
2015-04-04 08:24:13 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = rtnl_link_register(&internal_dev_link_ops);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = ovs_vport_ops_register(&ovs_internal_vport_ops);
|
|
|
|
if (err < 0)
|
|
|
|
rtnl_link_unregister(&internal_dev_link_ops);
|
|
|
|
|
|
|
|
return err;
|
2015-01-07 12:55:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovs_internal_dev_rtnl_link_unregister(void)
|
|
|
|
{
|
2015-04-04 08:24:13 +02:00
|
|
|
ovs_vport_ops_unregister(&ovs_internal_vport_ops);
|
2015-01-07 12:55:49 +01:00
|
|
|
rtnl_link_unregister(&internal_dev_link_ops);
|
|
|
|
}
|