This source file includes following definitions.
- vlan_prio
- arp_hash
- neigh_replace
- setup_l2e_send_pending
- arpq_enqueue
- t3_l2t_send_slow
- t3_l2t_send_event
- alloc_l2e
- t3_l2e_free
- reuse_entry
- t3_l2t_get
- handle_failed_resolution
- t3_l2t_update
- t3_init_l2t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
47 #define VLAN_NONE 0xfff
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 static inline unsigned int vlan_prio(const struct l2t_entry *e)
64 {
65 return e->vlan >> 13;
66 }
67
68 static inline unsigned int arp_hash(u32 key, int ifindex,
69 const struct l2t_data *d)
70 {
71 return jhash_2words(key, ifindex, 0) & (d->nentries - 1);
72 }
73
74 static inline void neigh_replace(struct l2t_entry *e, struct neighbour *n)
75 {
76 neigh_hold(n);
77 if (e->neigh)
78 neigh_release(e->neigh);
79 e->neigh = n;
80 }
81
82
83
84
85
86
87 static int setup_l2e_send_pending(struct t3cdev *dev, struct sk_buff *skb,
88 struct l2t_entry *e)
89 {
90 struct cpl_l2t_write_req *req;
91 struct sk_buff *tmp;
92
93 if (!skb) {
94 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
95 if (!skb)
96 return -ENOMEM;
97 }
98
99 req = __skb_put(skb, sizeof(*req));
100 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
101 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx));
102 req->params = htonl(V_L2T_W_IDX(e->idx) | V_L2T_W_IFF(e->smt_idx) |
103 V_L2T_W_VLAN(e->vlan & VLAN_VID_MASK) |
104 V_L2T_W_PRIO(vlan_prio(e)));
105 memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
106 memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
107 skb->priority = CPL_PRIORITY_CONTROL;
108 cxgb3_ofld_send(dev, skb);
109
110 skb_queue_walk_safe(&e->arpq, skb, tmp) {
111 __skb_unlink(skb, &e->arpq);
112 cxgb3_ofld_send(dev, skb);
113 }
114 e->state = L2T_STATE_VALID;
115
116 return 0;
117 }
118
119
120
121
122
123 static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
124 {
125 __skb_queue_tail(&e->arpq, skb);
126 }
127
128 int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
129 struct l2t_entry *e)
130 {
131 again:
132 switch (e->state) {
133 case L2T_STATE_STALE:
134 neigh_event_send(e->neigh, NULL);
135 spin_lock_bh(&e->lock);
136 if (e->state == L2T_STATE_STALE)
137 e->state = L2T_STATE_VALID;
138 spin_unlock_bh(&e->lock);
139
140 case L2T_STATE_VALID:
141 return cxgb3_ofld_send(dev, skb);
142 case L2T_STATE_RESOLVING:
143 spin_lock_bh(&e->lock);
144 if (e->state != L2T_STATE_RESOLVING) {
145
146 spin_unlock_bh(&e->lock);
147 goto again;
148 }
149 arpq_enqueue(e, skb);
150 spin_unlock_bh(&e->lock);
151
152
153
154
155
156
157
158
159
160 if (!neigh_event_send(e->neigh, NULL)) {
161 skb = alloc_skb(sizeof(struct cpl_l2t_write_req),
162 GFP_ATOMIC);
163 if (!skb)
164 break;
165
166 spin_lock_bh(&e->lock);
167 if (!skb_queue_empty(&e->arpq))
168 setup_l2e_send_pending(dev, skb, e);
169 else
170 __kfree_skb(skb);
171 spin_unlock_bh(&e->lock);
172 }
173 }
174 return 0;
175 }
176
177 EXPORT_SYMBOL(t3_l2t_send_slow);
178
179 void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e)
180 {
181 again:
182 switch (e->state) {
183 case L2T_STATE_STALE:
184 neigh_event_send(e->neigh, NULL);
185 spin_lock_bh(&e->lock);
186 if (e->state == L2T_STATE_STALE) {
187 e->state = L2T_STATE_VALID;
188 }
189 spin_unlock_bh(&e->lock);
190 return;
191 case L2T_STATE_VALID:
192 return;
193 case L2T_STATE_RESOLVING:
194 spin_lock_bh(&e->lock);
195 if (e->state != L2T_STATE_RESOLVING) {
196
197 spin_unlock_bh(&e->lock);
198 goto again;
199 }
200 spin_unlock_bh(&e->lock);
201
202
203
204
205
206
207
208
209
210 neigh_event_send(e->neigh, NULL);
211 }
212 }
213
214 EXPORT_SYMBOL(t3_l2t_send_event);
215
216
217
218
219 static struct l2t_entry *alloc_l2e(struct l2t_data *d)
220 {
221 struct l2t_entry *end, *e, **p;
222
223 if (!atomic_read(&d->nfree))
224 return NULL;
225
226
227 for (e = d->rover, end = &d->l2tab[d->nentries]; e != end; ++e)
228 if (atomic_read(&e->refcnt) == 0)
229 goto found;
230
231 for (e = &d->l2tab[1]; atomic_read(&e->refcnt); ++e) ;
232 found:
233 d->rover = e + 1;
234 atomic_dec(&d->nfree);
235
236
237
238
239
240 if (e->state != L2T_STATE_UNUSED) {
241 int hash = arp_hash(e->addr, e->ifindex, d);
242
243 for (p = &d->l2tab[hash].first; *p; p = &(*p)->next)
244 if (*p == e) {
245 *p = e->next;
246 break;
247 }
248 e->state = L2T_STATE_UNUSED;
249 }
250 return e;
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264 void t3_l2e_free(struct l2t_data *d, struct l2t_entry *e)
265 {
266 spin_lock_bh(&e->lock);
267 if (atomic_read(&e->refcnt) == 0) {
268 if (e->neigh) {
269 neigh_release(e->neigh);
270 e->neigh = NULL;
271 }
272 }
273 spin_unlock_bh(&e->lock);
274 atomic_inc(&d->nfree);
275 }
276
277 EXPORT_SYMBOL(t3_l2e_free);
278
279
280
281
282
283 static inline void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
284 {
285 unsigned int nud_state;
286
287 spin_lock(&e->lock);
288
289 if (neigh != e->neigh)
290 neigh_replace(e, neigh);
291 nud_state = neigh->nud_state;
292 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
293 !(nud_state & NUD_VALID))
294 e->state = L2T_STATE_RESOLVING;
295 else if (nud_state & NUD_CONNECTED)
296 e->state = L2T_STATE_VALID;
297 else
298 e->state = L2T_STATE_STALE;
299 spin_unlock(&e->lock);
300 }
301
302 struct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct dst_entry *dst,
303 struct net_device *dev, const void *daddr)
304 {
305 struct l2t_entry *e = NULL;
306 struct neighbour *neigh;
307 struct port_info *p;
308 struct l2t_data *d;
309 int hash;
310 u32 addr;
311 int ifidx;
312 int smt_idx;
313
314 rcu_read_lock();
315 neigh = dst_neigh_lookup(dst, daddr);
316 if (!neigh)
317 goto done_rcu;
318
319 addr = *(u32 *) neigh->primary_key;
320 ifidx = neigh->dev->ifindex;
321
322 if (!dev)
323 dev = neigh->dev;
324 p = netdev_priv(dev);
325 smt_idx = p->port_id;
326
327 d = L2DATA(cdev);
328 if (!d)
329 goto done_rcu;
330
331 hash = arp_hash(addr, ifidx, d);
332
333 write_lock_bh(&d->lock);
334 for (e = d->l2tab[hash].first; e; e = e->next)
335 if (e->addr == addr && e->ifindex == ifidx &&
336 e->smt_idx == smt_idx) {
337 l2t_hold(d, e);
338 if (atomic_read(&e->refcnt) == 1)
339 reuse_entry(e, neigh);
340 goto done_unlock;
341 }
342
343
344 e = alloc_l2e(d);
345 if (e) {
346 spin_lock(&e->lock);
347 e->next = d->l2tab[hash].first;
348 d->l2tab[hash].first = e;
349 e->state = L2T_STATE_RESOLVING;
350 e->addr = addr;
351 e->ifindex = ifidx;
352 e->smt_idx = smt_idx;
353 atomic_set(&e->refcnt, 1);
354 neigh_replace(e, neigh);
355 if (is_vlan_dev(neigh->dev))
356 e->vlan = vlan_dev_vlan_id(neigh->dev);
357 else
358 e->vlan = VLAN_NONE;
359 spin_unlock(&e->lock);
360 }
361 done_unlock:
362 write_unlock_bh(&d->lock);
363 done_rcu:
364 if (neigh)
365 neigh_release(neigh);
366 rcu_read_unlock();
367 return e;
368 }
369
370 EXPORT_SYMBOL(t3_l2t_get);
371
372
373
374
375
376
377
378
379
380 static void handle_failed_resolution(struct t3cdev *dev, struct sk_buff_head *arpq)
381 {
382 struct sk_buff *skb, *tmp;
383
384 skb_queue_walk_safe(arpq, skb, tmp) {
385 struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
386
387 __skb_unlink(skb, arpq);
388 if (cb->arp_failure_handler)
389 cb->arp_failure_handler(dev, skb);
390 else
391 cxgb3_ofld_send(dev, skb);
392 }
393 }
394
395
396
397
398
399 void t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh)
400 {
401 struct sk_buff_head arpq;
402 struct l2t_entry *e;
403 struct l2t_data *d = L2DATA(dev);
404 u32 addr = *(u32 *) neigh->primary_key;
405 int ifidx = neigh->dev->ifindex;
406 int hash = arp_hash(addr, ifidx, d);
407
408 read_lock_bh(&d->lock);
409 for (e = d->l2tab[hash].first; e; e = e->next)
410 if (e->addr == addr && e->ifindex == ifidx) {
411 spin_lock(&e->lock);
412 goto found;
413 }
414 read_unlock_bh(&d->lock);
415 return;
416
417 found:
418 __skb_queue_head_init(&arpq);
419
420 read_unlock(&d->lock);
421 if (atomic_read(&e->refcnt)) {
422 if (neigh != e->neigh)
423 neigh_replace(e, neigh);
424
425 if (e->state == L2T_STATE_RESOLVING) {
426 if (neigh->nud_state & NUD_FAILED) {
427 skb_queue_splice_init(&e->arpq, &arpq);
428 } else if (neigh->nud_state & (NUD_CONNECTED|NUD_STALE))
429 setup_l2e_send_pending(dev, NULL, e);
430 } else {
431 e->state = neigh->nud_state & NUD_CONNECTED ?
432 L2T_STATE_VALID : L2T_STATE_STALE;
433 if (!ether_addr_equal(e->dmac, neigh->ha))
434 setup_l2e_send_pending(dev, NULL, e);
435 }
436 }
437 spin_unlock_bh(&e->lock);
438
439 if (!skb_queue_empty(&arpq))
440 handle_failed_resolution(dev, &arpq);
441 }
442
443 struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
444 {
445 struct l2t_data *d;
446 int i;
447
448 d = kvzalloc(struct_size(d, l2tab, l2t_capacity), GFP_KERNEL);
449 if (!d)
450 return NULL;
451
452 d->nentries = l2t_capacity;
453 d->rover = &d->l2tab[1];
454 atomic_set(&d->nfree, l2t_capacity - 1);
455 rwlock_init(&d->lock);
456
457 for (i = 0; i < l2t_capacity; ++i) {
458 d->l2tab[i].idx = i;
459 d->l2tab[i].state = L2T_STATE_UNUSED;
460 __skb_queue_head_init(&d->l2tab[i].arpq);
461 spin_lock_init(&d->l2tab[i].lock);
462 atomic_set(&d->l2tab[i].refcnt, 0);
463 }
464 return d;
465 }