summaryrefslogtreecommitdiff
path: root/0061-cdi-support-modules-operate-registry-annotations.patch
blob: aa26ca286ae754cf2f2c0af3c521dc9dd54d2d94 (plain)
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
From 4527dc8b6f7ab438742a8f7403b24420f646236d Mon Sep 17 00:00:00 2001
From: liuxu <liuxu156@huawei.com>
Date: Mon, 8 Apr 2024 11:57:16 +0800
Subject: [PATCH 61/69] cdi:support modules operate/registry/annotations

---
 .../modules/device/cdi/cdi_annotations.c      |  72 +++++++
 src/daemon/modules/device/cdi/cdi_registry.c  |  14 +-
 src/daemon/modules/device/cdi_operate.c       |  53 +++++-
 src/utils/cutils/utils.c                      |  11 ++
 src/utils/cutils/utils.h                      |   2 +
 src/utils/cutils/utils_array.c                | 175 +++++++++++++++++-
 src/utils/cutils/utils_array.h                |  31 ++++
 7 files changed, 350 insertions(+), 8 deletions(-)

diff --git a/src/daemon/modules/device/cdi/cdi_annotations.c b/src/daemon/modules/device/cdi/cdi_annotations.c
index cfe6e099..020816d7 100644
--- a/src/daemon/modules/device/cdi/cdi_annotations.c
+++ b/src/daemon/modules/device/cdi/cdi_annotations.c
@@ -21,12 +21,84 @@
 
 #include "error.h"
 #include "utils.h"
+#include "utils_array.h"
 #include "cdi_parser.h"
 
 #define CDI_ANNOTATIONS_PREFIX "cdi.k8s.io/"
 
+static int parse_devices(string_array *devices, const char *value, char **error)
+{
+    __isula_auto_array_t char **parts = NULL;
+    char **pos;
+
+    parts = util_string_split(value, ',');
+    if (parts == NULL) {
+        ERROR("Invalid CDI device value %s", value);
+        format_errorf(error, "Invalid CDI device value %s", value);
+        return -1;
+    }
+    for (pos = parts; pos != NULL && *pos != NULL; pos++) {
+        if (!cdi_parser_is_qualified_name(*pos)) {
+            ERROR("Invalid CDI device name %s", *pos);
+            format_errorf(error, "Invalid CDI device name %s", *pos);
+            return -1;
+        }
+        if (util_append_string_array(devices, *pos) != 0) {
+            ERROR("Out of memory");
+            *error = util_strdup_s("Out of memory");
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
 int cdi_parse_annotations(json_map_string_string *annotations, string_array **keys,
                           string_array **devices, char **error)
 {
+    char *key = NULL;
+    char *value = NULL;
+    size_t i;
+    __isula_auto_string_array_t string_array *keys_array = NULL;
+    __isula_auto_string_array_t string_array *devices_array = NULL;
+
+    if (annotations == NULL || keys == NULL || devices == NULL || error == NULL) {
+        ERROR("Invalid argument");
+        return -1;
+    }
+
+    keys_array = util_common_calloc_s(sizeof(*keys_array));
+    if (keys_array == NULL) {
+        ERROR("Out of memory");
+        *error = util_strdup_s("Out of memory");
+        return -1;
+    }
+    devices_array = util_common_calloc_s(sizeof(*devices_array));
+    if (devices_array == NULL) {
+        ERROR("Out of memory");
+        *error = util_strdup_s("Out of memory");
+        return -1;
+    }
+
+    for (i = 0; i < annotations->len; i++) {
+        key = annotations->keys[i];
+        value = annotations->values[i];
+        if (!util_has_prefix(key, CDI_ANNOTATIONS_PREFIX)) {
+            continue;
+        }
+        if (parse_devices(devices_array, value, error) != 0) {
+            return -1;
+        }
+        if (util_append_string_array(keys_array, key) != 0) {
+            ERROR("Out of memory");
+            *error = util_strdup_s("Out of memory");
+            return -1;
+        }
+    }
+
+    *keys = keys_array;
+    keys_array = NULL;
+    *devices = devices_array;
+    devices_array = NULL;
     return 0;
 }
diff --git a/src/daemon/modules/device/cdi/cdi_registry.c b/src/daemon/modules/device/cdi/cdi_registry.c
index 68767a5f..be381132 100644
--- a/src/daemon/modules/device/cdi/cdi_registry.c
+++ b/src/daemon/modules/device/cdi/cdi_registry.c
@@ -14,12 +14,24 @@
  ******************************************************************************/
 #include "cdi_registry.h"
 
+#include <util_atomic.h>
+#include <isula_libutils/auto_cleanup.h>
+
+static struct cdi_registry g_cdi_reg = { 0 };
+
 int cdi_registry_init(string_array *spec_dirs)
 {
+    // isulad will use default dirs when spec_dirs == NULL
+    g_cdi_reg.cdi_cache = cdi_new_cache(spec_dirs);
+    if (g_cdi_reg.cdi_cache == NULL) {
+        ERROR("Failed to init registry");
+        return -1;
+    }
+    g_cdi_reg.ops = cdi_get_cache_ops();
     return 0;
 }
 
 struct cdi_registry *cdi_get_registry(void)
 {
-    return NULL;
+    return &g_cdi_reg;
 }
diff --git a/src/daemon/modules/device/cdi_operate.c b/src/daemon/modules/device/cdi_operate.c
index c5187ab1..f99bb7e4 100644
--- a/src/daemon/modules/device/cdi_operate.c
+++ b/src/daemon/modules/device/cdi_operate.c
@@ -14,23 +14,66 @@
  ******************************************************************************/
 #include "cdi_operate_api.h"
 
+#include <isula_libutils/log.h>
+
+#include "utils.h"
+#include "error.h"
+#include "cdi_registry.h"
+#include "cdi_annotations.h"
+#include "cdi_spec_dirs.h"
+
 int cdi_operate_registry_init(char **specs_dirs, size_t specs_dirs_len)
 {
-    return 0;
+    string_array spec_dirs_array = {
+        .items = specs_dirs,
+        .len = specs_dirs_len,
+        .cap = specs_dirs_len,
+    };
+    
+    return cdi_registry_init(&spec_dirs_array);
 }
 
 int cdi_operate_refresh(void)
 {
-    return 0;
+    struct cdi_registry *registry = cdi_get_registry();
+    if (registry == NULL || registry->ops == NULL || registry->ops->refresh == NULL) {
+        ERROR("Failed to get registry");
+        return -1;
+    }
+    
+    return registry->ops->refresh(registry->cdi_cache);
 }
 
 int cdi_operate_inject_devices(oci_runtime_spec *spec, string_array *devices)
 {
-    return 0;
+    struct cdi_registry *registry = NULL;
+
+    if (spec == NULL || devices == NULL) {
+        ERROR("Invalid params");
+        return -1;
+    }
+    
+    registry = cdi_get_registry();
+    if (registry == NULL || registry->ops == NULL || registry->ops->inject_devices == NULL) {
+        ERROR("Failed to get registry");
+        return -1;
+    }
+    
+    return registry->ops->inject_devices(registry->cdi_cache, spec, devices);
 }
 
 int cdi_operate_parse_annotations(json_map_string_string *annotations, string_array **keys,
                                   string_array **devices, char **error)
 {
-    return 0;
-}
\ No newline at end of file
+    if (error == NULL) {
+        ERROR("Invalid argument");
+        return -1;
+    }
+    if (annotations == NULL || keys == NULL || devices == NULL) {
+        ERROR("Invalid params");
+        *error = util_strdup_s("Invalid params");
+        return -1;
+    }
+
+    return cdi_parse_annotations(annotations, keys, devices, error);
+}
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index 8da2cc60..9a33f935 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -59,6 +59,17 @@ int malloc_trim(size_t pad)
 }
 #endif
 
+void util_swap_ptr(void **p1, void **p2)
+{
+    void *tmp;
+    if (p1 == NULL || p2 == NULL) {
+        return;
+    }
+    tmp = *p1;
+    *p1 = *p2;
+    *p2 = tmp;
+}
+
 int util_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize)
 {
     void *tmp = NULL;
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 3acf0698..3671272a 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -320,6 +320,8 @@ struct signame {
         }                         \
     } while (0)
 
+void util_swap_ptr(void **p1, void **p2);
+
 int util_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize);
 
 int util_check_inherited(bool closeall, int fd_to_ignore);
diff --git a/src/utils/cutils/utils_array.c b/src/utils/cutils/utils_array.c
index 25f19b8b..72294005 100644
--- a/src/utils/cutils/utils_array.c
+++ b/src/utils/cutils/utils_array.c
@@ -86,6 +86,27 @@ void util_free_sensitive_array(char **array)
     free(array);
 }
 
+char **util_copy_array_by_len(char **array, size_t len)
+{
+    char **new_array = NULL;
+    size_t i;
+ 
+    if (array == NULL || len == 0) {
+        return NULL;
+    }
+ 
+    new_array = util_smart_calloc_s(sizeof(char *), len);
+    if (new_array == NULL) {
+        ERROR("Out of memory");
+        return NULL;
+    }
+ 
+    for (i = 0; i < len; i++) {
+        new_array[i] = util_strdup_s(array[i]);
+    }
+    return new_array;
+}
+
 int util_array_append(char ***array, const char *element)
 {
     size_t len;
@@ -166,7 +187,7 @@ bool util_array_contain(const char **array, const char *element)
     return false;
 }
 
-static size_t get_string_array_scale_size(size_t old_size)
+static size_t get_array_scale_size(size_t old_size)
 {
 #define DOUBLE_THRESHOLD 1024
     const size_t max_threshold = MAX_MEMORY_SIZE / sizeof(char *);
@@ -188,7 +209,7 @@ static size_t get_string_array_scale_size(size_t old_size)
 
 static bool do_expand_array(string_array *array)
 {
-    size_t new_size = get_string_array_scale_size(array->cap);
+    size_t new_size = get_array_scale_size(array->cap);
     char **new_items = NULL;
 
     // array capability sure less than MAX_MEMORY_SIZE
@@ -237,6 +258,29 @@ out:
     return 0;
 }
 
+string_array *util_copy_string_array(string_array *sarr)
+{
+    string_array *ptr = NULL;
+    size_t i;
+ 
+    if (sarr == NULL) {
+        ERROR("Invalid string array");
+        return NULL;
+    }
+ 
+    ptr = util_string_array_new(sarr->cap);
+    if (ptr == NULL) {
+        ERROR("Out of memory");
+        return NULL;
+    }
+    for (i = 0; i < sarr->len; i++) {
+        ptr->items[i] = util_strdup_s(sarr->items[i]);
+        ptr->len += 1;
+    }
+ 
+    return ptr;
+}
+
 bool util_string_array_contain(const string_array *sarr, const char *elem)
 {
     size_t i;
@@ -339,3 +383,130 @@ int util_common_array_append_pointer(void ***array, void *element)
 
     return 0;
 }
+
+void *util_clone_ptr(void *item)
+{
+    return item;
+}
+
+common_array *util_common_array_new(size_t len, free_common_array_item_cb free_item_cb,
+                                    clone_common_array_item_cb clone_item_cb)
+{
+    common_array *ptr = NULL;
+
+    if (len == 0 || free_item_cb == NULL || clone_item_cb == NULL) {
+        return NULL;
+    }
+
+    ptr = (common_array *)util_common_calloc_s(sizeof(common_array));
+    if (ptr == NULL) {
+        ERROR("Out of memory");
+        return NULL;
+    }
+
+    ptr->items = (void **)util_smart_calloc_s(sizeof(void *), len);
+    if (ptr->items == NULL) {
+        ERROR("Out of memory");
+        free(ptr);
+        return NULL;
+    }
+
+    ptr->len = 0;
+    ptr->cap = len;
+    ptr->free_item_cb = free_item_cb;
+    ptr->clone_item_cb = clone_item_cb;
+
+    return ptr;
+}
+
+void util_free_common_array(common_array *ptr)
+{
+    size_t i;
+
+    if (ptr == NULL || ptr->free_item_cb == NULL) {
+        return;
+    }
+
+    for (i = 0; i < ptr->len; i++) {
+        ptr->free_item_cb(ptr->items[i]);
+        ptr->items[i] = NULL;
+    }
+    free(ptr->items);
+    ptr->items = NULL;
+    ptr->len = 0;
+    ptr->cap = 0;
+    ptr->free_item_cb = NULL;
+    ptr->clone_item_cb = NULL;
+
+    free(ptr);
+}
+
+static bool do_expand_common_array(common_array *array)
+{
+    size_t new_size = get_array_scale_size(array->cap);
+    void **new_items = NULL;
+
+    // array capability sure less than MAX_MEMORY_SIZE
+    // so we need to check Overflow:
+    if (new_size == array->cap) {
+        ERROR("Too large common array, overflow memory");
+        return false;
+    }
+
+    // new_size * sizeof(*new_items) and list->len * sizeof(*list->items)
+    if (util_mem_realloc((void **)&new_items, new_size * sizeof(void *), (void *)array->items,
+                         array->len * sizeof(void *)) != 0) {
+        ERROR("Out of memory");
+        return false;
+    }
+    array->items = new_items;
+    array->cap = new_size;
+
+    return true;
+}
+
+int util_append_common_array(common_array *arr, void *val)
+{
+    if (arr == NULL || arr->clone_item_cb == NULL) {
+        ERROR("Invalid common array");
+        return -1;
+    }
+
+    if (val == NULL) {
+        DEBUG("Empty new item, just ignore it");
+        return 0;
+    }
+
+    if (arr->len < arr->cap) {
+        goto out;
+    }
+
+    // expand common array
+    if (!do_expand_common_array(arr)) {
+        return -1;
+    }
+
+out:
+    arr->items[arr->len] = arr->clone_item_cb(val);
+    arr->len += 1;
+    return 0;
+}
+
+int util_merge_common_array(common_array *dest_arr, common_array *src_arr)
+{
+    size_t i;
+
+    if (dest_arr == NULL || dest_arr->clone_item_cb == NULL ||
+        src_arr == NULL || src_arr->clone_item_cb == NULL) {
+        ERROR("Invalid common array");
+        return -1;
+    }
+
+    for (i = 0; i < src_arr->len; i++) {
+        if (util_append_common_array(dest_arr, src_arr->items[i]) != 0) {
+            ERROR("Failed to append element");
+            return -1;
+        }
+    }
+    return 0;
+}
diff --git a/src/utils/cutils/utils_array.h b/src/utils/cutils/utils_array.h
index 1c084595..0c4fd217 100644
--- a/src/utils/cutils/utils_array.h
+++ b/src/utils/cutils/utils_array.h
@@ -30,6 +30,8 @@ void util_free_array_by_len(char **array, size_t len);
 
 void util_free_array(char **array);
 
+char **util_copy_array_by_len(char **array, size_t len);
+
 int util_grow_array(char ***orig_array, size_t *orig_capacity, size_t size,
                     size_t increment);
 
@@ -52,6 +54,8 @@ void util_free_string_array(string_array *ptr);
 
 int util_append_string_array(string_array *sarr, const char *val);
 
+string_array *util_copy_string_array(string_array *sarr);
+
 bool util_string_array_contain(const string_array *sarr, const char *elem);
 
 void util_free_sensitive_array(char **array);
@@ -63,6 +67,33 @@ define_auto_cleanup_callback(util_free_array, char *);
 // define auto free macro for char *
 #define __isula_auto_array_t auto_cleanup_tag(util_free_array)
 
+define_auto_cleanup_callback(util_free_string_array, string_array);
+#define __isula_auto_string_array_t auto_cleanup_tag(util_free_string_array)
+
+typedef void (*free_common_array_item_cb)(void *item);
+typedef void *(*clone_common_array_item_cb)(void *item);
+typedef struct common_array_t {
+    void **items;
+    size_t len;
+    size_t cap;
+    free_common_array_item_cb free_item_cb;
+    clone_common_array_item_cb clone_item_cb;
+} common_array;
+
+void *util_clone_ptr(void *item);
+
+common_array *util_common_array_new(size_t len, free_common_array_item_cb free_item_cb,
+                                    clone_common_array_item_cb clone_item_cb);
+
+void util_free_common_array(common_array *ptr);
+
+int util_append_common_array(common_array *arr, void *val);
+
+int util_merge_common_array(common_array *dest_arr, common_array *src_arr);
+
+define_auto_cleanup_callback(util_free_common_array, common_array);
+#define __isula_auto_common_array_t auto_cleanup_tag(util_free_common_array)
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.34.1