HostRouter.cc 4.59 KB
Newer Older
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
// HostRouter.cc

/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2003 University of Utah and the Flux Group.
 * All rights reserved.
 */

#include "lib.h"
#include "Exception.h"
#include "HostRouter.h"
#include "Assigner.h"
#include "bitmath.h"
#include "coprocess.h"

using namespace std;

HostRouter::HostRouter()
{
}

HostRouter::~HostRouter()
{
}

auto_ptr<Router> HostRouter::clone(void) const
{
28
29
//    return auto_ptr<Router>(new HostRouter(*this));
    return auto_ptr<Router>(NULL);
30
31
32
33
}

void HostRouter::calculateRoutes(void)
{
34
    if (hosts.size() != 0 && lans.size() != 0)
35
    {
36
        FileWrapper file(coprocess(ROUTECALC).release());
37
        m_tableList.clear();
38
39
        m_tableList.resize(hosts.size());
        for (size_t i = 0; i < hosts.size(); ++i)
40
41
42
        {
            if (isValidNode(i))
            {
43
44
                m_tableList[i].resize(hosts.size());
                for (size_t j = 0; j < hosts[i].size(); ++j)
45
                {
46
47
                    size_t lan = hosts[i][j];
                    for (size_t k = 0; k < lans[lan].hosts.size(); ++k)
48
                    {
49
                        size_t node = lans[lan].hosts[k];
50
51
52
                        if (i != node)
                        {
                            write(file, 'i', i, node,
53
                                  static_cast<float>(lans[lan].weight));
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
                        }
                    }
                }
            }
        }
        write(file, 'C');

        int readCount = 0;
        int source = 0;
        int dest = 0;
        int firstHop = 0;
        int distance = 0;
        readCount = read(file, source, dest, firstHop, distance);
        while (readCount == 4)
        {
            if (isValidNode(source))
            {
                m_tableList[source][dest] = firstHop;
            }
            readCount = read(file, source, dest, firstHop, distance);
        }
    }
}

void HostRouter::print(ostream & output) const
{
80
81
82
83
84
85
86
    // Preprocessing. Figure out which IP addresses to use for all
    // adjascent interfaces. We have the first hop in terms of hosts,
    // and this structure will provide a link from the host to the interface
    // IP address.
    // This maps adjascent nodes to IP addresses.
    map<size_t, IPAddress> nodeToIP;

87
88
89
90
91
    output << "%%" << endl;
    for (size_t i = 0; i < m_tableList.size(); ++i)
    {
        if (m_tableList[i].size() != 0)
        {
92
93
            findAdjascentInterfaces(i, nodeToIP);
            printTable(output, i, nodeToIP);
94
95
96
97
        }
    }
}

98
99
void HostRouter::findAdjascentInterfaces(size_t node,
                                map<size_t, IPAddress> & nodeToIP) const
100
{
101
102
    nodeToIP.clear();
    // in every LAN that we're connected to
103
104
    vector<size_t>::const_iterator lanPos = hosts[node].begin();
    for (; lanPos != hosts[node].end(); ++lanPos)
105
    {
106
        // in every node that is in one of those LANs
107
        vector<size_t> const & firstHopList = hosts[*lanPos];
108
109
        vector<size_t>::const_iterator pos = firstHopList.begin();
        for (; pos != firstHopList.end(); ++pos)
110
        {
111
112
113
114
            // Note that we'll be repeatedly overwriting an IPAddress for
            // the current node. But that is ok since the current node is
            // never used as a firstHop.
            // If this becomes an issue, put a conditional here.
115
            nodeToIP[*pos] = lans[*lanPos].partition->getPrefix() + *pos + 1;
116
117
118
119
120
        }
    }
}


121
122
void HostRouter::printTable(ostream & output, size_t node,
                            map<size_t, IPAddress> & nodeToIP) const
123
{
124
    size_t lanCount = lans.size();
125
126
127
    // Print the routing table itself
    output << "Routing table for node: " << node << endl;
    for (size_t destLan = 0; destLan < lanCount; ++destLan)
128
    {
129
        for (size_t destNodeIndex = 0;
130
             destNodeIndex < lans[destLan].hosts.size();
131
             ++destNodeIndex)
132
        {
133
            size_t destNode = lans[destLan].hosts[destNodeIndex];
134
135

            // if the destination LAN is not connected to the current node.
136
137
138
            if (find(hosts[node].begin(),
                     hosts[node].end(), destLan)
                == hosts[node].end())
139
            {
140
                output << "Destination: "
141
142
                       << ipToString(lans[destLan].partition->getPrefix()
                                     + destNodeIndex + 1)
143
144
145
146
147
                       << "/32" // host routing means no subnets
                       << " FirstHop: "
                       << ipToString(
                           nodeToIP[m_tableList[node][destNode]])
                       << endl;
148
149
150
            }
        }
    }
151
152
    // Use '%%' to divide routing tables from another.
    output << "%%" << endl;
153
}