"Fossies" - the Fresh Open Source Software Archive

Member "aoe-aoe6-86/linux/drivers/block/aoe/aoenet.c" (4 Jul 2015, 5397 Bytes) of archive /linux/misc/aoe-aoe6-86.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (c) 2013 Coraid, Inc.  See COPYING for GPL terms. */
    2 /*
    3  * aoenet.c
    4  * Ethernet portion of AoE driver
    5  */
    6 
    7 #include <linux/hdreg.h>
    8 #include <linux/blkdev.h>
    9 #include <linux/netdevice.h>
   10 #include <linux/moduleparam.h>
   11 #include <net/net_namespace.h>
   12 #include <asm/unaligned.h>
   13 #include "aoe.h"
   14 
   15 #define NECODES 5
   16 
   17 static char *aoe_errlist[] =
   18 {
   19     "no such error",
   20     "unrecognized command code",
   21     "bad argument parameter",
   22     "device unavailable",
   23     "config string present",
   24     "unsupported version"
   25 };
   26 
   27 enum {
   28     IFLISTSZ = 1024,
   29 };
   30 
   31 static char aoe_iflist[IFLISTSZ];
   32 module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
   33 MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=dev1[,dev2...]\n");
   34 
   35 static wait_queue_head_t txwq;
   36 static struct ktstate kts;
   37 
   38 #ifndef MODULE
   39 static int __init aoe_iflist_setup(char *str)
   40 {
   41     strncpy(aoe_iflist, str, IFLISTSZ);
   42     aoe_iflist[IFLISTSZ - 1] = '\0';
   43     return 1;
   44 }
   45 
   46 __setup("aoe_iflist=", aoe_iflist_setup);
   47 #endif
   48 
   49 static spinlock_t txlock;
   50 static struct sk_buff_head skbtxq;
   51 
   52 /* enters with txlock held */
   53 static int
   54 tx(int id)
   55 {
   56     struct sk_buff *skb;
   57     struct aoe_hdr *h;
   58     struct aoe_atahdr *ah;
   59     struct net_device *ifp;
   60     int changef;
   61 
   62     while ((skb = skb_dequeue(&skbtxq))) {
   63         spin_unlock_irq(&txlock);
   64         h = (struct aoe_hdr *) skb_mac_header(skb);
   65         ah = (struct aoe_atahdr *) (h + 1);
   66         if (current->flags & PF_MEMALLOC)
   67             changef = 0;
   68         else
   69             changef = ah->aflags & AOEAFL_WRITE;
   70         if (changef)
   71             current->flags |= PF_MEMALLOC;
   72         ifp = skb->dev;
   73         if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit())
   74             printk(KERN_WARNING
   75                 "aoe: packet could not be sent on %s.  %s\n",
   76                 ifp ? ifp->name : "netif",
   77                 "consider increasing tx_queue_len");
   78         if (changef)
   79             current->flags &= ~PF_MEMALLOC;
   80         spin_lock_irq(&txlock);
   81     }
   82     return 0;
   83 }
   84 
   85 /* This function is copied here from linux-2.6.10-rc3-bk11/lib/string.c
   86  * for compatibility with FC2, which issues a warning on MODPOST
   87  * about strcspn being undefined.
   88  */
   89 static size_t
   90 aoe_strcspn(const char *s, const char *reject)
   91 {
   92     const char *p;
   93     const char *r;
   94     size_t count = 0;
   95 
   96     for (p = s; *p != '\0'; ++p) {
   97         for (r = reject; *r != '\0'; ++r) {
   98             if (*p == *r)
   99                 return count;
  100         }
  101         ++count;
  102     }
  103 
  104     return count;
  105 }
  106 
  107 int
  108 is_aoe_netif(struct net_device *ifp)
  109 {
  110     register char *p, *q;
  111     register int len;
  112 
  113     if (aoe_iflist[0] == '\0')
  114         return 1;
  115 
  116     p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
  117     for (; *p; p = q + strspn(q, WHITESPACE)) {
  118         q = p + aoe_strcspn(p, WHITESPACE);
  119         if (q != p)
  120             len = q - p;
  121         else
  122             len = strlen(p); /* last token in aoe_iflist */
  123 
  124         if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
  125             return 1;
  126         if (q == p)
  127             break;
  128     }
  129 
  130     return 0;
  131 }
  132 
  133 int
  134 set_aoe_iflist(const char __user *user_str, size_t size)
  135 {
  136     if (size >= IFLISTSZ)
  137         return -EINVAL;
  138 
  139     if (copy_from_user(aoe_iflist, user_str, size)) {
  140         printk(KERN_INFO "aoe: copy from user failed\n");
  141         return -EFAULT;
  142     }
  143     aoe_iflist[size] = 0x00;
  144     return 0;
  145 }
  146 
  147 unsigned long long
  148 mac_addr(char addr[6])
  149 {
  150     u64 n = 0;
  151     char *p = (char *) &n;
  152 
  153     memcpy(p + 2, addr, 6); /* (sizeof addr != 6) */
  154 
  155     return (unsigned long long) __be64_to_cpu(n);
  156 }
  157 
  158 void
  159 aoenet_xmit(struct sk_buff *sl)
  160 {
  161     struct sk_buff *skb;
  162     ulong flags;
  163 
  164     while ((skb = sl)) {
  165         sl = sl->next;
  166         skb->next = skb->prev = NULL;
  167         spin_lock_irqsave(&txlock, flags);
  168         skb_queue_tail(&skbtxq, skb);
  169         spin_unlock_irqrestore(&txlock, flags);
  170         wake_up(&txwq);
  171     }
  172 }
  173 
  174 /*
  175  * (1) len doesn't include the header by default.  I want this.
  176  */
  177 static int
  178 aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
  179 {
  180     struct aoe_hdr *h;
  181     struct aoe_atahdr *ah;
  182     u32 n;
  183     int sn;
  184 
  185     if (dev_net(ifp) != &init_net)
  186         goto exit;
  187 
  188     skb = skb_share_check(skb, GFP_ATOMIC);
  189     if (skb == NULL)
  190         return 0;
  191     if (!is_aoe_netif(ifp))
  192         goto exit;
  193     skb_push(skb, ETH_HLEN);    /* (1) */
  194     sn = sizeof(*h) + sizeof(*ah);
  195     if (skb->len >= sn) {
  196         sn -= skb_headlen(skb);
  197         if (sn > 0 && !__pskb_pull_tail(skb, sn))
  198             goto exit;
  199     }
  200     h = (struct aoe_hdr *) skb->data;
  201     n = be32_to_cpu(get_unaligned(&h->tag));
  202     if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
  203         goto exit;
  204 
  205     if (h->verfl & AOEFL_ERR) {
  206         n = h->err;
  207         if (n > NECODES)
  208             n = 0;
  209         if (net_ratelimit())
  210             printk(KERN_ERR
  211                 "aoe: error packet from %d.%d@%s; ecode=%d '%s'\n",
  212                 be16_to_cpu(get_unaligned(&h->major)), h->minor,
  213                 skb->dev->name, h->err, aoe_errlist[n]);
  214         goto exit;
  215     }
  216 
  217     switch (h->cmd) {
  218     case AOECMD_ATA:
  219         /* ata_rsp may keep skb for later processing or give it back */
  220         skb = aoecmd_ata_rsp(skb);
  221         break;
  222     case AOECMD_CFG:
  223         aoecmd_cfg_rsp(skb);
  224         break;
  225     default:
  226         if (h->cmd >= AOECMD_VEND_MIN)
  227             break;  /* don't complain about vendor commands */
  228         printk(KERN_INFO
  229             "aoe: warning: unknown aoe command type 0x%02x\n",
  230             h->cmd);
  231         break;
  232     }
  233     if (skb)
  234 exit:       dev_kfree_skb(skb);
  235     return 0;
  236 }
  237 
  238 static struct packet_type aoe_pt = {
  239     .type = __constant_htons(ETH_P_AOE),
  240     .func = aoenet_rcv,
  241 };
  242 
  243 int __init
  244 aoenet_init(void)
  245 {
  246     skb_queue_head_init(&skbtxq);
  247     init_waitqueue_head(&txwq);
  248     spin_lock_init(&txlock);
  249     kts.lock = &txlock;
  250     kts.fn = tx;
  251     kts.waitq = &txwq;
  252     kts.id = 0;
  253     snprintf(kts.name, sizeof(kts.name), "aoe_tx%d", kts.id);
  254     if (aoe_ktstart(&kts))
  255         return -EAGAIN;
  256     dev_add_pack(&aoe_pt);
  257     return 0;
  258 }
  259 
  260 void
  261 aoenet_exit(void)
  262 {
  263     aoe_ktstop(&kts);
  264     skb_queue_purge(&skbtxq);
  265     dev_remove_pack(&aoe_pt);
  266 }
  267