-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmonitor.c
209 lines (180 loc) · 4.41 KB
/
monitor.c
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
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
/*
Copyright (C) 2010-2014 GRNET S.A.
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <xseg/xseg.h>
#include <sys/time.h>
#include "peer.h"
#define INPUT_BUF_SIZE 256
#define MAX_NR_ARGS 100
struct monitord {
uint32_t mon_portno;
};
struct monitor_io {
uint32_t src_portno;
void *src_priv;
};
void custom_peer_usage()
{
return;
}
static int forward(struct peerd *peer, struct peer_req *pr)
{
int r;
r = submit_peer_req(peer, pr);
if (r < 0) {
printf("couldn't forward request");
return -1;
}
return 0;
}
static int complete_forwarded(struct peerd *peer, struct peer_req *pr)
{
struct xseg_request *req = pr->req;
// assert mio->src_portno != NoPort
if (req->state & XS_SERVED)
complete(peer, pr);
else if (req->state & XS_FAILED)
fail (peer, pr);
else {
printf("invalid state\n");
return -1;
}
return 0;
}
int dispatch(struct peerd *peer, struct peer_req *pr, struct xseg_request *xreq,
enum dispatch_reason reason)
{
struct xseg_request *req = pr->req;
if (req->state & (XS_SERVED | XS_FAILED)){
log_pr("completing", pr);
complete_forwarded(peer, pr);
}
else {
log_pr("forwarding", pr);
forward(peer,pr);
}
return 0;
}
int mpause(struct peerd *peer)
{
struct xseg *xseg = peer->xseg;
struct xseg_port *port = xseg_get_port(xseg, peer->portno_start);
if (!port)
return -1;
xlock_acquire(&port->rq_lock);
xlock_acquire(&port->pq_lock);
return 0;
}
int munpause(struct peerd *peer)
{
struct xseg *xseg = peer->xseg;
struct xseg_port *port = xseg_get_port(xseg, peer->portno_start);
if (!port)
return -1;
xlock_release(&port->rq_lock);
xlock_release(&port->pq_lock);
return 0;
}
struct peerd *main_peer;
void main_loop(void)
{
int ret;
struct peerd * peer = main_peer;
char buf[INPUT_BUF_SIZE];
char *nl;
unsigned int portno = NoPort, dstgw, srcgw;
for (;;){
printf("waitin next line\n");
if (fgets(buf, INPUT_BUF_SIZE, stdin)) {
nl = strchr(buf, '\n');
if (nl)
*nl = 0;
buf[INPUT_BUF_SIZE -1] = 0;
printf("got line input\n");
ret = sscanf(buf, "set srcgw %u %u", &portno, &srcgw);
if (ret == 2){
printf("found setsrcgw\n");
xseg_set_srcgw(peer->xseg, (uint32_t) portno, (uint32_t) srcgw);
continue;
};
ret = sscanf(buf, "set dstgw %u %u", &portno, &dstgw);
if (ret == 2){
printf("found set dstgw\n");
xseg_set_dstgw(peer->xseg, (uint32_t) portno, (uint32_t) dstgw);
continue;
};
ret = sscanf(buf, "getandset srcgw %u %u", &portno, &srcgw);
if (ret == 2){
printf("found getand set srcgw\n");
xseg_getandset_srcgw(peer->xseg, (uint32_t) portno, (uint32_t) srcgw);
continue;
};
ret = sscanf(buf, "getandset dstgw %u %u", &portno, &dstgw);
if (ret == 2){
printf("found getandset dstgw\n");
xseg_getandset_dstgw(peer->xseg, (uint32_t) portno, (uint32_t) dstgw);
continue;
};
ret = sscanf(buf, "pause %u", &portno);
if (ret == 1){
printf("found pause\n");
mpause(peer);
continue;
};
ret = sscanf(buf, "unpause %u", &portno);
if (ret == 1){
printf("found unpause\n");
munpause(peer);
continue;
};
}
else
exit(0);
}
}
int custom_peer_init(struct peerd *peer, int argc, char *argv[])
{
int i;
struct monitor_io *mio;
struct monitord *monitor;
monitor = malloc(sizeof(struct monitord));
if (!monitor)
return -1;
peer->priv = monitor;
monitor->mon_portno = NoPort;
for (i = 0; i < peer->nr_ops; i++) {
mio = malloc(sizeof(struct monitor_io));
if (!mio)
return -1;
peer->peer_reqs[i].priv = mio;
mio->src_portno = NoPort;
}
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-mp") && (i + 1 < argc)) {
monitor->mon_portno = atoi(argv[i+1]);
i+=1;
continue;
}
}
main_peer = peer;
peer->interactive_func = main_loop;
return 0;
}
void custom_peer_finalize(struct peerd *peer)
{
return;
}