l2t.c 12 KB

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