]> git.proxmox.com Git - mirror_frr.git/blame - lib/srv6.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / srv6.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
e496b420
HS
2/*
3 * SRv6 definitions
4 * Copyright (C) 2020 Hiroki Shirokura, LINE Corporation
e496b420
HS
5 */
6
7#ifndef _FRR_SRV6_H
8#define _FRR_SRV6_H
9
10#include <zebra.h>
f2867068
HS
11#include "prefix.h"
12#include "json.h"
13
e496b420
HS
14#include <arpa/inet.h>
15#include <netinet/in.h>
16
17#define SRV6_MAX_SIDS 16
f2867068 18#define SRV6_LOCNAME_SIZE 256
e496b420
HS
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#define sid2str(sid, str, size) \
25 inet_ntop(AF_INET6, sid, str, size)
26
27enum seg6_mode_t {
28 INLINE,
29 ENCAP,
30 L2ENCAP,
31};
32
33enum seg6local_action_t {
34 ZEBRA_SEG6_LOCAL_ACTION_UNSPEC = 0,
35 ZEBRA_SEG6_LOCAL_ACTION_END = 1,
36 ZEBRA_SEG6_LOCAL_ACTION_END_X = 2,
37 ZEBRA_SEG6_LOCAL_ACTION_END_T = 3,
38 ZEBRA_SEG6_LOCAL_ACTION_END_DX2 = 4,
39 ZEBRA_SEG6_LOCAL_ACTION_END_DX6 = 5,
40 ZEBRA_SEG6_LOCAL_ACTION_END_DX4 = 6,
41 ZEBRA_SEG6_LOCAL_ACTION_END_DT6 = 7,
42 ZEBRA_SEG6_LOCAL_ACTION_END_DT4 = 8,
43 ZEBRA_SEG6_LOCAL_ACTION_END_B6 = 9,
44 ZEBRA_SEG6_LOCAL_ACTION_END_B6_ENCAP = 10,
45 ZEBRA_SEG6_LOCAL_ACTION_END_BM = 11,
46 ZEBRA_SEG6_LOCAL_ACTION_END_S = 12,
47 ZEBRA_SEG6_LOCAL_ACTION_END_AS = 13,
48 ZEBRA_SEG6_LOCAL_ACTION_END_AM = 14,
49 ZEBRA_SEG6_LOCAL_ACTION_END_BPF = 15,
8bea07e4 50 ZEBRA_SEG6_LOCAL_ACTION_END_DT46 = 16,
e496b420
HS
51};
52
53struct seg6_segs {
54 size_t num_segs;
55 struct in6_addr segs[256];
56};
57
58struct seg6local_context {
59 struct in_addr nh4;
60 struct in6_addr nh6;
61 uint32_t table;
62};
63
f2867068
HS
64struct srv6_locator {
65 char name[SRV6_LOCNAME_SIZE];
66 struct prefix_ipv6 prefix;
ac6a9479
HS
67
68 /*
69 * Bit length of SRv6 locator described in
70 * draft-ietf-bess-srv6-services-05#section-3.2.1
71 */
72 uint8_t block_bits_length;
73 uint8_t node_bits_length;
f2867068 74 uint8_t function_bits_length;
ac6a9479
HS
75 uint8_t argument_bits_length;
76
f2867068
HS
77 int algonum;
78 uint64_t current;
79 bool status_up;
80 struct list *chunks;
81
9b7491e1 82 uint8_t flags;
02b1544a 83#define SRV6_LOCATOR_USID (1 << 0) /* The SRv6 Locator is a uSID Locator */
9b7491e1 84
f2867068
HS
85 QOBJ_FIELDS;
86};
87DECLARE_QOBJ_TYPE(srv6_locator);
88
89struct srv6_locator_chunk {
ac6a9479
HS
90 char locator_name[SRV6_LOCNAME_SIZE];
91 struct prefix_ipv6 prefix;
92
93 /*
94 * Bit length of SRv6 locator described in
95 * draft-ietf-bess-srv6-services-05#section-3.2.1
96 */
97 uint8_t block_bits_length;
98 uint8_t node_bits_length;
99 uint8_t function_bits_length;
100 uint8_t argument_bits_length;
101
102 /*
103 * For Zclient communication values
104 */
f2867068
HS
105 uint8_t keep;
106 uint8_t proto;
107 uint16_t instance;
108 uint32_t session_id;
9b7491e1
CS
109
110 uint8_t flags;
f2867068
HS
111};
112
04947825
CS
113/*
114 * SRv6 Endpoint Behavior codepoints, as defined by IANA in
115 * https://www.iana.org/assignments/segment-routing/segment-routing.xhtml
116 */
117enum srv6_endpoint_behavior_codepoint {
118 SRV6_ENDPOINT_BEHAVIOR_RESERVED = 0x0000,
119 SRV6_ENDPOINT_BEHAVIOR_END_DT6 = 0x0012,
120 SRV6_ENDPOINT_BEHAVIOR_END_DT4 = 0x0013,
121 SRV6_ENDPOINT_BEHAVIOR_END_DT46 = 0x0014,
122 SRV6_ENDPOINT_BEHAVIOR_END_DT6_USID = 0x003E,
123 SRV6_ENDPOINT_BEHAVIOR_END_DT4_USID = 0x003F,
124 SRV6_ENDPOINT_BEHAVIOR_END_DT46_USID = 0x0040,
125 SRV6_ENDPOINT_BEHAVIOR_OPAQUE = 0xFFFF,
f2867068
HS
126};
127
eab0f8f0
HS
128struct nexthop_srv6 {
129 /* SRv6 localsid info for Endpoint-behaviour */
130 enum seg6local_action_t seg6local_action;
131 struct seg6local_context seg6local_ctx;
132
133 /* SRv6 Headend-behaviour */
134 struct in6_addr seg6_segs;
135};
136
e496b420
HS
137static inline const char *seg6_mode2str(enum seg6_mode_t mode)
138{
139 switch (mode) {
140 case INLINE:
141 return "INLINE";
142 case ENCAP:
143 return "ENCAP";
144 case L2ENCAP:
145 return "L2ENCAP";
146 default:
147 return "unknown";
148 }
149}
150
151static inline bool sid_same(
152 const struct in6_addr *a,
153 const struct in6_addr *b)
154{
155 if (!a && !b)
156 return true;
157 else if (!(a && b))
158 return false;
159 else
160 return memcmp(a, b, sizeof(struct in6_addr)) == 0;
161}
162
163static inline bool sid_diff(
164 const struct in6_addr *a,
165 const struct in6_addr *b)
166{
167 return !sid_same(a, b);
168}
169
170static inline bool sid_zero(
171 const struct in6_addr *a)
172{
173 struct in6_addr zero = {};
174
175 return sid_same(a, &zero);
176}
177
178static inline void *sid_copy(struct in6_addr *dst,
179 const struct in6_addr *src)
180{
181 return memcpy(dst, src, sizeof(struct in6_addr));
182}
183
184const char *
185seg6local_action2str(uint32_t action);
186
7a4b49bd
HS
187const char *seg6local_context2str(char *str, size_t size,
188 const struct seg6local_context *ctx,
189 uint32_t action);
e496b420
HS
190
191int snprintf_seg6_segs(char *str,
192 size_t size, const struct seg6_segs *segs);
193
f2867068
HS
194extern struct srv6_locator *srv6_locator_alloc(const char *name);
195extern struct srv6_locator_chunk *srv6_locator_chunk_alloc(void);
196extern void srv6_locator_free(struct srv6_locator *locator);
69467313 197extern void srv6_locator_chunk_free(struct srv6_locator_chunk **chunk);
f2867068
HS
198json_object *srv6_locator_chunk_json(const struct srv6_locator_chunk *chunk);
199json_object *srv6_locator_json(const struct srv6_locator *loc);
559f4b2f
YS
200json_object *srv6_locator_detailed_json(const struct srv6_locator *loc);
201json_object *
202srv6_locator_chunk_detailed_json(const struct srv6_locator_chunk *chunk);
f2867068 203
e496b420
HS
204#ifdef __cplusplus
205}
206#endif
207
208#endif