5deb9702eb30ab481c0c8867870310287dad3bcc
[vpp.git] / src / vnet / bfd / bfd_protocol.c
1 /*
2  * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/bfd/bfd_protocol.h>
16
17 u8
18 bfd_pkt_get_version (const bfd_pkt_t * pkt)
19 {
20   return pkt->head.vers_diag >> 5;
21 }
22
23 void
24 bfd_pkt_set_version (bfd_pkt_t * pkt, int version)
25 {
26   pkt->head.vers_diag =
27     (version << 5) | (pkt->head.vers_diag & ((1 << 5) - 1));
28 }
29
30 u8
31 bfd_pkt_get_diag_code (const bfd_pkt_t * pkt)
32 {
33   return pkt->head.vers_diag & ((1 << 5) - 1);
34 }
35
36 void
37 bfd_pkt_set_diag_code (bfd_pkt_t * pkt, int value)
38 {
39   pkt->head.vers_diag =
40     (pkt->head.vers_diag & ~((1 << 5) - 1)) | (value & ((1 << 5) - 1));
41 }
42
43 u8
44 bfd_pkt_get_state (const bfd_pkt_t * pkt)
45 {
46   return pkt->head.sta_flags >> 6;
47 }
48
49 void
50 bfd_pkt_set_state (bfd_pkt_t * pkt, int value)
51 {
52   pkt->head.sta_flags = (value << 6) | (pkt->head.sta_flags & ((1 << 6) - 1));
53 }
54
55 u8
56 bfd_pkt_get_poll (const bfd_pkt_t * pkt)
57 {
58   return (pkt->head.sta_flags >> 5) & 1;
59 }
60
61 void
62 bfd_pkt_set_poll (bfd_pkt_t * pkt)
63 {
64   pkt->head.sta_flags |= 1 << 5;
65 }
66
67 u8
68 bfd_pkt_get_final (const bfd_pkt_t * pkt)
69 {
70   return (pkt->head.sta_flags >> 4) & 1;
71 }
72
73 void
74 bfd_pkt_set_final (bfd_pkt_t * pkt)
75 {
76   pkt->head.sta_flags |= 1 << 4;
77 }
78
79 u8
80 bfd_pkt_get_control_plane_independent (const bfd_pkt_t * pkt)
81 {
82   return (pkt->head.sta_flags >> 3) & 1;
83 }
84
85 #if 0
86 void
87 bfd_pkt_set_control_plane_independent (bfd_pkt_t * pkt)
88 {
89   pkt->head.sta_flags |= 1 << 3;
90 }
91 #endif
92
93 u8
94 bfd_pkt_get_auth_present (const bfd_pkt_t * pkt)
95 {
96   return (pkt->head.sta_flags >> 2) & 1;
97 }
98
99 void
100 bfd_pkt_set_auth_present (bfd_pkt_t * pkt)
101 {
102   pkt->head.sta_flags |= 1 << 2;
103 }
104
105 u8
106 bfd_pkt_get_demand (const bfd_pkt_t * pkt)
107 {
108   return (pkt->head.sta_flags >> 1) & 1;
109 }
110
111 #if 0
112 void
113 bfd_pkt_set_demand (bfd_pkt_t * pkt)
114 {
115   pkt->head.sta_flags |= 1 << 1;
116 }
117 #endif
118
119 u8
120 bfd_pkt_get_multipoint (const bfd_pkt_t * pkt)
121 {
122   return (pkt->head.sta_flags >> 0) & 1;
123 }
124
125 #if 0
126 void
127 bfd_pkt_set_multipoint (bfd_pkt_t * pkt)
128 {
129   pkt->head.sta_flags |= 1 << 0;
130 }
131 #endif
132
133 u32
134 bfd_max_len_for_auth_type (bfd_auth_type_e auth_type)
135 {
136 #define F(t, l, n, s) \
137   if (auth_type == t) \
138     {                 \
139       return l;       \
140     }
141   foreach_bfd_auth_type (F);
142 #undef F
143   return 0;
144 }
145
146 const char *
147 bfd_auth_type_str (bfd_auth_type_e auth_type)
148 {
149 #define F(t, l, n, s) \
150   if (auth_type == t) \
151     {                 \
152       return s;       \
153     }
154   foreach_bfd_auth_type (F);
155 #undef F
156   return "UNKNOWN";
157 }
158
159 const char *
160 bfd_diag_code_string (bfd_diag_code_e diag)
161 {
162 #define F(n, t, s)             \
163   case BFD_DIAG_CODE_NAME (t): \
164     return s;
165   switch (diag)
166     {
167     foreach_bfd_diag_code (F)}
168   return "UNKNOWN";
169 #undef F
170 }
171
172 const char *
173 bfd_state_string (bfd_state_e state)
174 {
175 #define F(n, t, s)         \
176   case BFD_STATE_NAME (t): \
177     return s;
178   switch (state)
179     {
180     foreach_bfd_state (F)}
181   return "UNKNOWN";
182 #undef F
183 }
184
185 /*
186  * fd.io coding-style-patch-verification: ON
187  *
188  * Local Variables:
189  * eval: (c-set-style "gnu")
190  * End:
191  */