clip_tbl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. * Copyright (C) 2003-2014 Chelsio Communications. All rights reserved.
  4. *
  5. * Written by Deepak (deepak.s@chelsio.com)
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. * FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file included in this
  10. * release for licensing terms and conditions.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/jhash.h>
  15. #include <linux/if_vlan.h>
  16. #include <net/addrconf.h>
  17. #include "cxgb4.h"
  18. #include "clip_tbl.h"
  19. static inline unsigned int ipv4_clip_hash(struct clip_tbl *c, const u32 *key)
  20. {
  21. unsigned int clipt_size_half = c->clipt_size / 2;
  22. return jhash_1word(*key, 0) % clipt_size_half;
  23. }
  24. static inline unsigned int ipv6_clip_hash(struct clip_tbl *d, const u32 *key)
  25. {
  26. unsigned int clipt_size_half = d->clipt_size / 2;
  27. u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
  28. return clipt_size_half +
  29. (jhash_1word(xor, 0) % clipt_size_half);
  30. }
  31. static unsigned int clip_addr_hash(struct clip_tbl *ctbl, const u32 *addr,
  32. u8 v6)
  33. {
  34. return v6 ? ipv6_clip_hash(ctbl, addr) :
  35. ipv4_clip_hash(ctbl, addr);
  36. }
  37. static int clip6_get_mbox(const struct net_device *dev,
  38. const struct in6_addr *lip)
  39. {
  40. struct adapter *adap = netdev2adap(dev);
  41. struct fw_clip_cmd c;
  42. memset(&c, 0, sizeof(c));
  43. c.op_to_write = htonl(FW_CMD_OP_V(FW_CLIP_CMD) |
  44. FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
  45. c.alloc_to_len16 = htonl(FW_CLIP_CMD_ALLOC_F | FW_LEN16(c));
  46. *(__be64 *)&c.ip_hi = *(__be64 *)(lip->s6_addr);
  47. *(__be64 *)&c.ip_lo = *(__be64 *)(lip->s6_addr + 8);
  48. return t4_wr_mbox_meat(adap, adap->mbox, &c, sizeof(c), &c, false);
  49. }
  50. static int clip6_release_mbox(const struct net_device *dev,
  51. const struct in6_addr *lip)
  52. {
  53. struct adapter *adap = netdev2adap(dev);
  54. struct fw_clip_cmd c;
  55. memset(&c, 0, sizeof(c));
  56. c.op_to_write = htonl(FW_CMD_OP_V(FW_CLIP_CMD) |
  57. FW_CMD_REQUEST_F | FW_CMD_READ_F);
  58. c.alloc_to_len16 = htonl(FW_CLIP_CMD_FREE_F | FW_LEN16(c));
  59. *(__be64 *)&c.ip_hi = *(__be64 *)(lip->s6_addr);
  60. *(__be64 *)&c.ip_lo = *(__be64 *)(lip->s6_addr + 8);
  61. return t4_wr_mbox_meat(adap, adap->mbox, &c, sizeof(c), &c, false);
  62. }
  63. int cxgb4_clip_get(const struct net_device *dev, const u32 *lip, u8 v6)
  64. {
  65. struct adapter *adap = netdev2adap(dev);
  66. struct clip_tbl *ctbl = adap->clipt;
  67. struct clip_entry *ce, *cte;
  68. u32 *addr = (u32 *)lip;
  69. int hash;
  70. int ret = -1;
  71. if (!ctbl)
  72. return 0;
  73. hash = clip_addr_hash(ctbl, addr, v6);
  74. read_lock_bh(&ctbl->lock);
  75. list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
  76. if (cte->addr6.sin6_family == AF_INET6 && v6)
  77. ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
  78. sizeof(struct in6_addr));
  79. else if (cte->addr.sin_family == AF_INET && !v6)
  80. ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
  81. sizeof(struct in_addr));
  82. if (!ret) {
  83. ce = cte;
  84. read_unlock_bh(&ctbl->lock);
  85. goto found;
  86. }
  87. }
  88. read_unlock_bh(&ctbl->lock);
  89. write_lock_bh(&ctbl->lock);
  90. if (!list_empty(&ctbl->ce_free_head)) {
  91. ce = list_first_entry(&ctbl->ce_free_head,
  92. struct clip_entry, list);
  93. list_del(&ce->list);
  94. INIT_LIST_HEAD(&ce->list);
  95. spin_lock_init(&ce->lock);
  96. atomic_set(&ce->refcnt, 0);
  97. atomic_dec(&ctbl->nfree);
  98. list_add_tail(&ce->list, &ctbl->hash_list[hash]);
  99. if (v6) {
  100. ce->addr6.sin6_family = AF_INET6;
  101. memcpy(ce->addr6.sin6_addr.s6_addr,
  102. lip, sizeof(struct in6_addr));
  103. ret = clip6_get_mbox(dev, (const struct in6_addr *)lip);
  104. if (ret) {
  105. write_unlock_bh(&ctbl->lock);
  106. return ret;
  107. }
  108. } else {
  109. ce->addr.sin_family = AF_INET;
  110. memcpy((char *)(&ce->addr.sin_addr), lip,
  111. sizeof(struct in_addr));
  112. }
  113. } else {
  114. write_unlock_bh(&ctbl->lock);
  115. return -ENOMEM;
  116. }
  117. write_unlock_bh(&ctbl->lock);
  118. found:
  119. atomic_inc(&ce->refcnt);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(cxgb4_clip_get);
  123. void cxgb4_clip_release(const struct net_device *dev, const u32 *lip, u8 v6)
  124. {
  125. struct adapter *adap = netdev2adap(dev);
  126. struct clip_tbl *ctbl = adap->clipt;
  127. struct clip_entry *ce, *cte;
  128. u32 *addr = (u32 *)lip;
  129. int hash;
  130. int ret = -1;
  131. hash = clip_addr_hash(ctbl, addr, v6);
  132. read_lock_bh(&ctbl->lock);
  133. list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
  134. if (cte->addr6.sin6_family == AF_INET6 && v6)
  135. ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
  136. sizeof(struct in6_addr));
  137. else if (cte->addr.sin_family == AF_INET && !v6)
  138. ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
  139. sizeof(struct in_addr));
  140. if (!ret) {
  141. ce = cte;
  142. read_unlock_bh(&ctbl->lock);
  143. goto found;
  144. }
  145. }
  146. read_unlock_bh(&ctbl->lock);
  147. return;
  148. found:
  149. write_lock_bh(&ctbl->lock);
  150. spin_lock_bh(&ce->lock);
  151. if (atomic_dec_and_test(&ce->refcnt)) {
  152. list_del(&ce->list);
  153. INIT_LIST_HEAD(&ce->list);
  154. list_add_tail(&ce->list, &ctbl->ce_free_head);
  155. atomic_inc(&ctbl->nfree);
  156. if (v6)
  157. clip6_release_mbox(dev, (const struct in6_addr *)lip);
  158. }
  159. spin_unlock_bh(&ce->lock);
  160. write_unlock_bh(&ctbl->lock);
  161. }
  162. EXPORT_SYMBOL(cxgb4_clip_release);
  163. /* Retrieves IPv6 addresses from a root device (bond, vlan) associated with
  164. * a physical device.
  165. * The physical device reference is needed to send the actul CLIP command.
  166. */
  167. static int cxgb4_update_dev_clip(struct net_device *root_dev,
  168. struct net_device *dev)
  169. {
  170. struct inet6_dev *idev = NULL;
  171. struct inet6_ifaddr *ifa;
  172. int ret = 0;
  173. idev = __in6_dev_get(root_dev);
  174. if (!idev)
  175. return ret;
  176. read_lock_bh(&idev->lock);
  177. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  178. ret = cxgb4_clip_get(dev, (const u32 *)ifa->addr.s6_addr, 1);
  179. if (ret < 0)
  180. break;
  181. }
  182. read_unlock_bh(&idev->lock);
  183. return ret;
  184. }
  185. int cxgb4_update_root_dev_clip(struct net_device *dev)
  186. {
  187. struct net_device *root_dev = NULL;
  188. int i, ret = 0;
  189. /* First populate the real net device's IPv6 addresses */
  190. ret = cxgb4_update_dev_clip(dev, dev);
  191. if (ret)
  192. return ret;
  193. /* Parse all bond and vlan devices layered on top of the physical dev */
  194. root_dev = netdev_master_upper_dev_get_rcu(dev);
  195. if (root_dev) {
  196. ret = cxgb4_update_dev_clip(root_dev, dev);
  197. if (ret)
  198. return ret;
  199. }
  200. for (i = 0; i < VLAN_N_VID; i++) {
  201. root_dev = __vlan_find_dev_deep_rcu(dev, htons(ETH_P_8021Q), i);
  202. if (!root_dev)
  203. continue;
  204. ret = cxgb4_update_dev_clip(root_dev, dev);
  205. if (ret)
  206. break;
  207. }
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(cxgb4_update_root_dev_clip);
  211. int clip_tbl_show(struct seq_file *seq, void *v)
  212. {
  213. struct adapter *adapter = seq->private;
  214. struct clip_tbl *ctbl = adapter->clipt;
  215. struct clip_entry *ce;
  216. char ip[60];
  217. int i;
  218. read_lock_bh(&ctbl->lock);
  219. seq_puts(seq, "IP Address Users\n");
  220. for (i = 0 ; i < ctbl->clipt_size; ++i) {
  221. list_for_each_entry(ce, &ctbl->hash_list[i], list) {
  222. ip[0] = '\0';
  223. sprintf(ip, "%pISc", &ce->addr);
  224. seq_printf(seq, "%-25s %u\n", ip,
  225. atomic_read(&ce->refcnt));
  226. }
  227. }
  228. seq_printf(seq, "Free clip entries : %d\n", atomic_read(&ctbl->nfree));
  229. read_unlock_bh(&ctbl->lock);
  230. return 0;
  231. }
  232. struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
  233. unsigned int clipt_end)
  234. {
  235. struct clip_entry *cl_list;
  236. struct clip_tbl *ctbl;
  237. unsigned int clipt_size;
  238. int i;
  239. if (clipt_start >= clipt_end)
  240. return NULL;
  241. clipt_size = clipt_end - clipt_start + 1;
  242. if (clipt_size < CLIPT_MIN_HASH_BUCKETS)
  243. return NULL;
  244. ctbl = t4_alloc_mem(sizeof(*ctbl) +
  245. clipt_size*sizeof(struct list_head));
  246. if (!ctbl)
  247. return NULL;
  248. ctbl->clipt_start = clipt_start;
  249. ctbl->clipt_size = clipt_size;
  250. INIT_LIST_HEAD(&ctbl->ce_free_head);
  251. atomic_set(&ctbl->nfree, clipt_size);
  252. rwlock_init(&ctbl->lock);
  253. for (i = 0; i < ctbl->clipt_size; ++i)
  254. INIT_LIST_HEAD(&ctbl->hash_list[i]);
  255. cl_list = t4_alloc_mem(clipt_size*sizeof(struct clip_entry));
  256. if (!cl_list) {
  257. t4_free_mem(ctbl);
  258. return NULL;
  259. }
  260. ctbl->cl_list = (void *)cl_list;
  261. for (i = 0; i < clipt_size; i++) {
  262. INIT_LIST_HEAD(&cl_list[i].list);
  263. list_add_tail(&cl_list[i].list, &ctbl->ce_free_head);
  264. }
  265. return ctbl;
  266. }
  267. void t4_cleanup_clip_tbl(struct adapter *adap)
  268. {
  269. struct clip_tbl *ctbl = adap->clipt;
  270. if (ctbl) {
  271. if (ctbl->cl_list)
  272. t4_free_mem(ctbl->cl_list);
  273. t4_free_mem(ctbl);
  274. }
  275. }
  276. EXPORT_SYMBOL(t4_cleanup_clip_tbl);