blob: b05d69735e341c5f7418ae1b720db10b80db47f4 [file] [log] [blame]
Saurab Dulal8ae870a2018-07-31 05:17:49 +00001 # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015-2019, The University of Memphis
4#
5# This file is part of Mini-NDN.
6# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
7#
8# Mini-NDN is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# Mini-NDN is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with Mini-NDN, e.g., in COPYING.md file.
20# If not, see <http://www.gnu.org/licenses/>.
21
22# IMPORTANT! This feature is in highly experimental phase and may go several changes
23# in future
24
25from mininet.log import info
26from ndn.apps.nfdc import Nfdc as nfdc
27from collections import defaultdict
28from ndn.apps.calculate_routing import CalculateRoutes
29from mininet.log import warn
30
31class GlobalRoutingHelper():
32 """
33 This module is a helper class which helps to create face and register routes
34 to NFD from a given node to all of its neighbors.
35
36 :param NetObject netObject: Mininet net object
37 :param FaceType faceType: UDP, Ethernet etc.
38 :param Routing routingType: (optional) Routing algorithm, link-state or hr etc
39
40 """
41 def __init__(self, netObject, faceType=nfdc.PROTOCOL_UDP, routingType="link-state"):
42 self.net = netObject
43 self.faceType = faceType
44 self.routingType = routingType
45 self.routes = []
46 self.namePrefixes = {host_name.name: [] for host_name in self.net.hosts}
47 self.routeObject = CalculateRoutes(self.net, self.routingType)
48
49 def globalRoutingHelperHandler(self):
50 for host in self.net.hosts:
51 neighborIPs = self.getNeighbor(host)
52 self.createFaces(host, neighborIPs)
53 self.routeAdd(host, neighborIPs)
54
55 info('Processed all the routes to NFD\n')
56
57 def addOrigin(self, nodes, prefix):
58 """
59 Add prefix/s as origin on node/s
60
61 :param Prefix prefix: Prefix that is originated by node/s (as producer) for this prefix
62 :param Nodes nodes: List of nodes from net object
63 """
64 for node in nodes:
65 self.namePrefixes[node.name] = prefix
66
67 def calculateNPossibleRoutes(self, nFaces=0):
68 """
69 By default, calculates all possible routes i.e. routes via all the faces of a node.
70 pass nFaces if want to compute routes via n number of faces. e.g. 2. For larger topology
71 the computation might take huge amount of time.
72
73 :param int nFaces: (optional) number of faces to consider while computing routes. Default
74 i.e. nFaces = 0 will compute all possible routes
75
76 """
77 self.routes = self.routeObject.getRoutes(nFaces)
78 if self.routes:
79 self.globalRoutingHelperHandler()
80 else:
81 warn("Route computation failed\n")
82
83 def calculateRoutes(self):
84 # Calculate shortest path for every node
85 calculateNPossibleRoutes(self, nFaces=1)
86
87 def createFaces(self, node, neighborIPs):
88 for ip in neighborIPs.values():
89 nfdc.createFace(node, ip, self.faceType)
90
91 def routeAdd(self, node, neighborIPs):
92 """
93 Add route from a node to its neighbors for each prefix/s advertised by destination node
94
95 :param Node node: source node (Mininet net.host)
96 :param IP neighborIPs: IP addresses of neighbors
97 """
98 neighbors = self.routes[node.name]
99 for route in neighbors:
100 destination = route[0]
101 cost = int(route[1])
102 nextHop = route[2]
103 defaultPrefix = "/ndn/{}-site/{}".format(destination, destination)
104 prefixes = [defaultPrefix] + self.namePrefixes[destination]
105 for prefix in prefixes:
106 # Register routes to all the available destination name prefix/s
107 nfdc.registerRoute(node, prefix, neighborIPs[nextHop], \
108 nfdc.PROTOCOL_UDP, cost=cost)
109 @staticmethod
110 def getNeighbor(node):
111 # Nodes to IP mapping
112 neighborIPs = defaultdict()
113 for intf in node.intfList():
114 link = intf.link
115 if link:
116 node1, node2 = link.intf1.node, link.intf2.node
117
118 if node1 == node:
119 other = node2
120 ip = other.IP(str(link.intf2))
121 else:
122 other = node1
123 ip = other.IP(str(link.intf1))
124
125 # Used later to create faces
126 neighborIPs[other.name] = ip
127 return neighborIPs