eth_bsd.c 6.02 KB
Newer Older
1
2
3
/*
	Ethernet device access for use on a BSD based OS.
 */
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <err.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/bpf.h>

#include <netinet/in.h>		// for ntohs()
#include <net/ethernet.h>	// for struct ether_header

20
//#include "ec_bsd.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
21
22
#include <ec/ec_int.h>
#include <log/log.h>
23
24
25
26
27
28
29
30

struct sockaddr_ll {
	uint16_t		sll_family;
	uint16_t		sll_protocol;
        uint8_t			sll_addr[ETHER_ADDR_LEN];
        size_t			sll_halen;
};

31
32
static struct timeval tx_timeout;
static struct timeval rx_timeout;
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
static struct sockaddr_ll sockaddr_ll;

int eth_timeout_set(int ec_sock, struct timeval *tx, struct timeval *rx)
{
	memcpy(&tx_timeout, tx, sizeof(struct timeval));
	memcpy(&rx_timeout, rx, sizeof(struct timeval));

	return 0;
}

int bpf_open(void)
{
	char		dev_name[10 + 1];
	int		fd = -1;
	int		i = 0;
	
	// open the first available BPF device
	while ((fd == -1) && (i < 100)) {
		sprintf(dev_name, "/dev/bpf%d", i);
		fd = open(dev_name, O_RDWR);
		if (fd >= 0)
			return fd;
		i++;
	}

58
	log_send(LOG_T_DEBUG, "bpf_open() fd: %d", fd);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	
	return fd;
}

int bpf_bind_interface(int bpf_fd, char *if_name)
{
	struct ifreq		bind_if;
	
	strncpy(bind_if.ifr_name, if_name, sizeof(bind_if.ifr_name));
	
	return ioctl(bpf_fd, BIOCSETIF, &bind_if);
}

int bpf_set_write_filter(int bpf_fd)
{
	struct bpf_insn		pass_all_filt[] = { BPF_STMT(BPF_RET+BPF_K, -1) };
	struct bpf_program	wfilter;
	
	wfilter.bf_len = 1;
	wfilter.bf_insns = pass_all_filt;
79
	log_send(LOG_T_DEBUG, "bpf_fd: %d", bpf_fd);
80
	if (ioctl(bpf_fd, BIOCSETWF, &wfilter) < 0)
81
		log_send(LOG_T_ERROR, "bpf_set_write_filter(): BIOCSETWF");
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
	
	return 0;
}


uint8_t			*bpf_buf;
int			bpf_buf_len;
int			bpf_cap_len;
int			bpf_cap_ofs;


int eth_open(char *if_name)
{
	int		ec_sock;
	int		val;


	ec_sock = bpf_open();
	if (ec_sock < 0)
		err(1, "bpf_open()");
	
	if (bpf_bind_interface(ec_sock, if_name) == -1)
		err(1, "bpf_bind_interface()");
	
	if (bpf_set_write_filter(ec_sock) != 0)
		err(1, "bpf_set_write_filter()");
	
	// return immediately when a packet is received
	val = 1;
	if (ioctl(ec_sock, BIOCIMMEDIATE, &val) == -1)
		err(1, "ioctl(BIOCIMMEDIATE)");
	
	// request buffer length
	if (ioctl(ec_sock, BIOCGBLEN, &bpf_buf_len) == -1)
		err(1, "ioctl(BIOCGBLEN)");

	bpf_buf = malloc(bpf_buf_len);
	if (bpf_buf == NULL)
		err(1, "couldn't allocate bpf_buf memory");
	
122
123
	log_send(LOG_T_DEBUG, "cap_len: %d", bpf_buf_len);
	log_send(LOG_T_DEBUG, "ec_sock: %d", ec_sock);
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
	
	sockaddr_ll.sll_addr[0] = 0xff;
	sockaddr_ll.sll_addr[1] = 0xff;
	sockaddr_ll.sll_addr[2] = 0xff;
	sockaddr_ll.sll_addr[3] = 0xff;
	sockaddr_ll.sll_addr[4] = 0xff;
	sockaddr_ll.sll_addr[5] = 0xff;

	return ec_sock;
}

int eth_close(int ec_sock)
{
	return close(ec_sock);
}

int eth_recv(int ec_sock, void *frame, int max_len)
{
	struct bpf_hdr		*bpf_packet;
	struct ether_header	*eth_hdr;
	uint8_t			*pkt;
	int			len, cpy_len;
	fd_set			fdr;
	struct timeval		timeout;
	int			ret;

	//TODO: think about zero-copy

	while (1) {
		// TODO: kqueue?
154
		memcpy(&timeout, &rx_timeout, sizeof(timeout));
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

		FD_ZERO(&fdr);
		FD_SET(ec_sock, &fdr);
		ret = select(ec_sock + 1, &fdr, NULL, NULL, &timeout);
		if (ret == 0) {
			return 0;
		}

		bpf_cap_len = read(ec_sock, bpf_buf, bpf_buf_len);
		if (bpf_cap_len <= 0)
			err(1, "bpf read()");
		
		bpf_cap_ofs = 0;
		while (bpf_cap_ofs < bpf_cap_len) {
			bpf_packet = (struct bpf_hdr *)&bpf_buf[bpf_cap_ofs];
			pkt = &bpf_buf[bpf_cap_ofs + bpf_packet->bh_hdrlen];
			
#if 0
			printf("dst: %02x %02x %02x %02x %02x %02x\n", pkt[0],
			    pkt[1], pkt[2], pkt[3], pkt[4], pkt[5]);
			printf("src: %02x %02x %02x %02x %02x %02x\n", pkt[6],
			    pkt[7], pkt[8], pkt[9], pkt[10], pkt[11]);
#endif			
			eth_hdr = (struct ether_header *)pkt;
//			printf(" type: %04x\n",
//			    ntohs(eth_hdr->ether_type));
			
			// braindead kludge!
			// TODO: create a BPF filter instead...
			len = bpf_packet->bh_caplen;
			if ((len <= ETHER_HDR_LEN) ||
			    (ntohs(eth_hdr->ether_type) != ETH_P_ETHERCAT)) {
				// not ethercat. skip this packet
				bpf_cap_ofs +=
				    BPF_WORDALIGN(bpf_packet->bh_hdrlen +
				    bpf_packet->bh_caplen);
				continue;
			}
			// another braindead kludge!
			// TODO: ignore packets we transmit :)
			if (!(pkt[6] & 0x02)) {
//				printf("it's our own packet...\n");
				bpf_cap_ofs +=
				    BPF_WORDALIGN(bpf_packet->bh_hdrlen +
				    bpf_packet->bh_caplen);
				continue;
			}

			// we have an Ethercat packet
			cpy_len = (len > max_len) ? max_len : len;
			memcpy(frame, &pkt[ETHER_HDR_LEN], cpy_len);
			
			// advance offset
			bpf_cap_ofs += BPF_WORDALIGN(bpf_packet->bh_hdrlen +
			    bpf_packet->bh_caplen);
			
			return cpy_len;
		}
	}
}


struct ethercat_pkt {
	struct ether_header	eth_hdr;
	uint8_t			data[1500];
} __packed;


struct ethercat_pkt		tx_pkt;



int eth_send(int ec_sock, void *frame, int len)
{
229
	struct sockaddr_ll	*addr = (struct sockaddr_ll *)&sockaddr_ll;
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
	int			result;
	uint8_t			*ptr;
	
	
	if (ETHER_HDR_LEN + len + ETHER_CRC_LEN > ETHER_MAX_LEN) {
		errx(1, "eth_send(): packet too long!");
	}

	//TODO: think about zero-copy
	
	// construct header
	memcpy(tx_pkt.eth_hdr.ether_dhost, addr->sll_addr, ETHER_ADDR_LEN);
	tx_pkt.eth_hdr.ether_type = addr->sll_protocol;
	// copy-in payload
	memcpy(tx_pkt.data, frame, len);

	ptr = (uint8_t *)&tx_pkt;
247
248
249
250
251
252
253
254
255
256
257
258
	//TODO: do this once during start-up, in ec_init()
	// destination address
	ptr[0] = 0xff;
	ptr[1] = 0xff;
	ptr[2] = 0xff;
	ptr[3] = 0xff;
	ptr[4] = 0xff;
	ptr[5] = 0xff;
	// protocol
	ptr[2 * 6 + 0] = ETH_P_ETHERCAT >> 8;
	ptr[2 * 6 + 1] = ETH_P_ETHERCAT & 0xff;
	
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#if 0
	printf("tx:");
	for (result = 0; result < ETHER_HDR_LEN + len; result++) {
		printf(" %02x", *ptr++);
	}
	printf("\n");
#endif
	
	result = write(ec_sock, &tx_pkt, ETHER_HDR_LEN + len);
//	printf("eth_send(): send returned %d\n", result);
	if (result >= ETHER_HDR_LEN) {
		// Jeroen's code expects number of bytes excluding header/crc
		result -= ETHER_HDR_LEN;
	}
//	printf("eth_send(): result %d\n", result);
	return result;
}