-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeacon.cpp
More file actions
106 lines (90 loc) · 3.21 KB
/
Copy pathbeacon.cpp
File metadata and controls
106 lines (90 loc) · 3.21 KB
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
/* Switch-to-switch link discovery */
#include "beacon.h"
#include <arpa/inet.h>
#include <cstdio>
#include <cstring>
#include <set>
#include "event.h"
#include "god.h"
#include "openflow.h"
// Set of outstanding polls, so we don't fire more than one event per poll
typedef std::map<uint32_t, std::pair<uint64_t, uint32_t> > outstanding_t;
static outstanding_t outstanding;
static void send_polls_event(void*);
static void send_poll(Client*, uint32_t, uint64_t);
static void poll_timeout(void*);
void send_polls(Client* client) {
Server* server = (Server*)client->server;
std::set<uint32_t>::iterator it;
for (it = client->ports.begin(); it != client->ports.end(); it++) {
send_poll(client, *it, 750);
}
server->schedule_event(1000, send_polls_event, (void*)client->uid);
}
void send_polls_event(void* arg) {
Client* client = client_table[(uint64_t)arg];
if (client != nullptr) {
send_polls(client);
}
}
void send_poll(Client* client, uint32_t port, uint64_t timeout) {
static uint64_t poll_id = 0;
Server* server = (Server*)client->server;
poll_id++;
outstanding.insert(std::pair<uint32_t, std::pair<uint64_t, uint32_t> >(
(uint32_t)poll_id, std::pair<uint64_t, uint32_t>(client->uid, port)));
switch_poll_t beacon;
memcpy(beacon.magic, SWITCH_POLL_MAGIC, 6);
beacon.poll_id = htonl((uint32_t)poll_id);
beacon.uid1 = htonl(client->uid >> 32);
beacon.uid2 = htonl((uint32_t)client->uid);
beacon.port_id = htonl(port);
send_packet_out(client, port, &beacon, sizeof(beacon));
server->schedule_event(timeout, poll_timeout, (void*)poll_id);
}
void recv_poll(Client* client, uint32_t port, const uint8_t* data) {
Server* server = (Server*)client->server;
Graph* graph = &server->graph;
switch_poll_t beacon;
memcpy(&beacon, data, sizeof(beacon));
uint32_t poll_id = ntohl(beacon.poll_id);
uint64_t from_uid =
((uint64_t)ntohl(beacon.uid1) << 32) | ntohl(beacon.uid2);
uint32_t from_port = ntohl(beacon.port_id);
if (outstanding.erase(poll_id) == 0) {
// Poll has already been handled (timeout fired or duplicate frame)
return;
} else if (graph->has_edge(from_uid, from_port, client->uid, port)) {
// Edge exists, do nothing
return;
}
graph->add_edge(from_uid, from_port, client->uid, port);
god_function(server);
}
void poll_timeout(void* arg) {
uint64_t poll_id = (uint64_t)arg;
outstanding_t::iterator it = outstanding.find((uint32_t)poll_id);
if (it == outstanding.end()) {
// Poll has already been handled (timeout fired or duplicate frame)
return;
}
uint64_t uid = it->second.first;
uint32_t port = it->second.second;
outstanding.erase(it);
Client* client = client_table[uid];
if (client == nullptr) {
// The client may have been removed
return;
}
port_down(client, port);
}
void port_down(Client* client, uint32_t port) {
Server* server = (Server*)client->server;
Graph* graph = &server->graph;
if (!graph->has_any_edge(client->uid, port)) {
// Edge wasn't there to begin with, do nothing
return;
}
graph->remove_edge(client->uid, port);
god_function(server);
}