Browse Source

zebra: add module to communicate routes to FPM

Enhance zebra to send routes to the (optional) Forwarding Path Manager
component using the interface defined by fpm/fpm.h.

  * configure.ac

    - Add --enable-fpm flag.

      The FPM-related code in zebra is activated only if the build is
      configured with '--enable-fpm'.

    - Add HAVE_NETLINK automake conditional.

      This allows us to conditionally build netlink-dependent C code.

  * zebra/{rib.h,zebra_rib.c}

    - Add the 'fpm_q_entries' field to the rib_dest_t structure. This
      allows dests to be placed on the fpm queue.

    - Define a couple new rib_dest_t flags that hold FPM-related
      state.

    - Invoke the zfpm_trigger_update() function for a route_node
      whenever the information to be sent to the FPM changes.

    - rib_can_delete_dest(): Return FALSE if we have to update the FPM
      about the given dest. This ensures that the dest is not deleted
      even if there are no ribs hanging off of it.

  * zebra/zebra_fpm.c

    This file holds most of the code for interacting with the FPM.

    - If quagga was configured with '--enable-fpm', periodically try
      to connect to the FPM.

    - When the connection comes up, enqueue all relevent dests to the
      FPM queue.

    - When the FPM socket is readable, dequeue the next rib_dest_t
      from the FPM queue, encode it in to a message and send the
      message to the FPM.

    - When the connection to the FPM goes down, remove all dests from
      the FPM queue, and then start trying to connect to the FPM
      again.

    - Expose the following new operational commands:

      show zebra fpm stats
      clear zebra fpm stats

  * zebra/zebra_fpm_netlink.c

    - zfpm_netlink_encode_route(): Function to encode information
      about a rib_dest_t in netlink format.

  * zebra/zebra_fpm_private.h

    Private header file for the zebra FPM module.

  * zebra/zebra_fpm.h

    Header file exported by zebra FPM module to the rest of zebra.

  * zebra/debug.c

    Add the 'debug zebra fpm' command.

  * zebra/main.c

    Initialize the zebra-FPM code on startup.

  * zebra/misc_null.c

    Add stub for zfpm_trigger_update().

  * zebra/Makefile.am

    - Include new file zebra_fpm.c in build.

    - Include zebra_fpm_netlink.c in build if HAVE_NETLINK is defined.

  * vtysh/Makefile.am

    Include zebra_fpm.c in list of files that define cli commands.

Signed-off-by: Avneesh Sachdev <avneesh@opensourcerouting.org>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Avneesh Sachdev 7 years ago
parent
commit
5adc2528d3
13 changed files with 2344 additions and 2 deletions
  1. 7 0
      configure.ac
  2. 2 1
      vtysh/Makefile.am
  3. 6 1
      zebra/Makefile.am
  4. 37 0
      zebra/debug.c
  5. 5 0
      zebra/debug.h
  6. 7 0
      zebra/main.c
  7. 7 0
      zebra/misc_null.c
  8. 18 0
      zebra/rib.h
  9. 1581 0
      zebra/zebra_fpm.c
  10. 34 0
      zebra/zebra_fpm.h
  11. 552 0
      zebra/zebra_fpm_netlink.c
  12. 56 0
      zebra/zebra_fpm_private.h
  13. 32 0
      zebra/zebra_rib.c

+ 7 - 0
configure.ac

@@ -272,6 +272,8 @@ AC_ARG_ENABLE(time-check,
 [  --disable-time-check          disable slow thread warning messages])
 AC_ARG_ENABLE(pcreposix,
 [  --enable-pcreposix          enable using PCRE Posix libs for regex functions])
+AC_ARG_ENABLE(fpm,
+[  --enable-fpm            enable Forwarding Plane Manager support])
 
 if test x"${enable_gcc_ultra_verbose}" = x"yes" ; then
   CFLAGS="${CFLAGS} -W -Wcast-qual -Wstrict-prototypes"
@@ -292,6 +294,10 @@ if test x"${enable_time_check}" != x"no" ; then
   fi
 fi
 
+if test "${enable_fpm}" = "yes"; then
+   AC_DEFINE(HAVE_FPM,,Forwarding Plane Manager support)
+fi
+
 if test "${enable_broken_aliases}" = "yes"; then
   if test "${enable_netlink}" = "yes"
   then
@@ -828,6 +834,7 @@ fi
 AC_SUBST(RT_METHOD)
 AC_SUBST(KERNEL_METHOD)
 AC_SUBST(OTHER_METHOD)
+AM_CONDITIONAL([HAVE_NETLINK], [test "x$netlink" = "xyes"])
 
 dnl --------------------------
 dnl Determine IS-IS I/O method

+ 2 - 1
vtysh/Makefile.am

@@ -33,7 +33,8 @@ vtysh_cmd_FILES = $(top_srcdir)/bgpd/*.c $(top_srcdir)/isisd/*.c \
 		  $(top_srcdir)/zebra/irdp_interface.c \
 		  $(top_srcdir)/zebra/rtadv.c $(top_srcdir)/zebra/zebra_vty.c \
 		  $(top_srcdir)/zebra/zserv.c $(top_srcdir)/zebra/router-id.c \
-		  $(top_srcdir)/zebra/zebra_routemap.c
+		  $(top_srcdir)/zebra/zebra_routemap.c \
+	          $(top_srcdir)/zebra/zebra_fpm.c
 
 vtysh_cmd.c: $(vtysh_cmd_FILES)
 	./$(EXTRA_DIST) $(vtysh_cmd_FILES) > vtysh_cmd.c

+ 6 - 1
zebra/Makefile.am

@@ -19,6 +19,10 @@ ioctl_method = @IOCTL_METHOD@
 otherobj = $(ioctl_method) $(ipforward) $(if_method) $(if_proc) \
 	$(rt_method) $(rtread_method) $(kernel_method) $(other_method)
 
+if HAVE_NETLINK
+othersrc = zebra_fpm_netlink.c
+endif
+
 AM_CFLAGS = $(PICFLAGS)
 AM_LDFLAGS = $(PILDFLAGS)
 
@@ -29,7 +33,8 @@ noinst_PROGRAMS = testzebra
 zebra_SOURCES = \
 	zserv.c main.c interface.c connected.c zebra_rib.c zebra_routemap.c \
 	redistribute.c debug.c rtadv.c zebra_snmp.c zebra_vty.c \
-	irdp_main.c irdp_interface.c irdp_packet.c router-id.c
+	irdp_main.c irdp_interface.c irdp_packet.c router-id.c zebra_fpm.c \
+	$(othersrc)
 
 testzebra_SOURCES = test_main.c zebra_rib.c interface.c connected.c debug.c \
 	zebra_vty.c \

+ 37 - 0
zebra/debug.c

@@ -29,6 +29,7 @@ unsigned long zebra_debug_event;
 unsigned long zebra_debug_packet;
 unsigned long zebra_debug_kernel;
 unsigned long zebra_debug_rib;
+unsigned long zebra_debug_fpm;
 
 DEFUN (show_debugging_zebra,
        show_debugging_zebra_cmd,
@@ -71,6 +72,9 @@ DEFUN (show_debugging_zebra,
   if (IS_ZEBRA_DEBUG_RIB_Q)
     vty_out (vty, "  Zebra RIB queue debugging is on%s", VTY_NEWLINE);
 
+  if (IS_ZEBRA_DEBUG_FPM)
+    vty_out (vty, "  Zebra FPM debugging is on%s", VTY_NEWLINE);
+
   return CMD_SUCCESS;
 }
 
@@ -169,6 +173,17 @@ DEFUN (debug_zebra_rib_q,
   return CMD_SUCCESS;
 }
 
+DEFUN (debug_zebra_fpm,
+       debug_zebra_fpm_cmd,
+       "debug zebra fpm",
+       DEBUG_STR
+       "Zebra configuration\n"
+       "Debug zebra FPM events\n")
+{
+  SET_FLAG (zebra_debug_fpm, ZEBRA_DEBUG_FPM);
+  return CMD_SUCCESS;
+}
+
 DEFUN (no_debug_zebra_events,
        no_debug_zebra_events_cmd,
        "no debug zebra events",
@@ -247,6 +262,18 @@ DEFUN (no_debug_zebra_rib_q,
   return CMD_SUCCESS;
 }
 
+DEFUN (no_debug_zebra_fpm,
+       no_debug_zebra_fpm_cmd,
+       "no debug zebra fpm",
+       NO_STR
+       DEBUG_STR
+       "Zebra configuration\n"
+       "Debug zebra FPM events\n")
+{
+  zebra_debug_fpm = 0;
+  return CMD_SUCCESS;
+}
+
 /* Debug node. */
 struct cmd_node debug_node =
 {
@@ -302,6 +329,11 @@ config_write_debug (struct vty *vty)
       vty_out (vty, "debug zebra rib queue%s", VTY_NEWLINE);
       write++;
     }
+  if (IS_ZEBRA_DEBUG_FPM)
+    {
+      vty_out (vty, "debug zebra fpm%s", VTY_NEWLINE);
+      write++;
+    }
   return write;
 }
 
@@ -312,6 +344,7 @@ zebra_debug_init (void)
   zebra_debug_packet = 0;
   zebra_debug_kernel = 0;
   zebra_debug_rib = 0;
+  zebra_debug_fpm = 0;
 
   install_node (&debug_node, config_write_debug);
 
@@ -325,11 +358,13 @@ zebra_debug_init (void)
   install_element (ENABLE_NODE, &debug_zebra_kernel_cmd);
   install_element (ENABLE_NODE, &debug_zebra_rib_cmd);
   install_element (ENABLE_NODE, &debug_zebra_rib_q_cmd);
+  install_element (ENABLE_NODE, &debug_zebra_fpm_cmd);
   install_element (ENABLE_NODE, &no_debug_zebra_events_cmd);
   install_element (ENABLE_NODE, &no_debug_zebra_packet_cmd);
   install_element (ENABLE_NODE, &no_debug_zebra_kernel_cmd);
   install_element (ENABLE_NODE, &no_debug_zebra_rib_cmd);
   install_element (ENABLE_NODE, &no_debug_zebra_rib_q_cmd);
+  install_element (ENABLE_NODE, &no_debug_zebra_fpm_cmd);
 
   install_element (CONFIG_NODE, &debug_zebra_events_cmd);
   install_element (CONFIG_NODE, &debug_zebra_packet_cmd);
@@ -338,9 +373,11 @@ zebra_debug_init (void)
   install_element (CONFIG_NODE, &debug_zebra_kernel_cmd);
   install_element (CONFIG_NODE, &debug_zebra_rib_cmd);
   install_element (CONFIG_NODE, &debug_zebra_rib_q_cmd);
+  install_element (CONFIG_NODE, &debug_zebra_fpm_cmd);
   install_element (CONFIG_NODE, &no_debug_zebra_events_cmd);
   install_element (CONFIG_NODE, &no_debug_zebra_packet_cmd);
   install_element (CONFIG_NODE, &no_debug_zebra_kernel_cmd);
   install_element (CONFIG_NODE, &no_debug_zebra_rib_cmd);
   install_element (CONFIG_NODE, &no_debug_zebra_rib_q_cmd);
+  install_element (CONFIG_NODE, &no_debug_zebra_fpm_cmd);
 }

+ 5 - 0
zebra/debug.h

@@ -36,6 +36,8 @@
 #define ZEBRA_DEBUG_RIB     0x01
 #define ZEBRA_DEBUG_RIB_Q   0x02
 
+#define ZEBRA_DEBUG_FPM     0x01
+
 /* Debug related macro. */
 #define IS_ZEBRA_DEBUG_EVENT  (zebra_debug_event & ZEBRA_DEBUG_EVENT)
 
@@ -49,10 +51,13 @@
 #define IS_ZEBRA_DEBUG_RIB  (zebra_debug_rib & ZEBRA_DEBUG_RIB)
 #define IS_ZEBRA_DEBUG_RIB_Q  (zebra_debug_rib & ZEBRA_DEBUG_RIB_Q)
 
+#define IS_ZEBRA_DEBUG_FPM (zebra_debug_fpm & ZEBRA_DEBUG_FPM)
+
 extern unsigned long zebra_debug_event;
 extern unsigned long zebra_debug_packet;
 extern unsigned long zebra_debug_kernel;
 extern unsigned long zebra_debug_rib;
+extern unsigned long zebra_debug_fpm;
 
 extern void zebra_debug_init (void);
 

+ 7 - 0
zebra/main.c

@@ -39,6 +39,7 @@
 #include "zebra/router-id.h"
 #include "zebra/irdp.h"
 #include "zebra/rtadv.h"
+#include "zebra/zebra_fpm.h"
 
 /* Zebra instance */
 struct zebra_t zebrad =
@@ -349,6 +350,12 @@ main (int argc, char **argv)
   zebra_snmp_init ();
 #endif /* HAVE_SNMP */
 
+#ifdef HAVE_FPM
+  zfpm_init (zebrad.master, 1, 0);
+#else
+  zfpm_init (zebrad.master, 0, 0);
+#endif
+
   /* Process the configuration file. Among other configuration
   *  directives we can meet those installing static routes. Such
   *  requests will not be executed immediately, but queued in

+ 7 - 0
zebra/misc_null.c

@@ -4,8 +4,15 @@
 #include "zebra/rtadv.h"
 #include "zebra/irdp.h"
 #include "zebra/interface.h"
+#include "zebra/zebra_fpm.h"
 
 void ifstat_update_proc (void) { return; }
 #pragma weak rtadv_config_write = ifstat_update_proc
 #pragma weak irdp_config_write = ifstat_update_proc
 #pragma weak ifstat_update_sysctl = ifstat_update_proc
+
+void
+zfpm_trigger_update (struct route_node *rn, const char *reason)
+{
+  return;
+}

+ 18 - 0
zebra/rib.h

@@ -25,6 +25,7 @@
 
 #include "prefix.h"
 #include "table.h"
+#include "queue.h"
 
 #define DISTANCE_INFINITY  255
 
@@ -116,6 +117,11 @@ typedef struct rib_dest_t_
    */
   u_int32_t flags;
 
+  /*
+   * Linkage to put dest on the FPM processing queue.
+   */
+  TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
+
 } rib_dest_t;
 
 #define RIB_ROUTE_QUEUED(x)	(1 << (x))
@@ -126,6 +132,18 @@ typedef struct rib_dest_t_
 #define ZEBRA_MAX_QINDEX        (MQ_SIZE - 1)
 
 /*
+ * This flag indicates that a given prefix has been 'advertised' to
+ * the FPM to be installed in the forwarding plane.
+ */
+#define RIB_DEST_SENT_TO_FPM   (1 << (ZEBRA_MAX_QINDEX + 1))
+
+/*
+ * This flag is set when we need to send an update to the FPM about a
+ * dest.
+ */
+#define RIB_DEST_UPDATE_FPM    (1 << (ZEBRA_MAX_QINDEX + 2))
+
+/*
  * Macro to iterate over each route for a destination (prefix).
  */
 #define RIB_DEST_FOREACH_ROUTE(dest, rib)				\

File diff suppressed because it is too large
+ 1581 - 0
zebra/zebra_fpm.c


+ 34 - 0
zebra/zebra_fpm.h

@@ -0,0 +1,34 @@
+/*
+ * Header file exported by the zebra FPM module to zebra.
+ *
+ * Copyright (C) 2012 by Open Source Routing.
+ * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * 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.
+ */
+
+#ifndef _ZEBRA_FPM_H
+#define _ZEBRA_FPM_H
+
+/*
+ * Externs.
+ */
+extern int zfpm_init (struct thread_master *master, int enable, uint16_t port);
+extern void zfpm_trigger_update (struct route_node *rn, const char *reason);
+
+#endif /* _ZEBRA_FPM_H */

+ 552 - 0
zebra/zebra_fpm_netlink.c

@@ -0,0 +1,552 @@
+/*
+ * Code for encoding/decoding FPM messages that are in netlink format.
+ *
+ * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
+ * Copyright (C) 2012 by Open Source Routing.
+ * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * 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.
+ */
+
+#include <zebra.h>
+
+#include "log.h"
+#include "rib.h"
+
+#include "rt_netlink.h"
+
+#include "zebra_fpm_private.h"
+
+/*
+ * addr_to_a
+ *
+ * Returns string representation of an address of the given AF.
+ */
+static inline const char *
+addr_to_a (u_char af, void *addr)
+{
+  if (!addr)
+    return "<No address>";
+
+  switch (af)
+    {
+
+    case AF_INET:
+      return inet_ntoa (*((struct in_addr *) addr));
+
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      return inet6_ntoa (*((struct in6_addr *) addr));
+#endif
+
+    default:
+      return "<Addr in unknown AF>";
+    }
+}
+
+/*
+ * prefix_addr_to_a
+ *
+ * Convience wrapper that returns a human-readable string for the
+ * address in a prefix.
+ */
+static const char *
+prefix_addr_to_a (struct prefix *prefix)
+{
+  if (!prefix)
+    return "<No address>";
+
+  return addr_to_a (prefix->family, &prefix->u.prefix);
+}
+
+/*
+ * af_addr_size
+ *
+ * The size of an address in a given address family.
+ */
+static size_t
+af_addr_size (u_char af)
+{
+  switch (af)
+    {
+
+    case AF_INET:
+      return 4;
+
+#ifdef HAVE_IPV6
+    case AF_INET6:
+      return 16;
+#endif
+
+    default:
+      assert(0);
+      return 16;
+    }
+}
+
+/*
+ * netlink_nh_info_t
+ *
+ * Holds information about a single nexthop for netlink. These info
+ * structures are transient and may contain pointers into rib
+ * data structures for convenience.
+ */
+typedef struct netlink_nh_info_t_
+{
+  uint32_t if_index;
+  union g_addr *gateway;
+
+  /*
+   * Information from the struct nexthop from which this nh was
+   * derived. For debug purposes only.
+   */
+  int recursive;
+  enum nexthop_types_t type;
+} netlink_nh_info_t;
+
+/*
+ * netlink_route_info_t
+ *
+ * A structure for holding information for a netlink route message.
+ */
+typedef struct netlink_route_info_t_
+{
+  uint16_t nlmsg_type;
+  u_char rtm_type;
+  uint32_t rtm_table;
+  u_char rtm_protocol;
+  u_char af;
+  struct prefix *prefix;
+  uint32_t *metric;
+  int num_nhs;
+
+  /*
+   * Nexthop structures. We keep things simple for now by enforcing a
+   * maximum of 64 in case MULTIPATH_NUM is 0;
+   */
+  netlink_nh_info_t nhs[MAX (MULTIPATH_NUM, 64)];
+  union g_addr *pref_src;
+} netlink_route_info_t;
+
+/*
+ * netlink_route_info_add_nh
+ *
+ * Add information about the given nexthop to the given route info
+ * structure.
+ *
+ * Returns TRUE if a nexthop was added, FALSE otherwise.
+ */
+static int
+netlink_route_info_add_nh (netlink_route_info_t *ri, struct nexthop *nexthop)
+{
+  netlink_nh_info_t nhi;
+  union g_addr *src;
+
+  memset (&nhi, 0, sizeof (nhi));
+  src = NULL;
+
+  if (ri->num_nhs >= (int) ZEBRA_NUM_OF (ri->nhs))
+    return 0;
+
+  if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
+    {
+      nhi.recursive = 1;
+      nhi.type = nexthop->rtype;
+
+      if (nexthop->rtype == NEXTHOP_TYPE_IPV4
+	  || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
+	{
+	  nhi.gateway = &nexthop->rgate;
+	  if (nexthop->src.ipv4.s_addr)
+	    src = &nexthop->src;
+	}
+
+#ifdef HAVE_IPV6
+      if (nexthop->rtype == NEXTHOP_TYPE_IPV6
+	  || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
+	  || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
+	{
+	  nhi.gateway = &nexthop->rgate;
+	}
+#endif /* HAVE_IPV6 */
+
+      if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
+	  || nexthop->rtype == NEXTHOP_TYPE_IFNAME
+	  || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
+	  || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
+	  || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
+	{
+	  nhi.if_index = nexthop->rifindex;
+	  if ((nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
+	       || nexthop->rtype == NEXTHOP_TYPE_IFINDEX)
+	      && nexthop->src.ipv4.s_addr)
+	    src = &nexthop->src;
+	}
+
+      goto done;
+    }
+
+  nhi.recursive = 0;
+  nhi.type = nexthop->type;
+
+  if (nexthop->type == NEXTHOP_TYPE_IPV4
+      || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
+    {
+      nhi.gateway = &nexthop->gate;
+      if (nexthop->src.ipv4.s_addr)
+	src = &nexthop->src;
+    }
+
+#ifdef HAVE_IPV6
+  if (nexthop->type == NEXTHOP_TYPE_IPV6
+      || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
+      || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
+    {
+      nhi.gateway = &nexthop->gate;
+    }
+#endif /* HAVE_IPV6 */
+  if (nexthop->type == NEXTHOP_TYPE_IFINDEX
+      || nexthop->type == NEXTHOP_TYPE_IFNAME
+      || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
+    {
+      nhi.if_index = nexthop->ifindex;
+
+      if (nexthop->src.ipv4.s_addr)
+	src = &nexthop->src;
+    }
+  else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
+	   || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
+    {
+      nhi.if_index = nexthop->ifindex;
+    }
+
+  /*
+   * Fall through...
+   */
+
+ done:
+  if (!nhi.gateway && nhi.if_index == 0)
+    return 0;
+
+  /*
+   * We have a valid nhi. Copy the structure over to the route_info.
+   */
+  ri->nhs[ri->num_nhs] = nhi;
+  ri->num_nhs++;
+
+  if (src && !ri->pref_src)
+    ri->pref_src = src;
+
+  return 1;
+}
+
+/*
+ * netlink_proto_from_route_type
+ */
+static u_char
+netlink_proto_from_route_type (int type)
+{
+  switch (type)
+    {
+    case ZEBRA_ROUTE_KERNEL:
+    case ZEBRA_ROUTE_CONNECT:
+      return RTPROT_KERNEL;
+
+    default:
+      return RTPROT_ZEBRA;
+    }
+}
+
+/*
+ * netlink_route_info_fill
+ *
+ * Fill out the route information object from the given route.
+ *
+ * Returns TRUE on success and FALSE on failure.
+ */
+static int
+netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
+			 rib_dest_t *dest, struct rib *rib)
+{
+  struct nexthop *nexthop = NULL;
+  int discard;
+
+  memset (ri, 0, sizeof (*ri));
+
+  ri->prefix = rib_dest_prefix (dest);
+  ri->af = rib_dest_af (dest);
+
+  ri->nlmsg_type = cmd;
+  ri->rtm_table = rib_dest_vrf (dest)->id;
+  ri->rtm_protocol = RTPROT_UNSPEC;
+
+  /*
+   * An RTM_DELROUTE need not be accompanied by any nexthops,
+   * particularly in our communication with the FPM.
+   */
+  if (cmd == RTM_DELROUTE && !rib)
+    goto skip;
+
+  if (rib)
+    ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
+
+  if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
+    discard = 1;
+  else
+    discard = 0;
+
+  if (cmd == RTM_NEWROUTE)
+    {
+      if (discard)
+        {
+          if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
+            ri->rtm_type = RTN_BLACKHOLE;
+          else if (rib->flags & ZEBRA_FLAG_REJECT)
+            ri->rtm_type = RTN_UNREACHABLE;
+          else
+            assert (0);
+        }
+      else
+        ri->rtm_type = RTN_UNICAST;
+    }
+
+  ri->metric = &rib->metric;
+
+  if (discard)
+    {
+      goto skip;
+    }
+
+  /* Multipath case. */
+  if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
+    {
+      for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
+        {
+
+          if ((cmd == RTM_NEWROUTE
+               && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
+              || (cmd == RTM_DELROUTE
+                  && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
+            {
+	      netlink_route_info_add_nh (ri, nexthop);
+              break;
+            }
+        }
+    }
+  else
+    {
+      for (nexthop = rib->nexthop;
+           nexthop && (MULTIPATH_NUM == 0 || ri->num_nhs < MULTIPATH_NUM);
+           nexthop = nexthop->next)
+        {
+          if ((cmd == RTM_NEWROUTE
+               && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
+              || (cmd == RTM_DELROUTE
+                  && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
+            {
+	      netlink_route_info_add_nh (ri, nexthop);
+            }
+        }
+    }
+
+  /* If there is no useful nexthop then return. */
+  if (ri->num_nhs == 0)
+    {
+      zfpm_debug ("netlink_encode_route(): No useful nexthop.");
+      return 0;
+    }
+
+ skip:
+  return 1;
+}
+
+/*
+ * netlink_route_info_encode
+ *
+ * Returns the number of bytes written to the buffer. 0 or a negative
+ * value indicates an error.
+ */
+static int
+netlink_route_info_encode (netlink_route_info_t *ri, char *in_buf,
+			   size_t in_buf_len)
+{
+  int bytelen;
+  int nexthop_num = 0;
+  size_t buf_offset;
+  netlink_nh_info_t *nhi;
+
+  struct
+  {
+    struct nlmsghdr n;
+    struct rtmsg r;
+    char buf[1];
+  } *req;
+
+  req = (void *) in_buf;
+
+  buf_offset = ((char *) req->buf) - ((char *) req);
+
+  if (in_buf_len < buf_offset) {
+    assert(0);
+    return 0;
+  }
+
+  memset (req, 0, buf_offset);
+
+  bytelen = af_addr_size (ri->af);
+
+  req->n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
+  req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
+  req->n.nlmsg_type = ri->nlmsg_type;
+  req->r.rtm_family = ri->af;
+  req->r.rtm_table = ri->rtm_table;
+  req->r.rtm_dst_len = ri->prefix->prefixlen;
+  req->r.rtm_protocol = ri->rtm_protocol;
+  req->r.rtm_scope = RT_SCOPE_UNIVERSE;
+
+  addattr_l (&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix, bytelen);
+
+  req->r.rtm_type = ri->rtm_type;
+
+  /* Metric. */
+  if (ri->metric)
+    addattr32 (&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
+
+  if (ri->num_nhs == 0)
+    goto done;
+
+  if (ri->num_nhs == 1)
+    {
+      nhi = &ri->nhs[0];
+
+      if (nhi->gateway)
+	{
+	  addattr_l (&req->n, in_buf_len, RTA_GATEWAY, nhi->gateway,
+		     bytelen);
+	}
+
+      if (nhi->if_index)
+	{
+	  addattr32 (&req->n, in_buf_len, RTA_OIF, nhi->if_index);
+	}
+
+      goto done;
+
+    }
+
+  /*
+   * Multipath case.
+   */
+  char buf[NL_PKT_BUF_SIZE];
+  struct rtattr *rta = (void *) buf;
+  struct rtnexthop *rtnh;
+
+  rta->rta_type = RTA_MULTIPATH;
+  rta->rta_len = RTA_LENGTH (0);
+  rtnh = RTA_DATA (rta);
+
+  for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++)
+    {
+      nhi = &ri->nhs[nexthop_num];
+
+      rtnh->rtnh_len = sizeof (*rtnh);
+      rtnh->rtnh_flags = 0;
+      rtnh->rtnh_hops = 0;
+      rtnh->rtnh_ifindex = 0;
+      rta->rta_len += rtnh->rtnh_len;
+
+      if (nhi->gateway)
+	{
+	  rta_addattr_l (rta, sizeof (buf), RTA_GATEWAY, nhi->gateway, bytelen);
+	  rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
+	}
+
+      if (nhi->if_index)
+	{
+	  rtnh->rtnh_ifindex = nhi->if_index;
+	}
+
+      rtnh = RTNH_NEXT (rtnh);
+    }
+
+  assert (rta->rta_len > RTA_LENGTH (0));
+  addattr_l (&req->n, in_buf_len, RTA_MULTIPATH, RTA_DATA (rta),
+	     RTA_PAYLOAD (rta));
+
+done:
+
+  if (ri->pref_src)
+    {
+      addattr_l (&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src, bytelen);
+    }
+
+  assert (req->n.nlmsg_len < in_buf_len);
+  return req->n.nlmsg_len;
+}
+
+/*
+ * zfpm_log_route_info
+ *
+ * Helper function to log the information in a route_info structure.
+ */
+static void
+zfpm_log_route_info (netlink_route_info_t *ri, const char *label)
+{
+  netlink_nh_info_t *nhi;
+  int i;
+
+  zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label,
+	      nl_msg_type_to_str (ri->nlmsg_type),
+	      prefix_addr_to_a (ri->prefix), ri->prefix->prefixlen,
+	      nl_rtproto_to_str (ri->rtm_protocol),
+	      ri->metric ? *ri->metric : 0);
+
+  for (i = 0; i < ri->num_nhs; i++)
+    {
+      nhi = &ri->nhs[i];
+      zfpm_debug("  Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
+		 nhi->if_index, addr_to_a (ri->af, nhi->gateway),
+		 nhi->recursive ? "yes" : "no",
+		 nexthop_type_to_str (nhi->type));
+    }
+}
+
+/*
+ * zfpm_netlink_encode_route
+ *
+ * Create a netlink message corresponding to the given route in the
+ * given buffer space.
+ *
+ * Returns the number of bytes written to the buffer. 0 or a negative
+ * value indicates an error.
+ */
+int
+zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
+			   char *in_buf, size_t in_buf_len)
+{
+  netlink_route_info_t ri_space, *ri;
+
+  ri = &ri_space;
+
+  if (!netlink_route_info_fill (ri, cmd, dest, rib))
+    return 0;
+
+  zfpm_log_route_info (ri, __FUNCTION__);
+
+  return netlink_route_info_encode (ri, in_buf, in_buf_len);
+}

+ 56 - 0
zebra/zebra_fpm_private.h

@@ -0,0 +1,56 @@
+/*
+ * Private header file for the zebra FPM module.
+ *
+ * Copyright (C) 2012 by Open Source Routing.
+ * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
+ *
+ * This file is part of GNU Zebra.
+ *
+ * GNU Zebra is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * GNU Zebra is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * 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.
+ */
+
+#ifndef _ZEBRA_FPM_PRIVATE_H
+#define _ZEBRA_FPM_PRIVATE_H
+
+#include "zebra/debug.h"
+
+#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
+
+#define zfpm_debug(...)						\
+  do {								\
+    if (IS_ZEBRA_DEBUG_FPM) zlog_debug("FPM: " __VA_ARGS__);	\
+  } while(0)
+
+#elif defined __GNUC__
+
+#define zfpm_debug(_args...)				\
+  do {							\
+    if (IS_ZEBRA_DEBUG_FPM) zlog_debug("FPM: " _args);	\
+  } while(0)
+
+#else
+static inline void zfpm_debug(const char *format, ...) { return; }
+#endif
+
+
+/*
+ * Externs
+ */
+extern int
+zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
+			   char *in_buf, size_t in_buf_len);
+
+#endif /* _ZEBRA_FPM_PRIVATE_H */

+ 32 - 0
zebra/zebra_rib.c

@@ -40,6 +40,7 @@
 #include "zebra/zserv.h"
 #include "zebra/redistribute.h"
 #include "zebra/debug.h"
+#include "zebra/zebra_fpm.h"
 
 /* Default rtm_table for all clients */
 extern struct zebra_t zebrad;
@@ -961,6 +962,11 @@ rib_install_kernel (struct route_node *rn, struct rib *rib)
   int ret = 0;
   struct nexthop *nexthop;
 
+  /*
+   * Make sure we update the FPM any time we send new information to
+   * the kernel.
+   */
+  zfpm_trigger_update (rn, "installing in kernel");
   switch (PREFIX_FAMILY (&rn->p))
     {
     case AF_INET:
@@ -988,6 +994,12 @@ rib_uninstall_kernel (struct route_node *rn, struct rib *rib)
   int ret = 0;
   struct nexthop *nexthop;
 
+  /*
+   * Make sure we update the FPM any time we send new information to
+   * the kernel.
+   */
+  zfpm_trigger_update (rn, "uninstalling from kernel");
+
   switch (PREFIX_FAMILY (&rn->p))
     {
     case AF_INET:
@@ -1012,6 +1024,8 @@ rib_uninstall (struct route_node *rn, struct rib *rib)
 {
   if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
     {
+      zfpm_trigger_update (rn, "rib_uninstall");
+
       redistribute_delete (&rn->p, rib);
       if (! RIB_SYSTEM_ROUTE (rib))
 	rib_uninstall_kernel (rn, rib);
@@ -1034,6 +1048,14 @@ rib_can_delete_dest (rib_dest_t *dest)
       return 0;
     }
 
+  /*
+   * Don't delete the dest if we have to update the FPM about this
+   * prefix.
+   */
+  if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) ||
+      CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM))
+    return 0;
+
   return 1;
 }
 
@@ -1187,6 +1209,8 @@ rib_process (struct route_node *rn)
                      __func__, buf, rn->p.prefixlen, select, fib);
       if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
         {
+	  zfpm_trigger_update (rn, "updating existing route");
+
           redistribute_delete (&rn->p, select);
           if (! RIB_SYSTEM_ROUTE (select))
             rib_uninstall_kernel (rn, select);
@@ -1228,6 +1252,9 @@ rib_process (struct route_node *rn)
       if (IS_ZEBRA_DEBUG_RIB)
         zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__,
           buf, rn->p.prefixlen, fib);
+
+      zfpm_trigger_update (rn, "removing existing route");
+
       redistribute_delete (&rn->p, fib);
       if (! RIB_SYSTEM_ROUTE (fib))
 	rib_uninstall_kernel (rn, fib);
@@ -1246,6 +1273,9 @@ rib_process (struct route_node *rn)
       if (IS_ZEBRA_DEBUG_RIB)
         zlog_debug ("%s: %s/%d: Adding route, select %p", __func__, buf,
           rn->p.prefixlen, select);
+
+      zfpm_trigger_update (rn, "new route selected");
+
       /* Set real nexthop. */
       nexthop_active_update (rn, select, 1);
 
@@ -3081,6 +3111,8 @@ rib_close_table (struct route_table *table)
           if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
 	    continue;
 
+	  zfpm_trigger_update (rn, NULL);
+
 	  if (! RIB_SYSTEM_ROUTE (rib))
 	    rib_uninstall_kernel (rn, rib);
         }