-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.cu
143 lines (126 loc) · 4.16 KB
/
socket.cu
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
/**
* @file socket.cu
* @brief samples from a socket
* @author John Melton, G0ORX/N6LYT
*/
/* Copyright (C)
* 2015 - John Melton, G0ORX/N6LYT
*
* Based on code by Steven Passe AD0ES and Vasiliy Gokoyev K3IT
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netpacket/packet.h>
#include <net/if_packet.h>
#include "common.cuh"
#include "inputbuffer.cuh"
#include "socket.cuh"
#include "time.cuh"
#define DATA_PROTO 0xefff
static int sockFd;
static pthread_t socketThread;
void* socketLoop(void* args);
void initSocket() {
struct ifreq ifbuffer;
int result;
int sockopt;
fprintf(stderr,"initSocket: %s\n", interface);
if ((sockFd = socket(AF_PACKET, SOCK_DGRAM, htons(DATA_PROTO))) < 0 ) {
fprintf(stderr,"initSocket: error creating socket %d\n", sockFd);
exit(EXIT_FAILURE);
}
memset(&ifbuffer, 0x00, sizeof(ifbuffer));
strncpy(ifbuffer.ifr_name, interface, IFNAMSIZ-1);
ioctl(sockFd, SIOCGIFFLAGS, &ifbuffer);
ifbuffer.ifr_flags |= IFF_PROMISC;
ioctl(sockFd, SIOCSIFFLAGS, &ifbuffer);
sockopt=1;
if((result=setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof sockopt)) < 0) {
fprintf(stderr,"initSocket: error setsockopt(SO_REUSEADDR) %d\n", result);
exit(EXIT_FAILURE);
}
if((result=setsockopt(sockFd, SOL_SOCKET, SO_BINDTODEVICE, interface, IFNAMSIZ-1)) < 0) {
fprintf(stderr,"initSocket: error setsockopt(SO_BINDTODEVICE) %d\n", result);
exit(EXIT_FAILURE);
}
result=pthread_create(&socketThread, NULL, socketLoop, NULL);
if(result<0) {
fprintf(stderr, "Error creating socket thread: %d\n", result);
exit(EXIT_FAILURE);
}
}
void* socketLoop(void* args) {
int bytestoread=L_SIZE*sizeof(short);
int count;
int offset;
int result;
#ifdef TIMING
long long starttime;
long long endtime;
int reads;
#endif
fprintf(stderr,"socketLoop: running on cpu %d\n", sched_getcpu());
// fprintf(stderr, "socketLoop: bytestoread=%d\n",bytestoread);
while(1) {
result=sem_wait(&inputBufferEmpty);
if(result!=0) {
fprintf(stderr,"socketLoop: sem_wait failed for inputBufferEmpty %d\n", result);
exit(EXIT_FAILURE);
}
#ifdef TIMING
starttime=current_timestamp();
reads=0;
#endif
offset=0;
while(offset!=bytestoread) {
count=recv(sockFd, &(inputBuffer[offset/sizeof(short)]), BYTES_PER_FRAME,0);
// fprintf(stderr,"read %d bytes to %d at %p\n",count,offset/sizeof(short), &(inputBuffer[offset/sizeof(short)]));
if(count<=0) {
if(errno != EINTR) {
fprintf(stderr, "socketLoop error reading: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
} else {
offset=offset+count;
}
#ifdef TIMING
reads++;
#endif
}
#ifdef TIMING
endtime=current_timestamp();
fprintf(stderr, "%d in %lld ms %d reads\n", L_SIZE, endtime-starttime, reads);
#endif
// signal input buffer is ready
result=sem_post(&inputBufferFull);
if(result!=0) {
fprintf(stderr,"socketLoop: sem_post failed for inputBufferFull %d\n", result);
exit(EXIT_FAILURE);
}
}
}