]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/graph.h
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / graph.h
index 8d8aa3823ba5f90474b2732449eda127ce22c1bd..87262a07b88757318ed1a1ce01d3a4e71c472a7f 100644 (file)
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with GNU Zebra; see the file COPYING.  If not, write to the Free
- * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #ifndef _ZEBRA_COMMAND_GRAPH_H
 #define _ZEBRA_COMMAND_GRAPH_H
 
+#include <stdbool.h>
 #include "vector.h"
+#include "buffer.h"
 
-struct graph
-{
-  vector nodes;
+struct graph {
+       vector nodes;
 };
 
-struct graph_node
-{
-  vector from;                  // nodes which have edges to this node
-  vector to;                    // nodes which this node has edges to
+struct graph_node {
+       vector from; // nodes which have edges to this node
+       vector to;   // nodes which this node has edges to
 
-  void *data;                   // node data
-  void (*del) (void *data);     // deletion callback
+       void *data;              // node data
+       void (*del)(void *data); // deletion callback
 };
 
-struct graph *
-graph_new (void);
+struct graph *graph_new(void);
 
 /**
  * Creates a new node.
@@ -52,8 +50,8 @@ graph_new (void);
  * @param[in] del data deletion callback
  * @return the new node
  */
-struct graph_node *
-graph_new_node (struct graph *graph, void *data, void (*del) (void*));
+struct graph_node *graph_new_node(struct graph *graph, void *data,
+                                 void (*del)(void *));
 
 /**
  * Deletes a node.
@@ -67,8 +65,7 @@ graph_new_node (struct graph *graph, void *data, void (*del) (void*));
  * @param[in] graph the graph this node belongs to
  * @param[out] node pointer to node to delete
  */
-void
-graph_delete_node (struct graph *graph, struct graph_node *node);
+void graph_delete_node(struct graph *graph, struct graph_node *node);
 
 /**
  * Makes a directed edge between two nodes.
@@ -77,8 +74,8 @@ graph_delete_node (struct graph *graph, struct graph_node *node);
  * @param[in] to
  * @return to
  */
-struct graph_node *
-graph_add_edge (struct graph_node *from, struct graph_node *to);
+struct graph_node *graph_add_edge(struct graph_node *from,
+                                 struct graph_node *to);
 
 /**
  * Removes a directed edge between two nodes.
@@ -86,8 +83,7 @@ graph_add_edge (struct graph_node *from, struct graph_node *to);
  * @param[in] from
  * @param[in] to
  */
-void
-graph_remove_edge (struct graph_node *from, struct graph_node *to);
+void graph_remove_edge(struct graph_node *from, struct graph_node *to);
 
 /**
  * Deletes a graph.
@@ -95,7 +91,77 @@ graph_remove_edge (struct graph_node *from, struct graph_node *to);
  *
  * @param graph the graph to delete
  */
-void
-graph_delete_graph (struct graph *graph);
+void graph_delete_graph(struct graph *graph);
 
+/*
+ * Finds a node in the graph.
+ *
+ * @param[in] graph the graph to search in
+ * @param[in] data the data to key off
+ * @return the first graph node whose data pointer matches `data`
+ */
+struct graph_node *graph_find_node(struct graph *graph, void *data);
+
+
+/*
+ * Determines whether two nodes have a directed edge between them.
+ *
+ * @param from
+ * @param to
+ * @return whether there is a directed edge from `from` to `to`.
+ */
+bool graph_has_edge(struct graph_node *from, struct graph_node *to);
+
+/*
+ * Depth-first search.
+ *
+ * Performs a depth-first traversal of the given graph, visiting each node
+ * exactly once and calling the user-provided callback for each visit.
+ *
+ * @param graph the graph to operate on
+ * @param start the node to take as the root
+ * @param dfs_cb callback called for each node visited in the traversal
+ * @param arg argument to provide to dfs_cb
+ */
+void graph_dfs(struct graph *graph, struct graph_node *start,
+              void (*dfs_cb)(struct graph_node *, void *), void *arg);
+
+#ifndef BUILDING_CLIPPY
+/*
+ * Clippy relies on a small subset of sources in lib/, but it cannot link
+ * libfrr since clippy itself is required to build libfrr. Instead it directly
+ * includes the sources it needs. One of these is the command graph
+ * implementation, which wraps this graph implementation. Since we need to use
+ * the buffer.[ch] sources here, which indirectly rely on most of libfrr, we
+ * have to ignore them when compiling clippy to avoid build dependency issues.
+ *
+ * TODO: Fix clippy build.
+ */
+
+/*
+ * Default node printer for use with graph_dump_dot.
+ *
+ * @param gn the node to print
+ * @param buf the buffer to print into
+ */
+void graph_dump_dot_default_print_cb(struct graph_node *gn, struct buffer *buf);
+
+/*
+ * Prints a graph in the DOT language.
+ *
+ * The generated output is produced from a depth-first traversal of the graph.
+ *
+ * @param graph the graph to print
+ * @param start the node to take as the root
+ * @param pcb callback called for each node in the traversal that should
+ *        print the node in the DOT language. Passing NULL for this argument
+ *        will use the default printer. See graph_dump_dot_default_print_cb for
+ *        an example.
+ * @return representation of graph in DOT language, allocated with MTYPE_TMP.
+ *         Caller is responsible for freeing this string.
+ */
+char *graph_dump_dot(struct graph *graph, struct graph_node *start,
+                    void (*pcb)(struct graph_node *, struct buffer *buf));
+
+#endif /* BUILDING_CLIPPY */
 #endif /* _ZEBRA_COMMAND_GRAPH_H */