Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #include <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | |
| 19 | #include <vnet/interface.h> |
| 20 | #include <vnet/api_errno.h> |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 21 | #include <vnet/fib/fib_table.h> |
| 22 | #include <vnet/syslog/syslog.h> |
| 23 | |
Filip Tehlar | 667bc42 | 2021-06-22 22:33:02 +0000 | [diff] [blame] | 24 | #include <vnet/format_fns.h> |
| 25 | #include <vnet/syslog/syslog.api_enum.h> |
| 26 | #include <vnet/syslog/syslog.api_types.h> |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 27 | |
Filip Tehlar | 667bc42 | 2021-06-22 22:33:02 +0000 | [diff] [blame] | 28 | #define REPLY_MSG_ID_BASE syslog_main.msg_id_base |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 29 | #include <vlibapi/api_helper_macros.h> |
| 30 | |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 31 | static int |
| 32 | syslog_severity_decode (vl_api_syslog_severity_t v, syslog_severity_t * s) |
| 33 | { |
| 34 | v = ntohl (v); |
| 35 | int rv = 0; |
| 36 | |
| 37 | switch (v) |
| 38 | { |
| 39 | case SYSLOG_API_SEVERITY_EMERG: |
| 40 | *s = SYSLOG_SEVERITY_EMERGENCY; |
| 41 | break; |
| 42 | case SYSLOG_API_SEVERITY_ALERT: |
| 43 | *s = SYSLOG_SEVERITY_ALERT; |
| 44 | break; |
| 45 | case SYSLOG_API_SEVERITY_CRIT: |
| 46 | *s = SYSLOG_SEVERITY_CRITICAL; |
| 47 | break; |
| 48 | case SYSLOG_API_SEVERITY_ERR: |
| 49 | *s = SYSLOG_SEVERITY_ERROR; |
| 50 | break; |
| 51 | case SYSLOG_API_SEVERITY_WARN: |
| 52 | *s = SYSLOG_SEVERITY_WARNING; |
| 53 | break; |
| 54 | case SYSLOG_API_SEVERITY_NOTICE: |
| 55 | *s = SYSLOG_SEVERITY_NOTICE; |
| 56 | break; |
| 57 | case SYSLOG_API_SEVERITY_INFO: |
| 58 | *s = SYSLOG_SEVERITY_INFORMATIONAL; |
| 59 | break; |
| 60 | case SYSLOG_API_SEVERITY_DBG: |
| 61 | *s = SYSLOG_SEVERITY_DEBUG; |
| 62 | break; |
| 63 | default: |
| 64 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 65 | } |
| 66 | |
| 67 | return rv; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | syslog_severity_encode (syslog_severity_t v, vl_api_syslog_severity_t * s) |
| 72 | { |
| 73 | int rv = 0; |
| 74 | switch (v) |
| 75 | { |
| 76 | case SYSLOG_SEVERITY_EMERGENCY: |
| 77 | *s = SYSLOG_API_SEVERITY_EMERG; |
| 78 | break; |
| 79 | case SYSLOG_SEVERITY_ALERT: |
| 80 | *s = SYSLOG_API_SEVERITY_ALERT; |
| 81 | break; |
| 82 | case SYSLOG_SEVERITY_CRITICAL: |
| 83 | *s = SYSLOG_API_SEVERITY_CRIT; |
| 84 | break; |
| 85 | case SYSLOG_SEVERITY_ERROR: |
| 86 | *s = SYSLOG_API_SEVERITY_ERR; |
| 87 | break; |
| 88 | case SYSLOG_SEVERITY_WARNING: |
| 89 | *s = SYSLOG_API_SEVERITY_WARN; |
| 90 | break; |
| 91 | case SYSLOG_SEVERITY_NOTICE: |
| 92 | *s = SYSLOG_API_SEVERITY_NOTICE; |
| 93 | break; |
| 94 | case SYSLOG_SEVERITY_INFORMATIONAL: |
| 95 | *s = SYSLOG_API_SEVERITY_INFO; |
| 96 | break; |
| 97 | case SYSLOG_SEVERITY_DEBUG: |
| 98 | *s = SYSLOG_API_SEVERITY_DBG; |
| 99 | break; |
| 100 | default: |
| 101 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 102 | } |
| 103 | |
| 104 | *s = htonl (*s); |
| 105 | return rv; |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | vl_api_syslog_set_sender_t_handler (vl_api_syslog_set_sender_t * mp) |
| 110 | { |
| 111 | vl_api_syslog_set_sender_reply_t *rmp; |
| 112 | ip4_address_t collector, src; |
| 113 | |
| 114 | clib_memcpy (&collector, &mp->collector_address, sizeof (collector)); |
| 115 | clib_memcpy (&src, &mp->src_address, sizeof (src)); |
| 116 | |
| 117 | int rv = set_syslog_sender (&collector, ntohs (mp->collector_port), &src, |
| 118 | ntohl (mp->vrf_id), ntohl (mp->max_msg_size)); |
| 119 | |
| 120 | REPLY_MACRO (VL_API_SYSLOG_SET_SENDER_REPLY); |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | vl_api_syslog_get_sender_t_handler (vl_api_syslog_get_sender_t * mp) |
| 125 | { |
| 126 | int rv = 0; |
| 127 | vl_api_syslog_get_sender_reply_t *rmp; |
| 128 | syslog_main_t *sm = &syslog_main; |
| 129 | u32 vrf_id; |
| 130 | |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 131 | REPLY_MACRO2 (VL_API_SYSLOG_GET_SENDER_REPLY, |
| 132 | ({ |
Paul Vinciguerra | 21bb9af | 2018-12-10 14:50:13 -0800 | [diff] [blame] | 133 | clib_memcpy (&rmp->collector_address, &(sm->collector), |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 134 | sizeof(ip4_address_t)); |
Paul Vinciguerra | 21bb9af | 2018-12-10 14:50:13 -0800 | [diff] [blame] | 135 | clib_memcpy (&rmp->src_address, &(sm->src_address), |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 136 | sizeof(ip4_address_t)); |
| 137 | rmp->collector_port = htons (sm->collector_port); |
| 138 | if (sm->fib_index == ~0) |
| 139 | vrf_id = ~0; |
| 140 | else |
| 141 | vrf_id = htonl (fib_table_get_table_id (sm->fib_index, FIB_PROTOCOL_IP4)); |
| 142 | rmp->vrf_id = vrf_id; |
| 143 | rmp->max_msg_size = htonl (sm->max_msg_size); |
| 144 | })) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void |
| 148 | vl_api_syslog_set_filter_t_handler (vl_api_syslog_set_filter_t * mp) |
| 149 | { |
| 150 | vl_api_syslog_set_filter_reply_t *rmp; |
| 151 | syslog_main_t *sm = &syslog_main; |
| 152 | int rv = 0; |
| 153 | syslog_severity_t s; |
| 154 | |
| 155 | rv = syslog_severity_decode (mp->severity, &s); |
| 156 | if (rv) |
| 157 | goto send_reply; |
| 158 | |
| 159 | sm->severity_filter = s; |
| 160 | |
| 161 | send_reply: |
| 162 | REPLY_MACRO (VL_API_SYSLOG_SET_FILTER_REPLY); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | vl_api_syslog_get_filter_t_handler (vl_api_syslog_get_filter_t * mp) |
| 167 | { |
| 168 | int rv = 0; |
| 169 | vl_api_syslog_get_filter_reply_t *rmp; |
| 170 | syslog_main_t *sm = &syslog_main; |
| 171 | |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 172 | REPLY_MACRO2 (VL_API_SYSLOG_GET_FILTER_REPLY, |
| 173 | ({ |
| 174 | rv = syslog_severity_encode (sm->severity_filter, &rmp->severity); |
| 175 | })) |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Filip Tehlar | 667bc42 | 2021-06-22 22:33:02 +0000 | [diff] [blame] | 178 | #include <vnet/syslog/syslog.api.c> |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 179 | |
| 180 | static clib_error_t * |
| 181 | syslog_api_hookup (vlib_main_t * vm) |
| 182 | { |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 183 | /* |
| 184 | * Set up the (msg_name, crc, message-id) table |
| 185 | */ |
Filip Tehlar | 667bc42 | 2021-06-22 22:33:02 +0000 | [diff] [blame] | 186 | REPLY_MSG_ID_BASE = setup_message_id_table (); |
Matus Fabian | b4515b4 | 2018-11-19 04:25:32 -0800 | [diff] [blame] | 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | VLIB_API_INIT_FUNCTION (syslog_api_hookup); |
| 192 | |
| 193 | /* |
| 194 | * fd.io coding-style-patch-verification: ON |
| 195 | * |
| 196 | * Local Variables: |
| 197 | * eval: (c-set-style "gnu") |
| 198 | * End: |
| 199 | */ |