-
Notifications
You must be signed in to change notification settings - Fork 33
/
wd_util.c
2885 lines (2361 loc) · 62.8 KB
/
wd_util.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
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
* Copyright 2020-2021 Linaro ltd.
*/
#define _GNU_SOURCE
#include <dirent.h>
#include <dlfcn.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <ctype.h>
#include "wd_sched.h"
#include "wd_util.h"
#define WD_ASYNC_DEF_POLL_NUM 1
#define WD_ASYNC_DEF_QUEUE_DEPTH 1024
#define WD_BALANCE_THRHD 1280
#define WD_RECV_MAX_CNT_SLEEP 60000000
#define WD_RECV_MAX_CNT_NOSLEEP 200000000
#define PRIVILEGE_FLAG 0600
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define WD_INIT_SLEEP_UTIME 1000
#define WD_INIT_RETRY_TIMES 10000
#define US2S(us) ((us) >> 20)
#define WD_INIT_RETRY_TIMEOUT 3
#define WD_SOFT_CTX_NUM 2
#define WD_SOFT_SYNC_CTX 0
#define WD_SOFT_ASYNC_CTX 1
#define WD_DRV_LIB_DIR "uadk"
struct msg_pool {
/* message array allocated dynamically */
void *msgs;
int *used;
__u32 msg_num;
__u32 msg_size;
int tail;
};
/* parse wd env begin */
/* define comp's combination of two operation type and two mode here */
static const char *comp_ctx_type[2][2] = {
{"sync-comp:", "sync-decomp:"},
{"async-comp:", "async-decomp:"}
};
/* define two ctx mode here for cipher and other alg */
static const char *ctx_type[2][1] = { {"sync:"}, {"async:"} };
static const char *wd_env_name[WD_TYPE_MAX] = {
"WD_COMP_CTX_NUM",
"WD_CIPHER_CTX_NUM",
"WD_DIGEST_CTX_NUM",
"WD_AEAD_CTX_NUM",
"WD_RSA_CTX_NUM",
"WD_DH_CTX_NUM",
"WD_ECC_CTX_NUM",
"WD_AGG_CTX_NUM",
};
struct async_task {
__u32 idx;
};
struct async_task_queue {
struct async_task *head;
int depth;
/* the producer offset of task queue */
int prod;
/* the consumer offset of task queue */
int cons;
int cur_task;
int left_task;
int end;
sem_t empty_sem;
sem_t full_sem;
pthread_mutex_t lock;
pthread_t tid;
int (*alg_poll_ctx)(__u32, __u32, __u32 *);
};
struct drv_lib_list {
void *dlhandle;
struct drv_lib_list *next;
};
struct acc_alg_item {
char *name;
char *algtype;
};
struct wd_ce_ctx {
char *drv_name;
void *priv;
};
static struct acc_alg_item alg_options[] = {
{"zlib", "zlib"},
{"gzip", "gzip"},
{"deflate", "deflate"},
{"lz77_zstd", "lz77_zstd"},
{"hashagg", "hashagg"},
{"rsa", "rsa"},
{"dh", "dh"},
{"ecdh", "ecdh"},
{"x25519", "x25519"},
{"x448", "x448"},
{"ecdsa", "ecdsa"},
{"sm2", "sm2"},
{"ecb(aes)", "cipher"},
{"cbc(aes)", "cipher"},
{"xts(aes)", "cipher"},
{"ofb(aes)", "cipher"},
{"cfb(aes)", "cipher"},
{"ctr(aes)", "cipher"},
{"cbc-cs1(aes)", "cipher"},
{"cbc-cs2(aes)", "cipher"},
{"cbc-cs3(aes)", "cipher"},
{"ecb(sm4)", "cipher"},
{"xts(sm4)", "cipher"},
{"cbc(sm4)", "cipher"},
{"ofb(sm4)", "cipher"},
{"cfb(sm4)", "cipher"},
{"ctr(sm4)", "cipher"},
{"cbc-cs1(sm4)", "cipher"},
{"cbc-cs2(sm4)", "cipher"},
{"cbc-cs3(sm4)", "cipher"},
{"ecb(des)", "cipher"},
{"cbc(des)", "cipher"},
{"ecb(des3_ede)", "cipher"},
{"cbc(des3_ede)", "cipher"},
{"ccm(aes)", "aead"},
{"gcm(aes)", "aead"},
{"ccm(sm4)", "aead"},
{"gcm(sm4)", "aead"},
{"authenc(hmac(sha256),cbc(aes))", "aead"},
{"authenc(hmac(sha256),cbc(sm4))", "aead"},
{"sm3", "digest"},
{"md5", "digest"},
{"sha1", "digest"},
{"sha256", "digest"},
{"sha224", "digest"},
{"sha384", "digest"},
{"sha512", "digest"},
{"sha512-224", "digest"},
{"sha512-256", "digest"},
{"cmac(aes)", "digest"},
{"gmac(aes)", "digest"},
{"xcbc-mac-96(aes)", "digest"},
{"xcbc-prf-128(aes)", "digest"},
{"", ""}
};
static void clone_ctx_to_internal(struct wd_ctx *ctx,
struct wd_ctx_internal *ctx_in)
{
ctx_in->ctx = ctx->ctx;
ctx_in->op_type = ctx->op_type;
ctx_in->ctx_mode = ctx->ctx_mode;
}
static int wd_shm_create(struct wd_ctx_config_internal *in)
{
int shm_size = sizeof(unsigned long) * WD_CTX_CNT_NUM;
void *ptr;
int shmid;
if (!wd_need_info())
return 0;
shmid = shmget(WD_IPC_KEY, shm_size, IPC_CREAT | PRIVILEGE_FLAG);
if (shmid < 0) {
WD_ERR("failed to get shared memory id(%d).\n", errno);
return -WD_EINVAL;
}
ptr = shmat(shmid, NULL, 0);
if (ptr == (void *)-1) {
WD_ERR("failed to get shared memory addr(%d).\n", errno);
return -WD_EINVAL;
}
memset(ptr, 0, shm_size);
in->shmid = shmid;
in->msg_cnt = ptr;
return 0;
}
static void wd_shm_delete(struct wd_ctx_config_internal *in)
{
if (!wd_need_info())
return;
/* deleted shared memory */
shmctl(in->shmid, IPC_RMID, NULL);
in->shmid = 0;
in->msg_cnt = NULL;
}
int wd_init_ctx_config(struct wd_ctx_config_internal *in,
struct wd_ctx_config *cfg)
{
struct wd_ctx_internal *ctxs;
__u32 i, j;
int ret;
if (!cfg->ctx_num) {
WD_ERR("invalid: ctx_num is 0!\n");
return -WD_EINVAL;
}
ret = wd_shm_create(in);
if (ret)
return ret;
ctxs = calloc(1, cfg->ctx_num * sizeof(struct wd_ctx_internal));
if (!ctxs) {
WD_ERR("failed to alloc memory for internal ctxs!\n");
ret = -WD_ENOMEM;
goto err_shm_del;
}
for (i = 0; i < cfg->ctx_num; i++) {
if (!cfg->ctxs[i].ctx) {
WD_ERR("invalid: ctx is NULL!\n");
ret = -WD_EINVAL;
goto err_out;
}
clone_ctx_to_internal(cfg->ctxs + i, ctxs + i);
ret = pthread_spin_init(&ctxs[i].lock, PTHREAD_PROCESS_SHARED);
if (ret) {
WD_ERR("failed to init ctxs lock!\n");
goto err_out;
}
}
in->ctxs = ctxs;
in->priv = cfg->priv;
in->ctx_num = cfg->ctx_num;
return 0;
err_out:
for (j = 0; j < i; j++)
pthread_spin_destroy(&ctxs[j].lock);
free(ctxs);
err_shm_del:
wd_shm_delete(in);
return ret;
}
int wd_init_sched(struct wd_sched *in, struct wd_sched *from)
{
if (!from->name || !from->sched_init ||
!from->pick_next_ctx || !from->poll_policy) {
WD_ERR("invalid: member of wd_sched is NULL!\n");
return -WD_EINVAL;
}
in->h_sched_ctx = from->h_sched_ctx;
in->name = strdup(from->name);
in->sched_init = from->sched_init;
in->pick_next_ctx = from->pick_next_ctx;
in->poll_policy = from->poll_policy;
return 0;
}
void wd_clear_sched(struct wd_sched *in)
{
char *name = (char *)in->name;
if (name)
free(name);
in->h_sched_ctx = 0;
in->name = NULL;
in->sched_init = NULL;
in->pick_next_ctx = NULL;
in->poll_policy = NULL;
}
void wd_clear_ctx_config(struct wd_ctx_config_internal *in)
{
__u32 i;
for (i = 0; i < in->ctx_num; i++)
pthread_spin_destroy(&in->ctxs[i].lock);
in->priv = NULL;
in->ctx_num = 0;
if (in->ctxs) {
free(in->ctxs);
in->ctxs = NULL;
}
wd_shm_delete(in);
}
void wd_memset_zero(void *data, __u32 size)
{
__u32 tmp = size;
char *s = data;
if (!s)
return;
while (tmp--)
*s++ = 0;
}
static void get_ctx_msg_num(struct wd_cap_config *cap, __u32 *msg_num)
{
if (!cap || !cap->ctx_msg_num)
return;
if (cap->ctx_msg_num > WD_POOL_MAX_ENTRIES) {
WD_INFO("ctx_msg_num %u is invalid, use default value: %u!\n",
cap->ctx_msg_num, *msg_num);
return;
}
*msg_num = cap->ctx_msg_num;
}
static int init_msg_pool(struct msg_pool *pool, __u32 msg_num, __u32 msg_size)
{
pool->msgs = calloc(1, msg_num * msg_size);
if (!pool->msgs) {
WD_ERR("failed to alloc memory for msgs arrary of msg pool!\n");
return -WD_ENOMEM;
}
pool->used = calloc(1, msg_num * sizeof(int));
if (!pool->used) {
free(pool->msgs);
pool->msgs = NULL;
WD_ERR("failed to alloc memory for used arrary of msg pool!\n");
return -WD_ENOMEM;
}
pool->msg_size = msg_size;
pool->msg_num = msg_num;
pool->tail = 0;
return 0;
}
static void uninit_msg_pool(struct msg_pool *pool)
{
if (!pool->msg_num)
return;
free(pool->msgs);
free(pool->used);
pool->msgs = NULL;
pool->used = NULL;
memset(pool, 0, sizeof(*pool));
}
int wd_init_async_request_pool(struct wd_async_msg_pool *pool, struct wd_ctx_config *config,
__u32 msg_num, __u32 msg_size)
{
__u32 pool_num = config->ctx_num;
__u32 i, j;
int ret;
pool->pool_num = pool_num;
pool->pools = calloc(1, pool_num * sizeof(struct msg_pool));
if (!pool->pools) {
WD_ERR("failed to alloc memory for async msg pools!\n");
return -WD_ENOMEM;
}
/* If user set valid msg num, use user's. */
get_ctx_msg_num(config->cap, &msg_num);
for (i = 0; i < pool_num; i++) {
if (config->ctxs[i].ctx_mode == CTX_MODE_SYNC)
continue;
ret = init_msg_pool(&pool->pools[i], msg_num, msg_size);
if (ret < 0)
goto err;
}
return 0;
err:
for (j = 0; j < i; j++)
uninit_msg_pool(&pool->pools[j]);
free(pool->pools);
pool->pools = NULL;
return ret;
}
void wd_uninit_async_request_pool(struct wd_async_msg_pool *pool)
{
__u32 i;
for (i = 0; i < pool->pool_num; i++)
uninit_msg_pool(&pool->pools[i]);
free(pool->pools);
pool->pools = NULL;
pool->pool_num = 0;
}
void *wd_find_msg_in_pool(struct wd_async_msg_pool *pool,
int ctx_idx, __u32 tag)
{
struct msg_pool *p;
__u32 msg_num;
if ((__u32)ctx_idx > pool->pool_num) {
WD_ERR("invalid: message ctx id index is %d!\n", ctx_idx);
return NULL;
}
p = &pool->pools[ctx_idx];
msg_num = p->msg_num;
/* tag value start from 1 */
if (tag == 0 || tag > msg_num) {
WD_ERR("invalid: message cache tag is %u!\n", tag);
return NULL;
}
return (void *)((uintptr_t)p->msgs + p->msg_size * (tag - 1));
}
int wd_get_msg_from_pool(struct wd_async_msg_pool *pool,
int ctx_idx, void **msg)
{
struct msg_pool *p = &pool->pools[ctx_idx];
__u32 msg_num = p->msg_num;
__u32 msg_size = p->msg_size;
__u32 cnt = 0;
__u32 idx = p->tail;
/* Scheduler set a sync ctx */
if (!msg_num)
return -WD_EINVAL;
while (__atomic_test_and_set(&p->used[idx], __ATOMIC_ACQUIRE)) {
idx = (idx + 1) % msg_num;
cnt++;
if (cnt == msg_num)
return -WD_EBUSY;
}
p->tail = (idx + 1) % msg_num;
*msg = (void *)((uintptr_t)p->msgs + msg_size * idx);
return idx + 1;
}
void wd_put_msg_to_pool(struct wd_async_msg_pool *pool, int ctx_idx, __u32 tag)
{
struct msg_pool *p = &pool->pools[ctx_idx];
__u32 msg_num = p->msg_num;
/* tag value start from 1 */
if (!tag || tag > msg_num) {
WD_ERR("invalid: message cache idx is %u!\n", tag);
return;
}
__atomic_clear(&p->used[tag - 1], __ATOMIC_RELEASE);
}
int wd_check_src_dst(void *src, __u32 in_bytes, void *dst, __u32 out_bytes)
{
if ((in_bytes && !src) || (out_bytes && !dst))
return -WD_EINVAL;
return 0;
}
int wd_check_datalist(struct wd_datalist *head, __u64 size)
{
struct wd_datalist *tmp = head;
__u64 list_size = 0;
while (tmp) {
if (tmp->data)
list_size += tmp->len;
tmp = tmp->next;
}
return list_size >= size ? 0 : -WD_EINVAL;
}
void dump_env_info(struct wd_env_config *config)
{
struct wd_env_config_per_numa *config_numa;
struct wd_ctx_range **ctx_table;
int i, j, k;
FOREACH_NUMA(i, config, config_numa) {
if (!config_numa->ctx_table)
continue;
ctx_table = config_numa->ctx_table;
WD_ERR("-> %s: %d: sync num: %u\n", __func__, i,
config_numa->sync_ctx_num);
WD_ERR("-> %s: %d: async num: %u\n", __func__, i,
config_numa->async_ctx_num);
for (j = 0; j < CTX_MODE_MAX; j++)
for (k = 0; k < config_numa->op_type_num; k++) {
WD_ERR("-> %d: [%d][%d].begin: %u\n",
i, j, k, ctx_table[j][k].begin);
WD_ERR("-> %d: [%d][%d].end: %u\n",
i, j, k, ctx_table[j][k].end);
WD_ERR("-> %d: [%d][%d].size: %u\n",
i, j, k, ctx_table[j][k].size);
}
}
}
static void *wd_get_config_numa(struct wd_env_config *config, int node)
{
struct wd_env_config_per_numa *config_numa;
int i;
FOREACH_NUMA(i, config, config_numa)
if (config_numa->node == node)
break;
if (i == config->numa_num) {
WD_ERR("invalid: missing numa node is %d!\n", node);
return NULL;
}
return config_numa;
}
static void wd_free_numa(struct wd_env_config *config)
{
struct wd_env_config_per_numa *config_numa;
int i;
FOREACH_NUMA(i, config, config_numa)
free(config_numa->dev);
free(config->config_per_numa);
config->config_per_numa = NULL;
config->numa_num = 0;
}
/**
* @numa_dev_num: number of devices of the same type (like sec2) on each numa.
* @numa_num: number of numa node that has this type of device.
*/
static __u16 wd_get_dev_numa(struct uacce_dev_list *head,
int *numa_dev_num, __u16 size)
{
struct uacce_dev_list *list = head;
__u16 numa_num = 0;
while (list) {
if (list->dev->numa_id >= size) {
WD_ERR("invalid: numa id is %d!\n", list->dev->numa_id);
return 0;
}
if (!numa_dev_num[list->dev->numa_id])
numa_num++;
numa_dev_num[list->dev->numa_id]++;
list = list->next;
}
return numa_num;
}
static void wd_set_numa_dev(struct uacce_dev_list *head,
struct wd_env_config *config)
{
struct uacce_dev_list *list = head;
struct wd_env_config_per_numa *config_numa;
struct uacce_dev *dev;
while (list) {
config_numa = wd_get_config_numa(config, list->dev->numa_id);
if (!config_numa)
break;
dev = config_numa->dev + config_numa->dev_num;
memcpy(dev, list->dev, sizeof(*list->dev));
config_numa->dev_num++;
list = list->next;
}
}
static int wd_set_config_numa(struct wd_env_config *config,
const int *numa_dev_num, int max_node)
{
struct wd_env_config_per_numa *config_numa;
int i;
config->config_per_numa = calloc(config->numa_num, sizeof(*config_numa));
if (!config->config_per_numa)
return -WD_ENOMEM;
config_numa = config->config_per_numa;
for (i = 0; i < max_node; i++) {
if (!numa_dev_num[i])
continue;
config_numa->node = i;
config_numa->dev = calloc(numa_dev_num[i],
sizeof(struct uacce_dev));
if (!config_numa->dev) {
/* free config_per_numa and all uacce dev */
wd_free_numa(config);
return -WD_ENOMEM;
}
config_numa->dev_num = 0;
config_numa++;
}
return 0;
}
static int wd_alloc_numa(struct wd_env_config *config,
const struct wd_alg_ops *ops)
{
struct uacce_dev_list *head;
int *numa_dev_num;
int ret, max_node;
max_node = numa_max_node() + 1;
if (max_node <= 0)
return -WD_EINVAL;
numa_dev_num = calloc(max_node, sizeof(int));
if (!numa_dev_num)
return -WD_ENOMEM;
/* get uacce_dev */
head = wd_get_accel_list(ops->alg_name);
if (!head) {
WD_ERR("invalid: no device to support %s\n", ops->alg_name);
ret = -WD_ENODEV;
goto free_numa_dev_num;
}
/* get numa num and device num of each numa from uacce_dev list */
config->numa_num = wd_get_dev_numa(head, numa_dev_num, max_node);
if (config->numa_num == 0 || config->numa_num > max_node) {
WD_ERR("invalid: numa number is %u!\n", config->numa_num);
ret = -WD_ENODEV;
goto free_list;
}
/* alloc and init config_per_numa and all uacce dev */
ret = wd_set_config_numa(config, numa_dev_num, max_node);
if (ret) {
WD_ERR("failed to set numa config, ret = %d!\n", ret);
goto free_list;
}
/* set device and device num for config numa from uacce_dev list */
wd_set_numa_dev(head, config);
wd_free_list_accels(head);
free(numa_dev_num);
return 0;
free_list:
config->numa_num = 0;
wd_free_list_accels(head);
free_numa_dev_num:
free(numa_dev_num);
return ret;
}
static int is_number(const char *str)
{
size_t i, len;
if (!str)
return 0;
len = strlen(str);
if (len == 0)
return 0;
if (len != 1 && str[0] == '0')
return 0;
for (i = 0; i < len; i++)
if (!(isdigit(str[i])))
return 0;
return 1;
}
static int str_to_bool(const char *s, bool *target)
{
int tmp;
if (!is_number(s))
return -WD_EINVAL;
tmp = strtol(s, NULL, 10);
if (tmp != 0 && tmp != 1)
return -WD_EINVAL;
*target = tmp;
return 0;
}
int wd_parse_async_poll_en(struct wd_env_config *config, const char *s)
{
int ret;
ret = str_to_bool(s, &config->enable_internal_poll);
if (ret)
WD_ERR("failed to parse async poll enable flag(%s)!\n", s);
return ret;
}
static int parse_num_on_numa(const char *s, int *num, int *node)
{
char *sep, *start, *left;
if (!strlen(s)) {
WD_ERR("invalid: input string length is zero!\n");
return -WD_EINVAL;
}
start = strdup(s);
if (!start)
return -WD_ENOMEM;
left = start;
sep = strsep(&left, "@");
if (!sep)
goto out;
if (is_number(sep) && is_number(left)) {
*num = strtol(sep, NULL, 10);
*node = strtol(left, NULL, 10);
free(start);
return 0;
}
out:
WD_ERR("invalid: input env format is %s!\n", s);
free(start);
return -WD_EINVAL;
}
static int wd_alloc_ctx_table_per_numa(struct wd_env_config_per_numa *config)
{
struct wd_ctx_range **ctx_table;
int i, j, ret;
if (config->ctx_table)
return 0;
ctx_table = calloc(1, sizeof(struct wd_ctx_range *) * CTX_MODE_MAX);
if (!ctx_table)
return -WD_ENOMEM;
for (i = 0; i < CTX_MODE_MAX; i++) {
ctx_table[i] = calloc(1,
sizeof(struct wd_ctx_range) *
config->op_type_num);
if (!ctx_table[i]) {
ret = -WD_ENOMEM;
goto free_mem;
}
}
config->ctx_table = ctx_table;
return 0;
free_mem:
for (j = 0; j < i; j++)
free(ctx_table[j]);
free(ctx_table);
return ret;
}
static void wd_free_ctx_table_per_numa(struct wd_env_config_per_numa *config)
{
int i;
if (!config->ctx_table)
return;
for (i = 0; i < CTX_MODE_MAX; i++)
free(config->ctx_table[i]);
free(config->ctx_table);
config->ctx_table = NULL;
}
static void wd_free_ctx_table(struct wd_env_config *config)
{
struct wd_env_config_per_numa *config_numa;
int i;
FOREACH_NUMA(i, config, config_numa)
wd_free_ctx_table_per_numa(config_numa);
}
static int get_and_fill_ctx_num(struct wd_env_config_per_numa *config_numa,
const char *p, int ctx_num)
{
struct wd_ctx_range **ctx_table = config_numa->ctx_table;
const char *type;
int i, j;
/**
* There're two types of environment variables, mode:num@node and
* mode-type:num@node, parse ctx num with comp_ctx_type and ctx_type.
*/
for (i = 0; i < CTX_MODE_MAX; i++)
for (j = 0; j < config_numa->op_type_num; j++) {
if (config_numa->op_type_num == 1)
type = ctx_type[i][j];
else
type = comp_ctx_type[i][j];
if (!strncmp(p, type, strlen(type))) {
ctx_table[i][j].size = ctx_num;
return 0;
}
}
return -WD_EINVAL;
}
static int wd_parse_section(struct wd_env_config *config, char *section)
{
struct wd_env_config_per_numa *config_numa;
int ctx_num, node, ret;
char *ctx_section;
ctx_section = index(section, ':');
if (!ctx_section) {
WD_ERR("invalid: ctx section got wrong format: %s!\n", section);
return -WD_EINVAL;
}
ctx_section++;
ret = parse_num_on_numa(ctx_section, &ctx_num, &node);
if (ret)
return ret;
config_numa = wd_get_config_numa(config, node);
if (!config_numa)
return -WD_EINVAL;
config_numa->op_type_num = config->op_type_num;
ret = wd_alloc_ctx_table_per_numa(config_numa);
if (ret)
return ret;
ret = get_and_fill_ctx_num(config_numa, section, ctx_num);
if (ret) {
WD_ERR("invalid: ctx section got wrong ctx type: %s!\n",
section);
wd_free_ctx_table_per_numa(config_numa);
return ret;
}
return 0;
}
static int get_start_ctx_index(struct wd_env_config *config,
struct wd_env_config_per_numa *config_numa)
{
struct wd_env_config_per_numa *config_cur = config->config_per_numa;
int start = 0;
for (; config_cur < config_numa; config_cur++)
start += config_cur->sync_ctx_num + config_cur->async_ctx_num;
return start;
}
static void set_ctx_index(struct wd_env_config_per_numa *config_numa,
__u8 mode, int *start)
{
struct wd_ctx_range **ctx_table = config_numa->ctx_table;
int size, i, sum = 0;
for (i = 0; i < config_numa->op_type_num; i++)
sum += ctx_table[mode][i].size;
if (mode)
config_numa->async_ctx_num = sum;
else
config_numa->sync_ctx_num = sum;
if (!sum)
return;
for (i = 0; i < config_numa->op_type_num; i++) {
size = ctx_table[mode][i].size;
if (!size)
continue;
ctx_table[mode][i].begin = *start;
ctx_table[mode][i].end = *start + size - 1;
*start += size;
}
}
static void wd_fill_ctx_table(struct wd_env_config *config)
{
struct wd_env_config_per_numa *config_numa;
int start, i, j;
FOREACH_NUMA(i, config, config_numa) {
if (!config_numa->ctx_table)
continue;
start = get_start_ctx_index(config, config_numa);
for (j = 0; j < CTX_MODE_MAX; j++)
set_ctx_index(config_numa, j, &start);
}
}
static int parse_ctx_num(struct wd_env_config *config, const char *s)
{
char *left, *section, *start;
int ret;
start = strdup(s);
if (!start)
return -WD_ENOMEM;
left = start;
while ((section = strsep(&left, ","))) {
ret = wd_parse_section(config, section);
if (ret)
goto err_free_ctx_table;
}
wd_fill_ctx_table(config);
free(start);
return 0;
err_free_ctx_table:
wd_free_ctx_table(config);
free(start);
return ret;
}
int wd_parse_ctx_num(struct wd_env_config *config, const char *s)
{
return parse_ctx_num(config, s);
}
int wd_parse_async_poll_num(struct wd_env_config *config, const char *s)
{
struct wd_env_config_per_numa *config_numa;
char *left, *section, *start;
int node, poll_num, ret;
if (!config->enable_internal_poll) {
WD_ERR("internal poll not enabled, skip parse poll number!\n");
return 0;
}
start = strdup(s);
if (!start)
return -ENOMEM;
left = start;
while ((section = strsep(&left, ","))) {
ret = parse_num_on_numa(section, &poll_num, &node);
if (ret)
goto out;