LCOV - code coverage report
Current view: top level - lib/graph - graph.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 231 379 60.9 %
Date: 2026-11-01 19:11:22 Functions: 29 36 80.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 160 296 54.1 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2020 Marvell International Ltd.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <fnmatch.h>
       6                 :            : #include <stdbool.h>
       7                 :            : #include <stdlib.h>
       8                 :            : 
       9                 :            : #include <eal_export.h>
      10                 :            : #include <rte_common.h>
      11                 :            : #include <rte_debug.h>
      12                 :            : #include <rte_errno.h>
      13                 :            : #include <rte_malloc.h>
      14                 :            : #include <rte_memzone.h>
      15                 :            : #include <rte_spinlock.h>
      16                 :            : #include <rte_string_fns.h>
      17                 :            : 
      18                 :            : #include "graph_private.h"
      19                 :            : #include "graph_pcap_private.h"
      20                 :            : 
      21                 :            : static struct graph_head graph_list = STAILQ_HEAD_INITIALIZER(graph_list);
      22                 :            : static rte_spinlock_t graph_lock = RTE_SPINLOCK_INITIALIZER;
      23                 :            : 
      24                 :            : bool
      25                 :          8 : graph_is_node_active_in_graph(struct node *node)
      26                 :            : {
      27                 :            :         struct graph *graph;
      28                 :            : 
      29         [ -  + ]:          8 :         STAILQ_FOREACH(graph, &graph_list, next) {
      30                 :            :                 struct graph_node *graph_node;
      31                 :            : 
      32         [ #  # ]:          0 :                 STAILQ_FOREACH(graph_node, &graph->node_list, next)
      33         [ #  # ]:          0 :                         if (graph_node->node == node)
      34                 :            :                                 return 1;
      35                 :            :         }
      36                 :            : 
      37                 :            :         return 0;
      38                 :            : }
      39                 :            : 
      40                 :            : /* Private functions */
      41                 :            : static struct graph *
      42                 :            : graph_from_id(rte_graph_t id)
      43                 :            : {
      44                 :            :         struct graph *graph;
      45   [ -  -  +  -  :         18 :         STAILQ_FOREACH(graph, &graph_list, next) {
          +  -  +  -  +  
                -  +  - ]
      46   [ -  -  +  +  :         18 :                 if (graph->id == id)
          +  +  +  +  +  
                +  +  + ]
      47                 :            :                         return graph;
      48                 :            :         }
      49                 :          0 :         rte_errno = EINVAL;
      50                 :            :         return NULL;
      51                 :            : }
      52                 :            : 
      53                 :            : static rte_graph_t
      54                 :            : graph_next_free_id(void)
      55                 :            : {
      56                 :            :         struct graph *graph;
      57                 :            :         rte_graph_t id = 0;
      58                 :            : 
      59   [ +  +  +  + ]:         29 :         STAILQ_FOREACH(graph, &graph_list, next) {
      60   [ +  -  +  + ]:         17 :                 if (id < graph->id)
      61                 :            :                         break;
      62                 :         16 :                 id = graph->id + 1;
      63                 :            :         }
      64                 :            : 
      65                 :            :         return id;
      66                 :            : }
      67                 :            : 
      68                 :            : static void
      69                 :         13 : graph_insert_ordered(struct graph *graph)
      70                 :            : {
      71                 :            :         struct graph *after, *g;
      72                 :            : 
      73                 :            :         after = NULL;
      74         [ +  + ]:         29 :         STAILQ_FOREACH(g, &graph_list, next) {
      75         [ +  + ]:         17 :                 if (g->id < graph->id)
      76                 :            :                         after = g;
      77         [ -  + ]:          1 :                 else if (g->id > graph->id)
      78                 :            :                         break;
      79                 :            :         }
      80         [ +  + ]:         13 :         if (after == NULL) {
      81         [ +  - ]:          4 :                 STAILQ_INSERT_HEAD(&graph_list, graph, next);
      82                 :            :         } else {
      83         [ +  + ]:          9 :                 STAILQ_INSERT_AFTER(&graph_list, after, graph, next);
      84                 :            :         }
      85                 :         13 : }
      86                 :            : 
      87                 :            : struct graph_head *
      88                 :          3 : graph_list_head_get(void)
      89                 :            : {
      90                 :          3 :         return &graph_list;
      91                 :            : }
      92                 :            : 
      93                 :            : rte_spinlock_t *
      94                 :      26058 : graph_spinlock_get(void)
      95                 :            : {
      96                 :      26058 :         return &graph_lock;
      97                 :            : }
      98                 :            : 
      99                 :            : void
     100                 :      13029 : graph_spinlock_lock(void)
     101                 :            : {
     102                 :      13029 :         rte_spinlock_lock(graph_spinlock_get());
     103                 :      13029 : }
     104                 :            : 
     105                 :            : void
     106                 :      13029 : graph_spinlock_unlock(void)
     107                 :            : {
     108                 :      13029 :         rte_spinlock_unlock(graph_spinlock_get());
     109                 :      13029 : }
     110                 :            : 
     111                 :            : static int
     112                 :        208 : graph_node_add(struct graph *graph, struct node *node)
     113                 :            : {
     114                 :            :         struct graph_node *graph_node;
     115                 :            :         size_t sz;
     116                 :            : 
     117                 :            :         /* Skip the duplicate nodes */
     118         [ +  + ]:       1066 :         STAILQ_FOREACH(graph_node, &graph->node_list, next)
     119         [ +  + ]:        977 :                 if (strncmp(node->name, graph_node->node->name,
     120                 :            :                             RTE_NODE_NAMESIZE) == 0)
     121                 :            :                         return 0;
     122                 :            : 
     123                 :            :         /* Allocate new graph node object */
     124                 :         89 :         sz = sizeof(*graph_node) + node->nb_edges * sizeof(struct node *);
     125                 :         89 :         graph_node = calloc(1, sz);
     126                 :            : 
     127         [ -  + ]:         89 :         if (graph_node == NULL)
     128                 :          0 :                 SET_ERR_JMP(ENOMEM, free, "Failed to calloc %s object",
     129                 :            :                             node->name);
     130                 :            : 
     131                 :            :         /* Initialize the graph node */
     132                 :         89 :         graph_node->node = node;
     133                 :            : 
     134                 :            :         /* Add to graph node list */
     135                 :         89 :         STAILQ_INSERT_TAIL(&graph->node_list, graph_node, next);
     136                 :         89 :         return 0;
     137                 :            : 
     138                 :            : free:
     139                 :            :         free(graph_node);
     140                 :          0 :         return -rte_errno;
     141                 :            : }
     142                 :            : 
     143                 :            : static struct graph_node *
     144                 :        166 : node_to_graph_node(struct graph *graph, struct node *node)
     145                 :            : {
     146                 :            :         struct graph_node *graph_node;
     147                 :            : 
     148         [ +  - ]:        860 :         STAILQ_FOREACH(graph_node, &graph->node_list, next)
     149         [ +  + ]:        860 :                 if (graph_node->node == node)
     150                 :        166 :                         return graph_node;
     151                 :            : 
     152                 :          0 :         SET_ERR_JMP(ENODEV, fail, "Found isolated node %s", node->name);
     153                 :            : fail:
     154                 :          0 :         return NULL;
     155                 :            : }
     156                 :            : 
     157                 :            : static int
     158                 :          9 : graph_node_edges_add(struct graph *graph)
     159                 :            : {
     160                 :            :         struct graph_node *graph_node;
     161                 :            :         struct node *adjacency;
     162                 :            :         const char *next;
     163                 :            :         rte_edge_t i;
     164                 :            : 
     165         [ +  + ]:         73 :         STAILQ_FOREACH(graph_node, &graph->node_list, next) {
     166         [ +  + ]:        195 :                 for (i = 0; i < graph_node->node->nb_edges; i++) {
     167                 :        131 :                         next = graph_node->node->next_nodes[i];
     168                 :        131 :                         adjacency = node_from_name(next);
     169         [ -  + ]:        131 :                         if (adjacency == NULL)
     170                 :          0 :                                 SET_ERR_JMP(EINVAL, fail,
     171                 :            :                                             "Node %s not registered", next);
     172         [ -  + ]:        131 :                         if (graph_node_add(graph, adjacency))
     173                 :          0 :                                 goto fail;
     174                 :            :                 }
     175                 :            :         }
     176                 :            :         return 0;
     177                 :          0 : fail:
     178                 :          0 :         return -rte_errno;
     179                 :            : }
     180                 :            : 
     181                 :            : static int
     182                 :         14 : graph_adjacency_list_update(struct graph *graph)
     183                 :            : {
     184                 :            :         struct graph_node *graph_node, *tmp;
     185                 :            :         struct node *adjacency;
     186                 :            :         const char *next;
     187                 :            :         rte_edge_t i;
     188                 :            : 
     189         [ +  + ]:        103 :         STAILQ_FOREACH(graph_node, &graph->node_list, next) {
     190         [ +  + ]:        255 :                 for (i = 0; i < graph_node->node->nb_edges; i++) {
     191                 :        166 :                         next = graph_node->node->next_nodes[i];
     192                 :        166 :                         adjacency = node_from_name(next);
     193         [ -  + ]:        166 :                         if (adjacency == NULL)
     194                 :          0 :                                 SET_ERR_JMP(EINVAL, fail,
     195                 :            :                                             "Node %s not registered", next);
     196                 :        166 :                         tmp = node_to_graph_node(graph, adjacency);
     197         [ -  + ]:        166 :                         if (tmp == NULL)
     198                 :          0 :                                 goto fail;
     199                 :        166 :                         graph_node->adjacency_list[i] = tmp;
     200                 :            :                 }
     201                 :            :         }
     202                 :            : 
     203                 :            :         return 0;
     204                 :          0 : fail:
     205                 :          0 :         return -rte_errno;
     206                 :            : }
     207                 :            : 
     208                 :            : static int
     209                 :         52 : expand_pattern_to_node(struct graph *graph, const char *pattern)
     210                 :            : {
     211                 :         52 :         struct node_head *node_head = node_list_head_get();
     212                 :            :         bool found = false;
     213                 :            :         struct node *node;
     214                 :            : 
     215                 :            :         /* Check for pattern match */
     216         [ +  + ]:       1915 :         STAILQ_FOREACH(node, node_head, next) {
     217         [ +  + ]:       1863 :                 if (fnmatch(pattern, node->name, 0) == 0) {
     218         [ -  + ]:         52 :                         if (graph_node_add(graph, node))
     219                 :          0 :                                 goto fail;
     220                 :            :                         found = true;
     221                 :            :                 }
     222                 :            :         }
     223         [ -  + ]:         52 :         if (found == false)
     224                 :          0 :                 SET_ERR_JMP(EFAULT, fail, "Pattern %s node not found", pattern);
     225                 :            : 
     226                 :            :         return 0;
     227                 :          0 : fail:
     228                 :          0 :         return -rte_errno;
     229                 :            : }
     230                 :            : 
     231                 :            : static void
     232                 :            : graph_cleanup(struct graph *graph)
     233                 :            : {
     234                 :            :         struct graph_node *graph_node;
     235                 :            : 
     236   [ -  -  +  +  :        103 :         while (!STAILQ_EMPTY(&graph->node_list)) {
                   +  + ]
     237                 :            :                 graph_node = STAILQ_FIRST(&graph->node_list);
     238   [ -  -  +  +  :         89 :                 STAILQ_REMOVE_HEAD(&graph->node_list, next);
                   +  + ]
     239                 :         89 :                 free(graph_node);
     240                 :            :         }
     241                 :            : }
     242                 :            : 
     243                 :            : static int
     244                 :         13 : graph_node_init(struct graph *graph)
     245                 :            : {
     246                 :            :         struct graph_node *graph_node;
     247                 :            :         const char *name;
     248                 :            :         int rc;
     249                 :            : 
     250         [ +  + ]:         96 :         STAILQ_FOREACH(graph_node, &graph->node_list, next) {
     251         [ +  - ]:         83 :                 if (graph_node->node->init) {
     252                 :         83 :                         name = graph_node->node->name;
     253                 :        166 :                         rc = graph_node->node->init(
     254                 :         83 :                                 graph->graph,
     255                 :         83 :                                 graph_node_name_to_ptr(graph->graph, name));
     256         [ -  + ]:         83 :                         if (rc)
     257                 :          0 :                                 SET_ERR_JMP(rc, err, "Node %s init() failed",
     258                 :            :                                             name);
     259                 :            :                 }
     260                 :            :         }
     261                 :            : 
     262                 :            :         return 0;
     263                 :            : err:
     264                 :          0 :         return -rte_errno;
     265                 :            : }
     266                 :            : 
     267                 :            : static void
     268                 :         13 : graph_node_fini(struct graph *graph)
     269                 :            : {
     270                 :            :         struct graph_node *graph_node;
     271                 :            : 
     272         [ +  + ]:         96 :         STAILQ_FOREACH(graph_node, &graph->node_list, next)
     273         [ -  + ]:         83 :                 if (graph_node->node->fini)
     274                 :          0 :                         graph_node->node->fini(
     275                 :          0 :                                 graph->graph,
     276                 :          0 :                                 graph_node_name_to_ptr(graph->graph,
     277                 :          0 :                                                        graph_node->node->name));
     278                 :         13 : }
     279                 :            : 
     280                 :            : void
     281                 :         20 : graph_node_replace_all(struct node *old, struct node *new)
     282                 :            : {
     283                 :            :         struct graph_node *graph_node;
     284                 :            :         struct graph *graph;
     285                 :            : 
     286         [ -  + ]:         20 :         STAILQ_FOREACH(graph, &graph_list, next) {
     287         [ #  # ]:          0 :                 STAILQ_FOREACH(graph_node, &graph->node_list, next) {
     288         [ #  # ]:          0 :                         if (graph_node->node == old)
     289                 :          0 :                                 graph_node->node = new;
     290                 :            :                 }
     291                 :            :         }
     292                 :         20 : }
     293                 :            : 
     294                 :            : static struct rte_graph *
     295                 :          0 : graph_mem_fixup_node_ctx(struct rte_graph *graph)
     296                 :            : {
     297                 :            :         struct rte_node *node;
     298                 :            :         struct node *node_db;
     299                 :            :         rte_graph_off_t off;
     300                 :            :         rte_node_t count;
     301                 :            :         const char *name;
     302                 :            : 
     303         [ #  # ]:          0 :         rte_graph_foreach_node(count, off, graph, node) {
     304         [ #  # ]:          0 :                 if (node->parent_id == RTE_NODE_ID_INVALID) /* Static node */
     305                 :          0 :                         name = node->name;
     306                 :            :                 else /* Cloned node */
     307                 :          0 :                         name = node->parent;
     308                 :            : 
     309                 :          0 :                 node_db = node_from_name(name);
     310         [ #  # ]:          0 :                 if (node_db == NULL)
     311                 :          0 :                         SET_ERR_JMP(ENOLINK, fail, "Node %s not found", name);
     312                 :            : 
     313         [ #  # ]:          0 :                 if (graph->pcap_enable) {
     314                 :          0 :                         node->process = graph_pcap_dispatch;
     315                 :          0 :                         node->original_process = node_db->process;
     316                 :            :                 } else
     317                 :          0 :                         node->process = node_db->process;
     318                 :            :         }
     319                 :            : 
     320                 :            :         return graph;
     321                 :            : fail:
     322                 :          0 :         return NULL;
     323                 :            : }
     324                 :            : 
     325                 :            : static struct rte_graph *
     326                 :          5 : graph_mem_fixup_secondary(struct rte_graph *graph)
     327                 :            : {
     328   [ +  -  +  - ]:          5 :         if (graph == NULL || rte_eal_process_type() == RTE_PROC_PRIMARY)
     329                 :          5 :                 return graph;
     330                 :            : 
     331   [ #  #  #  # ]:          0 :         if (graph_pcap_file_open(graph->pcap_filename) || graph_pcap_mp_init())
     332                 :          0 :                 graph_pcap_exit(graph);
     333                 :            : 
     334                 :          0 :         return graph_mem_fixup_node_ctx(graph);
     335                 :            : }
     336                 :            : 
     337                 :            : RTE_EXPORT_SYMBOL(rte_graph_model_mcore_dispatch_core_bind)
     338                 :            : int
     339                 :          1 : rte_graph_model_mcore_dispatch_core_bind(rte_graph_t id, int lcore)
     340                 :            : {
     341                 :            :         struct graph *graph;
     342                 :            : 
     343                 :            :         if (graph_from_id(id) == NULL) {
     344                 :          0 :                 rte_errno = ENOENT;
     345                 :          0 :                 goto fail;
     346                 :            :         }
     347                 :            : 
     348         [ -  + ]:          1 :         if (rte_lcore_has_role(lcore, RTE_LCORE_ROLE_OFF))
     349                 :          0 :                 SET_ERR_JMP(ENOLINK, fail, "lcore %d is invalid", lcore);
     350                 :            : 
     351         [ +  - ]:          2 :         STAILQ_FOREACH(graph, &graph_list, next)
     352         [ +  + ]:          2 :                 if (graph->id == id)
     353                 :            :                         break;
     354                 :            : 
     355         [ -  + ]:          1 :         if (graph->graph->model != RTE_GRAPH_MODEL_MCORE_DISPATCH) {
     356                 :          0 :                 rte_errno = EPERM;
     357                 :          0 :                 goto fail;
     358                 :            :         }
     359                 :            : 
     360                 :          1 :         graph->lcore_id = lcore;
     361                 :          1 :         graph->graph->dispatch.lcore_id = graph->lcore_id;
     362                 :          1 :         graph->socket = rte_lcore_to_socket_id(lcore);
     363                 :            : 
     364                 :            :         /* Rebuild source bitmap with only source nodes bound to this lcore */
     365                 :          1 :         graph_src_bitmap_rebuild(graph);
     366                 :            : 
     367                 :          1 :         return 0;
     368                 :            : 
     369                 :          0 : fail:
     370                 :          0 :         return -rte_errno;
     371                 :            : }
     372                 :            : 
     373                 :            : RTE_EXPORT_SYMBOL(rte_graph_model_mcore_dispatch_core_unbind)
     374                 :            : void
     375                 :          1 : rte_graph_model_mcore_dispatch_core_unbind(rte_graph_t id)
     376                 :            : {
     377                 :            :         struct graph *graph;
     378                 :            : 
     379                 :            :         if (graph_from_id(id) == NULL)
     380                 :          0 :                 goto fail;
     381         [ +  - ]:          2 :         STAILQ_FOREACH(graph, &graph_list, next)
     382         [ +  + ]:          2 :                 if (graph->id == id)
     383                 :            :                         break;
     384                 :            : 
     385                 :          1 :         graph->lcore_id = RTE_MAX_LCORE;
     386                 :          1 :         graph->graph->dispatch.lcore_id = RTE_MAX_LCORE;
     387                 :            : 
     388                 :          1 : fail:
     389                 :          1 :         return;
     390                 :            : }
     391                 :            : 
     392                 :            : RTE_EXPORT_SYMBOL(rte_graph_lookup)
     393                 :            : struct rte_graph *
     394                 :          5 : rte_graph_lookup(const char *name)
     395                 :            : {
     396                 :            :         const struct rte_memzone *mz;
     397                 :            :         struct rte_graph *rc = NULL;
     398                 :            : 
     399                 :          5 :         mz = rte_memzone_lookup(name);
     400         [ +  - ]:          5 :         if (mz)
     401                 :          5 :                 rc = mz->addr;
     402                 :            : 
     403                 :          5 :         return graph_mem_fixup_secondary(rc);
     404                 :            : }
     405                 :            : 
     406                 :            : RTE_EXPORT_SYMBOL(rte_graph_create)
     407                 :            : rte_graph_t
     408                 :          9 : rte_graph_create(const char *name, struct rte_graph_param *prm)
     409                 :            : {
     410                 :            :         rte_node_t src_node_count;
     411                 :            :         struct graph *graph;
     412                 :            :         const char *pattern;
     413                 :            :         uint16_t i;
     414                 :            : 
     415                 :          9 :         graph_spinlock_lock();
     416                 :            : 
     417                 :            :         /* Check arguments sanity */
     418         [ -  + ]:          9 :         if (prm == NULL)
     419                 :          0 :                 SET_ERR_JMP(EINVAL, fail, "Param should not be NULL");
     420                 :            : 
     421         [ -  + ]:          9 :         if (name == NULL)
     422                 :          0 :                 SET_ERR_JMP(EINVAL, fail, "Graph name should not be NULL");
     423                 :            : 
     424                 :            :         /* Check for existence of duplicate graph */
     425         [ +  + ]:         18 :         STAILQ_FOREACH(graph, &graph_list, next)
     426         [ -  + ]:          9 :                 if (strncmp(name, graph->name, RTE_GRAPH_NAMESIZE) == 0)
     427                 :          0 :                         SET_ERR_JMP(EEXIST, fail, "Found duplicate graph %s",
     428                 :            :                                     name);
     429                 :            : 
     430                 :            :         /* Create graph object */
     431                 :          9 :         graph = calloc(1, sizeof(*graph));
     432         [ -  + ]:          9 :         if (graph == NULL)
     433                 :          0 :                 SET_ERR_JMP(ENOMEM, fail, "Failed to calloc graph object");
     434                 :            : 
     435                 :            :         /* Initialize the graph object */
     436                 :          9 :         STAILQ_INIT(&graph->node_list);
     437         [ -  + ]:          9 :         if (rte_strscpy(graph->name, name, RTE_GRAPH_NAMESIZE) < 0)
     438                 :          0 :                 SET_ERR_JMP(E2BIG, free, "Too big name=%s", name);
     439                 :            : 
     440                 :            :         /* Expand node pattern and add the nodes to the graph */
     441         [ +  + ]:         61 :         for (i = 0; i < prm->nb_node_patterns; i++) {
     442                 :         52 :                 pattern = prm->node_patterns[i];
     443         [ -  + ]:         52 :                 if (expand_pattern_to_node(graph, pattern))
     444                 :          0 :                         goto graph_cleanup;
     445                 :            :         }
     446                 :            : 
     447                 :            :         /* Go over all the nodes edges and add them to the graph */
     448         [ -  + ]:          9 :         if (graph_node_edges_add(graph))
     449                 :          0 :                 goto graph_cleanup;
     450                 :            : 
     451                 :            :         /* Update adjacency list of all nodes in the graph */
     452         [ -  + ]:          9 :         if (graph_adjacency_list_update(graph))
     453                 :          0 :                 goto graph_cleanup;
     454                 :            : 
     455                 :            :         /* Make sure at least a source node present in the graph */
     456                 :          9 :         src_node_count = graph_src_nodes_count(graph);
     457         [ -  + ]:          9 :         if (src_node_count == 0)
     458                 :          0 :                 goto graph_cleanup;
     459                 :            : 
     460                 :            :         /* Make sure no node is pointing to source node */
     461         [ -  + ]:          9 :         if (graph_node_has_edge_to_src_node(graph))
     462                 :          0 :                 goto graph_cleanup;
     463                 :            : 
     464                 :            :         /* Don't allow node has loop to self */
     465         [ -  + ]:          9 :         if (graph_node_has_loop_edge(graph))
     466                 :          0 :                 goto graph_cleanup;
     467                 :            : 
     468                 :            :         /* Do BFS from src nodes on the graph to find isolated nodes */
     469         [ +  + ]:          9 :         if (graph_has_isolated_node(graph))
     470                 :          1 :                 goto graph_cleanup;
     471                 :            : 
     472                 :            :         /* Compute topological depth for bitmap scheduling order */
     473         [ -  + ]:          8 :         if (graph_topo_order_compute(graph))
     474                 :          0 :                 goto graph_cleanup;
     475                 :            : 
     476                 :            :         /* Initialize pcap config. */
     477                 :          8 :         graph_pcap_enable(prm->pcap_enable);
     478                 :            : 
     479                 :            :         /* Initialize graph object */
     480                 :          8 :         graph->socket = prm->socket_id;
     481                 :          8 :         graph->src_node_count = src_node_count;
     482                 :          8 :         graph->node_count = graph_nodes_count(graph);
     483                 :          8 :         graph->id = graph_next_free_id();
     484                 :          8 :         graph->parent_id = RTE_GRAPH_ID_INVALID;
     485                 :          8 :         graph->lcore_id = RTE_MAX_LCORE;
     486                 :          8 :         graph->num_pkt_to_capture = prm->num_pkt_to_capture;
     487         [ -  + ]:          8 :         if (prm->pcap_filename)
     488                 :          0 :                 rte_strscpy(graph->pcap_filename, prm->pcap_filename, RTE_GRAPH_PCAP_FILE_SZ);
     489                 :            : 
     490                 :            :         /* Allocate the Graph fast path memory and populate the data */
     491         [ -  + ]:          8 :         if (graph_fp_mem_create(graph))
     492                 :          0 :                 goto graph_cleanup;
     493                 :            : 
     494                 :            :         /* Call init() of the all the nodes in the graph */
     495         [ -  + ]:          8 :         if (graph_node_init(graph))
     496                 :          0 :                 goto graph_mem_destroy;
     497                 :            : 
     498                 :            :         /* All good, Lets add the graph to the list */
     499                 :          8 :         graph_insert_ordered(graph);
     500                 :            : 
     501                 :          8 :         graph_spinlock_unlock();
     502                 :          8 :         return graph->id;
     503                 :            : 
     504                 :            : graph_mem_destroy:
     505                 :          0 :         graph_fp_mem_destroy(graph);
     506                 :            : graph_cleanup:
     507                 :            :         graph_cleanup(graph);
     508                 :          1 : free:
     509                 :          1 :         free(graph);
     510                 :          1 : fail:
     511                 :          1 :         graph_spinlock_unlock();
     512                 :          1 :         return RTE_GRAPH_ID_INVALID;
     513                 :            : }
     514                 :            : 
     515                 :            : RTE_EXPORT_SYMBOL(rte_graph_destroy)
     516                 :            : int
     517                 :         13 : rte_graph_destroy(rte_graph_t id)
     518                 :            : {
     519                 :            :         struct graph *graph, *tmp;
     520                 :            :         int rc = -ENOENT;
     521                 :            : 
     522                 :         13 :         graph_spinlock_lock();
     523                 :            : 
     524                 :         13 :         graph = STAILQ_FIRST(&graph_list);
     525         [ +  - ]:         26 :         while (graph != NULL) {
     526                 :         26 :                 tmp = STAILQ_NEXT(graph, next);
     527         [ +  + ]:         26 :                 if (graph->id == id) {
     528                 :            :                         /* Destroy the schedule work queue if has */
     529         [ +  + ]:         13 :                         if (rte_graph_worker_model_get(graph->graph) ==
     530                 :            :                             RTE_GRAPH_MODEL_MCORE_DISPATCH)
     531                 :          3 :                                 graph_sched_wq_destroy(graph);
     532                 :            : 
     533                 :            :                         /* Call fini() of the all the nodes in the graph */
     534                 :         13 :                         graph_node_fini(graph);
     535                 :            :                         /* Destroy graph fast path memory */
     536                 :         13 :                         rc = graph_fp_mem_destroy(graph);
     537         [ +  - ]:         13 :                         if (rc)
     538                 :          0 :                                 SET_ERR_JMP(rc, done, "Graph %s destroy failed",
     539                 :            :                                             graph->name);
     540                 :            : 
     541                 :            :                         graph_cleanup(graph);
     542   [ +  +  +  -  :         26 :                         STAILQ_REMOVE(&graph_list, graph, graph, next);
             +  +  +  + ]
     543                 :         13 :                         free(graph);
     544                 :         13 :                         goto done;
     545                 :            :                 }
     546                 :            :                 graph = tmp;
     547                 :            :         }
     548                 :          0 : done:
     549                 :         13 :         graph_spinlock_unlock();
     550                 :         13 :         return rc;
     551                 :            : }
     552                 :            : 
     553                 :            : static rte_graph_t
     554                 :          5 : graph_clone(struct graph *parent_graph, const char *name, struct rte_graph_param *prm)
     555                 :            : {
     556                 :            :         struct graph_node *graph_node;
     557                 :            :         struct graph *graph;
     558                 :            : 
     559                 :          5 :         graph_spinlock_lock();
     560                 :            : 
     561                 :            :         /* Don't allow cloning a node from a cloned graph */
     562         [ -  + ]:          5 :         if (parent_graph->parent_id != RTE_GRAPH_ID_INVALID)
     563                 :          0 :                 SET_ERR_JMP(EEXIST, fail, "A cloned graph is not allowed to be cloned");
     564                 :            : 
     565                 :            :         /* Create graph object */
     566                 :          5 :         graph = calloc(1, sizeof(*graph));
     567         [ -  + ]:          5 :         if (graph == NULL)
     568                 :          0 :                 SET_ERR_JMP(ENOMEM, fail, "Failed to calloc cloned graph object");
     569                 :            : 
     570                 :            :         /* Naming ceremony of the new graph. name is node->name + "-" + name */
     571         [ -  + ]:          5 :         if (clone_name(graph->name, parent_graph->name, name))
     572                 :          0 :                 goto free;
     573                 :            : 
     574                 :            :         /* Check for existence of duplicate graph */
     575         [ -  + ]:          5 :         if (rte_graph_from_name(graph->name) != RTE_GRAPH_ID_INVALID)
     576                 :          0 :                 SET_ERR_JMP(EEXIST, free, "Found duplicate graph %s",
     577                 :            :                             graph->name);
     578                 :            : 
     579                 :            :         /* Clone nodes from parent graph firstly */
     580                 :          5 :         STAILQ_INIT(&graph->node_list);
     581         [ +  + ]:         30 :         STAILQ_FOREACH(graph_node, &parent_graph->node_list, next) {
     582         [ -  + ]:         25 :                 if (graph_node_add(graph, graph_node->node))
     583                 :          0 :                         goto graph_cleanup;
     584                 :            :         }
     585                 :            : 
     586                 :            :         /* Just update adjacency list of all nodes in the graph */
     587         [ -  + ]:          5 :         if (graph_adjacency_list_update(graph))
     588                 :          0 :                 goto graph_cleanup;
     589                 :            : 
     590                 :            :         /* Compute topological depth for bitmap scheduling order */
     591         [ -  + ]:          5 :         if (graph_topo_order_compute(graph))
     592                 :          0 :                 goto graph_cleanup;
     593                 :            : 
     594                 :            :         /* Initialize the graph object */
     595                 :          5 :         graph->src_node_count = parent_graph->src_node_count;
     596                 :          5 :         graph->node_count = parent_graph->node_count;
     597                 :          5 :         graph->parent_id = parent_graph->id;
     598                 :          5 :         graph->lcore_id = parent_graph->lcore_id;
     599                 :          5 :         graph->socket = parent_graph->socket;
     600                 :          5 :         graph->id = graph_next_free_id();
     601                 :            : 
     602                 :            :         /* Allocate the Graph fast path memory and populate the data */
     603         [ -  + ]:          5 :         if (graph_fp_mem_create(graph))
     604                 :          0 :                 goto graph_cleanup;
     605                 :            : 
     606                 :            :         /* Clone the graph model */
     607                 :          5 :         graph->graph->model = parent_graph->graph->model;
     608                 :            : 
     609                 :            :         /* Create the graph schedule work queue */
     610         [ +  + ]:          5 :         if (rte_graph_worker_model_get(graph->graph) == RTE_GRAPH_MODEL_MCORE_DISPATCH) {
     611         [ -  + ]:          1 :                 if (graph_sched_wq_create(graph, parent_graph, prm))
     612                 :          0 :                         goto graph_mem_destroy;
     613                 :            : 
     614                 :          1 :                 graph->graph->dispatch.notify_cb = prm->dispatch.notify_cb;
     615                 :          1 :                 graph->graph->dispatch.cb_priv = prm->dispatch.cb_priv;
     616                 :            :         }
     617                 :            : 
     618                 :            :         /* Call init() of the all the nodes in the graph */
     619         [ -  + ]:          5 :         if (graph_node_init(graph))
     620                 :          0 :                 goto graph_mem_destroy;
     621                 :            : 
     622                 :            :         /* All good, Lets add the graph to the list */
     623                 :          5 :         graph_insert_ordered(graph);
     624                 :            : 
     625                 :          5 :         graph_spinlock_unlock();
     626                 :          5 :         return graph->id;
     627                 :            : 
     628                 :          0 : graph_mem_destroy:
     629                 :          0 :         graph_fp_mem_destroy(graph);
     630                 :            : graph_cleanup:
     631                 :            :         graph_cleanup(graph);
     632                 :          0 : free:
     633                 :          0 :         free(graph);
     634                 :          0 : fail:
     635                 :          0 :         graph_spinlock_unlock();
     636                 :          0 :         return RTE_GRAPH_ID_INVALID;
     637                 :            : }
     638                 :            : 
     639                 :            : RTE_EXPORT_SYMBOL(rte_graph_clone)
     640                 :            : rte_graph_t
     641                 :          5 : rte_graph_clone(rte_graph_t id, const char *name, struct rte_graph_param *prm)
     642                 :            : {
     643                 :            :         struct graph *graph;
     644                 :            : 
     645                 :            :         if (graph_from_id(id) == NULL)
     646                 :          0 :                 goto fail;
     647         [ +  - ]:          6 :         STAILQ_FOREACH(graph, &graph_list, next)
     648         [ +  + ]:          6 :                 if (graph->id == id)
     649                 :          5 :                         return graph_clone(graph, name, prm);
     650                 :            : 
     651                 :          0 : fail:
     652                 :            :         return RTE_GRAPH_ID_INVALID;
     653                 :            : }
     654                 :            : 
     655                 :            : RTE_EXPORT_SYMBOL(rte_graph_from_name)
     656                 :            : rte_graph_t
     657                 :          7 : rte_graph_from_name(const char *name)
     658                 :            : {
     659                 :            :         struct graph *graph;
     660                 :            : 
     661         [ +  + ]:         15 :         STAILQ_FOREACH(graph, &graph_list, next)
     662         [ +  + ]:         10 :                 if (strncmp(graph->name, name, RTE_GRAPH_NAMESIZE) == 0)
     663                 :          2 :                         return graph->id;
     664                 :            : 
     665                 :            :         return RTE_GRAPH_ID_INVALID;
     666                 :            : }
     667                 :            : 
     668                 :            : RTE_EXPORT_SYMBOL(rte_graph_id_to_name)
     669                 :            : char *
     670                 :          1 : rte_graph_id_to_name(rte_graph_t id)
     671                 :            : {
     672                 :            :         struct graph *graph;
     673                 :            : 
     674                 :            :         if (graph_from_id(id) == NULL)
     675                 :          0 :                 goto fail;
     676         [ +  - ]:          2 :         STAILQ_FOREACH(graph, &graph_list, next)
     677         [ +  + ]:          2 :                 if (graph->id == id)
     678                 :          1 :                         return graph->name;
     679                 :            : 
     680                 :          0 : fail:
     681                 :            :         return NULL;
     682                 :            : }
     683                 :            : 
     684                 :            : RTE_EXPORT_SYMBOL(rte_graph_node_get)
     685                 :            : struct rte_node *
     686                 :          5 : rte_graph_node_get(rte_graph_t gid, uint32_t nid)
     687                 :            : {
     688                 :            :         struct rte_node *node;
     689                 :            :         struct graph *graph;
     690                 :            :         rte_graph_off_t off;
     691                 :            :         rte_node_t count;
     692                 :            : 
     693                 :            :         if (graph_from_id(gid) == NULL)
     694                 :          0 :                 goto fail;
     695         [ +  - ]:          6 :         STAILQ_FOREACH(graph, &graph_list, next)
     696         [ +  + ]:          6 :                 if (graph->id == gid) {
     697         [ +  - ]:         19 :                         rte_graph_foreach_node(count, off, graph->graph,
     698                 :            :                                                 node) {
     699         [ +  + ]:         19 :                                 if (node->id == nid)
     700                 :          5 :                                         return node;
     701                 :            :                         }
     702                 :            :                         break;
     703                 :            :                 }
     704                 :          0 : fail:
     705                 :            :         return NULL;
     706                 :            : }
     707                 :            : 
     708                 :            : RTE_EXPORT_SYMBOL(rte_graph_node_get_by_name)
     709                 :            : struct rte_node *
     710                 :          4 : rte_graph_node_get_by_name(const char *graph_name, const char *node_name)
     711                 :            : {
     712                 :            :         struct rte_node *node;
     713                 :            :         struct graph *graph;
     714                 :            :         rte_graph_off_t off;
     715                 :            :         rte_node_t count;
     716                 :            : 
     717         [ +  - ]:          4 :         STAILQ_FOREACH(graph, &graph_list, next)
     718         [ +  - ]:          4 :                 if (!strncmp(graph->name, graph_name, RTE_GRAPH_NAMESIZE)) {
     719         [ +  - ]:         14 :                         rte_graph_foreach_node(count, off, graph->graph,
     720                 :            :                                                 node) {
     721         [ +  + ]:         14 :                                 if (!strncmp(node->name, node_name,
     722                 :            :                                              RTE_NODE_NAMESIZE))
     723                 :          4 :                                         return node;
     724                 :            :                         }
     725                 :            :                         break;
     726                 :            :                 }
     727                 :            : 
     728                 :            :         return NULL;
     729                 :            : }
     730                 :            : 
     731                 :            : RTE_EXPORT_SYMBOL(__rte_node_stream_alloc)
     732                 :            : void __rte_noinline
     733                 :         96 : __rte_node_stream_alloc(struct rte_graph *graph, struct rte_node *node)
     734                 :            : {
     735                 :         96 :         uint16_t size = node->size;
     736                 :            : 
     737         [ -  + ]:         96 :         RTE_VERIFY(size != UINT16_MAX);
     738                 :            :         /* Allocate double amount of size to avoid immediate realloc */
     739                 :         96 :         size = RTE_MIN(UINT16_MAX, RTE_MAX(RTE_GRAPH_BURST_SIZE, size * 2));
     740                 :         96 :         node->objs = rte_realloc_socket(node->objs, size * sizeof(void *),
     741                 :            :                                         RTE_CACHE_LINE_SIZE, graph->socket);
     742         [ -  + ]:         96 :         RTE_VERIFY(node->objs);
     743                 :         96 :         node->size = size;
     744                 :         96 :         node->realloc_count++;
     745                 :         96 : }
     746                 :            : 
     747                 :            : RTE_EXPORT_SYMBOL(__rte_node_stream_alloc_size)
     748                 :            : void __rte_noinline
     749                 :          3 : __rte_node_stream_alloc_size(struct rte_graph *graph, struct rte_node *node,
     750                 :            :                              uint16_t req_size)
     751                 :            : {
     752                 :          3 :         uint16_t size = node->size;
     753                 :            : 
     754         [ -  + ]:          3 :         RTE_VERIFY(size != UINT16_MAX);
     755                 :            :         /* Allocate double amount of size to avoid immediate realloc */
     756                 :          3 :         size = RTE_MIN(UINT16_MAX, RTE_MAX(RTE_GRAPH_BURST_SIZE, req_size * 2));
     757                 :          3 :         node->objs = rte_realloc_socket(node->objs, size * sizeof(void *),
     758                 :            :                                         RTE_CACHE_LINE_SIZE, graph->socket);
     759         [ -  + ]:          3 :         RTE_VERIFY(node->objs);
     760                 :          3 :         node->size = size;
     761                 :          3 :         node->realloc_count++;
     762                 :          3 : }
     763                 :            : 
     764                 :            : static int
     765                 :          0 : graph_to_dot(FILE *f, struct graph *graph)
     766                 :            : {
     767                 :            :         struct graph_node *graph_node;
     768                 :            :         char *node_name;
     769                 :            :         rte_edge_t i;
     770                 :            :         int rc;
     771                 :            : 
     772                 :          0 :         rc = fprintf(f, "digraph \"%s\" {\n\trankdir=LR;\n", graph->name);
     773         [ #  # ]:          0 :         if (rc < 0)
     774                 :          0 :                 goto end;
     775                 :            : 
     776                 :            :         rc = fprintf(f, "\tnode [margin=0.02 fontsize=11 fontname=sans];\n");
     777         [ #  # ]:          0 :         if (rc < 0)
     778                 :          0 :                 goto end;
     779                 :            : 
     780         [ #  # ]:          0 :         STAILQ_FOREACH(graph_node, &graph->node_list, next) {
     781                 :            :                 const char *attrs = "";
     782                 :          0 :                 node_name = graph_node->node->name;
     783                 :            : 
     784                 :            :                 rc = fprintf(f, "\t\"%s\"", node_name);
     785         [ #  # ]:          0 :                 if (rc < 0)
     786                 :          0 :                         goto end;
     787         [ #  # ]:          0 :                 if (graph_node->node->flags & RTE_NODE_SOURCE_F) {
     788                 :            :                         attrs = " [color=blue style=bold]";
     789                 :            :                         rc = fprintf(f, "%s", attrs);
     790         [ #  # ]:          0 :                         if (rc < 0)
     791                 :          0 :                                 goto end;
     792         [ #  # ]:          0 :                 } else if (graph_node->node->nb_edges == 0) {
     793                 :            :                         rc = fprintf(f, " [fontcolor=darkorange shape=plain]");
     794         [ #  # ]:          0 :                         if (rc < 0)
     795                 :          0 :                                 goto end;
     796                 :            :                 }
     797                 :            :                 rc = fprintf(f, ";\n");
     798         [ #  # ]:          0 :                 if (rc < 0)
     799                 :          0 :                         goto end;
     800         [ #  # ]:          0 :                 for (i = 0; i < graph_node->node->nb_edges; i++) {
     801                 :            :                         const char *node_attrs = attrs;
     802         [ #  # ]:          0 :                         if (graph_node->adjacency_list[i]->node->nb_edges == 0)
     803                 :            :                                 node_attrs = " [color=darkorange]";
     804                 :            :                         rc = fprintf(f, "\t\"%s\" -> \"%s\"%s;\n", node_name,
     805                 :          0 :                                      graph_node->adjacency_list[i]->node->name,
     806                 :            :                                      node_attrs);
     807         [ #  # ]:          0 :                         if (rc < 0)
     808                 :          0 :                                 goto end;
     809                 :            :                 }
     810                 :            :         }
     811                 :            :         rc = fprintf(f, "}\n");
     812         [ #  # ]:          0 :         if (rc < 0)
     813                 :          0 :                 goto end;
     814                 :            : 
     815                 :            :         return 0;
     816                 :          0 : end:
     817                 :          0 :         rte_errno = EBADF;
     818                 :          0 :         return -rte_errno;
     819                 :            : }
     820                 :            : 
     821                 :            : RTE_EXPORT_SYMBOL(rte_graph_export)
     822                 :            : int
     823                 :          0 : rte_graph_export(const char *name, FILE *f)
     824                 :            : {
     825                 :            :         struct graph *graph;
     826                 :            :         int rc = ENOENT;
     827                 :            : 
     828         [ #  # ]:          0 :         STAILQ_FOREACH(graph, &graph_list, next) {
     829         [ #  # ]:          0 :                 if (strncmp(graph->name, name, RTE_GRAPH_NAMESIZE) == 0) {
     830                 :          0 :                         rc = graph_to_dot(f, graph);
     831                 :          0 :                         goto end;
     832                 :            :                 }
     833                 :            :         }
     834                 :          0 : end:
     835                 :          0 :         return -rc;
     836                 :            : }
     837                 :            : 
     838                 :            : static void
     839                 :          0 : graph_scan_dump(FILE *f, rte_graph_t id, bool all)
     840                 :            : {
     841                 :            :         struct graph *graph;
     842                 :            : 
     843         [ #  # ]:          0 :         RTE_VERIFY(f);
     844                 :            :         if (graph_from_id(id) == NULL)
     845                 :          0 :                 goto fail;
     846                 :            : 
     847         [ #  # ]:          0 :         STAILQ_FOREACH(graph, &graph_list, next) {
     848         [ #  # ]:          0 :                 if (all == true) {
     849                 :          0 :                         graph_dump(f, graph);
     850         [ #  # ]:          0 :                 } else if (graph->id == id) {
     851                 :          0 :                         graph_dump(f, graph);
     852                 :          0 :                         return;
     853                 :            :                 }
     854                 :            :         }
     855                 :          0 : fail:
     856                 :            :         return;
     857                 :            : }
     858                 :            : 
     859                 :            : RTE_EXPORT_SYMBOL(rte_graph_dump)
     860                 :            : void
     861                 :          0 : rte_graph_dump(FILE *f, rte_graph_t id)
     862                 :            : {
     863                 :          0 :         graph_scan_dump(f, id, false);
     864                 :          0 : }
     865                 :            : 
     866                 :            : RTE_EXPORT_SYMBOL(rte_graph_list_dump)
     867                 :            : void
     868                 :          0 : rte_graph_list_dump(FILE *f)
     869                 :            : {
     870                 :          0 :         graph_scan_dump(f, 0, true);
     871                 :          0 : }
     872                 :            : 
     873                 :            : RTE_EXPORT_SYMBOL(rte_graph_max_count)
     874                 :            : rte_graph_t
     875                 :          0 : rte_graph_max_count(void)
     876                 :            : {
     877                 :            :         struct graph *graph;
     878                 :            :         rte_graph_t count = 0;
     879                 :            : 
     880         [ #  # ]:          0 :         STAILQ_FOREACH(graph, &graph_list, next)
     881                 :          0 :                 count++;
     882                 :            : 
     883                 :          0 :         return count;
     884                 :            : }
     885                 :            : 
     886         [ -  + ]:        305 : RTE_LOG_REGISTER_DEFAULT(rte_graph_logtype, INFO);

Generated by: LCOV version 1.14