forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathethif.c
More file actions
1718 lines (1457 loc) · 55.2 KB
/
Copy pathethif.c
File metadata and controls
1718 lines (1457 loc) · 55.2 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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* LWIP service - ethif.c - ethernet interfaces */
/*
* The most important aspect of this module is to maintain a send queue for the
* interface. This send queue consists of packets to send. At times, the user
* may request a change to the driver configuration. While configuration
* requests would ideally be enqueued in the send queue, this has proven too
* problematic to work in practice, especially since out-of-memory conditions
* may prevent configuration requests from being accepted immediately in such a
* model. Instead, we take a simple and blunt approach: configuration requests
* "cut in line" and thus take precedence over pending packets in the send
* queue. This may not always be entirely correct: for example, packets may be
* transmitted with the old ethernet address after the network device has
* already been reconfigured to receive from a new ethernet address. However,
* this should not be a real problem, and we take care explicitly of perhaps
* the most problematic case: packets not getting checksummed due to checksum
* offloading configuration changes.
*
* Even with this blunt approach, we maintain three concurrent configurations:
* the active, the pending, and the wanted configuration. The active one is
* the last known active configuration at the network driver. It used not only
* to report whether the device is in RUNNING state, but also to replay the
* active configuration to a restarted driver. The pending configuration is
* a partially new configuration that has been given to ndev to send to the
* driver, but not yet acknowledged by the driver. Finally, the wanted
* configuration is the latest one that has yet to be given to ndev.
*
* Each configuration has a bitmask indicating which part of the configuration
* has changed, in order to limit work on the driver side. This is also the
* reason that the pending and wanted configurations are separate: if e.g. a
* media change is pending at the driver, and the user also requests a mode
* change, we do not want the media change to be repeated after it has been
* acknowleged by the driver, just to change the mode as well. In this example
* the pending configuration will have NDEV_SET_MEDIA set, and the wanted
* configuration will have NDEV_SET_MODE set. Once acknowledged, the pending
* bitmask is cleared and the wanted bitmask is tested to see if another
* configuration change should be given to ndev. Technically, this could lead
* to starvation of actual packet transmission, but we expect configuration
* changes to be very rare, since they are always user initiated.
*
* It is important to note for understanding the code that for some fields
* (mode, flags, caps), the three configurations are cascading: even though the
* wanted configuration may not have NDEV_SET_MODE set, its mode field will
* still contain the most recently requested mode; that is, the mode in the
* pending configuration if that one has NDEV_SET_MODE set, or otherwise the
* mode in the active configuration. For that reason, we carefully merge
* configuration requests into the next level (wanted -> pending -> active),
* updating just the fields that have been changed by the previous level. This
* approach simplifies obtaining current values a lot, but is not very obvious.
*
* Also, we never send multiple configuration requests at once, even though
* ndev would let us do that: we use a single array for the list of multicast
* ethernet addresses that we send to the driver, which the driver may retrieve
* (using a memory grant) at any time. We necessarily recompute the multicast
* list before sending a configuration request, and thus, sending multiple
* requests at once may lead to the driver retrieving a corrupted list.
*/
#include "lwip.h"
#include "ethif.h"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "lwip/igmp.h"
#include "lwip/mld6.h"
#include <net/if_media.h>
#define ETHIF_MAX_MTU 1500 /* maximum MTU value for ethernet */
#define ETHIF_DEF_MTU ETHIF_MAX_MTU /* default MTU value that we use */
#define ETHIF_MCAST_MAX 8 /* maximum number of multicast addresses */
struct ethif {
struct ifdev ethif_ifdev; /* interface device, MUST be first */
ndev_id_t ethif_ndev; /* network device ID */
unsigned int ethif_flags; /* interface flags (ETHIFF_) */
uint32_t ethif_caps; /* driver capabilities (NDEV_CAPS_) */
uint32_t ethif_media; /* driver-reported media type (IFM_) */
struct ndev_conf ethif_active; /* active configuration (at driver) */
struct ndev_conf ethif_pending; /* pending configuration (at ndev) */
struct ndev_conf ethif_wanted; /* desired configuration (waiting) */
struct ndev_hwaddr ethif_mclist[ETHIF_MCAST_MAX]; /* multicast list */
struct { /* send queue (packet/conf refs) */
struct pbuf *es_head; /* first (oldest) request reference */
struct pbuf **es_unsentp; /* ptr-ptr to first unsent request */
struct pbuf **es_tailp; /* ptr-ptr for adding new requests */
unsigned int es_count; /* buffer count, see ETHIF_PBUF_.. */
} ethif_snd;
struct { /* receive queue (packets) */
struct pbuf *er_head; /* first (oldest) request buffer */
struct pbuf **er_tailp; /* ptr-ptr for adding new requests */
} ethif_rcv;
SIMPLEQ_ENTRY(ethif) ethif_next; /* next in free list */
} ethif_array[NR_NDEV]; /* any other value would be suboptimal */
#define ethif_get_name(ethif) (ifdev_get_name(&(ethif)->ethif_ifdev))
#define ethif_get_netif(ethif) (ifdev_get_netif(&(ethif)->ethif_ifdev))
#define ETHIFF_DISABLED 0x01 /* driver has disappeared */
#define ETHIFF_FIRST_CONF 0x02 /* first configuration request sent */
/*
* Send queue limit settings. Both are counted in number of pbuf objects.
* ETHIF_PBUF_MIN is the minimum number of pbuf objects that can always be
* enqueued on a particular interface's send queue. It should be at least the
* number of pbufs for one single packet after being reduced to the ndev limit,
* so NDEV_IOV_MAX (8) is a natural fit. The ETHIF_PBUF_MAX_n values define
* the maximum number of pbufs that may be used by all interface send queues
* combined, whichever of the two is smaller. The resulting number must be set
* fairly high, because at any time there may be a lot of active TCP sockets
* that all generate a (multi-pbuf) packet as a result of a clock tick. It is
* currently a function of the size of the buffer pool, capped to a value that
* is a function of the number of TCP sockets (assuming one packet per socket;
* up to MSS/BUFSIZE+1 data pbufs, one header pbuf, one extra as margin). The
* difference between the per-interface guaranteed minimum and the global
* maximum is what makes up a pool of "spares", which are really just tokens
* allowing for enqueuing of that many pbufs.
*/
#define ETHIF_PBUF_MIN (NDEV_IOV_MAX)
#define ETHIF_PBUF_MAX_1 (mempool_cur_buffers() >> 1)
#define ETHIF_PBUF_MAX_2 (NR_TCPSOCK * (TCP_MSS / MEMPOOL_BUFSIZE + 3))
static unsigned int ethif_spares;
static SIMPLEQ_HEAD(, ethif) ethif_freelist; /* free ethif objects */
static const struct ifdev_ops ethif_ops;
#ifdef INET6
static ip6_addr_t ethif_ip6addr_allnodes_ll;
#endif /* INET6 */
/*
* Initialize the ethernet interfaces module.
*/
void
ethif_init(void)
{
unsigned int slot;
/* Initialize the list of free ethif objects. */
SIMPLEQ_INIT(ðif_freelist);
for (slot = 0; slot < __arraycount(ethif_array); slot++)
SIMPLEQ_INSERT_TAIL(ðif_freelist, ðif_array[slot],
ethif_next);
/* Initialize the number of in-use spare tokens. */
ethif_spares = 0;
#ifdef INET6
/* Preinitialize the link-local all-nodes IPv6 multicast address. */
ip6_addr_set_allnodes_linklocal(ðif_ip6addr_allnodes_ll);
#endif /* INET6 */
}
/*
* As the result of some event, the NetBSD-style interface flags for this
* interface may have changed. Recompute and update the flags as appropriate.
*/
static void
ethif_update_ifflags(struct ethif * ethif)
{
unsigned int ifflags;
ifflags = ifdev_get_ifflags(ðif->ethif_ifdev);
/* These are the flags that we might update here. */
ifflags &= ~(IFF_RUNNING | IFF_ALLMULTI);
/*
* For us, the RUNNING flag indicates that -as far as we know- the
* network device is fully operational and has its I/O engines running.
* This is a reflection of the current state, not of any intention, so
* we look at the active configuration here. We use the same approach
* for one other receive state flags here (ALLMULTI).
*/
if ((ethif->ethif_flags &
(ETHIFF_DISABLED | ETHIFF_FIRST_CONF)) == 0 &&
ethif->ethif_active.nconf_mode != NDEV_MODE_DOWN) {
ifflags |= IFF_RUNNING;
if (ethif->ethif_active.nconf_mode & NDEV_MODE_MCAST_ALL)
ifflags |= IFF_ALLMULTI;
}
ifdev_update_ifflags(ðif->ethif_ifdev, ifflags);
}
/*
* Add a multicast hardware receive address into the set of hardware addresses
* in the given configuration, if the given address is not already in the
* configuration's set. Adjust the configuration's mode as needed. Return
* TRUE If the address was added, and FALSE if the address could not be added
* due to a full list (of 'max' elements), in which case the mode is changed
* from receiving from listed multicast addresses to receiving from all
* multicast addresses.
*/
static int
ethif_add_mcast(struct ndev_conf * nconf, unsigned int max,
struct ndev_hwaddr * hwaddr)
{
unsigned int slot;
/*
* See if the hardware address is already in the list we produced so
* far. This makes the multicast list generation O(n^2) but we do not
* expect many entries nor is the list size large anyway.
*/
for (slot = 0; slot < nconf->nconf_mccount; slot++)
if (!memcmp(&nconf->nconf_mclist[slot], hwaddr,
sizeof(*hwaddr)))
return TRUE;
if (nconf->nconf_mccount < max) {
memcpy(&nconf->nconf_mclist[slot], hwaddr, sizeof(*hwaddr));
nconf->nconf_mccount++;
nconf->nconf_mode |= NDEV_MODE_MCAST_LIST;
return TRUE;
} else {
nconf->nconf_mode &= ~NDEV_MODE_MCAST_LIST;
nconf->nconf_mode |= NDEV_MODE_MCAST_ALL;
return FALSE;
}
}
/*
* Add the ethernet hardware address derived from the given IPv4 multicast
* address, to the list of multicast addresses.
*/
static int
ethif_add_mcast_v4(struct ndev_conf * nconf, unsigned int max,
const ip4_addr_t * ip4addr)
{
struct ndev_hwaddr hwaddr;
/* 01:00:05:xx:xx:xx with the lower 23 bits of the IPv4 address. */
hwaddr.nhwa_addr[0] = LL_IP4_MULTICAST_ADDR_0;
hwaddr.nhwa_addr[1] = LL_IP4_MULTICAST_ADDR_1;
hwaddr.nhwa_addr[2] = LL_IP4_MULTICAST_ADDR_2;
hwaddr.nhwa_addr[3] = (ip4_addr_get_u32(ip4addr) >> 16) & 0x7f;
hwaddr.nhwa_addr[4] = (ip4_addr_get_u32(ip4addr) >> 8) & 0xff;
hwaddr.nhwa_addr[5] = (ip4_addr_get_u32(ip4addr) >> 0) & 0xff;
return ethif_add_mcast(nconf, max, &hwaddr);
}
/*
* Add the ethernet hardware address derived from the given IPv6 multicast
* address, to the list of multicast addresses.
*/
static int
ethif_add_mcast_v6(struct ndev_conf * nconf, unsigned int max,
const ip6_addr_t * ip6addr)
{
struct ndev_hwaddr hwaddr;
/* 33:33:xx:xx:xx:xx with the lower 32 bits of the IPv6 address. */
hwaddr.nhwa_addr[0] = LL_IP6_MULTICAST_ADDR_0;
hwaddr.nhwa_addr[1] = LL_IP6_MULTICAST_ADDR_1;
memcpy(&hwaddr.nhwa_addr[2], &ip6addr->addr[3], sizeof(uint32_t));
return ethif_add_mcast(nconf, max, &hwaddr);
}
/*
* Set up the multicast mode for a configuration that is to be sent to a
* network driver, generating a multicast receive address list for the driver
* as applicable.
*/
static void
ethif_gen_mcast(struct ethif * ethif, struct ndev_conf * nconf)
{
struct igmp_group *group4;
struct mld_group *group6;
unsigned int max;
/* Make sure that multicast is supported at all for this interface. */
if (!(ethif->ethif_caps & NDEV_CAP_MCAST))
return;
/* Make sure the mode is being (re)configured to be up. */
if (!(nconf->nconf_set & NDEV_SET_MODE) ||
nconf->nconf_mode == NDEV_MODE_DOWN)
return;
/* Recompute the desired multicast flags. */
nconf->nconf_mode &= ~(NDEV_MODE_MCAST_LIST | NDEV_MODE_MCAST_ALL);
/* If promiscuous mode is enabled, receive all multicast packets. */
if (nconf->nconf_mode & NDEV_MODE_PROMISC) {
nconf->nconf_mode |= NDEV_MODE_MCAST_ALL;
return;
}
/*
* Map all IGMP/MLD6 multicast addresses to ethernet addresses, merging
* any duplicates to save slots. We have to add the MLD6 all-nodes
* multicast address ourselves, which also means the list is never
* empty unless compiling with USE_INET6=no. If the list is too small
* for all addresses, opt to receive all multicast packets instead.
*/
nconf->nconf_mclist = ethif->ethif_mclist;
nconf->nconf_mccount = 0;
max = __arraycount(ethif->ethif_mclist);
for (group4 = netif_igmp_data(ethif_get_netif(ethif)); group4 != NULL;
group4 = group4->next)
if (!ethif_add_mcast_v4(nconf, max, &group4->group_address))
return;
#ifdef INET6
if (!ethif_add_mcast_v6(nconf, max, ðif_ip6addr_allnodes_ll))
return;
#endif /* INET6 */
for (group6 = netif_mld6_data(ethif_get_netif(ethif)); group6 != NULL;
group6 = group6->next)
if (!ethif_add_mcast_v6(nconf, max, &group6->group_address))
return;
}
/*
* Merge a source configuration into a destination configuration, copying any
* fields intended to be set from the source into the destination and clearing
* the "set" mask in the source, without changing the source fields, so that
* the source will reflect the destination's contents.
*/
static void
ethif_merge_conf(struct ndev_conf * dconf, struct ndev_conf * sconf)
{
dconf->nconf_set |= sconf->nconf_set;
if (sconf->nconf_set & NDEV_SET_MODE)
dconf->nconf_mode = sconf->nconf_mode;
if (sconf->nconf_set & NDEV_SET_CAPS)
dconf->nconf_caps = sconf->nconf_caps;
if (sconf->nconf_set & NDEV_SET_FLAGS)
dconf->nconf_flags = sconf->nconf_flags;
if (sconf->nconf_set & NDEV_SET_MEDIA)
dconf->nconf_media = sconf->nconf_media;
if (sconf->nconf_set & NDEV_SET_HWADDR)
memcpy(&dconf->nconf_hwaddr, &sconf->nconf_hwaddr,
sizeof(dconf->nconf_hwaddr));
sconf->nconf_set = 0;
}
/*
* Return TRUE if we can and should try to pass a configuration request to the
* ndev layer on this interface, or FALSE otherwise.
*/
static int
ethif_can_conf(struct ethif * ethif)
{
/* Is there a configuration change waiting? The common case is no. */
if (ethif->ethif_wanted.nconf_set == 0)
return FALSE;
/*
* Is there a configuration change pending already? Then wait for it
* to be acknowledged first.
*/
if (ethif->ethif_pending.nconf_set != 0)
return FALSE;
/* Make sure the interface is in the appropriate state. */
if (ethif->ethif_flags & ETHIFF_DISABLED)
return FALSE;
/* First let all current packet send requests finish. */
return (ethif->ethif_snd.es_unsentp == ðif->ethif_snd.es_head);
}
/*
* Return TRUE if we can and should try to pass the next unsent packet send
* request to the ndev layer on this interface, or FALSE otherwise.
*/
static int
ethif_can_send(struct ethif * ethif)
{
/* Is there anything to hand to ndev at all? The common case is no. */
if (*ethif->ethif_snd.es_unsentp == NULL)
return FALSE;
/*
* Is there a configuration change pending? Then we cannot send
* packets yet. Always let all configuration changes through first.
*/
if (ethif->ethif_pending.nconf_set != 0 ||
ethif->ethif_wanted.nconf_set != 0)
return FALSE;
/* Make sure the interface is in the appropriate state. */
if ((ethif->ethif_flags & (ETHIFF_DISABLED | ETHIFF_FIRST_CONF)) != 0)
return FALSE;
return TRUE;
}
/*
* Return TRUE if we can and should try to receive packets on this interface
* and are ready to accept received packets, or FALSE otherwise.
*/
static int
ethif_can_recv(struct ethif * ethif)
{
if ((ethif->ethif_flags & (ETHIFF_DISABLED | ETHIFF_FIRST_CONF)) != 0)
return FALSE;
/*
* We do not check the link status here. There is no reason not to
* spawn receive requests, or accept received packets, while the link
* is reported to be down.
*/
return ifdev_is_up(ðif->ethif_ifdev);
}
/*
* Polling function, invoked after each message loop iteration. Check whether
* any configuration change or packets can be sent to the driver, and whether
* any new packet receive requests can be enqueued at the driver.
*/
static void
ethif_poll(struct ifdev * ifdev)
{
struct ethif *ethif = (struct ethif *)ifdev;
struct pbuf *pbuf, *pref;
/*
* If a configuration request is desired, see if we can send it to the
* driver now. Otherwise, attempt to send any packets if possible.
* In both cases, a failure of the ndev call indicates that we should
* try again later.
*/
if (ethif_can_conf(ethif)) {
ethif_gen_mcast(ethif, ðif->ethif_wanted);
/*
* On success, move the wanted configuration into the pending
* slot. Otherwise, try again on the next poll iteration.
*/
if (ndev_conf(ethif->ethif_ndev, ðif->ethif_wanted) == OK)
ethif_merge_conf(ðif->ethif_pending,
ðif->ethif_wanted);
} else {
while (ethif_can_send(ethif)) {
pref = *ethif->ethif_snd.es_unsentp;
if (pref->type == PBUF_REF)
pbuf = (struct pbuf *)pref->payload;
else
pbuf = pref;
if (ndev_send(ethif->ethif_ndev, pbuf) == OK)
ethif->ethif_snd.es_unsentp =
pchain_end(pref);
else
break;
}
}
/*
* Attempt to create additional receive requests for the driver, if
* applicable. We currently do not set a limit on the maximum number
* of concurrently pending receive requests here, because the maximum
* in ndev is already quite low. That may have to be changed one day.
*/
while (ethif_can_recv(ethif) && ndev_can_recv(ethif->ethif_ndev)) {
/*
* Allocate a buffer for the network device driver to copy the
* received packet into. Allocation may fail if no buffers are
* available at this time; in that case simply try again later.
* We add room for a VLAN tag even though we do not support
* such tags just yet.
*/
if ((pbuf = pchain_alloc(PBUF_RAW, ETH_PAD_LEN + ETH_HDR_LEN +
ETHIF_MAX_MTU + NDEV_ETH_PACKET_TAG)) == NULL)
break;
/*
* Effectively throw away two bytes in order to align TCP/IP
* header fields to 32 bits. See the short discussion in
* lwipopts.h as to why we are not using lwIP's ETH_PAD_SIZE.
*/
util_pbuf_header(pbuf, -ETH_PAD_LEN);
/*
* Send the request to the driver. This may still fail due to
* grant allocation failure, in which case we try again later.
*/
if (ndev_recv(ethif->ethif_ndev, pbuf) != OK) {
pbuf_free(pbuf);
break;
}
/*
* Hold on to the packet buffer until the receive request
* completes or is aborted, or the driver disappears.
*/
*ethif->ethif_rcv.er_tailp = pbuf;
ethif->ethif_rcv.er_tailp = pchain_end(pbuf);
}
}
/*
* Complete the link-layer header of the packet by filling in a source address.
* This is relevant for BPF-generated packets only, and thus we can safely
* modify the given pbuf.
*/
static void
ethif_hdrcmplt(struct ifdev * ifdev, struct pbuf * pbuf)
{
struct netif *netif;
/* Make sure there is an ethernet packet header at all. */
if (pbuf->len < ETH_HDR_LEN)
return;
netif = ifdev_get_netif(ifdev);
/*
* Insert the source ethernet address into the packet. The source
* address is located right after the destination address at the start
* of the packet.
*/
memcpy((uint8_t *)pbuf->payload + netif->hwaddr_len, netif->hwaddr,
netif->hwaddr_len);
}
/*
* Return TRUE if the given additional number of spare tokens may be used, or
* FALSE if the limit has been reached. Each spare token represents one
* enqueued pbuf. The limit must be such that we do not impede normal traffic
* but also do not spend the entire buffer pool on enqueued packets.
*/
static int
ethif_can_spare(unsigned int spares)
{
unsigned int max;
/*
* Use the configured maximum, which depends on the current size of the
* buffer pool.
*/
max = ETHIF_PBUF_MAX_1;
/*
* However, limit the total to a value based on the maximum number of
* TCP packets that can, in the worst case, be expected to queue up at
* any single moment.
*/
if (max > ETHIF_PBUF_MAX_2)
max = ETHIF_PBUF_MAX_2;
return (spares + ethif_spares <= max - ETHIF_PBUF_MIN * NR_NDEV);
}
/*
* Process a packet as output on an ethernet interface.
*/
static err_t
ethif_output(struct ifdev * ifdev, struct pbuf * pbuf, struct netif * netif)
{
struct ethif *ethif = (struct ethif *)ifdev;
struct pbuf *pref, *pcopy;
size_t padding;
unsigned int count, spares;
/* Packets must never be sent on behalf of another interface. */
assert(netif == NULL);
/*
* The caller already rejects packets while the interface or link is
* down. We do want to keep enqueuing packets while the driver is
* restarting, so do not check ETHIFF_DISABLED or ETHIFF_FIRST_CONF.
*/
/*
* Reject oversized packets immediately. This should not happen.
* Undersized packets are padded below.
*/
if (pbuf->tot_len > NDEV_ETH_PACKET_MAX) {
printf("LWIP: attempt to send oversized ethernet packet "
"(size %u)\n", pbuf->tot_len);
util_stacktrace();
return ERR_MEM;
}
/*
* The original lwIP idea for processing output packets is that we make
* a copy of the packet here, so that lwIP is free to do whatever it
* wants with the original packet (e.g., keep on the TCP retransmission
* queue). More recently, lwIP has made progress towards allowing the
* packet to be referenced only, decreasing the reference count only
* once the packet has been actually sent. For many embedded systems,
* that change now allows zero-copy transmission with direct DMA from
* the provided packet buffer. We are not so lucky: we have to make an
* additional inter-process copy anyway. We do however use the same
* referencing system to avoid having to make yet another copy of the
* packet here.
*
* There was previously a check on (pbuf->ref > 1) here, to ensure that
* we would never enqueue packets that are retransmitted while we were
* still in the process of sending the initial copy. Now that for ARP
* and NDP queuing, packets are referenced rather than copied (lwIP
* patch #9272), we can no longer perform that check: packets may
* legitimately have a reference count of 2 at this point. The second
* reference will be dropped by the caller immediately after we return.
*/
/*
* There are two cases in which we need to make a copy of the packet
* after all:
*
* 1) in the case that the packet needs to be padded in order to reach
* the minimum ethernet packet size (for drivers' convenience);
* 2) in the (much more exceptional) case that the given pbuf chain
* exceeds the maximum vector size for network driver requests.
*/
if (NDEV_ETH_PACKET_MIN > pbuf->tot_len)
padding = NDEV_ETH_PACKET_MIN - pbuf->tot_len;
else
padding = 0;
count = pbuf_clen(pbuf);
if (padding != 0 || count > NDEV_IOV_MAX) {
pcopy = pchain_alloc(PBUF_RAW, pbuf->tot_len + padding);
if (pcopy == NULL) {
ifdev_output_drop(ifdev);
return ERR_MEM;
}
if (pbuf_copy(pcopy, pbuf) != ERR_OK)
panic("unexpected pbuf copy failure");
if (padding > 0) {
/*
* This restriction can be lifted if needed, but it
* involves hairy pbuf traversal and our standard pool
* size should be way in excess of the minimum packet
* size.
*/
assert(pcopy->len == pbuf->tot_len + padding);
memset((char *)pcopy->payload + pbuf->tot_len, 0,
padding);
}
count = pbuf_clen(pcopy);
assert(count <= NDEV_IOV_MAX);
pbuf = pcopy;
} else
pcopy = NULL;
/*
* Restrict the size of the send queue, so that it will not exhaust the
* buffer pool.
*/
if (ethif->ethif_snd.es_count >= ETHIF_PBUF_MIN)
spares = count;
else if (ethif->ethif_snd.es_count + count > ETHIF_PBUF_MIN)
spares = ethif->ethif_snd.es_count + count - ETHIF_PBUF_MIN;
else
spares = 0;
if (spares > 0 && !ethif_can_spare(spares)) {
if (pcopy != NULL)
pbuf_free(pcopy);
ifdev_output_drop(ifdev);
return ERR_MEM;
}
/*
* A side effect of the referencing approach is that we cannot touch
* the last pbuf's "next" pointer. Thus, we need another way of
* linking together the buffers on the send queue. We use a linked
* list of PBUF_REF-type buffers for this instead. However, do this
* only when we have not made a copy of the original pbuf, because then
* we might as well use the copy instead.
*/
if (pcopy == NULL) {
if ((pref = pbuf_alloc(PBUF_RAW, 0, PBUF_REF)) == NULL) {
ifdev_output_drop(ifdev);
return ERR_MEM;
}
pbuf_ref(pbuf);
pref->payload = pbuf;
pref->tot_len = 0;
pref->len = count;
} else
pref = pcopy;
/* If the send queue was empty so far, set the IFF_OACTIVE flag. */
if (ethif->ethif_snd.es_head == NULL)
ifdev_update_ifflags(ðif->ethif_ifdev,
ifdev_get_ifflags(ðif->ethif_ifdev) | IFF_OACTIVE);
/*
* Enqueue the packet on the send queue. It will be sent from the
* polling function as soon as possible. TODO: see if sending it from
* here makes any performance difference at all.
*/
*ethif->ethif_snd.es_tailp = pref;
ethif->ethif_snd.es_tailp = pchain_end(pref);
ethif->ethif_snd.es_count += count;
ethif_spares += spares;
return ERR_OK;
}
/*
* Transmit an ethernet packet on an ethernet interface, as requested by lwIP.
*/
static err_t
ethif_linkoutput(struct netif * netif, struct pbuf * pbuf)
{
struct ifdev *ifdev = netif_get_ifdev(netif);
/*
* Let ifdev make the callback to our output function, so that it can
* pass the packet to BPF devices and generically update statistics.
*/
return ifdev_output(ifdev, pbuf, NULL /*netif*/, TRUE /*to_bpf*/,
TRUE /*hdrcmplt*/);
}
/*
* The multicast address list has changed. See to it that the change will make
* it to the network driver at some point.
*/
static err_t
ethif_set_mcast(struct ethif * ethif)
{
/*
* Simply generate a mode change request, unless the interface is down.
* Once the mode change request is about to be sent to the driver, we
* will recompute the multicast settings.
*/
if (ifdev_is_up(ðif->ethif_ifdev))
ethif->ethif_wanted.nconf_set |= NDEV_SET_MODE;
return ERR_OK;
}
/*
* An IPv4 multicast address has been added to or removed from the list of IPv4
* multicast addresses.
*/
static err_t
ethif_set_mcast_v4(struct netif * netif, const ip4_addr_t * group __unused,
enum netif_mac_filter_action action __unused)
{
return ethif_set_mcast((struct ethif *)netif_get_ifdev(netif));
}
/*
* An IPv6 multicast address has been added to or removed from the list of IPv6
* multicast addresses.
*/
static err_t
ethif_set_mcast_v6(struct netif * netif, const ip6_addr_t * group __unused,
enum netif_mac_filter_action action __unused)
{
return ethif_set_mcast((struct ethif *)netif_get_ifdev(netif));
}
/*
* Initialization function for an ethernet-type netif interface, called from
* lwIP at interface creation time.
*/
static err_t
ethif_init_netif(struct ifdev * ifdev, struct netif * netif)
{
struct ethif *ethif = (struct ethif *)ifdev;
/*
* Fill in a dummy name. Since it is only two characters, do not
* bother trying to reuse part of the given name. If this name is ever
* actually used anywhere, the dummy should suffice for debugging.
*/
netif->name[0] = 'e';
netif->name[1] = 'n';
netif->linkoutput = ethif_linkoutput;
memset(netif->hwaddr, 0, sizeof(netif->hwaddr));
/*
* Set the netif flags, partially based on the capabilities reported by
* the network device driver. The reason that we do this now is that
* lwIP tests for some of these flags and starts appropriate submodules
* (e.g., IGMP) right after returning from this function. If we set
* the flags later, we also have to take over management of those
* submodules, which is something we'd rather avoid. For this reason
* in particular, we also do not support capability mask changes after
* driver restarts - see ethif_enable().
*/
netif->flags = NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
if (ethif->ethif_caps & NDEV_CAP_BCAST)
netif->flags |= NETIF_FLAG_BROADCAST;
if (ethif->ethif_caps & NDEV_CAP_MCAST) {
/* The IGMP code adds the all-stations multicast entry. */
netif->igmp_mac_filter = ethif_set_mcast_v4;
netif->flags |= NETIF_FLAG_IGMP;
/* For MLD6 we have to add the all-nodes entry ourselves. */
netif->mld_mac_filter = ethif_set_mcast_v6;
netif->flags |= NETIF_FLAG_MLD6;
}
return ERR_OK;
}
/*
* The ndev layer reports that a new network device driver has appeared, with
* the given ndev identifier, a driver-given name, and a certain set of
* capabilities. Create a new ethernet interface object for it. On success,
* return a pointer to the object (for later callbacks from ndev). In that
* case, the ndev layer will always immediately call ethif_enable() afterwards.
* On failure, return NULL, in which case ndev will forget about the driver.
*/
struct ethif *
ethif_add(ndev_id_t id, const char * name, uint32_t caps)
{
struct ethif *ethif;
unsigned int ifflags;
int r;
/*
* First make sure that the interface name is valid, unique, and not
* reserved for virtual interface types.
*/
if ((r = ifdev_check_name(name, NULL /*vtype_slot*/)) != OK) {
/*
* There is some risk in printing bad stuff, but this may help
* in preventing serious driver writer frustration..
*/
printf("LWIP: invalid driver name '%s' (%d)\n", name, r);
return NULL;
}
/* Then see if there is a free ethernet interface object available. */
if (SIMPLEQ_EMPTY(ðif_freelist)) {
printf("LWIP: out of slots for driver name '%s'\n", name);
return NULL;
}
/*
* All good; set up the interface. First initialize the object, since
* adding the interface to lwIP might spawn some activity right away.
*/
ethif = SIMPLEQ_FIRST(ðif_freelist);
SIMPLEQ_REMOVE_HEAD(ðif_freelist, ethif_next);
/* Initialize the ethif structure. */
memset(ethif, 0, sizeof(*ethif));
ethif->ethif_ndev = id;
ethif->ethif_flags = ETHIFF_DISABLED;
ethif->ethif_caps = caps;
ethif->ethif_snd.es_head = NULL;
ethif->ethif_snd.es_unsentp = ðif->ethif_snd.es_head;
ethif->ethif_snd.es_tailp = ðif->ethif_snd.es_head;
ethif->ethif_snd.es_count = 0;
ethif->ethif_rcv.er_head = NULL;
ethif->ethif_rcv.er_tailp = ðif->ethif_rcv.er_head;
/*
* Set all the three configurations to the same initial values. Since
* any change to the configuration will go through all three, this
* allows us to obtain various parts of the status (in particular, the
* mode, flags, enabled capabilities, and media type selection) from
* any of the three without having to consult the others. Note that
* the hardware address is set to a indeterminate initial value, as it
* is left to the network driver unless specifically overridden.
*/
ethif->ethif_active.nconf_set = 0;
ethif->ethif_active.nconf_mode = NDEV_MODE_DOWN;
ethif->ethif_active.nconf_flags = 0;
ethif->ethif_active.nconf_caps = 0;
ethif->ethif_active.nconf_media =
IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0);
memcpy(ðif->ethif_pending, ðif->ethif_active,
sizeof(ethif->ethif_pending));
memcpy(ðif->ethif_wanted, ðif->ethif_pending,
sizeof(ethif->ethif_wanted));
/*
* Compute the initial NetBSD-style interface flags. The IFF_SIMPLEX
* interface flag is always enabled because we do not support network
* drivers that are receiving their own packets. In particular, lwIP
* currently does not deal well with receiving back its own multicast
* packets, which leads to IPv6 DAD failures. The other two flags
* (IFF_BROADCAST, IFF_MULTICAST) denote capabilities, not enabled
* receipt modes.
*/
ifflags = IFF_SIMPLEX;
if (caps & NDEV_CAP_BCAST)
ifflags |= IFF_BROADCAST;
if (caps & NDEV_CAP_MCAST)
ifflags |= IFF_MULTICAST;
/* Finally, add the interface to ifdev and lwIP. This cannot fail. */
ifdev_add(ðif->ethif_ifdev, name, ifflags, IFT_ETHER, ETH_HDR_LEN,
ETHARP_HWADDR_LEN, DLT_EN10MB, ETHIF_DEF_MTU,
ND6_IFF_PERFORMNUD | ND6_IFF_AUTO_LINKLOCAL, ðif_ops);
return ethif;
}
/*
* The link status and/or media type of an ethernet interface has changed.
*/
static void
ethif_set_status(struct ethif * ethif, uint32_t link, uint32_t media)
{
unsigned int iflink;
/* We save the media type locally for now. */
ethif->ethif_media = media;
/* Let the ifdev module handle the details of the link change. */
switch (link) {
case NDEV_LINK_UP: iflink = LINK_STATE_UP; break;
case NDEV_LINK_DOWN: iflink = LINK_STATE_DOWN; break;
default: iflink = LINK_STATE_UNKNOWN; break;
}
ifdev_update_link(ðif->ethif_ifdev, iflink);
}
/*
* The ndev layer reports that a previously added or disabled network device
* driver has been (re)enabled. Start by initializing the driver. Return TRUE
* if the interface could indeed be enabled, or FALSE if it should be forgotten
* altogether after all.
*/
int
ethif_enable(struct ethif * ethif, const char * name,
const struct ndev_hwaddr * hwaddr, uint8_t hwaddr_len, uint32_t caps,
uint32_t link, uint32_t media)
{
int r;
assert(ethif->ethif_flags & ETHIFF_DISABLED);
/*
* One disadvantage of keeping service labels and ethernet driver names
* disjunct is that the ethernet driver may mess with its name between
* restarts. Ultimately we may end up renaming our ethernet drivers
* such that their labels match their names, in which case we no longer
* need the drivers themselves to produce a name, and we can retire
* this check.
*/
if (name != NULL && strcmp(ethif_get_name(ethif), name)) {
printf("LWIP: driver '%s' restarted with name '%s'\n",
ethif_get_name(ethif), name);
return FALSE;
}
/*
* The hardware address length is just a sanity check for now. After
* the initialization reply, we assume the same length is used for all
* addresses, which is also the maximum, namely 48 bits (six bytes).
*/
if (hwaddr_len != ETHARP_HWADDR_LEN) {
printf("LWIP: driver '%s' reports hwaddr length %u\n",
ethif_get_name(ethif), hwaddr_len);