From c447ad08b2c880d5f3ef35af2e4055fcbc970961 Mon Sep 17 00:00:00 2001
From: Donald Sharp <sharpd@cumulusnetworks.com>
Date: Fri, 3 May 2019 20:54:20 -0400
Subject: [PATCH] doc, zebra: Remove "table X" command

This command is broken and has been broken since the introduction
of vrf's.  Since no-one has complained it is safe to assume that
there is no call for this specialized linux command.  Remove
from the system with extreme prejudice.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
---
 doc/user/zebra.rst   |  8 --------
 zebra/redistribute.c |  9 ++++-----
 zebra/zebra_rib.c    |  3 +--
 zebra/zebra_router.c |  1 -
 zebra/zebra_router.h |  3 ---
 zebra/zebra_vrf.c    | 12 ++++--------
 zebra/zebra_vty.c    | 42 ------------------------------------------
 zebra/zserv.c        |  4 ----
 zebra/zserv.h        |  3 ---
 9 files changed, 9 insertions(+), 76 deletions(-)

--- a/doc/user/zebra.rst
+++ b/doc/user/zebra.rst
@@ -284,6 +284,14 @@
    for InterASv2 link in OSPF (RFC5392).  Note that this option is not yet
    supported for ISIS (RFC5316).
 
+.. index:: table TABLENO
+.. clicmd:: table TABLENO
+
+   Select the primary kernel routing table to be used. This only works for
+   kernels supporting multiple routing tables (like GNU/Linux 2.2.x and later).
+   After setting TABLENO with this command, static routes defined after this
+   are added to the specified table.
+
 .. index:: ip nht resolve-via-default
 .. clicmd:: ip nht resolve-via-default
 
--- a/zebra/zebra_rib.c
+++ b/zebra/zebra_rib.c
@@ -167,7 +167,8 @@
 
 int is_zebra_main_routing_table(uint32_t table_id)
 {
-	if (table_id == RT_TABLE_MAIN)
+	if ((table_id == RT_TABLE_MAIN)
+	    || (table_id == zrouter.rtm_table_default))
 		return 1;
 	return 0;
 }
--- a/zebra/zebra_router.c
+++ b/zebra/zebra_router.c
@@ -243,6 +243,7 @@
 {
 	zrouter.sequence_num = 0;
 
+	zrouter.rtm_table_default = 0;
 	zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS;
 
 	zrouter.rtadv_sock = -1;
--- a/zebra/zebra_router.h
+++ b/zebra/zebra_router.h
@@ -139,6 +139,9 @@
 	/* A sequence number used for tracking routes */
 	_Atomic uint32_t sequence_num;
 
+	/* The default table used for this router */
+	uint32_t rtm_table_default;
+
 	/* rib work queue */
 #define ZEBRA_RIB_PROCESS_HOLD_TIME 10
 #define ZEBRA_RIB_PROCESS_RETRY_TIME 1
--- a/zebra/zebra_vty.c
+++ b/zebra/zebra_vty.c
@@ -3113,6 +3113,40 @@
 	return 1;
 }
 
+#ifdef HAVE_NETLINK
+/* Display default rtm_table for all clients. */
+DEFUN (show_table,
+       show_table_cmd,
+       "show table",
+       SHOW_STR
+       "default routing table to use for all clients\n")
+{
+	vty_out(vty, "table %d\n", zrouter.rtm_table_default);
+	return CMD_SUCCESS;
+}
+
+DEFUN (config_table,
+       config_table_cmd,
+       "table TABLENO",
+       "Configure target kernel routing table\n"
+       "TABLE integer\n")
+{
+	zrouter.rtm_table_default = strtol(argv[1]->arg, (char **)0, 10);
+	return CMD_SUCCESS;
+}
+
+DEFUN (no_config_table,
+       no_config_table_cmd,
+       "no table [TABLENO]",
+       NO_STR
+       "Configure target kernel routing table\n"
+       "TABLE integer\n")
+{
+	zrouter.rtm_table_default = 0;
+	return CMD_SUCCESS;
+}
+#endif
+
 DEFUN (show_zebra,
        show_zebra_cmd,
        "show zebra",
@@ -3356,6 +3390,8 @@
 /* Table configuration write function. */
 static int config_write_table(struct vty *vty)
 {
+	if (zrouter.rtm_table_default)
+		vty_out(vty, "table %d\n", zrouter.rtm_table_default);
 	return 0;
 }
 
@@ -3459,6 +3495,12 @@
 	install_element(CONFIG_NODE, &no_ip_forwarding_cmd);
 	install_element(ENABLE_NODE, &show_zebra_cmd);
 
+#ifdef HAVE_NETLINK
+	install_element(VIEW_NODE, &show_table_cmd);
+	install_element(CONFIG_NODE, &config_table_cmd);
+	install_element(CONFIG_NODE, &no_config_table_cmd);
+#endif /* HAVE_NETLINK */
+
 	install_element(VIEW_NODE, &show_ipv6_forwarding_cmd);
 	install_element(CONFIG_NODE, &ipv6_forwarding_cmd);
 	install_element(CONFIG_NODE, &no_ipv6_forwarding_cmd);
--- a/zebra/zserv.c
+++ b/zebra/zserv.c
@@ -696,6 +696,9 @@
 	client->wb = buffer_new(0);
 	TAILQ_INIT(&(client->gr_info_queue));
 
+	/* Set table number. */
+	client->rtm_table = zrouter.rtm_table_default;
+
 	atomic_store_explicit(&client->connect_time, (uint32_t) monotime(NULL),
 			      memory_order_relaxed);
 
@@ -893,6 +896,7 @@
 
 	vty_out(vty, "------------------------ \n");
 	vty_out(vty, "FD: %d \n", client->sock);
+	vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
 
 	connect_time = (time_t) atomic_load_explicit(&client->connect_time,
 						     memory_order_relaxed);
--- a/zebra/zserv.h
+++ b/zebra/zserv.h
@@ -119,6 +119,9 @@
 	/* Threads for the main pthread */
 	struct thread *t_cleanup;
 
+	/* default routing table this client munges */
+	int rtm_table;
+
 	/* This client's redistribute flag. */
 	struct redist_proto mi_redist[AFI_MAX][ZEBRA_ROUTE_MAX];
 	vrf_bitmap_t redist[AFI_MAX][ZEBRA_ROUTE_MAX];
