2 /* Copyright (c) 2007 Kayo Fujiwara. All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "gtnets_simulator.h"
8 #include "gtnets_topology.h"
15 #include "xbt/asserts.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets_simulator, surf_network_gtnets,
18 "Logging specific to the SURF network GTNetS simulator");
23 static vector<void*> meta_flows;
24 static int* meta_nflow;
25 static int meta_flg = 0;
28 void static tcp_sent_callback(void* action, double completion_time);
31 // TODO: check the default values.
36 sim_ = new Simulator();
37 topo_ = new GTNETS_Topology();
38 uniform_jitter_ = NULL;
41 // Set default values.
42 TCP::DefaultAdvWin(wsize);
43 TCP::DefaultSegSize(1000);
44 TCP::DefaultTxBuffer(128000);
45 TCP::DefaultRxBuffer(128000);
48 rm_ = new RoutingManual();
49 Routing::SetRouting(rm_);
54 map<int, Linkp2p*>::iterator it;
55 for (it = gtnets_links_.begin(); it != gtnets_links_.end(); it++){
58 while (!gtnets_links_.empty())
59 gtnets_links_.erase(gtnets_links_.begin());
61 map<int, Node*>::iterator it3;
62 for (it3 = gtnets_nodes_.begin(); it3 != gtnets_nodes_.end(); it3++){
65 while (!gtnets_nodes_.empty())
66 gtnets_nodes_.erase(gtnets_nodes_.begin());
68 map<int, TCPServer*>::iterator it4;
69 for (it4 = gtnets_servers_.begin(); it4 != gtnets_servers_.end(); it4++){
72 while (!gtnets_servers_.empty())
73 gtnets_servers_.erase(gtnets_servers_.begin());
75 map<int, TCPSend*>::iterator it5;
76 for (it5 = gtnets_clients_.begin(); it5 != gtnets_clients_.end(); it5++){
79 while (!gtnets_clients_.empty())
80 gtnets_clients_.erase(gtnets_clients_.begin());
91 int GTSim::add_router(int id){
92 xbt_assert1(!(topo_->add_router(id) < 0), "can't add router %d. already exists", id);
95 //bandwidth: in bytes.
96 //latency: in seconds.
97 int GTSim::add_link(int id, double bandwidth, double latency){
98 double bw = bandwidth * 8; //Bandwidth in bits (used in GTNETS).
99 xbt_assert1(!(topo_->add_link(id) < 0),"Can't add link %d. already exists", id);
100 DEBUG3("Creating a new P2P, linkid %d, bandwidth %gl, latency %gl", id, bandwidth, latency);
101 gtnets_links_[id] = new Linkp2p(bw, latency);
103 DEBUG1("Using jitter %f", jitter_);
105 double max = jitter_*latency;
106 if(uniform_jitter_ == NULL){
107 uniform_jitter_ = new Uniform(min,max);
109 gtnets_links_[id]->Jitter((const Random &) *uniform_jitter_);
114 // if gtnets_nodes_ includes id, return true, otherwise return false.
115 bool GTSim::node_include(int id){
116 if (gtnets_nodes_.find(id) != gtnets_nodes_.end()) return true;
120 // if gtnets_link_ includes id, return true, otherwise return false.
121 bool GTSim::link_include(int id){
122 if (gtnets_links_.find(id) != gtnets_links_.end()) return true;
126 int GTSim::add_onehop_route(int src, int dst, int link){
127 xbt_assert3(!(topo_->add_onehop_route(src, dst, link) < 0), "Cannot add a route, src: %d, dst: %d, link: %d", src, dst, link);
131 // Generate the gtnets nodes according to topo_.
132 void GTSim::add_nodes(){
133 static unsigned int address = IPAddr("192.168.0.1");
134 IPAddr helper = IPAddr();
135 vector<GTNETS_Node*> nodes = topo_->nodes();
136 vector<GTNETS_Node*>::iterator it;
138 for (it = nodes.begin(); it != nodes.end(); it++){
140 gtnets_nodes_[id] = new Node();
141 gtnets_nodes_[id]->SetIPAddr(address++);
142 DEBUG2("In GTSim, add_node: %d, with IPAddr %s", id, helper.ToDotted(address-1));
147 void GTSim::node_connect(){
149 map<int, GTNETS_Link*> links = topo_->links();
150 map<int, GTNETS_Link*>::iterator it;
151 int linkid, srcid, dstid;
152 for (it = links.begin(); it != links.end(); it++){
153 linkid = it->second->id();
154 //if link is used in a route, connect the two nodes.
155 if (it->second->src_node() && it->second->dst_node()){
157 srcid = it->second->src_node()->id();
158 dstid = it->second->dst_node()->id();
160 gtnets_nodes_[srcid]->
161 AddDuplexLink(gtnets_nodes_[dstid], *(gtnets_links_[linkid]));
162 DEBUG3("Setting DuplexLink, src %d, dst %d, linkid %d", srcid, dstid, linkid);
167 // Create nodes and routes from the temporary topology, GTNETS_Topolgy.
168 void GTSim::create_gtnets_topology(){
173 void GTSim::print_topology(){
174 topo_->print_topology();
177 // Add a route that includes more than one hop. All one hop
178 // routes must have been added. When this function is called
179 // for the first time, all gtnets nodes are generated.
180 int GTSim::add_route(int src, int dst, int* links, int nlink){
181 if (is_topology_ == 0){
182 create_gtnets_topology();
186 IPAddr_t mymask = IPAddr("255.255.255.255");
188 int src_node = topo_->nodeid_from_hostid(src);
189 int dst_node = topo_->nodeid_from_hostid(dst);
191 xbt_assert1(!(gtnets_nodes_.find(src_node) == gtnets_nodes_.end()), "Node %d not found", src_node);
192 xbt_assert1(!(gtnets_nodes_.find(dst_node) == gtnets_nodes_.end()), "Node %d not found", dst_node);
194 Node* tmpsrc = gtnets_nodes_[src_node];
195 Node* tmpdst = gtnets_nodes_[dst_node];
197 int next_node, cur_node;
200 for (int i = 0; i < nlink; i++){
201 xbt_assert1(!(gtnets_nodes_.find(cur_node) == gtnets_nodes_.end()), "Node %d not found", cur_node);
202 next_node = topo_->peer_node_id(links[i], cur_node);
203 xbt_assert0(!(next_node < 0), "Peer node not found");
204 xbt_assert1(!(gtnets_nodes_.find(next_node) == gtnets_nodes_.end()), "Node %d not found", next_node);
207 Node* tmpcur = gtnets_nodes_[cur_node];
208 Node* tmpnext = gtnets_nodes_[next_node];
210 tmpcur->AddRoute(tmpdst->GetIPAddr(),
212 tmpcur->GetIfByNode(tmpnext),
213 tmpnext->GetIPAddr());
215 tmpnext->AddRoute(tmpsrc->GetIPAddr(),
217 tmpnext->GetIfByNode(tmpcur),
218 tmpcur->GetIPAddr());
220 cur_node = next_node;
223 xbt_assert2(!(cur_node != dst_node), "Route inconsistency, last: %d, dst: %d",cur_node, dst_node);
230 int GTSim::create_flow(int src, int dst, long datasize, void* metadata){
231 //if no route with more than one links, topology has not been generated.
233 if (is_topology_ == 0){
234 create_gtnets_topology();
238 int src_node = topo_->nodeid_from_hostid(src);
239 xbt_assert1(!(src_node < 0), "Src %d not found", src_node);
241 int dst_node = topo_->nodeid_from_hostid(dst);
242 xbt_assert1(!(dst_node < 0), "Dst %d not found", dst_node);
244 gtnets_servers_[nflow_] = (TCPServer*) gtnets_nodes_[dst_node]->
245 AddApplication(TCPServer(TCPReno()));
246 gtnets_servers_[nflow_]->BindAndListen(1000+nflow_);
248 gtnets_clients_[nflow_] = (TCPSend*)gtnets_nodes_[src_node]->
249 AddApplication(TCPSend(metadata, gtnets_nodes_[dst_node]->GetIPAddr(),
250 1000+nflow_, Constant(datasize), TCPReno()));
251 gtnets_clients_[nflow_]->SetSendCallBack(tcp_sent_callback);
252 gtnets_clients_[nflow_]->Start(0);
254 gtnets_action_to_flow_[metadata] = nflow_;
260 Time_t GTSim::get_time_to_next_flow_completion(){
267 //remain needs to be updated in the future
274 if ( (soon_pid=fork()) != 0){
275 read(pfds[0], &t1, sizeof(Time_t));
276 waitpid(soon_pid, &status, 0);
279 t = sim_->RunUntilNextCompletion();
280 write(pfds[1], (const void*)&t, sizeof(Time_t));
287 double GTSim::gtnets_get_flow_rx(void *metadata){
288 int flow_id = gtnets_action_to_flow_[metadata];
289 return gtnets_servers_[flow_id]->GetTotRx();
292 int GTSim::run_until_next_flow_completion(void ***metadata, int *number_of_flows){
295 meta_nflow = number_of_flows;
298 Time_t t1 = sim_->RunUntilNextCompletion();
300 *metadata = (meta_flows.empty() ? NULL : &meta_flows[0]);
304 int GTSim::run(double delta){
310 void GTSim::set_jitter(double d){
314 void static tcp_sent_callback(void* action, double completion_time){
315 // Schedule the flow complete event.
317 new SimulatorEvent(SimulatorEvent::FLOW_COMPLETE);
318 Simulator::instance->Schedule(e, 0, Simulator::instance);
321 meta_flows.push_back(action);