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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
|
/******************************************************************************
* drivers/xen/netback/netback.c
*
* Back-end of the driver for virtual network devices. This portion of the
* driver exports a 'unified' network-device interface that can be accessed
* by any operating system that implements a compatible front end. A
* reference front-end implementation can be found in:
* drivers/xen/netfront/netfront.c
*
* Copyright (c) 2002-2005, K A Fraser
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "common.h"
#include <xen/balloon.h>
#include <xen/interface/memory.h>
/*#define NETBE_DEBUG_INTERRUPT*/
struct netbk_rx_meta {
skb_frag_t frag;
int id;
};
static void netif_idx_release(u16 pending_idx);
static void netif_page_release(struct page *page);
static void make_tx_response(netif_t *netif,
netif_tx_request_t *txp,
s8 st);
static netif_rx_response_t *make_rx_response(netif_t *netif,
u16 id,
s8 st,
u16 offset,
u16 size,
u16 flags);
static void net_tx_action(unsigned long unused);
static DECLARE_TASKLET(net_tx_tasklet, net_tx_action, 0);
static void net_rx_action(unsigned long unused);
static DECLARE_TASKLET(net_rx_tasklet, net_rx_action, 0);
static struct timer_list net_timer;
#define MAX_PENDING_REQS 256
static struct sk_buff_head rx_queue;
static multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
static mmu_update_t rx_mmu[NET_RX_RING_SIZE];
static gnttab_transfer_t grant_rx_op[NET_RX_RING_SIZE];
static unsigned char rx_notify[NR_IRQS];
static unsigned long mmap_vstart;
#define MMAP_VADDR(_req) (mmap_vstart + ((_req) * PAGE_SIZE))
#define PKT_PROT_LEN 64
static struct {
netif_tx_request_t req;
netif_t *netif;
} pending_tx_info[MAX_PENDING_REQS];
static u16 pending_ring[MAX_PENDING_REQS];
typedef unsigned int PEND_RING_IDX;
#define MASK_PEND_IDX(_i) ((_i)&(MAX_PENDING_REQS-1))
static PEND_RING_IDX pending_prod, pending_cons;
#define NR_PENDING_REQS (MAX_PENDING_REQS - pending_prod + pending_cons)
/* Freed TX SKBs get batched on this ring before return to pending_ring. */
static u16 dealloc_ring[MAX_PENDING_REQS];
static PEND_RING_IDX dealloc_prod, dealloc_cons;
static struct sk_buff_head tx_queue;
static grant_handle_t grant_tx_handle[MAX_PENDING_REQS];
static gnttab_unmap_grant_ref_t tx_unmap_ops[MAX_PENDING_REQS];
static gnttab_map_grant_ref_t tx_map_ops[MAX_PENDING_REQS];
static struct list_head net_schedule_list;
static spinlock_t net_schedule_list_lock;
#define MAX_MFN_ALLOC 64
static unsigned long mfn_list[MAX_MFN_ALLOC];
static unsigned int alloc_index = 0;
static inline unsigned long alloc_mfn(void)
{
return mfn_list[--alloc_index];
}
static int check_mfn(int nr)
{
struct xen_memory_reservation reservation = {
.extent_order = 0,
.domid = DOMID_SELF
};
if (likely(alloc_index >= nr))
return 0;
set_xen_guest_handle(reservation.extent_start, mfn_list + alloc_index);
reservation.nr_extents = MAX_MFN_ALLOC - alloc_index;
alloc_index += HYPERVISOR_memory_op(XENMEM_increase_reservation,
&reservation);
return alloc_index >= nr ? 0 : -ENOMEM;
}
static inline void maybe_schedule_tx_action(void)
{
smp_mb();
if ((NR_PENDING_REQS < (MAX_PENDING_REQS/2)) &&
!list_empty(&net_schedule_list))
tasklet_schedule(&net_tx_tasklet);
}
/*
* A gross way of confirming the origin of an skb data page. The slab
* allocator abuses a field in the page struct to cache the kmem_cache_t ptr.
*/
static inline int is_xen_skb(struct sk_buff *skb)
{
extern kmem_cache_t *skbuff_cachep;
kmem_cache_t *cp = (kmem_cache_t *)virt_to_page(skb->head)->lru.next;
return (cp == skbuff_cachep);
}
static struct sk_buff *netbk_copy_skb(struct sk_buff *skb)
{
struct skb_shared_info *ninfo;
struct sk_buff *nskb;
unsigned long offset;
int ret;
int len;
int headlen;
nskb = alloc_skb(SKB_MAX_HEAD(0), GFP_ATOMIC);
if (unlikely(!nskb))
goto err;
skb_reserve(nskb, 16);
headlen = nskb->end - nskb->data;
if (headlen > skb_headlen(skb))
headlen = skb_headlen(skb);
ret = skb_copy_bits(skb, 0, __skb_put(nskb, headlen), headlen);
BUG_ON(ret);
ninfo = skb_shinfo(nskb);
ninfo->gso_size = skb_shinfo(skb)->gso_size;
ninfo->gso_type = skb_shinfo(skb)->gso_type;
offset = headlen;
len = skb->len - headlen;
nskb->len = skb->len;
nskb->data_len = len;
nskb->truesize += len;
while (len) {
struct page *page;
int copy;
int zero;
if (unlikely(ninfo->nr_frags >= MAX_SKB_FRAGS)) {
dump_stack();
goto err_free;
}
copy = len >= PAGE_SIZE ? PAGE_SIZE : len;
zero = len >= PAGE_SIZE ? 0 : __GFP_ZERO;
page = alloc_page(GFP_ATOMIC | zero);
if (unlikely(!page))
goto err_free;
ret = skb_copy_bits(skb, offset, page_address(page), copy);
BUG_ON(ret);
ninfo->frags[ninfo->nr_frags].page = page;
ninfo->frags[ninfo->nr_frags].page_offset = 0;
ninfo->frags[ninfo->nr_frags].size = copy;
ninfo->nr_frags++;
offset += copy;
len -= copy;
}
offset = nskb->data - skb->data;
nskb->h.raw = skb->h.raw + offset;
nskb->nh.raw = skb->nh.raw + offset;
nskb->mac.raw = skb->mac.raw + offset;
return nskb;
err_free:
kfree_skb(nskb);
err:
return NULL;
}
static inline int netbk_max_required_rx_slots(netif_t *netif)
{
if (netif->features & (NETIF_F_SG|NETIF_F_TSO))
return MAX_SKB_FRAGS + 2; /* header + extra_info + frags */
return 1; /* all in one */
}
static inline int netbk_queue_full(netif_t *netif)
{
RING_IDX peek = netif->rx_req_cons_peek;
RING_IDX needed = netbk_max_required_rx_slots(netif);
return ((netif->rx.sring->req_prod - peek) < needed) ||
((netif->rx.rsp_prod_pvt + NET_RX_RING_SIZE - peek) < needed);
}
int netif_be_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
netif_t *netif = netdev_priv(dev);
BUG_ON(skb->dev != dev);
/* Drop the packet if the target domain has no receive buffers. */
if (unlikely(!netif_running(dev) || !netif_carrier_ok(dev)))
goto drop;
if (unlikely(netbk_queue_full(netif))) {
/* Not a BUG_ON() -- misbehaving netfront can trigger this. */
if (netbk_can_queue(dev))
DPRINTK("Queue full but not stopped!\n");
goto drop;
}
/*
* We do not copy the packet unless:
* 1. The data is shared; or
* 2. The data is not allocated from our special cache.
* 3. The data is fragmented.
*/
if (skb_cloned(skb) || skb_is_nonlinear(skb) || !is_xen_skb(skb)) {
struct sk_buff *nskb = netbk_copy_skb(skb);
if ( unlikely(nskb == NULL) )
goto drop;
/* Copy only the header fields we use in this driver. */
nskb->dev = skb->dev;
nskb->ip_summed = skb->ip_summed;
nskb->proto_data_valid = skb->proto_data_valid;
dev_kfree_skb(skb);
skb = nskb;
}
netif->rx_req_cons_peek += skb_shinfo(skb)->nr_frags + 1 +
!!skb_shinfo(skb)->gso_size;
netif_get(netif);
if (netbk_can_queue(dev) && netbk_queue_full(netif)) {
netif->rx.sring->req_event = netif->rx_req_cons_peek +
netbk_max_required_rx_slots(netif);
mb(); /* request notification /then/ check & stop the queue */
if (netbk_queue_full(netif))
netif_stop_queue(dev);
}
skb_queue_tail(&rx_queue, skb);
tasklet_schedule(&net_rx_tasklet);
return 0;
drop:
netif->stats.tx_dropped++;
dev_kfree_skb(skb);
return 0;
}
#if 0
static void xen_network_done_notify(void)
{
static struct net_device *eth0_dev = NULL;
if (unlikely(eth0_dev == NULL))
eth0_dev = __dev_get_by_name("eth0");
netif_rx_schedule(eth0_dev);
}
/*
* Add following to poll() function in NAPI driver (Tigon3 is example):
* if ( xen_network_done() )
* tg3_enable_ints(tp);
*/
int xen_network_done(void)
{
return skb_queue_empty(&rx_queue);
}
#endif
static u16 netbk_gop_frag(netif_t *netif, struct page *page, int count, int i)
{
multicall_entry_t *mcl = rx_mcl + count;
mmu_update_t *mmu = rx_mmu + count;
gnttab_transfer_t *gop = grant_rx_op + count;
netif_rx_request_t *req;
unsigned long old_mfn, new_mfn;
old_mfn = virt_to_mfn(page_address(page));
if (!xen_feature(XENFEAT_auto_translated_physmap)) {
new_mfn = alloc_mfn();
/*
* Set the new P2M table entry before reassigning
* the old data page. Heed the comment in
* pgtable-2level.h:pte_page(). :-)
*/
set_phys_to_machine(page_to_pfn(page), new_mfn);
MULTI_update_va_mapping(mcl, (unsigned long)page_address(page),
pfn_pte_ma(new_mfn, PAGE_KERNEL), 0);
mmu->ptr = ((maddr_t)new_mfn << PAGE_SHIFT) |
MMU_MACHPHYS_UPDATE;
mmu->val = page_to_pfn(page);
}
req = RING_GET_REQUEST(&netif->rx, netif->rx.req_cons + i);
gop->mfn = old_mfn;
gop->domid = netif->domid;
gop->ref = req->gref;
return req->id;
}
static void netbk_gop_skb(struct sk_buff *skb, struct netbk_rx_meta *meta,
int count)
{
netif_t *netif = netdev_priv(skb->dev);
int nr_frags = skb_shinfo(skb)->nr_frags;
int i;
int extra;
meta[count].frag.page_offset = skb_shinfo(skb)->gso_type;
meta[count].frag.size = skb_shinfo(skb)->gso_size;
extra = !!meta[count].frag.size + 1;
for (i = 0; i < nr_frags; i++) {
meta[++count].frag = skb_shinfo(skb)->frags[i];
meta[count].id = netbk_gop_frag(netif, meta[count].frag.page,
count, i + extra);
}
/*
* This must occur at the end to ensure that we don't trash
* skb_shinfo until we're done.
*/
meta[count - nr_frags].id = netbk_gop_frag(netif,
virt_to_page(skb->data),
count - nr_frags, 0);
netif->rx.req_cons += nr_frags + extra;
}
static inline void netbk_free_pages(int nr_frags, struct netbk_rx_meta *meta)
{
int i;
for (i = 0; i < nr_frags; i++)
put_page(meta[i].frag.page);
}
static int netbk_check_gop(int nr_frags, domid_t domid, int count)
{
multicall_entry_t *mcl = rx_mcl + count;
gnttab_transfer_t *gop = grant_rx_op + count;
int status = NETIF_RSP_OKAY;
int i;
for (i = 0; i <= nr_frags; i++) {
if (!xen_feature(XENFEAT_auto_translated_physmap)) {
/* The update_va_mapping() must not fail. */
BUG_ON(mcl->result != 0);
mcl++;
}
/* Check the reassignment error code. */
if (gop->status != 0) {
DPRINTK("Bad status %d from grant transfer to DOM%u\n",
gop->status, domid);
/*
* Page no longer belongs to us unless GNTST_bad_page,
* but that should be a fatal error anyway.
*/
BUG_ON(gop->status == GNTST_bad_page);
status = NETIF_RSP_ERROR;
}
gop++;
}
return status;
}
static void netbk_add_frag_responses(netif_t *netif, int status,
struct netbk_rx_meta *meta, int nr_frags)
{
int i;
for (i = 0; i < nr_frags; i++) {
int id = meta[i].id;
int flags = (i == nr_frags - 1) ? 0 : NETRXF_more_data;
make_rx_response(netif, id, status, meta[i].frag.page_offset,
meta[i].frag.size, flags);
}
}
static void net_rx_action(unsigned long unused)
{
netif_t *netif = NULL;
s8 status;
u16 id, irq, flags;
netif_rx_response_t *resp;
struct netif_extra_info *extra;
multicall_entry_t *mcl;
struct sk_buff_head rxq;
struct sk_buff *skb;
int notify_nr = 0;
int ret;
int nr_frags;
int count;
/*
* Putting hundreds of bytes on the stack is considered rude.
* Static works because a tasklet can only be on one CPU at any time.
*/
static u16 notify_list[NET_RX_RING_SIZE];
static struct netbk_rx_meta meta[NET_RX_RING_SIZE];
skb_queue_head_init(&rxq);
count = 0;
while ((skb = skb_dequeue(&rx_queue)) != NULL) {
nr_frags = skb_shinfo(skb)->nr_frags;
*(int *)skb->cb = nr_frags;
if (!xen_feature(XENFEAT_auto_translated_physmap) &&
check_mfn(nr_frags + 1)) {
/* Memory squeeze? Back off for an arbitrary while. */
if ( net_ratelimit() )
WPRINTK("Memory squeeze in netback "
"driver.\n");
mod_timer(&net_timer, jiffies + HZ);
skb_queue_head(&rx_queue, skb);
break;
}
netbk_gop_skb(skb, meta, count);
count += nr_frags + 1;
__skb_queue_tail(&rxq, skb);
/* Filled the batch queue? */
if (count + MAX_SKB_FRAGS >= NET_RX_RING_SIZE)
break;
}
if (!count)
return;
if (!xen_feature(XENFEAT_auto_translated_physmap)) {
mcl = rx_mcl + count;
mcl[-1].args[MULTI_UVMFLAGS_INDEX] = UVMF_TLB_FLUSH|UVMF_ALL;
mcl->op = __HYPERVISOR_mmu_update;
mcl->args[0] = (unsigned long)rx_mmu;
mcl->args[1] = count;
mcl->args[2] = 0;
mcl->args[3] = DOMID_SELF;
ret = HYPERVISOR_multicall(rx_mcl, count + 1);
BUG_ON(ret != 0);
}
ret = HYPERVISOR_grant_table_op(GNTTABOP_transfer, grant_rx_op, count);
BUG_ON(ret != 0);
count = 0;
while ((skb = __skb_dequeue(&rxq)) != NULL) {
nr_frags = *(int *)skb->cb;
atomic_set(&(skb_shinfo(skb)->dataref), 1);
skb_shinfo(skb)->nr_frags = 0;
skb_shinfo(skb)->frag_list = NULL;
netif = netdev_priv(skb->dev);
netif->stats.tx_bytes += skb->len;
netif->stats.tx_packets++;
netbk_free_pages(nr_frags, meta + count + 1);
status = netbk_check_gop(nr_frags, netif->domid, count);
id = meta[count].id;
flags = nr_frags ? NETRXF_more_data : 0;
if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
flags |= NETRXF_csum_blank | NETRXF_data_validated;
else if (skb->proto_data_valid) /* remote but checksummed? */
flags |= NETRXF_data_validated;
resp = make_rx_response(netif, id, status,
offset_in_page(skb->data),
skb_headlen(skb), flags);
extra = NULL;
if (meta[count].frag.size) {
struct netif_extra_info *gso =
(struct netif_extra_info *)
RING_GET_RESPONSE(&netif->rx,
netif->rx.rsp_prod_pvt++);
if (extra)
extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
else
resp->flags |= NETRXF_extra_info;
gso->u.gso.size = meta[count].frag.size;
gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
gso->u.gso.pad = 0;
gso->u.gso.features = 0;
gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
gso->flags = 0;
extra = gso;
}
netbk_add_frag_responses(netif, status, meta + count + 1,
nr_frags);
RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netif->rx, ret);
irq = netif->irq;
if (ret && !rx_notify[irq]) {
rx_notify[irq] = 1;
notify_list[notify_nr++] = irq;
}
if (netif_queue_stopped(netif->dev) &&
!netbk_queue_full(netif))
netif_wake_queue(netif->dev);
netif_put(netif);
dev_kfree_skb(skb);
count += nr_frags + 1;
}
while (notify_nr != 0) {
irq = notify_list[--notify_nr];
rx_notify[irq] = 0;
notify_remote_via_irq(irq);
}
/* More work to do? */
if (!skb_queue_empty(&rx_queue) && !timer_pending(&net_timer))
tasklet_schedule(&net_rx_tasklet);
#if 0
else
xen_network_done_notify();
#endif
}
static void net_alarm(unsigned long unused)
{
tasklet_schedule(&net_rx_tasklet);
}
struct net_device_stats *netif_be_get_stats(struct net_device *dev)
{
netif_t *netif = netdev_priv(dev);
return &netif->stats;
}
static int __on_net_schedule_list(netif_t *netif)
{
return netif->list.next != NULL;
}
static void remove_from_net_schedule_list(netif_t *netif)
{
spin_lock_irq(&net_schedule_list_lock);
if (likely(__on_net_schedule_list(netif))) {
list_del(&netif->list);
netif->list.next = NULL;
netif_put(netif);
}
spin_unlock_irq(&net_schedule_list_lock);
}
static void add_to_net_schedule_list_tail(netif_t *netif)
{
if (__on_net_schedule_list(netif))
return;
spin_lock_irq(&net_schedule_list_lock);
if (!__on_net_schedule_list(netif) &&
likely(netif_running(netif->dev) &&
netif_carrier_ok(netif->dev))) {
list_add_tail(&netif->list, &net_schedule_list);
netif_get(netif);
}
spin_unlock_irq(&net_schedule_list_lock);
}
/*
* Note on CONFIG_XEN_NETDEV_PIPELINED_TRANSMITTER:
* If this driver is pipelining transmit requests then we can be very
* aggressive in avoiding new-packet notifications -- frontend only needs to
* send a notification if there are no outstanding unreceived responses.
* If we may be buffer transmit buffers for any reason then we must be rather
* more conservative and treat this as the final check for pending work.
*/
void netif_schedule_work(netif_t *netif)
{
int more_to_do;
#ifdef CONFIG_XEN_NETDEV_PIPELINED_TRANSMITTER
more_to_do = RING_HAS_UNCONSUMED_REQUESTS(&netif->tx);
#else
RING_FINAL_CHECK_FOR_REQUESTS(&netif->tx, more_to_do);
#endif
if (more_to_do) {
add_to_net_schedule_list_tail(netif);
maybe_schedule_tx_action();
}
}
void netif_deschedule_work(netif_t *netif)
{
remove_from_net_schedule_list(netif);
}
static void tx_credit_callback(unsigned long data)
{
netif_t *netif = (netif_t *)data;
netif->remaining_credit = netif->credit_bytes;
netif_schedule_work(netif);
}
inline static void net_tx_action_dealloc(void)
{
gnttab_unmap_grant_ref_t *gop;
u16 pending_idx;
PEND_RING_IDX dc, dp;
netif_t *netif;
int ret;
dc = dealloc_cons;
dp = dealloc_prod;
/* Ensure we see all indexes enqueued by netif_idx_release(). */
smp_rmb();
/*
* Free up any grants we have finished using
*/
gop = tx_unmap_ops;
while (dc != dp) {
pending_idx = dealloc_ring[MASK_PEND_IDX(dc++)];
gnttab_set_unmap_op(gop, MMAP_VADDR(pending_idx),
GNTMAP_host_map,
grant_tx_handle[pending_idx]);
gop++;
}
ret = HYPERVISOR_grant_table_op(
GNTTABOP_unmap_grant_ref, tx_unmap_ops, gop - tx_unmap_ops);
BUG_ON(ret);
while (dealloc_cons != dp) {
pending_idx = dealloc_ring[MASK_PEND_IDX(dealloc_cons++)];
netif = pending_tx_info[pending_idx].netif;
make_tx_response(netif, &pending_tx_info[pending_idx].req,
NETIF_RSP_OKAY);
pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx;
netif_put(netif);
}
}
static void netbk_tx_err(netif_t *netif, netif_tx_request_t *txp, RING_IDX end)
{
RING_IDX cons = netif->tx.req_cons;
do {
make_tx_response(netif, txp, NETIF_RSP_ERROR);
if (cons >= end)
break;
txp = RING_GET_REQUEST(&netif->tx, cons++);
} while (1);
netif->tx.req_cons = cons;
netif_schedule_work(netif);
netif_put(netif);
}
static int netbk_count_requests(netif_t *netif, netif_tx_request_t *txp,
int work_to_do)
{
netif_tx_request_t *first = txp;
RING_IDX cons = netif->tx.req_cons;
int frags = 0;
while (txp->flags & NETTXF_more_data) {
if (frags >= work_to_do) {
DPRINTK("Need more frags\n");
return -frags;
}
txp = RING_GET_REQUEST(&netif->tx, cons + frags);
if (txp->size > first->size) {
DPRINTK("Frags galore\n");
return -frags;
}
first->size -= txp->size;
frags++;
if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
DPRINTK("txp->offset: %x, size: %u\n",
txp->offset, txp->size);
return -frags;
}
}
return frags;
}
static gnttab_map_grant_ref_t *netbk_get_requests(netif_t *netif,
struct sk_buff *skb,
gnttab_map_grant_ref_t *mop)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
skb_frag_t *frags = shinfo->frags;
netif_tx_request_t *txp;
unsigned long pending_idx = *((u16 *)skb->data);
RING_IDX cons = netif->tx.req_cons;
int i, start;
/* Skip first skb fragment if it is on same page as header fragment. */
start = ((unsigned long)shinfo->frags[0].page == pending_idx);
for (i = start; i < shinfo->nr_frags; i++) {
txp = RING_GET_REQUEST(&netif->tx, cons++);
pending_idx = pending_ring[MASK_PEND_IDX(pending_cons++)];
gnttab_set_map_op(mop++, MMAP_VADDR(pending_idx),
GNTMAP_host_map | GNTMAP_readonly,
txp->gref, netif->domid);
memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
netif_get(netif);
pending_tx_info[pending_idx].netif = netif;
frags[i].page = (void *)pending_idx;
}
return mop;
}
static int netbk_tx_check_mop(struct sk_buff *skb,
gnttab_map_grant_ref_t **mopp)
{
gnttab_map_grant_ref_t *mop = *mopp;
int pending_idx = *((u16 *)skb->data);
netif_t *netif = pending_tx_info[pending_idx].netif;
netif_tx_request_t *txp;
struct skb_shared_info *shinfo = skb_shinfo(skb);
int nr_frags = shinfo->nr_frags;
int i, err, start;
/* Check status of header. */
err = mop->status;
if (unlikely(err)) {
txp = &pending_tx_info[pending_idx].req;
make_tx_response(netif, txp, NETIF_RSP_ERROR);
pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx;
netif_put(netif);
} else {
set_phys_to_machine(
__pa(MMAP_VADDR(pending_idx)) >> PAGE_SHIFT,
FOREIGN_FRAME(mop->dev_bus_addr >> PAGE_SHIFT));
grant_tx_handle[pending_idx] = mop->handle;
}
/* Skip first skb fragment if it is on same page as header fragment. */
start = ((unsigned long)shinfo->frags[0].page == pending_idx);
for (i = start; i < nr_frags; i++) {
int j, newerr;
pending_idx = (unsigned long)shinfo->frags[i].page;
/* Check error status: if okay then remember grant handle. */
newerr = (++mop)->status;
if (likely(!newerr)) {
set_phys_to_machine(
__pa(MMAP_VADDR(pending_idx))>>PAGE_SHIFT,
FOREIGN_FRAME(mop->dev_bus_addr>>PAGE_SHIFT));
grant_tx_handle[pending_idx] = mop->handle;
/* Had a previous error? Invalidate this fragment. */
if (unlikely(err))
netif_idx_release(pending_idx);
continue;
}
/* Error on this fragment: respond to client with an error. */
txp = &pending_tx_info[pending_idx].req;
make_tx_response(netif, txp, NETIF_RSP_ERROR);
pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx;
netif_put(netif);
/* Not the first error? Preceding frags already invalidated. */
if (err)
continue;
/* First error: invalidate header and preceding fragments. */
pending_idx = *((u16 *)skb->data);
netif_idx_release(pending_idx);
for (j = start; j < i; j++) {
pending_idx = (unsigned long)shinfo->frags[i].page;
netif_idx_release(pending_idx);
}
/* Remember the error: invalidate all subsequent fragments. */
err = newerr;
}
*mopp = mop + 1;
return err;
}
static void netbk_fill_frags(struct sk_buff *skb)
{
struct skb_shared_info *shinfo = skb_shinfo(skb);
int nr_frags = shinfo->nr_frags;
int i;
for (i = 0; i < nr_frags; i++) {
|