blob: 195a6e52eef6703d59425c8f7c48b1d490e72d91 [file] [log] [blame]
Matus Fabianb4515b42018-11-19 04:25:32 -08001/*
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 Fabianb4515b42018-11-19 04:25:32 -080021#include <vnet/fib/fib_table.h>
22#include <vnet/syslog/syslog.h>
23
Filip Tehlar667bc422021-06-22 22:33:02 +000024#include <vnet/format_fns.h>
25#include <vnet/syslog/syslog.api_enum.h>
26#include <vnet/syslog/syslog.api_types.h>
Matus Fabianb4515b42018-11-19 04:25:32 -080027
Filip Tehlar667bc422021-06-22 22:33:02 +000028#define REPLY_MSG_ID_BASE syslog_main.msg_id_base
Matus Fabianb4515b42018-11-19 04:25:32 -080029#include <vlibapi/api_helper_macros.h>
30
Matus Fabianb4515b42018-11-19 04:25:32 -080031static int
32syslog_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
70static int
71syslog_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
108static void
109vl_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
123static void
124vl_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 Fabianb4515b42018-11-19 04:25:32 -0800131 REPLY_MACRO2 (VL_API_SYSLOG_GET_SENDER_REPLY,
132 ({
Paul Vinciguerra21bb9af2018-12-10 14:50:13 -0800133 clib_memcpy (&rmp->collector_address, &(sm->collector),
Matus Fabianb4515b42018-11-19 04:25:32 -0800134 sizeof(ip4_address_t));
Paul Vinciguerra21bb9af2018-12-10 14:50:13 -0800135 clib_memcpy (&rmp->src_address, &(sm->src_address),
Matus Fabianb4515b42018-11-19 04:25:32 -0800136 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 Fabianb4515b42018-11-19 04:25:32 -0800145}
146
147static void
148vl_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
161send_reply:
162 REPLY_MACRO (VL_API_SYSLOG_SET_FILTER_REPLY);
163}
164
165static void
166vl_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 Fabianb4515b42018-11-19 04:25:32 -0800172 REPLY_MACRO2 (VL_API_SYSLOG_GET_FILTER_REPLY,
173 ({
174 rv = syslog_severity_encode (sm->severity_filter, &rmp->severity);
175 }))
Matus Fabianb4515b42018-11-19 04:25:32 -0800176}
177
Filip Tehlar667bc422021-06-22 22:33:02 +0000178#include <vnet/syslog/syslog.api.c>
Matus Fabianb4515b42018-11-19 04:25:32 -0800179
180static clib_error_t *
181syslog_api_hookup (vlib_main_t * vm)
182{
Matus Fabianb4515b42018-11-19 04:25:32 -0800183 /*
184 * Set up the (msg_name, crc, message-id) table
185 */
Filip Tehlar667bc422021-06-22 22:33:02 +0000186 REPLY_MSG_ID_BASE = setup_message_id_table ();
Matus Fabianb4515b42018-11-19 04:25:32 -0800187
188 return 0;
189}
190
191VLIB_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 */