Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 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 | */ |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 15 | /** |
| 16 | * @file |
| 17 | * @brief BFD protocol implementation |
| 18 | */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 19 | #include <vnet/bfd/bfd_protocol.h> |
| 20 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 21 | u8 |
| 22 | bfd_pkt_get_version (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 23 | { |
| 24 | return pkt->head.vers_diag >> 5; |
| 25 | } |
| 26 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 27 | void |
| 28 | bfd_pkt_set_version (bfd_pkt_t * pkt, int version) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 29 | { |
| 30 | pkt->head.vers_diag = |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 31 | (version << 5) | (pkt->head.vers_diag & ((1 << 5) - 1)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 34 | u8 |
| 35 | bfd_pkt_get_diag_code (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 36 | { |
| 37 | return pkt->head.vers_diag & ((1 << 5) - 1); |
| 38 | } |
| 39 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 40 | void |
| 41 | bfd_pkt_set_diag_code (bfd_pkt_t * pkt, int value) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 42 | { |
| 43 | pkt->head.vers_diag = |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 44 | (pkt->head.vers_diag & ~((1 << 5) - 1)) | (value & ((1 << 5) - 1)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 47 | u8 |
| 48 | bfd_pkt_get_state (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 49 | { |
| 50 | return pkt->head.sta_flags >> 6; |
| 51 | } |
| 52 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 53 | void |
| 54 | bfd_pkt_set_state (bfd_pkt_t * pkt, int value) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 55 | { |
| 56 | pkt->head.sta_flags = (value << 6) | (pkt->head.sta_flags & ((1 << 6) - 1)); |
| 57 | } |
| 58 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 59 | u8 |
| 60 | bfd_pkt_get_poll (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 61 | { |
| 62 | return (pkt->head.sta_flags >> 5) & 1; |
| 63 | } |
| 64 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 65 | void |
| 66 | bfd_pkt_set_poll (bfd_pkt_t * pkt) |
| 67 | { |
| 68 | pkt->head.sta_flags |= 1 << 5; |
| 69 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 70 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 71 | u8 |
| 72 | bfd_pkt_get_final (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 73 | { |
| 74 | return (pkt->head.sta_flags >> 4) & 1; |
| 75 | } |
| 76 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 77 | void |
| 78 | bfd_pkt_set_final (bfd_pkt_t * pkt) |
| 79 | { |
| 80 | pkt->head.sta_flags |= 1 << 4; |
| 81 | } |
| 82 | |
| 83 | u8 |
| 84 | bfd_pkt_get_control_plane_independent (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 85 | { |
| 86 | return (pkt->head.sta_flags >> 3) & 1; |
| 87 | } |
| 88 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 89 | #if 0 |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 90 | void |
| 91 | bfd_pkt_set_control_plane_independent (bfd_pkt_t * pkt) |
| 92 | { |
| 93 | pkt->head.sta_flags |= 1 << 3; |
| 94 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 95 | #endif |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 96 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 97 | u8 |
| 98 | bfd_pkt_get_auth_present (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 99 | { |
| 100 | return (pkt->head.sta_flags >> 2) & 1; |
| 101 | } |
| 102 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 103 | void |
| 104 | bfd_pkt_set_auth_present (bfd_pkt_t * pkt) |
| 105 | { |
| 106 | pkt->head.sta_flags |= 1 << 2; |
| 107 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 108 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 109 | u8 |
| 110 | bfd_pkt_get_demand (const bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 111 | { |
| 112 | return (pkt->head.sta_flags >> 1) & 1; |
| 113 | } |
| 114 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 115 | #if 0 |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 116 | void |
| 117 | bfd_pkt_set_demand (bfd_pkt_t * pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 118 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 119 | pkt->head.sta_flags |= 1 << 1; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 120 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 121 | #endif |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 122 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 123 | u8 |
| 124 | bfd_pkt_get_multipoint (const bfd_pkt_t * pkt) |
| 125 | { |
| 126 | return (pkt->head.sta_flags >> 0) & 1; |
| 127 | } |
| 128 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 129 | #if 0 |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 130 | void |
| 131 | bfd_pkt_set_multipoint (bfd_pkt_t * pkt) |
| 132 | { |
| 133 | pkt->head.sta_flags |= 1 << 0; |
| 134 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 135 | #endif |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 136 | |
| 137 | u32 |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 138 | bfd_max_key_len_for_auth_type (bfd_auth_type_e auth_type) |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 139 | { |
| 140 | #define F(t, l, n, s) \ |
| 141 | if (auth_type == t) \ |
| 142 | { \ |
| 143 | return l; \ |
| 144 | } |
| 145 | foreach_bfd_auth_type (F); |
| 146 | #undef F |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | const char * |
| 151 | bfd_auth_type_str (bfd_auth_type_e auth_type) |
| 152 | { |
| 153 | #define F(t, l, n, s) \ |
| 154 | if (auth_type == t) \ |
| 155 | { \ |
| 156 | return s; \ |
| 157 | } |
| 158 | foreach_bfd_auth_type (F); |
| 159 | #undef F |
| 160 | return "UNKNOWN"; |
| 161 | } |
| 162 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 163 | const char * |
| 164 | bfd_diag_code_string (bfd_diag_code_e diag) |
| 165 | { |
| 166 | #define F(n, t, s) \ |
| 167 | case BFD_DIAG_CODE_NAME (t): \ |
| 168 | return s; |
| 169 | switch (diag) |
| 170 | { |
| 171 | foreach_bfd_diag_code (F)} |
| 172 | return "UNKNOWN"; |
| 173 | #undef F |
| 174 | } |
| 175 | |
| 176 | const char * |
| 177 | bfd_state_string (bfd_state_e state) |
| 178 | { |
| 179 | #define F(n, t, s) \ |
| 180 | case BFD_STATE_NAME (t): \ |
| 181 | return s; |
| 182 | switch (state) |
| 183 | { |
| 184 | foreach_bfd_state (F)} |
| 185 | return "UNKNOWN"; |
| 186 | #undef F |
| 187 | } |
| 188 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 189 | /* |
| 190 | * fd.io coding-style-patch-verification: ON |
| 191 | * |
| 192 | * Local Variables: |
| 193 | * eval: (c-set-style "gnu") |
| 194 | * End: |
| 195 | */ |