mirror of
https://github.com/suyu-emu/horinux.git
synced 2026-09-21 14:15:21 -07:00
tap: Abstract type of virtual interface from tap implementation
macvlan object is re-structured to hold tap related elements in a separate entity, tap_dev. Upon NETDEV_REGISTER device_event, tap_dev is registered with idr and fetched again on tap_open. Few of the tap functions are modified to accepted tap_dev as argument. tap_dev object includes callbacks to be used by underlying virtual interface to take care of tx and rx accounting. Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
committed by
David S. Miller
parent
ebc05ba7e8
commit
6fe3faf867
@@ -1525,7 +1525,6 @@ static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
|
|||||||
int macvlan_link_register(struct rtnl_link_ops *ops)
|
int macvlan_link_register(struct rtnl_link_ops *ops)
|
||||||
{
|
{
|
||||||
/* common fields */
|
/* common fields */
|
||||||
ops->priv_size = sizeof(struct macvlan_dev);
|
|
||||||
ops->validate = macvlan_validate;
|
ops->validate = macvlan_validate;
|
||||||
ops->maxtype = IFLA_MACVLAN_MAX;
|
ops->maxtype = IFLA_MACVLAN_MAX;
|
||||||
ops->policy = macvlan_policy;
|
ops->policy = macvlan_policy;
|
||||||
@@ -1548,6 +1547,7 @@ static struct rtnl_link_ops macvlan_link_ops = {
|
|||||||
.newlink = macvlan_newlink,
|
.newlink = macvlan_newlink,
|
||||||
.dellink = macvlan_dellink,
|
.dellink = macvlan_dellink,
|
||||||
.get_link_net = macvlan_get_link_net,
|
.get_link_net = macvlan_get_link_net,
|
||||||
|
.priv_size = sizeof(struct macvlan_dev),
|
||||||
};
|
};
|
||||||
|
|
||||||
static int macvlan_device_event(struct notifier_block *unused,
|
static int macvlan_device_event(struct notifier_block *unused,
|
||||||
|
|||||||
+57
-14
@@ -24,6 +24,11 @@
|
|||||||
#include <linux/virtio_net.h>
|
#include <linux/virtio_net.h>
|
||||||
#include <linux/skb_array.h>
|
#include <linux/skb_array.h>
|
||||||
|
|
||||||
|
struct macvtap_dev {
|
||||||
|
struct macvlan_dev vlan;
|
||||||
|
struct tap_dev tap;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Variables for dealing with macvtaps device numbers.
|
* Variables for dealing with macvtaps device numbers.
|
||||||
*/
|
*/
|
||||||
@@ -46,22 +51,55 @@ static struct cdev macvtap_cdev;
|
|||||||
#define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
|
#define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
|
||||||
NETIF_F_TSO6 | NETIF_F_UFO)
|
NETIF_F_TSO6 | NETIF_F_UFO)
|
||||||
|
|
||||||
|
static void macvtap_count_tx_dropped(struct tap_dev *tap)
|
||||||
|
{
|
||||||
|
struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
|
||||||
|
struct macvlan_dev *vlan = &vlantap->vlan;
|
||||||
|
|
||||||
|
this_cpu_inc(vlan->pcpu_stats->tx_dropped);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void macvtap_count_rx_dropped(struct tap_dev *tap)
|
||||||
|
{
|
||||||
|
struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
|
||||||
|
struct macvlan_dev *vlan = &vlantap->vlan;
|
||||||
|
|
||||||
|
macvlan_count_rx(vlan, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void macvtap_update_features(struct tap_dev *tap,
|
||||||
|
netdev_features_t features)
|
||||||
|
{
|
||||||
|
struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
|
||||||
|
struct macvlan_dev *vlan = &vlantap->vlan;
|
||||||
|
|
||||||
|
vlan->set_features = features;
|
||||||
|
netdev_update_features(vlan->dev);
|
||||||
|
}
|
||||||
|
|
||||||
static int macvtap_newlink(struct net *src_net,
|
static int macvtap_newlink(struct net *src_net,
|
||||||
struct net_device *dev,
|
struct net_device *dev,
|
||||||
struct nlattr *tb[],
|
struct nlattr *tb[],
|
||||||
struct nlattr *data[])
|
struct nlattr *data[])
|
||||||
{
|
{
|
||||||
struct macvlan_dev *vlan = netdev_priv(dev);
|
struct macvtap_dev *vlantap = netdev_priv(dev);
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
INIT_LIST_HEAD(&vlan->queue_list);
|
INIT_LIST_HEAD(&vlantap->tap.queue_list);
|
||||||
|
|
||||||
/* Since macvlan supports all offloads by default, make
|
/* Since macvlan supports all offloads by default, make
|
||||||
* tap support all offloads also.
|
* tap support all offloads also.
|
||||||
*/
|
*/
|
||||||
vlan->tap_features = TUN_OFFLOADS;
|
vlantap->tap.tap_features = TUN_OFFLOADS;
|
||||||
|
|
||||||
err = netdev_rx_handler_register(dev, tap_handle_frame, vlan);
|
/* Register callbacks for rx/tx drops accounting and updating
|
||||||
|
* net_device features
|
||||||
|
*/
|
||||||
|
vlantap->tap.count_tx_dropped = macvtap_count_tx_dropped;
|
||||||
|
vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped;
|
||||||
|
vlantap->tap.update_features = macvtap_update_features;
|
||||||
|
|
||||||
|
err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap);
|
||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
@@ -74,14 +112,18 @@ static int macvtap_newlink(struct net *src_net,
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vlantap->tap.dev = vlantap->vlan.dev;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void macvtap_dellink(struct net_device *dev,
|
static void macvtap_dellink(struct net_device *dev,
|
||||||
struct list_head *head)
|
struct list_head *head)
|
||||||
{
|
{
|
||||||
|
struct macvtap_dev *vlantap = netdev_priv(dev);
|
||||||
|
|
||||||
netdev_rx_handler_unregister(dev);
|
netdev_rx_handler_unregister(dev);
|
||||||
tap_del_queues(dev);
|
tap_del_queues(&vlantap->tap);
|
||||||
macvlan_dellink(dev, head);
|
macvlan_dellink(dev, head);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,13 +138,14 @@ static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
|
|||||||
.setup = macvtap_setup,
|
.setup = macvtap_setup,
|
||||||
.newlink = macvtap_newlink,
|
.newlink = macvtap_newlink,
|
||||||
.dellink = macvtap_dellink,
|
.dellink = macvtap_dellink,
|
||||||
|
.priv_size = sizeof(struct macvtap_dev),
|
||||||
};
|
};
|
||||||
|
|
||||||
static int macvtap_device_event(struct notifier_block *unused,
|
static int macvtap_device_event(struct notifier_block *unused,
|
||||||
unsigned long event, void *ptr)
|
unsigned long event, void *ptr)
|
||||||
{
|
{
|
||||||
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
||||||
struct macvlan_dev *vlan;
|
struct macvtap_dev *vlantap;
|
||||||
struct device *classdev;
|
struct device *classdev;
|
||||||
dev_t devt;
|
dev_t devt;
|
||||||
int err;
|
int err;
|
||||||
@@ -112,7 +155,7 @@ static int macvtap_device_event(struct notifier_block *unused,
|
|||||||
return NOTIFY_DONE;
|
return NOTIFY_DONE;
|
||||||
|
|
||||||
snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
|
snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
|
||||||
vlan = netdev_priv(dev);
|
vlantap = netdev_priv(dev);
|
||||||
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case NETDEV_REGISTER:
|
case NETDEV_REGISTER:
|
||||||
@@ -120,15 +163,15 @@ static int macvtap_device_event(struct notifier_block *unused,
|
|||||||
* been registered but before register_netdevice has
|
* been registered but before register_netdevice has
|
||||||
* finished running.
|
* finished running.
|
||||||
*/
|
*/
|
||||||
err = tap_get_minor(vlan);
|
err = tap_get_minor(&vlantap->tap);
|
||||||
if (err)
|
if (err)
|
||||||
return notifier_from_errno(err);
|
return notifier_from_errno(err);
|
||||||
|
|
||||||
devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
|
devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
|
||||||
classdev = device_create(&macvtap_class, &dev->dev, devt,
|
classdev = device_create(&macvtap_class, &dev->dev, devt,
|
||||||
dev, tap_name);
|
dev, tap_name);
|
||||||
if (IS_ERR(classdev)) {
|
if (IS_ERR(classdev)) {
|
||||||
tap_free_minor(vlan);
|
tap_free_minor(&vlantap->tap);
|
||||||
return notifier_from_errno(PTR_ERR(classdev));
|
return notifier_from_errno(PTR_ERR(classdev));
|
||||||
}
|
}
|
||||||
err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
|
err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
|
||||||
@@ -138,15 +181,15 @@ static int macvtap_device_event(struct notifier_block *unused,
|
|||||||
break;
|
break;
|
||||||
case NETDEV_UNREGISTER:
|
case NETDEV_UNREGISTER:
|
||||||
/* vlan->minor == 0 if NETDEV_REGISTER above failed */
|
/* vlan->minor == 0 if NETDEV_REGISTER above failed */
|
||||||
if (vlan->minor == 0)
|
if (vlantap->tap.minor == 0)
|
||||||
break;
|
break;
|
||||||
sysfs_remove_link(&dev->dev.kobj, tap_name);
|
sysfs_remove_link(&dev->dev.kobj, tap_name);
|
||||||
devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
|
devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
|
||||||
device_destroy(&macvtap_class, devt);
|
device_destroy(&macvtap_class, devt);
|
||||||
tap_free_minor(vlan);
|
tap_free_minor(&vlantap->tap);
|
||||||
break;
|
break;
|
||||||
case NETDEV_CHANGE_TX_QUEUE_LEN:
|
case NETDEV_CHANGE_TX_QUEUE_LEN:
|
||||||
if (tap_queue_resize(vlan))
|
if (tap_queue_resize(&vlantap->tap))
|
||||||
return NOTIFY_BAD;
|
return NOTIFY_BAD;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+118
-146
File diff suppressed because it is too large
Load Diff
+53
-4
@@ -14,11 +14,60 @@ static inline struct socket *tap_get_socket(struct file *f)
|
|||||||
}
|
}
|
||||||
#endif /* CONFIG_MACVTAP */
|
#endif /* CONFIG_MACVTAP */
|
||||||
|
|
||||||
|
#include <net/sock.h>
|
||||||
|
#include <linux/skb_array.h>
|
||||||
|
|
||||||
|
#define MAX_TAP_QUEUES 256
|
||||||
|
|
||||||
|
struct tap_queue;
|
||||||
|
|
||||||
|
struct tap_dev {
|
||||||
|
struct net_device *dev;
|
||||||
|
u16 flags;
|
||||||
|
/* This array tracks active taps. */
|
||||||
|
struct tap_queue __rcu *taps[MAX_TAP_QUEUES];
|
||||||
|
/* This list tracks all taps (both enabled and disabled) */
|
||||||
|
struct list_head queue_list;
|
||||||
|
int numvtaps;
|
||||||
|
int numqueues;
|
||||||
|
netdev_features_t tap_features;
|
||||||
|
int minor;
|
||||||
|
|
||||||
|
void (*update_features)(struct tap_dev *tap, netdev_features_t features);
|
||||||
|
void (*count_tx_dropped)(struct tap_dev *tap);
|
||||||
|
void (*count_rx_dropped)(struct tap_dev *tap);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A tap queue is the central object of tap module, it connects
|
||||||
|
* an open character device to virtual interface. There can be
|
||||||
|
* multiple queues on one interface, which map back to queues
|
||||||
|
* implemented in hardware on the underlying device.
|
||||||
|
*
|
||||||
|
* tap_proto is used to allocate queues through the sock allocation
|
||||||
|
* mechanism.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct tap_queue {
|
||||||
|
struct sock sk;
|
||||||
|
struct socket sock;
|
||||||
|
struct socket_wq wq;
|
||||||
|
int vnet_hdr_sz;
|
||||||
|
struct tap_dev __rcu *tap;
|
||||||
|
struct file *file;
|
||||||
|
unsigned int flags;
|
||||||
|
u16 queue_index;
|
||||||
|
bool enabled;
|
||||||
|
struct list_head next;
|
||||||
|
struct skb_array skb_array;
|
||||||
|
};
|
||||||
|
|
||||||
rx_handler_result_t tap_handle_frame(struct sk_buff **pskb);
|
rx_handler_result_t tap_handle_frame(struct sk_buff **pskb);
|
||||||
void tap_del_queues(struct net_device *dev);
|
void tap_del_queues(struct tap_dev *tap);
|
||||||
int tap_get_minor(struct macvlan_dev *vlan);
|
int tap_get_minor(struct tap_dev *tap);
|
||||||
void tap_free_minor(struct macvlan_dev *vlan);
|
void tap_free_minor(struct tap_dev *tap);
|
||||||
int tap_queue_resize(struct macvlan_dev *vlan);
|
int tap_queue_resize(struct tap_dev *tap);
|
||||||
int tap_create_cdev(struct cdev *tap_cdev,
|
int tap_create_cdev(struct cdev *tap_cdev,
|
||||||
dev_t *tap_major, const char *device_name);
|
dev_t *tap_major, const char *device_name);
|
||||||
void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev);
|
void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev);
|
||||||
|
|||||||
Reference in New Issue
Block a user