aoenet.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #ifndef MODULE
  31. static int __init aoe_iflist_setup(char *str)
  32. {
  33. strncpy(aoe_iflist, str, IFLISTSZ);
  34. aoe_iflist[IFLISTSZ - 1] = '\0';
  35. return 1;
  36. }
  37. __setup("aoe_iflist=", aoe_iflist_setup);
  38. #endif
  39. int
  40. is_aoe_netif(struct net_device *ifp)
  41. {
  42. register char *p, *q;
  43. register int len;
  44. if (aoe_iflist[0] == '\0')
  45. return 1;
  46. p = aoe_iflist + strspn(aoe_iflist, WHITESPACE);
  47. for (; *p; p = q + strspn(q, WHITESPACE)) {
  48. q = p + strcspn(p, WHITESPACE);
  49. if (q != p)
  50. len = q - p;
  51. else
  52. len = strlen(p); /* last token in aoe_iflist */
  53. if (strlen(ifp->name) == len && !strncmp(ifp->name, p, len))
  54. return 1;
  55. if (q == p)
  56. break;
  57. }
  58. return 0;
  59. }
  60. int
  61. set_aoe_iflist(const char __user *user_str, size_t size)
  62. {
  63. if (size >= IFLISTSZ)
  64. return -EINVAL;
  65. if (copy_from_user(aoe_iflist, user_str, size)) {
  66. printk(KERN_INFO "aoe: copy from user failed\n");
  67. return -EFAULT;
  68. }
  69. aoe_iflist[size] = 0x00;
  70. return 0;
  71. }
  72. void
  73. aoenet_xmit(struct sk_buff_head *queue)
  74. {
  75. struct sk_buff *skb, *tmp;
  76. skb_queue_walk_safe(queue, skb, tmp) {
  77. __skb_unlink(skb, queue);
  78. dev_queue_xmit(skb);
  79. }
  80. }
  81. /*
  82. * (1) len doesn't include the header by default. I want this.
  83. */
  84. static int
  85. aoenet_rcv(struct sk_buff *skb, struct net_device *ifp, struct packet_type *pt, struct net_device *orig_dev)
  86. {
  87. struct aoe_hdr *h;
  88. u32 n;
  89. if (dev_net(ifp) != &init_net)
  90. goto exit;
  91. skb = skb_share_check(skb, GFP_ATOMIC);
  92. if (skb == NULL)
  93. return 0;
  94. if (skb_linearize(skb))
  95. goto exit;
  96. if (!is_aoe_netif(ifp))
  97. goto exit;
  98. skb_push(skb, ETH_HLEN); /* (1) */
  99. h = (struct aoe_hdr *) skb_mac_header(skb);
  100. n = get_unaligned_be32(&h->tag);
  101. if ((h->verfl & AOEFL_RSP) == 0 || (n & 1<<31))
  102. goto exit;
  103. if (h->verfl & AOEFL_ERR) {
  104. n = h->err;
  105. if (n > NECODES)
  106. n = 0;
  107. if (net_ratelimit())
  108. printk(KERN_ERR
  109. "%s%d.%d@%s; ecode=%d '%s'\n",
  110. "aoe: error packet from ",
  111. get_unaligned_be16(&h->major),
  112. h->minor, skb->dev->name,
  113. h->err, aoe_errlist[n]);
  114. goto exit;
  115. }
  116. switch (h->cmd) {
  117. case AOECMD_ATA:
  118. aoecmd_ata_rsp(skb);
  119. break;
  120. case AOECMD_CFG:
  121. aoecmd_cfg_rsp(skb);
  122. break;
  123. default:
  124. if (h->cmd >= AOECMD_VEND_MIN)
  125. break; /* don't complain about vendor commands */
  126. printk(KERN_INFO "aoe: unknown cmd %d\n", h->cmd);
  127. }
  128. exit:
  129. dev_kfree_skb(skb);
  130. return 0;
  131. }
  132. static struct packet_type aoe_pt __read_mostly = {
  133. .type = __constant_htons(ETH_P_AOE),
  134. .func = aoenet_rcv,
  135. };
  136. int __init
  137. aoenet_init(void)
  138. {
  139. dev_add_pack(&aoe_pt);
  140. return 0;
  141. }
  142. void
  143. aoenet_exit(void)
  144. {
  145. dev_remove_pack(&aoe_pt);
  146. }