2019-06-25 11:12:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright(c) 2010-2014 Intel Corporation.
|
2017-04-21 10:43:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/version.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/rwsem.h>
|
2018-05-15 09:49:22 +00:00
|
|
|
#include <linux/mutex.h>
|
2017-04-21 10:43:26 +00:00
|
|
|
#include <linux/nsproxy.h>
|
|
|
|
#include <net/net_namespace.h>
|
|
|
|
#include <net/netns/generic.h>
|
|
|
|
|
2020-06-18 16:55:50 +00:00
|
|
|
#include <rte_kni_common.h>
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "kni_dev.h"
|
|
|
|
|
2020-06-18 16:55:50 +00:00
|
|
|
MODULE_VERSION(KNI_VERSION);
|
2017-04-21 10:43:26 +00:00
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
|
|
|
MODULE_AUTHOR("Intel Corporation");
|
|
|
|
MODULE_DESCRIPTION("Kernel Module for managing kni devices");
|
|
|
|
|
|
|
|
#define KNI_RX_LOOP_NUM 1000
|
|
|
|
|
|
|
|
#define KNI_MAX_DEVICES 32
|
|
|
|
|
|
|
|
/* loopback mode */
|
2018-05-15 09:49:22 +00:00
|
|
|
static char *lo_mode;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
/* Kernel thread mode */
|
2018-05-15 09:49:22 +00:00
|
|
|
static char *kthread_mode;
|
|
|
|
static uint32_t multiple_kthread_on;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
/* Default carrier state for created KNI network interfaces */
|
|
|
|
static char *carrier;
|
2020-06-18 16:55:50 +00:00
|
|
|
uint32_t kni_dflt_carrier;
|
2019-06-25 11:12:58 +00:00
|
|
|
|
2022-09-02 04:40:05 +00:00
|
|
|
/* Request processing support for bifurcated drivers. */
|
|
|
|
static char *enable_bifurcated;
|
|
|
|
uint32_t bifurcated_support;
|
|
|
|
|
2017-04-21 10:43:26 +00:00
|
|
|
#define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
|
|
|
|
|
|
|
|
static int kni_net_id;
|
|
|
|
|
|
|
|
struct kni_net {
|
|
|
|
unsigned long device_in_use; /* device in use flag */
|
2018-05-15 09:49:22 +00:00
|
|
|
struct mutex kni_kthread_lock;
|
2017-04-21 10:43:26 +00:00
|
|
|
struct task_struct *kni_kthread;
|
|
|
|
struct rw_semaphore kni_list_lock;
|
|
|
|
struct list_head kni_list_head;
|
|
|
|
};
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
static int __net_init
|
|
|
|
kni_init_net(struct net *net)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
|
|
|
struct kni_net *knet = net_generic(net, kni_net_id);
|
2018-05-15 09:49:22 +00:00
|
|
|
|
|
|
|
memset(knet, 0, sizeof(*knet));
|
2017-04-21 10:43:26 +00:00
|
|
|
#else
|
|
|
|
struct kni_net *knet;
|
|
|
|
int ret;
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
|
2017-04-21 10:43:26 +00:00
|
|
|
if (!knet) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clear the bit of device in use */
|
|
|
|
clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
mutex_init(&knet->kni_kthread_lock);
|
|
|
|
|
2017-04-21 10:43:26 +00:00
|
|
|
init_rwsem(&knet->kni_list_lock);
|
|
|
|
INIT_LIST_HEAD(&knet->kni_list_head);
|
|
|
|
|
|
|
|
#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
ret = net_assign_generic(net, kni_net_id, knet);
|
|
|
|
if (ret < 0)
|
|
|
|
kfree(knet);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
static void __net_exit
|
|
|
|
kni_exit_net(struct net *net)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
2018-05-15 09:49:22 +00:00
|
|
|
struct kni_net *knet __maybe_unused;
|
|
|
|
|
|
|
|
knet = net_generic(net, kni_net_id);
|
|
|
|
mutex_destroy(&knet->kni_kthread_lock);
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
#ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
2017-04-21 10:43:26 +00:00
|
|
|
kfree(knet);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations kni_net_ops = {
|
|
|
|
.init = kni_init_net,
|
|
|
|
.exit = kni_exit_net,
|
|
|
|
#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
|
|
|
.id = &kni_net_id,
|
|
|
|
.size = sizeof(struct kni_net),
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
static int
|
|
|
|
kni_thread_single(void *data)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
2018-05-15 09:49:22 +00:00
|
|
|
struct kni_net *knet = data;
|
|
|
|
int j;
|
|
|
|
struct kni_dev *dev;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
down_read(&knet->kni_list_lock);
|
|
|
|
for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
|
|
|
|
list_for_each_entry(dev, &knet->kni_list_head, list) {
|
|
|
|
kni_net_rx(dev);
|
|
|
|
kni_net_poll_resp(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up_read(&knet->kni_list_lock);
|
|
|
|
#ifdef RTE_KNI_PREEMPT_DEFAULT
|
|
|
|
/* reschedule out for a while */
|
|
|
|
schedule_timeout_interruptible(
|
|
|
|
usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
|
2017-04-21 10:43:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
static int
|
|
|
|
kni_thread_multiple(void *param)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
2018-05-15 09:49:22 +00:00
|
|
|
int j;
|
|
|
|
struct kni_dev *dev = param;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
|
|
|
|
kni_net_rx(dev);
|
|
|
|
kni_net_poll_resp(dev);
|
|
|
|
}
|
|
|
|
#ifdef RTE_KNI_PREEMPT_DEFAULT
|
|
|
|
schedule_timeout_interruptible(
|
|
|
|
usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
|
|
|
|
#endif
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kni_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct net *net = current->nsproxy->net_ns;
|
|
|
|
struct kni_net *knet = net_generic(net, kni_net_id);
|
|
|
|
|
|
|
|
/* kni device can be opened by one user only per netns */
|
|
|
|
if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
file->private_data = get_net(net);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("/dev/kni opened\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kni_dev_remove(struct kni_dev *dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2022-09-02 04:40:05 +00:00
|
|
|
/*
|
|
|
|
* The memory of kni device is allocated and released together
|
|
|
|
* with net device. Release mbuf before freeing net device.
|
|
|
|
*/
|
|
|
|
kni_net_release_fifo_phy(dev);
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
if (dev->net_dev) {
|
|
|
|
unregister_netdev(dev->net_dev);
|
|
|
|
free_netdev(dev->net_dev);
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kni_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct net *net = file->private_data;
|
|
|
|
struct kni_net *knet = net_generic(net, kni_net_id);
|
|
|
|
struct kni_dev *dev, *n;
|
|
|
|
|
|
|
|
/* Stop kernel thread for single mode */
|
|
|
|
if (multiple_kthread_on == 0) {
|
2018-05-15 09:49:22 +00:00
|
|
|
mutex_lock(&knet->kni_kthread_lock);
|
2017-04-21 10:43:26 +00:00
|
|
|
/* Stop kernel thread */
|
2018-05-15 09:49:22 +00:00
|
|
|
if (knet->kni_kthread != NULL) {
|
|
|
|
kthread_stop(knet->kni_kthread);
|
|
|
|
knet->kni_kthread = NULL;
|
|
|
|
}
|
|
|
|
mutex_unlock(&knet->kni_kthread_lock);
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
down_write(&knet->kni_list_lock);
|
|
|
|
list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
|
|
|
|
/* Stop kernel thread for multiple mode */
|
|
|
|
if (multiple_kthread_on && dev->pthread != NULL) {
|
|
|
|
kthread_stop(dev->pthread);
|
|
|
|
dev->pthread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del(&dev->list);
|
2022-09-02 04:40:05 +00:00
|
|
|
kni_dev_remove(dev);
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
up_write(&knet->kni_list_lock);
|
|
|
|
|
|
|
|
/* Clear the bit of device in use */
|
|
|
|
clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
|
|
|
|
|
|
|
|
put_net(net);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("/dev/kni closed\n");
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-05-15 09:49:22 +00:00
|
|
|
kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
2018-05-15 09:49:22 +00:00
|
|
|
if (!kni || !dev)
|
|
|
|
return -1;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
/* Check if network name has been used */
|
|
|
|
if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
|
|
|
|
pr_err("KNI name %s duplicated\n", dev->name);
|
|
|
|
return -1;
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-05-15 09:49:22 +00:00
|
|
|
kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
2018-05-15 09:49:22 +00:00
|
|
|
/**
|
|
|
|
* Create a new kernel thread for multiple mode, set its core affinity,
|
|
|
|
* and finally wake it up.
|
|
|
|
*/
|
|
|
|
if (multiple_kthread_on) {
|
|
|
|
kni->pthread = kthread_create(kni_thread_multiple,
|
|
|
|
(void *)kni, "kni_%s", kni->name);
|
|
|
|
if (IS_ERR(kni->pthread)) {
|
|
|
|
kni_dev_remove(kni);
|
|
|
|
return -ECANCELED;
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
if (force_bind)
|
|
|
|
kthread_bind(kni->pthread, kni->core_id);
|
|
|
|
wake_up_process(kni->pthread);
|
|
|
|
} else {
|
|
|
|
mutex_lock(&knet->kni_kthread_lock);
|
|
|
|
|
|
|
|
if (knet->kni_kthread == NULL) {
|
|
|
|
knet->kni_kthread = kthread_create(kni_thread_single,
|
|
|
|
(void *)knet, "kni_single");
|
|
|
|
if (IS_ERR(knet->kni_kthread)) {
|
|
|
|
mutex_unlock(&knet->kni_kthread_lock);
|
|
|
|
kni_dev_remove(kni);
|
|
|
|
return -ECANCELED;
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
if (force_bind)
|
|
|
|
kthread_bind(knet->kni_kthread, kni->core_id);
|
|
|
|
wake_up_process(knet->kni_kthread);
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
mutex_unlock(&knet->kni_kthread_lock);
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-05-15 09:49:22 +00:00
|
|
|
kni_ioctl_create(struct net *net, uint32_t ioctl_num,
|
|
|
|
unsigned long ioctl_param)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
|
|
|
struct kni_net *knet = net_generic(net, kni_net_id);
|
|
|
|
int ret;
|
|
|
|
struct rte_kni_device_info dev_info;
|
|
|
|
struct net_device *net_dev = NULL;
|
|
|
|
struct kni_dev *kni, *dev, *n;
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_info("Creating kni...\n");
|
2017-04-21 10:43:26 +00:00
|
|
|
/* Check the buffer size, to avoid warning */
|
|
|
|
if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Copy kni info from user space */
|
2019-11-23 08:13:38 +00:00
|
|
|
if (copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info)))
|
|
|
|
return -EFAULT;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
/* Check if name is zero-ended */
|
|
|
|
if (strnlen(dev_info.name, sizeof(dev_info.name)) == sizeof(dev_info.name)) {
|
|
|
|
pr_err("kni.name not zero-terminated");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:43:26 +00:00
|
|
|
/**
|
2018-05-15 09:49:22 +00:00
|
|
|
* Check if the cpu core id is valid for binding.
|
2017-04-21 10:43:26 +00:00
|
|
|
*/
|
2018-05-15 09:49:22 +00:00
|
|
|
if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
|
|
|
|
pr_err("cpu %u is not online\n", dev_info.core_id);
|
2017-04-21 10:43:26 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it has been created */
|
|
|
|
down_read(&knet->kni_list_lock);
|
|
|
|
list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
|
|
|
|
if (kni_check_param(dev, &dev_info) < 0) {
|
|
|
|
up_read(&knet->kni_list_lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up_read(&knet->kni_list_lock);
|
|
|
|
|
|
|
|
net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
|
2018-05-15 09:49:22 +00:00
|
|
|
#ifdef NET_NAME_USER
|
|
|
|
NET_NAME_USER,
|
2017-04-21 10:43:26 +00:00
|
|
|
#endif
|
|
|
|
kni_net_init);
|
|
|
|
if (net_dev == NULL) {
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_err("error allocating device \"%s\"\n", dev_info.name);
|
2017-04-21 10:43:26 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_net_set(net_dev, net);
|
|
|
|
|
|
|
|
kni = netdev_priv(net_dev);
|
|
|
|
|
|
|
|
kni->net_dev = net_dev;
|
|
|
|
kni->core_id = dev_info.core_id;
|
|
|
|
strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
|
|
|
|
|
|
|
|
/* Translate user space info into kernel space info */
|
2020-06-18 16:55:50 +00:00
|
|
|
if (dev_info.iova_mode) {
|
|
|
|
#ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
|
|
|
|
kni->tx_q = iova_to_kva(current, dev_info.tx_phys);
|
|
|
|
kni->rx_q = iova_to_kva(current, dev_info.rx_phys);
|
|
|
|
kni->alloc_q = iova_to_kva(current, dev_info.alloc_phys);
|
|
|
|
kni->free_q = iova_to_kva(current, dev_info.free_phys);
|
|
|
|
|
|
|
|
kni->req_q = iova_to_kva(current, dev_info.req_phys);
|
|
|
|
kni->resp_q = iova_to_kva(current, dev_info.resp_phys);
|
|
|
|
kni->sync_va = dev_info.sync_va;
|
|
|
|
kni->sync_kva = iova_to_kva(current, dev_info.sync_phys);
|
|
|
|
kni->usr_tsk = current;
|
|
|
|
kni->iova_mode = 1;
|
|
|
|
#else
|
|
|
|
pr_err("KNI module does not support IOVA to VA translation\n");
|
|
|
|
return -EINVAL;
|
|
|
|
#endif
|
|
|
|
} else {
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2020-06-18 16:55:50 +00:00
|
|
|
kni->tx_q = phys_to_virt(dev_info.tx_phys);
|
|
|
|
kni->rx_q = phys_to_virt(dev_info.rx_phys);
|
|
|
|
kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
|
|
|
|
kni->free_q = phys_to_virt(dev_info.free_phys);
|
|
|
|
|
|
|
|
kni->req_q = phys_to_virt(dev_info.req_phys);
|
|
|
|
kni->resp_q = phys_to_virt(dev_info.resp_phys);
|
|
|
|
kni->sync_va = dev_info.sync_va;
|
|
|
|
kni->sync_kva = phys_to_virt(dev_info.sync_phys);
|
|
|
|
kni->iova_mode = 0;
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
kni->mbuf_size = dev_info.mbuf_size;
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(unsigned long long) dev_info.tx_phys, kni->tx_q);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(unsigned long long) dev_info.rx_phys, kni->rx_q);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(unsigned long long) dev_info.alloc_phys, kni->alloc_q);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("free_phys: 0x%016llx, free_q addr: 0x%p\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(unsigned long long) dev_info.free_phys, kni->free_q);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("req_phys: 0x%016llx, req_q addr: 0x%p\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(unsigned long long) dev_info.req_phys, kni->req_q);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(unsigned long long) dev_info.resp_phys, kni->resp_q);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("mbuf_size: %u\n", kni->mbuf_size);
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2020-06-18 16:55:50 +00:00
|
|
|
/* if user has provided a valid mac address */
|
2022-09-02 04:40:05 +00:00
|
|
|
if (is_valid_ether_addr(dev_info.mac_addr)) {
|
|
|
|
#ifdef HAVE_ETH_HW_ADDR_SET
|
|
|
|
eth_hw_addr_set(net_dev, dev_info.mac_addr);
|
|
|
|
#else
|
2020-06-18 16:55:50 +00:00
|
|
|
memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
|
2022-09-02 04:40:05 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* Assign random MAC address. */
|
|
|
|
eth_hw_addr_random(net_dev);
|
|
|
|
}
|
2019-06-25 11:12:58 +00:00
|
|
|
|
|
|
|
if (dev_info.mtu)
|
|
|
|
net_dev->mtu = dev_info.mtu;
|
2020-06-18 16:55:50 +00:00
|
|
|
#ifdef HAVE_MAX_MTU_PARAM
|
|
|
|
net_dev->max_mtu = net_dev->mtu;
|
|
|
|
|
|
|
|
if (dev_info.min_mtu)
|
|
|
|
net_dev->min_mtu = dev_info.min_mtu;
|
|
|
|
|
|
|
|
if (dev_info.max_mtu)
|
|
|
|
net_dev->max_mtu = dev_info.max_mtu;
|
|
|
|
#endif
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
ret = register_netdev(net_dev);
|
|
|
|
if (ret) {
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_err("error %i registering device \"%s\"\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
ret, dev_info.name);
|
2018-05-15 09:49:22 +00:00
|
|
|
kni->net_dev = NULL;
|
2017-04-21 10:43:26 +00:00
|
|
|
kni_dev_remove(kni);
|
2018-05-15 09:49:22 +00:00
|
|
|
free_netdev(net_dev);
|
2017-04-21 10:43:26 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
netif_carrier_off(net_dev);
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
ret = kni_run_thread(knet, kni, dev_info.force_bind);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
down_write(&knet->kni_list_lock);
|
|
|
|
list_add(&kni->list, &knet->kni_list_head);
|
|
|
|
up_write(&knet->kni_list_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-05-15 09:49:22 +00:00
|
|
|
kni_ioctl_release(struct net *net, uint32_t ioctl_num,
|
|
|
|
unsigned long ioctl_param)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
|
|
|
struct kni_net *knet = net_generic(net, kni_net_id);
|
|
|
|
int ret = -EINVAL;
|
|
|
|
struct kni_dev *dev, *n;
|
|
|
|
struct rte_kni_device_info dev_info;
|
|
|
|
|
|
|
|
if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
|
2018-05-15 09:49:22 +00:00
|
|
|
return -EINVAL;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2019-11-23 08:13:38 +00:00
|
|
|
if (copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info)))
|
|
|
|
return -EFAULT;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
/* Release the network device according to its name */
|
|
|
|
if (strlen(dev_info.name) == 0)
|
2019-11-23 08:13:38 +00:00
|
|
|
return -EINVAL;
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
down_write(&knet->kni_list_lock);
|
|
|
|
list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
|
|
|
|
if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (multiple_kthread_on && dev->pthread != NULL) {
|
|
|
|
kthread_stop(dev->pthread);
|
|
|
|
dev->pthread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_del(&dev->list);
|
2022-09-02 04:40:05 +00:00
|
|
|
kni_dev_remove(dev);
|
2017-04-21 10:43:26 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
up_write(&knet->kni_list_lock);
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_info("%s release kni named %s\n",
|
2017-04-21 10:43:26 +00:00
|
|
|
(ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-02 04:40:05 +00:00
|
|
|
static long
|
|
|
|
kni_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
|
2017-04-21 10:43:26 +00:00
|
|
|
{
|
2022-09-02 04:40:05 +00:00
|
|
|
long ret = -EINVAL;
|
2017-04-21 10:43:26 +00:00
|
|
|
struct net *net = current->nsproxy->net_ns;
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch according to the ioctl called
|
|
|
|
*/
|
|
|
|
switch (_IOC_NR(ioctl_num)) {
|
|
|
|
case _IOC_NR(RTE_KNI_IOCTL_TEST):
|
|
|
|
/* For test only, not used */
|
|
|
|
break;
|
|
|
|
case _IOC_NR(RTE_KNI_IOCTL_CREATE):
|
|
|
|
ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
|
|
|
|
break;
|
|
|
|
case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
|
|
|
|
ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("IOCTL default\n");
|
2017-04-21 10:43:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-02 04:40:05 +00:00
|
|
|
static long
|
|
|
|
kni_compat_ioctl(struct file *file, unsigned int ioctl_num,
|
2017-04-21 10:43:26 +00:00
|
|
|
unsigned long ioctl_param)
|
|
|
|
{
|
|
|
|
/* 32 bits app on 64 bits OS to be supported later */
|
2018-05-15 09:49:22 +00:00
|
|
|
pr_debug("Not implemented.\n");
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
static const struct file_operations kni_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = kni_open,
|
|
|
|
.release = kni_release,
|
2022-09-02 04:40:05 +00:00
|
|
|
.unlocked_ioctl = kni_ioctl,
|
|
|
|
.compat_ioctl = kni_compat_ioctl,
|
2018-05-15 09:49:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice kni_misc = {
|
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
|
|
|
.name = KNI_DEVICE,
|
|
|
|
.fops = &kni_fops,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init
|
|
|
|
kni_parse_kthread_mode(void)
|
|
|
|
{
|
|
|
|
if (!kthread_mode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(kthread_mode, "single") == 0)
|
|
|
|
return 0;
|
|
|
|
else if (strcmp(kthread_mode, "multiple") == 0)
|
|
|
|
multiple_kthread_on = 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
static int __init
|
|
|
|
kni_parse_carrier_state(void)
|
|
|
|
{
|
|
|
|
if (!carrier) {
|
2020-06-18 16:55:50 +00:00
|
|
|
kni_dflt_carrier = 0;
|
2019-06-25 11:12:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(carrier, "off") == 0)
|
2020-06-18 16:55:50 +00:00
|
|
|
kni_dflt_carrier = 0;
|
2019-06-25 11:12:58 +00:00
|
|
|
else if (strcmp(carrier, "on") == 0)
|
2020-06-18 16:55:50 +00:00
|
|
|
kni_dflt_carrier = 1;
|
2019-06-25 11:12:58 +00:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-02 04:40:05 +00:00
|
|
|
static int __init
|
|
|
|
kni_parse_bifurcated_support(void)
|
|
|
|
{
|
|
|
|
if (!enable_bifurcated) {
|
|
|
|
bifurcated_support = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(enable_bifurcated, "on") == 0)
|
|
|
|
bifurcated_support = 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
static int __init
|
|
|
|
kni_init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (kni_parse_kthread_mode() < 0) {
|
|
|
|
pr_err("Invalid parameter for kthread_mode\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multiple_kthread_on == 0)
|
|
|
|
pr_debug("Single kernel thread for all KNI devices\n");
|
|
|
|
else
|
|
|
|
pr_debug("Multiple kernel thread mode enabled\n");
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
if (kni_parse_carrier_state() < 0) {
|
|
|
|
pr_err("Invalid parameter for carrier\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:55:50 +00:00
|
|
|
if (kni_dflt_carrier == 0)
|
2019-06-25 11:12:58 +00:00
|
|
|
pr_debug("Default carrier state set to off.\n");
|
|
|
|
else
|
|
|
|
pr_debug("Default carrier state set to on.\n");
|
|
|
|
|
2022-09-02 04:40:05 +00:00
|
|
|
if (kni_parse_bifurcated_support() < 0) {
|
|
|
|
pr_err("Invalid parameter for bifurcated support\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (bifurcated_support == 1)
|
|
|
|
pr_debug("bifurcated support is enabled.\n");
|
|
|
|
|
2018-05-15 09:49:22 +00:00
|
|
|
#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
|
|
|
rc = register_pernet_subsys(&kni_net_ops);
|
|
|
|
#else
|
|
|
|
rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
|
|
|
|
#endif
|
|
|
|
if (rc)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
rc = misc_register(&kni_misc);
|
|
|
|
if (rc != 0) {
|
|
|
|
pr_err("Misc registration failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the lo mode according to the input parameter */
|
|
|
|
kni_net_config_lo_mode(lo_mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
|
|
|
unregister_pernet_subsys(&kni_net_ops);
|
|
|
|
#else
|
|
|
|
unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit
|
|
|
|
kni_exit(void)
|
|
|
|
{
|
|
|
|
misc_deregister(&kni_misc);
|
|
|
|
#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
|
|
|
|
unregister_pernet_subsys(&kni_net_ops);
|
|
|
|
#else
|
|
|
|
unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:43:26 +00:00
|
|
|
module_init(kni_init);
|
|
|
|
module_exit(kni_exit);
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
module_param(lo_mode, charp, 0644);
|
2017-04-21 10:43:26 +00:00
|
|
|
MODULE_PARM_DESC(lo_mode,
|
|
|
|
"KNI loopback mode (default=lo_mode_none):\n"
|
2019-06-25 11:12:58 +00:00
|
|
|
"\t\tlo_mode_none Kernel loopback disabled\n"
|
|
|
|
"\t\tlo_mode_fifo Enable kernel loopback with fifo\n"
|
|
|
|
"\t\tlo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"
|
|
|
|
"\t\t"
|
2017-04-21 10:43:26 +00:00
|
|
|
);
|
|
|
|
|
2019-06-25 11:12:58 +00:00
|
|
|
module_param(kthread_mode, charp, 0644);
|
2017-04-21 10:43:26 +00:00
|
|
|
MODULE_PARM_DESC(kthread_mode,
|
|
|
|
"Kernel thread mode (default=single):\n"
|
2019-06-25 11:12:58 +00:00
|
|
|
"\t\tsingle Single kernel thread mode enabled.\n"
|
|
|
|
"\t\tmultiple Multiple kernel thread mode enabled.\n"
|
|
|
|
"\t\t"
|
|
|
|
);
|
|
|
|
|
|
|
|
module_param(carrier, charp, 0644);
|
|
|
|
MODULE_PARM_DESC(carrier,
|
|
|
|
"Default carrier state for KNI interface (default=off):\n"
|
|
|
|
"\t\toff Interfaces will be created with carrier state set to off.\n"
|
|
|
|
"\t\ton Interfaces will be created with carrier state set to on.\n"
|
|
|
|
"\t\t"
|
2017-04-21 10:43:26 +00:00
|
|
|
);
|
2022-09-02 04:40:05 +00:00
|
|
|
|
|
|
|
module_param(enable_bifurcated, charp, 0644);
|
|
|
|
MODULE_PARM_DESC(enable_bifurcated,
|
|
|
|
"Enable request processing support for bifurcated drivers, "
|
|
|
|
"which means releasing rtnl_lock before calling userspace callback and "
|
|
|
|
"supporting async requests (default=off):\n"
|
|
|
|
"\t\ton Enable request processing support for bifurcated drivers.\n"
|
|
|
|
"\t\t"
|
|
|
|
);
|