blob: 21e79c6e2bd2b57a419338d0a302dbe5fd037899 [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
131 /* *INDENT-OFF* */
132 REPLY_MACRO2 (VL_API_SYSLOG_GET_SENDER_REPLY,
133 ({
Paul Vinciguerra21bb9af2018-12-10 14:50:13 -0800134 clib_memcpy (&rmp->collector_address, &(sm->collector),
Matus Fabianb4515b42018-11-19 04:25:32 -0800135 sizeof(ip4_address_t));
Paul Vinciguerra21bb9af2018-12-10 14:50:13 -0800136 clib_memcpy (&rmp->src_address, &(sm->src_address),
Matus Fabianb4515b42018-11-19 04:25:32 -0800137 sizeof(ip4_address_t));
138 rmp->collector_port = htons (sm->collector_port);
139 if (sm->fib_index == ~0)
140 vrf_id = ~0;
141 else
142 vrf_id = htonl (fib_table_get_table_id (sm->fib_index, FIB_PROTOCOL_IP4));
143 rmp->vrf_id = vrf_id;
144 rmp->max_msg_size = htonl (sm->max_msg_size);
145 }))
146 /* *INDENT-ON* */
147}
148
149static void
150vl_api_syslog_set_filter_t_handler (vl_api_syslog_set_filter_t * mp)
151{
152 vl_api_syslog_set_filter_reply_t *rmp;
153 syslog_main_t *sm = &syslog_main;
154 int rv = 0;
155 syslog_severity_t s;
156
157 rv = syslog_severity_decode (mp->severity, &s);
158 if (rv)
159 goto send_reply;
160
161 sm->severity_filter = s;
162
163send_reply:
164 REPLY_MACRO (VL_API_SYSLOG_SET_FILTER_REPLY);
165}
166
167static void
168vl_api_syslog_get_filter_t_handler (vl_api_syslog_get_filter_t * mp)
169{
170 int rv = 0;
171 vl_api_syslog_get_filter_reply_t *rmp;
172 syslog_main_t *sm = &syslog_main;
173
174 /* *INDENT-OFF* */
175 REPLY_MACRO2 (VL_API_SYSLOG_GET_FILTER_REPLY,
176 ({
177 rv = syslog_severity_encode (sm->severity_filter, &rmp->severity);
178 }))
179 /* *INDENT-ON* */
180}
181
Filip Tehlar667bc422021-06-22 22:33:02 +0000182#include <vnet/syslog/syslog.api.c>
Matus Fabianb4515b42018-11-19 04:25:32 -0800183
184static clib_error_t *
185syslog_api_hookup (vlib_main_t * vm)
186{
Matus Fabianb4515b42018-11-19 04:25:32 -0800187 /*
188 * Set up the (msg_name, crc, message-id) table
189 */
Filip Tehlar667bc422021-06-22 22:33:02 +0000190 REPLY_MSG_ID_BASE = setup_message_id_table ();
Matus Fabianb4515b42018-11-19 04:25:32 -0800191
192 return 0;
193}
194
195VLIB_API_INIT_FUNCTION (syslog_api_hookup);
196
197/*
198 * fd.io coding-style-patch-verification: ON
199 *
200 * Local Variables:
201 * eval: (c-set-style "gnu")
202 * End:
203 */