xref: /dpdk/examples/ipsec-secgw/rt.c (revision b2feed01d6675e7918740db2028a0037e9af1c2d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Routing Table (RT)
36  */
37 #include <sys/types.h>
38 #include <rte_lpm.h>
39 #include <rte_errno.h>
40 
41 #include "ipsec.h"
42 
43 #define RT_IPV4_MAX_RULES         64
44 
45 struct ipv4_route {
46 	uint32_t ip;
47 	uint8_t  depth;
48 	uint8_t  if_out;
49 };
50 
51 /* In the default routing table we have:
52  * ep0 protected ports 0 and 1, and unprotected ports 2 and 3.
53  */
54 static struct ipv4_route rt_ipv4_ep0[] = {
55 	{ IPv4(172, 16, 2, 5), 32, 0 },
56 	{ IPv4(172, 16, 2, 6), 32, 0 },
57 	{ IPv4(172, 16, 2, 7), 32, 1 },
58 	{ IPv4(172, 16, 2, 8), 32, 1 },
59 
60 	{ IPv4(192, 168, 115, 0), 24, 2 },
61 	{ IPv4(192, 168, 116, 0), 24, 2 },
62 	{ IPv4(192, 168, 117, 0), 24, 3 },
63 	{ IPv4(192, 168, 118, 0), 24, 3 },
64 
65 	{ IPv4(192, 168, 210, 0), 24, 2 },
66 
67 	{ IPv4(192, 168, 240, 0), 24, 2 },
68 	{ IPv4(192, 168, 250, 0), 24, 0 }
69 };
70 
71 /* In the default routing table we have:
72  * ep1 protected ports 0 and 1, and unprotected ports 2 and 3.
73  */
74 static struct ipv4_route rt_ipv4_ep1[] = {
75 	{ IPv4(172, 16, 1, 5), 32, 2 },
76 	{ IPv4(172, 16, 1, 6), 32, 2 },
77 	{ IPv4(172, 16, 1, 7), 32, 3 },
78 	{ IPv4(172, 16, 1, 8), 32, 3 },
79 
80 	{ IPv4(192, 168, 105, 0), 24, 0 },
81 	{ IPv4(192, 168, 106, 0), 24, 0 },
82 	{ IPv4(192, 168, 107, 0), 24, 1 },
83 	{ IPv4(192, 168, 108, 0), 24, 1 },
84 
85 	{ IPv4(192, 168, 200, 0), 24, 0 },
86 
87 	{ IPv4(192, 168, 240, 0), 24, 2 },
88 	{ IPv4(192, 168, 250, 0), 24, 0 }
89 };
90 
91 void
92 rt_init(struct socket_ctx *ctx, int socket_id, unsigned ep)
93 {
94 	char name[PATH_MAX];
95 	unsigned i;
96 	int ret;
97 	struct rte_lpm *lpm;
98 	struct ipv4_route *rt;
99 	char a, b, c, d;
100 	unsigned nb_routes;
101 	struct rte_lpm_config conf = { 0 };
102 
103 	if (ctx == NULL)
104 		rte_exit(EXIT_FAILURE, "NULL context.\n");
105 
106 	if (ctx->rt_ipv4 != NULL)
107 		rte_exit(EXIT_FAILURE, "Routing Table for socket %u already "
108 			"initialized\n", socket_id);
109 
110 	printf("Creating Routing Table (RT) context with %u max routes\n",
111 			RT_IPV4_MAX_RULES);
112 
113 	if (ep == 0) {
114 		rt = rt_ipv4_ep0;
115 		nb_routes = RTE_DIM(rt_ipv4_ep0);
116 	} else if (ep == 1) {
117 		rt = rt_ipv4_ep1;
118 		nb_routes = RTE_DIM(rt_ipv4_ep1);
119 	} else
120 		rte_exit(EXIT_FAILURE, "Invalid EP value %u. Only 0 or 1 "
121 			"supported.\n", ep);
122 
123 	/* create the LPM table */
124 	snprintf(name, sizeof(name), "%s_%u", "rt_ipv4", socket_id);
125 	conf.max_rules = RT_IPV4_MAX_RULES;
126 	conf.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
127 	lpm = rte_lpm_create(name, socket_id, &conf);
128 	if (lpm == NULL)
129 		rte_exit(EXIT_FAILURE, "Unable to create LPM table "
130 			"on socket %d\n", socket_id);
131 
132 	/* populate the LPM table */
133 	for (i = 0; i < nb_routes; i++) {
134 		ret = rte_lpm_add(lpm, rt[i].ip, rt[i].depth, rt[i].if_out);
135 		if (ret < 0)
136 			rte_exit(EXIT_FAILURE, "Unable to add entry num %u to "
137 				"LPM table on socket %d\n", i, socket_id);
138 
139 		uint32_t_to_char(rt[i].ip, &a, &b, &c, &d);
140 		printf("LPM: Adding route %hhu.%hhu.%hhu.%hhu/%hhu (%hhu)\n",
141 				a, b, c, d, rt[i].depth, rt[i].if_out);
142 	}
143 
144 	ctx->rt_ipv4 = (struct rt_ctx *)lpm;
145 }
146