l2t.c 12 KB

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