syslog_api.c revision 39d69112
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>
21
22#include <vnet/fib/fib_table.h>
23#include <vnet/syslog/syslog.h>
24
25#include <vnet/vnet_msg_enum.h>
26
27#define vl_typedefs		/* define message structures */
28#include <vnet/vnet_all_api_h.h>
29#undef vl_typedefs
30
31#define vl_endianfun		/* define message structures */
32#include <vnet/vnet_all_api_h.h>
33#undef vl_endianfun
34
35/* instantiate all the print functions we know about */
36#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37#define vl_printfun
38#include <vnet/vnet_all_api_h.h>
39#undef vl_printfun
40
41#include <vlibapi/api_helper_macros.h>
42
43#define foreach_vpe_api_msg             \
44_(SYSLOG_SET_SENDER, syslog_set_sender) \
45_(SYSLOG_GET_SENDER, syslog_get_sender) \
46_(SYSLOG_SET_FILTER, syslog_set_filter) \
47_(SYSLOG_GET_FILTER, syslog_get_filter)
48
49static int
50syslog_severity_decode (vl_api_syslog_severity_t v, syslog_severity_t * s)
51{
52  v = ntohl (v);
53  int rv = 0;
54
55  switch (v)
56    {
57    case SYSLOG_API_SEVERITY_EMERG:
58      *s = SYSLOG_SEVERITY_EMERGENCY;
59      break;
60    case SYSLOG_API_SEVERITY_ALERT:
61      *s = SYSLOG_SEVERITY_ALERT;
62      break;
63    case SYSLOG_API_SEVERITY_CRIT:
64      *s = SYSLOG_SEVERITY_CRITICAL;
65      break;
66    case SYSLOG_API_SEVERITY_ERR:
67      *s = SYSLOG_SEVERITY_ERROR;
68      break;
69    case SYSLOG_API_SEVERITY_WARN:
70      *s = SYSLOG_SEVERITY_WARNING;
71      break;
72    case SYSLOG_API_SEVERITY_NOTICE:
73      *s = SYSLOG_SEVERITY_NOTICE;
74      break;
75    case SYSLOG_API_SEVERITY_INFO:
76      *s = SYSLOG_SEVERITY_INFORMATIONAL;
77      break;
78    case SYSLOG_API_SEVERITY_DBG:
79      *s = SYSLOG_SEVERITY_DEBUG;
80      break;
81    default:
82      rv = VNET_API_ERROR_INVALID_VALUE;
83    }
84
85  return rv;
86}
87
88static int
89syslog_severity_encode (syslog_severity_t v, vl_api_syslog_severity_t * s)
90{
91  int rv = 0;
92  switch (v)
93    {
94    case SYSLOG_SEVERITY_EMERGENCY:
95      *s = SYSLOG_API_SEVERITY_EMERG;
96      break;
97    case SYSLOG_SEVERITY_ALERT:
98      *s = SYSLOG_API_SEVERITY_ALERT;
99      break;
100    case SYSLOG_SEVERITY_CRITICAL:
101      *s = SYSLOG_API_SEVERITY_CRIT;
102      break;
103    case SYSLOG_SEVERITY_ERROR:
104      *s = SYSLOG_API_SEVERITY_ERR;
105      break;
106    case SYSLOG_SEVERITY_WARNING:
107      *s = SYSLOG_API_SEVERITY_WARN;
108      break;
109    case SYSLOG_SEVERITY_NOTICE:
110      *s = SYSLOG_API_SEVERITY_NOTICE;
111      break;
112    case SYSLOG_SEVERITY_INFORMATIONAL:
113      *s = SYSLOG_API_SEVERITY_INFO;
114      break;
115    case SYSLOG_SEVERITY_DEBUG:
116      *s = SYSLOG_API_SEVERITY_DBG;
117      break;
118    default:
119      rv = VNET_API_ERROR_INVALID_VALUE;
120    }
121
122  *s = htonl (*s);
123  return rv;
124}
125
126static void
127vl_api_syslog_set_sender_t_handler (vl_api_syslog_set_sender_t * mp)
128{
129  vl_api_syslog_set_sender_reply_t *rmp;
130  ip4_address_t collector, src;
131
132  clib_memcpy (&collector, &mp->collector_address, sizeof (collector));
133  clib_memcpy (&src, &mp->src_address, sizeof (src));
134
135  int rv = set_syslog_sender (&collector, ntohs (mp->collector_port), &src,
136			      ntohl (mp->vrf_id), ntohl (mp->max_msg_size));
137
138  REPLY_MACRO (VL_API_SYSLOG_SET_SENDER_REPLY);
139}
140
141static void
142vl_api_syslog_get_sender_t_handler (vl_api_syslog_get_sender_t * mp)
143{
144  int rv = 0;
145  vl_api_syslog_get_sender_reply_t *rmp;
146  syslog_main_t *sm = &syslog_main;
147  u32 vrf_id;
148
149  /* *INDENT-OFF* */
150  REPLY_MACRO2 (VL_API_SYSLOG_GET_SENDER_REPLY,
151  ({
152    clib_memcpy (&rmp->collector_address, &(sm->collector),
153                 sizeof(ip4_address_t));
154    clib_memcpy (&rmp->src_address, &(sm->src_address),
155                 sizeof(ip4_address_t));
156    rmp->collector_port = htons (sm->collector_port);
157    if (sm->fib_index == ~0)
158      vrf_id = ~0;
159    else
160      vrf_id = htonl (fib_table_get_table_id (sm->fib_index, FIB_PROTOCOL_IP4));
161    rmp->vrf_id = vrf_id;
162    rmp->max_msg_size = htonl (sm->max_msg_size);
163  }))
164  /* *INDENT-ON* */
165}
166
167static void
168vl_api_syslog_set_filter_t_handler (vl_api_syslog_set_filter_t * mp)
169{
170  vl_api_syslog_set_filter_reply_t *rmp;
171  syslog_main_t *sm = &syslog_main;
172  int rv = 0;
173  syslog_severity_t s;
174
175  rv = syslog_severity_decode (mp->severity, &s);
176  if (rv)
177    goto send_reply;
178
179  sm->severity_filter = s;
180
181send_reply:
182  REPLY_MACRO (VL_API_SYSLOG_SET_FILTER_REPLY);
183}
184
185static void
186vl_api_syslog_get_filter_t_handler (vl_api_syslog_get_filter_t * mp)
187{
188  int rv = 0;
189  vl_api_syslog_get_filter_reply_t *rmp;
190  syslog_main_t *sm = &syslog_main;
191
192  /* *INDENT-OFF* */
193  REPLY_MACRO2 (VL_API_SYSLOG_GET_FILTER_REPLY,
194  ({
195     rv = syslog_severity_encode (sm->severity_filter, &rmp->severity);
196  }))
197  /* *INDENT-ON* */
198}
199
200#define vl_msg_name_crc_list
201#include <vnet/vnet_all_api_h.h>
202#undef vl_msg_name_crc_list
203
204static void
205setup_message_id_table (api_main_t * am)
206{
207#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
208  foreach_vl_msg_name_crc_syslog;
209#undef _
210}
211
212static clib_error_t *
213syslog_api_hookup (vlib_main_t * vm)
214{
215  api_main_t *am = vlibapi_get_main ();
216
217#define _(N,n)                                                  \
218    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
219                           vl_api_##n##_t_handler,              \
220                           vl_noop_handler,                     \
221                           vl_api_##n##_t_endian,               \
222                           vl_api_##n##_t_print,                \
223                           sizeof(vl_api_##n##_t), 1);
224  foreach_vpe_api_msg;
225#undef _
226
227  /*
228   * Set up the (msg_name, crc, message-id) table
229   */
230  setup_message_id_table (am);
231
232  return 0;
233}
234
235VLIB_API_INIT_FUNCTION (syslog_api_hookup);
236
237/*
238 * fd.io coding-style-patch-verification: ON
239 *
240 * Local Variables:
241 * eval: (c-set-style "gnu")
242 * End:
243 */
244