]> git.proxmox.com Git - mirror_frr.git/blame - lib/graph.h
Merge pull request #5410 from ton31337/feature/bgp_default-route_with_route-map_set
[mirror_frr.git] / lib / graph.h
CommitLineData
16d7c050
QY
1/*
2 * Graph data structure.
3 *
4 * --
5 * Copyright (C) 2016 Cumulus Networks, Inc.
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
896014f4
DL
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16d7c050
QY
22 */
23
24#ifndef _ZEBRA_COMMAND_GRAPH_H
25#define _ZEBRA_COMMAND_GRAPH_H
26
9428e089 27#include <stdbool.h>
16d7c050 28#include "vector.h"
58f8a9ec 29#include "buffer.h"
16d7c050 30
5e244469
RW
31#ifdef __cplusplus
32extern "C" {
33#endif
34
d62a17ae 35struct graph {
36 vector nodes;
1eb5e8dc 37};
16d7c050 38
d62a17ae 39struct graph_node {
40 vector from; // nodes which have edges to this node
41 vector to; // nodes which this node has edges to
16d7c050 42
d62a17ae 43 void *data; // node data
44 void (*del)(void *data); // deletion callback
16d7c050
QY
45};
46
d62a17ae 47struct graph *graph_new(void);
1eb5e8dc 48
16d7c050
QY
49/**
50 * Creates a new node.
51 *
52 * @struct graph the graph this node exists in
53 * @param[in] data this node's data
54 * @param[in] del data deletion callback
55 * @return the new node
56 */
d62a17ae 57struct graph_node *graph_new_node(struct graph *graph, void *data,
58 void (*del)(void *));
16d7c050
QY
59
60/**
61 * Deletes a node.
62 *
63 * Before deletion, this function removes all edges to and from this node from
64 * any neighbor nodes.
65 *
16d7c050
QY
66 * If *data and *del are non-null, the following call is made:
67 * (*node->del) (node->data);
68 *
ba06a3a0 69 * @param[in] graph the graph this node belongs to
16d7c050
QY
70 * @param[out] node pointer to node to delete
71 */
d62a17ae 72void graph_delete_node(struct graph *graph, struct graph_node *node);
16d7c050
QY
73
74/**
75 * Makes a directed edge between two nodes.
76 *
77 * @param[in] from
78 * @param[in] to
79 * @return to
80 */
d62a17ae 81struct graph_node *graph_add_edge(struct graph_node *from,
82 struct graph_node *to);
16d7c050
QY
83
84/**
ba06a3a0 85 * Removes a directed edge between two nodes.
16d7c050 86 *
ba06a3a0
QY
87 * @param[in] from
88 * @param[in] to
89 */
d62a17ae 90void graph_remove_edge(struct graph_node *from, struct graph_node *to);
ba06a3a0
QY
91
92/**
93 * Deletes a graph.
16d7c050
QY
94 * Calls graph_delete_node on each node before freeing the graph struct itself.
95 *
96 * @param graph the graph to delete
97 */
d62a17ae 98void graph_delete_graph(struct graph *graph);
16d7c050 99
9428e089
QY
100/*
101 * Finds a node in the graph.
102 *
103 * @param[in] graph the graph to search in
104 * @param[in] data the data to key off
105 * @return the first graph node whose data pointer matches `data`
106 */
107struct graph_node *graph_find_node(struct graph *graph, void *data);
108
109
110/*
111 * Determines whether two nodes have a directed edge between them.
112 *
113 * @param from
114 * @param to
115 * @return whether there is a directed edge from `from` to `to`.
116 */
117bool graph_has_edge(struct graph_node *from, struct graph_node *to);
118
58f8a9ec
QY
119/*
120 * Depth-first search.
121 *
122 * Performs a depth-first traversal of the given graph, visiting each node
123 * exactly once and calling the user-provided callback for each visit.
124 *
125 * @param graph the graph to operate on
126 * @param start the node to take as the root
127 * @param dfs_cb callback called for each node visited in the traversal
128 * @param arg argument to provide to dfs_cb
129 */
130void graph_dfs(struct graph *graph, struct graph_node *start,
131 void (*dfs_cb)(struct graph_node *, void *), void *arg);
132
133#ifndef BUILDING_CLIPPY
134/*
135 * Clippy relies on a small subset of sources in lib/, but it cannot link
136 * libfrr since clippy itself is required to build libfrr. Instead it directly
137 * includes the sources it needs. One of these is the command graph
138 * implementation, which wraps this graph implementation. Since we need to use
139 * the buffer.[ch] sources here, which indirectly rely on most of libfrr, we
140 * have to ignore them when compiling clippy to avoid build dependency issues.
141 *
142 * TODO: Fix clippy build.
143 */
144
145/*
146 * Default node printer for use with graph_dump_dot.
147 *
148 * @param gn the node to print
149 * @param buf the buffer to print into
150 */
151void graph_dump_dot_default_print_cb(struct graph_node *gn, struct buffer *buf);
152
153/*
154 * Prints a graph in the DOT language.
155 *
156 * The generated output is produced from a depth-first traversal of the graph.
157 *
158 * @param graph the graph to print
159 * @param start the node to take as the root
160 * @param pcb callback called for each node in the traversal that should
161 * print the node in the DOT language. Passing NULL for this argument
162 * will use the default printer. See graph_dump_dot_default_print_cb for
163 * an example.
164 * @return representation of graph in DOT language, allocated with MTYPE_TMP.
165 * Caller is responsible for freeing this string.
166 */
167char *graph_dump_dot(struct graph *graph, struct graph_node *start,
168 void (*pcb)(struct graph_node *, struct buffer *buf));
169
170#endif /* BUILDING_CLIPPY */
5e244469
RW
171
172#ifdef __cplusplus
173}
174#endif
175
16d7c050 176#endif /* _ZEBRA_COMMAND_GRAPH_H */