l2t.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/if.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/jhash.h>
  39. #include <net/neighbour.h>
  40. #include "cxgb4.h"
  41. #include "l2t.h"
  42. #include "t4_msg.h"
  43. #include "t4fw_api.h"
  44. #define VLAN_NONE 0xfff
  45. /* identifies sync vs async L2T_WRITE_REQs */
  46. #define F_SYNC_WR (1 << 12)
  47. enum {
  48. L2T_STATE_VALID, /* entry is up to date */
  49. L2T_STATE_STALE, /* entry may be used but needs revalidation */
  50. L2T_STATE_RESOLVING, /* entry needs address resolution */
  51. L2T_STATE_SYNC_WRITE, /* synchronous write of entry underway */
  52. /* when state is one of the below the entry is not hashed */
  53. L2T_STATE_SWITCHING, /* entry is being used by a switching filter */
  54. L2T_STATE_UNUSED /* entry not in use */
  55. };
  56. struct l2t_data {
  57. rwlock_t lock;
  58. atomic_t nfree; /* number of free entries */
  59. struct l2t_entry *rover; /* starting point for next allocation */
  60. struct l2t_entry l2tab[L2T_SIZE];
  61. };
  62. static inline unsigned int vlan_prio(const struct l2t_entry *e)
  63. {
  64. return e->vlan >> 13;
  65. }
  66. static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
  67. {
  68. if (atomic_add_return(1, &e->refcnt) == 1) /* 0 -> 1 transition */
  69. atomic_dec(&d->nfree);
  70. }
  71. /*
  72. * To avoid having to check address families we do not allow v4 and v6
  73. * neighbors to be on the same hash chain. We keep v4 entries in the first
  74. * half of available hash buckets and v6 in the second.
  75. */
  76. enum {
  77. L2T_SZ_HALF = L2T_SIZE / 2,
  78. L2T_HASH_MASK = L2T_SZ_HALF - 1
  79. };
  80. static inline unsigned int arp_hash(const u32 *key, int ifindex)
  81. {
  82. return jhash_2words(*key, ifindex, 0) & L2T_HASH_MASK;
  83. }
  84. static inline unsigned int ipv6_hash(const u32 *key, int ifindex)
  85. {
  86. u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
  87. return L2T_SZ_HALF + (jhash_2words(xor, ifindex, 0) & L2T_HASH_MASK);
  88. }
  89. static unsigned int addr_hash(const u32 *addr, int addr_len, int ifindex)
  90. {
  91. return addr_len == 4 ? arp_hash(addr, ifindex) :
  92. ipv6_hash(addr, ifindex);
  93. }
  94. /*
  95. * Checks if an L2T entry is for the given IP/IPv6 address. It does not check
  96. * whether the L2T entry and the address are of the same address family.
  97. * Callers ensure an address is only checked against L2T entries of the same
  98. * family, something made trivial by the separation of IP and IPv6 hash chains
  99. * mentioned above. Returns 0 if there's a match,
  100. */
  101. static int addreq(const struct l2t_entry *e, const u32 *addr)
  102. {
  103. if (e->v6)
  104. return (e->addr[0] ^ addr[0]) | (e->addr[1] ^ addr[1]) |
  105. (e->addr[2] ^ addr[2]) | (e->addr[3] ^ addr[3]);
  106. return e->addr[0] ^ addr[0];
  107. }
  108. static void neigh_replace(struct l2t_entry *e, struct neighbour *n)
  109. {
  110. neigh_hold(n);
  111. if (e->neigh)
  112. neigh_release(e->neigh);
  113. e->neigh = n;
  114. }
  115. /*
  116. * Write an L2T entry. Must be called with the entry locked.
  117. * The write may be synchronous or asynchronous.
  118. */
  119. static int write_l2e(struct adapter *adap, struct l2t_entry *e, int sync)
  120. {
  121. struct sk_buff *skb;
  122. struct cpl_l2t_write_req *req;
  123. skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
  124. if (!skb)
  125. return -ENOMEM;
  126. req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
  127. INIT_TP_WR(req, 0);
  128. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ,
  129. e->idx | (sync ? F_SYNC_WR : 0) |
  130. TID_QID(adap->sge.fw_evtq.abs_id)));
  131. req->params = htons(L2T_W_PORT(e->lport) | L2T_W_NOREPLY(!sync));
  132. req->l2t_idx = htons(e->idx);
  133. req->vlan = htons(e->vlan);
  134. if (e->neigh)
  135. memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
  136. memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
  137. set_wr_txq(skb, CPL_PRIORITY_CONTROL, 0);
  138. t4_ofld_send(adap, skb);
  139. if (sync && e->state != L2T_STATE_SWITCHING)
  140. e->state = L2T_STATE_SYNC_WRITE;
  141. return 0;
  142. }
  143. /*
  144. * Send packets waiting in an L2T entry's ARP queue. Must be called with the
  145. * entry locked.
  146. */
  147. static void send_pending(struct adapter *adap, struct l2t_entry *e)
  148. {
  149. while (e->arpq_head) {
  150. struct sk_buff *skb = e->arpq_head;
  151. e->arpq_head = skb->next;
  152. skb->next = NULL;
  153. t4_ofld_send(adap, skb);
  154. }
  155. e->arpq_tail = NULL;
  156. }
  157. /*
  158. * Process a CPL_L2T_WRITE_RPL. Wake up the ARP queue if it completes a
  159. * synchronous L2T_WRITE. Note that the TID in the reply is really the L2T
  160. * index it refers to.
  161. */
  162. void do_l2t_write_rpl(struct adapter *adap, const struct cpl_l2t_write_rpl *rpl)
  163. {
  164. unsigned int tid = GET_TID(rpl);
  165. unsigned int idx = tid & (L2T_SIZE - 1);
  166. if (unlikely(rpl->status != CPL_ERR_NONE)) {
  167. dev_err(adap->pdev_dev,
  168. "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
  169. rpl->status, idx);
  170. return;
  171. }
  172. if (tid & F_SYNC_WR) {
  173. struct l2t_entry *e = &adap->l2t->l2tab[idx];
  174. spin_lock(&e->lock);
  175. if (e->state != L2T_STATE_SWITCHING) {
  176. send_pending(adap, e);
  177. e->state = (e->neigh->nud_state & NUD_STALE) ?
  178. L2T_STATE_STALE : L2T_STATE_VALID;
  179. }
  180. spin_unlock(&e->lock);
  181. }
  182. }
  183. /*
  184. * Add a packet to an L2T entry's queue of packets awaiting resolution.
  185. * Must be called with the entry's lock held.
  186. */
  187. static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
  188. {
  189. skb->next = NULL;
  190. if (e->arpq_head)
  191. e->arpq_tail->next = skb;
  192. else
  193. e->arpq_head = skb;
  194. e->arpq_tail = skb;
  195. }
  196. int cxgb4_l2t_send(struct net_device *dev, struct sk_buff *skb,
  197. struct l2t_entry *e)
  198. {
  199. struct adapter *adap = netdev2adap(dev);
  200. again:
  201. switch (e->state) {
  202. case L2T_STATE_STALE: /* entry is stale, kick off revalidation */
  203. neigh_event_send(e->neigh, NULL);
  204. spin_lock_bh(&e->lock);
  205. if (e->state == L2T_STATE_STALE)
  206. e->state = L2T_STATE_VALID;
  207. spin_unlock_bh(&e->lock);
  208. case L2T_STATE_VALID: /* fast-path, send the packet on */
  209. return t4_ofld_send(adap, skb);
  210. case L2T_STATE_RESOLVING:
  211. case L2T_STATE_SYNC_WRITE:
  212. spin_lock_bh(&e->lock);
  213. if (e->state != L2T_STATE_SYNC_WRITE &&
  214. e->state != L2T_STATE_RESOLVING) {
  215. spin_unlock_bh(&e->lock);
  216. goto again;
  217. }
  218. arpq_enqueue(e, skb);
  219. spin_unlock_bh(&e->lock);
  220. if (e->state == L2T_STATE_RESOLVING &&
  221. !neigh_event_send(e->neigh, NULL)) {
  222. spin_lock_bh(&e->lock);
  223. if (e->state == L2T_STATE_RESOLVING && e->arpq_head)
  224. write_l2e(adap, e, 1);
  225. spin_unlock_bh(&e->lock);
  226. }
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(cxgb4_l2t_send);
  231. /*
  232. * Allocate a free L2T entry. Must be called with l2t_data.lock held.
  233. */
  234. static struct l2t_entry *alloc_l2e(struct l2t_data *d)
  235. {
  236. struct l2t_entry *end, *e, **p;
  237. if (!atomic_read(&d->nfree))
  238. return NULL;
  239. /* there's definitely a free entry */
  240. for (e = d->rover, end = &d->l2tab[L2T_SIZE]; e != end; ++e)
  241. if (atomic_read(&e->refcnt) == 0)
  242. goto found;
  243. for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
  244. ;
  245. found:
  246. d->rover = e + 1;
  247. atomic_dec(&d->nfree);
  248. /*
  249. * The entry we found may be an inactive entry that is
  250. * presently in the hash table. We need to remove it.
  251. */
  252. if (e->state < L2T_STATE_SWITCHING)
  253. for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
  254. if (*p == e) {
  255. *p = e->next;
  256. e->next = NULL;
  257. break;
  258. }
  259. e->state = L2T_STATE_UNUSED;
  260. return e;
  261. }
  262. /*
  263. * Called when an L2T entry has no more users.
  264. */
  265. static void t4_l2e_free(struct l2t_entry *e)
  266. {
  267. struct l2t_data *d;
  268. spin_lock_bh(&e->lock);
  269. if (atomic_read(&e->refcnt) == 0) { /* hasn't been recycled */
  270. if (e->neigh) {
  271. neigh_release(e->neigh);
  272. e->neigh = NULL;
  273. }
  274. while (e->arpq_head) {
  275. struct sk_buff *skb = e->arpq_head;
  276. e->arpq_head = skb->next;
  277. kfree_skb(skb);
  278. }
  279. e->arpq_tail = NULL;
  280. }
  281. spin_unlock_bh(&e->lock);
  282. d = container_of(e, struct l2t_data, l2tab[e->idx]);
  283. atomic_inc(&d->nfree);
  284. }
  285. void cxgb4_l2t_release(struct l2t_entry *e)
  286. {
  287. if (atomic_dec_and_test(&e->refcnt))
  288. t4_l2e_free(e);
  289. }
  290. EXPORT_SYMBOL(cxgb4_l2t_release);
  291. /*
  292. * Update an L2T entry that was previously used for the same next hop as neigh.
  293. * Must be called with softirqs disabled.
  294. */
  295. static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
  296. {
  297. unsigned int nud_state;
  298. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  299. if (neigh != e->neigh)
  300. neigh_replace(e, neigh);
  301. nud_state = neigh->nud_state;
  302. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
  303. !(nud_state & NUD_VALID))
  304. e->state = L2T_STATE_RESOLVING;
  305. else if (nud_state & NUD_CONNECTED)
  306. e->state = L2T_STATE_VALID;
  307. else
  308. e->state = L2T_STATE_STALE;
  309. spin_unlock(&e->lock);
  310. }
  311. struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
  312. const struct net_device *physdev,
  313. unsigned int priority)
  314. {
  315. u8 lport;
  316. u16 vlan;
  317. struct l2t_entry *e;
  318. int addr_len = neigh->tbl->key_len;
  319. u32 *addr = (u32 *)neigh->primary_key;
  320. int ifidx = neigh->dev->ifindex;
  321. int hash = addr_hash(addr, addr_len, ifidx);
  322. if (neigh->dev->flags & IFF_LOOPBACK)
  323. lport = netdev2pinfo(physdev)->tx_chan + 4;
  324. else
  325. lport = netdev2pinfo(physdev)->lport;
  326. if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
  327. vlan = vlan_dev_vlan_id(neigh->dev);
  328. else
  329. vlan = VLAN_NONE;
  330. write_lock_bh(&d->lock);
  331. for (e = d->l2tab[hash].first; e; e = e->next)
  332. if (!addreq(e, addr) && e->ifindex == ifidx &&
  333. e->vlan == vlan && e->lport == lport) {
  334. l2t_hold(d, e);
  335. if (atomic_read(&e->refcnt) == 1)
  336. reuse_entry(e, neigh);
  337. goto done;
  338. }
  339. /* Need to allocate a new entry */
  340. e = alloc_l2e(d);
  341. if (e) {
  342. spin_lock(&e->lock); /* avoid race with t4_l2t_free */
  343. e->state = L2T_STATE_RESOLVING;
  344. memcpy(e->addr, addr, addr_len);
  345. e->ifindex = ifidx;
  346. e->hash = hash;
  347. e->lport = lport;
  348. e->v6 = addr_len == 16;
  349. atomic_set(&e->refcnt, 1);
  350. neigh_replace(e, neigh);
  351. e->vlan = vlan;
  352. e->next = d->l2tab[hash].first;
  353. d->l2tab[hash].first = e;
  354. spin_unlock(&e->lock);
  355. }
  356. done:
  357. write_unlock_bh(&d->lock);
  358. return e;
  359. }
  360. EXPORT_SYMBOL(cxgb4_l2t_get);
  361. /*
  362. * Called when address resolution fails for an L2T entry to handle packets
  363. * on the arpq head. If a packet specifies a failure handler it is invoked,
  364. * otherwise the packet is sent to the device.
  365. */
  366. static void handle_failed_resolution(struct adapter *adap, struct sk_buff *arpq)
  367. {
  368. while (arpq) {
  369. struct sk_buff *skb = arpq;
  370. const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
  371. arpq = skb->next;
  372. skb->next = NULL;
  373. if (cb->arp_err_handler)
  374. cb->arp_err_handler(cb->handle, skb);
  375. else
  376. t4_ofld_send(adap, skb);
  377. }
  378. }
  379. /*
  380. * Called when the host's neighbor layer makes a change to some entry that is
  381. * loaded into the HW L2 table.
  382. */
  383. void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
  384. {
  385. struct l2t_entry *e;
  386. struct sk_buff *arpq = NULL;
  387. struct l2t_data *d = adap->l2t;
  388. int addr_len = neigh->tbl->key_len;
  389. u32 *addr = (u32 *) neigh->primary_key;
  390. int ifidx = neigh->dev->ifindex;
  391. int hash = addr_hash(addr, addr_len, ifidx);
  392. read_lock_bh(&d->lock);
  393. for (e = d->l2tab[hash].first; e; e = e->next)
  394. if (!addreq(e, addr) && e->ifindex == ifidx) {
  395. spin_lock(&e->lock);
  396. if (atomic_read(&e->refcnt))
  397. goto found;
  398. spin_unlock(&e->lock);
  399. break;
  400. }
  401. read_unlock_bh(&d->lock);
  402. return;
  403. found:
  404. read_unlock(&d->lock);
  405. if (neigh != e->neigh)
  406. neigh_replace(e, neigh);
  407. if (e->state == L2T_STATE_RESOLVING) {
  408. if (neigh->nud_state & NUD_FAILED) {
  409. arpq = e->arpq_head;
  410. e->arpq_head = e->arpq_tail = NULL;
  411. } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
  412. e->arpq_head) {
  413. write_l2e(adap, e, 1);
  414. }
  415. } else {
  416. e->state = neigh->nud_state & NUD_CONNECTED ?
  417. L2T_STATE_VALID : L2T_STATE_STALE;
  418. if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
  419. write_l2e(adap, e, 0);
  420. }
  421. spin_unlock_bh(&e->lock);
  422. if (arpq)
  423. handle_failed_resolution(adap, arpq);
  424. }
  425. struct l2t_data *t4_init_l2t(void)
  426. {
  427. int i;
  428. struct l2t_data *d;
  429. d = t4_alloc_mem(sizeof(*d));
  430. if (!d)
  431. return NULL;
  432. d->rover = d->l2tab;
  433. atomic_set(&d->nfree, L2T_SIZE);
  434. rwlock_init(&d->lock);
  435. for (i = 0; i < L2T_SIZE; ++i) {
  436. d->l2tab[i].idx = i;
  437. d->l2tab[i].state = L2T_STATE_UNUSED;
  438. spin_lock_init(&d->l2tab[i].lock);
  439. atomic_set(&d->l2tab[i].refcnt, 0);
  440. }
  441. return d;
  442. }
  443. #include <linux/module.h>
  444. #include <linux/debugfs.h>
  445. #include <linux/seq_file.h>
  446. static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
  447. {
  448. struct l2t_entry *l2tab = seq->private;
  449. return pos >= L2T_SIZE ? NULL : &l2tab[pos];
  450. }
  451. static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
  452. {
  453. return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  454. }
  455. static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  456. {
  457. v = l2t_get_idx(seq, *pos);
  458. if (v)
  459. ++*pos;
  460. return v;
  461. }
  462. static void l2t_seq_stop(struct seq_file *seq, void *v)
  463. {
  464. }
  465. static char l2e_state(const struct l2t_entry *e)
  466. {
  467. switch (e->state) {
  468. case L2T_STATE_VALID: return 'V';
  469. case L2T_STATE_STALE: return 'S';
  470. case L2T_STATE_SYNC_WRITE: return 'W';
  471. case L2T_STATE_RESOLVING: return e->arpq_head ? 'A' : 'R';
  472. case L2T_STATE_SWITCHING: return 'X';
  473. default:
  474. return 'U';
  475. }
  476. }
  477. static int l2t_seq_show(struct seq_file *seq, void *v)
  478. {
  479. if (v == SEQ_START_TOKEN)
  480. seq_puts(seq, " Idx IP address "
  481. "Ethernet address VLAN/P LP State Users Port\n");
  482. else {
  483. char ip[60];
  484. struct l2t_entry *e = v;
  485. spin_lock_bh(&e->lock);
  486. if (e->state == L2T_STATE_SWITCHING)
  487. ip[0] = '\0';
  488. else
  489. sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
  490. seq_printf(seq, "%4u %-25s %17pM %4d %u %2u %c %5u %s\n",
  491. e->idx, ip, e->dmac,
  492. e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
  493. l2e_state(e), atomic_read(&e->refcnt),
  494. e->neigh ? e->neigh->dev->name : "");
  495. spin_unlock_bh(&e->lock);
  496. }
  497. return 0;
  498. }
  499. static const struct seq_operations l2t_seq_ops = {
  500. .start = l2t_seq_start,
  501. .next = l2t_seq_next,
  502. .stop = l2t_seq_stop,
  503. .show = l2t_seq_show
  504. };
  505. static int l2t_seq_open(struct inode *inode, struct file *file)
  506. {
  507. int rc = seq_open(file, &l2t_seq_ops);
  508. if (!rc) {
  509. struct adapter *adap = inode->i_private;
  510. struct seq_file *seq = file->private_data;
  511. seq->private = adap->l2t->l2tab;
  512. }
  513. return rc;
  514. }
  515. const struct file_operations t4_l2t_fops = {
  516. .owner = THIS_MODULE,
  517. .open = l2t_seq_open,
  518. .read = seq_read,
  519. .llseek = seq_lseek,
  520. .release = seq_release,
  521. };