parse_ptop.cc 4.57 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
3
4
5
6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

Mac Newbold's avatar
Mac Newbold committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * parse ptop files.  These are basic topologies
 * that are used to represent the physical topology.
 *
 * format:
 * node <name> <type> [<type2> ...]
 *   <type> = <t>[:<max>]
 *   <t>    = pc | switch | dnard
 *   <max>  = how many virtual entities of that type per physical entity.
 * link <name> <src>[:<mac>] <dst>[:<mac>] <size> <number>
 */

#include <LEDA/graph_alg.h>
#include <LEDA/graphwin.h>
21
#include <LEDA/ugraph.h>
Mac Newbold's avatar
Mac Newbold committed
22
23
24
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
25
#include <LEDA/sortseq.h>
Mac Newbold's avatar
Mac Newbold committed
26
27
28
29
30
31
32
#include <iostream.h>
#include <string.h>
#include <stdio.h>

#include "common.h"
#include "physical.h"

33
extern dictionary<string,node> pname2node;
Mac Newbold's avatar
Mac Newbold committed
34
35
extern node pnodes[MAX_PNODES];	// int -> node map
node_array<int> switch_index;
36
extern list<string> ptypes;
Mac Newbold's avatar
Mac Newbold committed
37

38
int parse_ptop(tb_pgraph &PG, tb_sgraph &SG, istream& i)
Mac Newbold's avatar
Mac Newbold committed
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
{
  int switchi=0;
  node no1;
  edge ed1;
  string s1, s2;
  char inbuf[255];
  char n1[32], n2[32];
  int size, num;
  int n=1;
  char *snext;
  char *snode;
  char *scur;
  char lname[32];
  int isswitch;

  switch_index.init(PG,MAX_PNODES,0);
  pnodes[0] = NULL;
  while (!i.eof()) {
    char *ret;
    i.getline(inbuf, 254);
    ret = strchr(inbuf, '\n');
    if (ret) *ret = 0;
    if (strlen(inbuf) == 0) { continue; }
    
    if (!strncmp(inbuf, "node", 4)) {
      isswitch = 0;
      snext = inbuf;
      scur = strsep(&snext," ");
      if (strcmp("node",scur) != 0) {
	fprintf(stderr, "bad node line: %s\n", inbuf);
      } else {
	scur = strsep(&snext," ");
	snode = scur;
	string s(snode);
	no1 = PG.new_node();
74
75
76
#ifdef GRAPH_DEBUG
	cout << "Found phys. node '"<<snode<<"'\n";
#endif
77
	PG[no1].name=string(snode);
Mac Newbold's avatar
Mac Newbold committed
78
79
80
81
	PG[no1].typed = false;
	PG[no1].max_load = 0;
	PG[no1].current_load = 0;
	PG[no1].pnodes_used=0;
82
83
	while ((scur = strsep(&snext," ")) != NULL &&
	       (strcmp(scur,"-"))) {
Mac Newbold's avatar
Mac Newbold committed
84
85
86
87
88
89
90
91
92
93
94
95
	  char *t,*load=scur;
	  int iload;
	  t = strsep(&load,":");
	  string stype(t);
	  if (load) {
	    if (sscanf(load,"%d",&iload) != 1) {
	      fprintf(stderr,"Bad load specifier: %s\n",load);
	      iload=1;
	    }
	  } else {
	    iload=1;
	  }
96
	  ptypes.push(stype);
Mac Newbold's avatar
Mac Newbold committed
97
98
99
100
	  if (strcmp(t,"switch") == 0) {
	    isswitch = 1;
	    PG[no1].types.insert(stype,1);
	    PG[no1].the_switch = no1;
101
102
103
	    node sw = SG.new_node();
	    SG[sw].mate = no1;
	    PG[no1].sgraph_switch = sw;
Mac Newbold's avatar
Mac Newbold committed
104
105
106
107
108
	    switch_index[no1] = switchi++;
	  } else {
	    PG[no1].types.insert(stype,iload);
	  }
	}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	/* Either end of line or - .  Read in features */
	while ((scur = strsep(&snext," ")) != NULL) {
	  char *feature=scur;
	  double icost;
	  char *t;
	  t = strsep(&feature,":");
	  string sfeat(t);
	  if ((! feature) || sscanf(feature,"%lg",&icost) != 1) {
	    fprintf(stderr,"Bad cost specifier for %s\n",t);
	    icost = 0.01;
	  }
	  PG[no1].features.insert(sfeat,icost);
	}
	/* Done */
Mac Newbold's avatar
Mac Newbold committed
123
124
	if (! isswitch)
	  pnodes[n++]=no1;
125
	pname2node.insert(s, no1);
Mac Newbold's avatar
Mac Newbold committed
126
127
128
129
130
131
132
      }
    }
    else if (!strncmp(inbuf, "link", 4)) {
      if (sscanf(inbuf, "link %s %s %s %d %d", lname, n1, n2, &size, &num)
	  != 5) {
	fprintf(stderr, "bad link line: %s\n", inbuf);
      } else {
133
	string linkname(lname);
Mac Newbold's avatar
Mac Newbold committed
134
135
136
137
138
139
	char *snode,*smac;
	char *dnode,*dmac;
	smac = n1;
	dmac = n2;
	snode = strsep(&smac,":");
	dnode = strsep(&dmac,":");
140
	if (pname2node.lookup(snode) == nil) {
Mac Newbold's avatar
Mac Newbold committed
141
142
143
	  fprintf(stderr,"PTOP error: Unknown source node %s\n",snode);
	  exit(1);
	}
144
	if (pname2node.lookup(dnode) == nil) {
Mac Newbold's avatar
Mac Newbold committed
145
146
147
	  fprintf(stderr,"PTOP error: Unknown destination node %s\n",dnode);
	  exit(1);
	}
148
149
	node node1 = pname2node.access(snode);
	node node2 = pname2node.access(dnode);
150
#define ISSWITCH(n) (PG[n].types.lookup("switch") != nil)
Mac Newbold's avatar
Mac Newbold committed
151
152
153
154
	for (int i = 0; i < num; ++i) {
	  ed1=PG.new_edge(node1, node2);
	  PG[ed1].bandwidth=size;
	  PG[ed1].bw_used=0;
155
	  PG[ed1].name=linkname;
156
157
	  PG[ed1].emulated=0;
	  PG[ed1].nonemulated=0;
Mac Newbold's avatar
Mac Newbold committed
158
	  if (smac)
159
	    PG[ed1].srcmac = string(smac);
Mac Newbold's avatar
Mac Newbold committed
160
	  else
161
	    PG[ed1].srcmac = string("(null)");
Mac Newbold's avatar
Mac Newbold committed
162
	  if (dmac)
163
	    PG[ed1].dstmac = string(dmac);
Mac Newbold's avatar
Mac Newbold committed
164
	  else
165
166
167
168
169
170
171
172
173
174
175
	    PG[ed1].dstmac = string("(null)");
	  if (ISSWITCH(node1) && ISSWITCH(node2)) {
	    if (i != 0) {
	      cout <<
		"Warning: Extra links between switches will be ignored." <<
		endl;
	    }
	    edge swedge = SG.new_edge(PG[node1].sgraph_switch,
				      PG[node2].sgraph_switch);
	    SG[swedge].mate = ed1;
	  }
Mac Newbold's avatar
Mac Newbold committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	}
	if (ISSWITCH(node1) &&
	    ! ISSWITCH(node2))
	  PG[node2].the_switch = node1;
	else if (ISSWITCH(node2) &&
		 ! ISSWITCH(node1))
	  PG[node1].the_switch = node2;
      }
    } else {
      fprintf(stderr, "unknown directive: %s\n", inbuf);
    }
  }
  return n-1;
}