api.c 30.2 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
H
haozi007 已提交
3 4 5 6
 * clibcni licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
H
haozi007 已提交
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
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
 * Author: tanyifeng
 * Create: 2019-04-25
 * Description: provide cni api functions
 ********************************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#include "api.h"

#include "log.h"
#include "invoke_errno.h"
#include "current.h"
#include "conf.h"
#include "args.h"
#include "tools.h"
#include "exec.h"
#include "utils.h"
#include "types.h"

static int add_network_list(const struct network_config_list *list, const struct runtime_conf *rc,
                            const char * const *paths, size_t paths_len, struct result **pret, char **err);

static int del_network_list(const struct network_config_list *list, const struct runtime_conf *rc,
                            const char * const *paths, size_t paths_len, char **err);

static int add_network(const struct network_config *net, const struct runtime_conf *rc, const char * const *paths,
                       size_t paths_len, struct result **add_result, char **err);

static int del_network(const struct network_config *net, const struct runtime_conf *rc, const char * const *paths,
                       size_t paths_len, char **err);

static int args(const char *action, const struct runtime_conf *rc, const char * const *paths, size_t paths_len,
                struct cni_args **cargs, char **err);

static int copy_cni_port_mapping(port_mapping *dst, const struct cni_port_mapping *src)
{
    bool invalid_arg = (dst == NULL || src == NULL);
    if (invalid_arg) {
        return -1;
    }
    if (src->protocol != NULL) {
H
haozi007 已提交
56
        dst->protocol = clibcni_util_strdup_s(src->protocol);
O
overweight 已提交
57 58
    }
    if (src->host_ip != NULL) {
H
haozi007 已提交
59
        dst->host_ip = clibcni_util_strdup_s(src->host_ip);
O
overweight 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    }
    dst->container_port = src->container_port;
    dst->host_port = src->host_port;

    return 0;
}

static int inject_port_mappings(const struct runtime_conf *rt, net_conf_runtime_config *rt_config, char **err)
{
    size_t j = 0;

    if (rt_config->port_mappings != NULL) {
        for (j = 0; j < rt_config->port_mappings_len; j++) {
            free_port_mapping(rt_config->port_mappings[j]);
            rt_config->port_mappings[j] = NULL;
        }
        free(rt_config->port_mappings);
        rt_config->port_mappings = NULL;
    }

    if (rt->p_mapping_len > (SIZE_MAX / sizeof(port_mapping *))) {
H
haozi007 已提交
81
        *err = clibcni_util_strdup_s("Too many mapping");
O
overweight 已提交
82 83 84 85
        ERROR("Too many mapping");
        return -1;
    }

H
haozi007 已提交
86
    rt_config->port_mappings = clibcni_util_common_calloc_s(sizeof(port_mapping *) * (rt->p_mapping_len));
O
overweight 已提交
87
    if (rt_config->port_mappings == NULL) {
H
haozi007 已提交
88
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
89 90 91 92
        ERROR("Out of memory");
        return -1;
    }
    for (j = 0; j < rt->p_mapping_len; j++) {
H
haozi007 已提交
93
        rt_config->port_mappings[j] = clibcni_util_common_calloc_s(sizeof(port_mapping));
O
overweight 已提交
94
        if (rt_config->port_mappings[j] == NULL) {
H
haozi007 已提交
95
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
96 97 98 99 100
            ERROR("Out of memory");
            return -1;
        }
        (rt_config->port_mappings_len)++;
        if (copy_cni_port_mapping(rt_config->port_mappings[j], rt->p_mapping[j]) != 0) {
H
haozi007 已提交
101
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            ERROR("Out of memory");
            return -1;
        }
    }
    return 0;
}

static int inject_runtime_config_items(const struct network_config *orig, const struct runtime_conf *rt,
                                       net_conf_runtime_config **rt_config, bool *inserted, char **err)
{
    char *work = NULL;
    bool value = false;
    int ret = -1;
    size_t i = 0;

H
haozi007 已提交
117
    *rt_config = clibcni_util_common_calloc_s(sizeof(net_conf_runtime_config));
O
overweight 已提交
118
    if (*rt_config == NULL) {
H
haozi007 已提交
119
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        ERROR("Out of memory");
        goto free_out;
    }
    for (i = 0; i < orig->network->capabilities->len; i++) {
        work = orig->network->capabilities->keys[i];
        value = orig->network->capabilities->values[i];
        if (!value || work == NULL) {
            continue;
        }
        if (strcmp(work, "portMappings") == 0 && rt->p_mapping_len > 0) {
            if (inject_port_mappings(rt, *rt_config, err) != 0) {
                ERROR("Inject port mappings failed");
                goto free_out;
            }
            *inserted = true;
        }
        /* new capabilities add here */
    }
    ret = 0;
free_out:
    return ret;
}

static int do_generate_net_conf_json(const struct network_config *orig, char **result, char **err)
{
    struct parser_context ctx = { OPT_GEN_SIMPLIFY, 0 };
    parser_error jerr = NULL;
    int ret = 0;

    /* generate new json str for injected config */
    *result = net_conf_generate_json(orig->network, &ctx, &jerr);
    if (*result == NULL) {
        if (asprintf(err, "generate json failed: %s", jerr) < 0) {
H
haozi007 已提交
153
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
            ERROR("Out of memory");
        }
        ERROR("Generate json: %s", jerr);
        ret = -1;
        goto out;
    }

out:
    free(jerr);
    return ret;
}

static inline bool check_inject_runtime_config_args(const struct network_config *orig, const struct runtime_conf *rt,
                                                    char * const *result, char * const *err)
{
    return (orig == NULL || rt == NULL || result == NULL || err == NULL);
}

static int inject_runtime_config(const struct network_config *orig, const struct runtime_conf *rt, char **result,
                                 char **err)
{
    bool insert_rt_config = false;
    int ret = -1;
    net_conf_runtime_config *rt_config = NULL;
    net_conf_runtime_config *save_conf = NULL;

    if (check_inject_runtime_config_args(orig, rt, result, err)) {
        ERROR("Invalid arguments");
        return -1;
    }

    if (orig->network == NULL || orig->network->capabilities == NULL) {
        return 0;
    }

    save_conf = orig->network->runtime_config;

    ret = inject_runtime_config_items(orig, rt, &rt_config, &insert_rt_config, err);
    if (ret != 0) {
        ERROR("inject runtime config failed: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    if (!insert_rt_config) {
        goto generate_result;
    }

    orig->network->runtime_config = rt_config;

generate_result:
    ret = do_generate_net_conf_json(orig, result, err);

free_out:
    orig->network->runtime_config = save_conf;
    free_net_conf_runtime_config(rt_config);
    if (ret != 0) {
        free(*result);
        *result = NULL;
    }
    return ret;
}

static int do_inject_prev_result(const struct result *prev_result, net_conf *work, char **err)
{
    if (prev_result == NULL) {
        return 0;
    }

    free_result_curr(work->prev_result);
    work->prev_result = result_curr_to_json_result(prev_result, err);
    if (work->prev_result == NULL) {
        return -1;
    }
    return 0;
}

static inline bool check_build_one_config(const struct network_config_list *list, const struct network_config *orig,
                                          const struct runtime_conf *rt, char * const *result, char * const *err)
{
    return (list == NULL || orig == NULL || rt == NULL || result == NULL || err == NULL);
}

static int build_one_config(const struct network_config_list *list, struct network_config *orig,
                            const struct result *prev_result, const struct runtime_conf *rt, char **result, char **err)
{
    int ret = -1;
    net_conf *work = NULL;

    if (check_build_one_config(list, orig, rt, result, err)) {
        ERROR("Invalid arguments");
        return ret;
    }

    work = orig->network;
    free(work->name);
H
haozi007 已提交
249
    work->name = clibcni_util_strdup_s(list->list->name);
O
overweight 已提交
250
    free(work->cni_version);
H
haozi007 已提交
251
    work->cni_version = clibcni_util_strdup_s(list->list->cni_version);
O
overweight 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265

    if (do_inject_prev_result(prev_result, work, err) != 0) {
        ERROR("Inject pre result failed: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    if (inject_runtime_config(orig, rt, result, err) != 0) {
        ERROR("Inject runtime config failed: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = 0;
free_out:
    if (ret != 0 && *err == NULL) {
H
haozi007 已提交
266
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    }
    return ret;
}

static int do_check_generate_net_conf_json(char **full_conf_bytes, struct network_config *pnet, char **err)
{
    struct parser_context ctx = { OPT_GEN_SIMPLIFY, 0 };
    parser_error serr = NULL;
    int ret = 0;

    if (*full_conf_bytes != NULL) {
        pnet->bytes = *full_conf_bytes;
        *full_conf_bytes = NULL;
    } else {
        pnet->bytes = net_conf_generate_json(pnet->network, &ctx, &serr);
        if (pnet->bytes == NULL) {
            if (asprintf(err, "Generate json failed: %s", serr) < 0) {
H
haozi007 已提交
284
                *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
            }
            ERROR("Generate json: %s", serr);
            ret = -1;
            goto out;
        }
    }

out:
    free(serr);
    return ret;
}

static int run_cni_plugin(const struct network_config_list *list, size_t i, const char *operator,
                          const struct runtime_conf *rc, const char * const *paths, size_t paths_len,
                          struct result **pret, char **err)
{
    int ret = -1;
    struct network_config net = { 0 };
    char *plugin_path = NULL;
    struct cni_args *cargs = NULL;
    char *full_conf_bytes = NULL;
    struct result *tmp_result = NULL;
    int save_errno = 0;

    net.network = list->list->plugins[i];
    if (net.network == NULL) {
H
haozi007 已提交
311
        *err = clibcni_util_strdup_s("Empty network");
O
overweight 已提交
312 313 314 315 316 317 318
        ERROR("Empty network");
        goto free_out;
    }

    ret = find_in_path(net.network->type, paths, paths_len, &plugin_path, &save_errno);
    if (ret != 0) {
        if (asprintf(err, "find plugin: \"%s\" failed: %s", net.network->type, get_invoke_err_msg(save_errno)) < 0) {
H
haozi007 已提交
319
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        }
        ERROR("find plugin: \"%s\" failed: %s", net.network->type, get_invoke_err_msg(save_errno));
        goto free_out;
    }

    tmp_result = pret != NULL ? *pret : NULL;
    ret = build_one_config(list, &net, tmp_result, rc, &full_conf_bytes, err);
    if (ret != 0) {
        ERROR("build config failed: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = do_check_generate_net_conf_json(&full_conf_bytes, &net, err);
    if (ret != 0) {
        ERROR("check gengerate net config failed: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = args(operator, rc, paths, paths_len, &cargs, err);
    if (ret != 0) {
        ERROR("get plugin arguments failed: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    if (pret == NULL) {
        ret = exec_plugin_without_result(plugin_path, net.bytes, cargs, err);
    } else {
        free_result(*pret);
        *pret = NULL;
        ret = exec_plugin_with_result(plugin_path, net.bytes, cargs, pret, err);
    }
free_out:
    free_cni_args(cargs);
    free(plugin_path);
    free(net.bytes);
    return ret;
}

static inline bool check_add_network_list_args(const struct network_config_list *list, const struct runtime_conf *rc,
                                               struct result * const *pret, char * const *err)
{
    return (list == NULL || list->list == NULL || rc == NULL || pret == NULL || err == NULL);
}

static int add_network_list(const struct network_config_list *list, const struct runtime_conf *rc,
                            const char * const *paths, size_t paths_len, struct result **pret, char **err)
{
    int ret = -1;
    size_t i = 0;
    struct result *prev_result = NULL;

    if (check_add_network_list_args(list, rc, pret, err)) {
        ERROR("Empty arguments");
        return -1;
    }

    for (i = 0; i < list->list->plugins_len; i++) {
        ret = run_cni_plugin(list, i, "ADD", rc, paths, paths_len, &prev_result, err);
        if (ret != 0) {
            ERROR("Run ADD cni failed: %s", *err != NULL ? *err : "");
            goto free_out;
        }
    }

    *pret = prev_result;
    ret = 0;
free_out:
    if (ret != 0) {
        free_result(prev_result);
    }
    return ret;
}

static inline bool check_del_network_list_args(const struct network_config_list *list, const struct runtime_conf *rc,
                                               char * const *err)
{
    return (list == NULL || list->list == NULL || rc == NULL || err == NULL);
}

static int del_network_list(const struct network_config_list *list, const struct runtime_conf *rc,
                            const char * const *paths, size_t paths_len, char **err)
{
    size_t i = 0;
    int ret = 0;

    if (check_del_network_list_args(list, rc, err)) {
        ERROR("Empty arguments");
        return -1;
    }

    for (i = list->list->plugins_len; i > 0; i--) {
        ret = run_cni_plugin(list, (i - 1), "DEL", rc, paths, paths_len, NULL, err);
        if (ret != 0) {
            ERROR("Run DEL cni failed: %s", *err != NULL ? *err : "");
            goto free_out;
        }
    }

free_out:
    return ret;
}

static inline bool check_add_network_args(const struct network_config *net, const struct runtime_conf *rc,
                                          char * const *err)
{
    return (net == NULL || rc == NULL || err == NULL);
}

static int add_network(const struct network_config *net, const struct runtime_conf *rc, const char * const *paths,
                       size_t paths_len, struct result **add_result, char **err)
{
    int ret = 0;
    char *plugin_path = NULL;
    char *net_bytes = NULL;
    struct cni_args *cargs = NULL;
    int save_errno = 0;

    if (check_add_network_args(net, rc, err)) {
        ERROR("Empty arguments");
        return -1;
    }
    ret = find_in_path(net->network->type, paths, paths_len, &plugin_path, &save_errno);
    if (ret != 0) {
        if (asprintf(err, "find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno)) < 0) {
H
haozi007 已提交
444
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        }
        ERROR("find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno));
        goto free_out;
    }

    ret = inject_runtime_config(net, rc, &net_bytes, err);
    if (ret != 0) {
        ERROR("Inject runtime config: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = args("ADD", rc, paths, paths_len, &cargs, err);
    if (ret != 0) {
        ERROR("Get ADD cni arguments: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = exec_plugin_with_result(plugin_path, net_bytes, cargs, add_result, err);
free_out:
    free(plugin_path);
    free(net_bytes);
    free_cni_args(cargs);
    return ret;
}

static inline bool check_del_network_args(const struct network_config *net, const struct runtime_conf *rc,
                                          char * const *err)
{
    return (net == NULL || net->network == NULL || rc == NULL || err == NULL);
}

static int del_network(const struct network_config *net, const struct runtime_conf *rc, const char * const *paths,
                       size_t paths_len, char **err)
{
    int ret = 0;
    char *plugin_path = NULL;
    char *net_bytes = NULL;
    struct cni_args *cargs = NULL;
    int save_errno = 0;

    if (check_del_network_args(net, rc, err)) {
        ERROR("Empty arguments");
        return -1;
    }
    ret = find_in_path(net->network->type, paths, paths_len, &plugin_path, &save_errno);
    if (ret != 0) {
        if (asprintf(err, "find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno)) < 0) {
H
haozi007 已提交
492
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        }
        ERROR("find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno));
        goto free_out;
    }

    ret = inject_runtime_config(net, rc, &net_bytes, err);
    if (ret != 0) {
        ERROR("Inject runtime config: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = args("DEL", rc, paths, paths_len, &cargs, err);
    if (ret != 0) {
        ERROR("Get DEL cni arguments: %s", *err != NULL ? *err : "");
        goto free_out;
    }

    ret = exec_plugin_without_result(plugin_path, net_bytes, cargs, err);
free_out:
    free(plugin_path);
    free(net_bytes);
    free_cni_args(cargs);
    return ret;
}

static int do_copy_plugin_args(const struct runtime_conf *rc, struct cni_args **cargs)
{
    size_t i = 0;

    if (rc->args_len == 0) {
        return 0;
    }

    if (rc->args_len > (INT_MAX / sizeof(char *)) / 2) {
        ERROR("Large arguments");
        return -1;
    }
H
haozi007 已提交
530
    (*cargs)->plugin_args = clibcni_util_common_calloc_s((rc->args_len) * sizeof(char *) * 2);
O
overweight 已提交
531 532 533 534 535
    if ((*cargs)->plugin_args == NULL) {
        ERROR("Out of memory");
        return -1;
    }
    for (i = 0; i < rc->args_len; i++) {
H
haozi007 已提交
536 537
        (*cargs)->plugin_args[i][0] = clibcni_util_strdup_s(rc->args[i][0]);
        (*cargs)->plugin_args[i][1] = clibcni_util_strdup_s(rc->args[i][1]);
O
overweight 已提交
538 539 540 541 542 543 544 545 546
        (*cargs)->plugin_args_len = (i + 1);
    }

    return 0;
}

static int copy_args(const struct runtime_conf *rc, struct cni_args **cargs)
{
    if (rc->container_id != NULL) {
H
haozi007 已提交
547
        (*cargs)->container_id = clibcni_util_strdup_s(rc->container_id);
O
overweight 已提交
548 549
    }
    if (rc->netns != NULL) {
H
haozi007 已提交
550
        (*cargs)->netns = clibcni_util_strdup_s(rc->netns);
O
overweight 已提交
551 552
    }
    if (rc->ifname != NULL) {
H
haozi007 已提交
553
        (*cargs)->ifname = clibcni_util_strdup_s(rc->ifname);
O
overweight 已提交
554 555 556 557 558 559 560 561 562 563 564 565
    }

    return do_copy_plugin_args(rc, cargs);
}

static int do_copy_args_paths(const char * const *paths, size_t paths_len, struct cni_args **cargs)
{
    if (paths == NULL) {
        return 0;
    }

    if (paths_len == 0) {
H
haozi007 已提交
566
        (*cargs)->path = clibcni_util_strdup_s("");
O
overweight 已提交
567
    } else {
H
haozi007 已提交
568
        (*cargs)->path = clibcni_util_string_join(":", paths, paths_len);
O
overweight 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        if ((*cargs)->path == NULL) {
            ERROR("Out of memory");
            return -1;
        }
    }
    return 0;
}

static inline bool check_args_args(const struct runtime_conf *rc, struct cni_args * const *cargs, char * const *err)
{
    return (rc == NULL || cargs == NULL || err == NULL);
}

static int args(const char *action, const struct runtime_conf *rc, const char * const *paths, size_t paths_len,
                struct cni_args **cargs, char **err)
{
    int ret = -1;

    if (check_args_args(rc, cargs, err)) {
        ERROR("Empty arguments");
        return ret;
    }
H
haozi007 已提交
591
    *cargs = clibcni_util_common_calloc_s(sizeof(struct cni_args));
O
overweight 已提交
592
    if (*cargs == NULL) {
H
haozi007 已提交
593
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
594 595 596 597
        ERROR("Out of memory");
        goto free_out;
    }
    if (action != NULL) {
H
haozi007 已提交
598
        (*cargs)->command = clibcni_util_strdup_s(action);
O
overweight 已提交
599 600 601 602 603 604 605 606 607 608 609
    }
    if (do_copy_args_paths(paths, paths_len, cargs) != 0) {
        goto free_out;
    }
    ret = copy_args(rc, cargs);

free_out:
    if (ret != 0) {
        free_cni_args(*cargs);
        *cargs = NULL;
        if (*err == NULL) {
H
haozi007 已提交
610
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        }
    }
    return ret;
}

void free_cni_port_mapping(struct cni_port_mapping *val)
{
    if (val != NULL) {
        free(val->protocol);
        free(val->host_ip);
        free(val);
    }
}

void free_cni_network_conf(struct cni_network_conf *val)
{
    if (val != NULL) {
        free(val->name);
        free(val->type);
        free(val->bytes);
        free(val);
    }
}

void free_cni_network_list_conf(struct cni_network_list_conf *val)
{
    if (val != NULL) {
        free(val->bytes);
        free(val->name);
        free(val->first_plugin_name);
        free(val->first_plugin_type);
        free(val);
    }
}

void free_runtime_conf(struct runtime_conf *rc)
{
    size_t i = 0;

    if (rc == NULL) {
        return;
    }

    free(rc->container_id);
    rc->container_id = NULL;
    free(rc->netns);
    rc->netns = NULL;
    free(rc->ifname);
    rc->ifname = NULL;

    for (i = 0; i < rc->args_len; i++) {
        free(rc->args[i][0]);
        free(rc->args[i][1]);
    }
    free(rc->args);
    rc->args = NULL;

    for (i = 0; i < rc->p_mapping_len; i++) {
        free_cni_port_mapping(rc->p_mapping[i]);
    }
    free(rc->p_mapping);
    rc->p_mapping = NULL;
    free(rc);
}

int cni_add_network_list(const char *net_list_conf_str, const struct runtime_conf *rc, char **paths,
                         struct result **pret, char **err)
{
    struct network_config_list *list = NULL;
    int ret = 0;
    size_t len = 0;

    if (err == NULL) {
        ERROR("Empty arguments");
        return -1;
    }
    if (net_list_conf_str == NULL) {
H
haozi007 已提交
688
        *err = clibcni_util_strdup_s("Empty net list conf argument");
O
overweight 已提交
689 690 691 692 693 694 695 696 697 698
        ERROR("Empty net list conf argument");
        return -1;
    }

    ret = conflist_from_bytes(net_list_conf_str, &list, err);
    if (ret != 0) {
        ERROR("Parse conf list failed: %s", *err != NULL ? *err : "");
        return ret;
    }

H
haozi007 已提交
699
    len = clibcni_util_array_len((const char * const *)paths);
O
overweight 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    ret = add_network_list(list, rc, (const char * const *)paths, len, pret, err);

    DEBUG("Add network list return with: %d", ret);
    free_network_config_list(list);
    return ret;
}

int cni_add_network(const char *net_conf_str, const struct runtime_conf *rc, char **paths, struct result **add_result,
                    char **err)
{
    struct network_config *net = NULL;
    int ret = 0;
    size_t len = 0;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    if (net_conf_str == NULL) {
H
haozi007 已提交
719
        *err = clibcni_util_strdup_s("Empty net conf argument");
O
overweight 已提交
720 721 722 723 724 725 726 727 728 729
        ERROR("Empty net conf argument");
        return -1;
    }

    ret = conf_from_bytes(net_conf_str, &net, err);
    if (ret != 0) {
        ERROR("Parse conf failed: %s", *err != NULL ? *err : "");
        return ret;
    }

H
haozi007 已提交
730
    len = clibcni_util_array_len((const char * const *)paths);
O
overweight 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    ret = add_network(net, rc, (const char * const *)paths, len, add_result, err);
    free_network_config(net);
    return ret;
}

int cni_del_network_list(const char *net_list_conf_str, const struct runtime_conf *rc, char **paths, char **err)
{
    struct network_config_list *list = NULL;
    int ret = 0;
    size_t len = 0;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    if (net_list_conf_str == NULL) {
H
haozi007 已提交
747
        *err = clibcni_util_strdup_s("Empty net list conf argument");
O
overweight 已提交
748 749 750 751 752 753 754 755 756 757
        ERROR("Empty net list conf argument");
        return -1;
    }

    ret = conflist_from_bytes(net_list_conf_str, &list, err);
    if (ret != 0) {
        ERROR("Parse conf list failed: %s", *err != NULL ? *err : "");
        return ret;
    }

H
haozi007 已提交
758
    len = clibcni_util_array_len((const char * const *)paths);
O
overweight 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    ret = del_network_list(list, rc, (const char * const *)paths, len, err);

    DEBUG("Delete network list return with: %d", ret);
    free_network_config_list(list);
    return ret;
}

int cni_del_network(const char *net_conf_str, const struct runtime_conf *rc, char **paths, char **err)
{
    struct network_config *net = NULL;
    int ret = 0;
    size_t len = 0;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    if (net_conf_str == NULL) {
H
haozi007 已提交
777
        *err = clibcni_util_strdup_s("Empty net conf argument");
O
overweight 已提交
778 779 780 781 782 783 784 785 786 787
        ERROR("Empty net conf argument");
        return -1;
    }

    ret = conf_from_bytes(net_conf_str, &net, err);
    if (ret != 0) {
        ERROR("Parse conf failed: %s", *err != NULL ? *err : "");
        return ret;
    }

H
haozi007 已提交
788
    len = clibcni_util_array_len((const char * const *)paths);
O
overweight 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
    ret = del_network(net, rc, (const char * const *)paths, len, err);
    free_network_config(net);
    return ret;
}

int cni_get_version_info(const char *plugin_type, char **paths, struct plugin_info **pinfo, char **err)
{
    int ret = 0;
    char *plugin_path = NULL;
    size_t len;
    int save_errno = 0;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
H
haozi007 已提交
805
    len = clibcni_util_array_len((const char * const *)paths);
O
overweight 已提交
806 807 808
    ret = find_in_path(plugin_type, (const char * const *)paths, len, &plugin_path, &save_errno);
    if (ret != 0) {
        if (asprintf(err, "find plugin: \"%s\" failed: %s", plugin_type, get_invoke_err_msg(save_errno)) < 0) {
H
haozi007 已提交
809
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        }
        ERROR("find plugin: \"%s\" failed: %s", plugin_type, get_invoke_err_msg(save_errno));
        return ret;
    }

    ret = raw_get_version_info(plugin_path, pinfo, err);
    free(plugin_path);
    return ret;
}

int cni_conf_files(const char *dir, const char **extensions, size_t ext_len, char ***result, char **err)
{
    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    return conf_files(dir, extensions, ext_len, result, err);
}

int cni_conf_from_file(const char *filename, struct cni_network_conf **config, char **err)
{
    int ret = 0;
    struct network_config *netconf = NULL;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    ret = conf_from_file(filename, &netconf, err);
    if (ret != 0) {
        ERROR("Parse conf file: %s failed: %s", filename, *err != NULL ? *err : "");
        return ret;
    }

H
haozi007 已提交
844
    *config = clibcni_util_common_calloc_s(sizeof(struct cni_network_conf));
O
overweight 已提交
845
    if (*config == NULL) {
H
haozi007 已提交
846
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
847 848 849 850 851 852
        ret = -1;
        ERROR("Out of memory");
        goto free_out;
    }

    if (netconf != NULL && netconf->network != NULL) {
H
haozi007 已提交
853 854
        (*config)->type = netconf->network->type ? clibcni_util_strdup_s(netconf->network->type) : NULL;
        (*config)->name = netconf->network->name ? clibcni_util_strdup_s(netconf->network->name) : NULL;
O
overweight 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    }
    if (netconf != NULL) {
        (*config)->bytes = netconf->bytes;
        netconf->bytes = NULL;
    }

    ret = 0;

free_out:
    free_network_config(netconf);
    return ret;
}

static void json_obj_to_cni_list_conf(struct network_config_list *src, struct cni_network_list_conf *list)
{
    if (src == NULL) {
        return;
    }

    list->bytes = src->bytes;
    src->bytes = NULL;
    if (src->list != NULL) {
H
haozi007 已提交
877
        list->name = src->list->name ? clibcni_util_strdup_s(src->list->name) : NULL;
O
overweight 已提交
878 879 880
        list->plugin_len = src->list->plugins_len;
        if (src->list->plugins_len > 0 && src->list->plugins != NULL && src->list->plugins[0] != NULL) {
            list->first_plugin_name = src->list->plugins[0]->name != NULL ?
H
haozi007 已提交
881
                                      clibcni_util_strdup_s(src->list->plugins[0]->name) : NULL;
O
overweight 已提交
882
            list->first_plugin_type = src->list->plugins[0]->type != NULL ?
H
haozi007 已提交
883
                                      clibcni_util_strdup_s(src->list->plugins[0]->type) : NULL;
O
overweight 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
        }
    }
}

int cni_conflist_from_bytes(const char *bytes, struct cni_network_list_conf **list, char **err)
{
    struct network_config_list *tmp_net_conf_list = NULL;
    int ret = 0;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    ret = conflist_from_bytes(bytes, &tmp_net_conf_list, err);
    if (ret != 0) {
        return ret;
    }
H
haozi007 已提交
901
    *list = clibcni_util_common_calloc_s(sizeof(struct cni_network_list_conf));
O
overweight 已提交
902
    if (*list == NULL) {
H
haozi007 已提交
903
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        ret = -1;
        ERROR("Out of memory");
        goto free_out;
    }

    json_obj_to_cni_list_conf(tmp_net_conf_list, *list);

    ret = 0;
free_out:
    free_network_config_list(tmp_net_conf_list);
    return ret;
}

int cni_conflist_from_file(const char *filename, struct cni_network_list_conf **list, char **err)
{
    struct network_config_list *tmp_net_conf_list = NULL;
    int ret = 0;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }
    ret = conflist_from_file(filename, &tmp_net_conf_list, err);
    if (ret != 0) {
        return ret;
    }
H
haozi007 已提交
930
    *list = clibcni_util_common_calloc_s(sizeof(struct cni_network_list_conf));
O
overweight 已提交
931
    if (*list == NULL) {
H
haozi007 已提交
932
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        ret = -1;
        ERROR("Out of memory");
        goto free_out;
    }

    json_obj_to_cni_list_conf(tmp_net_conf_list, *list);

    ret = 0;
free_out:
    free_network_config_list(tmp_net_conf_list);
    return ret;
}

static inline bool check_cni_conflist_from_conf_args(const struct cni_network_conf *cni_conf,
                                                     struct cni_network_list_conf * const *cni_conf_list)
{
    return (cni_conf == NULL || cni_conf_list == NULL);
}

int cni_conflist_from_conf(const struct cni_network_conf *cni_conf, struct cni_network_list_conf **cni_conf_list,
                           char **err)
{
    struct network_config *net = NULL;
    struct network_config_list *net_list = NULL;
    int ret = 0;
    bool invalid_arg = false;

    if (err == NULL) {
        ERROR("Empty err");
        return -1;
    }

    invalid_arg = check_cni_conflist_from_conf_args(cni_conf, cni_conf_list);
    if (invalid_arg) {
H
haozi007 已提交
967
        *err = clibcni_util_strdup_s("Empty cni conf or conflist argument");
O
overweight 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981
        ERROR("Empty cni conf or conflist argument");
        return -1;
    }

    ret = conf_from_bytes(cni_conf->bytes, &net, err);
    if (ret != 0) {
        goto free_out;
    }

    ret = conflist_from_conf(net, &net_list, err);
    if (ret != 0) {
        goto free_out;
    }

H
haozi007 已提交
982
    *cni_conf_list = clibcni_util_common_calloc_s(sizeof(struct cni_network_list_conf));
O
overweight 已提交
983
    if (*cni_conf_list == NULL) {
H
haozi007 已提交
984
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        ERROR("Out of memory");
        ret = -1;
        goto free_out;
    }

    json_obj_to_cni_list_conf(net_list, *cni_conf_list);
    ret = 0;

free_out:
    if (net != NULL) {
        free_network_config(net);
    }
    free_network_config_list(net_list);
    return ret;
}

int cni_log_init(const char *driver, const char *file, const char *priority)
{
    struct clibcni_log_config conf = { 0 };

    conf.name = "clibcni";
    conf.driver = driver;
    conf.file = file;
    conf.priority = priority;
    return clibcni_log_enable(&conf);
}

void cni_set_log_prefix(const char *prefix)
{
    clibcni_set_log_prefix(prefix);
}

void cni_free_log_prefix()
{
    clibcni_free_log_prefix();
}