Module: xenomai-gch
Branch: for-forge
Commit: 43c51d042841990dec15e1b9afee37fad73ad0fc
URL:    
http://git.xenomai.org/?p=xenomai-gch.git;a=commit;h=43c51d042841990dec15e1b9afee37fad73ad0fc

Author: Gilles Chanteperdrix <gilles.chanteperd...@xenomai.org>
Date:   Tue Nov 11 17:20:41 2014 +0100

rtnet: rtskb_pool changes

An rtskb_pool now has lock/unlock operations, called for the first dequeued
buffer and the last queued one. At this chance, the struct rtskb_pool is
introduced, and used everywhere a pool was used.

rtskb_module_pool_init creates an rtskb_pool whose lock/unlock operations
lock a module (the module from which rtskb_module_pool_init was called).

The socket skb_pool lock/unlocks the socket rtdm_fd.

Drivers pool not fixed yet.

---

 kernel/drivers/net/addons/cap.c                    |   24 +-
 kernel/drivers/net/addons/proxy.c                  |    4 +-
 kernel/drivers/net/stack/include/rtdev.h           |   15 +-
 .../drivers/net/stack/include/rtmac/rtmac_disc.h   |    8 +-
 .../drivers/net/stack/include/rtmac/rtmac_vnic.h   |    2 +-
 kernel/drivers/net/stack/include/rtmac/tdma/tdma.h |   14 +-
 kernel/drivers/net/stack/include/rtnet_socket.h    |    6 +-
 kernel/drivers/net/stack/include/rtskb.h           |   74 ++--
 kernel/drivers/net/stack/include/rtwlan.h          |   20 +-
 kernel/drivers/net/stack/include/stack_mgr.h       |    8 +-
 kernel/drivers/net/stack/ipv4/icmp.c               |   31 +-
 kernel/drivers/net/stack/ipv4/ip_fragment.c        |    9 +-
 kernel/drivers/net/stack/ipv4/tcp/tcp.c            |   17 +-
 kernel/drivers/net/stack/ipv4/udp/udp.c            |  360 ++++++++++----------
 kernel/drivers/net/stack/packet/af_packet.c        |  329 +++++++++---------
 kernel/drivers/net/stack/rtcfg/rtcfg_frame.c       |    4 +-
 .../drivers/net/stack/rtmac/nomac/nomac_module.c   |    5 +-
 kernel/drivers/net/stack/rtmac/rtmac_disc.c        |   13 +-
 kernel/drivers/net/stack/rtmac/rtmac_syms.c        |    2 +-
 kernel/drivers/net/stack/rtmac/rtmac_vnic.c        |   28 +-
 kernel/drivers/net/stack/rtmac/tdma/tdma_ioctl.c   |    4 +-
 kernel/drivers/net/stack/rtskb.c                   |  163 ++++++---
 kernel/drivers/net/stack/socket.c                  |   48 +--
 kernel/drivers/net/stack/stack_mgr.c               |   76 ++---
 24 files changed, 678 insertions(+), 586 deletions(-)

diff --git a/kernel/drivers/net/addons/cap.c b/kernel/drivers/net/addons/cap.c
index 5586c77..e72122e 100644
--- a/kernel/drivers/net/addons/cap.c
+++ b/kernel/drivers/net/addons/cap.c
@@ -54,7 +54,7 @@ MODULE_PARM_DESC(rtcap_rtskbs, "Number of real-time socket 
buffers per "
 
 static rtdm_nrtsig_t        cap_signal;
 static struct rtskb_queue   cap_queue;
-static struct rtskb_queue   cap_pool;
+static struct rtskb_pool   cap_pool;
 
 static struct tap_device_t {
     struct net_device       *tap_dev;
@@ -69,13 +69,10 @@ static struct tap_device_t {
 
 void rtcap_rx_hook(struct rtskb *rtskb)
 {
-    if ((rtskb->cap_comp_skb = rtskb_dequeue(&cap_pool)) == 0) {
+    if ((rtskb->cap_comp_skb = rtskb_pool_dequeue(&cap_pool)) == 0) {
        tap_device[rtskb->rtdev->ifindex].tap_dev_stats.rx_dropped++;
        return;
     }
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    cap_pool.pool_balance--;
-#endif
 
     if (cap_queue.first == NULL)
        cap_queue.first = rtskb;
@@ -97,13 +94,10 @@ int rtcap_xmit_hook(struct rtskb *rtskb, struct 
rtnet_device *rtdev)
     rtdm_lockctx_t      context;
 
 
-    if ((rtskb->cap_comp_skb = rtskb_dequeue(&cap_pool)) == 0) {
+    if ((rtskb->cap_comp_skb = rtskb_pool_dequeue(&cap_pool)) == 0) {
        tap_dev->tap_dev_stats.rx_dropped++;
        return tap_dev->orig_xmit(rtskb, rtdev);
     }
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    cap_pool.pool_balance--;
-#endif
 
     rtskb->cap_next  = NULL;
     rtskb->cap_start = rtskb->data;
@@ -156,10 +150,7 @@ void rtcap_kfree_rtskb(struct rtskb *rtskb)
 
        rtdm_lock_put_irqrestore(&rtcap_lock, context);
 
-       rtskb_queue_tail(comp_skb->pool, comp_skb);
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-       comp_skb->pool->pool_balance++;
-#endif
+       rtskb_pool_queue_tail(comp_skb->pool, comp_skb);
 
        return;
     }
@@ -167,10 +158,7 @@ void rtcap_kfree_rtskb(struct rtskb *rtskb)
     rtdm_lock_put_irqrestore(&rtcap_lock, context);
 
     rtskb->chain_end = rtskb;
-    rtskb_queue_tail(rtskb->pool, rtskb);
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    rtskb->pool->pool_balance++;
-#endif
+    rtskb_pool_queue_tail(rtskb->pool, rtskb);
 }
 
 
@@ -510,7 +498,7 @@ int __init rtcap_init(void)
        goto error2;
     }
 
-    if (rtskb_pool_init(&cap_pool, rtcap_rtskbs * devices) <
+    if (rtskb_module_pool_init(&cap_pool, rtcap_rtskbs * devices) <
            rtcap_rtskbs * devices) {
        rtskb_pool_release(&cap_pool);
        ret = -ENOMEM;
diff --git a/kernel/drivers/net/addons/proxy.c 
b/kernel/drivers/net/addons/proxy.c
index 1fedd7d..7c8c492 100644
--- a/kernel/drivers/net/addons/proxy.c
+++ b/kernel/drivers/net/addons/proxy.c
@@ -71,7 +71,7 @@ static unsigned int proxy_rtskbs = DEFAULT_PROXY_RTSKBS;
 module_param(proxy_rtskbs, uint, 0444);
 MODULE_PARM_DESC(proxy_rtskbs, "Number of realtime socket buffers in proxy 
pool");
 
-static struct rtskb_queue rtskb_pool;
+static struct rtskb_pool rtskb_pool;
 
 static struct rtskb_queue tx_queue;
 static struct rtskb_queue rx_queue;
@@ -361,7 +361,7 @@ static int __init rtnetproxy_init_module(void)
 #endif
 
     /* Initialize the proxy's rtskb pool (JK) */
-    if (rtskb_pool_init(&rtskb_pool, proxy_rtskbs) < proxy_rtskbs) {
+    if (rtskb_module_pool_init(&rtskb_pool, proxy_rtskbs) < proxy_rtskbs) {
        err = -ENOMEM;
        goto err1;
     }
diff --git a/kernel/drivers/net/stack/include/rtdev.h 
b/kernel/drivers/net/stack/include/rtdev.h
index 868e3bb..d9ca6cc 100644
--- a/kernel/drivers/net/stack/include/rtdev.h
+++ b/kernel/drivers/net/stack/include/rtdev.h
@@ -48,10 +48,10 @@
 
 
 enum rtnet_link_state {
-       __RTNET_LINK_STATE_XOFF = 0,
-       __RTNET_LINK_STATE_START,
-       __RTNET_LINK_STATE_PRESENT,
-       __RTNET_LINK_STATE_NOCARRIER,
+        __RTNET_LINK_STATE_XOFF = 0,
+        __RTNET_LINK_STATE_START,
+        __RTNET_LINK_STATE_PRESENT,
+        __RTNET_LINK_STATE_NOCARRIER,
 };
 
 /***
@@ -143,8 +143,8 @@ struct rtnet_device {
     unsigned int        (*get_mtu)(struct rtnet_device *rtdev,
                                    unsigned int priority);
 
-    int                 (*do_ioctl)(struct rtnet_device *rtdev, 
-                                   unsigned int request, void * cmd);
+    int                 (*do_ioctl)(struct rtnet_device *rtdev,
+                                    unsigned int request, void * cmd);
     struct net_device_stats *(*get_stats)(struct rtnet_device *rtdev);
 
     /* DMA pre-mapping hooks */
@@ -199,12 +199,13 @@ struct rtnet_device *rtdev_get_loopback(void);
 
 static inline void rtdev_reference(struct rtnet_device *rtdev)
 {
+    smp_mb__before_atomic();
     atomic_inc(&rtdev->refcount);
 }
 
 static inline void rtdev_dereference(struct rtnet_device *rtdev)
 {
-    smp_mb__before_atomic_dec();
+    smp_mb__before_atomic();
     atomic_dec(&rtdev->refcount);
 }
 
diff --git a/kernel/drivers/net/stack/include/rtmac/rtmac_disc.h 
b/kernel/drivers/net/stack/include/rtmac/rtmac_disc.h
index 58663ce..09c87ab 100644
--- a/kernel/drivers/net/stack/include/rtmac/rtmac_disc.h
+++ b/kernel/drivers/net/stack/include/rtmac/rtmac_disc.h
@@ -41,7 +41,7 @@ struct rtmac_priv {
     int (*orig_start_xmit)(struct rtskb *skb, struct rtnet_device *dev);
     struct net_device       *vnic;
     struct net_device_stats vnic_stats;
-    struct rtskb_queue      vnic_skb_pool;
+    struct rtskb_pool       vnic_skb_pool;
     unsigned int            vnic_max_mtu;
 
     u8                      disc_priv[0] __attribute__ ((aligned(16)));
@@ -77,13 +77,17 @@ struct rtmac_disc {
     struct rtnet_ioctls ioctls;
 
     struct rtmac_proc_entry *proc_entries;
+
+    struct module *owner;
 };
 
 
 int rtmac_disc_attach(struct rtnet_device *rtdev, struct rtmac_disc *disc);
 int rtmac_disc_detach(struct rtnet_device *rtdev);
 
-int rtmac_disc_register(struct rtmac_disc *disc);
+int __rtmac_disc_register(struct rtmac_disc *disc, struct module *module);
+#define rtmac_disc_register(disc) __rtmac_disc_register(disc, THIS_MODULE)
+
 void rtmac_disc_deregister(struct rtmac_disc *disc);
 
 #ifdef CONFIG_PROC_FS
diff --git a/kernel/drivers/net/stack/include/rtmac/rtmac_vnic.h 
b/kernel/drivers/net/stack/include/rtmac/rtmac_vnic.h
index 1adf295..393d579 100644
--- a/kernel/drivers/net/stack/include/rtmac/rtmac_vnic.h
+++ b/kernel/drivers/net/stack/include/rtmac/rtmac_vnic.h
@@ -39,7 +39,7 @@ int rtmac_vnic_xmit(struct sk_buff *skb, struct net_device 
*dev);
 void rtmac_vnic_set_max_mtu(struct rtnet_device *rtdev, unsigned int max_mtu);
 
 int rtmac_vnic_add(struct rtnet_device *rtdev, vnic_xmit_handler vnic_xmit);
-void rtmac_vnic_unregister(struct rtnet_device *rtdev);
+int rtmac_vnic_unregister(struct rtnet_device *rtdev);
 
 static inline void rtmac_vnic_cleanup(struct rtnet_device *rtdev)
 {
diff --git a/kernel/drivers/net/stack/include/rtmac/tdma/tdma.h 
b/kernel/drivers/net/stack/include/rtmac/tdma/tdma.h
index 1e305be..0275672 100644
--- a/kernel/drivers/net/stack/include/rtmac/tdma/tdma.h
+++ b/kernel/drivers/net/stack/include/rtmac/tdma/tdma.h
@@ -113,11 +113,11 @@ struct tdma_priv {
 
 #ifdef ALIGN_RTOS_TASK
     __u8                        __align[(ALIGN_RTOS_TASK -
-                                        ((sizeof(unsigned int) +
-                                          sizeof(struct rtnet_device *) +
-                                          sizeof(struct rtdm_device)
-                                         ) & (ALIGN_RTOS_TASK-1))
-                                        ) & (ALIGN_RTOS_TASK-1)];
+                                         ((sizeof(unsigned int) +
+                                           sizeof(struct rtnet_device *) +
+                                           sizeof(struct rtdm_device)
+                                          ) & (ALIGN_RTOS_TASK-1))
+                                         ) & (ALIGN_RTOS_TASK-1)];
 #endif
     rtdm_task_t                 worker_task;
     rtdm_event_t                worker_wakeup;
@@ -145,7 +145,7 @@ struct tdma_priv {
     rtdm_lock_t                 lock;
 
 #ifdef CONFIG_XENO_DRIVERS_NET_TDMA_MASTER
-    struct rtskb_queue          cal_rtskb_pool;
+    struct rtskb_pool           cal_rtskb_pool;
     u64                         cycle_period;
     u64                         backup_sync_inc;
 #endif
@@ -162,7 +162,7 @@ extern struct rtmac_disc        tdma_disc;
     struct tdma_job *entry; \
     rtdm_printk("%s:%d - ", __FUNCTION__, __LINE__); \
     list_for_each_entry(entry, &tdma->first_job->entry, entry) \
-       rtdm_printk("%d ", entry->id); \
+        rtdm_printk("%d ", entry->id); \
     rtdm_printk("\n"); \
 } while (0)
 
diff --git a/kernel/drivers/net/stack/include/rtnet_socket.h 
b/kernel/drivers/net/stack/include/rtnet_socket.h
index 2ba9f30..ff63f7f 100644
--- a/kernel/drivers/net/stack/include/rtnet_socket.h
+++ b/kernel/drivers/net/stack/include/rtnet_socket.h
@@ -41,7 +41,7 @@
 struct rtsocket {
     unsigned short          protocol;
 
-    struct rtskb_queue      skb_pool;
+    struct rtskb_pool       skb_pool;
     unsigned int            pool_size;
     struct mutex            pool_nrt_lock;
 
@@ -92,7 +92,7 @@ static inline struct rtdm_fd *rt_socket_fd(struct rtsocket 
*sock)
     rtdm_fd_unlock(rt_socket_fd(sock))
 
 int rt_socket_init(struct rtdm_fd *fd, unsigned short protocol);
-int rt_socket_cleanup(struct rtdm_fd *fd);
+void rt_socket_cleanup(struct rtdm_fd *fd);
 int rt_socket_common_ioctl(struct rtdm_fd *fd, int request, void *arg);
 int rt_socket_if_ioctl(struct rtdm_fd *fd, int request, void *arg);
 int rt_socket_select_bind(struct rtdm_fd *fd,
@@ -100,7 +100,7 @@ int rt_socket_select_bind(struct rtdm_fd *fd,
                          enum rtdm_selecttype type,
                          unsigned fd_index);
 
-int rt_bare_socket_init(struct rtsocket *sock, unsigned short protocol,
+int rt_bare_socket_init(struct rtdm_fd *fd, unsigned short protocol,
                        unsigned int priority, unsigned int pool_size);
 
 static inline void rt_bare_socket_cleanup(struct rtsocket *sock)
diff --git a/kernel/drivers/net/stack/include/rtskb.h 
b/kernel/drivers/net/stack/include/rtskb.h
index 6bbc440..cd8b7cb 100644
--- a/kernel/drivers/net/stack/include/rtskb.h
+++ b/kernel/drivers/net/stack/include/rtskb.h
@@ -158,7 +158,7 @@ struct rtskb {
     struct rtskb        *chain_end; /* marks the end of a rtskb chain starting
                                        with this very rtskb */
 
-    struct rtskb_queue  *pool;      /* owning pool */
+    struct rtskb_pool   *pool;      /* owning pool */
 
     unsigned int        priority;   /* bit 0..15: prio, 16..31: user-defined */
 
@@ -214,8 +214,6 @@ struct rtskb {
 
 #ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
     unsigned char       *buf_end;
-
-    int                 chain_len;
 #endif
 
 #ifdef CONFIG_XENO_DRIVERS_NET_ADDON_RTCAP
@@ -234,9 +232,18 @@ struct rtskb_queue {
     struct rtskb        *first;
     struct rtskb        *last;
     rtdm_lock_t         lock;
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    int                 pool_balance;
-#endif
+};
+
+struct rtskb_pool_lock_ops {
+    int (*trylock)(void *cookie);
+    void (*unlock)(void *cookie);
+};
+
+struct rtskb_pool {
+    struct rtskb_queue queue;
+    const struct rtskb_pool_lock_ops *lock_ops;
+    unsigned lock_count;
+    void *lock_cookie;
 };
 
 #define QUEUE_MAX_PRIO          0
@@ -278,7 +285,11 @@ extern void rtskb_over_panic(struct rtskb *skb, int len, 
void *here);
 extern void rtskb_under_panic(struct rtskb *skb, int len, void *here);
 #endif
 
-extern struct rtskb *alloc_rtskb(unsigned int size, struct rtskb_queue *pool);
+extern struct rtskb *rtskb_pool_dequeue(struct rtskb_pool *pool);
+
+extern void rtskb_pool_queue_tail(struct rtskb_pool *pool, struct rtskb *skb);
+
+extern struct rtskb *alloc_rtskb(unsigned int size, struct rtskb_pool *pool);
 #define dev_alloc_rtskb(len, pool)  alloc_rtskb(len, pool)
 
 extern void kfree_rtskb(struct rtskb *skb);
@@ -700,44 +711,29 @@ static inline dma_addr_t rtskb_data_dma_addr(struct rtskb 
*rtskb,
     return rtskb->buf_dma_addr + rtskb->data - rtskb->buf_start + offset;
 }
 
-extern struct rtskb_queue global_pool;
+extern struct rtskb_pool global_pool;
 
-extern unsigned int rtskb_pool_init(struct rtskb_queue *pool,
-                                    unsigned int initial_size);
-extern unsigned int rtskb_pool_init_rt(struct rtskb_queue *pool,
-                                       unsigned int initial_size);
-extern void __rtskb_pool_release(struct rtskb_queue *pool);
-extern void __rtskb_pool_release_rt(struct rtskb_queue *pool);
+extern unsigned int rtskb_pool_init(struct rtskb_pool *pool,
+                                    unsigned int initial_size,
+                                    const struct rtskb_pool_lock_ops *lock_ops,
+                                    void *lock_cookie);
 
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-#define rtskb_pool_release(pool)                            \
-    do {                                                    \
-        RTNET_ASSERT((pool)->pool_balance == 0,             \
-                     rtdm_printk("pool: %p\n", (pool)););   \
-        __rtskb_pool_release((pool));                       \
-    } while (0)
-#define rtskb_pool_release_rt(pool)                         \
-    do {                                                    \
-        RTNET_ASSERT((pool)->pool_balance == 0,             \
-                     rtdm_printk("pool: %p\n", (pool)););   \
-        __rtskb_pool_release_rt((pool));                    \
-    } while (0)
-#else
-#define rtskb_pool_release      __rtskb_pool_release
-#define rtskb_pool_release_rt   __rtskb_pool_release_rt
-#endif
+extern unsigned int __rtskb_module_pool_init(struct rtskb_pool *pool,
+                                            unsigned int initial_size,
+                                            struct module *module);
+
+#define rtskb_module_pool_init(pool, size) \
+    __rtskb_module_pool_init(pool, size, THIS_MODULE)
+
+extern int rtskb_pool_release(struct rtskb_pool *pool);
 
-extern unsigned int rtskb_pool_extend(struct rtskb_queue *pool,
+extern unsigned int rtskb_pool_extend(struct rtskb_pool *pool,
                                       unsigned int add_rtskbs);
-extern unsigned int rtskb_pool_extend_rt(struct rtskb_queue *pool,
-                                         unsigned int add_rtskbs);
-extern unsigned int rtskb_pool_shrink(struct rtskb_queue *pool,
+extern unsigned int rtskb_pool_shrink(struct rtskb_pool *pool,
                                       unsigned int rem_rtskbs);
-extern unsigned int rtskb_pool_shrink_rt(struct rtskb_queue *pool,
-                                         unsigned int rem_rtskbs);
-extern int rtskb_acquire(struct rtskb *rtskb, struct rtskb_queue *comp_pool);
+extern int rtskb_acquire(struct rtskb *rtskb, struct rtskb_pool *comp_pool);
 extern struct rtskb* rtskb_clone(struct rtskb *rtskb,
-                                 struct rtskb_queue *pool);
+                                 struct rtskb_pool *pool);
 
 extern int rtskb_pools_init(void);
 extern void rtskb_pools_release(void);
diff --git a/kernel/drivers/net/stack/include/rtwlan.h 
b/kernel/drivers/net/stack/include/rtwlan.h
index d1d65e4..ef2dcd0 100644
--- a/kernel/drivers/net/stack/include/rtwlan.h
+++ b/kernel/drivers/net/stack/include/rtwlan.h
@@ -1,6 +1,6 @@
 /* rtwlan.h
  *
- * This file is a rtnet adaption from ieee80211/ieee80211.h used by the 
+ * This file is a rtnet adaption from ieee80211/ieee80211.h used by the
  * rt2x00-2.0.0-b3 sourceforge project
  *
  * Merged with mainline ieee80211.h in Aug 2004.  Original ieee802_11
@@ -17,7 +17,7 @@
  * <jketr...@linux.intel.com>
  * Copyright (c) 2004-2005, Intel Corporation
  *
- * Adaption to rtnet 
+ * Adaption to rtnet
  * Copyright (c) 2006, Daniel Gregorek <d...@gmx.de>
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -75,7 +75,7 @@
 
 /* management */
 #define IEEE80211_STYPE_ASSOC_REQ      0x0000
-#define IEEE80211_STYPE_ASSOC_RESP     0x0010
+#define IEEE80211_STYPE_ASSOC_RESP      0x0010
 #define IEEE80211_STYPE_REASSOC_REQ    0x0020
 #define IEEE80211_STYPE_REASSOC_RESP   0x0030
 #define IEEE80211_STYPE_PROBE_REQ      0x0040
@@ -112,12 +112,12 @@
 #define WLAN_FC_GET_TYPE(fc) ((fc) & IEEE80211_FCTL_FTYPE)
 #define WLAN_FC_GET_STYPE(fc) ((fc) & IEEE80211_FCTL_STYPE)
 
-#define IEEE80211_DSSS_RATE_1MB                        0x02
-#define IEEE80211_DSSS_RATE_2MB                        0x04
-#define IEEE80211_DSSS_RATE_5MB                        0x0B
-#define IEEE80211_DSSS_RATE_11MB               0x16
-#define IEEE80211_OFDM_RATE_6MB                        0x0C
-#define IEEE80211_OFDM_RATE_9MB                        0x12
+#define IEEE80211_DSSS_RATE_1MB                 0x02
+#define IEEE80211_DSSS_RATE_2MB                 0x04
+#define IEEE80211_DSSS_RATE_5MB                 0x0B
+#define IEEE80211_DSSS_RATE_11MB                0x16
+#define IEEE80211_OFDM_RATE_6MB                 0x0C
+#define IEEE80211_OFDM_RATE_9MB                 0x12
 #define IEEE80211_OFDM_RATE_12MB               0x18
 #define IEEE80211_OFDM_RATE_18MB               0x24
 #define IEEE80211_OFDM_RATE_24MB               0x30
@@ -154,7 +154,7 @@ struct rtwlan_device {
 
     struct rtwlan_stats stats;
 
-    struct rtskb_queue skb_pool;
+    struct rtskb_pool skb_pool;
 
     int mode;
 
diff --git a/kernel/drivers/net/stack/include/stack_mgr.h 
b/kernel/drivers/net/stack/include/stack_mgr.h
index fd2893b..871bbd4 100644
--- a/kernel/drivers/net/stack/include/stack_mgr.h
+++ b/kernel/drivers/net/stack/include/stack_mgr.h
@@ -48,9 +48,9 @@ struct rtpacket_type {
 
     int                 (*handler)(struct rtskb *, struct rtpacket_type *);
     int                 (*err_handler)(struct rtskb *, struct rtnet_device *,
-                                      struct rtpacket_type *);
-    bool                (*trylock)(struct rtpacket_type *)
-    void                (*unlock)(struct rtpacket_type *)
+                                       struct rtpacket_type *);
+    bool                (*trylock)(struct rtpacket_type *);
+    void                (*unlock)(struct rtpacket_type *);
 };
 
 
@@ -59,7 +59,7 @@ int rtdev_remove_pack(struct rtpacket_type *pt);
 
 static inline bool rtdev_lock_pack(struct rtpacket_type *pt)
 {
-    ++pt->recount;
+    ++pt->refcount;
     return true;
 }
 
diff --git a/kernel/drivers/net/stack/ipv4/icmp.c 
b/kernel/drivers/net/stack/ipv4/icmp.c
index 94e6ad8..4b83942 100644
--- a/kernel/drivers/net/stack/ipv4/icmp.c
+++ b/kernel/drivers/net/stack/ipv4/icmp.c
@@ -87,7 +87,8 @@ static struct {
     struct rtsocket socket;
 } icmp_socket_container;
 
-#define icmp_socket     icmp_socket_container.socket
+#define icmp_fd                (&icmp_socket_container.dummy.fd)
+#define icmp_socket     ((struct rtsocket *)rtdm_fd_to_private(icmp_fd))
 
 
 void rt_icmp_queue_echo_request(struct rt_proc_call *call)
@@ -133,7 +134,7 @@ void rt_icmp_cleanup_echo_requests(void)
     }
 
     /* purge any pending ICMP fragments */
-    rt_ip_frag_invalidate_socket(&icmp_socket);
+    rt_ip_frag_invalidate_socket(icmp_socket);
 }
 
 
@@ -194,9 +195,12 @@ static void rt_icmp_send_reply(struct icmp_bxm 
*icmp_param, struct rtskb *skb)
                           skb->rtdev->local_ip) != 0)
        return;
 
-    err = rt_ip_build_xmit(&icmp_socket, rt_icmp_glue_reply_bits, icmp_param,
+    rt_socket_reference(icmp_socket);
+    err = rt_ip_build_xmit(icmp_socket, rt_icmp_glue_reply_bits, icmp_param,
                           sizeof(struct icmphdr) + icmp_param->data_len,
                           &rt, MSG_DONTWAIT);
+    if (err)
+           rt_socket_dereference(icmp_socket);
 
     rtdev_dereference(rt.rtdev);
 
@@ -320,9 +324,13 @@ static int rt_icmp_send_request(u32 daddr, struct icmp_bxm 
*icmp_param)
     size = icmp_param->head_len + icmp_param->data_len;
     if (size + 20 /* ip header */ > rt.rtdev->get_mtu(rt.rtdev, RT_ICMP_PRIO))
        err = -EMSGSIZE;
-    else
-       err = rt_ip_build_xmit(&icmp_socket, rt_icmp_glue_request_bits,
+    else {
+       rt_socket_reference(icmp_socket);
+       err = rt_ip_build_xmit(icmp_socket, rt_icmp_glue_request_bits,
                               icmp_param, size, &rt, MSG_DONTWAIT);
+       if (err)
+           rt_socket_dereference(icmp_socket);
+    }
 
     rtdev_dereference(rt.rtdev);
 
@@ -342,7 +350,7 @@ int rt_icmp_send_echo(u32 daddr, u16 id, u16 sequence, 
size_t msg_size)
 
 
     /* first purge any potentially pending ICMP fragments */
-    rt_ip_frag_invalidate_socket(&icmp_socket);
+    rt_ip_frag_invalidate_socket(icmp_socket);
 
     icmp_param.head.icmph.type = ICMP_ECHO;
     icmp_param.head.icmph.code = 0;
@@ -440,7 +448,7 @@ struct rtsocket *rt_icmp_dest_socket(struct rtskb *skb)
 {
     /* Note that the socket's refcount is not used by this protocol.
      * The socket returned here is static and not part of the global pool. */
-    return &icmp_socket;
+    return icmp_socket;
 }
 
 
@@ -520,12 +528,13 @@ void __init rt_icmp_init(void)
     unsigned int skbs;
 
 
-    skbs = rt_bare_socket_init(&icmp_socket, IPPROTO_ICMP, RT_ICMP_PRIO,
-                              ICMP_REPLY_POOL_SIZE);
+    skbs = rt_bare_socket_init(icmp_fd, IPPROTO_ICMP, RT_ICMP_PRIO,
+                           ICMP_REPLY_POOL_SIZE);
     if (skbs < ICMP_REPLY_POOL_SIZE)
        printk("RTnet: allocated only %d icmp rtskbs\n", skbs);
 
-    icmp_socket.prot.inet.tos = 0;
+    icmp_socket->prot.inet.tos = 0;
+    icmp_fd->refs = 1;
 
     rt_inet_add_protocol(&icmp_protocol);
 }
@@ -539,5 +548,5 @@ void rt_icmp_release(void)
 {
     rt_icmp_cleanup_echo_requests();
     rt_inet_del_protocol(&icmp_protocol);
-    rt_bare_socket_cleanup(&icmp_socket);
+    rt_bare_socket_cleanup(icmp_socket);
 }
diff --git a/kernel/drivers/net/stack/ipv4/ip_fragment.c 
b/kernel/drivers/net/stack/ipv4/ip_fragment.c
index ed141e6..ca0696b 100644
--- a/kernel/drivers/net/stack/ipv4/ip_fragment.c
+++ b/kernel/drivers/net/stack/ipv4/ip_fragment.c
@@ -158,9 +158,6 @@ static struct rtskb *add_to_collector(struct rtskb *skb, 
unsigned int offset, in
 
             /* Extend the chain */
             first_skb->chain_end = skb;
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-            first_skb->chain_len++;
-#endif
 
             /* Sanity check: unordered fragments are not allowed! */
             if (offset != p_coll->buf_size) {
@@ -190,9 +187,9 @@ static struct rtskb *add_to_collector(struct rtskb *skb, 
unsigned int offset, in
 
 #ifdef CONFIG_XENO_DRIVERS_NET_ADDON_PROXY
     if (rt_ip_fallback_handler) {
-           __rtskb_push(skb, iph->ihl*4);
-           rt_ip_fallback_handler(skb);
-           return NULL;
+            __rtskb_push(skb, iph->ihl*4);
+            rt_ip_fallback_handler(skb);
+            return NULL;
     }
 #endif
 
diff --git a/kernel/drivers/net/stack/ipv4/tcp/tcp.c 
b/kernel/drivers/net/stack/ipv4/tcp/tcp.c
index c0a2496..14c43ea 100644
--- a/kernel/drivers/net/stack/ipv4/tcp/tcp.c
+++ b/kernel/drivers/net/stack/ipv4/tcp/tcp.c
@@ -175,7 +175,13 @@ struct rt_tcp_dispatched_packet_send_cmd {
 
 MODULE_LICENSE("GPL");
 
-static struct tcp_socket rst_socket;
+static struct {
+       struct rtdm_dev_context dummy;
+       struct tcp_socket rst_socket;
+} rst_socket_container;
+
+#define rst_fd         (&rst_socket_container.dummy.fd)
+#define rst_socket     (*(struct tcp_socket *)rtdm_private_to_fd(rst_fd))
 
 static u32 tcp_auto_port_start = 1024;
 static u32 tcp_auto_port_mask  = ~(RT_TCP_SOCKETS-1);
@@ -837,6 +843,7 @@ static struct rtsocket *rt_tcp_dest_socket(struct rtskb 
*skb)
            rst_socket.sync.ack_seq = rt_tcp_compute_ack_seq(th, data_len);
 
            if (rt_ip_route_output(&rst_socket.rt, daddr, saddr) == 0) {
+               rt_socket_reference(&rst_socket.sock);
                rt_tcp_send(&rst_socket, TCP_FLAG_ACK|TCP_FLAG_RST);
                rtdev_dereference(rst_socket.rt.rtdev);
            }
@@ -985,6 +992,7 @@ static void rt_tcp_rcv(struct rtskb *skb)
 
        if (rt_ip_route_output(&rst_socket.rt, rst_socket.daddr,
                               rst_socket.saddr) == 0) {
+           rt_socket_reference(&rst_socket.sock);
            rt_tcp_send(&rst_socket, TCP_FLAG_RST|TCP_FLAG_ACK);
            rtdev_dereference(rst_socket.rt.rtdev);
        }
@@ -1347,7 +1355,7 @@ static void rt_tcp_socket_destruct(struct tcp_socket* ts)
 /***
  *  rt_tcp_close
  */
-static int rt_tcp_close(struct rtdm_fd *fd)
+static void rt_tcp_close(struct rtdm_fd *fd)
 {
     struct tcp_socket* ts = rtdm_fd_to_private(fd);
     struct rt_tcp_dispatched_packet_send_cmd send_cmd;
@@ -1403,7 +1411,7 @@ static int rt_tcp_close(struct rtdm_fd *fd)
 
     rt_tcp_socket_destruct(ts);
 
-    return rt_socket_cleanup(fd);
+    rt_socket_cleanup(fd);
 }
 
 /***
@@ -2255,11 +2263,12 @@ int __init rt_tcp_init(void)
        INIT_HLIST_HEAD(&port_hash[i]);
 
     /* Perform essential initialization of the RST|ACK socket */
-    skbs = rt_bare_socket_init(&rst_socket.sock, IPPROTO_TCP, RT_TCP_RST_PRIO,
+    skbs = rt_bare_socket_init(rst_fd, IPPROTO_TCP, RT_TCP_RST_PRIO,
                               RT_TCP_RST_POOL_SIZE);
     if (skbs < RT_TCP_RST_POOL_SIZE)
        printk("rttcp: allocated only %d RST|ACK rtskbs\n", skbs);
     rst_socket.sock.prot.inet.tos = 0;
+    rst_fd->refs = 1;
     rtdm_lock_init(&rst_socket.socket_lock);
 
     /*
diff --git a/kernel/drivers/net/stack/ipv4/udp/udp.c 
b/kernel/drivers/net/stack/ipv4/udp/udp.c
index fcaf90d..3966e54 100644
--- a/kernel/drivers/net/stack/ipv4/udp/udp.c
+++ b/kernel/drivers/net/stack/ipv4/udp/udp.c
@@ -88,40 +88,40 @@ module_param(auto_port_start, uint, 0444);
 module_param(auto_port_mask, uint, 0444);
 MODULE_PARM_DESC(auto_port_start, "Start of automatically assigned port 
range");
 MODULE_PARM_DESC(auto_port_mask,
-                "Mask that defines port range for automatic assignment");
+                 "Mask that defines port range for automatic assignment");
 
 static inline struct udp_socket *port_hash_search(u32 saddr, u16 sport)
 {
-       unsigned bucket = sport & port_hash_mask;
-       struct udp_socket *sock;
+        unsigned bucket = sport & port_hash_mask;
+        struct udp_socket *sock;
 
-       hlist_for_each_entry(sock, &port_hash[bucket], link)
-               if (sock->sport == sport &&
-                   (saddr == INADDR_ANY
-                    || sock->saddr == saddr
-                    || sock->saddr == INADDR_ANY))
-                       return sock;
+        hlist_for_each_entry(sock, &port_hash[bucket], link)
+                if (sock->sport == sport &&
+                    (saddr == INADDR_ANY
+                     || sock->saddr == saddr
+                     || sock->saddr == INADDR_ANY))
+                        return sock;
 
-       return NULL;
+        return NULL;
 }
 
 static inline int port_hash_insert(struct udp_socket *sock, u32 saddr, u16 
sport)
 {
-       unsigned bucket;
+        unsigned bucket;
 
-       if (port_hash_search(saddr, sport))
-               return -EADDRINUSE;
+        if (port_hash_search(saddr, sport))
+                return -EADDRINUSE;
 
-       bucket = sport & port_hash_mask;
-       sock->saddr = saddr;
-       sock->sport = sport;
-       hlist_add_head(&sock->link, &port_hash[bucket]);
-       return 0;
+        bucket = sport & port_hash_mask;
+        sock->saddr = saddr;
+        sock->sport = sport;
+        hlist_add_head(&sock->link, &port_hash[bucket]);
+        return 0;
 }
 
 static inline void port_hash_del(struct udp_socket *sock)
 {
-       hlist_del(&sock->link);
+        hlist_del(&sock->link);
 }
 
 /***
@@ -136,9 +136,9 @@ static inline struct rtsocket *rt_udp_v4_lookup(u32 daddr, 
u16 dport)
     sock = port_hash_search(daddr, dport);
     if (sock && rt_socket_reference(sock->sock) == 0) {
 
-           rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
+            rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
 
-           return sock->sock;
+            return sock->sock;
     }
 
     rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
@@ -154,7 +154,7 @@ static inline struct rtsocket *rt_udp_v4_lookup(u32 daddr, 
u16 dport)
  *  @addr:  local address
  */
 int rt_udp_bind(struct rtsocket *sock, const struct sockaddr *addr,
-               socklen_t addrlen)
+                socklen_t addrlen)
 {
     struct sockaddr_in  *usin = (struct sockaddr_in *)addr;
     rtdm_lockctx_t      context;
@@ -163,30 +163,30 @@ int rt_udp_bind(struct rtsocket *sock, const struct 
sockaddr *addr,
 
 
     if ((addrlen < (int)sizeof(struct sockaddr_in)) ||
-       ((usin->sin_port & auto_port_mask) == auto_port_start))
-       return -EINVAL;
+        ((usin->sin_port & auto_port_mask) == auto_port_start))
+        return -EINVAL;
 
     rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
 
     if ((index = sock->prot.inet.reg_index) < 0) {
-       /* socket is being closed */
-       err = -EBADF;
-       goto unlock_out;
+        /* socket is being closed */
+        err = -EBADF;
+        goto unlock_out;
     }
     if (sock->prot.inet.state != TCP_CLOSE) {
-       err = -EINVAL;
-       goto unlock_out;
+        err = -EINVAL;
+        goto unlock_out;
     }
 
     port_hash_del(&port_registry[index]);
     if (port_hash_insert(&port_registry[index],
-                        usin->sin_addr.s_addr,
-                        usin->sin_port ?: index + auto_port_start)) {
-           port_hash_insert(&port_registry[index],
-                            port_registry[index].saddr,
-                            port_registry[index].sport);
-           rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
-           return -EADDRINUSE;
+                         usin->sin_addr.s_addr,
+                         usin->sin_port ?: index + auto_port_start)) {
+            port_hash_insert(&port_registry[index],
+                             port_registry[index].saddr,
+                             port_registry[index].sport);
+            rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
+            return -EADDRINUSE;
     }
 
     /* set the source-addr */
@@ -207,7 +207,7 @@ int rt_udp_bind(struct rtsocket *sock, const struct 
sockaddr *addr,
  *  rt_udp_connect
  */
 int rt_udp_connect(struct rtsocket *sock, const struct sockaddr *serv_addr,
-                  socklen_t addrlen)
+                   socklen_t addrlen)
 {
     struct sockaddr_in  *usin = (struct sockaddr_in *) serv_addr;
     rtdm_lockctx_t      context;
@@ -215,39 +215,39 @@ int rt_udp_connect(struct rtsocket *sock, const struct 
sockaddr *serv_addr,
 
 
     if (usin->sin_family == AF_UNSPEC) {
-       if ((index = sock->prot.inet.reg_index) < 0)
-           /* socket is being closed */
-           return -EBADF;
-
-       rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
-
-       sock->prot.inet.saddr = INADDR_ANY;
-       /* Note: The following line differs from standard stacks, and we also
-                don't remove the socket from the port list. Might get fixed in
-                the future... */
-       sock->prot.inet.sport = index + auto_port_start;
-       sock->prot.inet.daddr = INADDR_ANY;
-       sock->prot.inet.dport = 0;
-       sock->prot.inet.state = TCP_CLOSE;
-
-       rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
+        if ((index = sock->prot.inet.reg_index) < 0)
+            /* socket is being closed */
+            return -EBADF;
+
+        rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
+
+        sock->prot.inet.saddr = INADDR_ANY;
+        /* Note: The following line differs from standard stacks, and we also
+                 don't remove the socket from the port list. Might get fixed in
+                 the future... */
+        sock->prot.inet.sport = index + auto_port_start;
+        sock->prot.inet.daddr = INADDR_ANY;
+        sock->prot.inet.dport = 0;
+        sock->prot.inet.state = TCP_CLOSE;
+
+        rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
     } else {
-       if ((addrlen < (int)sizeof(struct sockaddr_in)) ||
-           (usin->sin_family != AF_INET))
-           return -EINVAL;
+        if ((addrlen < (int)sizeof(struct sockaddr_in)) ||
+            (usin->sin_family != AF_INET))
+            return -EINVAL;
 
-       rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
+        rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
 
-       if (sock->prot.inet.state != TCP_CLOSE) {
-           rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
-           return -EINVAL;
-       }
+        if (sock->prot.inet.state != TCP_CLOSE) {
+            rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
+            return -EINVAL;
+        }
 
-       sock->prot.inet.state = TCP_ESTABLISHED;
-       sock->prot.inet.daddr = usin->sin_addr.s_addr;
-       sock->prot.inet.dport = usin->sin_port;
+        sock->prot.inet.state = TCP_ESTABLISHED;
+        sock->prot.inet.daddr = usin->sin_addr.s_addr;
+        sock->prot.inet.dport = usin->sin_port;
 
-       rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
+        rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
     }
 
     return 0;
@@ -269,7 +269,7 @@ int rt_udp_socket(struct rtdm_fd *fd)
 
 
     if ((ret = rt_socket_init(fd, IPPROTO_UDP)) != 0)
-       return ret;
+        return ret;
 
     sock->prot.inet.saddr = INADDR_ANY;
     sock->prot.inet.state = TCP_CLOSE;
@@ -279,16 +279,16 @@ int rt_udp_socket(struct rtdm_fd *fd)
 
     /* enforce maximum number of UDP sockets */
     if (free_ports == 0) {
-       rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
-       rt_socket_cleanup(fd);
-       return -EAGAIN;
+        rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
+        rt_socket_cleanup(fd);
+        return -EAGAIN;
     }
     free_ports--;
 
     /* find free auto-port in bitmap */
     for (i = 0; i < RT_PORT_BITMAP_WORDS; i++)
-       if (port_bitmap[i] != (unsigned long)-1)
-           break;
+        if (port_bitmap[i] != (unsigned long)-1)
+            break;
     index = ffz(port_bitmap[i]);
     set_bit(index, &port_bitmap[i]);
     index += i*32;
@@ -309,7 +309,7 @@ int rt_udp_socket(struct rtdm_fd *fd)
 /***
  *  rt_udp_close
  */
-int rt_udp_close(struct rtdm_fd *fd)
+void rt_udp_close(struct rtdm_fd *fd)
 {
     struct rtsocket *sock = rtdm_fd_to_private(fd);
     struct rtskb    *del;
@@ -322,13 +322,13 @@ int rt_udp_close(struct rtdm_fd *fd)
     sock->prot.inet.state = TCP_CLOSE;
 
     if (sock->prot.inet.reg_index >= 0) {
-       port = sock->prot.inet.reg_index;
-       clear_bit(port % BITS_PER_LONG, &port_bitmap[port / BITS_PER_LONG]);
-       port_hash_del(&port_registry[port]);
+        port = sock->prot.inet.reg_index;
+        clear_bit(port % BITS_PER_LONG, &port_bitmap[port / BITS_PER_LONG]);
+        port_hash_del(&port_registry[port]);
 
-       free_ports++;
+        free_ports++;
 
-       sock->prot.inet.reg_index = -1;
+        sock->prot.inet.reg_index = -1;
     }
 
     rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
@@ -338,9 +338,9 @@ int rt_udp_close(struct rtdm_fd *fd)
 
     /* free packets in incoming queue */
     while ((del = rtskb_dequeue(&sock->incoming)) != NULL)
-       kfree_rtskb(del);
+        kfree_rtskb(del);
 
-    return rt_socket_cleanup(fd);
+    rt_socket_cleanup(fd);
 }
 
 
@@ -353,17 +353,17 @@ int rt_udp_ioctl(struct rtdm_fd *fd, unsigned int 
request, void *arg)
 
     /* fast path for common socket IOCTLs */
     if (_IOC_TYPE(request) == RTIOC_TYPE_NETWORK)
-       return rt_socket_common_ioctl(fd, request, arg);
+        return rt_socket_common_ioctl(fd, request, arg);
 
     switch (request) {
-       case _RTIOC_BIND:
-           return rt_udp_bind(sock, setaddr->addr, setaddr->addrlen);
+        case _RTIOC_BIND:
+            return rt_udp_bind(sock, setaddr->addr, setaddr->addrlen);
 
-       case _RTIOC_CONNECT:
-           return rt_udp_connect(sock, setaddr->addr, setaddr->addrlen);
+        case _RTIOC_CONNECT:
+            return rt_udp_connect(sock, setaddr->addr, setaddr->addrlen);
 
-       default:
-           return rt_ip_ioctl(fd, request, arg);
+        default:
+            return rt_ip_ioctl(fd, request, arg);
     }
 }
 
@@ -389,19 +389,19 @@ ssize_t rt_udp_recvmsg(struct rtdm_fd *fd, struct msghdr 
*msg, int msg_flags)
 
     /* non-blocking receive? */
     if (msg_flags & MSG_DONTWAIT)
-       timeout = -1;
+        timeout = -1;
 
     ret = rtdm_sem_timeddown(&sock->pending_sem, timeout, NULL);
     if (unlikely(ret < 0))
-       switch (ret) {
-           case -EWOULDBLOCK:
-           case -ETIMEDOUT:
-           case -EINTR:
-               return ret;
+        switch (ret) {
+            case -EWOULDBLOCK:
+            case -ETIMEDOUT:
+            case -EINTR:
+                return ret;
 
-           default:
-               return -EBADF;   /* socket has been closed */
-       }
+            default:
+                return -EBADF;   /* socket has been closed */
+        }
 
     skb = rtskb_dequeue_chain(&sock->incoming);
     RTNET_ASSERT(skb != NULL, return -EFAULT;);
@@ -413,9 +413,9 @@ ssize_t rt_udp_recvmsg(struct rtdm_fd *fd, struct msghdr 
*msg, int msg_flags)
     /* copy the address */
     msg->msg_namelen = sizeof(*sin);
     if (sin) {
-       sin->sin_family      = AF_INET;
-       sin->sin_port        = uh->source;
-       sin->sin_addr.s_addr = skb->nh.iph->saddr;
+        sin->sin_family      = AF_INET;
+        sin->sin_port        = uh->source;
+        sin->sin_addr.s_addr = skb->nh.iph->saddr;
     }
 
     /* remove the UDP header */
@@ -425,41 +425,41 @@ ssize_t rt_udp_recvmsg(struct rtdm_fd *fd, struct msghdr 
*msg, int msg_flags)
 
     /* iterate over all IP fragments */
     do {
-       rtskb_trim(skb, data_len);
+        rtskb_trim(skb, data_len);
 
-       block_size = skb->len;
-       copied += block_size;
-       data_len -= block_size;
+        block_size = skb->len;
+        copied += block_size;
+        data_len -= block_size;
 
-       /* The data must not be longer than the available buffer size */
-       if (copied > len) {
-           block_size -= copied - len;
-           copied = len;
-           msg->msg_flags |= MSG_TRUNC;
+        /* The data must not be longer than the available buffer size */
+        if (copied > len) {
+            block_size -= copied - len;
+            copied = len;
+            msg->msg_flags |= MSG_TRUNC;
 
-           /* copy the data */
-           rt_memcpy_tokerneliovec(msg->msg_iov, skb->data, block_size);
+            /* copy the data */
+            rt_memcpy_tokerneliovec(msg->msg_iov, skb->data, block_size);
 
-           break;
-       }
+            break;
+        }
 
-       /* copy the data */
-       rt_memcpy_tokerneliovec(msg->msg_iov, skb->data, block_size);
+        /* copy the data */
+        rt_memcpy_tokerneliovec(msg->msg_iov, skb->data, block_size);
 
-       /* next fragment */
-       skb = skb->next;
+        /* next fragment */
+        skb = skb->next;
     } while (skb != NULL);
 
     /* did we copied all bytes? */
     if (data_len > 0)
-       msg->msg_flags |= MSG_TRUNC;
+        msg->msg_flags |= MSG_TRUNC;
 
     if ((msg_flags & MSG_PEEK) == 0)
-       kfree_rtskb(first_skb);
+        kfree_rtskb(first_skb);
     else {
-       __rtskb_push(first_skb, sizeof(struct udphdr));
-       rtskb_queue_head(&sock->incoming, first_skb);
-       rtdm_sem_up(&sock->pending_sem);
+        __rtskb_push(first_skb, sizeof(struct udphdr));
+        rtskb_queue_head(&sock->incoming, first_skb);
+        rtdm_sem_up(&sock->pending_sem);
     }
 
     return copied;
@@ -486,7 +486,7 @@ struct udpfakehdr
  *
  */
 static int rt_udp_getfrag(const void *p, unsigned char *to,
-                         unsigned int offset, unsigned int fraglen)
+                          unsigned int offset, unsigned int fraglen)
 {
     struct udpfakehdr *ufh = (struct udpfakehdr *)p;
     int i;
@@ -494,27 +494,27 @@ static int rt_udp_getfrag(const void *p, unsigned char 
*to,
 
     // We should optimize this function a bit (copy+csum...)!
     if (offset==0) {
-       /* Checksum of the complete data part of the UDP message: */
-       for (i = 0; i < ufh->iovlen; i++) {
-           ufh->wcheck = csum_partial(ufh->iov[i].iov_base, 
ufh->iov[i].iov_len,
-                                      ufh->wcheck);
-       }
+        /* Checksum of the complete data part of the UDP message: */
+        for (i = 0; i < ufh->iovlen; i++) {
+            ufh->wcheck = csum_partial(ufh->iov[i].iov_base, 
ufh->iov[i].iov_len,
+                                       ufh->wcheck);
+        }
 
-       rt_memcpy_fromkerneliovec(to + sizeof(struct udphdr), ufh->iov,
-                                 fraglen - sizeof(struct udphdr));
+        rt_memcpy_fromkerneliovec(to + sizeof(struct udphdr), ufh->iov,
+                                  fraglen - sizeof(struct udphdr));
 
-       /* Checksum of the udp header: */
-       ufh->wcheck = csum_partial((unsigned char *)ufh,
-                                  sizeof(struct udphdr), ufh->wcheck);
+        /* Checksum of the udp header: */
+        ufh->wcheck = csum_partial((unsigned char *)ufh,
+                                   sizeof(struct udphdr), ufh->wcheck);
 
-       ufh->uh.check = csum_tcpudp_magic(ufh->saddr, ufh->daddr, 
ntohs(ufh->uh.len),
-                                         IPPROTO_UDP, ufh->wcheck);
+        ufh->uh.check = csum_tcpudp_magic(ufh->saddr, ufh->daddr, 
ntohs(ufh->uh.len),
+                                          IPPROTO_UDP, ufh->wcheck);
 
-       if (ufh->uh.check == 0)
-           ufh->uh.check = -1;
+        if (ufh->uh.check == 0)
+            ufh->uh.check = -1;
 
-       memcpy(to, ufh, sizeof(struct udphdr));
-       return 0;
+        memcpy(to, ufh, sizeof(struct udphdr));
+        return 0;
     }
 
     rt_memcpy_fromkerneliovec(to, ufh->iov, fraglen);
@@ -543,32 +543,32 @@ ssize_t rt_udp_sendmsg(struct rtdm_fd *fd, const struct 
msghdr *msg, int msg_fla
 
 
     if ((len < 0) || (len > 0xFFFF-sizeof(struct iphdr)-sizeof(struct udphdr)))
-       return -EMSGSIZE;
+        return -EMSGSIZE;
 
     if (msg_flags & MSG_OOB)   /* Mirror BSD error message compatibility */
-       return -EOPNOTSUPP;
+        return -EOPNOTSUPP;
 
     if (msg_flags & ~(MSG_DONTROUTE|MSG_DONTWAIT) )
-       return -EINVAL;
+        return -EINVAL;
 
     if ((msg->msg_name) && (msg->msg_namelen==sizeof(struct sockaddr_in))) {
-       usin = (struct sockaddr_in*) msg->msg_name;
+        usin = (struct sockaddr_in*) msg->msg_name;
 
-       if ((usin->sin_family != AF_INET) && (usin->sin_family != AF_UNSPEC))
-           return -EINVAL;
+        if ((usin->sin_family != AF_INET) && (usin->sin_family != AF_UNSPEC))
+            return -EINVAL;
 
-       daddr = usin->sin_addr.s_addr;
-       dport = usin->sin_port;
+        daddr = usin->sin_addr.s_addr;
+        dport = usin->sin_port;
 
-       rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
+        rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
     } else {
-       rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
+        rtdm_lock_get_irqsave(&udp_socket_base_lock, context);
 
-       if (sock->prot.inet.state != TCP_ESTABLISHED)
-           return -ENOTCONN;
+        if (sock->prot.inet.state != TCP_ESTABLISHED)
+            return -ENOTCONN;
 
-       daddr = sock->prot.inet.daddr;
-       dport = sock->prot.inet.dport;
+        daddr = sock->prot.inet.daddr;
+        dport = sock->prot.inet.dport;
     }
     saddr         = sock->prot.inet.saddr;
     ufh.uh.source = sock->prot.inet.sport;
@@ -576,12 +576,12 @@ ssize_t rt_udp_sendmsg(struct rtdm_fd *fd, const struct 
msghdr *msg, int msg_fla
     rtdm_lock_put_irqrestore(&udp_socket_base_lock, context);
 
     if ((daddr | dport) == 0)
-       return -EINVAL;
+        return -EINVAL;
 
     /* get output route */
     err = rt_ip_route_output(&rt, daddr, saddr);
     if (err)
-       return err;
+        return err;
 
     /* we found a route, remember the routing dest-addr could be the netmask */
     ufh.saddr     = saddr != INADDR_ANY ? saddr : rt.rtdev->local_ip;
@@ -598,9 +598,9 @@ ssize_t rt_udp_sendmsg(struct rtdm_fd *fd, const struct 
msghdr *msg, int msg_fla
     rtdev_dereference(rt.rtdev);
 
     if (!err)
-       return len;
+        return len;
     else
-       return err;
+        return err;
 }
 
 
@@ -609,9 +609,9 @@ ssize_t rt_udp_sendmsg(struct rtdm_fd *fd, const struct 
msghdr *msg, int msg_fla
  *  rt_udp_check
  */
 static inline unsigned short rt_udp_check(struct udphdr *uh, int len,
-                                         unsigned long saddr,
-                                         unsigned long daddr,
-                                         unsigned long base)
+                                          unsigned long saddr,
+                                          unsigned long daddr,
+                                          unsigned long base)
 {
     return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
 }
@@ -628,24 +628,24 @@ struct rtsocket *rt_udp_dest_socket(struct rtskb *skb)
 
 
     if (uh->check == 0)
-       skb->ip_summed = CHECKSUM_UNNECESSARY;
+        skb->ip_summed = CHECKSUM_UNNECESSARY;
 /* ip_summed (yet) never equals CHECKSUM_PARTIAL
     else
-       if (skb->ip_summed == CHECKSUM_PARTIAL) {
-           skb->ip_summed = CHECKSUM_UNNECESSARY;
+        if (skb->ip_summed == CHECKSUM_PARTIAL) {
+            skb->ip_summed = CHECKSUM_UNNECESSARY;
 
-           if ( !rt_udp_check(uh, ulen, saddr, daddr, skb->csum) )
-               return NULL;
+            if ( !rt_udp_check(uh, ulen, saddr, daddr, skb->csum) )
+                return NULL;
 
-           skb->ip_summed = CHECKSUM_NONE;
-       }*/
+            skb->ip_summed = CHECKSUM_NONE;
+        }*/
 
     if (skb->ip_summed != CHECKSUM_UNNECESSARY)
-       skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
+        skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
 
     /* patch broadcast daddr */
     if (daddr == rtdev->broadcast_ip)
-       daddr = rtdev->local_ip;
+        daddr = rtdev->local_ip;
 
     /* find the destination socket */
     skb->sk = rt_udp_v4_lookup(daddr, uh->dest);
@@ -675,7 +675,7 @@ void rt_udp_rcv (struct rtskb *skb)
     rtdm_lock_put_irqrestore(&sock->param_lock, context);
 
     if (callback_func)
-       callback_func(rt_socket_fd(sock), callback_arg);
+        callback_func(rt_socket_fd(sock), callback_arg);
 }
 
 
@@ -703,9 +703,9 @@ static struct rtinet_protocol udp_protocol = {
 
 static struct rtdm_driver udp_driver = {
     .profile_info =     RTDM_PROFILE_INFO(udp,
-                                       RTDM_CLASS_NETWORK,
-                                       RTDM_SUBCLASS_RTNET,
-                                       RTNET_RTDM_VER),
+                                        RTDM_CLASS_NETWORK,
+                                        RTDM_SUBCLASS_RTNET,
+                                        RTNET_RTDM_VER),
     .device_flags =     RTDM_PROTOCOL_DEVICE,
     .context_size =     sizeof(struct rtsocket),
 
@@ -714,13 +714,13 @@ static struct rtdm_driver udp_driver = {
 
     /* default is UDP */
     .ops = {
-       .socket =       rt_inet_socket,
-       .close =        rt_udp_close,
-       .ioctl_rt =     rt_udp_ioctl,
-       .ioctl_nrt =    rt_udp_ioctl,
-       .recvmsg_rt =   rt_udp_recvmsg,
-       .sendmsg_rt =   rt_udp_sendmsg,
-       .select =       rt_socket_select_bind,
+        .socket =       rt_inet_socket,
+        .close =        rt_udp_close,
+        .ioctl_rt =     rt_udp_ioctl,
+        .ioctl_nrt =    rt_udp_ioctl,
+        .recvmsg_rt =   rt_udp_recvmsg,
+        .sendmsg_rt =   rt_udp_sendmsg,
+        .select =       rt_socket_select_bind,
     },
 };
 
@@ -736,14 +736,14 @@ static int __init rt_udp_init(void)
 {
     int i;
     if ((auto_port_start < 0) || (auto_port_start >= 0x10000 - RT_UDP_SOCKETS))
-       auto_port_start = 1024;
+        auto_port_start = 1024;
     auto_port_start = htons(auto_port_start & (auto_port_mask & 0xFFFF));
     auto_port_mask  = htons(auto_port_mask | 0xFFFF0000);
 
     rt_inet_add_protocol(&udp_protocol);
 
     for (i = 0; i < ARRAY_SIZE(port_hash); i++)
-           INIT_HLIST_HEAD(&port_hash[i]);
+            INIT_HLIST_HEAD(&port_hash[i]);
 
     return rtdm_dev_register(&udp_device);
 }
diff --git a/kernel/drivers/net/stack/packet/af_packet.c 
b/kernel/drivers/net/stack/packet/af_packet.c
index c848d0c..6cb4543 100644
--- a/kernel/drivers/net/stack/packet/af_packet.c
+++ b/kernel/drivers/net/stack/packet/af_packet.c
@@ -38,7 +38,7 @@ MODULE_LICENSE("GPL");
 static int rt_packet_rcv(struct rtskb *skb, struct rtpacket_type *pt)
 {
     struct rtsocket *sock   = container_of(pt, struct rtsocket,
-                                          prot.packet.packet_type);
+                                           prot.packet.packet_type);
     int             ifindex = sock->prot.packet.ifindex;
     void            (*callback_func)(struct rtdm_fd *, void *);
     void            *callback_arg;
@@ -46,23 +46,23 @@ static int rt_packet_rcv(struct rtskb *skb, struct 
rtpacket_type *pt)
 
 
     if (unlikely((ifindex != 0) && (ifindex != skb->rtdev->ifindex)))
-       return -EUNATCH;
+        return -EUNATCH;
 
     if (rt_socket_reference(sock) < 0)
-       return -EUNATCH;
+        return -EUNATCH;
 
 #ifdef CONFIG_XENO_DRIVERS_NET_ETH_P_ALL
     if (pt->type == htons(ETH_P_ALL)) {
-       struct rtskb *clone_skb = rtskb_clone(skb, &sock->skb_pool);
-       if (clone_skb == NULL)
-           goto out;
-       skb = clone_skb;
+        struct rtskb *clone_skb = rtskb_clone(skb, &sock->skb_pool);
+        if (clone_skb == NULL)
+            goto out;
+        skb = clone_skb;
     } else
 #endif /* CONFIG_XENO_DRIVERS_NET_ETH_P_ALL */
-       if (unlikely(rtskb_acquire(skb, &sock->skb_pool) < 0)) {
-           kfree_rtskb(skb);
-           goto out;
-       }
+        if (unlikely(rtskb_acquire(skb, &sock->skb_pool) < 0)) {
+            kfree_rtskb(skb);
+            goto out;
+        }
 
     rtdev_reference(skb->rtdev);
     rtskb_queue_tail(&sock->incoming, skb);
@@ -74,7 +74,7 @@ static int rt_packet_rcv(struct rtskb *skb, struct 
rtpacket_type *pt)
     rtdm_lock_put_irqrestore(&sock->param_lock, context);
 
     if (callback_func)
-       callback_func(rt_socket_fd(sock), callback_arg);
+        callback_func(rt_socket_fd(sock), callback_arg);
 
   out:
     rt_socket_dereference(sock);
@@ -84,11 +84,11 @@ static int rt_packet_rcv(struct rtskb *skb, struct 
rtpacket_type *pt)
 static bool rt_packet_trylock(struct rtpacket_type *pt)
 {
     struct rtsocket *sock   = container_of(pt, struct rtsocket,
-                                          prot.packet.packet_type);
+                                           prot.packet.packet_type);
     struct rtdm_fd *fd = rtdm_private_to_fd(sock);
 
     if (rtdm_fd_lock(fd) < 0)
-       return false;
+        return false;
 
     return rtdev_lock_pack(pt);
 }
@@ -96,7 +96,7 @@ static bool rt_packet_trylock(struct rtpacket_type *pt)
 static void rt_packet_unlock(struct rtpacket_type *pt)
 {
     struct rtsocket *sock   = container_of(pt, struct rtsocket,
-                                          prot.packet.packet_type);
+                                           prot.packet.packet_type);
     struct rtdm_fd *fd = rtdm_private_to_fd(sock);
     rtdev_unlock_pack(pt);
     rtdm_fd_unlock(fd);
@@ -106,7 +106,7 @@ static void rt_packet_unlock(struct rtpacket_type *pt)
  *  rt_packet_bind
  */
 static int rt_packet_bind(struct rtsocket *sock, const struct sockaddr *addr,
-                         socklen_t addrlen)
+                          socklen_t addrlen)
 {
     struct sockaddr_ll      *sll = (struct sockaddr_ll *)addr;
     struct rtpacket_type    *pt  = &sock->prot.packet.packet_type;
@@ -116,8 +116,8 @@ static int rt_packet_bind(struct rtsocket *sock, const 
struct sockaddr *addr,
 
 
     if ((addrlen < (int)sizeof(struct sockaddr_ll)) ||
-       (sll->sll_family != AF_PACKET))
-       return -EINVAL;
+        (sll->sll_family != AF_PACKET))
+        return -EINVAL;
 
     new_type = (sll->sll_protocol != 0) ? sll->sll_protocol : sock->protocol;
 
@@ -125,8 +125,8 @@ static int rt_packet_bind(struct rtsocket *sock, const 
struct sockaddr *addr,
 
     /* release existing binding */
     if ((pt->type != 0) && ((ret = rtdev_remove_pack(pt)) < 0)) {
-       rtdm_lock_put_irqrestore(&sock->param_lock, context);
-       return ret;
+        rtdm_lock_put_irqrestore(&sock->param_lock, context);
+        return ret;
     }
 
     pt->type = new_type;
@@ -134,14 +134,14 @@ static int rt_packet_bind(struct rtsocket *sock, const 
struct sockaddr *addr,
 
     /* if protocol is non-zero, register the packet type */
     if (new_type != 0) {
-       pt->handler     = rt_packet_rcv;
-       pt->err_handler = NULL;
-       pt->trylock     = rt_packet_trylock;
-       pt->unlock      = rt_packet_unlock;
+        pt->handler     = rt_packet_rcv;
+        pt->err_handler = NULL;
+        pt->trylock     = rt_packet_trylock;
+        pt->unlock      = rt_packet_unlock;
 
-       ret = rtdev_add_pack(pt);
+        ret = rtdev_add_pack(pt);
     } else
-       ret = 0;
+        ret = 0;
 
     rtdm_lock_put_irqrestore(&sock->param_lock, context);
 
@@ -154,7 +154,7 @@ static int rt_packet_bind(struct rtsocket *sock, const 
struct sockaddr *addr,
  *  rt_packet_getsockname
  */
 static int rt_packet_getsockname(struct rtsocket *sock, struct sockaddr *addr,
-                                socklen_t *addrlen)
+                                 socklen_t *addrlen)
 {
     struct sockaddr_ll  *sll = (struct sockaddr_ll*)addr;
     struct rtnet_device *rtdev;
@@ -162,7 +162,7 @@ static int rt_packet_getsockname(struct rtsocket *sock, 
struct sockaddr *addr,
 
 
     if (*addrlen < sizeof(struct sockaddr_ll))
-       return -EINVAL;
+        return -EINVAL;
 
     rtdm_lock_get_irqsave(&sock->param_lock, context);
 
@@ -174,15 +174,15 @@ static int rt_packet_getsockname(struct rtsocket *sock, 
struct sockaddr *addr,
 
     rtdev = rtdev_get_by_index(sll->sll_ifindex);
     if (rtdev != NULL) {
-       sll->sll_hatype = rtdev->type;
-       sll->sll_halen  = rtdev->addr_len;
+        sll->sll_hatype = rtdev->type;
+        sll->sll_halen  = rtdev->addr_len;
 
-       memcpy(sll->sll_addr, rtdev->dev_addr, rtdev->addr_len);
+        memcpy(sll->sll_addr, rtdev->dev_addr, rtdev->addr_len);
 
-       rtdev_dereference(rtdev);
+        rtdev_dereference(rtdev);
     } else {
-       sll->sll_hatype = 0;
-       sll->sll_halen  = 0;
+        sll->sll_hatype = 0;
+        sll->sll_halen  = 0;
     }
 
     *addrlen = sizeof(struct sockaddr_ll);
@@ -202,22 +202,22 @@ static int rt_packet_socket(struct rtdm_fd *fd, int 
protocol)
 
 
     if ((ret = rt_socket_init(fd, protocol)) != 0)
-       return ret;
+        return ret;
 
-    sock->prot.packet.packet_type.type  = protocol;
-    sock->prot.packet.ifindex           = 0;
-    sock->prot.packet.trylock           = rt_packet_trylock;
-    sock->prot.packet.unlock            = rt_packet_unlock;
+    sock->prot.packet.packet_type.type         = protocol;
+    sock->prot.packet.ifindex                  = 0;
+    sock->prot.packet.packet_type.trylock      = rt_packet_trylock;
+    sock->prot.packet.packet_type.unlock        = rt_packet_unlock;
 
     /* if protocol is non-zero, register the packet type */
     if (protocol != 0) {
-       sock->prot.packet.packet_type.handler     = rt_packet_rcv;
-       sock->prot.packet.packet_type.err_handler = NULL;
+        sock->prot.packet.packet_type.handler     = rt_packet_rcv;
+        sock->prot.packet.packet_type.err_handler = NULL;
 
-       if ((ret = rtdev_add_pack(&sock->prot.packet.packet_type)) < 0) {
-           rt_socket_cleanup(fd);
-           return ret;
-       }
+        if ((ret = rtdev_add_pack(&sock->prot.packet.packet_type)) < 0) {
+            rt_socket_cleanup(fd);
+            return ret;
+        }
     }
 
     return 0;
@@ -228,31 +228,30 @@ static int rt_packet_socket(struct rtdm_fd *fd, int 
protocol)
 /***
  *  rt_packet_close
  */
-static int rt_packet_close(struct rtdm_fd *fd)
+static void rt_packet_close(struct rtdm_fd *fd)
 {
     struct rtsocket         *sock = rtdm_fd_to_private(fd);
     struct rtpacket_type    *pt = &sock->prot.packet.packet_type;
     struct rtskb            *del;
-    int                     ret = 0;
     rtdm_lockctx_t          context;
 
 
     rtdm_lock_get_irqsave(&sock->param_lock, context);
 
     if (pt->type != 0) {
-       rtdev_remove_pack(pt);
-       pt->type = 0;
+        rtdev_remove_pack(pt);
+        pt->type = 0;
     }
 
     rtdm_lock_put_irqrestore(&sock->param_lock, context);
 
     /* free packets in incoming queue */
     while ((del = rtskb_dequeue(&sock->incoming)) != NULL) {
-       rtdev_dereference(del->rtdev);
-       kfree_rtskb(del);
+        rtdev_dereference(del->rtdev);
+        kfree_rtskb(del);
     }
 
-    return rt_socket_cleanup(fd);
+    rt_socket_cleanup(fd);
 }
 
 
@@ -269,18 +268,18 @@ static int rt_packet_ioctl(struct rtdm_fd *fd, unsigned 
int request, void *arg)
 
     /* fast path for common socket IOCTLs */
     if (_IOC_TYPE(request) == RTIOC_TYPE_NETWORK)
-       return rt_socket_common_ioctl(fd, request, arg);
+        return rt_socket_common_ioctl(fd, request, arg);
 
     switch (request) {
-       case _RTIOC_BIND:
-           return rt_packet_bind(sock, setaddr->addr, setaddr->addrlen);
+        case _RTIOC_BIND:
+            return rt_packet_bind(sock, setaddr->addr, setaddr->addrlen);
 
-       case _RTIOC_GETSOCKNAME:
-           return rt_packet_getsockname(sock, getaddr->addr,
-                                        getaddr->addrlen);
+        case _RTIOC_GETSOCKNAME:
+            return rt_packet_getsockname(sock, getaddr->addr,
+                                         getaddr->addrlen);
 
-       default:
-           return rt_socket_if_ioctl(fd, request, arg);
+        default:
+            return rt_socket_if_ioctl(fd, request, arg);
     }
 }
 
@@ -304,19 +303,19 @@ rt_packet_recvmsg(struct rtdm_fd *fd, struct msghdr *msg, 
int msg_flags)
 
     /* non-blocking receive? */
     if (msg_flags & MSG_DONTWAIT)
-       timeout = -1;
+        timeout = -1;
 
     ret = rtdm_sem_timeddown(&sock->pending_sem, timeout, NULL);
     if (unlikely(ret < 0))
-       switch (ret) {
-           case -EWOULDBLOCK:
-           case -ETIMEDOUT:
-           case -EINTR:
-               return ret;
+        switch (ret) {
+            case -EWOULDBLOCK:
+            case -ETIMEDOUT:
+            case -EINTR:
+                return ret;
 
-           default:
-               return -EBADF;   /* socket has been closed */
-       }
+            default:
+                return -EBADF;   /* socket has been closed */
+        }
 
     rtskb = rtskb_dequeue_chain(&sock->incoming);
     RTNET_ASSERT(rtskb != NULL, return -EFAULT;);
@@ -326,39 +325,39 @@ rt_packet_recvmsg(struct rtdm_fd *fd, struct msghdr *msg, 
int msg_flags)
     /* copy the address */
     msg->msg_namelen = sizeof(*sll);
     if (sll != NULL) {
-       struct rtnet_device *rtdev = rtskb->rtdev;
+        struct rtnet_device *rtdev = rtskb->rtdev;
 
-       sll->sll_family   = AF_PACKET;
-       sll->sll_hatype   = rtdev->type;
-       sll->sll_protocol = rtskb->protocol;
-       sll->sll_pkttype  = rtskb->pkt_type;
-       sll->sll_ifindex  = rtdev->ifindex;
+        sll->sll_family   = AF_PACKET;
+        sll->sll_hatype   = rtdev->type;
+        sll->sll_protocol = rtskb->protocol;
+        sll->sll_pkttype  = rtskb->pkt_type;
+        sll->sll_ifindex  = rtdev->ifindex;
 
-       /* Ethernet specific - we rather need some parse handler here */
-       memcpy(sll->sll_addr, rtskb->mac.ethernet->h_source, ETH_ALEN);
-       sll->sll_halen = ETH_ALEN;
+        /* Ethernet specific - we rather need some parse handler here */
+        memcpy(sll->sll_addr, rtskb->mac.ethernet->h_source, ETH_ALEN);
+        sll->sll_halen = ETH_ALEN;
     }
 
     /* Include the header in raw delivery */
     if (rtdm_fd_to_context(fd)->device->driver->socket_type != SOCK_DGRAM)
-       rtskb_push(rtskb, rtskb->data - rtskb->mac.raw);
+        rtskb_push(rtskb, rtskb->data - rtskb->mac.raw);
 
     copy_len = real_len = rtskb->len;
 
     /* The data must not be longer than the available buffer size */
     if (copy_len > len) {
-       copy_len = len;
-       msg->msg_flags |= MSG_TRUNC;
+        copy_len = len;
+        msg->msg_flags |= MSG_TRUNC;
     }
 
     rt_memcpy_tokerneliovec(msg->msg_iov, rtskb->data, copy_len);
 
     if ((msg_flags & MSG_PEEK) == 0) {
-       rtdev_dereference(rtskb->rtdev);
-       kfree_rtskb(rtskb);
+        rtdev_dereference(rtskb->rtdev);
+        kfree_rtskb(rtskb);
     } else {
-       rtskb_queue_head(&sock->incoming, rtskb);
-       rtdm_sem_up(&sock->pending_sem);
+        rtskb_queue_head(&sock->incoming, rtskb);
+        rtdm_sem_up(&sock->pending_sem);
     }
 
     return real_len;
@@ -384,51 +383,51 @@ rt_packet_sendmsg(struct rtdm_fd *fd, const struct msghdr 
*msg, int msg_flags)
 
 
     if (msg_flags & MSG_OOB)    /* Mirror BSD error message compatibility */
-       return -EOPNOTSUPP;
+        return -EOPNOTSUPP;
     if (msg_flags & ~MSG_DONTWAIT)
-       return -EINVAL;
+        return -EINVAL;
 
     if (sll == NULL) {
-       /* Note: We do not care about races with rt_packet_bind here -
-          the user has to do so. */
-       ifindex = sock->prot.packet.ifindex;
-       proto   = sock->prot.packet.packet_type.type;
-       addr    = NULL;
+        /* Note: We do not care about races with rt_packet_bind here -
+           the user has to do so. */
+        ifindex = sock->prot.packet.ifindex;
+        proto   = sock->prot.packet.packet_type.type;
+        addr    = NULL;
     } else {
-       if ((msg->msg_namelen < sizeof(struct sockaddr_ll)) ||
-           (msg->msg_namelen <
-               (sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) ||
-           ((sll->sll_family != AF_PACKET) &&
-           (sll->sll_family != AF_UNSPEC)))
-       return -EINVAL;
-
-       ifindex = sll->sll_ifindex;
-       proto   = sll->sll_protocol;
-       addr    = sll->sll_addr;
+        if ((msg->msg_namelen < sizeof(struct sockaddr_ll)) ||
+            (msg->msg_namelen <
+                (sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) ||
+            ((sll->sll_family != AF_PACKET) &&
+            (sll->sll_family != AF_UNSPEC)))
+        return -EINVAL;
+
+        ifindex = sll->sll_ifindex;
+        proto   = sll->sll_protocol;
+        addr    = sll->sll_addr;
     }
 
     if ((rtdev = rtdev_get_by_index(ifindex)) == NULL)
-       return -ENODEV;
+        return -ENODEV;
 
     rtskb = alloc_rtskb(rtdev->hard_header_len + len, &sock->skb_pool);
     if (rtskb == NULL) {
-       ret = -ENOBUFS;
-       goto out;
+        ret = -ENOBUFS;
+        goto out;
     }
 
     /* If an RTmac discipline is active, this becomes a pure sanity check to
        avoid writing beyond rtskb boundaries. The hard check is then performed
        upon rtdev_xmit() by the discipline's xmit handler. */
     if (len > rtdev->mtu +
-       ((rtdm_fd_to_context(fd)->device->driver->socket_type == SOCK_RAW) ?
-           rtdev->hard_header_len : 0)) {
-       ret = -EMSGSIZE;
-       goto err;
+        ((rtdm_fd_to_context(fd)->device->driver->socket_type == SOCK_RAW) ?
+            rtdev->hard_header_len : 0)) {
+        ret = -EMSGSIZE;
+        goto err;
     }
 
     if ((sll != NULL) && (sll->sll_halen != rtdev->addr_len)) {
-       ret = -EINVAL;
-       goto err;
+        ret = -EINVAL;
+        goto err;
     }
 
     rtskb_reserve(rtskb, rtdev->hard_header_len);
@@ -437,26 +436,26 @@ rt_packet_sendmsg(struct rtdm_fd *fd, const struct msghdr 
*msg, int msg_flags)
     rtskb->priority = sock->priority;
 
     if (rtdev->hard_header) {
-       int hdr_len;
-
-       ret = -EINVAL;
-       hdr_len = rtdev->hard_header(rtskb, rtdev, ntohs(proto),
-                                    addr, NULL, len);
-       if (rtdm_fd_to_context(fd)->device->driver->socket_type != SOCK_DGRAM) {
-           rtskb->tail = rtskb->data;
-           rtskb->len = 0;
-       } else if (hdr_len < 0)
-           goto err;
+        int hdr_len;
+
+        ret = -EINVAL;
+        hdr_len = rtdev->hard_header(rtskb, rtdev, ntohs(proto),
+                                     addr, NULL, len);
+        if (rtdm_fd_to_context(fd)->device->driver->socket_type != SOCK_DGRAM) 
{
+            rtskb->tail = rtskb->data;
+            rtskb->len = 0;
+        } else if (hdr_len < 0)
+            goto err;
     }
 
     rt_memcpy_fromkerneliovec(rtskb_put(rtskb, len), msg->msg_iov, len);
 
     if ((rtdev->flags & IFF_UP) != 0) {
-       if ((ret = rtdev_xmit(rtskb)) == 0)
-           ret = len;
+        if ((ret = rtdev_xmit(rtskb)) == 0)
+            ret = len;
     } else {
-       ret = -ENETDOWN;
-       goto err;
+        ret = -ENETDOWN;
+        goto err;
     }
 
  out:
@@ -472,9 +471,9 @@ rt_packet_sendmsg(struct rtdm_fd *fd, const struct msghdr 
*msg, int msg_flags)
 
 static struct rtdm_driver packet_proto_drv = {
     .profile_info =     RTDM_PROFILE_INFO(packet,
-                                       RTDM_CLASS_NETWORK,
-                                       RTDM_SUBCLASS_RTNET,
-                                       RTNET_RTDM_VER),
+                                        RTDM_CLASS_NETWORK,
+                                        RTDM_SUBCLASS_RTNET,
+                                        RTNET_RTDM_VER),
     .device_flags =     RTDM_PROTOCOL_DEVICE,
     .context_size =     sizeof(struct rtsocket),
 
@@ -483,13 +482,13 @@ static struct rtdm_driver packet_proto_drv = {
 
 
     .ops = {
-       .socket =       rt_packet_socket,
-       .close =        rt_packet_close,
-       .ioctl_rt =     rt_packet_ioctl,
-       .ioctl_nrt =    rt_packet_ioctl,
-       .recvmsg_rt =   rt_packet_recvmsg,
-       .sendmsg_rt =   rt_packet_sendmsg,
-       .select =       rt_socket_select_bind,
+        .socket =       rt_packet_socket,
+        .close =        rt_packet_close,
+        .ioctl_rt =     rt_packet_ioctl,
+        .ioctl_nrt =    rt_packet_ioctl,
+        .recvmsg_rt =   rt_packet_recvmsg,
+        .sendmsg_rt =   rt_packet_sendmsg,
+        .select =       rt_socket_select_bind,
     },
 };
 
@@ -501,9 +500,9 @@ static struct rtdm_device packet_proto_dev = {
 
 static struct rtdm_driver raw_packet_proto_drv = {
     .profile_info =     RTDM_PROFILE_INFO(raw_packet,
-                                       RTDM_CLASS_NETWORK,
-                                       RTDM_SUBCLASS_RTNET,
-                                       RTNET_RTDM_VER),
+                                        RTDM_CLASS_NETWORK,
+                                        RTDM_SUBCLASS_RTNET,
+                                        RTNET_RTDM_VER),
     .device_flags =     RTDM_PROTOCOL_DEVICE,
     .device_count =     1,
     .context_size =     sizeof(struct rtsocket),
@@ -512,13 +511,13 @@ static struct rtdm_driver raw_packet_proto_drv = {
     .socket_type =      SOCK_RAW,
 
     .ops = {
-       .socket =       rt_packet_socket,
-       .close =        rt_packet_close,
-       .ioctl_rt =     rt_packet_ioctl,
-       .ioctl_nrt =    rt_packet_ioctl,
-       .recvmsg_rt =   rt_packet_recvmsg,
-       .sendmsg_rt =   rt_packet_sendmsg,
-       .select =       rt_socket_select_bind,
+        .socket =       rt_packet_socket,
+        .close =        rt_packet_close,
+        .ioctl_rt =     rt_packet_ioctl,
+        .ioctl_nrt =    rt_packet_ioctl,
+        .recvmsg_rt =   rt_packet_recvmsg,
+        .sendmsg_rt =   rt_packet_sendmsg,
+        .select =       rt_socket_select_bind,
     },
 };
 
@@ -533,11 +532,11 @@ static int __init rt_packet_proto_init(void)
 
     err = rtdm_dev_register(&packet_proto_dev);
     if (err)
-       return err;
+        return err;
 
     err = rtdm_dev_register(&raw_packet_proto_dev);
     if (err)
-       rtdm_dev_unregister(&packet_proto_dev);
+        rtdm_dev_unregister(&packet_proto_dev);
 
     return err;
 }
@@ -562,13 +561,13 @@ module_exit(rt_packet_proto_release);
 static int hex2int(unsigned char hex_char)
 {
     if ((hex_char >= '0') && (hex_char <= '9'))
-       return hex_char - '0';
+        return hex_char - '0';
     else if ((hex_char >= 'a') && (hex_char <= 'f'))
-       return hex_char - 'a' + 10;
+        return hex_char - 'a' + 10;
     else if ((hex_char >= 'A') && (hex_char <= 'F'))
-       return hex_char - 'A' + 10;
+        return hex_char - 'A' + 10;
     else
-       return -EINVAL;
+        return -EINVAL;
 }
 
 
@@ -580,25 +579,25 @@ int rt_eth_aton(unsigned char *addr_buf, const char *mac)
 
 
     while (1) {
-       if (*mac == 0)
-           return -EINVAL;
+        if (*mac == 0)
+            return -EINVAL;
 
-       if ((nibble = hex2int(*mac++)) < 0)
-           return nibble;
-       *addr_buf = nibble << 4;
+        if ((nibble = hex2int(*mac++)) < 0)
+            return nibble;
+        *addr_buf = nibble << 4;
 
-       if (*mac == 0)
-           return -EINVAL;
+        if (*mac == 0)
+            return -EINVAL;
 
-       if ((nibble = hex2int(*mac++)) < 0)
-           return nibble;
-       *addr_buf++ |= nibble;
+        if ((nibble = hex2int(*mac++)) < 0)
+            return nibble;
+        *addr_buf++ |= nibble;
 
-       if (++i == 6)
-           break;
+        if (++i == 6)
+            break;
 
-       if ((*mac == 0) || (*mac++ != ':'))
-           return -EINVAL;
+        if ((*mac == 0) || (*mac++ != ':'))
+            return -EINVAL;
 
     }
     return 0;
diff --git a/kernel/drivers/net/stack/rtcfg/rtcfg_frame.c 
b/kernel/drivers/net/stack/rtcfg/rtcfg_frame.c
index 910349a..bcd11c4 100644
--- a/kernel/drivers/net/stack/rtcfg/rtcfg_frame.c
+++ b/kernel/drivers/net/stack/rtcfg/rtcfg_frame.c
@@ -35,7 +35,7 @@ static unsigned int num_rtskbs = 32;
 module_param(num_rtskbs, uint, 0444);
 MODULE_PARM_DESC(num_rtskbs, "Number of realtime socket buffers used by 
RTcfg");
 
-static struct rtskb_queue   rtcfg_pool;
+static struct rtskb_pool    rtcfg_pool;
 static rtdm_task_t          rx_task;
 static rtdm_event_t         rx_event;
 static struct rtskb_queue   rx_queue;
@@ -541,7 +541,7 @@ int __init rtcfg_init_frames(void)
     int ret;
 
 
-    if (rtskb_pool_init(&rtcfg_pool, num_rtskbs) < num_rtskbs)
+    if (rtskb_module_pool_init(&rtcfg_pool, num_rtskbs) < num_rtskbs)
         return -ENOMEM;
 
     rtskb_queue_init(&rx_queue);
diff --git a/kernel/drivers/net/stack/rtmac/nomac/nomac_module.c 
b/kernel/drivers/net/stack/rtmac/nomac/nomac_module.c
index b874bbe..5e8f3ba 100644
--- a/kernel/drivers/net/stack/rtmac/nomac/nomac_module.c
+++ b/kernel/drivers/net/stack/rtmac/nomac/nomac_module.c
@@ -96,12 +96,9 @@ int nomac_attach(struct rtnet_device *rtdev, void *priv)
 int nomac_detach(struct rtnet_device *rtdev, void *priv)
 {
     struct nomac_priv   *nomac = (struct nomac_priv *)priv;
-    int                 ret;
 
 
-    ret =  nomac_dev_release(nomac);
-    if (ret < 0)
-        return ret;
+    nomac_dev_release(nomac);
 
     /* ... */
     RTNET_MOD_DEC_USE_COUNT;
diff --git a/kernel/drivers/net/stack/rtmac/rtmac_disc.c 
b/kernel/drivers/net/stack/rtmac/rtmac_disc.c
index 8143de0..704ebd4 100644
--- a/kernel/drivers/net/stack/rtmac/rtmac_disc.c
+++ b/kernel/drivers/net/stack/rtmac/rtmac_disc.c
@@ -70,6 +70,9 @@ int rtmac_disc_attach(struct rtnet_device *rtdev, struct 
rtmac_disc *disc)
     if (rtdev->flags & IFF_LOOPBACK)
         return -EINVAL;
 
+    if (!try_module_get(disc->owner))
+        return -EIDRM;
+
     /* alloc memory */
     priv = kmalloc(sizeof(struct rtmac_priv) + disc->priv_size, GFP_KERNEL);
     if (!priv) {
@@ -137,7 +140,9 @@ int rtmac_disc_detach(struct rtnet_device *rtdev)
     priv = rtdev->mac_priv;
     RTNET_ASSERT(priv != NULL, return -EINVAL;);
 
-    rtmac_vnic_unregister(rtdev);
+    ret = rtmac_vnic_unregister(rtdev);
+    if (ret < 0)
+        return ret;
 
     /* call release function of discipline */
     ret = disc->detach(rtdev, priv->disc_priv);
@@ -160,6 +165,8 @@ int rtmac_disc_detach(struct rtnet_device *rtdev)
 
     kfree(priv);
 
+    module_put(disc->owner);
+
     return 0;
 }
 
@@ -186,7 +193,7 @@ static struct rtmac_disc *rtmac_get_disc_by_name(const char 
*name)
 
 
 
-int rtmac_disc_register(struct rtmac_disc *disc)
+int __rtmac_disc_register(struct rtmac_disc *disc, struct module *module)
 {
     int ret;
 
@@ -198,6 +205,8 @@ int rtmac_disc_register(struct rtmac_disc *disc)
     RTNET_ASSERT(disc->attach != NULL, return -EINVAL;);
     RTNET_ASSERT(disc->detach != NULL, return -EINVAL;);
 
+    disc->owner = module;
+
     if (rtmac_get_disc_by_name(disc->name) != NULL)
     {
         printk("RTmac: discipline '%s' already registered!\n", disc->name);
diff --git a/kernel/drivers/net/stack/rtmac/rtmac_syms.c 
b/kernel/drivers/net/stack/rtmac/rtmac_syms.c
index 05835c0..b89412f 100644
--- a/kernel/drivers/net/stack/rtmac/rtmac_syms.c
+++ b/kernel/drivers/net/stack/rtmac/rtmac_syms.c
@@ -26,7 +26,7 @@
 #include <rtmac/rtmac_vnic.h>
 
 
-EXPORT_SYMBOL(rtmac_disc_register);
+EXPORT_SYMBOL(__rtmac_disc_register);
 EXPORT_SYMBOL(rtmac_disc_deregister);
 
 EXPORT_SYMBOL(rtmac_disc_attach);
diff --git a/kernel/drivers/net/stack/rtmac/rtmac_vnic.c 
b/kernel/drivers/net/stack/rtmac/rtmac_vnic.c
index dbd0d99..25d2c4e 100644
--- a/kernel/drivers/net/stack/rtmac/rtmac_vnic.c
+++ b/kernel/drivers/net/stack/rtmac/rtmac_vnic.c
@@ -45,7 +45,7 @@ static struct rtskb_queue   rx_queue;
 int rtmac_vnic_rx(struct rtskb *rtskb, u16 type)
 {
     struct rtmac_priv *mac_priv = rtskb->rtdev->mac_priv;
-    struct rtskb_queue *pool = &mac_priv->vnic_skb_pool;
+    struct rtskb_pool *pool = &mac_priv->vnic_skb_pool;
 
 
     if (rtskb_acquire(rtskb, pool) != 0) {
@@ -142,7 +142,7 @@ int rtmac_vnic_xmit(struct sk_buff *skb, struct net_device 
*dev)
 {
     struct rtnet_device     *rtdev = *(struct rtnet_device **)netdev_priv(dev);
     struct net_device_stats *stats = &rtdev->mac_priv->vnic_stats;
-    struct rtskb_queue      *pool = &rtdev->mac_priv->vnic_skb_pool;
+    struct rtskb_pool       *pool = &rtdev->mac_priv->vnic_skb_pool;
     struct ethhdr           *ethernet = (struct ethhdr*)skb->data;
     struct rtskb            *rtskb;
     int                     res;
@@ -261,7 +261,19 @@ static void rtmac_vnic_setup(struct net_device *dev)
 #endif
 }
 
+static int rtmac_vnic_pool_trylock(void *cookie)
+{
+    return 1;
+}
 
+static void rtmac_vnic_pool_unlock(void *cookie)
+{
+}
+
+const struct rtskb_pool_lock_ops rtmac_vnic_pool_lock_ops = {
+    .trylock = rtmac_vnic_pool_trylock,
+    .unlock = rtmac_vnic_pool_unlock,
+};
 
 int rtmac_vnic_add(struct rtnet_device *rtdev, vnic_xmit_handler vnic_xmit)
 {
@@ -281,7 +293,8 @@ int rtmac_vnic_add(struct rtnet_device *rtdev, 
vnic_xmit_handler vnic_xmit)
 
     /* create the rtskb pool */
     if (rtskb_pool_init(&mac_priv->vnic_skb_pool,
-                        vnic_rtskbs) < vnic_rtskbs) {
+                            vnic_rtskbs, &rtmac_vnic_pool_lock_ops,
+                            NULL) < vnic_rtskbs) {
         res = -ENOMEM;
         goto error;
     }
@@ -318,16 +331,21 @@ int rtmac_vnic_add(struct rtnet_device *rtdev, 
vnic_xmit_handler vnic_xmit)
 
 
 
-void rtmac_vnic_unregister(struct rtnet_device *rtdev)
+int rtmac_vnic_unregister(struct rtnet_device *rtdev)
 {
     struct rtmac_priv   *mac_priv = rtdev->mac_priv;
-
+    int err;
 
     if (mac_priv->vnic) {
+        err = rtskb_pool_release(&mac_priv->vnic_skb_pool);
+        if (err < 0)
+            return err;
         unregister_netdev(mac_priv->vnic);
         free_netdev(mac_priv->vnic);
         mac_priv->vnic = NULL;
     }
+
+    return 0;
 }
 
 
diff --git a/kernel/drivers/net/stack/rtmac/tdma/tdma_ioctl.c 
b/kernel/drivers/net/stack/rtmac/tdma/tdma_ioctl.c
index e9b1172..a1f5e5b 100644
--- a/kernel/drivers/net/stack/rtmac/tdma/tdma_ioctl.c
+++ b/kernel/drivers/net/stack/rtmac/tdma/tdma_ioctl.c
@@ -70,8 +70,8 @@ static int tdma_ioctl_master(struct rtnet_device *rtdev,
         cycle_ms = 1;
     msleep(3*cycle_ms);
 
-    if (rtskb_pool_init(&tdma->cal_rtskb_pool,
-                        cfg->args.master.max_cal_requests) !=
+    if (rtskb_module_pool_init(&tdma->cal_rtskb_pool,
+                                cfg->args.master.max_cal_requests) !=
         cfg->args.master.max_cal_requests) {
         ret = -ENOMEM;
         goto err_out;
diff --git a/kernel/drivers/net/stack/rtskb.c b/kernel/drivers/net/stack/rtskb.c
index ae0466a..11043f8 100644
--- a/kernel/drivers/net/stack/rtskb.c
+++ b/kernel/drivers/net/stack/rtskb.c
@@ -39,7 +39,7 @@ MODULE_PARM_DESC(global_rtskbs, "Number of realtime socket 
buffers in global poo
 static struct kmem_cache *rtskb_slab_pool;
 
 /* pool of rtskbs for global use */
-struct rtskb_queue global_pool;
+struct rtskb_pool global_pool;
 EXPORT_SYMBOL(global_pool);
 
 /* pool statistics */
@@ -151,26 +151,71 @@ void rtskb_under_panic(struct rtskb *skb, int sz, void 
*here)
 EXPORT_SYMBOL(rtskb_under_panic);
 #endif /* CONFIG_XENO_DRIVERS_NET_CHECKED */
 
+static struct rtskb *__rtskb_pool_dequeue(struct rtskb_pool *pool)
+{
+    struct rtskb_queue *queue = &pool->queue;
+    struct rtskb *skb;
+
+    if (pool->lock_count == 0 && !pool->lock_ops->trylock(pool->lock_cookie))
+            return NULL;
+    skb = __rtskb_dequeue(queue);
+    if (skb == NULL) {
+        if (pool->lock_count == 0) /* This can only happen if pool has 0 
packets */
+            pool->lock_ops->unlock(pool->lock_cookie);
+    } else
+        ++pool->lock_count;
+
+    return skb;
+}
+
+struct rtskb *rtskb_pool_dequeue(struct rtskb_pool *pool)
+{
+    struct rtskb_queue *queue = &pool->queue;
+    rtdm_lockctx_t context;
+    struct rtskb *skb;
+
+    rtdm_lock_get_irqsave(&queue->lock, context);
+    skb = __rtskb_pool_dequeue(pool);
+    rtdm_lock_put_irqrestore(&queue->lock, context);
+
+    return skb;
+}
+EXPORT_SYMBOL_GPL(rtskb_pool_dequeue);
+
+static void __rtskb_pool_queue_tail(struct rtskb_pool *pool, struct rtskb *skb)
+{
+    struct rtskb_queue *queue = &pool->queue;
+
+    __rtskb_queue_tail(queue,skb);
+    if (--pool->lock_count == 0)
+        pool->lock_ops->unlock(pool->lock_cookie);
+}
+
+void rtskb_pool_queue_tail(struct rtskb_pool *pool, struct rtskb *skb)
+{
+    struct rtskb_queue *queue = &pool->queue;
+    rtdm_lockctx_t context;
+
+    rtdm_lock_get_irqsave(&queue->lock, context);
+    __rtskb_pool_queue_tail(pool, skb);
+    rtdm_lock_put_irqrestore(&queue->lock, context);
+}
+EXPORT_SYMBOL_GPL(rtskb_pool_queue_tail);
 
 /***
  *  alloc_rtskb - allocate an rtskb from a pool
  *  @size: required buffer size (to check against maximum boundary)
  *  @pool: pool to take the rtskb from
  */
-struct rtskb *alloc_rtskb(unsigned int size, struct rtskb_queue *pool)
+struct rtskb *alloc_rtskb(unsigned int size, struct rtskb_pool *pool)
 {
     struct rtskb *skb;
 
-
     RTNET_ASSERT(size <= SKB_DATA_ALIGN(RTSKB_SIZE), return NULL;);
 
-    skb = rtskb_dequeue(pool);
+    skb = rtskb_pool_dequeue(pool);
     if (!skb)
         return NULL;
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    pool->pool_balance--;
-    skb->chain_len = 1;
-#endif
 
     /* Load the data pointers. */
     skb->data = skb->buf_start;
@@ -228,29 +273,21 @@ void kfree_rtskb(struct rtskb *skb)
 
             rtdm_lock_put_irqrestore(&rtcap_lock, context);
 
-            rtskb_queue_tail(comp_skb->pool, comp_skb);
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-            comp_skb->pool->pool_balance++;
-#endif
+            rtskb_pool_queue_tail(comp_skb->pool, comp_skb);
         }
         else {
             rtdm_lock_put_irqrestore(&rtcap_lock, context);
 
             skb->chain_end = skb;
-            rtskb_queue_tail(skb->pool, skb);
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-            skb->pool->pool_balance++;
-#endif
+            rtskb_pool_queue_tail(skb->pool, skb);
         }
 
     } while (chain_end != skb);
 
 #else  /* CONFIG_XENO_DRIVERS_NET_ADDON_RTCAP */
 
-    rtskb_queue_tail(skb->pool, skb);
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    skb->pool->pool_balance += skb->chain_len;
-#endif
+    rtskb_pool_queue_tail(skb->pool, skb);
+
 
 #endif /* CONFIG_XENO_DRIVERS_NET_ADDON_RTCAP */
 }
@@ -264,15 +301,14 @@ EXPORT_SYMBOL(kfree_rtskb);
  *  @initial_size: number of rtskbs to allocate
  *  return: number of actually allocated rtskbs
  */
-unsigned int rtskb_pool_init(struct rtskb_queue *pool,
-                             unsigned int initial_size)
+unsigned int rtskb_pool_init(struct rtskb_pool *pool,
+                            unsigned int initial_size,
+                            const struct rtskb_pool_lock_ops *lock_ops,
+                            void *lock_cookie)
 {
     unsigned int i;
 
-    rtskb_queue_init(pool);
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    pool->pool_balance = 0;
-#endif
+    rtskb_queue_init(&pool->queue);
 
     i = rtskb_pool_extend(pool, initial_size);
 
@@ -280,33 +316,64 @@ unsigned int rtskb_pool_init(struct rtskb_queue *pool,
     if (rtskb_pools > rtskb_pools_max)
         rtskb_pools_max = rtskb_pools;
 
+    pool->lock_ops = lock_ops;
+    pool->lock_count = 0;
+    pool->lock_cookie = lock_cookie;
+
     return i;
 }
 
 EXPORT_SYMBOL(rtskb_pool_init);
 
+static int rtskb_module_pool_trylock(void *cookie)
+{
+    return try_module_get(cookie);
+}
+
+static void rtskb_module_pool_unlock(void *cookie)
+{
+    module_put(cookie);
+}
+
+const struct rtskb_pool_lock_ops rtskb_module_lock_ops = {
+    .trylock = rtskb_module_pool_trylock,
+    .unlock = rtskb_module_pool_unlock,
+};
+
+unsigned int __rtskb_module_pool_init(struct rtskb_pool *pool,
+                                    unsigned int initial_size,
+                                    struct module *module)
+{
+    return rtskb_pool_init(pool, initial_size, &rtskb_module_lock_ops, module);
+}
+EXPORT_SYMBOL(__rtskb_module_pool_init);
+
 
 /***
  *  __rtskb_pool_release
  *  @pool: pool to release
  */
-void __rtskb_pool_release(struct rtskb_queue *pool)
+int rtskb_pool_release(struct rtskb_pool *pool)
 {
     struct rtskb *skb;
 
-    while ((skb = rtskb_dequeue(pool)) != NULL) {
+    if (pool->lock_count)
+        return -EBUSY;
+
+    while ((skb = rtskb_dequeue(&pool->queue)) != NULL) {
         rtdev_unmap_rtskb(skb);
         kmem_cache_free(rtskb_slab_pool, skb);
         rtskb_amount--;
     }
 
     rtskb_pools--;
+    return 0;
 }
 
-EXPORT_SYMBOL(__rtskb_pool_release);
+EXPORT_SYMBOL(rtskb_pool_release);
 
 
-unsigned int rtskb_pool_extend(struct rtskb_queue *pool,
+unsigned int rtskb_pool_extend(struct rtskb_pool *pool,
                                unsigned int add_rtskbs)
 {
     unsigned int i;
@@ -335,7 +402,7 @@ unsigned int rtskb_pool_extend(struct rtskb_queue *pool,
         if (rtdev_map_rtskb(skb) < 0)
             break;
 
-        rtskb_queue_tail(pool, skb);
+        rtskb_queue_tail(&pool->queue, skb);
 
         rtskb_amount++;
         if (rtskb_amount > rtskb_amount_max)
@@ -346,7 +413,7 @@ unsigned int rtskb_pool_extend(struct rtskb_queue *pool,
 }
 
 
-unsigned int rtskb_pool_shrink(struct rtskb_queue *pool,
+unsigned int rtskb_pool_shrink(struct rtskb_pool *pool,
                                unsigned int rem_rtskbs)
 {
     unsigned int    i;
@@ -354,7 +421,7 @@ unsigned int rtskb_pool_shrink(struct rtskb_queue *pool,
 
 
     for (i = 0; i < rem_rtskbs; i++) {
-        if ((skb = rtskb_dequeue(pool)) == NULL)
+        if ((skb = rtskb_dequeue(&pool->queue)) == NULL)
             break;
 
         rtdev_unmap_rtskb(skb);
@@ -367,39 +434,31 @@ unsigned int rtskb_pool_shrink(struct rtskb_queue *pool,
 
 
 /* Note: acquires only the first skb of a chain! */
-int rtskb_acquire(struct rtskb *rtskb, struct rtskb_queue *comp_pool)
+int rtskb_acquire(struct rtskb *rtskb, struct rtskb_pool *comp_pool)
 {
     struct rtskb *comp_rtskb;
-    struct rtskb_queue *release_pool;
+    struct rtskb_pool *release_pool;
     rtdm_lockctx_t context;
 
 
-    rtdm_lock_get_irqsave(&comp_pool->lock, context);
+    rtdm_lock_get_irqsave(&comp_pool->queue.lock, context);
 
-    comp_rtskb = __rtskb_dequeue(comp_pool);
+    comp_rtskb = __rtskb_pool_dequeue(comp_pool);
     if (!comp_rtskb) {
-        rtdm_lock_put_irqrestore(&comp_pool->lock, context);
+        rtdm_lock_put_irqrestore(&comp_pool->queue.lock, context);
         return -ENOMEM;
     }
 
-    rtdm_lock_put(&comp_pool->lock);
-
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    comp_pool->pool_balance--;
-#endif
+    rtdm_lock_put(&comp_pool->queue.lock);
 
     comp_rtskb->chain_end = comp_rtskb;
     comp_rtskb->pool = release_pool = rtskb->pool;
 
-    rtdm_lock_get(&release_pool->lock);
+    rtdm_lock_get(&release_pool->queue.lock);
 
-#ifdef CONFIG_XENO_DRIVERS_NET_CHECKED
-    comp_rtskb->chain_len = 1;
-    release_pool->pool_balance++;
-#endif
-    __rtskb_queue_tail(release_pool, comp_rtskb);
+    __rtskb_pool_queue_tail(release_pool, comp_rtskb);
 
-    rtdm_lock_put_irqrestore(&release_pool->lock, context);
+    rtdm_lock_put_irqrestore(&release_pool->queue.lock, context);
 
     rtskb->pool = comp_pool;
 
@@ -410,7 +469,7 @@ EXPORT_SYMBOL(rtskb_acquire);
 
 
 /* clone rtskb to another, allocating the new rtskb from pool */
-struct rtskb* rtskb_clone(struct rtskb *rtskb, struct rtskb_queue *pool)
+struct rtskb* rtskb_clone(struct rtskb *rtskb, struct rtskb_pool *pool)
 {
     struct rtskb    *clone_rtskb;
     unsigned int    total_len;
@@ -473,7 +532,7 @@ int rtskb_pools_init(void)
     rtskb_amount_max = 0;
 
     /* create the global rtskb pool */
-    if (rtskb_pool_init(&global_pool, global_rtskbs) < global_rtskbs)
+    if (rtskb_module_pool_init(&global_pool, global_rtskbs) < global_rtskbs)
         goto err_out;
 
 #ifdef CONFIG_XENO_DRIVERS_NET_ADDON_RTCAP
diff --git a/kernel/drivers/net/stack/socket.c 
b/kernel/drivers/net/stack/socket.c
index 463dc12..e1ba07a 100644
--- a/kernel/drivers/net/stack/socket.c
+++ b/kernel/drivers/net/stack/socket.c
@@ -49,17 +49,33 @@ MODULE_PARM_DESC(socket_rtskbs, "Default number of realtime 
socket buffers in so
  *  internal socket functions                                           *
  ************************************************************************/
 
-int rt_bare_socket_init(struct rtsocket *sock, unsigned short protocol,
+static int rtskb_socket_pool_trylock(void *cookie)
+{
+    return rtdm_fd_lock(cookie) >= 0;
+}
+
+static void rtskb_socket_pool_unlock(void *cookie)
+{
+    rtdm_fd_unlock(cookie);
+}
+
+static const struct rtskb_pool_lock_ops rtskb_socket_pool_ops = {
+    .trylock = rtskb_socket_pool_trylock,
+    .unlock = rtskb_socket_pool_unlock,
+};
+
+int rt_bare_socket_init(struct rtdm_fd *fd,
+                       unsigned short protocol,
                        unsigned int priority, unsigned int pool_size)
 {
+    struct rtsocket *sock = rtdm_fd_to_private(fd);
     sock->protocol = protocol;
     sock->priority = priority;
 
-    return rtskb_pool_init(&sock->skb_pool, pool_size);
+    return rtskb_pool_init(&sock->skb_pool,
+                       pool_size, &rtskb_socket_pool_ops, fd);
 }
-EXPORT_SYMBOL(rt_bare_socket_init);
-
-
+EXPORT_SYMBOL_GPL(rt_bare_socket_init);
 
 /***
  *  rt_socket_init - initialises a new socket structure
@@ -79,10 +95,9 @@ int rt_socket_init(struct rtdm_fd *fd, unsigned short 
protocol)
     rtdm_lock_init(&sock->param_lock);
     rtdm_sem_init(&sock->pending_sem, 0);
 
-    pool_size = rt_bare_socket_init(sock, protocol,
-                                   RTSKB_PRIO_VALUE(SOCK_DEF_PRIO,
-                                                    RTSKB_DEF_RT_CHANNEL),
-                                   socket_rtskbs);
+    pool_size = rt_bare_socket_init(fd, protocol,
+               RTSKB_PRIO_VALUE(SOCK_DEF_PRIO,
+                               RTSKB_DEF_RT_CHANNEL), socket_rtskbs);
     sock->pool_size = pool_size;
     mutex_init(&sock->pool_nrt_lock);
 
@@ -103,10 +118,9 @@ int rt_socket_init(struct rtdm_fd *fd, unsigned short 
protocol)
 /***
  *  rt_socket_cleanup - releases resources allocated for the socket
  */
-int rt_socket_cleanup(struct rtdm_fd *fd)
+void rt_socket_cleanup(struct rtdm_fd *fd)
 {
     struct rtsocket *sock  = rtdm_fd_to_private(fd);
-    int ret = 0;
 
 
     rtdm_sem_destroy(&sock->pending_sem);
@@ -115,18 +129,10 @@ int rt_socket_cleanup(struct rtdm_fd *fd)
 
     set_bit(SKB_POOL_CLOSED, &sock->flags);
 
-    if (sock->pool_size > 0) {
-       sock->pool_size -= rtskb_pool_shrink(&sock->skb_pool, sock->pool_size);
-
-       if (sock->pool_size > 0)
-           ret = -EAGAIN;
-       else
-           rtskb_pool_release(&sock->skb_pool);
-    }
+    if (sock->pool_size > 0)
+       rtskb_pool_release(&sock->skb_pool);
 
     mutex_unlock(&sock->pool_nrt_lock);
-
-    return ret;
 }
 
 
diff --git a/kernel/drivers/net/stack/stack_mgr.c 
b/kernel/drivers/net/stack/stack_mgr.c
index 4286292..820d9d3 100644
--- a/kernel/drivers/net/stack/stack_mgr.c
+++ b/kernel/drivers/net/stack/stack_mgr.c
@@ -59,21 +59,21 @@ int rtdev_add_pack(struct rtpacket_type *pt)
     INIT_LIST_HEAD(&pt->list_entry);
     pt->refcount = 0;
     if (pt->trylock == NULL)
-       pt->trylock = rtdev_lock_pack;
+        pt->trylock = rtdev_lock_pack;
     if (pt->unlock == NULL)
-       pt->unlock = rtdev_unlock_pack;
+        pt->unlock = rtdev_unlock_pack;
 
     rtdm_lock_get_irqsave(&rt_packets_lock, context);
 
     if (pt->type == htons(ETH_P_ALL))
 #ifdef CONFIG_XENO_DRIVERS_NET_ETH_P_ALL
-       list_add_tail(&pt->list_entry, &rt_packets_all);
+        list_add_tail(&pt->list_entry, &rt_packets_all);
 #else /* !CONFIG_XENO_DRIVERS_NET_ETH_P_ALL */
-       ret = -EINVAL;
+        ret = -EINVAL;
 #endif /* CONFIG_XENO_DRIVERS_NET_ETH_P_ALL */
     else
-       list_add_tail(&pt->list_entry,
-                     &rt_packets[ntohs(pt->type) & RTPACKET_HASH_KEY_MASK]);
+        list_add_tail(&pt->list_entry,
+                      &rt_packets[ntohs(pt->type) & RTPACKET_HASH_KEY_MASK]);
 
     rtdm_lock_put_irqrestore(&rt_packets_lock, context);
 
@@ -98,9 +98,9 @@ int rtdev_remove_pack(struct rtpacket_type *pt)
     rtdm_lock_get_irqsave(&rt_packets_lock, context);
 
     if (pt->refcount > 0)
-       ret = -EAGAIN;
+        ret = -EAGAIN;
     else
-       list_del(&pt->list_entry);
+        list_del(&pt->list_entry);
 
     rtdm_lock_put_irqrestore(&rt_packets_lock, context);
 
@@ -128,9 +128,9 @@ void rtnetif_rx(struct rtskb *skb)
     rtdev_reference(rtdev);
 
     if (unlikely(rtskb_fifo_insert_inirq(&rx.fifo, skb) < 0)) {
-       rtdm_printk("RTnet: dropping packet in %s()\n", __FUNCTION__);
-       kfryee_rtskb(skb);
-       rtdev_dereference(rtdev);
+        rtdm_printk("RTnet: dropping packet in %s()\n", __FUNCTION__);
+        kfree_rtskb(skb);
+        rtdev_dereference(rtdev);
     }
 }
 
@@ -162,46 +162,46 @@ __DELIVER_PREFIX void rt_stack_deliver(struct rtskb 
*rtskb)
 #ifdef CONFIG_XENO_DRIVERS_NET_ETH_P_ALL
     eth_p_all_hit = 0;
     list_for_each_entry(pt_entry, &rt_packets_all, list_entry) {
-       if (!pt_entry->trylock(pt_entry))
-           continue;
-       rtdm_lock_put_irqrestore(&rt_packets_lock, context);
+        if (!pt_entry->trylock(pt_entry))
+            continue;
+        rtdm_lock_put_irqrestore(&rt_packets_lock, context);
 
-       pt_entry->handler(rtskb, pt_entry);
+        pt_entry->handler(rtskb, pt_entry);
 
-       rtdm_lock_get_irqsave(&rt_packets_lock, context);
-       pt_entry->unlock(pt_entry);
-       eth_p_all_hit = 1;
+        rtdm_lock_get_irqsave(&rt_packets_lock, context);
+        pt_entry->unlock(pt_entry);
+        eth_p_all_hit = 1;
     }
 #endif /* CONFIG_XENO_DRIVERS_NET_ETH_P_ALL */
 
     hash = ntohs(rtskb->protocol) & RTPACKET_HASH_KEY_MASK;
 
     list_for_each_entry(pt_entry, &rt_packets[hash], list_entry)
-       if (pt_entry->type == rtskb->protocol) {
-           if (!pt_entry->trylock(pt_entry))
-               continue;
-           rtdm_lock_put_irqrestore(&rt_packets_lock, context);
+        if (pt_entry->type == rtskb->protocol) {
+            if (!pt_entry->trylock(pt_entry))
+                continue;
+            rtdm_lock_put_irqrestore(&rt_packets_lock, context);
 
-           err = pt_entry->handler(rtskb, pt_entry);
+            err = pt_entry->handler(rtskb, pt_entry);
 
-           rtdm_lock_get_irqsave(&rt_packets_lock, context);
-           pt_entry->unlock(pt_entry);
+            rtdm_lock_get_irqsave(&rt_packets_lock, context);
+            pt_entry->unlock(pt_entry);
 
-           rtdev_dereference(rtdev);
+            rtdev_dereference(rtdev);
 
-           if (likely(!err)) {
-               rtdm_lock_put_irqrestore(&rt_packets_lock, context);
-               return;
-           }
-       }
+            if (likely(!err)) {
+                rtdm_lock_put_irqrestore(&rt_packets_lock, context);
+                return;
+            }
+        }
 
     rtdm_lock_put_irqrestore(&rt_packets_lock, context);
 
     /* Don't warn if ETH_P_ALL listener were present or when running in
        promiscuous mode (RTcap). */
     if (unlikely(!eth_p_all_hit && !(rtdev->flags & IFF_PROMISC)))
-       rtdm_printk("RTnet: no one cared for packet with layer 3 "
-                   "protocol type 0x%04x\n", ntohs(rtskb->protocol));
+        rtdm_printk("RTnet: no one cared for packet with layer 3 "
+                    "protocol type 0x%04x\n", ntohs(rtskb->protocol));
 
     kfree_rtskb(rtskb);
     rtdev_dereference(rtdev);
@@ -219,9 +219,9 @@ static void rt_stack_mgr_task(void *arg)
 
 
     while (rtdm_event_wait(mgr_event) == 0) {
-       /* we are the only reader => no locking required */
-       while ((rtskb = __rtskb_fifo_remove(&rx.fifo)))
-           rt_stack_deliver(rtskb);
+        /* we are the only reader => no locking required */
+        while ((rtskb = __rtskb_fifo_remove(&rx.fifo)))
+            rt_stack_deliver(rtskb);
     }
 }
 
@@ -259,7 +259,7 @@ int rt_stack_mgr_init (struct rtnet_mgr *mgr)
     rtskb_fifo_init(&rx.fifo, CONFIG_XENO_DRIVERS_NET_RX_FIFO_SIZE);
 
     for (i = 0; i < RTPACKET_HASH_TBL_SIZE; i++)
-       INIT_LIST_HEAD(&rt_packets[i]);
+        INIT_LIST_HEAD(&rt_packets[i]);
 #ifdef CONFIG_XENO_DRIVERS_NET_ETH_P_ALL
     INIT_LIST_HEAD(&rt_packets_all);
 #endif /* CONFIG_XENO_DRIVERS_NET_ETH_P_ALL */
@@ -267,7 +267,7 @@ int rt_stack_mgr_init (struct rtnet_mgr *mgr)
     rtdm_event_init(&mgr->event, 0);
 
     return rtdm_task_init(&mgr->task, "rtnet-stack", rt_stack_mgr_task, mgr,
-                         stack_mgr_prio, 0);
+                          stack_mgr_prio, 0);
 }
 
 


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to