This repository was archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnetwork.c
More file actions
956 lines (809 loc) · 23 KB
/
network.c
File metadata and controls
956 lines (809 loc) · 23 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "ggaoed.h"
#include <atomic_ops.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <linux/filter.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
/* These were added in kernel 2.6.31 */
#ifndef PACKET_TX_RING
#define PACKET_TX_RING 13
#define PACKET_LOSS 14
#define TP_STATUS_AVAILABLE 0x0
#define TP_STATUS_SEND_REQUEST 0x1
#define TP_STATUS_SENDING 0x2
#define TP_STATUS_WRONG_FORMAT 0x4
#endif /* PACKET_TX_RING */
/**********************************************************************
* Global variables
*/
/* List of all interfaces we currently listen on */
GPtrArray *ifaces;
GQueue active_ifaces;
static void net_io(uint32_t events, void *data);
static void destroy_one_ring(struct netif *iface, int what);
/**********************************************************************
* Generic functions
*/
static void free_iface(struct netif *iface)
{
unsigned i;
for (i = 0; i < iface->deferred->len; i++)
drop_request(g_ptr_array_index(iface->deferred, i));
if (iface->deferred->len)
g_ptr_array_remove_range(iface->deferred, 0, iface->deferred->len);
if (iface->fd >= 0)
{
if (iface->ring_ptr)
{
munmap(iface->ring_ptr, iface->ring_len);
destroy_one_ring(iface, PACKET_RX_RING);
destroy_one_ring(iface, PACKET_TX_RING);
}
del_fd(iface->fd);
close(iface->fd);
}
if (iface->devices->len)
netlog(iface, LOG_ERR, "Being destroyed but devices are still attached");
g_ptr_array_free(iface->devices, TRUE);
g_ptr_array_free(iface->deferred, TRUE);
g_free(iface->name);
g_slice_free(struct netif, iface);
}
static struct netif *alloc_iface(int ifindex, const char *name)
{
struct netif *iface;
iface = g_slice_new0(struct netif);
iface->ifindex = ifindex;
iface->fd = -1;
iface->name = g_strdup(name);
iface->event_ctx.callback = net_io;
iface->event_ctx.data = iface;
iface->devices = g_ptr_array_new();
iface->deferred = g_ptr_array_new();
iface->chain.data = iface;
if (!get_netif_config(name, &iface->cfg))
{
free_iface(iface);
return NULL;
}
return iface;
}
/* Process a packet received from the network */
static void process_packet(struct netif *iface, void *packet, unsigned len,
const struct timespec *tv)
{
const struct aoe_hdr *hdr = packet;
unsigned i, l, u, shelf, slot;
struct device *dev;
iface->stats.rx_bytes += len;
++iface->stats.rx_cnt;
/* Check protocol */
if (G_UNLIKELY(hdr->addr.ether_type != htons(ETH_P_AOE)))
{
iface->stats.ignored++;
return;
}
/* Check protocol version */
if (G_UNLIKELY(hdr->version != AOE_VERSION))
{
iface->stats.ignored++;
return;
}
/* Ignore responses */
if (hdr->is_response)
{
iface->stats.ignored++;
return;
}
shelf = hdr->shelf;
slot = hdr->slot;
/* Broadcast requests: do a linear scan */
if (G_UNLIKELY(shelf == htons(SHELF_BCAST) || slot == SLOT_BCAST))
{
int processed = FALSE;
for (i = 0; i < iface->devices->len; i++)
{
dev = g_ptr_array_index(iface->devices, i);
if ((shelf == htons(SHELF_BCAST) || dev->cfg.shelf == shelf) &&
(slot == SLOT_BCAST || dev->cfg.slot == slot))
{
process_request(iface, dev, packet, len, tv);
processed = TRUE;
}
}
if (processed)
iface->stats.broadcast++;
else
iface->stats.ignored++;
return;
}
/* Not a broadcast: do a binary search */
l = 0;
u = iface->devices->len;
while (l < u)
{
i = (l + u) / 2;
dev = g_ptr_array_index(iface->devices, i);
if (dev->cfg.shelf < shelf || (dev->cfg.shelf == shelf &&
dev->cfg.slot < slot))
l = i + 1;
else if (dev->cfg.shelf > shelf || (dev->cfg.shelf == shelf &&
dev->cfg.slot > slot))
u = i;
else
return process_request(iface, dev, packet, len, tv);
}
iface->stats.ignored++;
}
/**********************************************************************
* Memory mapped ring buffer handling
*/
/* Receive packets from the network using a ringbuffer shared with the kernel */
static void rx_ring(struct netif *iface)
{
unsigned cnt, was_drop;
struct tpacket2_hdr *h;
struct timespec tv;
void *data;
was_drop = 0;
for (cnt = 0; cnt < iface->rx_ring.cnt; ++cnt)
{
data = h = iface->rx_ring.frames[iface->rx_ring.idx];
if (!h->tp_status)
break;
if (++iface->rx_ring.idx >= iface->rx_ring.cnt)
iface->rx_ring.idx = 0;
if (G_UNLIKELY(h->tp_snaplen < (int)sizeof(struct aoe_hdr)))
{
netlog(iface, LOG_DEBUG, "Packet too short");
++iface->stats.dropped;
goto next;
}
/* Use the receiving time of the packet as the start time of
* the request */
tv.tv_sec = h->tp_sec;
tv.tv_nsec = h->tp_nsec;
/* The AoE header also contains the ethernet header, so we have
* start from h->tp_mac instead of h->tp_net */
process_packet(iface, data + h->tp_mac, h->tp_snaplen, &tv);
was_drop |= h->tp_status & TP_STATUS_LOSING;
next:
h->tp_status = TP_STATUS_KERNEL;
/* Make sure other CPUs know about the status change */
AO_nop_full();
}
if (cnt >= iface->rx_ring.cnt)
++iface->stats.rx_buffers_full;
if (was_drop)
{
struct tpacket_stats stats;
socklen_t len;
len = sizeof(stats);
if (!getsockopt(iface->fd, SOL_PACKET, PACKET_STATISTICS, &stats, &len))
iface->stats.dropped += stats.tp_drops;
}
++iface->stats.rx_runs;
}
static void tx_ring(struct netif *iface, struct queue_item *q)
{
struct tpacket2_hdr *h;
unsigned cnt;
void *data;
/* This may happen if the MTU changes while requests are
* in flight */
if (G_UNLIKELY(q->hdrlen + q->length > (unsigned)iface->mtu))
{
drop_request(q);
return;
}
for (cnt = 0; cnt < iface->tx_ring.cnt; ++cnt)
{
h = iface->tx_ring.frames[iface->tx_ring.idx++];
if (iface->tx_ring.idx >= iface->tx_ring.cnt)
iface->tx_ring.idx = 0;
if (h->tp_status == TP_STATUS_AVAILABLE ||
h->tp_status == TP_STATUS_WRONG_FORMAT)
break;
}
if (cnt >= iface->tx_ring.cnt)
{
++iface->stats.tx_buffers_full;
g_ptr_array_add(iface->deferred, q);
if (!iface->congested)
{
modify_fd(iface->fd, &iface->event_ctx, EPOLLIN | EPOLLOUT);
iface->congested = TRUE;
}
return;
}
/* Should not happen */
if (G_UNLIKELY(h->tp_status == TP_STATUS_WRONG_FORMAT))
netlog(iface, LOG_ERR, "Bad packet format on send");
/* Fill the frame */
data = (void *)h + iface->tp_hdrlen;
memcpy(data, &q->aoe_hdr, q->hdrlen);
h->tp_len = q->hdrlen;
if (q->length)
{
memcpy(data + q->hdrlen, q->buf, q->length);
h->tp_len += q->length;
}
iface->stats.tx_bytes += h->tp_len;
++iface->stats.tx_cnt;
if (q->dev && G_UNLIKELY(q->dev->cfg.trace_io))
devlog(q->dev, LOG_DEBUG, "%s/%08x: Response sent",
ether_ntoa((struct ether_addr *)&q->aoe_hdr.addr.ether_dhost),
(uint32_t)ntohl(q->aoe_hdr.tag));
drop_request(q);
/* Make sure buffer writes are stable before we update the status */
AO_nop_write();
h->tp_status = TP_STATUS_SEND_REQUEST;
/* Make sure other CPUs know about the status change */
AO_nop_full();
if (!iface->is_active)
{
g_queue_push_tail_link(&active_ifaces, &iface->chain);
iface->is_active = TRUE;
}
}
/* Call send() for interfaces that have packets queued in the ring buffer */
void run_ifaces(void)
{
GList *l;
int ret;
while ((l = g_queue_pop_head_link(&active_ifaces)))
{
struct netif *iface = l->data;
iface->is_active = FALSE;
ret = send(iface->fd, NULL, 0, MSG_DONTWAIT | MSG_NOSIGNAL);
if (ret == -1 && errno != EAGAIN)
neterr(iface, "Async write error");
else
++iface->stats.tx_runs;
}
}
static void setup_one_ring(struct netif *iface, unsigned ring_size, int mtu, int what)
{
unsigned page_size, max_blocks;
struct tpacket_req req;
struct ring *ring;
const char *name;
int ret;
name = what == PACKET_RX_RING ? "RX" : "TX";
ring = what == PACKET_RX_RING ? &iface->rx_ring : &iface->tx_ring;
/* For RX, the frame looks like:
* - struct tpacket2_hdr
* - padding to 16-byte boundary (this is included in iface->tp_hdrlen)
* - padding: 16 - sizeof(struct ether_hdr)
* - raw packet
* - padding to 16-byte boundary
*
* So the raw packet is aligned so that the data part starts on a
* 16-byte boundary, not the packet header. This means we need an extra
* 16 bytes for the frame size.
*
* The TX frame is simpler:
* - struct tpacket2_hdr
* - padding to 16-byte boundary (this is included in iface->tp_hdrlen)
* - raw packet
* - padding to 16-byte boundary */
ring->frame_size = TPACKET_ALIGN(iface->tp_hdrlen) + TPACKET_ALIGN(mtu);
if (what == PACKET_RX_RING)
ring->frame_size += TPACKET_ALIGNMENT;
if (what == PACKET_TX_RING && defaults.tx_ring_bug)
{
unsigned maxsect;
/* Kernel 2.6.31 has a bug and it requires a larger frame size
* than what is in fact used */
maxsect = (mtu - sizeof(struct aoe_ata_hdr)) / 512;
ring->frame_size = TPACKET_ALIGN(sizeof(struct aoe_ata_hdr) +
maxsect * 512 + 576 + 32);
}
req.tp_frame_size = ring->frame_size;
/* The number of blocks is limited by the kernel implementation */
page_size = sysconf(_SC_PAGESIZE);
max_blocks = page_size / sizeof(void *);
/* Start with a large block size and if that fails try to lower it */
req.tp_block_size = 64 * 1024;
ret = -1;
while (req.tp_block_size > req.tp_frame_size && req.tp_block_size >= page_size)
{
req.tp_block_nr = ring_size / req.tp_block_size;
if (req.tp_block_nr > max_blocks)
req.tp_block_nr = max_blocks;
req.tp_frame_nr = (req.tp_block_size / req.tp_frame_size) * req.tp_block_nr;
ret = setsockopt(iface->fd, SOL_PACKET, what, &req, sizeof(req));
if (!ret)
break;
req.tp_block_size >>= 1;
}
if (ret)
{
neterr(iface, "Failed to set up the %s ring buffer", name);
memset(ring, 0, sizeof(*ring));
return;
}
ring->len = req.tp_block_size * req.tp_block_nr;
ring->block_size = req.tp_block_size;
ring->cnt = req.tp_frame_nr;
ring->frames = g_new0(void *, req.tp_frame_nr);
}
static void destroy_one_ring(struct netif *iface, int what)
{
struct tpacket_req req;
struct ring *ring;
ring = what == PACKET_RX_RING ? &iface->rx_ring : &iface->tx_ring;
memset(&req, 0, sizeof(req));
setsockopt(iface->fd, SOL_PACKET, what, &req, sizeof(req));
g_free(ring->frames);
memset(ring, 0, sizeof(*ring));
}
/* Set up pointers to the individual frames */
static void setup_frames(struct ring *ring, void *data)
{
unsigned i, j, cnt, blocks, frames;
/* Number of blocks in the ring */
blocks = ring->len / ring->block_size;
/* Number of frames in a block */
frames = ring->block_size / ring->frame_size;
for (i = cnt = 0; i < blocks; i++)
for (j = 0; j < frames; j++)
ring->frames[cnt++] = data + i * ring->block_size + j * ring->frame_size;
}
/* Allocate and map the shared ring buffer */
static void setup_rings(struct netif *iface, unsigned size, int mtu)
{
const char *unit;
socklen_t len;
int ret, val;
/* The function can be called on MTU change, so destroy the previous ring
* if any */
if (iface->ring_ptr)
{
munmap(iface->ring_ptr, iface->ring_len);
iface->ring_ptr = NULL;
iface->ring_len = 0;
destroy_one_ring(iface, PACKET_RX_RING);
destroy_one_ring(iface, PACKET_TX_RING);
}
if (!size)
return;
/* We want version 2 ring buffers to avoid 64-bit uncleanness */
val = TPACKET_V2;
ret = setsockopt(iface->fd, SOL_PACKET, PACKET_VERSION, &val, sizeof(val));
if (ret)
{
neterr(iface, "Failed to set version 2 ring buffer format");
return;
}
val = TPACKET_V2;
len = sizeof(val);
ret = getsockopt(iface->fd, SOL_PACKET, PACKET_HDRLEN, &val, &len);
if (ret)
{
neterr(iface, "Failed to determine the header length of the ring buffer");
return;
}
iface->tp_hdrlen = TPACKET_ALIGN(val);
/* Drop badly formatted packets */
val = 1;
ret = setsockopt(iface->fd, SOL_PACKET, PACKET_LOSS, &val, sizeof(val));
if (ret)
neterr(iface, "Failed to set packet drop mode");
/* The RX and TX rings share the memory mapped area, so give
* half the requested size to each */
setup_one_ring(iface, size * 1024 / 2, mtu, PACKET_RX_RING);
setup_one_ring(iface, size * 1024 / 2, mtu, PACKET_TX_RING);
/* Both rings must be mapped using a single mmap() call */
iface->ring_len = iface->rx_ring.len + iface->tx_ring.len;
if (!iface->ring_len)
return;
iface->ring_ptr = mmap(NULL, iface->ring_len, PROT_READ | PROT_WRITE,
MAP_SHARED, iface->fd, 0);
if (iface->ring_ptr == MAP_FAILED)
{
neterr(iface, "Failed to mmap the ring buffer");
destroy_one_ring(iface, PACKET_RX_RING);
destroy_one_ring(iface, PACKET_TX_RING);
iface->ring_ptr = NULL;
iface->ring_len = 0;
return;
}
len = 0;
if (iface->rx_ring.len)
{
setup_frames(&iface->rx_ring, iface->ring_ptr);
len = iface->rx_ring.len;
}
if (iface->tx_ring.len)
setup_frames(&iface->tx_ring, iface->ring_ptr + len);
len = human_format(iface->ring_len, &unit);
netlog(iface, LOG_INFO, "Set up %u %s ring buffer (%u RX/%u TX packets)",
len, unit, iface->rx_ring.cnt, iface->tx_ring.cnt);
}
/**********************************************************************
* Traditional single-packet I/O
*/
/* Receive packets from the network using recvfrom() */
static void rx_recvfrom(struct netif *iface)
{
unsigned cnt;
void *packet;
int len;
packet = alloc_packet(iface->mtu);
/* Limit the number of requests to process before giving back
* control to other tasks */
#define MAX_LOOP 64
for (cnt = 0; cnt < MAX_LOOP; cnt++)
{
len = recvfrom(iface->fd, packet, iface->mtu,
MSG_DONTWAIT | MSG_TRUNC, NULL, NULL);
if (len < 0)
{
if (errno == EINTR)
continue;
if (errno == EAGAIN)
break;
neterr(iface, "Read error");
del_fd(iface->fd);
close(iface->fd);
iface->fd = -1;
break;
}
if (G_UNLIKELY(len < (int)sizeof(struct aoe_hdr)))
{
netlog(iface, LOG_DEBUG, "Packet too short");
iface->stats.dropped++;
continue;
}
if (G_UNLIKELY(len > iface->mtu))
{
netlog(iface, LOG_ERR, "Received packet size (%d) is larger than "
"the configured MTU", len);
iface->stats.dropped++;
continue;
}
process_packet(iface, packet, len, NULL);
}
++iface->stats.rx_runs;
free_packet(packet, iface->mtu);
}
static void tx_sendmsg(struct netif *iface, struct queue_item *q)
{
static char zeroes[ETH_ZLEN];
struct iovec iov[3];
struct msghdr msg;
unsigned len;
int ret;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
iov[0].iov_base = &q->aoe_hdr;
iov[0].iov_len = len = q->hdrlen;
msg.msg_iovlen++;
if (q->length)
{
iov[msg.msg_iovlen].iov_base = q->buf;
iov[msg.msg_iovlen++].iov_len = q->length;
len += q->length;
}
/* If the frame is too small then it must be padded. On real networks
* this is not neccessary but virtual interfaces tend to "forget" the
* padding and that can make clients unhappy */
if (len < ETH_ZLEN)
{
iov[msg.msg_iovlen].iov_base = zeroes;
iov[msg.msg_iovlen++].iov_len = ETH_ZLEN - len;
}
ret = sendmsg(iface->fd, &msg, MSG_DONTWAIT);
if (ret != -1)
{
iface->stats.tx_bytes += ret;
++iface->stats.tx_cnt;
if (q->dev && G_UNLIKELY(q->dev->cfg.trace_io))
devlog(q->dev, LOG_DEBUG, "%s/%08x: Response sent",
ether_ntoa((struct ether_addr *)&q->aoe_hdr.addr.ether_dhost),
(uint32_t)ntohl(q->aoe_hdr.tag));
drop_request(q);
return;
}
if (errno == EAGAIN)
{
++iface->stats.tx_buffers_full;
g_ptr_array_add(iface->deferred, q);
if (!iface->congested)
{
modify_fd(iface->fd, &iface->event_ctx, EPOLLIN | EPOLLOUT);
iface->congested = TRUE;
}
}
else
{
neterr(iface, "Write error");
drop_request(q);
}
}
/**********************************************************************
* Generic I/O handling
*/
/* Network I/O event handler callback */
static void net_io(uint32_t events, void *data)
{
struct netif *iface = data;
unsigned i;
if (events & EPOLLOUT)
{
iface->congested = FALSE;
for (i = 0; i < iface->deferred->len; i++)
{
struct queue_item *q;
q = g_ptr_array_index(iface->deferred, i);
send_response(q);
if (iface->congested)
{
/* send_response() adds the request to
* the end of the deferred queue when it
* sets the congested flag, so we must
* remove the duplicate entry here */
++i;
break;
}
}
g_ptr_array_remove_range(iface->deferred, 0, i);
if (!iface->deferred->len)
modify_fd(iface->fd, &iface->event_ctx, EPOLLIN);
}
if (events & EPOLLIN)
{
if (iface->rx_ring.frames)
rx_ring(iface);
else
rx_recvfrom(iface);
}
}
void send_response(struct queue_item *q)
{
struct netif *const iface = q->iface;
if (!iface || iface->fd == -1)
{
drop_request(q);
return;
}
if (iface->congested)
{
g_ptr_array_add(iface->deferred, q);
return;
}
if (iface->tx_ring.frames)
tx_ring(iface, q);
else
tx_sendmsg(iface, q);
}
static int dev_sort(const void *a, const void *b)
{
const struct device *const *deva = a;
const struct device *const *devb = b;
/* Note: dev->cfg.shelf is in network byte order so the following
* will not give a natural order */
if ((*deva)->cfg.shelf == (*devb)->cfg.shelf)
return (*deva)->cfg.slot - (*devb)->cfg.slot;
else
return (*deva)->cfg.shelf - (*devb)->cfg.shelf;
}
/* Attach any new devices to the interface and ensure that the list
* is sorted by shelf/slot */
static void attach_new_devices(struct netif *iface)
{
g_ptr_array_foreach(devices, attach_device, iface);
g_ptr_array_sort(iface->devices, dev_sort);
}
static void setup_filter(struct netif *iface)
{
static struct sock_filter filter[] =
{
/* Load the type into register */
BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
/* Does it match AoE (0x88a2)? */
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x88a2, 0, 4),
/* Load the flags into register */
BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 14),
/* Check to see if the Resp flag is set */
BPF_STMT(BPF_ALU+BPF_AND+BPF_K, (1 << 3)),
/* Yes, goto INVALID */
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 1),
/* VALID: return -1 (allow the packet to be read) */
BPF_STMT(BPF_RET+BPF_K, -1),
/* INVALID: return 0 (ignore the packet) */
BPF_STMT(BPF_RET+BPF_K, 0),
};
static struct sock_fprog prog =
{
.filter = filter,
.len = G_N_ELEMENTS(filter)
};
if (setsockopt(iface->fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
neterr(iface, "Failed to set up the socket filter");
}
/* Setting SO_SNDBUF/SO_RCVBUF is just advisory, so report the real value being
* used */
static void set_buffer(struct netif *iface, int what, int size)
{
const char *unit;
socklen_t len;
int ret, val;
ret = setsockopt(iface->fd, SOL_SOCKET, what, &size, sizeof(size));
if (ret)
{
neterr(iface, "Failed to set the %s buffer size",
what == SO_SNDBUF ? "send" : "receive");
return;
}
len = sizeof(val);
if (getsockopt(iface->fd, SOL_SOCKET, what, &val, &len))
val = size;
ret = human_format(val, &unit);
netlog(iface, LOG_INFO, "The %s buffer is %d %s",
what == SO_SNDBUF ? "send" : "receive", ret, unit);
}
/* Validate an interface when it is found */
void validate_iface(const char *name, int ifindex, int mtu, const char *macaddr)
{
struct netif_config newcfg;
struct netif *iface;
unsigned i;
for (i = 0; i < ifaces->len; i++)
{
iface = g_ptr_array_index(ifaces, i);
if (iface->ifindex == ifindex)
break;
}
if (i >= ifaces->len)
{
/* This is a new interface. Check if we want to listen on this
* interface or not */
if (!match_patternlist(defaults.interfaces, name))
return logit(LOG_DEBUG, "net/%s: Does not match the configured "
"pattern list, ignoring", name);
iface = alloc_iface(ifindex, name);
g_ptr_array_add(ifaces, iface);
}
else
{
/* The interface already exists. If we no longer want to listen on
* this interface, invalidate it */
if (!match_patternlist(defaults.interfaces, name))
return invalidate_iface(ifindex);
/* If the interface got renamed, re-validate the list of attached
* devices */
if (strcmp(iface->name, name))
{
netlog(iface, LOG_NOTICE, "Interface name changed to %s",
name);
g_free(iface->name);
iface->name = g_strdup(name);
while (iface->devices->len)
detach_device(iface, g_ptr_array_index(iface->devices, 0));
attach_new_devices(iface);
}
}
memcpy(&iface->mac, macaddr, ETH_ALEN);
/* If the new configuration has errors, just use the old config */
if (!get_netif_config(iface->name, &newcfg))
newcfg = iface->cfg;
/* Clamp the MTU if the configuration says so */
if (newcfg.mtu && mtu > newcfg.mtu)
mtu = newcfg.mtu;
if (iface->fd == -1)
{
struct sockaddr_ll sa;
iface->fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_AOE));
if (iface->fd == -1)
{
neterr(iface, "Failed to allocate network socket");
return invalidate_iface(ifindex);
}
/* Set up the ring buffer before binding the socket to avoid
* packets ending up in the normal receive buffer */
setup_rings(iface, newcfg.ring_size, mtu);
iface->mtu = mtu;
memset(&sa, 0, sizeof(sa));
sa.sll_family = AF_PACKET;
sa.sll_protocol = htons(ETH_P_AOE);
sa.sll_ifindex = ifindex;
if (bind(iface->fd, (struct sockaddr *)&sa, sizeof(sa)) == -1)
{
neterr(iface, "bind() failed");
return invalidate_iface(ifindex);
}
setup_filter(iface);
add_fd(iface->fd, &iface->event_ctx);
netlog(iface, LOG_INFO, "Listener started (MTU: %d)", mtu);
/* We _are_ using the OS default at this point */
iface->cfg.send_buf_size = 0;
iface->cfg.recv_buf_size = 0;
}
else
{
/* If either the MTU or the ring buffer size changes, we have
* to destroy & re-allocate the ring buffer */
if (iface->mtu != mtu || newcfg.ring_size != iface->cfg.ring_size)
setup_rings(iface, newcfg.ring_size, mtu);
/* If the MTU has changed, tell it to the initiators */
if (iface->mtu != mtu)
{
netlog(iface, LOG_NOTICE, "MTU changed to %d", mtu);
iface->mtu = mtu;
for (i = 0; i < iface->devices->len; i++)
send_advertisment(g_ptr_array_index(iface->devices, i), iface);
}
}
if (newcfg.send_buf_size &&
newcfg.send_buf_size != iface->cfg.send_buf_size)
set_buffer(iface, SO_SNDBUF, newcfg.send_buf_size * 1024);
if (newcfg.recv_buf_size &&
newcfg.recv_buf_size != iface->cfg.recv_buf_size)
set_buffer(iface, SO_RCVBUF, newcfg.recv_buf_size * 1024);
/* destroy_netif_config(&iface->cfg); */
iface->cfg = newcfg;
attach_new_devices(iface);
}
void invalidate_iface(int ifindex)
{
struct netif *iface;
unsigned i;
for (i = 0; i < ifaces->len; i++)
{
iface = g_ptr_array_index(ifaces, i);
if (iface->ifindex == ifindex)
break;
}
if (i >= ifaces->len)
return;
netlog(iface, LOG_DEBUG, "Shutting down");
/* Keep the interface order, it is important for re-validation */
g_ptr_array_remove_index(ifaces, i);
while (iface->devices->len)
detach_device(iface, g_ptr_array_index(iface->devices, 0));
free_iface(iface);
}
void setup_ifaces(void)
{
unsigned i;
if (!ifaces)
ifaces = g_ptr_array_new();
/* Re-validate all interfaces */
for (i = 0; i < ifaces->len;)
{
struct netif *iface = g_ptr_array_index(ifaces, i);
if (!match_patternlist(defaults.interfaces, iface->name))
invalidate_iface(iface->ifindex);
else
i++;
}
/* Trigger a device enumeration in case the new configuration enabled
* more interfaces */
netmon_enumerate();
}
void done_ifaces(void)
{
struct netif *iface;
while (ifaces->len)
{
iface = g_ptr_array_index(ifaces, 0);
invalidate_iface(iface->ifindex);
}
g_ptr_array_free(ifaces, TRUE);
}