-
Notifications
You must be signed in to change notification settings - Fork 4
/
linux-2.6.32-754.30.2.patch
1617 lines (1526 loc) · 78.3 KB
/
linux-2.6.32-754.30.2.patch
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
From 96e4014acdf532853d1218599400af6e81c316f5 Mon Sep 17 00:00:00 2001
From: Dongli Zhang <[email protected]>
Date: Fri, 14 Aug 2020 13:58:42 -0700
Subject: [PATCH 1/1] linux-2.6.32-754.30.2
linux-2.6.32-754.30.2
Signed-off-by: Dongli Zhang <[email protected]>
---
drivers/net/virtio_net.c | 265 ++++++++++++++++++++++
drivers/virtio/virtio.c | 3 +
drivers/virtio/virtio_ring.c | 428 +++++++++++++++++++++++++++++++++++
include/linux/irq.h | 57 +++++
include/linux/netdevice.h | 19 ++
include/linux/virtio.h | 11 +
include/linux/virtio_pci.h | 6 +
include/linux/virtio_ring.h | 105 +++++++++
kernel/irq/chip.c | 15 ++
kernel/irq/spurious.c | 9 +
net/core/dev.c | 14 ++
11 files changed, 932 insertions(+)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index a887b152..b02c006a 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -74,6 +74,26 @@ struct receive_queue {
struct napi_struct napi;
/* Number of input buffers, and max we've ever had. */
+ /*
+ * 使用receive_queue->max的地方:
+ * - drivers/net/virtio_net.c|625| <<try_fill_recv>> if (unlikely(rq->num > rq->max))
+ * - drivers/net/virtio_net.c|626| <<try_fill_recv>> rq->max = rq->num;
+ * - drivers/net/virtio_net.c|697| <<virtnet_poll>> if (rq->num < rq->max / 2) {
+ *
+ *
+ * 在以下修改receive_queue->num:
+ * - drivers/net/virtio_net.c|442| <<receive_mergeable>> --rq->num;
+ * - drivers/net/virtio_net.c|457| <<receive_mergeable>> --rq->num;
+ * - drivers/net/virtio_net.c|691| <<try_fill_recv>> ++rq->num;
+ * - drivers/net/virtio_net.c|787| <<virtnet_poll>> --rq->num;
+ * - drivers/net/virtio_net.c|1570| <<free_unused_bufs>> --vi->rq[i].num;
+ * 在以下使用receive_queue->num:
+ * - drivers/net/virtio_net.c|699| <<try_fill_recv>> if (unlikely(rq->num > rq->max))
+ * - drivers/net/virtio_net.c|700| <<try_fill_recv>> rq->max = rq->num;
+ * - drivers/net/virtio_net.c|797| <<virtnet_poll>> if (rq->num < rq->max / 2) {
+ * - drivers/net/virtio_net.c|1572| <<free_unused_bufs>> BUG_ON(vi->rq[i].num != 0);
+ * - drivers/net/virtio_net.c|1864| <<virtnet_probe>> if (vi->rq[i].num == 0) {
+ */
unsigned int num, max;
/* Chain pages by the private ptr. */
@@ -105,12 +125,37 @@ struct virtnet_info
u16 max_queue_pairs;
/* # of queue pairs currently used by the driver */
+ /*
+ * 在以下修改virtnet_info->curr_queue_pairs:
+ * - drivers/net/virtio_net.c|1023| <<virtnet_set_queues>> vi->curr_queue_pairs = queue_pairs;
+ * - drivers/net/virtio_net.c|1701| <<virtnet_probe>> vi->curr_queue_pairs = 1;
+ * 在以下使用virtnet_info->curr_queue_pairs:
+ * - drivers/net/virtio_net.c|651| <<refill_work>> for (i = 0; i < vi->curr_queue_pairs; i++) {
+ * - drivers/net/virtio_net.c|708| <<virtnet_open>> if (i < vi->curr_queue_pairs)
+ * - drivers/net/virtio_net.c|876| <<virtnet_netpoll>> for (i = 0; i < vi->curr_queue_pairs; i++)
+ * - drivers/net/virtio_net.c|1164| <<virtnet_clean_affinity>> ++i % vi->curr_queue_pairs;
+ * - drivers/net/virtio_net.c|1178| <<virtnet_set_affinity>> if (vi->curr_queue_pairs == 1 ||
+ * - drivers/net/virtio_net.c|1251| <<virtnet_get_channels>> channels->combined_count = vi->curr_queue_pairs;
+ * - drivers/net/virtio_net.c|1719| <<virtnet_probe>> for (i = 0; i < vi->curr_queue_pairs; i++) {
+ * - drivers/net/virtio_net.c|1852| <<virtnet_restore>> for (i = 0; i < vi->curr_queue_pairs; i++)
+ * - drivers/net/virtio_net.c|1861| <<virtnet_restore>> virtnet_set_queues(vi, vi->curr_queue_pairs);
+ */
u16 curr_queue_pairs;
/* I like... big packets and I cannot lie! */
bool big_packets;
/* Host will merge rx buffers for big packets (shake it! shake it!) */
+ /*
+ * 在以下使用virtnet_info->mergeable_rx_bufs:
+ * - drivers/net/virtio_net.c|267| <<page_to_skb>> if (vi->mergeable_rx_bufs) {
+ * - drivers/net/virtio_net.c|397| <<receive_buf>> if (vi->mergeable_rx_bufs || vi->big_packets)
+ * - drivers/net/virtio_net.c|403| <<receive_buf>> if (vi->mergeable_rx_bufs)
+ * - drivers/net/virtio_net.c|582| <<try_fill_recv>> if (vi->mergeable_rx_bufs)
+ * - drivers/net/virtio_net.c|761| <<xmit_skb>> if (vi->mergeable_rx_bufs)
+ * - drivers/net/virtio_net.c|1380| <<free_unused_bufs>> if (vi->mergeable_rx_bufs || vi->big_packets)
+ * - drivers/net/virtio_net.c|1636| <<virtnet_probe>> vi->mergeable_rx_bufs = true;
+ */
bool mergeable_rx_bufs;
/* Has control virtqueue */
@@ -123,6 +168,19 @@ struct virtnet_info
struct virtnet_stats __percpu *stats;
/* Work struct for refilling if we run low on memory. */
+ /*
+ * drivers/net/virtio_net.c|742| <<refill_work>> container_of(work, struct virtnet_info, refill.work);
+ 60 drivers/net/virtio_net.c|757| <<refill_work>> schedule_delayed_work(&vi->refill, HZ/2);
+ 61 drivers/net/virtio_net.c|799| <<virtnet_poll>> queue_delayed_work(vi->st_wq, &vi->refill, 0);
+ 62 drivers/net/virtio_net.c|826| <<virtnet_open>> schedule_delayed_work(&vi->refill, 0);
+ 63 drivers/net/virtio_net.c|1162| <<virtnet_set_queues>> schedule_delayed_work(&vi->refill, 0);
+ 64 drivers/net/virtio_net.c|1174| <<virtnet_close>> cancel_delayed_work_sync(&vi->refill);
+ 65 drivers/net/virtio_net.c|1672| <<virtnet_alloc_queues>> INIT_DELAYED_WORK(&vi->refill, refill_work);
+ 66 drivers/net/virtio_net.c|1805| <<virtnet_probe>> INIT_DELAYED_WORK(&vi->refill, refill_work);
+ 67 drivers/net/virtio_net.c|1897| <<virtnet_probe>> cancel_delayed_work_sync(&vi->refill);
+ 68 drivers/net/virtio_net.c|1963| <<virtnet_freeze>> cancel_delayed_work_sync(&vi->refill);
+ * - drivers/net/virtio_net.c|1995| <<virtnet_restore>> queue_delayed_work(vi->st_wq, &vi->refill, 0);
+ */
struct delayed_work refill;
/* Work struct for config space updates */
@@ -182,8 +240,14 @@ static int rxq2vq(int rxq)
return rxq * 2;
}
+/*
+ * 返回共享的共享的char cb[48];
+ */
static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
{
+ /*
+ * 共享的char cb[48];
+ */
return (struct skb_vnet_hdr *)skb->cb;
}
@@ -191,6 +255,19 @@ static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
* private is used to chain pages for big packets, put the whole
* most recent used list in the beginning for reuse
*/
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|357| <<page_to_skb>> give_pages(rq, page);
+ * - drivers/net/virtio_net.c|387| <<receive_big>> give_pages(rq, page);
+ * - drivers/net/virtio_net.c|433| <<receive_mergeable>> give_pages(rq, page);
+ * - drivers/net/virtio_net.c|443| <<receive_mergeable>> give_pages(rq, page);
+ * - drivers/net/virtio_net.c|468| <<receive_buf>> give_pages(rq, buf);
+ * - drivers/net/virtio_net.c|585| <<add_recvbuf_big>> give_pages(rq, list);
+ * - drivers/net/virtio_net.c|597| <<add_recvbuf_big>> give_pages(rq, list);
+ * - drivers/net/virtio_net.c|615| <<add_recvbuf_big>> give_pages(rq, first);
+ * - drivers/net/virtio_net.c|641| <<add_recvbuf_mergeable>> give_pages(rq, page);
+ * - drivers/net/virtio_net.c|1528| <<free_unused_bufs>> give_pages(&vi->rq[i], buf);
+ */
static void give_pages(struct receive_queue *rq, struct page *page)
{
struct page *end;
@@ -201,8 +278,19 @@ static void give_pages(struct receive_queue *rq, struct page *page)
rq->pages = page;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|551| <<add_recvbuf_big>> first = get_a_page(rq, gfp);
+ * - drivers/net/virtio_net.c|564| <<add_recvbuf_big>> first = get_a_page(rq, gfp);
+ * - drivers/net/virtio_net.c|598| <<add_recvbuf_mergeable>> page = get_a_page(rq, gfp);
+ * - drivers/net/virtio_net.c|1477| <<free_receive_bufs>> __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
+ *
+ * 如果receive_queue->pages上有page, 拿出来
+ * 否则分配新的struct page
+ */
static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
{
+ /* struct page *pages; */
struct page *p = rq->pages;
if (p) {
@@ -214,6 +302,10 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
return p;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|1552| <<virtnet_find_vqs>> callbacks[txq2vq(i)] = skb_xmit_done;
+ */
static void skb_xmit_done(struct virtqueue *vq)
{
struct virtnet_info *vi = vq->vdev->priv;
@@ -225,6 +317,11 @@ static void skb_xmit_done(struct virtqueue *vq)
netif_wake_subqueue(vi->dev, vq2txq(vq));
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|320| <<page_to_skb>> set_skb_frag(skb, page, offset, &len);
+ * - drivers/net/virtio_net.c|396| <<receive_mergeable>> set_skb_frag(skb, page, 0, &len);
+ */
static void set_skb_frag(struct sk_buff *skb, struct page *page,
unsigned int offset, unsigned int *len)
{
@@ -244,6 +341,14 @@ static void set_skb_frag(struct sk_buff *skb, struct page *page,
}
/* Called from bottom half context */
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|347| <<receive_big>> struct sk_buff *skb = page_to_skb(rq, page, len);
+ * - drivers/net/virtio_net.c|372| <<receive_mergeable>> struct sk_buff *skb = page_to_skb(rq, page, len);
+ *
+ * page->private可能串着一个或者多个page
+ * 把这些page copy成一个skb
+ */
static struct sk_buff *page_to_skb(struct receive_queue *rq,
struct page *page, unsigned int len)
{
@@ -262,6 +367,9 @@ static struct sk_buff *page_to_skb(struct receive_queue *rq,
skb_reserve(skb, NET_IP_ALIGN);
+ /*
+ * 返回共享的共享的char cb[48];
+ */
hdr = skb_vnet_hdr(skb);
if (vi->mergeable_rx_bufs) {
@@ -326,6 +434,10 @@ err:
return NULL;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|435| <<receive_buf>> skb = receive_mergeable(dev, rq, buf, len);
+ */
static struct sk_buff *receive_mergeable(struct net_device *dev,
struct receive_queue *rq,
void *buf,
@@ -383,6 +495,10 @@ err_buf:
return NULL;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|718| <<virtnet_poll>> receive_buf(rq, buf, len);
+ */
static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
{
struct virtnet_info *vi = rq->vq->vdev->priv;
@@ -547,6 +663,10 @@ static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
return err;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|614| <<try_fill_recv>> err = add_recvbuf_mergeable(rq, gfp);
+ */
static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
{
struct page *page;
@@ -556,6 +676,10 @@ static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
if (!page)
return -ENOMEM;
+ /*
+ * struct receive_queue:
+ * -> struct scatterlist sg[MAX_SKB_FRAGS + 2];
+ */
sg_init_one(rq->sg, page_address(page), PAGE_SIZE);
err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page);
@@ -572,6 +696,14 @@ static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
* before we're receiving packets, or from refill_work which is
* careful to disable receiving (using napi_disable).
*/
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|676| <<refill_work>> still_empty = !try_fill_recv(rq, GFP_KERNEL);
+ * - drivers/net/virtio_net.c|714| <<virtnet_poll>> if (!try_fill_recv(rq, GFP_ATOMIC))
+ * - drivers/net/virtio_net.c|741| <<virtnet_open>> if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
+ * - drivers/net/virtio_net.c|1767| <<virtnet_probe>> try_fill_recv(&vi->rq[i], GFP_KERNEL);
+ * - drivers/net/virtio_net.c|1900| <<virtnet_restore>> if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
+ */
static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
{
struct virtnet_info *vi = rq->vq->vdev->priv;
@@ -589,8 +721,28 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
oom = err == -ENOMEM;
if (err < 0)
break;
+ /*
+ * 在以下修改receive_queue->num:
+ * - drivers/net/virtio_net.c|442| <<receive_mergeable>> --rq->num;
+ * - drivers/net/virtio_net.c|457| <<receive_mergeable>> --rq->num;
+ * - drivers/net/virtio_net.c|691| <<try_fill_recv>> ++rq->num;
+ * - drivers/net/virtio_net.c|787| <<virtnet_poll>> --rq->num;
+ * - drivers/net/virtio_net.c|1570| <<free_unused_bufs>> --vi->rq[i].num;
+ * 在以下使用receive_queue->num:
+ * - drivers/net/virtio_net.c|699| <<try_fill_recv>> if (unlikely(rq->num > rq->max))
+ * - drivers/net/virtio_net.c|700| <<try_fill_recv>> rq->max = rq->num;
+ * - drivers/net/virtio_net.c|797| <<virtnet_poll>> if (rq->num < rq->max / 2) {
+ * - drivers/net/virtio_net.c|1572| <<free_unused_bufs>> BUG_ON(vi->rq[i].num != 0);
+ * - drivers/net/virtio_net.c|1864| <<virtnet_probe>> if (vi->rq[i].num == 0) {
+ */
++rq->num;
} while (err > 0);
+ /*
+ * 使用receive_queue->max的地方:
+ * - drivers/net/virtio_net.c|625| <<try_fill_recv>> if (unlikely(rq->num > rq->max))
+ * - drivers/net/virtio_net.c|626| <<try_fill_recv>> rq->max = rq->num;
+ * - drivers/net/virtio_net.c|697| <<virtnet_poll>> if (rq->num < rq->max / 2) {
+ */
if (unlikely(rq->num > rq->max))
rq->max = rq->num;
virtqueue_kick(rq->vq);
@@ -609,6 +761,12 @@ static void skb_recv_done(struct virtqueue *rvq)
}
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|745| <<refill_work>> virtnet_napi_enable(rq);
+ * - drivers/net/virtio_net.c|811| <<virtnet_open>> virtnet_napi_enable(&vi->rq[i]);
+ * - drivers/net/virtio_net.c|1968| <<virtnet_restore>> virtnet_napi_enable(&vi->rq[i]);
+ */
static void virtnet_napi_enable(struct receive_queue *rq)
{
napi_enable(&rq->napi);
@@ -625,6 +783,11 @@ static void virtnet_napi_enable(struct receive_queue *rq)
}
}
+/*
+ * 在以下使用refill_work():
+ * - drivers/net/virtio_net.c|1672| <<virtnet_alloc_queues>> INIT_DELAYED_WORK(&vi->refill, refill_work);
+ * - drivers/net/virtio_net.c|1805| <<virtnet_probe>> INIT_DELAYED_WORK(&vi->refill, refill_work);
+ */
static void refill_work(struct work_struct *work)
{
struct virtnet_info *vi =
@@ -647,6 +810,10 @@ static void refill_work(struct work_struct *work)
}
}
+/*
+ * 在以下使用virtnet_poll():
+ * - drivers/net/virtio_net.c|1565| <<virtnet_alloc_queues>> netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
+ */
static int virtnet_poll(struct napi_struct *napi, int budget)
{
struct receive_queue *rq =
@@ -656,13 +823,42 @@ static int virtnet_poll(struct napi_struct *napi, int budget)
unsigned int r, len, received = 0;
again:
+ /*
+ * 这里有两种情况 ...
+ * 1. received < budget
+ * 2. received >= budget
+ */
while (received < budget &&
(buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
receive_buf(rq, buf, len);
+ /*
+ * 在以下修改receive_queue->num:
+ * - drivers/net/virtio_net.c|442| <<receive_mergeable>> --rq->num;
+ * - drivers/net/virtio_net.c|457| <<receive_mergeable>> --rq->num;
+ * - drivers/net/virtio_net.c|691| <<try_fill_recv>> ++rq->num;
+ * - drivers/net/virtio_net.c|787| <<virtnet_poll>> --rq->num;
+ * - drivers/net/virtio_net.c|1570| <<free_unused_bufs>> --vi->rq[i].num;
+ * 在以下使用receive_queue->num:
+ * - drivers/net/virtio_net.c|699| <<try_fill_recv>> if (unlikely(rq->num > rq->max))
+ * - drivers/net/virtio_net.c|700| <<try_fill_recv>> rq->max = rq->num;
+ * - drivers/net/virtio_net.c|797| <<virtnet_poll>> if (rq->num < rq->max / 2) {
+ * - drivers/net/virtio_net.c|1572| <<free_unused_bufs>> BUG_ON(vi->rq[i].num != 0);
+ * - drivers/net/virtio_net.c|1864| <<virtnet_probe>> if (vi->rq[i].num == 0) {
+ *
+ * 比如:
+ * num = 213,
+ * max = 256,
+ */
--rq->num;
received++;
}
+ /*
+ * 使用receive_queue->max的地方:
+ * - drivers/net/virtio_net.c|625| <<try_fill_recv>> if (unlikely(rq->num > rq->max))
+ * - drivers/net/virtio_net.c|626| <<try_fill_recv>> rq->max = rq->num;
+ * - drivers/net/virtio_net.c|697| <<virtnet_poll>> if (rq->num < rq->max / 2) {
+ */
if (rq->num < rq->max / 2) {
if (!try_fill_recv(rq, GFP_ATOMIC))
queue_delayed_work(vi->st_wq, &vi->refill, 0);
@@ -699,6 +895,11 @@ static int virtnet_open(struct net_device *dev)
return 0;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|923| <<start_xmit>> free_old_xmit_skbs(sq);
+ * - drivers/net/virtio_net.c|958| <<start_xmit>> capacity += free_old_xmit_skbs(sq);
+ */
static unsigned int free_old_xmit_skbs(struct send_queue *sq)
{
struct sk_buff *skb;
@@ -722,8 +923,17 @@ static unsigned int free_old_xmit_skbs(struct send_queue *sq)
static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
{
+ /*
+ * 返回共享的共享的char cb[48];
+ */
struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
+ /*
+ * struct send_queue:
+ * -> struct virtqueue *vq;
+ * -> struct virtio_device *vdev;
+ * -> void *priv;
+ */
struct virtnet_info *vi = sq->vq->vdev->priv;
pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
@@ -758,12 +968,32 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
hdr->mhdr.num_buffers = 0;
/* Encode metadata header at front. */
+ /*
+ * struct skb_vnet_hdr {
+ * union {
+ * struct virtio_net_hdr hdr;
+ * struct virtio_net_hdr_mrg_rxbuf mhdr;
+ * };
+ * unsigned int num_sg;
+ * };
+ *
+ * struct send_queue:
+ * -> // TX: fragments + linear part + virtio header
+ * -> struct scatterlist sg[MAX_SKB_FRAGS + 2];
+ */
if (vi->mergeable_rx_bufs)
sg_set_buf(sq->sg, &hdr->mhdr, sizeof hdr->mhdr);
else
sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
+ /*
+ * !! 注意最后的+1
+ */
hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
+ /*
+ * 输入是sq->sg[若干个]
+ * 实际是hdr->num_sg个
+ */
return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
0, skb);
}
@@ -838,6 +1068,17 @@ static void virtnet_netpoll(struct net_device *dev)
* supported by the hypervisor, as indicated by feature bits, should
* never fail unless improperly formated.
*/
+/*
+ * called by:
+ * - rivers/net/virtio_net.c|955| <<virtnet_set_mac_address>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
+ * - drivers/net/virtio_net.c|1013| <<virtnet_ack_link_announce>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
+ * - drivers/net/virtio_net.c|1037| <<virtnet_set_queues>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
+ * - drivers/net/virtio_net.c|1087| <<virtnet_set_rx_mode>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
+ * - drivers/net/virtio_net.c|1095| <<virtnet_set_rx_mode>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
+ * - drivers/net/virtio_net.c|1132| <<virtnet_set_rx_mode>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
+ * - drivers/net/virtio_net.c|1147| <<virtnet_vlan_rx_add_vid>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
+ * - drivers/net/virtio_net.c|1159| <<virtnet_vlan_rx_kill_vid>> if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
+ */
static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
struct scatterlist *data, int out, int in)
{
@@ -957,6 +1198,11 @@ static void virtnet_ack_link_announce(struct virtnet_info *vi)
rtnl_unlock();
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|1234| <<virtnet_set_channels>> err = virtnet_set_queues(vi, queue_pairs);
+ * - drivers/net/virtio_net.c|1861| <<virtnet_restore>> virtnet_set_queues(vi, vi->curr_queue_pairs);
+ */
static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
{
struct scatterlist sg;
@@ -1362,6 +1608,11 @@ static void free_receive_bufs(struct virtnet_info *vi)
}
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|1860| <<virtnet_probe>> free_unused_bufs(vi);
+ * - drivers/net/virtio_net.c|1910| <<remove_vq_common>> free_unused_bufs(vi);
+ */
static void free_unused_bufs(struct virtnet_info *vi)
{
void *buf;
@@ -1632,6 +1883,20 @@ static int virtnet_probe(struct virtio_device *vdev)
|| virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
vi->big_packets = true;
+ /*
+ * 在以下使用virtnet_info->mergeable_rx_bufs:
+ * - drivers/net/virtio_net.c|267| <<page_to_skb>> if (vi->mergeable_rx_bufs) {
+ * - drivers/net/virtio_net.c|397| <<receive_buf>> if (vi->mergeable_rx_bufs || vi->big_packets)
+ * - drivers/net/virtio_net.c|403| <<receive_buf>> if (vi->mergeable_rx_bufs)
+ * - drivers/net/virtio_net.c|582| <<try_fill_recv>> if (vi->mergeable_rx_bufs)
+ * - drivers/net/virtio_net.c|761| <<xmit_skb>> if (vi->mergeable_rx_bufs)
+ * - drivers/net/virtio_net.c|1380| <<free_unused_bufs>> if (vi->mergeable_rx_bufs || vi->big_packets)
+ * - drivers/net/virtio_net.c|1636| <<virtnet_probe>> vi->mergeable_rx_bufs = true;
+ *
+ * 例子
+ * # cat /sys/bus/virtio/devices/virtio1/features
+ * 1100011111111111111101110000110000000000000000000000000000000000
+ */
if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
vi->mergeable_rx_bufs = true;
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index a64a087b..5ab9827d 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -47,6 +47,9 @@ static ssize_t features_show(struct device *_d,
len += sprintf(buf+len, "\n");
return len;
}
+/*
+ * struct bus_type virtio_bus.dev_attrs = virtio_dev_attrs[]
+ */
static struct device_attribute virtio_dev_attrs[] = {
__ATTR_RO(device),
__ATTR_RO(vendor),
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 6de0ffae..916ab9bb 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -80,19 +80,74 @@ struct vring_virtqueue
bool broken;
/* Host supports indirect buffers */
+ /*
+ * 在以下使用vring_virtqueue->indirect:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|263| <<virtqueue_add_buf>> if (vq->indirect)
+ * - drivers/virtio/virtio_ring.c|595| <<vring_new_virtqueue>> vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
+ */
bool indirect;
/* Host publishes avail event idx */
bool event;
/* Number of free buffers */
+ /*
+ * 在以下修改vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|150| <<vring_add_indirect>> vq->num_free--;
+ * - drivers/virtio/virtio_ring.c|227| <<virtqueue_add_buf>> vq->num_free -= out + in;
+ * - drivers/virtio/virtio_ring.c|346| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|352| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|603| <<vring_new_virtqueue>> vq->num_free = num;
+ * 在以下使用vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|214| <<virtqueue_add_buf>> if (vq->num_free < out + in) {
+ * - drivers/virtio/virtio_ring.c|216| <<virtqueue_add_buf>> out + in, vq->num_free);
+ * - drivers/virtio/virtio_ring.c|264| <<virtqueue_add_buf>> return vq->num_free ? vq->vring.num : 0;
+ * - drivers/virtio/virtio_ring.c|265| <<virtqueue_add_buf>> return vq->num_free;
+ * - drivers/virtio/virtio_ring.c|532| <<virtqueue_detach_unused_buf>> BUG_ON(vq->num_free != vq->vring.num);
+ */
unsigned int num_free;
/* Head of free buffer list. */
+ /*
+ * 在以下修改vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|159| <<vring_add_indirect>> vq->free_head = vq->vring.desc[head].next;
+ * - drivers/virtio/virtio_ring.c|248| <<virtqueue_add_buf>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|350| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|604| <<vring_new_virtqueue>> vq->free_head = 0;
+ *
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|153| <<vring_add_indirect>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|229| <<virtqueue_add_buf>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|230| <<virtqueue_add_buf>> for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
+ * - drivers/virtio/virtio_ring.c|349| <<detach_buf>> vq->vring.desc[i].next = vq->free_head;
+ */
unsigned int free_head;
/* Number we've added since last sync. */
+ /*
+ * 在以下使用vring_virtqueue->num_added:
+ * - drivers/virtio/virtio_ring.c|373| <<virtqueue_add_buf>> avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
+ * - drivers/virtio/virtio_ring.c|423| <<virtqueue_kick_prepare>> new = vq->vring.avail->idx = old + vq->num_added;
+ * - drivers/virtio/virtio_ring.c|424| <<virtqueue_kick_prepare>> vq->num_added = 0;
+ * - drivers/virtio/virtio_ring.c|889| <<vring_new_virtqueue>> vq->num_added = 0;
+ */
unsigned int num_added;
/* Last used index we've seen. */
+ /*
+ * 在以下修改vring_virtqueue->last_used_idx:
+ * - drivers/virtio/virtio_ring.c|636| <<virtqueue_get_buf>> vq->last_used_idx++;
+ * - drivers/virtio/virtio_ring.c|910| <<vring_new_virtqueue>> vq->last_used_idx = 0;
+ * 在以下使用vring_virtqueue->last_used_idx:
+ * - drivers/virtio/virtio_ring.c|576| <<more_used>> return vq->last_used_idx != vq->vring.used->idx;
+ * - drivers/virtio/virtio_ring.c|621| <<virtqueue_get_buf>> i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
+ * - drivers/virtio/virtio_ring.c|622| <<virtqueue_get_buf>> *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
+ * - drivers/virtio/virtio_ring.c|641| <<virtqueue_get_buf>> vring_used_event(&vq->vring) = vq->last_used_idx;
+ * - drivers/virtio/virtio_ring.c|726| <<virtqueue_enable_cb_prepare>> vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
+ * - drivers/virtio/virtio_ring.c|795| <<virtqueue_enable_cb_delayed>> bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
+ * - drivers/virtio/virtio_ring.c|796| <<virtqueue_enable_cb_delayed>> vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
+ * - drivers/virtio/virtio_ring.c|798| <<virtqueue_enable_cb_delayed>> if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
+ */
u16 last_used_idx;
/* How to notify other side. FIXME: commonalize hcalls! */
@@ -113,6 +168,10 @@ struct vring_virtqueue
#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
/* Set up an indirect table of descriptors and add it to the queue. */
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|206| <<virtqueue_add_buf>> head = vring_add_indirect(vq, sg, out, in);
+ */
static int vring_add_indirect(struct vring_virtqueue *vq,
struct scatterlist sg[],
unsigned int out,
@@ -147,6 +206,24 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
desc[i-1].next = 0;
/* We're about to use a buffer */
+ /*
+ * 在以下修改vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|150| <<vring_add_indirect>> vq->num_free--;
+ * - drivers/virtio/virtio_ring.c|227| <<virtqueue_add_buf>> vq->num_free -= out + in;
+ * - drivers/virtio/virtio_ring.c|346| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|352| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|603| <<vring_new_virtqueue>> vq->num_free = num;
+ *
+ * 在以下使用vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|214| <<virtqueue_add_buf>> if (vq->num_free < out + in) {
+ * - drivers/virtio/virtio_ring.c|216| <<virtqueue_add_buf>> out + in, vq->num_free);
+ * - drivers/virtio/virtio_ring.c|264| <<virtqueue_add_buf>> return vq->num_free ? vq->vring.num : 0;
+ * - drivers/virtio/virtio_ring.c|265| <<virtqueue_add_buf>> return vq->num_free;
+ * - drivers/virtio/virtio_ring.c|532| <<virtqueue_detach_unused_buf>> BUG_ON(vq->num_free != vq->vring.num);
+ *
+ * indirect的时候只需要一个entry (element):
+ */
vq->num_free--;
/* Use a single buffer which doesn't continue */
@@ -156,11 +233,32 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
vq->vring.desc[head].len = i * sizeof(struct vring_desc);
/* Update free pointer */
+ /*
+ * 在以下修改vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|159| <<vring_add_indirect>> vq->free_head = vq->vring.desc[head].next;
+ * - drivers/virtio/virtio_ring.c|248| <<virtqueue_add_buf>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|350| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|604| <<vring_new_virtqueue>> vq->free_head = 0;
+ *
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|153| <<vring_add_indirect>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|229| <<virtqueue_add_buf>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|230| <<virtqueue_add_buf>> for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
+ * - drivers/virtio/virtio_ring.c|349| <<detach_buf>> vq->vring.desc[i].next = vq->free_head;
+ */
vq->free_head = vq->vring.desc[head].next;
return head;
}
+/*
+ * called by:
+ * - drivers/net/virtio_net.c|192| <<vq2txq>> return (virtqueue_get_queue_index(vq) - 1) / 2;
+ * - drivers/net/virtio_net.c|202| <<vq2rxq>> return virtqueue_get_queue_index(vq) / 2;
+ * - drivers/scsi/virtio_scsi.c|236| <<virtscsi_req_done>> int index = virtqueue_get_queue_index(vq) - VIRTIO_SCSI_VQ_BASE;
+ * - drivers/virtio/virtio_pci.c|187| <<vp_notify>> iowrite16(virtqueue_get_queue_index(vq),
+ * - drivers/virtio/virtio_pci.c|456| <<vp_del_vq>> iowrite16(virtqueue_get_queue_index(vq),
+ */
int virtqueue_get_queue_index(struct virtqueue *_vq)
{
struct vring_virtqueue *vq = to_vvq(_vq);
@@ -168,12 +266,34 @@ int virtqueue_get_queue_index(struct virtqueue *_vq)
}
EXPORT_SYMBOL_GPL(virtqueue_get_queue_index);
+/*
+ * called by:
+ * - drivers/block/virtio_blk.c|167| <<do_req>> if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr) < 0) {
+ * - drivers/char/hw_random/virtio-rng.c|54| <<register_buffer>> if (virtqueue_add_buf(vq, &sg, 0, 1, random_data) < 0)
+ * - drivers/char/virtio_console.c|424| <<add_inbuf>> ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
+ * - drivers/char/virtio_console.c|491| <<__send_control_msg>> if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
+ * - drivers/char/virtio_console.c|541| <<__send_to_port>> ret = virtqueue_add_buf(out_vq, sg, nents, 0, data);
+ * - drivers/net/virtio_net.c|513| <<add_recvbuf_small>> err = virtqueue_add_buf(rq->vq, rq->sg, 0, 2, skb);
+ * - drivers/net/virtio_net.c|558| <<add_recvbuf_big>> err = virtqueue_add_buf(rq->vq, rq->sg, 0, MAX_SKB_FRAGS + 2,
+ * - drivers/net/virtio_net.c|577| <<add_recvbuf_mergeable>> err = virtqueue_add_buf(rq->vq, rq->sg, 0, 1, page);
+ * - drivers/net/virtio_net.c|792| <<xmit_skb>> return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
+ * - drivers/net/virtio_net.c|892| <<virtnet_send_command>> BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi) < 0);
+ * - drivers/scsi/virtio_scsi.c|316| <<virtscsi_kick_event>> ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node);
+ * - drivers/scsi/virtio_scsi.c|505| <<virtscsi_kick_cmd>> ret = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd);
+ * - drivers/virtio/virtio_balloon.c|109| <<tell_host>> if (virtqueue_add_buf(vq, &sg, 1, 0, vb) < 0)
+ * - drivers/virtio/virtio_balloon.c|254| <<stats_handle_request>> if (virtqueue_add_buf(vq, &sg, 1, 0, vb) < 0)
+ * - drivers/virtio/virtio_balloon.c|339| <<init_vqs>> if (virtqueue_add_buf(vb->stats_vq,
+ * - net/9p/trans_virtio.c|203| <<p9_virtio_request>> if (virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc) < 0) {
+ */
int virtqueue_add_buf(struct virtqueue *_vq,
struct scatterlist sg[],
unsigned int out,
unsigned int in,
void *data)
{
+ /*
+ * struct vring_virtqueue包含virtqueue
+ */
struct vring_virtqueue *vq = to_vvq(_vq);
unsigned int i, avail, head, uninitialized_var(prev);
@@ -183,7 +303,16 @@ int virtqueue_add_buf(struct virtqueue *_vq,
/* If the host supports indirect descriptor tables, and we have multiple
* buffers, then go indirect. FIXME: tune this threshold */
+ /*
+ * 在以下使用vring_virtqueue->indirect:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|263| <<virtqueue_add_buf>> if (vq->indirect)
+ * - drivers/virtio/virtio_ring.c|595| <<vring_new_virtqueue>> vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
+ */
if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ /*
+ * 只在这里被调用
+ */
head = vring_add_indirect(vq, sg, out, in);
if (head != vq->vring.num)
goto add_head;
@@ -192,6 +321,21 @@ int virtqueue_add_buf(struct virtqueue *_vq,
BUG_ON(out + in > vq->vring.num);
BUG_ON(out + in == 0);
+ /*
+ * 在以下修改vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|150| <<vring_add_indirect>> vq->num_free--;
+ * - drivers/virtio/virtio_ring.c|227| <<virtqueue_add_buf>> vq->num_free -= out + in;
+ * - drivers/virtio/virtio_ring.c|346| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|352| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|603| <<vring_new_virtqueue>> vq->num_free = num;
+ * 在以下使用vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|214| <<virtqueue_add_buf>> if (vq->num_free < out + in) {
+ * - drivers/virtio/virtio_ring.c|216| <<virtqueue_add_buf>> out + in, vq->num_free);
+ * - drivers/virtio/virtio_ring.c|264| <<virtqueue_add_buf>> return vq->num_free ? vq->vring.num : 0;
+ * - drivers/virtio/virtio_ring.c|265| <<virtqueue_add_buf>> return vq->num_free;
+ * - drivers/virtio/virtio_ring.c|532| <<virtqueue_detach_unused_buf>> BUG_ON(vq->num_free != vq->vring.num);
+ */
if (vq->num_free < out + in) {
pr_debug("Can't add buf len %i - avail = %i\n",
out + in, vq->num_free);
@@ -205,8 +349,36 @@ int virtqueue_add_buf(struct virtqueue *_vq,
}
/* We're about to use some buffers from the free list. */
+ /*
+ * 在以下修改vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|150| <<vring_add_indirect>> vq->num_free--;
+ * - drivers/virtio/virtio_ring.c|227| <<virtqueue_add_buf>> vq->num_free -= out + in;
+ * - drivers/virtio/virtio_ring.c|346| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|352| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|603| <<vring_new_virtqueue>> vq->num_free = num;
+ * 在以下使用vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|214| <<virtqueue_add_buf>> if (vq->num_free < out + in) {
+ * - drivers/virtio/virtio_ring.c|216| <<virtqueue_add_buf>> out + in, vq->num_free);
+ * - drivers/virtio/virtio_ring.c|264| <<virtqueue_add_buf>> return vq->num_free ? vq->vring.num : 0;
+ * - drivers/virtio/virtio_ring.c|265| <<virtqueue_add_buf>> return vq->num_free;
+ * - drivers/virtio/virtio_ring.c|532| <<virtqueue_detach_unused_buf>> BUG_ON(vq->num_free != vq->vring.num);
+ */
vq->num_free -= out + in;
+ /*
+ * 在以下修改vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|159| <<vring_add_indirect>> vq->free_head = vq->vring.desc[head].next;
+ * - drivers/virtio/virtio_ring.c|248| <<virtqueue_add_buf>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|350| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|604| <<vring_new_virtqueue>> vq->free_head = 0;
+ *
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|153| <<vring_add_indirect>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|229| <<virtqueue_add_buf>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|230| <<virtqueue_add_buf>> for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
+ * - drivers/virtio/virtio_ring.c|349| <<detach_buf>> vq->vring.desc[i].next = vq->free_head;
+ */
head = vq->free_head;
for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
@@ -234,6 +406,21 @@ add_head:
/* Put entry in available array (but don't update avail->idx until they
* do sync). FIXME: avoid modulus here? */
+ /*
+ * struct vring_virtqueue *vq:
+ * -> struct virtqueue vq;
+ * -> struct vring vring;
+ * -> unsigned int num;
+ * -> struct vring_desc *desc;
+ * -> struct vring_avail *avail;
+ * -> __u16 flags;
+ * -> __u16 idx;
+ * -> __u16 ring[];
+ * -> struct vring_used *used;
+ * -> __u16 flags;
+ * -> __u16 idx;
+ * -> struct vring_used_elem ring[];
+ */
avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
vq->vring.avail->ring[avail] = head;
@@ -241,12 +428,37 @@ add_head:
END_USE(vq);
/* If we're indirect, we can fit many (assuming not OOM). */
+ /*
+ * indirect的话一个entry可以放入好多的entry, 所以有一个就行
+ */
if (vq->indirect)
return vq->num_free ? vq->vring.num : 0;
+ /*
+ * 在以下修改vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|150| <<vring_add_indirect>> vq->num_free--;
+ * - drivers/virtio/virtio_ring.c|227| <<virtqueue_add_buf>> vq->num_free -= out + in;
+ * - drivers/virtio/virtio_ring.c|346| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|352| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|603| <<vring_new_virtqueue>> vq->num_free = num;
+ * 在以下使用vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|214| <<virtqueue_add_buf>> if (vq->num_free < out + in) {
+ * - drivers/virtio/virtio_ring.c|216| <<virtqueue_add_buf>> out + in, vq->num_free);
+ * - drivers/virtio/virtio_ring.c|264| <<virtqueue_add_buf>> return vq->num_free ? vq->vring.num : 0;
+ * - drivers/virtio/virtio_ring.c|265| <<virtqueue_add_buf>> return vq->num_free;
+ * - drivers/virtio/virtio_ring.c|532| <<virtqueue_detach_unused_buf>> BUG_ON(vq->num_free != vq->vring.num);
+ */
return vq->num_free;
}
EXPORT_SYMBOL_GPL(virtqueue_add_buf);
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|508| <<virtscsi_kick_cmd>> ret = virtqueue_kick_prepare(vq->vq);
+ * - drivers/virtio/virtio_ring.c|452| <<virtqueue_kick>> if (virtqueue_kick_prepare(vq))
+ *
+ * 当vq->event的时候不被VRING_USED_F_NO_NOTIFY影响
+ */
bool virtqueue_kick_prepare(struct virtqueue *_vq)
{
struct vring_virtqueue *vq = to_vvq(_vq);
@@ -265,7 +477,18 @@ bool virtqueue_kick_prepare(struct virtqueue *_vq)
/* Need to update avail index before checking if we should notify */
virtio_mb();
+ /*
+ * VIRTIO_RING_F_EVENT_IDX:
+ * 后端用了used ring的最后一个元素,告诉前端驱动后端处理到哪个avail ring上的元素了,
+ * 同时前端使用avail ring的最后一个元素告诉后端,处理到那个used ring了
+ *
+ * vring_avail_event():
+ * 返回used的最后一个元素, 告诉前端驱动后端处理到哪个avail ring上的元素了
+ */
if (vq->event) {
+ /*
+ * 相当于当event_idx + 1 > old的时候返回true
+ */
needs_kick = vring_need_event(vring_avail_event(&vq->vring),
new, old);
} else {
@@ -282,6 +505,11 @@ EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
*
* This does not need to be serialized.
*/
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|513| <<virtscsi_kick_cmd>> virtqueue_notify(vq->vq);
+ * - drivers/virtio/virtio_ring.c|453| <<virtqueue_kick>> virtqueue_notify(vq);
+ */
void virtqueue_notify(struct virtqueue *_vq)
{
struct vring_virtqueue *vq = to_vvq(_vq);
@@ -301,6 +529,22 @@ EXPORT_SYMBOL_GPL(virtqueue_notify);
* Caller must ensure we don't call this with other virtqueue
* operations at the same time (except where noted).
*/
+/*
+ * called by:
+ * - drivers/block/virtio_blk.c|195| <<do_virtblk_request>> virtqueue_kick(vblk->vq);
+ * - drivers/char/hw_random/virtio-rng.c|57| <<register_buffer>> virtqueue_kick(vq);
+ * - drivers/char/virtio_console.c|425| <<add_inbuf>> virtqueue_kick(vq);
+ * - drivers/char/virtio_console.c|492| <<__send_control_msg>> virtqueue_kick(vq);
+ * - drivers/char/virtio_console.c|544| <<__send_to_port>> virtqueue_kick(out_vq);
+ * - drivers/net/virtio_net.c|701| <<try_fill_recv>> virtqueue_kick(rq->vq);
+ * - drivers/net/virtio_net.c|951| <<start_xmit>> virtqueue_kick(sq->vq);
+ * - drivers/net/virtio_net.c|1029| <<virtnet_send_command>> virtqueue_kick(vi->cvq);
+ * - drivers/scsi/virtio_scsi.c|318| <<virtscsi_kick_event>> virtqueue_kick(vscsi->event_vq.vq);
+ * - drivers/virtio/virtio_balloon.c|111| <<tell_host>> virtqueue_kick(vq);
+ * - drivers/virtio/virtio_balloon.c|256| <<stats_handle_request>> virtqueue_kick(vq);
+ * - drivers/virtio/virtio_balloon.c|342| <<init_vqs>> virtqueue_kick(vb->stats_vq);
+ * - net/9p/trans_virtio.c|209| <<p9_virtio_request>> virtqueue_kick(chan->vq);
+ */
void virtqueue_kick(struct virtqueue *vq)
{
if (virtqueue_kick_prepare(vq))
@@ -308,6 +552,11 @@ void virtqueue_kick(struct virtqueue *vq)
}
EXPORT_SYMBOL_GPL(virtqueue_kick);
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|523| <<virtqueue_get_buf>> detach_buf(vq, i);
+ * - drivers/virtio/virtio_ring.c|653| <<virtqueue_detach_unused_buf>> detach_buf(vq, i);
+ */
static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
{
unsigned int i;
@@ -322,22 +571,78 @@ static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
kfree(phys_to_virt(vq->vring.desc[i].addr));
+ /*
+ * centos上rx的flags都是2
+ */
while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
i = vq->vring.desc[i].next;
vq->num_free++;
}
vq->vring.desc[i].next = vq->free_head;
+ /*
+ * 在以下修改vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|159| <<vring_add_indirect>> vq->free_head = vq->vring.desc[head].next;
+ * - drivers/virtio/virtio_ring.c|248| <<virtqueue_add_buf>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|350| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|604| <<vring_new_virtqueue>> vq->free_head = 0;
+ *
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|153| <<vring_add_indirect>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|229| <<virtqueue_add_buf>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|230| <<virtqueue_add_buf>> for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
+ * - drivers/virtio/virtio_ring.c|349| <<detach_buf>> vq->vring.desc[i].next = vq->free_head;
+ */
vq->free_head = head;
/* Plus final descriptor */
+ /*
+ * 在以下修改vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|150| <<vring_add_indirect>> vq->num_free--;
+ * - drivers/virtio/virtio_ring.c|227| <<virtqueue_add_buf>> vq->num_free -= out + in;
+ * - drivers/virtio/virtio_ring.c|346| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|352| <<detach_buf>> vq->num_free++;
+ * - drivers/virtio/virtio_ring.c|603| <<vring_new_virtqueue>> vq->num_free = num;
+ * 在以下使用vring_virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|205| <<virtqueue_add_buf>> if (vq->indirect && (out + in) > 1 && vq->num_free) {
+ * - drivers/virtio/virtio_ring.c|214| <<virtqueue_add_buf>> if (vq->num_free < out + in) {
+ * - drivers/virtio/virtio_ring.c|216| <<virtqueue_add_buf>> out + in, vq->num_free);
+ * - drivers/virtio/virtio_ring.c|264| <<virtqueue_add_buf>> return vq->num_free ? vq->vring.num : 0;
+ * - drivers/virtio/virtio_ring.c|265| <<virtqueue_add_buf>> return vq->num_free;
+ * - drivers/virtio/virtio_ring.c|532| <<virtqueue_detach_unused_buf>> BUG_ON(vq->num_free != vq->vring.num);
+ */
vq->num_free++;
}
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|579| <<virtqueue_get_buf>> if (!more_used(vq)) {
+ * - drivers/virtio/virtio_ring.c|806| <<vring_interrupt>> if (!more_used(vq)) {
+ */
static inline bool more_used(const struct vring_virtqueue *vq)
{
return vq->last_used_idx != vq->vring.used->idx;
}
+/*
+ * called by:
+ * - drivers/block/virtio_blk.c|65| <<blk_done>> while ((vbr = virtqueue_get_buf(vblk->vq, &len)) != NULL) {
+ * - drivers/char/hw_random/virtio-rng.c|41| <<random_recv_done>> if (!virtqueue_get_buf(vq, &len))
+ * - drivers/char/virtio_console.c|402| <<get_inbuf>> buf = virtqueue_get_buf(port->in_vq, &len);
+ * - drivers/char/virtio_console.c|493| <<__send_control_msg>> while (!virtqueue_get_buf(vq, &len))
+ * - drivers/char/virtio_console.c|520| <<reclaim_consumed_buffers>> while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
+ * - drivers/char/virtio_console.c|566| <<__send_to_port>> while (!virtqueue_get_buf(out_vq, &len))
+ * - drivers/char/virtio_console.c|1647| <<control_work_handler>> while ((buf = virtqueue_get_buf(vq, &len))) {
+ * - drivers/char/virtio_console.c|1854| <<remove_controlq_data>> while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
+ * - drivers/net/virtio_net.c|381| <<receive_mergeable>> page = virtqueue_get_buf(rq->vq, &len);
+ * - drivers/net/virtio_net.c|400| <<receive_mergeable>> buf = virtqueue_get_buf(rq->vq, &len);
+ * - drivers/net/virtio_net.c|691| <<virtnet_poll>> (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
+ * - drivers/net/virtio_net.c|740| <<free_old_xmit_skbs>> while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
+ * - drivers/net/virtio_net.c|946| <<virtnet_send_command>> while (!virtqueue_get_buf(vi->cvq, &tmp))
+ * - drivers/scsi/virtio_scsi.c|226| <<virtscsi_vq_done>> while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
+ * - drivers/virtio/virtio_balloon.c|95| <<balloon_ack>> vb = virtqueue_get_buf(vq, &len);
+ * - drivers/virtio/virtio_balloon.c|237| <<stats_request>> vb = virtqueue_get_buf(vq, &len);
+ * - net/9p/trans_virtio.c|131| <<req_done>> while ((rc = virtqueue_get_buf(chan->vq, &len)) != NULL) {
+ */
void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
{
struct vring_virtqueue *vq = to_vvq(_vq);
@@ -375,6 +680,20 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
/* detach_buf clears data, so grab it now. */
ret = vq->data[i];
detach_buf(vq, i);
+ /*
+ * 在以下修改vring_virtqueue->last_used_idx:
+ * - drivers/virtio/virtio_ring.c|636| <<virtqueue_get_buf>> vq->last_used_idx++;
+ * - drivers/virtio/virtio_ring.c|910| <<vring_new_virtqueue>> vq->last_used_idx = 0;
+ * 在以下使用vring_virtqueue->last_used_idx:
+ * - drivers/virtio/virtio_ring.c|576| <<more_used>> return vq->last_used_idx != vq->vring.used->idx;
+ * - drivers/virtio/virtio_ring.c|621| <<virtqueue_get_buf>> i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
+ * - drivers/virtio/virtio_ring.c|622| <<virtqueue_get_buf>> *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
+ * - drivers/virtio/virtio_ring.c|641| <<virtqueue_get_buf>> vring_used_event(&vq->vring) = vq->last_used_idx;
+ * - drivers/virtio/virtio_ring.c|726| <<virtqueue_enable_cb_prepare>> vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
+ * - drivers/virtio/virtio_ring.c|795| <<virtqueue_enable_cb_delayed>> bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;