]> git.proxmox.com Git - mirror_frr.git/blob - lib/srv6.h
lib: Add support for flags to the SRv6 locator
[mirror_frr.git] / lib / srv6.h
1 /*
2 * SRv6 definitions
3 * Copyright (C) 2020 Hiroki Shirokura, LINE Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef _FRR_SRV6_H
21 #define _FRR_SRV6_H
22
23 #include <zebra.h>
24 #include "prefix.h"
25 #include "json.h"
26
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #define SRV6_MAX_SIDS 16
31 #define SRV6_LOCNAME_SIZE 256
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define sid2str(sid, str, size) \
38 inet_ntop(AF_INET6, sid, str, size)
39
40 enum seg6_mode_t {
41 INLINE,
42 ENCAP,
43 L2ENCAP,
44 };
45
46 enum seg6local_action_t {
47 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC = 0,
48 ZEBRA_SEG6_LOCAL_ACTION_END = 1,
49 ZEBRA_SEG6_LOCAL_ACTION_END_X = 2,
50 ZEBRA_SEG6_LOCAL_ACTION_END_T = 3,
51 ZEBRA_SEG6_LOCAL_ACTION_END_DX2 = 4,
52 ZEBRA_SEG6_LOCAL_ACTION_END_DX6 = 5,
53 ZEBRA_SEG6_LOCAL_ACTION_END_DX4 = 6,
54 ZEBRA_SEG6_LOCAL_ACTION_END_DT6 = 7,
55 ZEBRA_SEG6_LOCAL_ACTION_END_DT4 = 8,
56 ZEBRA_SEG6_LOCAL_ACTION_END_B6 = 9,
57 ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP = 10,
58 ZEBRA_SEG6_LOCAL_ACTION_END_BM = 11,
59 ZEBRA_SEG6_LOCAL_ACTION_END_S = 12,
60 ZEBRA_SEG6_LOCAL_ACTION_END_AS = 13,
61 ZEBRA_SEG6_LOCAL_ACTION_END_AM = 14,
62 ZEBRA_SEG6_LOCAL_ACTION_END_BPF = 15,
63 ZEBRA_SEG6_LOCAL_ACTION_END_DT46 = 16,
64 };
65
66 struct seg6_segs {
67 size_t num_segs;
68 struct in6_addr segs[256];
69 };
70
71 struct seg6local_context {
72 struct in_addr nh4;
73 struct in6_addr nh6;
74 uint32_t table;
75 };
76
77 struct srv6_locator {
78 char name[SRV6_LOCNAME_SIZE];
79 struct prefix_ipv6 prefix;
80
81 /*
82 * Bit length of SRv6 locator described in
83 * draft-ietf-bess-srv6-services-05#section-3.2.1
84 */
85 uint8_t block_bits_length;
86 uint8_t node_bits_length;
87 uint8_t function_bits_length;
88 uint8_t argument_bits_length;
89
90 int algonum;
91 uint64_t current;
92 bool status_up;
93 struct list *chunks;
94
95 uint8_t flags;
96
97 QOBJ_FIELDS;
98 };
99 DECLARE_QOBJ_TYPE(srv6_locator);
100
101 struct srv6_locator_chunk {
102 char locator_name[SRV6_LOCNAME_SIZE];
103 struct prefix_ipv6 prefix;
104
105 /*
106 * Bit length of SRv6 locator described in
107 * draft-ietf-bess-srv6-services-05#section-3.2.1
108 */
109 uint8_t block_bits_length;
110 uint8_t node_bits_length;
111 uint8_t function_bits_length;
112 uint8_t argument_bits_length;
113
114 /*
115 * For Zclient communication values
116 */
117 uint8_t keep;
118 uint8_t proto;
119 uint16_t instance;
120 uint32_t session_id;
121
122 uint8_t flags;
123 };
124
125 struct nexthop_srv6 {
126 /* SRv6 localsid info for Endpoint-behaviour */
127 enum seg6local_action_t seg6local_action;
128 struct seg6local_context seg6local_ctx;
129
130 /* SRv6 Headend-behaviour */
131 struct in6_addr seg6_segs;
132 };
133
134 static inline const char *seg6_mode2str(enum seg6_mode_t mode)
135 {
136 switch (mode) {
137 case INLINE:
138 return "INLINE";
139 case ENCAP:
140 return "ENCAP";
141 case L2ENCAP:
142 return "L2ENCAP";
143 default:
144 return "unknown";
145 }
146 }
147
148 static inline bool sid_same(
149 const struct in6_addr *a,
150 const struct in6_addr *b)
151 {
152 if (!a && !b)
153 return true;
154 else if (!(a && b))
155 return false;
156 else
157 return memcmp(a, b, sizeof(struct in6_addr)) == 0;
158 }
159
160 static inline bool sid_diff(
161 const struct in6_addr *a,
162 const struct in6_addr *b)
163 {
164 return !sid_same(a, b);
165 }
166
167 static inline bool sid_zero(
168 const struct in6_addr *a)
169 {
170 struct in6_addr zero = {};
171
172 return sid_same(a, &zero);
173 }
174
175 static inline void *sid_copy(struct in6_addr *dst,
176 const struct in6_addr *src)
177 {
178 return memcpy(dst, src, sizeof(struct in6_addr));
179 }
180
181 const char *
182 seg6local_action2str(uint32_t action);
183
184 const char *seg6local_context2str(char *str, size_t size,
185 const struct seg6local_context *ctx,
186 uint32_t action);
187
188 int snprintf_seg6_segs(char *str,
189 size_t size, const struct seg6_segs *segs);
190
191 extern struct srv6_locator *srv6_locator_alloc(const char *name);
192 extern struct srv6_locator_chunk *srv6_locator_chunk_alloc(void);
193 extern void srv6_locator_free(struct srv6_locator *locator);
194 extern void srv6_locator_chunk_free(struct srv6_locator_chunk *chunk);
195 json_object *srv6_locator_chunk_json(const struct srv6_locator_chunk *chunk);
196 json_object *srv6_locator_json(const struct srv6_locator *loc);
197 json_object *srv6_locator_detailed_json(const struct srv6_locator *loc);
198 json_object *
199 srv6_locator_chunk_detailed_json(const struct srv6_locator_chunk *chunk);
200
201 #ifdef __cplusplus
202 }
203 #endif
204
205 #endif