aoenet.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoenet.c
  4. * Ethernet portion of AoE driver
  5. */
  6. #include <linux/gfp.h>
  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. #define NECODES 5
  15. static char *aoe_errlist[] =
  16. {
  17. "no such error",
  18. "unrecognized command code",
  19. "bad argument parameter",
  20. "device unavailable",
  21. "config string present",
  22. "unsupported version"
  23. };
  24. enum {
  25. IFLISTSZ = 1024,
  26. };
  27. static char aoe_iflist[IFLISTSZ];
  28. module_param_string(aoe_iflist, aoe_iflist, IFLISTSZ, 0600);
  29. MODULE_PARM_DESC(aoe_iflist, "aoe_iflist=\"dev1 [dev2 ...]\"");
  30. static wait_queue_head_t txwq;
  31. static struct ktstate kts;
  32. #ifndef MODULE
  33. static int __init aoe_iflist_setup(char *str)
  34. {
  35. strncpy(aoe_iflist, str, IFLISTSZ);
  36. aoe_iflist[IFLISTSZ - 1] = '\0';
  37. return 1;
  38. }
  39. __setup("aoe_iflist=", aoe_iflist_setup);
  40. #endif
  41. static spinlock_t txlock;
  42. static struct sk_buff_head skbtxq;
  43. /* enters with txlock held */
  44. static int
  45. tx(void)
  46. {
  47. struct sk_buff *skb;
  48. while ((skb = skb_dequeue(&skbtxq))) {
  49. spin_unlock_irq(&txlock);
  50. dev_queue_xmit(skb);
  51. spin_lock_irq(&txlock);
  52. }
  53. return 0;
  54. }
  55. int
  56. is_aoe_netif(struct net_device *ifp)
  57. {
  58. register char *p, *q;
  59. register int len;
  60. if (aoe_iflist[0] == '\0')
  61. return 1;
  62. p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
  63. for (; *p; p = q + strspn(q, WHITESPACE)) {
  64. q = p + strcspn(p, WHITESPACE);
  65. if (q != p)
  66. len = q - p;
  67. else
  68. len = strlen(p); /* last token in aoe_iflist */
  69. if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
  70. return 1;
  71. if (q == p)
  72. break;
  73. }
  74. return 0;
  75. }
  76. int
  77. set_aoe_iflist(const char __user *user_str, size_t size)
  78. {
  79. if (size >= IFLISTSZ)
  80. return -EINVAL;
  81. if (copy_from_user(aoe_iflist, user_str, size)) {
  82. printk(KERN_INFO "aoe: copy from user failed\n");
  83. return -EFAULT;
  84. }
  85. aoe_iflist[size] = 0x00;
  86. return 0;
  87. }
  88. void
  89. aoenet_xmit(struct sk_buff_head *queue)
  90. {
  91. struct sk_buff *skb, *tmp;
  92. ulong flags;
  93. skb_queue_walk_safe(queue, skb, tmp) {
  94. __skb_unlink(skb, queue);
  95. spin_lock_irqsave(&txlock, flags);
  96. skb_queue_tail(&skbtxq, skb);
  97. spin_unlock_irqrestore(&txlock, flags);
  98. wake_up(&txwq);
  99. }
  100. }
  101. /*
  102. * (1) len doesn't include the header by default. I want this.
  103. */
  104. static int
  105. aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
  106. {
  107. struct aoe_hdr *h;
  108. struct aoe_atahdr *ah;
  109. u32 n;
  110. int sn;
  111. if (dev_net(ifp) != &init_net)
  112. goto exit;
  113. skb = skb_share_check(skb, GFP_ATOMIC);
  114. if (skb == NULL)
  115. return 0;
  116. if (!is_aoe_netif(ifp))
  117. goto exit;
  118. skb_push(skb, ETH_HLEN); /* (1) */
  119. sn = sizeof(*h) + sizeof(*ah);
  120. if (skb->len >= sn) {
  121. sn -= skb_headlen(skb);
  122. if (sn > 0 && !__pskb_pull_tail(skb, sn))
  123. goto exit;
  124. }
  125. h = (struct aoe_hdr *) skb->data;
  126. n = get_unaligned_be32(&h->tag);
  127. if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
  128. goto exit;
  129. if (h->verfl & AOEFL_ERR) {
  130. n = h->err;
  131. if (n > NECODES)
  132. n = 0;
  133. if (net_ratelimit())
  134. printk(KERN_ERR
  135. "%s%d.%d@%s; ecode=%d '%s'\n",
  136. "aoe: error packet from ",
  137. get_unaligned_be16(&h->major),
  138. h->minor, skb->dev->name,
  139. h->err, aoe_errlist[n]);
  140. goto exit;
  141. }
  142. switch (h->cmd) {
  143. case AOECMD_ATA:
  144. /* ata_rsp may keep skb for later processing or give it back */
  145. skb = aoecmd_ata_rsp(skb);
  146. break;
  147. case AOECMD_CFG:
  148. aoecmd_cfg_rsp(skb);
  149. break;
  150. default:
  151. if (h->cmd >= AOECMD_VEND_MIN)
  152. break; /* don't complain about vendor commands */
  153. pr_info("aoe: unknown AoE command type 0x%02x\n", h->cmd);
  154. break;
  155. }
  156. if (!skb)
  157. return 0;
  158. exit:
  159. dev_kfree_skb(skb);
  160. return 0;
  161. }
  162. static struct packet_type aoe_pt __read_mostly = {
  163. .type = __constant_htons(ETH_P_AOE),
  164. .func = aoenet_rcv,
  165. };
  166. int __init
  167. aoenet_init(void)
  168. {
  169. skb_queue_head_init(&skbtxq);
  170. init_waitqueue_head(&txwq);
  171. spin_lock_init(&txlock);
  172. kts.lock = &txlock;
  173. kts.fn = tx;
  174. kts.waitq = &txwq;
  175. kts.name = "aoe_tx";
  176. if (aoe_ktstart(&kts))
  177. return -EAGAIN;
  178. dev_add_pack(&aoe_pt);
  179. return 0;
  180. }
  181. void
  182. aoenet_exit(void)
  183. {
  184. aoe_ktstop(&kts);
  185. skb_queue_purge(&skbtxq);
  186. dev_remove_pack(&aoe_pt);
  187. }