summaryrefslogtreecommitdiffstats
path: root/src/framework/hal/hal.h
blob: 5a8f51e12694242caa75cf675f7a8408a0590a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* 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 _HAL_H_
#define _HAL_H_

#include <stdint.h>
#include "hal_api.h"
#include "nstack_log.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C" {
/* *INDENT-ON* */
#endif

#define HAL_DRV_MAX   32

#define HAL_IO_REGISTER(name, ops) \
    static __attribute__((__constructor__)) void __hal_register##name(void)  \
    {\
        hal_io_adpt_register(ops); \
    } \


#define HAL_MAX_PCI_ADDR_LEN 16

#define HAL_MAX_DRIVER_NAME_LEN 128

#define HAL_MAX_PATH_LEN        4096    //max path length on linux is 4096

#define HAL_SCRIPT_LENGTH       256

#define HAL_HDL_TO_ID(hdl) (hdl.id)

/* IO using DPDK interface */
typedef struct dpdk_if
{
  uint8_t port_id;              /**< DPDK port identifier */
  uint8_t slave_num;
  uint8_t slave_port[HAL_MAX_SLAVES_PER_BOND];

  uint32_t hw_vlan_filter:1;
  uint32_t hw_vlan_strip:1;
  uint32_t rsv30:30;

  uint32_t rx_queue_num;
  uint32_t rx_ring_size[HAL_ETH_MAX_QUEUE_NUM];
  hal_mempool_t *rx_pool[HAL_ETH_MAX_QUEUE_NUM];

  uint32_t tx_queue_num;
  uint32_t tx_ring_size[HAL_ETH_MAX_QUEUE_NUM];

  char pci_addr[HAL_MAX_PCI_ADDR_LEN];
  char nic_name[HAL_MAX_NIC_NAME_LEN];
  char nic_type[HAL_MAX_NIC_NAME_LEN];
  char driver_name[HAL_MAX_DRIVER_NAME_LEN];
} dpdk_if_t;

typedef struct netif_inst
{
  enum
  {
    NETIF_STATE_FREE = 0,
    NETIF_STATE_ACTIVE
  } state;

  hal_hdl_t hdl;

  const struct netif_ops *ops;          /**< Implementation specific methods */

  union
  {
    dpdk_if_t dpdk_if;              /**< using DPDK for IO */
  } data;

} netif_inst_t;

typedef struct netif_ops
{
  const char *name;
  int (*init_global) (int argc, char **argv);
  int (*init_local) (void);
  int (*open) (netif_inst_t * inst, const char *name, const char *type);
  int (*close) (netif_inst_t * inst);
  int (*start) (netif_inst_t * inst);
  int (*stop) (netif_inst_t * inst);
  int (*bond) (netif_inst_t * inst, const char *bond_name,
               uint8_t slave_num, netif_inst_t * slave[]);
    uint32_t (*mtu) (netif_inst_t * inst);
  int (*macaddr) (netif_inst_t * inst, void *mac_addr);
  int (*capability) (netif_inst_t * inst, hal_netif_capa_t * info);
    uint16_t (*recv) (netif_inst_t * inst, uint16_t queue_id,
                      hal_mbuf_t ** rx_pkts, uint16_t nb_pkts);
    uint16_t (*send) (netif_inst_t * inst, uint16_t queue_id,
                      hal_mbuf_t ** tx_pkts, uint16_t nb_pkts);
    uint32_t (*link_status) (netif_inst_t * inst);
  int (*stats) (netif_inst_t * inst, hal_netif_stats_t * stats);
  int (*stats_reset) (netif_inst_t * inst);
  int (*config) (netif_inst_t * inst, hal_netif_config_t * conf);
  int (*mcastaddr) (netif_inst_t * inst, void *mc_addr_set,
                    void *mc_addr, uint32_t nb_mc_addr);
  int (*add_mac) (netif_inst_t * inst, void *mc_addr);
  int (*rmv_mac) (netif_inst_t * inst, void *mc_addr);
  int (*allmcast) (netif_inst_t * inst, uint8_t enable);
} netif_ops_t;

extern netif_inst_t netif_tbl[HAL_MAX_NIC_NUM];

static inline netif_inst_t *
alloc_netif_inst ()
{
  int i;
  netif_inst_t *inst;

  for (i = 0; i < HAL_MAX_NIC_NUM; ++i)
    {
      inst = &netif_tbl[i];

      if (NETIF_STATE_FREE == inst->state)
        {
          inst->state = NETIF_STATE_ACTIVE;

          inst->hdl.id = i;

          return inst;
        }
    }

  return NULL;

}

static inline netif_inst_t *
get_netif_inst (hal_hdl_t hdl)
{
  netif_inst_t *inst;

  if (unlikely (!hal_is_valid (hdl)))
    {
      NSHAL_LOGERR ("inst id is not valid]inst=%i, HAL_MAX_NIC_NUM=%d",
                    HAL_HDL_TO_ID (hdl), HAL_MAX_NIC_NUM);

      return NULL;
    }

  inst = &netif_tbl[HAL_HDL_TO_ID (hdl)];

  if (unlikely ((NETIF_STATE_ACTIVE != inst->state) || (NULL == inst->ops)))
    {
      NSHAL_LOGERR ("netif is not active]inst=%i", HAL_HDL_TO_ID (hdl));

      return NULL;
    }

  return inst;
}

int hal_snprintf (char *buffer, size_t buflen, const char *format, ...);
int hal_is_script_valid (const char *cmd);
int hal_run_script (const char *cmd, char *result_buf, size_t max_result_len);
void hal_io_adpt_register (const netif_ops_t * ops);

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif

#endif