l2t.c 13 KB

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