examples/ipsec-secgw: support IPv6
[dpdk.git] / examples / ipsec-secgw / rt.c
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_lpm6.h>
40 #include <rte_errno.h>
41 #include <rte_ip.h>
42
43 #include "ipsec.h"
44
45 #define RT_IPV4_MAX_RULES       1024
46 #define RT_IPV6_MAX_RULES       1024
47
48 struct ip4_route {
49         uint32_t ip;
50         uint8_t depth;
51         uint8_t if_out;
52 };
53
54 struct ip6_route {
55         uint8_t ip[16];
56         uint8_t depth;
57         uint8_t if_out;
58 };
59
60 static struct ip4_route rt_ip4_ep0[] = {
61         /* Outbound */
62         /* Tunnels */
63         { IPv4(172, 16, 2, 5), 32, 0 },
64         { IPv4(172, 16, 2, 6), 32, 1 },
65         /* Bypass */
66         { IPv4(192, 168, 240, 0), 24, 0 },
67         { IPv4(192, 168, 241, 0), 24, 1 },
68
69         /* Inbound */
70         /* Tunnels */
71         { IPv4(192, 168, 115, 0), 24, 2 },
72         { IPv4(192, 168, 116, 0), 24, 3 },
73         { IPv4(192, 168, 65, 0), 24, 2 },
74         { IPv4(192, 168, 66, 0), 24, 3 },
75         /* NULL */
76         { IPv4(192, 168, 210, 0), 24, 2 },
77         { IPv4(192, 168, 211, 0), 24, 3 },
78         /* Bypass */
79         { IPv4(192, 168, 245, 0), 24, 2 },
80         { IPv4(192, 168, 246, 0), 24, 3 },
81 };
82
83 static struct ip6_route rt_ip6_ep0[] = {
84         /* Outbound */
85         /* Tunnels */
86         { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
87                   0x22, 0x22, 0x22, 0x22, 0x22, 0x55, 0x55 }, 116, 0 },
88         { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
89                   0x22, 0x22, 0x22, 0x22, 0x22, 0x66, 0x66 }, 116, 1 },
90         /* Inbound */
91         /* Tunnels */
92         { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
93                   0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
94         { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb,
95                   0xbb, 0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
96         { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
97                   0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
98         { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
99                   0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
100 };
101
102 static struct ip4_route rt_ip4_ep1[] = {
103         /* Outbound */
104         /* Tunnels */
105         { IPv4(172, 16, 1, 5), 32, 0 },
106         { IPv4(172, 16, 1, 6), 32, 1 },
107         /* Bypass */
108         { IPv4(192, 168, 245, 0), 24, 0 },
109         { IPv4(192, 168, 246, 0), 24, 1 },
110
111         /* Inbound */
112         /* Tunnels */
113         { IPv4(192, 168, 105, 0), 24, 2 },
114         { IPv4(192, 168, 106, 0), 24, 3 },
115         { IPv4(192, 168, 55, 0), 24, 2 },
116         { IPv4(192, 168, 56, 0), 24, 3 },
117         /* NULL */
118         { IPv4(192, 168, 200, 0), 24, 2 },
119         { IPv4(192, 168, 201, 0), 24, 3 },
120         /* Bypass */
121         { IPv4(192, 168, 240, 0), 24, 2 },
122         { IPv4(192, 168, 241, 0), 24, 3 },
123 };
124
125 static struct ip6_route rt_ip6_ep1[] = {
126         /* Outbound */
127         /* Tunnels */
128         { { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
129                   0x11, 0x11, 0x11, 0x11, 0x11, 0x55, 0x55 }, 116, 0 },
130         { { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
131                   0x11, 0x11, 0x11, 0x11, 0x11, 0x66, 0x66 }, 116, 1 },
132
133         /* Inbound */
134         /* Tunnels */
135         { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
136                   0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
137         { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb,
138                   0xbb, 0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
139         { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
140                   0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
141         { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
142                   0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
143 };
144
145 void
146 rt_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t ep)
147 {
148         char name[PATH_MAX];
149         uint32_t i;
150         int32_t ret;
151         struct rte_lpm *lpm;
152         struct rte_lpm6 *lpm6;
153         struct ip4_route *rt;
154         struct ip6_route *rt6;
155         char a, b, c, d;
156         uint32_t nb_routes, nb_routes6;
157         struct rte_lpm_config conf = { 0 };
158         struct rte_lpm6_config conf6 = { 0 };
159
160         if (ctx == NULL)
161                 rte_exit(EXIT_FAILURE, "NULL context.\n");
162
163         if (ctx->rt_ip4 != NULL)
164                 rte_exit(EXIT_FAILURE, "IPv4 Routing Table for socket %u "
165                         "already initialized\n", socket_id);
166
167         if (ctx->rt_ip6 != NULL)
168                 rte_exit(EXIT_FAILURE, "IPv6 Routing Table for socket %u "
169                         "already initialized\n", socket_id);
170
171         printf("Creating IPv4 Routing Table (RT) context with %u max routes\n",
172                         RT_IPV4_MAX_RULES);
173
174         if (ep == 0) {
175                 rt = rt_ip4_ep0;
176                 nb_routes = RTE_DIM(rt_ip4_ep0);
177                 rt6 = rt_ip6_ep0;
178                 nb_routes6 = RTE_DIM(rt_ip6_ep0);
179         } else if (ep == 1) {
180                 rt = rt_ip4_ep1;
181                 nb_routes = RTE_DIM(rt_ip4_ep1);
182                 rt6 = rt_ip6_ep1;
183                 nb_routes6 = RTE_DIM(rt_ip6_ep1);
184         } else
185                 rte_exit(EXIT_FAILURE, "Invalid EP value %u. Only 0 or 1 "
186                         "supported.\n", ep);
187
188         /* create the LPM table */
189         snprintf(name, sizeof(name), "%s_%u", "rt_ip4", socket_id);
190         conf.max_rules = RT_IPV4_MAX_RULES;
191         conf.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
192         lpm = rte_lpm_create(name, socket_id, &conf);
193         if (lpm == NULL)
194                 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
195                         "on socket %d\n", name, socket_id);
196
197         /* populate the LPM table */
198         for (i = 0; i < nb_routes; i++) {
199                 ret = rte_lpm_add(lpm, rt[i].ip, rt[i].depth, rt[i].if_out);
200                 if (ret < 0)
201                         rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
202                                 "LPM table on socket %d\n", i, name, socket_id);
203
204                 uint32_t_to_char(rt[i].ip, &a, &b, &c, &d);
205                 printf("LPM: Adding route %hhu.%hhu.%hhu.%hhu/%hhu (%hhu)\n",
206                                 a, b, c, d, rt[i].depth, rt[i].if_out);
207         }
208
209         snprintf(name, sizeof(name), "%s_%u", "rt_ip6", socket_id);
210         conf6.max_rules = RT_IPV6_MAX_RULES;
211         conf6.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
212         lpm6 = rte_lpm6_create(name, socket_id, &conf6);
213         if (lpm6 == NULL)
214                 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
215                         "on socket %d\n", name, socket_id);
216
217         /* populate the LPM table */
218         for (i = 0; i < nb_routes6; i++) {
219                 ret = rte_lpm6_add(lpm6, rt6[i].ip, rt6[i].depth,
220                                 rt6[i].if_out);
221                 if (ret < 0)
222                         rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
223                                 "LPM table on socket %d\n", i, name, socket_id);
224
225                 printf("LPM6: Adding route "
226                         " %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hhx (%hhx)\n",
227                         (uint16_t)((rt6[i].ip[0] << 8) | rt6[i].ip[1]),
228                         (uint16_t)((rt6[i].ip[2] << 8) | rt6[i].ip[3]),
229                         (uint16_t)((rt6[i].ip[4] << 8) | rt6[i].ip[5]),
230                         (uint16_t)((rt6[i].ip[6] << 8) | rt6[i].ip[7]),
231                         (uint16_t)((rt6[i].ip[8] << 8) | rt6[i].ip[9]),
232                         (uint16_t)((rt6[i].ip[10] << 8) | rt6[i].ip[11]),
233                         (uint16_t)((rt6[i].ip[12] << 8) | rt6[i].ip[13]),
234                         (uint16_t)((rt6[i].ip[14] << 8) | rt6[i].ip[15]),
235                         rt6[i].depth, rt6[i].if_out);
236         }
237
238         ctx->rt_ip4 = (struct rt_ctx *)lpm;
239         ctx->rt_ip6 = (struct rt_ctx *)lpm6;
240 }