LCOV - code coverage report
Current view: top level - vnet/syslog - syslog_api.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 44 88 50.0 %
Date: 2023-07-05 22:20:52 Functions: 9 9 100.0 %

          Line data    Source code
       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             : #include <vnet/fib/fib_table.h>
      22             : #include <vnet/syslog/syslog.h>
      23             : 
      24             : #include <vnet/format_fns.h>
      25             : #include <vnet/syslog/syslog.api_enum.h>
      26             : #include <vnet/syslog/syslog.api_types.h>
      27             : 
      28             : #define REPLY_MSG_ID_BASE syslog_main.msg_id_base
      29             : #include <vlibapi/api_helper_macros.h>
      30             : 
      31             : static int
      32           5 : syslog_severity_decode (vl_api_syslog_severity_t v, syslog_severity_t * s)
      33             : {
      34           5 :   v = ntohl (v);
      35           5 :   int rv = 0;
      36             : 
      37           5 :   switch (v)
      38             :     {
      39           0 :     case SYSLOG_API_SEVERITY_EMERG:
      40           0 :       *s = SYSLOG_SEVERITY_EMERGENCY;
      41           0 :       break;
      42           0 :     case SYSLOG_API_SEVERITY_ALERT:
      43           0 :       *s = SYSLOG_SEVERITY_ALERT;
      44           0 :       break;
      45           0 :     case SYSLOG_API_SEVERITY_CRIT:
      46           0 :       *s = SYSLOG_SEVERITY_CRITICAL;
      47           0 :       break;
      48           0 :     case SYSLOG_API_SEVERITY_ERR:
      49           0 :       *s = SYSLOG_SEVERITY_ERROR;
      50           0 :       break;
      51           1 :     case SYSLOG_API_SEVERITY_WARN:
      52           1 :       *s = SYSLOG_SEVERITY_WARNING;
      53           1 :       break;
      54           0 :     case SYSLOG_API_SEVERITY_NOTICE:
      55           0 :       *s = SYSLOG_SEVERITY_NOTICE;
      56           0 :       break;
      57           4 :     case SYSLOG_API_SEVERITY_INFO:
      58           4 :       *s = SYSLOG_SEVERITY_INFORMATIONAL;
      59           4 :       break;
      60           0 :     case SYSLOG_API_SEVERITY_DBG:
      61           0 :       *s = SYSLOG_SEVERITY_DEBUG;
      62           0 :       break;
      63           0 :     default:
      64           0 :       rv = VNET_API_ERROR_INVALID_VALUE;
      65             :     }
      66             : 
      67           5 :   return rv;
      68             : }
      69             : 
      70             : static int
      71           1 : syslog_severity_encode (syslog_severity_t v, vl_api_syslog_severity_t * s)
      72             : {
      73           1 :   int rv = 0;
      74           1 :   switch (v)
      75             :     {
      76           0 :     case SYSLOG_SEVERITY_EMERGENCY:
      77           0 :       *s = SYSLOG_API_SEVERITY_EMERG;
      78           0 :       break;
      79           0 :     case SYSLOG_SEVERITY_ALERT:
      80           0 :       *s = SYSLOG_API_SEVERITY_ALERT;
      81           0 :       break;
      82           0 :     case SYSLOG_SEVERITY_CRITICAL:
      83           0 :       *s = SYSLOG_API_SEVERITY_CRIT;
      84           0 :       break;
      85           0 :     case SYSLOG_SEVERITY_ERROR:
      86           0 :       *s = SYSLOG_API_SEVERITY_ERR;
      87           0 :       break;
      88           1 :     case SYSLOG_SEVERITY_WARNING:
      89           1 :       *s = SYSLOG_API_SEVERITY_WARN;
      90           1 :       break;
      91           0 :     case SYSLOG_SEVERITY_NOTICE:
      92           0 :       *s = SYSLOG_API_SEVERITY_NOTICE;
      93           0 :       break;
      94           0 :     case SYSLOG_SEVERITY_INFORMATIONAL:
      95           0 :       *s = SYSLOG_API_SEVERITY_INFO;
      96           0 :       break;
      97           0 :     case SYSLOG_SEVERITY_DEBUG:
      98           0 :       *s = SYSLOG_API_SEVERITY_DBG;
      99           0 :       break;
     100           0 :     default:
     101           0 :       rv = VNET_API_ERROR_INVALID_VALUE;
     102             :     }
     103             : 
     104           1 :   *s = htonl (*s);
     105           1 :   return rv;
     106             : }
     107             : 
     108             : static void
     109           7 : 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           7 :   clib_memcpy (&collector, &mp->collector_address, sizeof (collector));
     115           7 :   clib_memcpy (&src, &mp->src_address, sizeof (src));
     116             : 
     117           7 :   int rv = set_syslog_sender (&collector, ntohs (mp->collector_port), &src,
     118             :                               ntohl (mp->vrf_id), ntohl (mp->max_msg_size));
     119             : 
     120           7 :   REPLY_MACRO (VL_API_SYSLOG_SET_SENDER_REPLY);
     121             : }
     122             : 
     123             : static void
     124           2 : vl_api_syslog_get_sender_t_handler (vl_api_syslog_get_sender_t * mp)
     125             : {
     126           2 :   int rv = 0;
     127             :   vl_api_syslog_get_sender_reply_t *rmp;
     128           2 :   syslog_main_t *sm = &syslog_main;
     129             :   u32 vrf_id;
     130             : 
     131             :   /* *INDENT-OFF* */
     132           2 :   REPLY_MACRO2 (VL_API_SYSLOG_GET_SENDER_REPLY,
     133             :   ({
     134             :     clib_memcpy (&rmp->collector_address, &(sm->collector),
     135             :                  sizeof(ip4_address_t));
     136             :     clib_memcpy (&rmp->src_address, &(sm->src_address),
     137             :                  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             : 
     149             : static void
     150           5 : vl_api_syslog_set_filter_t_handler (vl_api_syslog_set_filter_t * mp)
     151             : {
     152             :   vl_api_syslog_set_filter_reply_t *rmp;
     153           5 :   syslog_main_t *sm = &syslog_main;
     154           5 :   int rv = 0;
     155             :   syslog_severity_t s;
     156             : 
     157           5 :   rv = syslog_severity_decode (mp->severity, &s);
     158           5 :   if (rv)
     159           0 :     goto send_reply;
     160             : 
     161           5 :   sm->severity_filter = s;
     162             : 
     163           5 : send_reply:
     164           5 :   REPLY_MACRO (VL_API_SYSLOG_SET_FILTER_REPLY);
     165             : }
     166             : 
     167             : static void
     168           1 : vl_api_syslog_get_filter_t_handler (vl_api_syslog_get_filter_t * mp)
     169             : {
     170           1 :   int rv = 0;
     171             :   vl_api_syslog_get_filter_reply_t *rmp;
     172           1 :   syslog_main_t *sm = &syslog_main;
     173             : 
     174             :   /* *INDENT-OFF* */
     175           1 :   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             : 
     182             : #include <vnet/syslog/syslog.api.c>
     183             : 
     184             : static clib_error_t *
     185         559 : syslog_api_hookup (vlib_main_t * vm)
     186             : {
     187             :   /*
     188             :    * Set up the (msg_name, crc, message-id) table
     189             :    */
     190         559 :   REPLY_MSG_ID_BASE = setup_message_id_table ();
     191             : 
     192         559 :   return 0;
     193             : }
     194             : 
     195       19599 : VLIB_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             :  */

Generated by: LCOV version 1.14