forked from sysml/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netfront.c
1893 lines (1619 loc) · 47.7 KB
/
netfront.c
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
/* Minimal network driver for Mini-OS.
* Copyright (c) 2006-2007 Jacob Gorm Hansen, University of Copenhagen.
* Copyright (c) 2014-2015 Joao Martins, NEC Europe Ltd.
* Copyright (c) 2015-2016 Simon Kuenzer, NEC Europe Ltd.
* Copyright (c) 2016 Kenichi Yasukata, NEC Europe Ltd.
* Based on netfront.c from Xen Linux.
*/
#include <mini-os/os.h>
#include <mini-os/mm.h>
#include <mini-os/xenbus.h>
#include <mini-os/events.h>
#include <errno.h>
#include <xen/io/netif.h>
#include <mini-os/gnttab.h>
#include <mini-os/xmalloc.h>
#include <mini-os/time.h>
#include <mini-os/netfront.h>
#include <mini-os/lib.h>
#include <mini-os/semaphore.h>
#include <xen/io/netif.h>
#if defined(__x86_64__) && !defined DEBUG_BUILD && !defined NOAVXMEMCPY
#include <rte_memcpy.h>
#define NETIF_MEMCPY(dst, src, len) rte_memcpy((dst), (src), (len))
#else
#define NETIF_MEMCPY(dst, src, len) memcpy((dst), (src), (len))
#endif
#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(num, div) (((num) + (div) - 1) / (div))
#endif
#ifdef HAVE_LWIP
#include "lwip/pbuf.h"
#include <lwip/stats.h>
#endif
DECLARE_WAIT_QUEUE_HEAD(netfront_queue);
DECLARE_WAIT_QUEUE_HEAD(netfront_txqueue);
#ifdef HAVE_LIBC
#define NETIF_SELECT_RX ((void*)-1)
#endif
#ifndef min
#define min(a, b) \
({ __typeof__ (a) __a = (a); \
__typeof__ (b) __b = (b); \
__a < __b ? __a : __b; })
#endif
#define dprintk(format, ...) do {} while(0)
#ifndef dprintk
#define dprintk(format, ...) printk(format,##__VA_ARGS__)
#endif
#define NET_TX_RING_SIZE __CONST_RING_SIZE(netif_tx, PAGE_SIZE)
#define NET_RX_RING_SIZE __CONST_RING_SIZE(netif_rx, PAGE_SIZE)
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
#if !defined CONFIG_NETFRONT_RX_BUFFERS || CONFIG_NETFRONT_RX_BUFFERS < 20
#define NET_RX_BUFFERS NET_RX_RING_SIZE
#else
#define NET_RX_BUFFERS CONFIG_NETFRONT_RX_BUFFERS
#endif
#endif
#define GRANT_INVALID_REF 0
struct netfront_dev;
struct net_txbuffer {
#if defined CONFIG_NETFRONT_PERSISTENT_GRANTS || !defined CONFIG_NETFRONT_LWIP_ONLY
void* page;
#endif
grant_ref_t gref;
#ifdef HAVE_LWIP
struct pbuf *pbuf;
#endif
};
struct net_rxbuffer {
void* page;
grant_ref_t gref;
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
unsigned short id;
#ifdef HAVE_LWIP
struct netfront_dev *dev;
struct pbuf_custom cpbuf;
#endif
#endif
};
#if !defined CONFIG_NETFRONT_PERSISTENT_GRANTS && defined HAVE_LWIP
#ifndef container_of
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif
#define _upcast_pbuf2rxbuf(p) \
(container_of(container_of(p, struct pbuf_custom, pbuf), struct net_rxbuffer, cpbuf))
#endif /* !CONFIG_NETFRONT_PERSISTENT_GRANTS && HAVE_LWIP */
struct netfront_dev {
domid_t dom;
unsigned short tx_freelist[NET_TX_RING_SIZE + 1];
struct semaphore tx_sem;
struct net_txbuffer tx_buffers[NET_TX_RING_SIZE];
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
struct net_rxbuffer rx_buffers[NET_RX_RING_SIZE];
#else
struct net_rxbuffer *rx_buffers[NET_RX_RING_SIZE];
struct net_rxbuffer rx_buffer_pool[NET_RX_BUFFERS];
unsigned short rx_freelist[NET_RX_BUFFERS + 1];
unsigned short rx_avail;
#endif
struct netif_tx_front_ring tx;
struct netif_rx_front_ring rx;
/* inflight response to be handled */
struct netif_rx_response rsp;
/* extras (if any) of the inflight buffer */
struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
/* used by pbuf_copy_bits */
struct pbuf *pbuf_cur;
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
uint32_t pbuf_off;
#endif
grant_ref_t tx_ring_ref;
grant_ref_t rx_ring_ref;
evtchn_port_t tx_evtchn;
evtchn_port_t rx_evtchn;
char *nodename;
char *backend;
char *mac;
#ifdef CONFIG_NETMAP
int netmap;
void *na;
#endif
xenbus_event_queue events;
#ifdef HAVE_LIBC
int fd;
unsigned char *data;
size_t len;
size_t rlen;
#endif
#ifdef HAVE_LWIP
void (*netif_rx_pbuf)(struct pbuf *p, void *arg);
#endif
void (*netif_rx)(unsigned char* data, int len, void *arg);
void *netif_rx_arg;
#ifdef CONFIG_NETFRONT_STATS
uint64_t txpkts;
uint64_t txbytes;
#endif
};
struct netfront_dev_list {
struct netfront_dev *dev;
unsigned char rawmac[6];
char *ip;
int refcount;
struct netfront_dev_list *next;
};
#ifdef CONFIG_NETMAP
#include <mini-os/netfront_netmap.h>
#endif
static struct netfront_dev_list *dev_list = NULL;
static void init_rx_buffers(struct netfront_dev *dev);
static void netfront_fillup_rx_buffers(struct netfront_dev *dev);
static void netfront_tx_buf_gc(struct netfront_dev *dev);
static struct netfront_dev *_init_netfront(struct netfront_dev *dev,
unsigned char rawmac[6], char **ip);
static void _shutdown_netfront(struct netfront_dev *dev);
static inline void add_id_to_freelist(unsigned short id, unsigned short *freelist)
{
freelist[id + 1] = freelist[0];
freelist[0] = id;
}
static inline unsigned short get_id_from_freelist(unsigned short *freelist)
{
unsigned int id = freelist[0];
freelist[0] = freelist[id + 1];
return id;
}
__attribute__((weak)) void netif_rx(unsigned char* data, int len, void *arg)
{
printk("%d bytes incoming at %p\n", len, data);
}
__attribute__((weak)) void net_app_main(void *si, unsigned char *mac)
{}
#ifdef HAVE_LWIP
__attribute__((weak)) void netif_rx_pbuf(struct pbuf *p, void *arg)
{
printk("%d bytes incoming at pbuf %p\n", p->len, p);
}
struct eth_addr *netfront_get_hwaddr(struct netfront_dev *dev,
struct eth_addr *out)
{
if (sscanf(dev->mac,
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&out->addr[0],
&out->addr[1],
&out->addr[2],
&out->addr[3],
&out->addr[4],
&out->addr[5]) == 6) {
return out;
}
return NULL;
}
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
/* Copies data to a pbuf */
static inline void pbuf_copy_bits(struct pbuf **p, uint32_t *offset,
unsigned char *data, int32_t len)
{
struct pbuf *q;
uint32_t q_ofs = *offset;
unsigned char *cur = data;
int l = min(len, (*p)->len);
dprintk("rx: copy: pbuf %p ofs %d len %d p->len %d l %d\n",
*p, q_ofs, len, (*p)->len, l);
/* pbuf chain */
for(q = *p; q && len > 0; cur += l) {
l = min(len, q->len - q_ofs);
NETIF_MEMCPY(q->payload + q_ofs, cur, l);
dprintk("rx: copy: pbuf %p ofs %d l %d\n", q, q_ofs, l);
len -= l;
q_ofs += l;
if (q_ofs >= q->len) {
q_ofs = 0;
q = q->next;
}
}
*p = q;
*offset = q_ofs;
}
/* Allocates a pbuf and initializes dev->pbuf_cur, dev->pbuf_off */
static inline struct pbuf *netfront_alloc_pbuf(struct netfront_dev *dev, int32_t len)
{
struct pbuf *p;
if (unlikely((len) > (0xFFFF - ETH_PAD_SIZE) || len <= 0))
return NULL; /* unsupported length: ignore */
p = pbuf_alloc(PBUF_RAW, (u16_t) (len + ETH_PAD_SIZE), PBUF_POOL);
dev->pbuf_cur = p;
dev->pbuf_off = 0;
return p;
}
#else /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
static void netfront_release_rxbuffer(struct net_rxbuffer *buf, struct netfront_dev *dev);
static void netfront_free_rxpbuf(struct pbuf *p)
{
struct net_rxbuffer *buf = _upcast_pbuf2rxbuf(p);
struct netfront_dev *dev;
dev = buf->dev;
netfront_release_rxbuffer(buf, dev);
}
static inline struct pbuf *netfront_init_rxpbuf(struct net_rxbuffer *buf, struct netfront_dev *dev)
{
struct pbuf *p;
p = pbuf_alloced_custom(PBUF_RAW, PAGE_SIZE, PBUF_REF, &buf->cpbuf, buf->page, PAGE_SIZE);
if (p == NULL)
return NULL;
buf->dev = dev;
buf->cpbuf.custom_free_function = netfront_free_rxpbuf;
return p;
}
#endif /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
#endif /* HAVE_LWIP */
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
static struct net_rxbuffer *netfront_get_rxbuffer(struct netfront_dev *dev)
{
struct net_rxbuffer *buf;
unsigned short id;
if (unlikely(dev->rx_avail == 0))
return NULL; /* out of rx buffers */
id = get_id_from_freelist(dev->rx_freelist);
buf = &dev->rx_buffer_pool[id];
buf->id = id;
#ifdef HAVE_LWIP
if (unlikely(netfront_init_rxpbuf(buf, dev) == NULL)) {
/* could not allocate custom pbuf */
add_id_to_freelist(id, dev->rx_freelist);
return NULL;
}
#endif /* HAVE_LWIP */
dev->rx_avail--;
return buf;
}
static void netfront_release_rxbuffer(struct net_rxbuffer *buf, struct netfront_dev *dev)
{
add_id_to_freelist(buf->id, dev->rx_freelist);
dev->rx_avail++;
}
#endif
/*
* Main entry point for handling a packet. If HAVE_LWIP is set
* we allow passing up pbufs upon registering the appropriate
* callback.
*/
static inline int handle_buffer(struct netfront_dev *dev,
struct netif_rx_response *rx,
struct net_rxbuffer *buf, int32_t realsize)
{
unsigned char* page = buf->page;
#ifdef HAVE_LIBC
if (dev->netif_rx == NETIF_SELECT_RX) {
int len = rx->status;
ASSERT(current == main_thread);
if (len > dev->len)
len = dev->len;
NETIF_MEMCPY(dev->data, page+rx->offset, len);
dev->rlen = len;
return 1;
}
#endif
#ifdef HAVE_LWIP
if (likely(dev->netif_rx_pbuf)) {
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
pbuf_copy_bits(&dev->pbuf_cur, &dev->pbuf_off, (void *)((uintptr_t)buf->page+rx->offset), rx->status);
#else
dev->pbuf_cur->tot_len = dev->pbuf_cur->len = rx->status;
dev->pbuf_cur->payload = (void *)((uintptr_t)buf->page+rx->offset);
#endif /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
return 1;
}
#endif
#ifndef CONFIG_NETFRONT_LWIP_ONLY
if (dev->netif_rx)
dev->netif_rx(page+rx->offset, rx->status, dev->netif_rx_arg);
#endif
return 1;
}
/* req->id is numbered from 0 - 255 */
static inline int netfront_rxidx(RING_IDX idx)
{
return idx & (NET_RX_RING_SIZE - 1);
}
/*
* Computes the size of the pbuf to allocate based
* on how many slots the (possible GSO) frame requires.
*/
static int netfront_get_size(struct netfront_dev *dev, RING_IDX ri)
{
struct netif_rx_response *rx;
int32_t len = 0;
int slots = 1;
do {
rx = RING_GET_RESPONSE(&dev->rx, ++ri);
dprintk("rx: scan: slot %d len %d (more %s)\n",
slots, rx->status,
(rx->flags & NETRXF_more_data
? "true": "false"));
len += rx->status;
slots++;
} while (rx->flags & NETRXF_more_data);
return len;
}
/*
* Reads extra slots to check for a GSO packet
*/
static int netfront_get_extras(struct netfront_dev *dev,
struct netif_extra_info *extras, RING_IDX ri)
{
struct netif_extra_info *extra;
RING_IDX cons = dev->rx.rsp_cons;
int err = 0;
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
struct net_rxbuffer *buf;
#endif
do {
extra = (struct netif_extra_info *)
RING_GET_RESPONSE(&dev->rx, ++cons);
if (unlikely(!extra->type ||
extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
printk("Invalid extra type: %d\n", extra->type);
err = -EINVAL;
} else {
dprintk("rx: scan: extra %u %s\n", extra->type,
(extra->flags & XEN_NETIF_EXTRA_FLAG_MORE
? "(more true)": ""));
NETIF_MEMCPY(&extras[extra->type - 1], extra,
sizeof(*extra));
}
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
buf = dev->rx_buffers[netfront_rxidx(cons)];
gnttab_end_access(buf->gref);
buf->gref = GRANT_INVALID_REF;
dev->rx_buffers[netfront_rxidx(cons)] = NULL;
netfront_release_rxbuffer(buf, dev);
#endif
} while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
dev->rx.rsp_cons = cons;
return err;
}
/*
* Reads RX responses for a single packet
*/
static int netfront_get_responses(struct netfront_dev *dev,
RING_IDX rp)
{
struct netif_rx_response *rsp = &(dev->rsp);
int32_t realsize = rsp->status;
int16_t size = rsp->status;
uint16_t id = rsp->id;
uint16_t flags = rsp->flags;
RING_IDX cons = rp;
uint16_t slots = 1;
int drop = 0;
#ifdef HAVE_LWIP
struct pbuf *p;
struct pbuf *first_p;
#endif
dprintk("rx: ring: len %d %s\n", size,
(flags & NETRXF_more_data ? "(more true) ": ""));
BUG_ON(id >= NET_RX_RING_SIZE);
if (flags & NETRXF_extra_info) {
memset(dev->extras, 0, sizeof(dev->extras));
netfront_get_extras(dev, dev->extras, cons);
cons = dev->rx.rsp_cons;
}
if (flags & NETRXF_more_data) {
dprintk("rx: scan: slot 0 len %d %s\n",
size, (flags & NETRXF_more_data ? "(more true)": ""));
realsize = size + netfront_get_size(dev, cons);
}
dprintk("rx: %c%c- %"PRIi32" bytes\n",
flags & NETRXF_extra_info ? 'S' : '-',
flags & ((NETRXF_csum_blank) | (NETRXF_data_validated)) ? 'C' : '-',
realsize);
#ifdef HAVE_LWIP
if (likely(dev->netif_rx_pbuf)) {
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
first_p = p = netfront_alloc_pbuf(dev, realsize);
drop = (p == NULL);
#else
first_p = p = &dev->rx_buffers[id]->cpbuf.pbuf;
drop = 0;
dev->pbuf_cur = p;
#endif /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
#if ETH_PAD_SIZE
if (likely(!drop))
pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
#endif /* ETH_PAD_SIZE */
}
#endif /* HAVE_LWIP */
for (;;) {
if (unlikely(rsp->status < 0 ||
(rsp->offset + rsp->status > PAGE_SIZE))) {
printk("rx: ring<%u>: status %d, flags %04x, offset %d\n",
cons + slots, size, flags, rsp->offset);
} else if (likely(!drop)) {
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
handle_buffer(dev, rsp, &dev->rx_buffers[id], realsize);
#else
handle_buffer(dev, rsp, dev->rx_buffers[id], realsize);
#endif
}
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
BUG_ON(dev->rx_buffers[id]->gref == GRANT_INVALID_REF);
gnttab_end_access(dev->rx_buffers[id]->gref);
dev->rx_buffers[id]->gref = GRANT_INVALID_REF;
dev->rx_buffers[id] = NULL;
#endif
if (!(flags & NETRXF_more_data))
break;
if (dev->rx.sring->rsp_prod <= cons + slots)
break;
rsp = RING_GET_RESPONSE(&dev->rx, cons + slots);
id = rsp->id;
BUG_ON(id >= NET_RX_RING_SIZE);
size = rsp->status;
flags = rsp->flags;
slots++;
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
if (likely(dev->netif_rx_pbuf && (!drop))) {
/* set tot_len */
p->tot_len = realsize;
realsize -= p->len;
/* ..and link it to next pbuf */
p->next = &dev->rx_buffers[id]->cpbuf.pbuf;
dev->pbuf_cur = p = p->next;
} else {
netfront_release_rxbuffer(dev->rx_buffers[id], dev);
}
#endif
dprintk("rx: ring: len %d %s %s\n", size,
(flags & NETRXF_more_data ? "(more true) ": ""),
(drop ? "DROP" : ""));
}
BUG_ON(slots > dev->rx.sring->rsp_prod - dev->rx.rsp_cons);
dev->rx.rsp_cons = cons + slots;
if (unlikely(drop))
goto err_drop;
#ifdef HAVE_LWIP
if (likely(dev->netif_rx_pbuf)) {
#if ETH_PAD_SIZE
pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif /* ETH_PAD_SIZE */
if (first_p->ref != 1)
printk("first_p->ref = %u\n", first_p->ref);
dev->netif_rx_pbuf(first_p, dev->netif_rx_arg);
}
#endif /* HAVE_LWIP */
return 1;
err_drop:
dprintk(" rx: dropped\n");
#ifdef HAVE_LWIP
if (first_p) {
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
pbuf_free(first_p);
#else /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
struct pbuf *next;
/* unchain pbuf and release */
p = first_p;
while (p != NULL) {
next = p->next;
p->tot_len = p->len;
p->next = NULL;
netfront_free_rxpbuf(p);
p = next;
}
#endif /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
}
if (likely(dev->netif_rx_pbuf))
dev->netif_rx_pbuf(NULL, dev->netif_rx_arg); /* notify drop */
#endif
return 0;
}
static void netfront_fillup_rx_buffers(struct netfront_dev *dev)
{
RING_IDX prod;
struct netif_rx_request *req;
grant_ref_t ref;
unsigned short id;
int notify;
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
struct net_rxbuffer* buf;
int flags;
#endif
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
local_irq_save(flags);
#endif
/* fill-up slots again */
for (prod = dev->rx.req_prod_pvt;
prod - dev->rx.rsp_cons < NET_RX_RING_SIZE;
prod++) {
id = netfront_rxidx(prod);
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
buf = netfront_get_rxbuffer(dev);
if (buf == NULL)
break; /* out of rx buffers */
BUG_ON(buf->page == NULL);
ref = gnttab_grant_access(dev->dom,virt_to_mfn(buf->page),0);
buf->gref = ref;
BUG_ON(ref == GRANT_INVALID_REF);
dev->rx_buffers[id] = buf;
#else
ref = dev->rx_buffers[id].gref;
#endif
req = RING_GET_REQUEST(&dev->rx, prod);
req->id = id;
req->gref = ref;
}
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
local_irq_restore(flags);
#endif
if (dev->rx.req_prod_pvt != prod) {
dev->rx.req_prod_pvt = prod;
wmb();
RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->rx, notify);
#ifdef CONFIG_SELECT_POLL
files[dev->fd].read = 0;
#endif
if (notify)
notify_remote_via_evtchn(dev->rx_evtchn);
}
}
void netfront_rx(struct netfront_dev *dev)
{
RING_IDX rp, cons;
struct netif_rx_response *rsp = &(dev->rsp);
int more, flags;
local_irq_save(flags);
netfront_tx_buf_gc(dev);
local_irq_restore(flags);
#ifdef CONFIG_NETMAP
if (dev->netmap) {
netmap_netfront_rx(dev);
return;
}
#endif
moretodo:
rp = dev->rx.sring->rsp_prod;
rmb(); /* Ensure we see queued responses up to 'rp'. */
cons = dev->rx.rsp_cons;
while (cons != rp) {
NETIF_MEMCPY(rsp, RING_GET_RESPONSE(&dev->rx, cons), sizeof(*rsp));
netfront_get_responses(dev, cons);
cons = dev->rx.rsp_cons;
}
dev->rx.rsp_cons = cons;
RING_FINAL_CHECK_FOR_RESPONSES(&dev->rx, more);
if(more)
goto moretodo;
netfront_fillup_rx_buffers(dev);
}
void netfront_rx_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
struct netfront_dev *dev = data;
int fd __maybe_unused = dev->fd;
#ifdef CONFIG_SELECT_POLL
if (fd != -1)
files[fd].read = 1;
wake_up(&netfront_queue);
#endif
}
void netfront_tx_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
#ifdef CONFIG_NETFRONT_WAITFORTX
wake_up(&netfront_txqueue);
#endif
}
void netfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
netfront_tx_handler(port, regs, data);
netfront_rx_handler(port, regs, data);
}
#ifdef HAVE_LIBC
void netfront_select_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
{
struct netfront_dev *dev = data;
int fd = dev->fd;
if (fd != -1)
files[fd].read = 1;
wake_up(&netfront_queue);
}
int netfront_tap_open(char *nodename)
{
struct netfront_dev *dev;
dev = init_netfront(nodename, NETIF_SELECT_RX, NULL, NULL);
if (!dev) {
printk("TAP open failed\n");
errno = EIO;
return -1;
}
dev->fd = alloc_fd(FTYPE_TAP);
printk("tap_open(%s) -> %d\n", nodename, dev->fd);
files[dev->fd].tap.dev = dev;
return dev->fd;
}
#ifndef CONFIG_NETFRONT_LWIP_ONLY
ssize_t netfront_receive(struct netfront_dev *dev, unsigned char *data,
size_t len)
{
unsigned long flags;
int fd = dev->fd;
ASSERT(current == main_thread);
dev->rlen = 0;
dev->data = data;
dev->len = len;
local_irq_save(flags);
netfront_rx(dev);
if (!dev->rlen && fd != -1)
/* No data for us, make select stop returning */
files[fd].read = 0;
/* Before re-enabling the interrupts, in case a packet just arrived in the
* meanwhile. */
local_irq_restore(flags);
dev->data = NULL;
dev->len = 0;
return dev->rlen;
}
#endif
#endif
#ifdef CONFIG_NETFRONT_LWIP_ONLY
static
#endif
void netfront_set_rx_handler(struct netfront_dev *dev,
void (*thenetif_rx)(unsigned char *data, int len,
void *arg),
void *arg)
{
if (dev->netif_rx && dev->netif_rx != netif_rx)
printk("Replacing netif_rx handler for dev %s\n", dev->nodename);
dev->netif_rx = thenetif_rx;
dev->netif_rx_arg = arg;
}
static void netfront_tx_buf_gc(struct netfront_dev *dev)
{
RING_IDX cons, prod;
unsigned short id;
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
struct net_txbuffer *buf;
#endif
do {
prod = dev->tx.sring->rsp_prod;
rmb(); /* Ensure we see responses up to 'rp'. */
for (cons = dev->tx.rsp_cons; cons != prod; cons++) {
struct netif_tx_response *txrsp;
txrsp = RING_GET_RESPONSE(&dev->tx, cons);
if (txrsp->status == NETIF_RSP_NULL)
continue;
if (txrsp->status == NETIF_RSP_DROPPED)
printk("netif drop for tx\n");
if (txrsp->status == NETIF_RSP_ERROR)
printk("netif error for tx\n");
id = txrsp->id;
BUG_ON(id >= NET_TX_RING_SIZE);
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
buf = &dev->tx_buffers[id];
gnttab_end_access(buf->gref);
buf->gref = GRANT_INVALID_REF;
#ifdef HAVE_LWIP
if (buf->pbuf) {
pbuf_free(buf->pbuf);
buf->pbuf = NULL;
}
#endif /* HAVE_LWIP */
#endif /* CONFIG_NETFRONT_PERSISTENT_GRANTS */
add_id_to_freelist(id, dev->tx_freelist);
up(&dev->tx_sem);
}
dev->tx.rsp_cons = prod;
/*
* Set a new event, then check for race with update of tx_cons.
* Note that it is essential to schedule a callback, no matter
* how few tx_buffers are pending. Even if there is space in the
* transmit ring, higher layers may be blocked because too much
* data is outstanding: in such cases notification from Xen is
* likely to be the only kick that we'll get.
*/
dev->tx.sring->rsp_event =
prod + ((dev->tx.sring->req_prod - prod) >> 1) + 1;
mb();
} while ((cons == prod) && (prod != dev->tx.sring->rsp_prod));
}
/*
* Gets a free TX request for copying data to backend
*/
static inline struct netif_tx_request *netfront_get_page(struct netfront_dev *dev)
{
struct netif_tx_request *tx;
unsigned short id;
struct net_txbuffer* buf;
int flags;
local_irq_save(flags);
if (unlikely(!trydown(&dev->tx_sem))) {
local_irq_restore(flags);
return NULL; /* we run out of available pages */
}
id = get_id_from_freelist(dev->tx_freelist);
buf = &dev->tx_buffers[id];
local_irq_restore(flags);
tx = RING_GET_REQUEST(&dev->tx, dev->tx.req_prod_pvt++);
tx->offset = 0;
tx->size = 0;
tx->id = id;
tx->flags = 0;
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
tx->gref = buf->gref;
#else
tx->gref = buf->gref = GRANT_INVALID_REF;
#endif
#ifdef HAVE_LWIP
buf->pbuf = NULL;
#endif
return tx;
}
#define netfront_tx_available(dev, slots) \
(((dev)->tx.req_prod_pvt - (dev)->tx.rsp_cons) < (NET_TX_RING_SIZE - (slots)))
#define netfront_tx_possible(dev, slots) \
(0 < (NET_TX_RING_SIZE - (slots)))
static inline void netfront_xmit_notify(struct netfront_dev *dev)
{
int notify;
/* So that backend sees new requests and check notify */
RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->tx, notify);
if (notify)
notify_remote_via_evtchn(dev->tx_evtchn);
}
#ifndef CONFIG_NETFRONT_LWIP_ONLY
/**
* Transmit function for raw buffers (non-GSO/TCO)
*/
void netfront_xmit(struct netfront_dev *dev, unsigned char *data, int len)
{
int flags;
struct netif_tx_request *tx;
struct net_txbuffer* buf;
void* page;
#ifdef CONFIG_NETMAP
if (dev->netmap) {
netmap_netfront_xmit(dev->na, data, len);
return;
}
#endif
BUG_ON(len > PAGE_SIZE);
if (!netfront_tx_available(dev, 1))
goto out;
tx = netfront_get_page(dev);
ASSERT(tx != NULL);
buf = &dev->tx_buffers[tx->id];
page = buf->page;
#ifndef CONFIG_NETFRONT_PERSISTENT_GRANTS
tx->gref = buf->gref = gnttab_grant_access(dev->dom,
virt_to_mfn(page), 0);
BUG_ON(tx->gref == GRANT_INVALID_REF);
#endif
NETIF_MEMCPY(page, data, len);
tx->flags |= (NETTXF_data_validated);
tx->size = len;
#ifdef CONFIG_NETFRONT_STATS
++dev->txpkts;
dev->txbytes += len;
#endif
netfront_xmit_notify(dev);
dprintk("tx: raw %d\n", len);
out:
local_irq_save(flags);
netfront_tx_buf_gc(dev);
local_irq_restore(flags);
}
#endif
#ifdef HAVE_LWIP
#ifdef CONFIG_NETFRONT_PERSISTENT_GRANTS
#define netfront_count_pbuf_slots(dev, p) \
DIV_ROUND_UP(((int)(p)->tot_len), PAGE_SIZE);
static inline struct netif_tx_request *netfront_make_txreqs_pgnt(struct netfront_dev *dev,
struct netif_tx_request *tx,
const struct pbuf *p, int *slots)
{
struct netif_tx_request *first_tx = tx;
const struct pbuf *first_p = p;
register unsigned long page_off, page_left;
register unsigned long p_off, p_left;
register unsigned long len;
register unsigned long tot_len;
void *page;
tot_len = 0;
p_off = 0;
p_left = p->len;
page = dev->tx_buffers[tx->id].page;
page_off = tx->offset = 0;
page_left = (PAGE_SIZE) - page_off;
tx->size = 0;
for (;;) {
len = min(page_left, p_left);
dprintk("tx: make_txreqs_pgnt: slot %3u, id %3u: page@%12p, page_off: %4lu page_left: %4lu <-%4lu bytes-- p@%12p, p_off: %4lu, p_left: %4lu\n",
*slots, tx->id, page, page_off, page_left, len, p->payload, p_off, p_left);
NETIF_MEMCPY((void *)(((uintptr_t) page) + page_off),
(void *)(((uintptr_t) p->payload) + p_off),
len);
p_off += len;
p_left -= len;
tx->size += len;
page_off += len;
page_left -= len;
tot_len += len;
if (p_left == 0) {
if (!p->next)
break; /* we are done processing this pbuf chain */
p = p->next;
p_off = 0;