AM64x MCU+ SDK  10.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_ETHHDR_T struct ethhdr
84 #define CB_SOCKLEN_T socklen_t
85 #define CB_SOCKADDR_T struct sockaddr
86 #define CB_SOCKADDR_LL_T struct sockaddr_ll
87 #define CB_SOCKADDR_IN_T struct sockaddr_in
88 #define CB_SOCKADDR_IN6_T struct sockaddr_in6
89 #define CB_SOCKADDR_STORAGE_T struct sockaddr_storage
90 #define CB_IN_ADDR_T struct in_addr
91 #define CB_IN6_ADDR_T struct in6_addr
92 #define CB_IFREQ_T struct ifreq
93 
94 #define CB_OPEN open
95 #define CB_CLOSE close
96 #define CB_WRITE write
97 #define CB_SOCKET socket
98 #define CB_IF_NAMETOINDEX if_nametoindex
99 #define CB_SOCK_BIND bind
100 #define CB_SOCK_IOCTL ioctl
101 #define CB_SOCK_CLOSE close
102 #define CB_SETSOCKOPT setsockopt
103 #define CB_SOCK_SENDTO sendto
104 #define CB_SELECT select
105 #define CB_SOCK_RECVFROM recvfrom
106 #define CB_SOCK_RECVMSG recvmsg
107 #define CB_SOCK_WRITE write
108 #define CB_SOCK_CONNECT connect
109 
110 #define CB_FCNTL fcntl
111 #define CB_FD_SET_T fd_set
112 #define CB_FDSET FD_SET
113 #define CB_FDCLR FD_CLR
114 #define CB_FDISSET FD_ISSET
115 #define CB_FDZERO FD_ZERO
116 
117 #endif // CB_ETHERNET_NON_POSIX_H
118 
119 /* virtual eth ports and ptp devices are used in the ovip mode.
120  * common suffix should be added after these prefixes */
125 #define CB_VIRTUAL_ETHDEV_PREFIX "cbeth"
126 
131 #define CB_VIRTUAL_PTPDEV_PREFIX "cbptp"
132 
137 #define CB_VIRTUAL_ETHDEV_MACU32 {0x02,0x01,0x45,0x10}
138 
139 /* the following definitions have some variations for platforms */
140 #ifndef H_SOURCE
141 #define H_SOURCE h_source
142 #endif
143 
144 #ifndef H_DEST
145 #define H_DEST h_dest
146 #endif
147 
148 #ifndef H_PROTO
149 #define H_PROTO h_proto
150 #endif
151 
152 /************************************************************
153  * definitions to handle PTP messages
154  ************************************************************/
158 #define PTP_HEAD_MSGTYPE(x) ((*(uint8_t *) (&((uint8_t *)(x))[0])) & 0x0F)
159 
163 #define PTP_HEAD_SEQID(x) ((((uint8_t *)(x))[30]<<8u)|(((uint8_t *)(x))[31]))
164 
168 #define PTP_HEAD_DOMAIN_NUMBER(x) (*(uint8_t *)(&((uint8_t *)(x))[4]))
169 
171 #define MAX_PTPDEV_NAME 32
172 
176 
177 
178 /************************************************************
179  * definitions to handle raw socket
180  ************************************************************/
188 typedef enum {
193 
197 typedef struct cb_rawsock_ovip_para {
198  uint32_t laddr;
199  uint16_t lport;
200  uint32_t daddr;
201  uint16_t dport;
203 
204 typedef enum {
208 
212 typedef struct cb_rawsock_paras{
213  const char *dev;
214  uint16_t proto;
215  uint16_t vlan_proto;
216  int priority;
220  uint16_t vlanid;
221  uint32_t sndbuf;
222  uint32_t rcvbuf;
224 
229 
230 /************************************************************
231  * functions
232  ************************************************************/
242 int cb_get_mac_bydev(CB_SOCKET_T sfd, const char *dev, ub_macaddr_t bmac);
243 
252 int cb_get_ip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp);
253 
262 int cb_get_brdip_bydev(CB_SOCKET_T sfd, const char *dev, CB_IN_ADDR_T *inp);
263 
281  int *mtusize, ub_macaddr_t bmac);
282 
289 
296 int cb_sock_set_priority(CB_SOCKET_T fd, int priority);
297 
305 int cb_expand_mtusize(CB_SOCKET_T fd, const char *dev, int *mtusize);
306 
314 int cb_set_promiscuous_mode(CB_SOCKET_T sfd, const char *dev, bool enable);
315 
324 int cb_reg_multicast_address(CB_SOCKET_T fd, const char *dev,
325  const unsigned char *mcastmac, int del);
326 
333 int cb_get_all_netdevs(int maxdevnum, netdevname_t *netdevs);
334 
347 int cb_get_ethtool_linkstate(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate);
348 
362 int cb_get_ethtool_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex);
363 
372 int cb_get_netdev_from_ptpdev(char *ptpdev, char *netdev);
373 
383 int cb_get_ptpdev_from_netdev(char *netdev, char *ptpdev);
384 
385 #endif
386 
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
Definition: cb_ethernet.h:206
cb_rawsock_paras_t::sock_mode
cb_sock_mode_t sock_mode
-1:raw socket over udp, 0:normal,
Definition: cb_ethernet.h:218
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
Definition: cb_ethernet.h:205
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:171
addr
uint64_t addr
Definition: csl_udmap_tr.h:3
cb_rawsock_ovip_para_t::laddr
uint32_t laddr
local IP address
Definition: cb_ethernet.h:198
CB_RAWSOCK_RDWR
@ CB_RAWSOCK_RDWR
Definition: cb_ethernet.h:189
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:191
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:222
cb_rawsock_paras_t::vlan_proto
uint16_t vlan_proto
protocol value in VLAN tag, not used in non-tagged
Definition: cb_ethernet.h:215
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:220
netdevname_t
char netdevname_t[CB_MAX_NETDEVNAME]
network device name.
Definition: cb_ethernet.h:228
cb_rawsock_ovip_para_t::daddr
uint32_t daddr
destination IP address
Definition: cb_ethernet.h:200
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:217
cb_rawsock_rw_t
cb_rawsock_rw_t
this enumeration defines permission for raw socket.
Definition: cb_ethernet.h:188
cb_rawsock_paras_t
raw socket parameters.to open or create raw socket this structure must be filled.
Definition: cb_ethernet.h:212
cb_rawsock_paras_t::proto
uint16_t proto
protocol value like ETH_P_1588
Definition: cb_ethernet.h:214
cb_rawsock_ovip_para_t::dport
uint16_t dport
destination IP port
Definition: cb_ethernet.h:201
cb_rawsock_paras_t::priority
int priority
PCP priority value in VLAN tag.
Definition: cb_ethernet.h:216
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:219
cb_rawsock_paras_t::sndbuf
uint32_t sndbuf
socket send buffer size, set to 0 to use default value
Definition: cb_ethernet.h:221
CB_SOCKADDR_LL_T
#define CB_SOCKADDR_LL_T
Definition: cb_ethernet.h:86
cb_rawsock_ovip_para_t
parameters to open the over IP mode raw socket, the values are in host order
Definition: cb_ethernet.h:197
CB_RAWSOCK_RDONLY
@ CB_RAWSOCK_RDONLY
Definition: cb_ethernet.h:190
cb_rawsock_paras_t::dev
const char * dev
ethernet device name
Definition: cb_ethernet.h:213
cb_rawsock_ovip_para_t::lport
uint16_t lport
local IP port
Definition: cb_ethernet.h:199
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:175
cb_sock_mode_t
cb_sock_mode_t
Definition: cb_ethernet.h:204
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:90
CB_MAX_NETDEVNAME
#define CB_MAX_NETDEVNAME
Definition: cb_ethernet.h:79