顯示具有 Linux Kernel 標籤的文章。 顯示所有文章
顯示具有 Linux Kernel 標籤的文章。 顯示所有文章

2017年9月27日 星期三

Namespace : net_generic

include/net/netns/generic.h
/*
 * Generic net pointers are to be used by modules to put some private
 * stuff on the struct net without explicit struct net modification
 *
 * The rules are simple:
 * 1. set pernet_operations->id.  After register_pernet_device you
 *    will have the id of your private pointer.
 * 2. set pernet_operations->size to have the code allocate and free
 *    a private structure pointed to from struct net.
 * 3. do not change this pointer while the net is alive;
 * 4. do not try to have any private reference on the net_generic object.
 *
 * After accomplishing all of the above, the private pointer can be
 * accessed with the net_generic() call.
 */

struct net_generic {
    union {
        struct {
            unsigned int len;
            struct rcu_head rcu;
        } s;

        void *ptr[0];
    };
};

static inline void *net_generic(const struct net *net, unsigned int id)
{
    struct net_generic *ng;
    void *ptr;

    rcu_read_lock();
    ng = rcu_dereference(net->gen);
    ptr = ng->ptr[id];
    rcu_read_unlock();

    return ptr;
}

➠ example
lib/librte_eal/linuxapp/kni/kni_misc.c

static int kni_net_id;

struct kni_net {
    unsigned long device_in_use; /* device in use flag */
    struct mutex kni_kthread_lock;
    struct task_struct *kni_kthread;
    struct rw_semaphore kni_list_lock;
    struct list_head kni_list_head;

};

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

static int __init
kni_init(void)

{
...
    rc = register_pernet_subsys(&kni_net_ops);
...
}

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);
...
}

include/net/net_namespace.h
struct pernet_operations {
    struct list_head list;
    int (*init)(struct net *net);
    void (*exit)(struct net *net);
    void (*exit_batch)(struct list_head *net_exit_list);
    unsigned int *id;
    size_t size;

};

net/core/net_namespace.c
/**
 *      register_pernet_subsys - register a network namespace subsystem
 *  @ops:  pernet operations structure for the subsystem
 *
 *  Register a subsystem which has init and exit functions
 *  that are called when network namespaces are created and
 *  destroyed respectively.
 *
 *  When registered all network namespace init functions are
 *  called for every existing network namespace.  Allowing kernel
 *  modules to have a race free view of the set of network namespaces.
 *
 *  When a new network namespace is created all of the init
 *  methods are called in the order in which they were registered.
 *
 *  When a network namespace is destroyed all of the exit methods
 *  are called in the reverse of the order with which they were
 *  registered.
 */
int register_pernet_subsys(struct pernet_operations *ops)
{
    int error;
    mutex_lock(&net_mutex);
    error =  register_pernet_operations(first_device, ops);
    mutex_unlock(&net_mutex);
    return error;
}

EXPORT_SYMBOL_GPL(register_pernet_subsys);



2017年9月7日 星期四

Ubuntu 17.04編譯kernel 4.13

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.04
Release: 17.04
Codename: zesty

# uname -a
Linux instance-1 4.10.0-32-generic #36-Ubuntu SMP Tue Aug 8 12:10:06 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.13.tar.xz
# tar Jxvf linux-4.13.tar.xz
# apt-get install libncurses5-dev make gcc bc libssl-dev
# make menuconfig


# make -j 2
# make modules_install install

# ls /boot/*4.13*
/boot/config-4.13.0
/boot/initrd.img-4.13.0
/boot/System.map-4.13.0
/boot/vmlinuz-4.13.0

How to create Linux Kernel Headers from Linux Kernel Source?
➠ Ubuntu distribution
# apt-get install linux-headers-$(uname -r)
dpkg -l | grep linux-headers-$(uname -r)
ii  linux-headers-3.16.0-77-generic        3.16.0-77.99~14.04.1                       amd64        Linux kernel headers for version 3.16.0 on 64 bit x86 SMP

# ls -al /lib/modules/`uname -r`/
lrwxrwxrwx  1 root root     40 Jun 28  2016 build -> /usr/src/linux-headers-3.16.0-77-generic
ls /usr/src/linux-headers-3.16.0-77
arch  block  crypto  Documentation  drivers  firmware  fs  include  init  ipc  Kbuild  Kconfig  kernel  lib  Makefile  mm  net  samples  scripts  security  sound  tools  ubuntu  usr  virt

➠ Kernel.org
# make headers_install
http://smilejay.com/2013/03/update-linux-headers/

參考資料:
kernel source code