/*
*------------------------------------------------------------------
* memif_api.c - memif api
*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*------------------------------------------------------------------
*/
#include <vlib/vlib.h>
#include <vnet/ethernet/ethernet.h>
#include <vlib/unix/unix.h>
#include <memif/memif.h>
#include <memif/private.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vnet/ip/ip_types_api.h>
#include <vnet/ethernet/ethernet_types_api.h>
/* define message IDs */
#include <vnet/format_fns.h>
#include <memif/memif.api_enum.h>
#include <memif/memif.api_types.h>
#define REPLY_MSG_ID_BASE mm->msg_id_base
#include <vlibapi/api_helper_macros.h>
/**
* @brief Message handler for memif_socket_filename_add_del API.
* @param mp the vl_api_memif_socket_filename_add_del_t API message
*/
void
vl_api_memif_socket_filename_add_del_t_handler
(vl_api_memif_socket_filename_add_del_t * mp)
{
memif_main_t *mm = &memif_main;
u8 is_add;
u32 socket_id;
u32 len;
u8 *socket_filename;
vl_api_memif_socket_filename_add_del_reply_t *rmp;
int rv;
/* is_add */
is_add = mp->is_add;
/* socket_id */
socket_id = clib_net_to_host_u32 (mp->socket_id);
if (socket_id == 0 || socket_id == ~0)
{
rv = VNET_API_ERROR_INVALID_ARGUMENT;
goto reply;
}
/* socket filename */
socket_filename = 0;
mp->socket_filename[ARRAY_LEN (mp->socket_filename) - 1] = 0;
len = strlen ((char *) mp->socket_filename);
if (mp->is_add)
{
vec_validate (socket_filename, len);
memcpy (socket_filename, mp->socket_filename, len);
}
rv = memif_socket_filename_add_del (is_add, socket_id, socket_filename);
vec_free (socket_filename);
reply:
REPLY_MACRO (VL_API_MEMIF_SOCKET_FILENAME_ADD_DEL_REPLY);
}
/**
* @brief Message handler for memif_create API.
* @param mp vl_api_memif_create_t * mp the api message
*/
void
vl_api_memif_create_t_handler (vl_api_memif_create_t * mp)
{
memif_main_t *mm = &memif_main;
vlib_main_t *vm = vlib_get_main ();
vl_api_memif_create_reply_t *rmp;
memif_create_if_args_t args = { 0 };
u32 ring_size = MEMIF_DEFAULT_RING_SIZE;
static const u8 empty_hw_addr[6];
int rv = 0;
mac_address_t mac;
/* id */
args.id = clib_net_to_host_u32 (mp->id);
/* socket-id */
args.socket_id = clib_net_to_host_u32 (mp->socket_id);
/* secret */
mp->secret[ARRAY_LEN (mp->secret) - 1] = 0;
if (strlen ((char *) mp->secret) > 0)
{
vec_validate (args.secret, strlen ((char *) mp->secret));
strncpy ((char *) args.secret, (char *) mp->secret,
vec_len (args.secret));
}
/* role */
args.is_master = (ntohl (mp->role) == MEMIF_ROLE_API_MASTER);
/* mode */
args.mode = ntohl (mp->mode);
args.is_zero_copy = mp->no_zero_copy ? 0 : 1;
/* rx/tx queues */
if (args.is_master == 0)
{
args.rx_queues = MEMIF_DEFAULT_RX_QUEUES;
args.tx_queues = MEMIF_DEFAULT_TX_QUEUES;
if (mp->rx_queues)
{
args.rx_queues = mp->rx_queues;
}
if (mp->tx_queues)
{
args.tx_queues = mp->tx_queues;
}
}
/* ring size */
if (mp->ring_size)
{
ring_size = ntohl (mp->ring_size);
}
if (!is_pow2 (ring_size))
{
rv = VNET_API_ERROR_INVALID_ARGUMENT;
goto reply;