-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgao_mmio_resource.c
1367 lines (1008 loc) · 43.7 KB
/
gao_mmio_resource.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
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#ifndef LINUX
#define LINUX
#endif
#include "gao_mmio_resource.h"
const static char *gao_resource_state_str[] = {"Unused", "Registered", "Active", "Configuring", "Deleting", "Error", "Invalid State"};
const char* gao_resource_state_string(gao_resource_state_t state) {
if(state >= GAO_RESOURCE_STATE_FINAL) return gao_resource_state_str[GAO_RESOURCE_STATE_FINAL];
else return gao_resource_state_str[state];
}
const static char *gao_owner_str[] = {"None", "Userspace", "Interface"};
const static char *gao_direction_str[] = {"NA", "RX", "TX"};
void gao_dump_buffers(struct gao_resources *resources) {
uint64_t index;
log_debug("Dump Buffer Groups: start=%lu end=%lu frame=%lu",
resources->buffer_start_phys, resources->buffer_end_phys, resources->buffer_space_frame);
for(index = 0; index < GAO_BUFFER_GROUPS; index++) {
log_debug("Index:%8lu virtaddr=%p",
(unsigned long)index, resources->buffer_groups[index]);
}
}
void gao_dump_descriptor(struct gao_descriptor *desc) {
log_debug("Desc: %016lx GFN=%04hx BufIdx=%04hx Len=%04hx Offset=%04hx",
(unsigned long)desc->descriptor, desc->gfn, desc->index, desc->len, desc->offset);
}
void gao_dump_descriptors(struct gao_resources *resources) {
uint64_t index;
log_debug("Dump Descriptors %p: head=%lu tail=%lu left=%lu",
resources->descriptor_ring.descriptors,
(unsigned long)resources->descriptor_ring.head,
(unsigned long)resources->descriptor_ring.tail,
(unsigned long)resources->descriptor_ring.left);
for(index = 0; index < GAO_DESCRIPTORS; index++) {
gao_dump_descriptor(&(*resources->descriptor_ring.descriptors)[index]);
}
}
void gao_dump_descriptor_ring(struct gao_descriptor_ring *ring) {
uint64_t index;
if(!ring) {
log_debug("Dump Descriptor Ring: %p", ring);
return;
}
log_debug("Dump Descriptor Ring: (dump %lu descriptors)", (unsigned long)ring->header.capacity);
log_debug("head=%10u tail=%10u size=%10u capacity=%10u",
(unsigned)ring->header.head, (unsigned)ring->header.tail,
(unsigned)ring->header.size, (unsigned)ring->header.capacity);
for(index = 0; index < ring->header.capacity; index++) {
gao_dump_descriptor(&ring->descriptors[index]);
}
}
void gao_dump_descriptor_ring_nested(struct gao_descriptor_ring *ring) {
uint64_t index;
gao_dump_descriptor_ring(ring);
if(!ring) return;
for(index = 0; index < ring->header.capacity; index++) {
gao_dump_descriptor(&ring->descriptors[index]);
}
}
void gao_dump_action(struct gao_action *action) {
log_debug("Action: id=%hhu port=%hhu queue=%hhu", action->action_id, action->fwd.dport, action->fwd.dqueue);
}
void gao_dump_resources(struct gao_resources *resources) {
log_debug("Resources:");
}
void gao_dump_ingress_queue_map(struct gao_ingress_queue_map *map) {
uint64_t port_index, subqueue_index, has_queues;
log_debug("Dumping Ingress Queue Map:");
for(port_index = 0; port_index < GAO_MAX_PORTS; port_index++) {
//Only dump if the port actually has queues
for(subqueue_index = 0, has_queues = 0; subqueue_index < GAO_MAX_PORT_SUBQUEUE; subqueue_index++) {
if(map->port[port_index].ring[subqueue_index]) has_queues++;
}
if(!has_queues) continue;
log_debug("Port %lu map:", (unsigned long)port_index);
for(subqueue_index = 0; subqueue_index < GAO_MAX_PORT_SUBQUEUE; subqueue_index++) {
log_debug(" [%lu]->%p", (unsigned long)subqueue_index,map->port[port_index].ring[subqueue_index]);
}
}
}
void gao_dump_queue_binding(struct gao_queue_binding *binding) {
log_debug("Queue Binding:");
log_debug("Owner Type:%10s filep=%p IfIndex/Queue/Direction=%lu/%lu/%2s",
gao_owner_str[binding->owner_type], binding->gao_file,
(unsigned long)binding->gao_ifindex, (unsigned long)binding->queue_index,
gao_direction_str[binding->direction_txrx]);
}
void gao_dump_queue(struct gao_queue *queue) {
uint64_t index;
log_debug("Dump Queue: index=%lu state=%s flags=%lx descriptors=%lu ring=%p",
(unsigned long)queue->index, gao_resource_state_str[queue->state],
(unsigned long)queue->flags, (unsigned long)queue->descriptors, queue->ring);
gao_dump_queue_binding(&queue->binding);
for(index = 0; index < GAO_MAX_PORT_SUBQUEUE; index++) {
log_debug("Subqueue Ring[%lu]: %p", (unsigned long)index, queue->subqueues[index].ring);
}
gao_dump_ingress_queue_map(&queue->queue_map);
}
void gao_dump_queue_nested(struct gao_queue *queue) {
gao_dump_queue(queue);
if(queue->ring) gao_dump_descriptor_ring(queue->ring);
}
void gao_dump_port(struct gao_port* port) {
log_debug("Dumping Port %lu: name=%s ifindex=%lu state=%s",
(unsigned long)port->gao_ifindex, gao_get_port_name(port), (unsigned long)port->ifindex, gao_resource_state_string(port->state));
log_debug("netdev=%p port_ops=%p", port->netdev, port->port_ops);
log_debug("Num Queues/Descriptors: rx=%u/%u tx=%u/%u",
(unsigned)port->num_rx_queues, (unsigned)port->num_rx_desc, (unsigned)port->num_tx_queues, (unsigned)port->num_tx_desc);
}
void gao_dump_port_nested(struct gao_port* port) {
uint64_t index;
gao_dump_port(port);
log_debug("RX Queues:");
for(index = 0; index < port->num_rx_queues; index++) {
if(!port->rx_queues[index]) continue;
gao_dump_queue(port->rx_queues[index]);
}
for(index = 0; index < port->num_tx_queues; index++) {
if(!port->tx_queues[index]) continue;
gao_dump_queue(port->tx_queues[index]);
}
}
void gao_dump_ports(struct gao_resources *resources) {
uint64_t index;
for(index = 0; index < GAO_MAX_PORTS; index++) {
if(resources->ports[index].state == GAO_RESOURCE_STATE_UNUSED) continue;
gao_dump_port(&resources->ports[index]);
}
}
void gao_dump_ports_nested(struct gao_resources *resources) {
uint64_t index;
for(index = 0; index < GAO_MAX_PORTS; index++) {
if(resources->ports[index].state == GAO_RESOURCE_STATE_UNUSED) continue;
gao_dump_port_nested(&resources->ports[index]);
}
}
void gao_dump_file(struct file *filep) {
struct gao_file_private *file_private = (struct gao_file_private*) filep->private_data;
log_debug("Dump File: state=%s filep=%p bind if/q/dir %lu/%lu/%d queue=%p",
gao_resource_state_string(file_private->state), filep,
(unsigned long)file_private->bound_gao_ifindex, (unsigned long)file_private->bound_queue_index,
file_private->bound_direction, file_private->bound_queue);
}
inline void swap_descriptors(struct gao_descriptor *desc1, struct gao_descriptor *desc2) {
uint64_t desc_tmp = desc1->descriptor;
desc1->descriptor = desc2->descriptor;
desc2->descriptor = desc_tmp;
}
/**
* This "validates" the buffers by checking if we kernel panic when we
* write into them. Writes a unique string into each buffer we can check
* in the descriptor and userspace validation later on. Can only be done on
* module init, and the module will not come up if this fails (neither will the machine...)
* @param resources
* @return Void on success, a machine in need of reboot on failure.
*/
static void gao_validate_buffer_groups(struct gao_resources *resources) {
uint64_t index, buffer_index;
uint16_t gfn, bufnum;
void *buffer_group, *buffer;
char strbuf[GAO_BUFFER_TEST_STR_LEN];
log_info("Validating buffer groups.");
for(index = 0; index < GAO_BUFFER_GROUPS; index++) {
buffer_group = resources->buffer_groups[index];
// log_debug("Validate buffer group %p:", buffer_group);
//Put a unique string in each buffer, and fill the rest with dead cows.
for(buffer_index = 0; buffer_index < GAO_BUFFER_GROUP_SIZE; buffer_index += GAO_BUFFER_SIZE) {
buffer = (buffer_group + buffer_index);
gfn = (uint16_t)GAO_VIRT_TO_GFN(buffer_group);
bufnum = (uint16_t)((unsigned long)(buffer_index/GAO_BUFFER_SIZE));
snprintf((char*)&strbuf, GAO_BUFFER_TEST_STR_LEN, GAO_BUFFER_TEST_STR_FMT, gfn, bufnum);
// log_debug("Write to %p: %s. Fill with cows for %d",
// buffer, (char*)&strbuf, (GAO_BUFFER_SIZE-GAO_BUFFER_TEST_STR_LEN));
snprintf((char*)buffer, GAO_BUFFER_TEST_STR_LEN, GAO_BUFFER_TEST_STR_FMT, gfn, bufnum);
memset((buffer + GAO_BUFFER_TEST_STR_LEN), GAO_BUFFER_FILL_VAL, (GAO_BUFFER_SIZE-GAO_BUFFER_TEST_STR_LEN));
}
}
log_info("Buffer validation successful. Start=%lx End=%lx Size=%lx (%lu MB)",
resources->buffer_start_phys, resources->buffer_end_phys, resources->buffer_space_frame, resources->buffer_space_frame >> 20);
return;
}
static void gao_free_buffer_group(void *buffer_group) {
int64_t page_index;
if(!buffer_group) gao_bug("Cannot free null buffer group.");
// log_debug("Freeing a buffer group: Virt=%p Phys=%lx GFN=%lx", buffer_group,
// (unsigned long)virt_to_phys(buffer_group),
// GAO_VIRT_TO_GFN(buffer_group));
for(page_index = 0; page_index < (GAO_BUFFER_GROUP_SIZE); page_index += GAO_SMALLPAGE_SIZE) {
ClearPageReserved( virt_to_page( (((unsigned long)buffer_group) + page_index) ));
}
kfree_null(buffer_group);
err:
return;
}
/**
* Free and unlock all buffer groups allocated to GAO.
* @param resources
*/
static void gao_free_buffer_groups(struct gao_resources *resources) {
uint64_t index;
log_info("Free buffer groups.");
gao_free_buffer_group(resources->dummy_group);
for(index = 0; index < GAO_BUFFER_GROUPS; index++) {
gao_free_buffer_group(resources->buffer_groups[index]);
}
}
static void* gao_alloc_buffer_group(void) {
int64_t page_index;
void* buffer_group = NULL;
buffer_group = kmalloc((GAO_BUFFER_GROUP_SIZE), GFP_KERNEL | __GFP_COMP);
if(!buffer_group) gao_error("Failed to allocate a buffer group.");
if( (((unsigned long)buffer_group) & ~GAO_GFN_MASK) != ((unsigned long)buffer_group) )
gao_error("A buffer group not aligned to GFN boundary.");
for(page_index = 0; page_index < (GAO_BUFFER_GROUP_SIZE); page_index += GAO_SMALLPAGE_SIZE) {
SetPageReserved(virt_to_page( (((unsigned long)buffer_group) + page_index) ));
}
// log_debug("Allocated a buffer group: Virt=%p Phys=%lx GFN=%lx", buffer_group,
// (unsigned long)virt_to_phys(buffer_group),
// GAO_VIRT_TO_GFN(buffer_group));
return buffer_group;
err:
return NULL;
}
/**
* Allocate the memory for the buffer groups. Each buffer group is physically
* contiguous and page-locked in memory.
* @param resources
* @return 0 on success, -ENOMEM on failure.
*/
static int64_t gao_init_buffer_groups(struct gao_resources *resources) {
int64_t ret = 0;
uint64_t index;
void* buffer_group = NULL;
unsigned long buffer_min = -1, buffer_max = 0, phys_addr;
log_info("Allocating buffer groups.");
resources->dummy_group = gao_alloc_buffer_group();
if(!resources->dummy_group) gao_error_val(-ENOMEM, "Failed to allocate dummy buffer group.");
for(index = 0; index < GAO_BUFFER_GROUPS; index++) {
buffer_group = gao_alloc_buffer_group();
if(!buffer_group) gao_error_val(-ENOMEM, "Failed to allocate buffer at index %lu.", (unsigned long)index);
resources->buffer_groups[index] = buffer_group;
//Set min and max range values
phys_addr = ((unsigned long)virt_to_phys(buffer_group));
if( phys_addr < buffer_min ) buffer_min = phys_addr;
if( phys_addr > buffer_max ) buffer_max = phys_addr;
}
resources->buffer_start_phys = buffer_min;
resources->buffer_end_phys = (buffer_max + GAO_BUFFER_GROUP_SIZE);
resources->buffer_space_frame = (resources->buffer_end_phys - resources->buffer_start_phys);
gao_validate_buffer_groups(resources);
log_info("Successfully allocated %lu buffer groups (%luMB). [Start=%lx End=%lx Frame=%lx (%lu MB) Gaps=%lu]",
(unsigned long)GAO_BUFFER_GROUPS, (unsigned long)(GAO_BUFFER_SPACE_SIZE >> 20),
(unsigned long)resources->buffer_start_phys, (unsigned long)resources->buffer_end_phys,
(unsigned long)resources->buffer_space_frame,
(unsigned long)resources->buffer_space_frame >> 20,
(unsigned long)((resources->buffer_space_frame/GAO_BUFFER_GROUP_SIZE) - GAO_BUFFER_GROUPS));
return 0;
err:
gao_free_buffer_groups(resources);
return ret;
}
/**
* Read the values written by the buffer validation by de-referencing each
* descriptor. Can only be done on module init, and module will not start if
* this does not succeed.
* @param resources
* @return
*/
static int64_t gao_validate_descriptor_allocator_ring(struct gao_resources *resources) {
int64_t ret = 0;
uint64_t index;
uint16_t gfn, bufnum;
char descriptor_str[GAO_BUFFER_TEST_STR_LEN], buffer_str[GAO_BUFFER_TEST_STR_LEN];
uint32_t *dead_cows;
struct gao_descriptor descriptor;
struct gao_descriptor (*descriptors)[GAO_DESCRIPTORS] = resources->descriptor_ring.descriptors;
log_info("Validating descriptors.");
dead_cows = vmalloc(GAO_BUFFER_SIZE - GAO_BUFFER_TEST_STR_LEN);
if(!dead_cows) gao_error_val(-ENOMEM, "No moooomery for cows.");
memset(dead_cows, GAO_BUFFER_FILL_VAL, (GAO_BUFFER_SIZE - GAO_BUFFER_TEST_STR_LEN));
for(index = 0; index < GAO_DESCRIPTORS; index++) {
descriptor.descriptor = (*descriptors)[index].descriptor;
// log_debug("Desc=%016lx Phys=%016lx Virt=%p",
// (unsigned long)descriptor.descriptor,
// GAO_DESC_TO_PHYS(descriptor.descriptor),
// GAO_DESC_TO_VIRT(descriptor.descriptor));
gfn = descriptor.gfn;
bufnum = descriptor.index;
snprintf((char*)&descriptor_str, GAO_BUFFER_TEST_STR_LEN, GAO_BUFFER_TEST_STR_FMT, gfn, bufnum);
strncpy((char*)&buffer_str, (char*)GAO_DESC_TO_VIRT(descriptor.descriptor), GAO_BUFFER_TEST_STR_LEN);
ret = strncmp((char*)&descriptor_str, (char*)&buffer_str, GAO_BUFFER_TEST_STR_LEN);
if(ret) {
gao_bug_val(-EINVAL, "Error while validating descriptors, strings unequal: Buffer=%s Descriptor=%s Equal=%ld",
(char*)&buffer_str, (char*)&descriptor_str, (long)ret);
log_bug("Buffer=%s Descriptor=%s Memcmp=%ld", (char*)&buffer_str, (char*)&descriptor_str, (long)ret);
}
ret = memcmp(dead_cows, GAO_DESC_TO_VIRT(descriptor.descriptor) + GAO_BUFFER_TEST_STR_LEN, (GAO_BUFFER_SIZE - GAO_BUFFER_TEST_STR_LEN));
if(ret) {
gao_bug_val(-EINVAL, "Error while validating descriptors, memory fill unequal at %ld.", (long)ret);
log_bug("Buffer=%s Descriptor=%s Memcmp=%ld", (char*)&buffer_str, (char*)&descriptor_str, (long)ret);
}
// log_debug("Buffer=%s Descriptor=%s Memcmp=%ld", (char*)&buffer_str, (char*)&descriptor_str, (long)ret);
}
vfree(dead_cows);
log_info("Descriptor validation successful.");
return 0;
err:
if(dead_cows) vfree(dead_cows);
return ret;
}
/**
* Dealloc the descriptor ring.
* @param resources
*/
static void gao_free_descriptor_allocator_ring(struct gao_resources *resources) {
log_info("Free descriptor groups.");
if(resources->descriptor_ring.descriptors)
vfree(resources->descriptor_ring.descriptors);
return;
}
/**
* Initialize the descriptor ring. Creates a descriptor for each buffer.
* @param resources
* @return 0 on success, -ENOMEM on failure.
*/
static int64_t gao_init_descriptor_allocator_ring(struct gao_resources *resources) {
int64_t ret = 0;
uint64_t index, buffer_index, descriptor_index = 0;
uint16_t gfn, bufnum;
void *buffer_group, *buffer;
struct gao_descriptor (*descriptors)[GAO_DESCRIPTORS] = NULL;
struct gao_descriptor descriptor;
log_info("Initializing descriptors.");
descriptors = vmalloc(GAO_DESCRIPTORS*sizeof(struct gao_descriptor));
if(!descriptors) gao_error_val(-ENOMEM, "Failed to allocate descriptor ring.");
for(index = 0; index < GAO_BUFFER_GROUPS; index++) {
buffer_group = resources->buffer_groups[index];
gfn = (uint16_t)(GAO_VIRT_TO_GFN(buffer_group));
//Fill each descriptor slot with information on every buffer
for(buffer_index = 0; buffer_index < GAO_BUFFER_GROUP_SIZE; buffer_index += GAO_BUFFER_SIZE) {
buffer = (buffer_group + buffer_index);
bufnum = (uint16_t)((unsigned long)(buffer_index/GAO_BUFFER_SIZE));
descriptor.len = 0;
descriptor.gfn = gfn;
descriptor.index = bufnum;
(*descriptors)[descriptor_index].descriptor = descriptor.descriptor;
descriptor_index++;
}
}
log_info("Created %lu descriptors (Max=%lu).",
(unsigned long)descriptor_index, (unsigned long)GAO_DESCRIPTORS);
resources->descriptor_ring.descriptors = descriptors;
resources->descriptor_ring.head = 0;
resources->descriptor_ring.tail = 0;
resources->descriptor_ring.left = descriptor_index;
spin_lock_init(&resources->descriptor_ring.lock);
if(descriptor_index != GAO_DESCRIPTORS)
gao_bug_val(-EINVAL, "Mismatch between number of descriptors and buffers!");
gao_validate_descriptor_allocator_ring(resources);
return 0;
err:
gao_free_descriptor_allocator_ring(resources);
return ret;
}
inline static void gao_lock_descriptor_allocator(struct gao_descriptor_allocator_ring *ring) {
log_debug("Spinlocking descriptor ring");
spin_lock(&ring->lock);
log_debug("Locked descriptor ring");
}
inline static void gao_unlock_descriptor_ring(struct gao_descriptor_allocator_ring *ring) {
log_debug("Unlocking descriptor ring");
spin_unlock(&ring->lock);
}
static void gao_free_descriptors(struct gao_resources *resources, struct gao_descriptor (*source_queue)[], uint64_t num_descriptors) {
int64_t index;
struct gao_descriptor_allocator_ring *ring = &resources->descriptor_ring;
log_debug("Trying to free %lu descriptors for %p",
(unsigned long)num_descriptors, source_queue);
gao_lock_descriptor_allocator(ring);
if(num_descriptors > (GAO_DESCRIPTORS - ring->left)) {
log_bug("Trying to free more descriptors than we should have... (Freeing: %lu Need: %lu)",
(unsigned long)num_descriptors, (unsigned long)(GAO_DESCRIPTORS - ring->left));
//Truncate the free and return what we have space for
//XXX: Better to crash here?
num_descriptors = (GAO_DESCRIPTORS - ring->left);
}
ring->left += num_descriptors;
//Copy the descriptors into the queue
for(index = 0; index < num_descriptors; index++, ring->tail = CIRC_NEXT(ring->tail, GAO_DESCRIPTORS)) {
(*ring->descriptors)[ring->tail].descriptor = (*source_queue)[index].descriptor;
//TODO: More robuest error handling here
if(!(*ring->descriptors)[ring->tail].descriptor) log_bug("Copied a zero descriptor into the ring!");
(*source_queue)[index].descriptor = 0;
}
gao_unlock_descriptor_ring(ring);
return;
}
/**
* Copy descriptors from the ring into the targeted queue. Copies them starting at
* index num_descriptors going backwards.
* @warning Spinlocks the descriptor ring
* @param resources
* @param target_queue The descriptor buffer to copy into
* @param num_descriptors The numbre of descriptors to take
* @return 0 on success, -ENOMEM on failure. On failure, allocates no descriptors.
*/
static int64_t gao_get_descriptors(struct gao_resources *resources, struct gao_descriptor (*target_queue)[], uint64_t num_descriptors) {
int64_t ret = 0, index;
struct gao_descriptor_allocator_ring *ring = &resources->descriptor_ring;
log_debug("Trying to get %lu descriptors for %p",
(unsigned long)num_descriptors, target_queue);
gao_lock_descriptor_allocator(ring);
if(num_descriptors > ring->left) {
gao_error_val(-ENOMEM, "Cannot allocate queue, insufficient descriptors. (Want: %lu have: %lu)",
(unsigned long)num_descriptors, (unsigned long)ring->left);
}
ring->left -= num_descriptors;
//Copy the descriptors into the queue
for(index = 0; index < num_descriptors; index++, ring->head = CIRC_NEXT(ring->head, GAO_DESCRIPTORS)) {
(*target_queue)[index].descriptor = (*ring->descriptors)[ring->head].descriptor;
if(!(*target_queue)[index].descriptor) log_bug("Copied a zero descriptor into the target queue!");
(*ring->descriptors)[ring->head].descriptor = 0;
}
err:
gao_unlock_descriptor_ring(ring);
return ret;
}
inline static void gao_free_descriptor_ring(struct gao_resources *resources, struct gao_descriptor_ring *ring) {
if(!ring) {
log_debug("Trying to free null descriptor ring.");
return;
}
if(ring->header.capacity)
gao_free_descriptors(resources, &ring->descriptors, ring->header.capacity);
vfree(ring);
return;
}
/**
* Create and initialize a new descriptor ring by taking descriptors from the global allocator.
* @warning Resource lock ... actually doesn't need to be held.
* @param resources
* @param num_descriptors
* @return
*/
static struct gao_descriptor_ring* gao_create_descriptor_ring(struct gao_resources *resources, uint64_t num_descriptors) {
struct gao_descriptor_ring *ring = NULL;
uint64_t queue_size = 0, index;
queue_size = (sizeof(struct gao_descriptor_ring_header)
+ (sizeof(struct gao_descriptor)*num_descriptors));
ring = vmalloc(queue_size);
check_ptr(ring);
memset((void*)ring, 0, queue_size);
if(gao_get_descriptors(resources, &ring->descriptors, num_descriptors))
gao_error("User queue creation failed, insufficient descriptors.");
//Initialize the ring values
for(index = 0; index < num_descriptors; index++) {
ring->descriptors[index].len = 0;
ring->descriptors[index].offset = GAO_DEFAULT_OFFSET;
}
ring->header.capacity = num_descriptors;
init_waitqueue_head(&ring->control.head_wait_queue);
init_waitqueue_head(&ring->control.tail_wait_queue);
ring->control.head_wait_queue_ref = &ring->control.head_wait_queue;
ring->control.tail_wait_queue_ref = &ring->control.tail_wait_queue;
ring->control.head_wake_condition_ref = &ring->control.head_wake_condition;
ring->control.tail_wake_condition_ref = &ring->control.tail_wake_condition;
spin_lock_init(&ring->control.head_lock);
spin_lock_init(&ring->control.tail_lock);
return ring;
err:
gao_free_descriptor_ring(resources, ring);
return NULL;
}
static void gao_free_egress_subqueue(struct gao_resources *resources, struct gao_egress_subqueue *subqueue) {
if(subqueue->ring) gao_free_descriptor_ring(resources, subqueue->ring);
subqueue->ring = NULL;
}
/**
* Create the subqueues for an egress queue.
* @warning Caller must hold resource lock
* @param resources
* @param port
*/
static void gao_free_egress_subqueues(struct gao_resources *resources, struct gao_queue *queue) {
int64_t index;
log_debug("Free subqueues for queue %p", queue);
if(!queue) return;
for(index = 0; index < GAO_MAX_PORT_SUBQUEUE; index++) {
gao_free_egress_subqueue(resources, &queue->subqueues[index]);
}
}
static int64_t gao_create_egress_subqueue(struct gao_resources *resources, struct gao_egress_subqueue *subqueue, uint64_t num_descriptors) {
int64_t ret = 0;
subqueue->ring = gao_create_descriptor_ring(resources, num_descriptors);
check_ptr_val(-ENOMEM, subqueue->ring);
subqueue->ring->header.head = 0;
subqueue->ring->header.tail = 0;
log_debug("Successfully created subqueue.");
return 0;
err:
gao_free_egress_subqueue(resources, subqueue);
return ret;
}
/**
* Delete a queue from its index. Will return descriptor groups, but does not unbind it.
* @warning Caller must hold resource lock.
* @warning Caller must have synchronized_rcu
* @param resources
* @param queue_index
*/
static void gao_free_queue(struct gao_resources *resources, struct gao_queue *queue) {
size_t alloc_size;
void *pipeline_addr;
log_debug("Deleting queue at %p", queue);
if(!queue) return;
gao_free_egress_subqueues(resources, queue);
gao_free_descriptor_ring(resources, queue->ring);
if(queue->action_map) vfree(queue->action_map);
if(queue->descriptor_pipeline) {
pipeline_addr = queue->descriptor_pipeline;
alloc_size = queue->descriptor_pipeline_size;
for(pipeline_addr = queue->descriptor_pipeline; pipeline_addr < ((void*)queue->descriptor_pipeline) + alloc_size; pipeline_addr += PAGE_SIZE) {
ClearPageReserved(vmalloc_to_page(pipeline_addr));
}
vfree(queue->descriptor_pipeline);
}
if(queue->action_pipeline) {
pipeline_addr = queue->action_pipeline;
alloc_size = queue->action_pipeline_size;
for(pipeline_addr = queue->action_pipeline; pipeline_addr < ((void*)queue->action_pipeline) + alloc_size; pipeline_addr += PAGE_SIZE) {
ClearPageReserved(vmalloc_to_page(pipeline_addr));
}
vfree(queue->action_pipeline);
}
vfree(queue);
return;
}
/**
* Create a new queue and fill it with valid descriptors. Does not bind the queue to anything.
* @warning Caller must hold resource lock.
* @param resources
* @param size The size of the queue in descriptors.
* @return The index of the new queue on success, -ENOMEM (if insufficient memory/resources), -EFBIG if queue too big.
*/
static struct gao_queue* gao_create_queue(struct gao_resources *resources, uint64_t num_descriptors, gao_direction_t direction) {
struct gao_queue* queue = NULL;
size_t alloc_size = 0;
void* pipeline_addr;
log_debug("Creating queue, size=%lu", (unsigned long)num_descriptors);
queue = vmalloc(sizeof(struct gao_queue));
check_ptr(queue);
memset((void*)queue, 0, sizeof(struct gao_queue));
queue->ring = gao_create_descriptor_ring(resources, num_descriptors);
check_ptr(queue->ring);
if(direction == GAO_DIRECTION_RX) {
queue->action_map = vmalloc(sizeof(struct gao_action)*num_descriptors);
check_ptr(queue->action_map);
//The following two structs are mmap'd to userspace. Allocate them as a multiple of the page size.
//I really suck at number theory.
alloc_size = (sizeof(struct gao_descriptor)*num_descriptors*GAO_ING_PIPELINE_DEPTH);
alloc_size = (alloc_size % PAGE_SIZE) ? alloc_size + (PAGE_SIZE-(alloc_size%PAGE_SIZE)) : alloc_size;
queue->descriptor_pipeline = vmalloc(alloc_size);
check_ptr(queue->descriptor_pipeline);
queue->descriptor_pipeline_size = alloc_size;
memset((void*)queue->descriptor_pipeline, 0, alloc_size);
log_debug("Allocated descriptor pipeline size %lu B at %p", alloc_size, queue->descriptor_pipeline);
//Reserve the pages
for(pipeline_addr = queue->descriptor_pipeline; pipeline_addr < ((void*)queue->descriptor_pipeline) + alloc_size; pipeline_addr += PAGE_SIZE) {
SetPageReserved(vmalloc_to_page(pipeline_addr));
}
alloc_size = (sizeof(struct gao_action)*num_descriptors*GAO_ING_PIPELINE_DEPTH);
alloc_size = (alloc_size % PAGE_SIZE) ? alloc_size + (PAGE_SIZE-(alloc_size%PAGE_SIZE)) : alloc_size;
//queue->action_pipeline = vmalloc(sizeof(struct gao_action)*num_descriptors*GAO_ING_PIPELINE_DEPTH);
queue->action_pipeline = vmalloc(alloc_size);
check_ptr(queue->action_pipeline);
queue->action_pipeline_size = alloc_size;
memset((void*)queue->action_pipeline, 0, alloc_size);
log_debug("Allocated action pipeline size %lu B at %p", alloc_size, queue->action_pipeline);
//Reserve the pages
for(pipeline_addr = queue->action_pipeline; pipeline_addr < ((void*)queue->action_pipeline) + alloc_size; pipeline_addr += PAGE_SIZE) {
SetPageReserved(vmalloc_to_page(pipeline_addr));
}
}
queue->descriptors = num_descriptors;
queue->state = GAO_RESOURCE_STATE_REGISTERED;
log_debug("Created queue of size=%lu at addr=%p", (unsigned long)num_descriptors, queue);
return queue;
err:
gao_free_queue(resources, queue);
return NULL;
}
/**
* From the perspective of a port that is being deleted, traverse all other ports and
* null bindings to our egress queues.
* @param resources
* @param port
*/
static void gao_port_null_egress_to_ingress_queue_map(struct gao_resources *resources, struct gao_port *port) {
uint64_t index = 0, subqueue_index;
struct gao_queue *ingress_queue = NULL;
struct gao_port *other_port = NULL;
if(!port) gao_bug("Null port");
log_debug("Nulling queue maps destined to port %lu", (unsigned long)port->gao_ifindex);
//TODO: For now, just consider the case of SW to SW queuing. In the future, allow HW QOS passthrough
//and direct binding of HW queues to queue maps.
for(index = 0; index < GAO_MAX_PORTS; index++) {
other_port = &resources->ports[index];
if(other_port->state != GAO_RESOURCE_STATE_ACTIVE) continue;
ingress_queue = other_port->rx_queues[0];
if(!ingress_queue) {
log_bug("Ingress side queue on active port %lu missing.", (unsigned long)index);
continue;
}
//Bind each subqueue on this queue to the other queue's map towards us
for(subqueue_index = 0; subqueue_index < GAO_MAX_PORT_SUBQUEUE; subqueue_index++) {
log_debug("Null port %lu queue %lu subqueue %lu ",
(unsigned long)other_port->gao_ifindex, (unsigned long)0, (unsigned long)subqueue_index);
ingress_queue->queue_map.port[port->gao_ifindex].ring[subqueue_index] = NULL;
}
}
err:
return;
}
/**
* Try to free a HW bound queue from a port. If the queue is bound to a file
* it will only set the state and wake the file. The queue is unbound from the
* port, but the file must then clean it up.
* @warning Caller must hold resource lock
* @param resources
* @param queue
*/
static void gao_free_port_queue(struct gao_resources *resources, struct gao_queue* queue) {
if(!queue) gao_error("Cannot delete queue, pointer null.");
//Anyone using the queue now will finish
queue->state = GAO_RESOURCE_STATE_DELETING;
synchronize_rcu();
//FIXME: Wake any file potentially waiting on the queue
//queue->ring->header.head_wake_condition_ref
queue->binding.port = NULL;
queue->binding.gao_ifindex = 0;
queue->binding.queue_index = 0;
queue->binding.direction_txrx = GAO_DIRECTION_NONE;
queue->binding.owner_type = GAO_QUEUE_OWNER_NONE;
queue->hw_private = NULL;
//If it is bound to a file, don't delete it. The file will see the state on next file op and clean it up.
if(!queue->binding.gao_file) {
gao_free_queue(resources, queue);
} else {
atomic_long_set(queue->ring->control.head_wake_condition_ref, 1);
wake_up_interruptible(queue->ring->control.head_wait_queue_ref);
}
err:
return;
}
/**
* Delete all the queues allocated to an interface.
* If the queues were bound to a file, don't delete them, but remove references.
* @warning Caller must hold resource lock
* @param resources
* @param interface
*/
void gao_delete_port_queues(struct gao_resources *resources, struct gao_port *port) {
uint64_t index;
struct gao_queue *queue = NULL;
//Remove references to our port from other ports.
gao_port_null_egress_to_ingress_queue_map(resources, port);
//Make sure nobody can be in the middle of transmitting these queues
//**This removes the requirement to check queue state in forwarding code (that is under RCU lock)
synchronize_rcu();
if(port->tx_arbiter_workqueue) {
//FIXME: Clean this up, the queue deletion routines also set the deleting state
//But we need to kill the arbiters here...
log_debug("Port %s[%lu] arbiter cleanup begins", gao_get_port_name(port), (unsigned long)port->gao_ifindex);
for(index = 0; index < port->num_tx_queues; index++) {
queue = port->tx_queues[index];
if(!queue) continue;
queue->state = GAO_RESOURCE_STATE_DELETING;
log_debug("Waking tx arbiter %lu for deletion", (unsigned long)index);
atomic_long_set(queue->ring->control.head_wake_condition_ref, 0);
wake_up_interruptible(queue->ring->control.head_wait_queue_ref);
atomic_long_set(queue->ring->control.tail_wake_condition_ref, ~0);
wake_up_interruptible(queue->ring->control.tail_wait_queue_ref);
}
log_debug("Port %s[%lu] draining arbiter workqueue...", gao_get_port_name(port), (unsigned long)port->gao_ifindex);
//This will block until all the tx arbiters have terminated
drain_workqueue(port->tx_arbiter_workqueue);
log_debug("Port %s[%lu] destroying arbiter workqueue...", gao_get_port_name(port), (unsigned long)port->gao_ifindex);
destroy_workqueue(port->tx_arbiter_workqueue);
port->tx_arbiter_workqueue = NULL;
}
for(index = 0; index < port->num_rx_queues; index++) {
queue = port->rx_queues[index];
if(!queue) continue;
port->rx_queues[index] = NULL;
gao_free_port_queue(resources, queue);
}
for(index = 0; index < port->num_tx_queues; index++) {
queue = port->tx_queues[index];
if(!queue) continue;
port->tx_queues[index] = NULL;
gao_free_port_queue(resources, queue);
}
}
/**
* From the perspective of one port, visit all other ports and bind our egress queues to their
* queue maps.
* @warning Caller must hold resource lock
* @param resources
* @param port
* @return
*/
static int64_t gao_port_bind_egress_to_ingress_queue_map(struct gao_resources *resources, struct gao_port *port) {
int64_t ret = 0;
uint64_t index = 0, subqueue_index;
struct gao_queue *egress_queue = NULL, *ingress_queue = NULL;
struct gao_port *other_port = NULL;
if(!port) gao_bug_val(-EINVAL, "Null port");
//TODO: For now, just consider the case of SW to SW queuing. In the future, allow HW QOS passthrough
//and direct binding of HW queues to queue maps.
egress_queue = port->tx_queues[0];
if(!egress_queue) gao_bug_val(-EINVAL, "Null egress queue");
//Traverse the other ports
for(index = 0; index < GAO_MAX_PORTS; index++) {
other_port = &resources->ports[index];
//If its ourselves, also bind it
if( (other_port->state != GAO_RESOURCE_STATE_ACTIVE) && (other_port != port)) continue;
//For now visit just their default queue
ingress_queue = other_port->rx_queues[0];
if(!ingress_queue) {
log_bug("Ingress side queue on active port %lu missing.", (unsigned long)index);
continue;
}
//Bind each subqueue on this queue to the other queue's map towards us
for(subqueue_index = 0; subqueue_index < GAO_MAX_PORT_SUBQUEUE; subqueue_index++) {
log_debug("Bind port %lu queue %lu subqueue %lu > port %lu queue %lu subqueue %lu",
(unsigned long)port->gao_ifindex, (unsigned long)0, (unsigned long)subqueue_index,
(unsigned long)other_port->gao_ifindex, (unsigned long)0, (unsigned long)subqueue_index);
ingress_queue->queue_map.port[port->gao_ifindex].ring[subqueue_index] =
egress_queue->subqueues[subqueue_index].ring;
}
}
return 0;
err:
return ret;
}
/**
* From the perspective of one port, traverse the other port's egress queues and bind their subqueues
* to our queue map.
* @warning Caller must hold resource lock
* @param resources
* @param port
* @return
*/
static int64_t gao_port_bind_ingress_to_egress_subqueues(struct gao_resources *resources, struct gao_port *port) {
int64_t ret;
uint64_t index, subqueue_index;
struct gao_port *other_port = NULL;
struct gao_queue *ingress_queue = NULL, *egress_queue = NULL;
if(!port) gao_bug_val(-EINVAL, "Null port");