clip_tbl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. refcount_inc(&ce->refcnt);
  86. return 0;
  87. }
  88. }
  89. read_unlock_bh(&ctbl->lock);
  90. write_lock_bh(&ctbl->lock);
  91. if (!list_empty(&ctbl->ce_free_head)) {
  92. ce = list_first_entry(&ctbl->ce_free_head,
  93. struct clip_entry, list);
  94. list_del(&ce->list);
  95. INIT_LIST_HEAD(&ce->list);
  96. spin_lock_init(&ce->lock);
  97. refcount_set(&ce->refcnt, 0);
  98. atomic_dec(&ctbl->nfree);
  99. list_add_tail(&ce->list, &ctbl->hash_list[hash]);
  100. if (v6) {
  101. ce->addr6.sin6_family = AF_INET6;
  102. memcpy(ce->addr6.sin6_addr.s6_addr,
  103. lip, sizeof(struct in6_addr));
  104. ret = clip6_get_mbox(dev, (const struct in6_addr *)lip);
  105. if (ret) {
  106. write_unlock_bh(&ctbl->lock);
  107. dev_err(adap->pdev_dev,
  108. "CLIP FW cmd failed with error %d, "
  109. "Connections using %pI6c wont be "
  110. "offloaded",
  111. ret, ce->addr6.sin6_addr.s6_addr);
  112. return ret;
  113. }
  114. } else {
  115. ce->addr.sin_family = AF_INET;
  116. memcpy((char *)(&ce->addr.sin_addr), lip,
  117. sizeof(struct in_addr));
  118. }
  119. } else {
  120. write_unlock_bh(&ctbl->lock);
  121. dev_info(adap->pdev_dev, "CLIP table overflow, "
  122. "Connections using %pI6c wont be offloaded",
  123. (void *)lip);
  124. return -ENOMEM;
  125. }
  126. write_unlock_bh(&ctbl->lock);
  127. refcount_set(&ce->refcnt, 1);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(cxgb4_clip_get);
  131. void cxgb4_clip_release(const struct net_device *dev, const u32 *lip, u8 v6)
  132. {
  133. struct adapter *adap = netdev2adap(dev);
  134. struct clip_tbl *ctbl = adap->clipt;
  135. struct clip_entry *ce, *cte;
  136. u32 *addr = (u32 *)lip;
  137. int hash;
  138. int ret = -1;
  139. if (!ctbl)
  140. return;
  141. hash = clip_addr_hash(ctbl, addr, v6);
  142. read_lock_bh(&ctbl->lock);
  143. list_for_each_entry(cte, &ctbl->hash_list[hash], list) {
  144. if (cte->addr6.sin6_family == AF_INET6 && v6)
  145. ret = memcmp(lip, cte->addr6.sin6_addr.s6_addr,
  146. sizeof(struct in6_addr));
  147. else if (cte->addr.sin_family == AF_INET && !v6)
  148. ret = memcmp(lip, (char *)(&cte->addr.sin_addr),
  149. sizeof(struct in_addr));
  150. if (!ret) {
  151. ce = cte;
  152. read_unlock_bh(&ctbl->lock);
  153. goto found;
  154. }
  155. }
  156. read_unlock_bh(&ctbl->lock);
  157. return;
  158. found:
  159. write_lock_bh(&ctbl->lock);
  160. spin_lock_bh(&ce->lock);
  161. if (refcount_dec_and_test(&ce->refcnt)) {
  162. list_del(&ce->list);
  163. INIT_LIST_HEAD(&ce->list);
  164. list_add_tail(&ce->list, &ctbl->ce_free_head);
  165. atomic_inc(&ctbl->nfree);
  166. if (v6)
  167. clip6_release_mbox(dev, (const struct in6_addr *)lip);
  168. }
  169. spin_unlock_bh(&ce->lock);
  170. write_unlock_bh(&ctbl->lock);
  171. }
  172. EXPORT_SYMBOL(cxgb4_clip_release);
  173. /* Retrieves IPv6 addresses from a root device (bond, vlan) associated with
  174. * a physical device.
  175. * The physical device reference is needed to send the actul CLIP command.
  176. */
  177. static int cxgb4_update_dev_clip(struct net_device *root_dev,
  178. struct net_device *dev)
  179. {
  180. struct inet6_dev *idev = NULL;
  181. struct inet6_ifaddr *ifa;
  182. int ret = 0;
  183. idev = __in6_dev_get(root_dev);
  184. if (!idev)
  185. return ret;
  186. read_lock_bh(&idev->lock);
  187. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  188. ret = cxgb4_clip_get(dev, (const u32 *)ifa->addr.s6_addr, 1);
  189. if (ret < 0)
  190. break;
  191. }
  192. read_unlock_bh(&idev->lock);
  193. return ret;
  194. }
  195. int cxgb4_update_root_dev_clip(struct net_device *dev)
  196. {
  197. struct net_device *root_dev = NULL;
  198. int i, ret = 0;
  199. /* First populate the real net device's IPv6 addresses */
  200. ret = cxgb4_update_dev_clip(dev, dev);
  201. if (ret)
  202. return ret;
  203. /* Parse all bond and vlan devices layered on top of the physical dev */
  204. root_dev = netdev_master_upper_dev_get_rcu(dev);
  205. if (root_dev) {
  206. ret = cxgb4_update_dev_clip(root_dev, dev);
  207. if (ret)
  208. return ret;
  209. }
  210. for (i = 0; i < VLAN_N_VID; i++) {
  211. root_dev = __vlan_find_dev_deep_rcu(dev, htons(ETH_P_8021Q), i);
  212. if (!root_dev)
  213. continue;
  214. ret = cxgb4_update_dev_clip(root_dev, dev);
  215. if (ret)
  216. break;
  217. }
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(cxgb4_update_root_dev_clip);
  221. int clip_tbl_show(struct seq_file *seq, void *v)
  222. {
  223. struct adapter *adapter = seq->private;
  224. struct clip_tbl *ctbl = adapter->clipt;
  225. struct clip_entry *ce;
  226. char ip[60];
  227. int i;
  228. read_lock_bh(&ctbl->lock);
  229. seq_puts(seq, "IP Address Users\n");
  230. for (i = 0 ; i < ctbl->clipt_size; ++i) {
  231. list_for_each_entry(ce, &ctbl->hash_list[i], list) {
  232. ip[0] = '\0';
  233. sprintf(ip, "%pISc", &ce->addr);
  234. seq_printf(seq, "%-25s %u\n", ip,
  235. refcount_read(&ce->refcnt));
  236. }
  237. }
  238. seq_printf(seq, "Free clip entries : %d\n", atomic_read(&ctbl->nfree));
  239. read_unlock_bh(&ctbl->lock);
  240. return 0;
  241. }
  242. struct clip_tbl *t4_init_clip_tbl(unsigned int clipt_start,
  243. unsigned int clipt_end)
  244. {
  245. struct clip_entry *cl_list;
  246. struct clip_tbl *ctbl;
  247. unsigned int clipt_size;
  248. int i;
  249. if (clipt_start >= clipt_end)
  250. return NULL;
  251. clipt_size = clipt_end - clipt_start + 1;
  252. if (clipt_size < CLIPT_MIN_HASH_BUCKETS)
  253. return NULL;
  254. ctbl = kvzalloc(sizeof(*ctbl) +
  255. clipt_size*sizeof(struct list_head), GFP_KERNEL);
  256. if (!ctbl)
  257. return NULL;
  258. ctbl->clipt_start = clipt_start;
  259. ctbl->clipt_size = clipt_size;
  260. INIT_LIST_HEAD(&ctbl->ce_free_head);
  261. atomic_set(&ctbl->nfree, clipt_size);
  262. rwlock_init(&ctbl->lock);
  263. for (i = 0; i < ctbl->clipt_size; ++i)
  264. INIT_LIST_HEAD(&ctbl->hash_list[i]);
  265. cl_list = kvcalloc(clipt_size, sizeof(struct clip_entry), GFP_KERNEL);
  266. if (!cl_list) {
  267. kvfree(ctbl);
  268. return NULL;
  269. }
  270. ctbl->cl_list = (void *)cl_list;
  271. for (i = 0; i < clipt_size; i++) {
  272. INIT_LIST_HEAD(&cl_list[i].list);
  273. list_add_tail(&cl_list[i].list, &ctbl->ce_free_head);
  274. }
  275. return ctbl;
  276. }
  277. void t4_cleanup_clip_tbl(struct adapter *adap)
  278. {
  279. struct clip_tbl *ctbl = adap->clipt;
  280. if (ctbl) {
  281. if (ctbl->cl_list)
  282. kvfree(ctbl->cl_list);
  283. kvfree(ctbl);
  284. }
  285. }
  286. EXPORT_SYMBOL(t4_cleanup_clip_tbl);