Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | /* |
| 16 | * srp_format.c: srp formatting/parsing. |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vnet/srp/srp.h> |
| 42 | #include <vnet/ethernet/ethernet.h> |
| 43 | |
| 44 | static u8 * format_srp_mode (u8 * s, va_list * args) |
| 45 | { |
| 46 | u32 mode = va_arg (*args, u32); |
| 47 | char * t = 0; |
| 48 | switch (mode) |
| 49 | { |
| 50 | #define _(f) case SRP_MODE_##f: t = #f; break; |
| 51 | foreach_srp_mode |
| 52 | #undef _ |
| 53 | default: t = 0; break; |
| 54 | } |
| 55 | if (t) |
| 56 | s = format (s, "%s", t); |
| 57 | else |
| 58 | s = format (s, "unknown 0x%x", mode); |
| 59 | |
| 60 | return s; |
| 61 | } |
| 62 | |
| 63 | u8 * format_srp_header_with_length (u8 * s, va_list * args) |
| 64 | { |
| 65 | srp_and_ethernet_header_t * h = va_arg (*args, srp_and_ethernet_header_t *); |
| 66 | u32 max_header_bytes = va_arg (*args, u32); |
| 67 | ethernet_main_t * em = ðernet_main; |
| 68 | uword indent, header_bytes; |
| 69 | |
| 70 | header_bytes = sizeof (h[0]); |
| 71 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 72 | return format (s, "srp header truncated"); |
| 73 | |
| 74 | indent = format_get_indent (s); |
| 75 | |
| 76 | s = format (s, "mode %U, ring %s, priority %d, ttl %d", |
| 77 | format_srp_mode, h->srp.mode, |
| 78 | h->srp.is_inner_ring ? "inner" : "outer", |
| 79 | h->srp.priority, h->srp.ttl); |
| 80 | |
| 81 | s = format (s, "\n%U%U: %U -> %U", |
| 82 | format_white_space, indent, |
| 83 | format_ethernet_type, clib_net_to_host_u16 (h->ethernet.type), |
| 84 | format_ethernet_address, h->ethernet.src_address, |
| 85 | format_ethernet_address, h->ethernet.dst_address); |
| 86 | |
| 87 | if (max_header_bytes != 0 && header_bytes < max_header_bytes) |
| 88 | { |
| 89 | ethernet_type_info_t * ti; |
| 90 | vlib_node_t * node; |
| 91 | |
| 92 | ti = ethernet_get_type_info (em, h->ethernet.type); |
| 93 | node = ti ? vlib_get_node (em->vlib_main, ti->node_index) : 0; |
| 94 | if (node && node->format_buffer) |
| 95 | s = format (s, "\n%U%U", |
| 96 | format_white_space, indent, |
| 97 | node->format_buffer, (void *) h + header_bytes, |
| 98 | max_header_bytes - header_bytes); |
| 99 | } |
| 100 | |
| 101 | return s; |
| 102 | } |
| 103 | |
| 104 | u8 * format_srp_header (u8 * s, va_list * args) |
| 105 | { |
| 106 | srp_header_t * m = va_arg (*args, srp_header_t *); |
| 107 | return format (s, "%U", format_srp_header_with_length, m, 0); |
| 108 | } |
| 109 | |
| 110 | uword |
| 111 | unformat_srp_header (unformat_input_t * input, va_list * args) |
| 112 | { |
| 113 | u8 ** result = va_arg (*args, u8 **); |
| 114 | srp_and_ethernet_header_t * h; |
| 115 | |
| 116 | { |
| 117 | void * p; |
| 118 | vec_add2 (*result, p, sizeof (h[0])); |
| 119 | h = p; |
| 120 | } |
| 121 | |
| 122 | if (! unformat (input, "%U: %U -> %U", |
| 123 | unformat_ethernet_type_net_byte_order, &h->ethernet.type, |
| 124 | unformat_ethernet_address, &h->ethernet.src_address, |
| 125 | unformat_ethernet_address, &h->ethernet.dst_address)) |
| 126 | return 0; |
| 127 | |
| 128 | h->srp.mode = SRP_MODE_data; |
| 129 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 130 | { |
| 131 | u32 x; |
| 132 | |
| 133 | if (unformat (input, "control")) |
| 134 | h->srp.mode = SRP_MODE_control_pass_to_host; |
| 135 | |
| 136 | else if (unformat (input, "pri %d", &x)) |
| 137 | h->srp.priority = x; |
| 138 | |
| 139 | else if (unformat (input, "ttl %d", &x)) |
| 140 | h->srp.ttl = x; |
| 141 | |
| 142 | else |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | return 1; |
| 147 | } |