l2t.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/skbuff.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/jhash.h>
  37. #include <linux/slab.h>
  38. #include <net/neighbour.h>
  39. #include "common.h"
  40. #include "t3cdev.h"
  41. #include "cxgb3_defs.h"
  42. #include "l2t.h"
  43. #include "t3_cpl.h"
  44. #include "firmware_exports.h"
  45. #define VLAN_NONE 0xfff
  46. /*
  47. * Module locking notes: There is a RW lock protecting the L2 table as a
  48. * whole plus a spinlock per L2T entry. Entry lookups and allocations happen
  49. * under the protection of the table lock, individual entry changes happen
  50. * while holding that entry's spinlock. The table lock nests outside the
  51. * entry locks. Allocations of new entries take the table lock as writers so
  52. * no other lookups can happen while allocating new entries. Entry updates
  53. * take the table lock as readers so multiple entries can be updated in
  54. * parallel. An L2T entry can be dropped by decrementing its reference count
  55. * and therefore can happen in parallel with entry allocation but no entry
  56. * can change state or increment its ref count during allocation as both of
  57. * these perform lookups.
  58. */
  59. static inline unsigned int vlan_prio(const struct l2t_entry *e)
  60. {
  61. return e->vlan >> 13;
  62. }
  63. static inline unsigned int arp_hash(u32 key, int ifindex,
  64. const struct l2t_data *d)
  65. {
  66. return jhash_2words(key, ifindex, 0) & (d->nentries - 1);
  67. }
  68. static inline void neigh_replace(struct l2t_entry *e, struct neighbour *n)
  69. {
  70. neigh_hold(n);
  71. if (e->neigh)
  72. neigh_release(e->neigh);
  73. e->neigh = n;
  74. }
  75. /*
  76. * Set up an L2T entry and send any packets waiting in the arp queue. The
  77. * supplied skb is used for the CPL_L2T_WRITE_REQ. Must be called with the
  78. * entry locked.
  79. */
  80. static int setup_l2e_send_pending(struct t3cdev *dev, struct sk_buff *skb,
  81. struct l2t_entry *e)
  82. {
  83. struct cpl_l2t_write_req *req;
  84. struct sk_buff *tmp;
  85. if (!skb) {
  86. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  87. if (!skb)
  88. return -ENOMEM;
  89. }
  90. req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
  91. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  92. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx));
  93. req->params = htonl(V_L2T_W_IDX(e->idx) | V_L2T_W_IFF(e->smt_idx) |
  94. V_L2T_W_VLAN(e->vlan & VLAN_VID_MASK) |
  95. V_L2T_W_PRIO(vlan_prio(e)));
  96. memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
  97. memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
  98. skb->priority = CPL_PRIORITY_CONTROL;
  99. cxgb3_ofld_send(dev, skb);
  100. skb_queue_walk_safe(&e->arpq, skb, tmp) {
  101. __skb_unlink(skb, &e->arpq);
  102. cxgb3_ofld_send(dev, skb);
  103. }
  104. e->state = L2T_STATE_VALID;
  105. return 0;
  106. }
  107. /*
  108. * Add a packet to the an L2T entry's queue of packets awaiting resolution.
  109. * Must be called with the entry's lock held.
  110. */
  111. static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
  112. {
  113. __skb_queue_tail(&e->arpq, skb);
  114. }
  115. int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
  116. struct l2t_entry *e)
  117. {
  118. again:
  119. switch (e->state) {
  120. case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
  121. neigh_event_send(e->neigh, NULL);
  122. spin_lock_bh(&e->lock);
  123. if (e->state == L2T_STATE_STALE)
  124. e->state = L2T_STATE_VALID;
  125. spin_unlock_bh(&e->lock);
  126. case L2T_STATE_VALID: /* fast-path, send the packet on */
  127. return cxgb3_ofld_send(dev, skb);
  128. case L2T_STATE_RESOLVING:
  129. spin_lock_bh(&e->lock);
  130. if (e->state != L2T_STATE_RESOLVING) {
  131. /* ARP already completed */
  132. spin_unlock_bh(&e->lock);
  133. goto again;
  134. }
  135. arpq_enqueue(e, skb);
  136. spin_unlock_bh(&e->lock);
  137. /*
  138. * Only the first packet added to the arpq should kick off
  139. * resolution. However, because the alloc_skb below can fail,
  140. * we allow each packet added to the arpq to retry resolution
  141. * as a way of recovering from transient memory exhaustion.
  142. * A better way would be to use a work request to retry L2T
  143. * entries when there's no memory.
  144. */
  145. if (!neigh_event_send(e->neigh, NULL)) {
  146. skb = alloc_skb(sizeof(struct cpl_l2t_write_req),
  147. GFP_ATOMIC);
  148. if (!skb)
  149. break;
  150. spin_lock_bh(&e->lock);
  151. if (!skb_queue_empty(&e->arpq))
  152. setup_l2e_send_pending(dev, skb, e);
  153. else /* we lost the race */
  154. __kfree_skb(skb);
  155. spin_unlock_bh(&e->lock);
  156. }
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(t3_l2t_send_slow);
  161. void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e)
  162. {
  163. again:
  164. switch (e->state) {
  165. case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
  166. neigh_event_send(e->neigh, NULL);
  167. spin_lock_bh(&e->lock);
  168. if (e->state == L2T_STATE_STALE) {
  169. e->state = L2T_STATE_VALID;
  170. }
  171. spin_unlock_bh(&e->lock);
  172. return;
  173. case L2T_STATE_VALID: /* fast-path, send the packet on */
  174. return;
  175. case L2T_STATE_RESOLVING:
  176. spin_lock_bh(&e->lock);
  177. if (e->state != L2T_STATE_RESOLVING) {
  178. /* ARP already completed */
  179. spin_unlock_bh(&e->lock);
  180. goto again;
  181. }
  182. spin_unlock_bh(&e->lock);
  183. /*
  184. * Only the first packet added to the arpq should kick off
  185. * resolution. However, because the alloc_skb below can fail,
  186. * we allow each packet added to the arpq to retry resolution
  187. * as a way of recovering from transient memory exhaustion.
  188. * A better way would be to use a work request to retry L2T
  189. * entries when there's no memory.
  190. */
  191. neigh_event_send(e->neigh, NULL);
  192. }
  193. return;
  194. }
  195. EXPORT_SYMBOL(t3_l2t_send_event);
  196. /*
  197. * Allocate a free L2T entry. Must be called with l2t_data.lock held.
  198. */
  199. static struct l2t_entry *alloc_l2e(struct l2t_data *d)
  200. {
  201. struct l2t_entry *end, *e, **p;
  202. if (!atomic_read(&d->nfree))
  203. return NULL;
  204. /* there's definitely a free entry */
  205. for (e = d->rover, end = &d->l2tab[d->nentries]; e != end; ++e)
  206. if (atomic_read(&e->refcnt) == 0)
  207. goto found;
  208. for (e = &d->l2tab[1]; atomic_read(&e->refcnt); ++e) ;
  209. found:
  210. d->rover = e + 1;
  211. atomic_dec(&d->nfree);
  212. /*
  213. * The entry we found may be an inactive entry that is
  214. * presently in the hash table. We need to remove it.
  215. */
  216. if (e->state != L2T_STATE_UNUSED) {
  217. int hash = arp_hash(e->addr, e->ifindex, d);
  218. for (p = &d->l2tab[hash].first; *p; p = &(*p)->next)
  219. if (*p == e) {
  220. *p = e->next;
  221. break;
  222. }
  223. e->state = L2T_STATE_UNUSED;
  224. }
  225. return e;
  226. }
  227. /*
  228. * Called when an L2T entry has no more users. The entry is left in the hash
  229. * table since it is likely to be reused but we also bump nfree to indicate
  230. * that the entry can be reallocated for a different neighbor. We also drop
  231. * the existing neighbor reference in case the neighbor is going away and is
  232. * waiting on our reference.
  233. *
  234. * Because entries can be reallocated to other neighbors once their ref count
  235. * drops to 0 we need to take the entry's lock to avoid races with a new
  236. * incarnation.
  237. */
  238. void t3_l2e_free(struct l2t_data *d, struct l2t_entry *e)
  239. {
  240. spin_lock_bh(&e->lock);
  241. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  242. if (e->neigh) {
  243. neigh_release(e->neigh);
  244. e->neigh = NULL;
  245. }
  246. }
  247. spin_unlock_bh(&e->lock);
  248. atomic_inc(&d->nfree);
  249. }
  250. EXPORT_SYMBOL(t3_l2e_free);
  251. /*
  252. * Update an L2T entry that was previously used for the same next hop as neigh.
  253. * Must be called with softirqs disabled.
  254. */
  255. static inline void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
  256. {
  257. unsigned int nud_state;
  258. spin_lock(&e->lock); /* avoid race with t3_l2t_free */
  259. if (neigh != e->neigh)
  260. neigh_replace(e, neigh);
  261. nud_state = neigh->nud_state;
  262. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
  263. !(nud_state & NUD_VALID))
  264. e->state = L2T_STATE_RESOLVING;
  265. else if (nud_state & NUD_CONNECTED)
  266. e->state = L2T_STATE_VALID;
  267. else
  268. e->state = L2T_STATE_STALE;
  269. spin_unlock(&e->lock);
  270. }
  271. struct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct neighbour *neigh,
  272. struct net_device *dev)
  273. {
  274. struct l2t_entry *e;
  275. struct l2t_data *d = L2DATA(cdev);
  276. u32 addr = *(u32 *) neigh->primary_key;
  277. int ifidx = neigh->dev->ifindex;
  278. int hash = arp_hash(addr, ifidx, d);
  279. struct port_info *p = netdev_priv(dev);
  280. int smt_idx = p->port_id;
  281. write_lock_bh(&d->lock);
  282. for (e = d->l2tab[hash].first; e; e = e->next)
  283. if (e->addr == addr && e->ifindex == ifidx &&
  284. e->smt_idx == smt_idx) {
  285. l2t_hold(d, e);
  286. if (atomic_read(&e->refcnt) == 1)
  287. reuse_entry(e, neigh);
  288. goto done;
  289. }
  290. /* Need to allocate a new entry */
  291. e = alloc_l2e(d);
  292. if (e) {
  293. spin_lock(&e->lock); /* avoid race with t3_l2t_free */
  294. e->next = d->l2tab[hash].first;
  295. d->l2tab[hash].first = e;
  296. e->state = L2T_STATE_RESOLVING;
  297. e->addr = addr;
  298. e->ifindex = ifidx;
  299. e->smt_idx = smt_idx;
  300. atomic_set(&e->refcnt, 1);
  301. neigh_replace(e, neigh);
  302. if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
  303. e->vlan = vlan_dev_vlan_id(neigh->dev);
  304. else
  305. e->vlan = VLAN_NONE;
  306. spin_unlock(&e->lock);
  307. }
  308. done:
  309. write_unlock_bh(&d->lock);
  310. return e;
  311. }
  312. EXPORT_SYMBOL(t3_l2t_get);
  313. /*
  314. * Called when address resolution fails for an L2T entry to handle packets
  315. * on the arpq head. If a packet specifies a failure handler it is invoked,
  316. * otherwise the packets is sent to the offload device.
  317. *
  318. * XXX: maybe we should abandon the latter behavior and just require a failure
  319. * handler.
  320. */
  321. static void handle_failed_resolution(struct t3cdev *dev, struct sk_buff_head *arpq)
  322. {
  323. struct sk_buff *skb, *tmp;
  324. skb_queue_walk_safe(arpq, skb, tmp) {
  325. struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
  326. __skb_unlink(skb, arpq);
  327. if (cb->arp_failure_handler)
  328. cb->arp_failure_handler(dev, skb);
  329. else
  330. cxgb3_ofld_send(dev, skb);
  331. }
  332. }
  333. /*
  334. * Called when the host's ARP layer makes a change to some entry that is
  335. * loaded into the HW L2 table.
  336. */
  337. void t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh)
  338. {
  339. struct sk_buff_head arpq;
  340. struct l2t_entry *e;
  341. struct l2t_data *d = L2DATA(dev);
  342. u32 addr = *(u32 *) neigh->primary_key;
  343. int ifidx = neigh->dev->ifindex;
  344. int hash = arp_hash(addr, ifidx, d);
  345. read_lock_bh(&d->lock);
  346. for (e = d->l2tab[hash].first; e; e = e->next)
  347. if (e->addr == addr && e->ifindex == ifidx) {
  348. spin_lock(&e->lock);
  349. goto found;
  350. }
  351. read_unlock_bh(&d->lock);
  352. return;
  353. found:
  354. __skb_queue_head_init(&arpq);
  355. read_unlock(&d->lock);
  356. if (atomic_read(&e->refcnt)) {
  357. if (neigh != e->neigh)
  358. neigh_replace(e, neigh);
  359. if (e->state == L2T_STATE_RESOLVING) {
  360. if (neigh->nud_state & NUD_FAILED) {
  361. skb_queue_splice_init(&e->arpq, &arpq);
  362. } else if (neigh->nud_state & (NUD_CONNECTED|NUD_STALE))
  363. setup_l2e_send_pending(dev, NULL, e);
  364. } else {
  365. e->state = neigh->nud_state & NUD_CONNECTED ?
  366. L2T_STATE_VALID : L2T_STATE_STALE;
  367. if (memcmp(e->dmac, neigh->ha, 6))
  368. setup_l2e_send_pending(dev, NULL, e);
  369. }
  370. }
  371. spin_unlock_bh(&e->lock);
  372. if (!skb_queue_empty(&arpq))
  373. handle_failed_resolution(dev, &arpq);
  374. }
  375. struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
  376. {
  377. struct l2t_data *d;
  378. int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
  379. d = cxgb_alloc_mem(size);
  380. if (!d)
  381. return NULL;
  382. d->nentries = l2t_capacity;
  383. d->rover = &d->l2tab[1]; /* entry 0 is not used */
  384. atomic_set(&d->nfree, l2t_capacity - 1);
  385. rwlock_init(&d->lock);
  386. for (i = 0; i < l2t_capacity; ++i) {
  387. d->l2tab[i].idx = i;
  388. d->l2tab[i].state = L2T_STATE_UNUSED;
  389. __skb_queue_head_init(&d->l2tab[i].arpq);
  390. spin_lock_init(&d->l2tab[i].lock);
  391. atomic_set(&d->l2tab[i].refcnt, 0);
  392. }
  393. return d;
  394. }
  395. void t3_free_l2t(struct l2t_data *d)
  396. {
  397. cxgb_free_mem(d);
  398. }