Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apis to register external logger function. #35

Merged
merged 1 commit into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-present Barefoot Networks, Inc.
Copyright 2016-present Barefoot Networks, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -13,14 +13,22 @@ 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.
*/
#ifndef _SWITCH_LOG_H_
#define _SWITCH_LOG_H_

#include "p4features.h"
#include <switchapi/switch_status.h>

#define SWITCH_API_ERROR printf
#define SWITCH_API_WARN printf
#define SWITCH_API_INFO printf
#define SWITCH_API_VERBOSE printf
#define SWITCH_API_TRACE printf
typedef enum switch_api_log_levels_ {
SWITCH_API_LOG_NONE = 0,
SWITCH_API_LOG_ERROR,
SWITCH_API_LOG_WARN,
SWITCH_API_LOG_INFO,
SWITCH_API_LOG_VERBOSE,
SWITCH_API_LOG_TRACE,
} switch_api_log_level_t;

char * switch_print_error(switch_status_t status);
typedef int (switch_api_log_fn_t)(switch_api_log_level_t level, char *fmt, ...);
void switch_api_log_function_set(switch_api_log_fn_t *log_fn);

#endif /* _SWITCH_LOG_H_ */
2 changes: 1 addition & 1 deletion switchapi/src/switch_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

#include "p4features.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_lag_int.h"
#include "switch_nhop_int.h"
#include "switch_defines.h"
Expand Down
8 changes: 6 additions & 2 deletions switchapi/src/switch_hostif.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
#include "switchapi/switch_nhop.h"
#include "switchapi/switch_mirror.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_hostif_int.h"
#include "switch_packet_int.h"
#include "switch_nhop_int.h"
Expand Down Expand Up @@ -741,6 +741,8 @@ switch_api_hostif_rx_packet_from_hw(switch_packet_header_t *packet_header, char

JLG(temp, switch_hostif_rcode_array, cpu_header->reason_code);
if (!temp) {
SWITCH_API_ERROR("rx_packet w/ un-handled reason_code 0x%x\n",
cpu_header->reason_code);
return SWITCH_STATUS_ITEM_NOT_FOUND;
}

Expand All @@ -749,13 +751,15 @@ switch_api_hostif_rx_packet_from_hw(switch_packet_header_t *packet_header, char
cpu_header->ingress_ifindex);

rcode_info = (switch_hostif_rcode_info_t *) (*(unsigned long *)temp);


#if 0 /* seems un-necessary check and failing for vlan 0 - checking it */
bd_handle = id_to_handle(SWITCH_HANDLE_TYPE_BD, cpu_header->ingress_bd);
bd_info = switch_bd_get(bd_handle);
if (!bd_info) {
SWITCH_API_ERROR("received packet on invalid bd %x", cpu_header->ingress_bd);
return SWITCH_STATUS_INVALID_HANDLE;
}
#endif

if ((rcode_info->rcode_api_info.reason_code == SWITCH_HOSTIF_REASON_CODE_NONE) ||
(rcode_info->rcode_api_info.channel == SWITCH_HOSTIF_CHANNEL_CB)) {
Expand Down
3 changes: 2 additions & 1 deletion switchapi/src/switch_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ limitations under the License.
#include "switch_neighbor_int.h"
#include "switch_lag_int.h"
#include "switch_stp_int.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_port_int.h"
#include "switch_tunnel_int.h"
#include "switch_acl_int.h"
Expand All @@ -52,6 +52,7 @@ static pthread_t stats_thread;
switch_status_t
switch_api_lib_init(switch_device_t device)
{
switch_log_init();
SWITCH_API_TRACE("Initializing switch api!!");
switch_pd_client_init(device);
switch_router_mac_init(device);
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ limitations under the License.
#include "switch_lag_int.h"
#include "switch_pd.h"
#include "switch_hostif_int.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_capability_int.h"

#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_l2.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License.
#include "switch_nhop_int.h"
#include "switch_neighbor_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"

#include <string.h>

Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_l3.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License.
#include "switch_nhop_int.h"
#include "switch_l3_int.h"
#include "switch_hostif_int.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "arpa/inet.h"
#include <string.h>
#include <assert.h>
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_lag.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License.
#include "switchapi/switch_port.h"
#include "switch_lag_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include <string.h>

#ifdef __cplusplus
Expand Down
35 changes: 34 additions & 1 deletion switchapi/src/switch_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ limitations under the License.
*/

#include "p4features.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include <stdarg.h>

switch_api_log_fn_t *switch_api_client_log_fn = NULL;
switch_api_log_level_t switch_api_default_log_level = SWITCH_API_LOG_INFO;

char * switch_print_error(switch_status_t status)
{
Expand All @@ -34,3 +38,32 @@ char * switch_print_error(switch_status_t status)
break;
}
}

int
switch_default_logger(switch_api_log_level_t level, char *fmt, ...)
{
va_list args;

if ((switch_api_default_log_level == SWITCH_API_LOG_NONE) ||
(level > switch_api_default_log_level)) {
return 0;
}
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);

return 1;
}

void
switch_api_log_function_set(switch_api_log_fn_t *log_fn)
{
switch_api_client_log_fn = log_fn;
}

void
switch_log_init()
{
switch_api_default_log_level = SWITCH_API_LOG_INFO;
switch_api_client_log_fn = switch_default_logger;
}
38 changes: 38 additions & 0 deletions switchapi/src/switch_log_int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2013-present Barefoot Networks, Inc.

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.
*/
#ifndef _SWITCH_LOG_INT_H_
#define _SWITCH_LOG_INT_H_

#include "p4features.h"
#include <switchapi/switch_status.h>
#include <switchapi/switch_log.h>

void switch_log_init();

extern switch_api_log_fn_t *switch_api_client_log_fn;

#define SWITCH_API_ERROR(...) \
switch_api_client_log_fn && switch_api_client_log_fn(SWITCH_API_LOG_ERROR, __VA_ARGS__);
#define SWITCH_API_WARN(...) \
switch_api_client_log_fn && switch_api_client_log_fn(SWITCH_API_LOG_WARN, __VA_ARGS__);
#define SWITCH_API_INFO(...) \
switch_api_client_log_fn && switch_api_client_log_fn(SWITCH_API_LOG_INFO, __VA_ARGS__);
#define SWITCH_API_VERBOSE(...) \
switch_api_client_log_fn && switch_api_client_log_fn(SWITCH_API_LOG_VERBOSE, __VA_ARGS__);
#define SWITCH_API_TRACE(...) \
switch_api_client_log_fn && switch_api_client_log_fn(SWITCH_API_LOG_TRACE, __VA_ARGS__);

#endif /*_SWITCH_LOG_INT_H_ */
2 changes: 1 addition & 1 deletion switchapi/src/switch_mcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
#include "switch_lag_int.h"
#include "switch_tunnel_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include <string.h>

#define SWITCH_MGID_TABLE_SIZE 16 * 1024
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
#include "switchapi/switch_status.h"
#include "switch_meter_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"

static void *switch_meter_array = NULL;

Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_mirror.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
#include "switch_mirror_int.h"
#include "switch_nhop_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_capability_int.h"

static void *switch_mirror_array = NULL;
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_neighbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
#include "switch_interface_int.h"
#include "switch_nhop_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_defines.h"
#include <string.h>

Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_nhop.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
#include "switch_neighbor_int.h"
#include "switch_hostif_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"

#ifdef __cplusplus
extern "C" {
Expand Down
21 changes: 18 additions & 3 deletions switchapi/src/switch_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ limitations under the License.
#include "switch_vlan_int.h"
#include "switch_port_int.h"
#include "switch_lag_int.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include <switchapi/switch_status.h>
#include <switchapi/switch_capability.h>
#include "switchapi/switch_utils.h"
Expand Down Expand Up @@ -214,6 +214,8 @@ switch_packet_rx_to_host(
return;
}

SWITCH_API_INFO("Rx packet reason_code 0x%x - send to fd %d, action %d\n",
rx_entry.reason_code, rx_info->intf_fd, rx_info->vlan_action);
if (rx_info->vlan_action == SWITCH_PACKET_VLAN_ADD) {
eth_header = (switch_ethernet_header_t *) packet;
if (ntohs(eth_header->ether_type) != SWITCH_ETHERTYPE_DOT1Q && rx_info->vlan_id) {
Expand Down Expand Up @@ -431,8 +433,8 @@ switch_packet_hostif_create(switch_device_t device, switch_hostif_info_t *hostif
int sock_flags = 0;
char *intf_name = NULL;
void *temp = NULL;
switch_api_capability_t api_switch_info;
switch_mac_addr_t mac;
switch_api_capability_t api_switch_info;
switch_mac_addr_t mac;

switch_api_capability_get(device, &api_switch_info);

Expand Down Expand Up @@ -704,6 +706,10 @@ switch_api_packet_net_filter_tx_create(
tx_info->bypass_flags = tx_action->bypass_flags;
tx_info->port = handle_to_id(tx_action->port_handle);

SWITCH_API_INFO("net_filter_tx_create: hostif 0x%x, vlan_id = %d, fd 0x%x, bypass 0x%x\n",
tx_key->hostif_handle, tx_key->vlan_valid ? tx_key->vlan_id : 0xFFF,
tx_entry.intf_fd, tx_info->bypass_flags);

tommy_list_insert_head(&packet_tx_filter_list, &(tx_info->node), tx_info);
tommy_list_sort(&packet_tx_filter_list, switch_packet_tx_filter_priority_compare);
return status;
Expand Down Expand Up @@ -900,6 +906,15 @@ switch_api_packet_net_filter_rx_create(
rx_info->vlan_action = rx_action->vlan_action;
rx_info->intf_fd = hostif_info->intf_fd;

SWITCH_API_INFO("net_filter_rx_create: port 0x%x, port_lag_hdl = 0x%x, "
"if_bd_hdl 0x%x, rcode 0x%x, rcode_mask 0x%x "
"vlan_id %d, fd %d, action %d\n",
rx_key->port_valid ? rx_key->port_handle : 0,
rx_key->port_lag_valid ? rx_key->port_lag_handle : 0,
rx_key->handle_valid ? rx_key->handle : 0,
rx_key->reason_code_valid ? rx_key->reason_code : 0,
rx_key->reason_code_mask,
rx_info->vlan_id, rx_info->vlan_action, rx_info->intf_fd);
/*
* Adding an element to the list results in sorting the list.
* tommy does not have a way to compare and insert the elements
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

#include "p4features.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include "switch_lag_int.h"
#include "switch_nhop_int.h"
#include "switch_defines.h"
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_rmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
#include "switchapi/switch_utils.h"
#include "switch_pd.h"
#include "switch_rmac_int.h"
#include "switch_log.h"
#include "switch_log_int.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_stp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
#include "switchapi/switch_interface.h"
#include "switch_stp_int.h"
#include "switch_pd.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include <string.h>

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License.
#include "switch_pd.h"
#include "switch_interface_int.h"
#include "switch_tunnel_int.h"
#include "switch_log.h"
#include "switch_log_int.h"
#include <string.h>
#include <netinet/in.h>

Expand Down
2 changes: 1 addition & 1 deletion switchapi/src/switch_vlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ limitations under the License.
#include "switchapi/switch_utils.h"
#include "switch_pd.h"
#include "switch_lag_int.h"
#include "switch_log.h"
#include "switch_log_int.h"

#ifdef __cplusplus
extern "C" {
Expand Down