AM261x MCU+ SDK: cb_ethernet.h Source File

| AM261x MCU+ SDK 26.00.00 |

cb_ethernet.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2023 Texas Instruments Incorporated
3 * Copyright (c) 2023 Excelfore Corporation (https://excelfore.com)
4 *
5 * All rights reserved not granted herein.
6 * Limited License.
7 *
8 * Texas Instruments Incorporated grants a world-wide, royalty-free,
9 * non-exclusive license under copyrights and patents it now or hereafter
10 * owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
11 * this software subject to the terms herein. With respect to the foregoing patent
12 * license, such license is granted solely to the extent that any such patent is necessary
13 * to Utilize the software alone. The patent license shall not apply to any combinations which
14 * include this software, other than combinations with devices manufactured by or for TI ("TI Devices").
15 * No hardware patent is licensed hereunder.
16 *
17 * Redistributions must preserve existing copyright notices and reproduce this license (including the
18 * above copyright notice and the disclaimer and (if applicable) source code license limitations below)
19 * in the documentation and/or other materials provided with the distribution
20 *
21 * Redistribution and use in binary form, without modification, are permitted provided that the following
22 * conditions are met:
23 *
24 * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
25 * software provided in binary form.
26 * * any redistribution and use are licensed by TI for use only with TI Devices.
27 * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
28 *
29 * If software source code is provided to you, modification and redistribution of the source code are permitted
30 * provided that the following conditions are met:
31 *
32 * * any redistribution and use of the source code, including any resulting derivative works, are licensed by
33 * TI for use only with TI Devices.
34 * * any redistribution and use of any object code compiled from the source code and any resulting derivative
35 * works, are licensed by TI for use only with TI Devices.
36 *
37 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
38 * promote products derived from this software without specific prior written permission.
39 *
40 * DISCLAIMER.
41 *
42 * THIS SOFTWARE IS PROVIDED BY TI AND TI"S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
43 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44 * IN NO EVENT SHALL TI AND TI"S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
46 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
49 */
59 #ifndef CB_ETHERNET_H_
60 #define CB_ETHERNET_H_
61
62 #ifdef CB_ETHERNET_NON_POSIX_H
63 /* non-posix platforms need to support necessary POSIX compatible
64 * functions and types which are defined as CB_* macros below.
65 * And provide them in a header file defined as CB_SOCKET_NON_POSIX_H */
66 #include CB_ETHERNET_NON_POSIX_H
67 #else
68 #include <unistd.h>
69 #include <sys/socket.h>
70 #include <sys/ioctl.h>
71 #include <netinet/in.h>
72 #include <net/if.h>
73 #include <net/if_arp.h>
74 #include <ifaddrs.h>
75 #include <fcntl.h>
76 #include <netpacket/packet.h>
77 #include <net/ethernet.h>
78
79 #define CB_MAX_NETDEVNAME IFNAMSIZ
80 #define CB_SOCKET_T int
81 #define CB_SOCKET_VALID(x) ((x)>=0)
82 #define CB_SOCKET_INVALID_VALUE -1
83 #define CB_STDIN_FILENO STDIN_FILENO
84 #define CB_STDOUT_FILENO STDOUT_FILENO
85 #define CB_STDERR_FILENO STDERR_FILENO
86 #define CB_SOCKET_INC(x) ((x)+1)
87 #define CB_ETHHDR_T struct ethhdr
88 #define CB_SOCKLEN_T socklen_t
89 #define CB_SOCKADDR_T struct sockaddr
90 #define CB_SOCKADDR_LL_T struct sockaddr_ll
91 #define CB_SOCKADDR_IN_T struct sockaddr_in
92 #define CB_SOCKADDR_IN6_T struct sockaddr_in6
93 #define CB_SOCKADDR_STORAGE_T struct sockaddr_storage
94 #define CB_IN_ADDR_T struct in_addr
95 #define CB_IN6_ADDR_T struct in6_addr
96 #define CB_IFREQ_T struct ifreq
97
98 #define CB_OPEN open
99 #define CB_CLOSE close
100 #define CB_WRITE write
101 #define CB_SOCKET socket
102 #define CB_IF_NAMETOINDEX if_nametoindex
103 #define CB_SOCK_BIND bind
104 #define CB_SOCK_IOCTL ioctl
105 #define CB_SOCK_CLOSE close
106 #define CB_SETSOCKOPT setsockopt
107 #define CB_SOCK_SENDTO sendto
108 #define CB_SELECT select
109 #define CB_SOCK_RECVFROM recvfrom
110 #define CB_SOCK_RECVMSG recvmsg
111 #define CB_SOCK_WRITE write
112 #define CB_SOCK_CONNECT connect
113
114 #define CB_FCNTL fcntl
115 #define CB_FD_SET_T fd_set
116 #define CB_FDSET FD_SET
117 #define CB_FDCLR FD_CLR
118 #define CB_FDISSET FD_ISSET
119 #define CB_FDZERO FD_ZERO
120
121 #endif // CB_ETHERNET_NON_POSIX_H
122
123 /* virtual eth ports and ptp devices are used in the ovip mode.
124 * common suffix should be added after these prefixes */
129 #define CB_VIRTUAL_ETHDEV_PREFIX "cbeth"
130
135 #define CB_VIRTUAL_PTPDEV_PREFIX "cbptp"
136
141 #define CB_VIRTUAL_ETHDEV_MACU32 {0x02,0x01,0x45,0x10}
142
143 /* the following definitions have some variations for platforms */
144 #ifndef H_SOURCE
145 #define H_SOURCE h_source
146 #endif
147
148 #ifndef H_DEST
149 #define H_DEST h_dest
150 #endif
151
152 #ifndef H_PROTO
153 #define H_PROTO h_proto
154 #endif
155
156 /************************************************************
157 * definitions to handle PTP messages
158 ************************************************************/
162 #define PTP_HEAD_MSGTYPE(x) ((*(uint8_t *) (&((uint8_t *)(x))[0])) & 0x0F)
163
167 #define PTP_HEAD_SEQID(x) ((((uint8_t *)(x))[30]<<8u)|(((uint8_t *)(x))[31]))
168
172 #define PTP_HEAD_DOMAIN_NUMBER(x) (*(uint8_t *)(&((uint8_t *)(x))[4]))
173
175 #define MAX_PTPDEV_NAME 32
176
179 typedef char ptpdevname_t[MAX_PTPDEV_NAME];
180
181
182 /************************************************************
183 * definitions to handle raw socket
184 ************************************************************/
192 typedef enum {
193 CB_RAWSOCK_RDWR = 0,
194 CB_RAWSOCK_RDONLY,
195 CB_RAWSOCK_WRONLY,
196 } cb_rawsock_rw_t;
197
201 typedef struct cb_rawsock_ovip_para {
202 uint32_t laddr;
203 uint16_t lport;
204 uint32_t daddr;
205 uint16_t dport;
206 } cb_rawsock_ovip_para_t;
207
208 typedef enum {
209 CB_SOCK_MODE_OVIP = -1,
210 CB_SOCK_MODE_NORMAL,
211 CB_SOCK_MODE_LSM,
212 } cb_sock_mode_t;
213
217 typedef struct cb_rawsock_paras{
218 const char *dev;
219 uint16_t proto;
220 uint16_t vlan_proto;
221 int priority;
222 cb_rawsock_rw_t rw_type;
223 cb_sock_mode_t sock_mode;
224 cb_rawsock_ovip_para_t *ovipp;
225 uint16_t vlanid;
226 uint32_t sndbuf;
227 uint32_t rcvbuf;
228 } cb_rawsock_paras_t;
229
233 typedef char netdevname_t[CB_MAX_NETDEVNAME];
234
235 /************************************************************
236 * functions
237 ************************************************************/
247 int cb_get_mac_bydev(CB_SOCKET_T sfd, const char *dev, ub_macaddr_t bmac);
248
257 int cb_get_ip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp);
258
267 int cb_get_brdip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp);
268
285 int cb_rawsock_open(cb_rawsock_paras_t *llrawp, CB_SOCKET_T *fd, CB_SOCKADDR_LL_T *addr,
286 int *mtusize, ub_macaddr_t bmac);
287
293 int cb_rawsock_close(CB_SOCKET_T fd);
294
301 int cb_sock_set_priority(CB_SOCKET_T fd, int priority);
302
310 int cb_expand_mtusize(CB_SOCKET_T fd, const char *dev, int *mtusize);
311
319 int cb_set_promiscuous_mode(CB_SOCKET_T sfd, const char *dev, bool enable);
320
329 int cb_reg_multicast_address(CB_SOCKET_T fd, const char *dev,
330 const unsigned char *mcastmac, int del);
331
338 int cb_get_all_netdevs(int maxdevnum, netdevname_t *netdevs);
339
352 int cb_get_ethtool_linkstate(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate);
353
367 int cb_get_ethtool_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex);
368
377 int cb_get_netdev_from_ptpdev(char *ptpdev, char *netdev);
378
388 int cb_get_ptpdev_from_netdev(char *netdev, char *ptpdev);
389
390 #endif
391
cb_expand_mtusize
int cb_expand_mtusize(CB_SOCKET_T fd, const char *dev, int *mtusize)
expand mtusize
CB_SOCK_MODE_NORMAL
@ CB_SOCK_MODE_NORMAL
Normal L2 raw socket.
Definition: cb_ethernet.h:210
cb_rawsock_paras_t::sock_mode
cb_sock_mode_t sock_mode
-1:raw socket over udp, 0:normal,
Definition: cb_ethernet.h:223
cb_get_netdev_from_ptpdev
int cb_get_netdev_from_ptpdev(char *ptpdev, char *netdev)
find network device name from ptp device name
cb_set_promiscuous_mode
int cb_set_promiscuous_mode(CB_SOCKET_T sfd, const char *dev, bool enable)
set the promiscuous mode on the socket
cb_get_ethtool_info
int cb_get_ethtool_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex)
get ethtool info(speed and duplex) from device name like 'eth0'
CB_SOCK_MODE_OVIP
@ CB_SOCK_MODE_OVIP
L2 packets over IP socket for testing.
Definition: cb_ethernet.h:209
CB_SOCKET_T
#define CB_SOCKET_T
Definition: cb_ethernet.h:80
cb_rawsock_open
int cb_rawsock_open(cb_rawsock_paras_t *llrawp, CB_SOCKET_T *fd, CB_SOCKADDR_LL_T *addr, int *mtusize, ub_macaddr_t bmac)
generic raw ethernet open
MAX_PTPDEV_NAME
#define MAX_PTPDEV_NAME
maximum character number of ptp device name
Definition: cb_ethernet.h:175
cb_rawsock_ovip_para_t::laddr
uint32_t laddr
local IP address
Definition: cb_ethernet.h:202
CB_RAWSOCK_RDWR
@ CB_RAWSOCK_RDWR
Definition: cb_ethernet.h:193
cb_rawsock_close
int cb_rawsock_close(CB_SOCKET_T fd)
close the socket opened by cb_rawsock_open
cb_get_mac_bydev
int cb_get_mac_bydev(CB_SOCKET_T sfd, const char *dev, ub_macaddr_t bmac)
get mac address from device name like 'eth0'
cb_get_ethtool_linkstate
int cb_get_ethtool_linkstate(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate)
get ethtool link state from device name like 'eth0'
CB_RAWSOCK_WRONLY
@ CB_RAWSOCK_WRONLY
Definition: cb_ethernet.h:195
cb_get_ptpdev_from_netdev
int cb_get_ptpdev_from_netdev(char *netdev, char *ptpdev)
find ptp device name from network device name
cb_rawsock_paras_t::rcvbuf
uint32_t rcvbuf
socket receive buffer size, set to 0 to use default value
Definition: cb_ethernet.h:227
cb_rawsock_paras_t::vlan_proto
uint16_t vlan_proto
protocol value in VLAN tag, not used in non-tagged
Definition: cb_ethernet.h:220
cb_get_brdip_bydev
int cb_get_brdip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp)
get broadcast ip address from device name like 'eth0'
cb_rawsock_paras_t::vlanid
uint16_t vlanid
vlan identifier
Definition: cb_ethernet.h:225
netdevname_t
char netdevname_t[CB_MAX_NETDEVNAME]
network device name.
Definition: cb_ethernet.h:233
cb_rawsock_ovip_para_t::daddr
uint32_t daddr
destination IP address
Definition: cb_ethernet.h:204
ub_macaddr_t
ub_bytearray6_t ub_macaddr_t
6-byte binary array is used for MAC Address
Definition: ub_strutils.h:71
cb_rawsock_paras_t::rw_type
cb_rawsock_rw_t rw_type
one of RAWSOCK_WRONLY, RAWSOCK_RDONLY, RAWSOCK_RDWR
Definition: cb_ethernet.h:222
cb_rawsock_rw_t
cb_rawsock_rw_t
this enumeration defines permission for raw socket.
Definition: cb_ethernet.h:192
cb_rawsock_paras_t
raw socket parameters.to open or create raw socket this structure must be filled.
Definition: cb_ethernet.h:217
cb_rawsock_paras_t::proto
uint16_t proto
protocol value like ETH_P_1588
Definition: cb_ethernet.h:219
cb_rawsock_ovip_para_t::dport
uint16_t dport
destination IP port
Definition: cb_ethernet.h:205
cb_rawsock_paras_t::priority
int priority
PCP priority value in VLAN tag.
Definition: cb_ethernet.h:221
cb_reg_multicast_address
int cb_reg_multicast_address(CB_SOCKET_T fd, const char *dev, const unsigned char *mcastmac, int del)
register/deregister multicast address to receive
cb_rawsock_paras_t::ovipp
cb_rawsock_ovip_para_t * ovipp
over-udp mode parameter
Definition: cb_ethernet.h:224
cb_rawsock_paras_t::sndbuf
uint32_t sndbuf
socket send buffer size, set to 0 to use default value
Definition: cb_ethernet.h:226
CB_SOCKADDR_LL_T
#define CB_SOCKADDR_LL_T
Definition: cb_ethernet.h:90
cb_rawsock_ovip_para_t
parameters to open the over IP mode raw socket, the values are in host order
Definition: cb_ethernet.h:201
CB_RAWSOCK_RDONLY
@ CB_RAWSOCK_RDONLY
Definition: cb_ethernet.h:194
cb_rawsock_paras_t::dev
const char * dev
ethernet device name
Definition: cb_ethernet.h:218
CB_SOCK_MODE_LSM
@ CB_SOCK_MODE_LSM
L2 raw socket for QNX LSM mode.
Definition: cb_ethernet.h:211
cb_rawsock_ovip_para_t::lport
uint16_t lport
local IP port
Definition: cb_ethernet.h:203
cb_sock_set_priority
int cb_sock_set_priority(CB_SOCKET_T fd, int priority)
set socket priority
ptpdevname_t
char ptpdevname_t[MAX_PTPDEV_NAME]
ptpdevice name.
Definition: cb_ethernet.h:179
cb_sock_mode_t
cb_sock_mode_t
Definition: cb_ethernet.h:208
cb_get_all_netdevs
int cb_get_all_netdevs(int maxdevnum, netdevname_t *netdevs)
get all network devices name availble in systems.
cb_get_ip_bydev
int cb_get_ip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp)
get ip address from device name like 'eth0'
CB_IN_ADDR_T
#define CB_IN_ADDR_T
Definition: cb_ethernet.h:94
CB_MAX_NETDEVNAME
#define CB_MAX_NETDEVNAME
Definition: cb_ethernet.h:79