aoenet.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (c) 2013 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(int id) __must_hold(&txlock)
  46. {
  47. struct sk_buff *skb;
  48. struct net_device *ifp;
  49. while ((skb = skb_dequeue(&skbtxq))) {
  50. spin_unlock_irq(&txlock);
  51. ifp = skb->dev;
  52. if (dev_queue_xmit(skb) == NET_XMIT_DROP && net_ratelimit())
  53. pr_warn("aoe: packet could not be sent on %s. %s\n",
  54. ifp ? ifp->name : "netif",
  55. "consider increasing tx_queue_len");
  56. spin_lock_irq(&txlock);
  57. }
  58. return 0;
  59. }
  60. int
  61. is_aoe_netif(struct net_device *ifp)
  62. {
  63. register char *p, *q;
  64. register int len;
  65. if (aoe_iflist[0] == '\0')
  66. return 1;
  67. p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
  68. for (; *p; p = q + strspn(q, WHITESPACE)) {
  69. q = p + strcspn(p, WHITESPACE);
  70. if (q != p)
  71. len = q - p;
  72. else
  73. len = strlen(p); /* last token in aoe_iflist */
  74. if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
  75. return 1;
  76. if (q == p)
  77. break;
  78. }
  79. return 0;
  80. }
  81. int
  82. set_aoe_iflist(const char __user *user_str, size_t size)
  83. {
  84. if (size >= IFLISTSZ)
  85. return -EINVAL;
  86. if (copy_from_user(aoe_iflist, user_str, size)) {
  87. printk(KERN_INFO "aoe: copy from user failed\n");
  88. return -EFAULT;
  89. }
  90. aoe_iflist[size] = 0x00;
  91. return 0;
  92. }
  93. void
  94. aoenet_xmit(struct sk_buff_head *queue)
  95. {
  96. struct sk_buff *skb, *tmp;
  97. ulong flags;
  98. skb_queue_walk_safe(queue, skb, tmp) {
  99. __skb_unlink(skb, queue);
  100. spin_lock_irqsave(&txlock, flags);
  101. skb_queue_tail(&skbtxq, skb);
  102. spin_unlock_irqrestore(&txlock, flags);
  103. wake_up(&txwq);
  104. }
  105. }
  106. /*
  107. * (1) len doesn't include the header by default. I want this.
  108. */
  109. static int
  110. aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
  111. {
  112. struct aoe_hdr *h;
  113. struct aoe_atahdr *ah;
  114. u32 n;
  115. int sn;
  116. if (dev_net(ifp) != &init_net)
  117. goto exit;
  118. skb = skb_share_check(skb, GFP_ATOMIC);
  119. if (skb == NULL)
  120. return 0;
  121. if (!is_aoe_netif(ifp))
  122. goto exit;
  123. skb_push(skb, ETH_HLEN); /* (1) */
  124. sn = sizeof(*h) + sizeof(*ah);
  125. if (skb->len >= sn) {
  126. sn -= skb_headlen(skb);
  127. if (sn > 0 && !__pskb_pull_tail(skb, sn))
  128. goto exit;
  129. }
  130. h = (struct aoe_hdr *) skb->data;
  131. n = get_unaligned_be32(&h->tag);
  132. if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
  133. goto exit;
  134. if (h->verfl & AOEFL_ERR) {
  135. n = h->err;
  136. if (n > NECODES)
  137. n = 0;
  138. if (net_ratelimit())
  139. printk(KERN_ERR
  140. "%s%d.%d@%s; ecode=%d '%s'\n",
  141. "aoe: error packet from ",
  142. get_unaligned_be16(&h->major),
  143. h->minor, skb->dev->name,
  144. h->err, aoe_errlist[n]);
  145. goto exit;
  146. }
  147. switch (h->cmd) {
  148. case AOECMD_ATA:
  149. /* ata_rsp may keep skb for later processing or give it back */
  150. skb = aoecmd_ata_rsp(skb);
  151. break;
  152. case AOECMD_CFG:
  153. aoecmd_cfg_rsp(skb);
  154. break;
  155. default:
  156. if (h->cmd >= AOECMD_VEND_MIN)
  157. break; /* don't complain about vendor commands */
  158. pr_info("aoe: unknown AoE command type 0x%02x\n", h->cmd);
  159. break;
  160. }
  161. if (!skb)
  162. return 0;
  163. exit:
  164. dev_kfree_skb(skb);
  165. return 0;
  166. }
  167. static struct packet_type aoe_pt __read_mostly = {
  168. .type = __constant_htons(ETH_P_AOE),
  169. .func = aoenet_rcv,
  170. };
  171. int __init
  172. aoenet_init(void)
  173. {
  174. skb_queue_head_init(&skbtxq);
  175. init_waitqueue_head(&txwq);
  176. spin_lock_init(&txlock);
  177. kts.lock = &txlock;
  178. kts.fn = tx;
  179. kts.waitq = &txwq;
  180. kts.id = 0;
  181. snprintf(kts.name, sizeof(kts.name), "aoe_tx%d", kts.id);
  182. if (aoe_ktstart(&kts))
  183. return -EAGAIN;
  184. dev_add_pack(&aoe_pt);
  185. return 0;
  186. }
  187. void
  188. aoenet_exit(void)
  189. {
  190. aoe_ktstop(&kts);
  191. skb_queue_purge(&skbtxq);
  192. dev_remove_pack(&aoe_pt);
  193. }