ioftools / BGPpysim / policy.py @ de0e8e06
History  View  Annotate  Download (2.58 KB)
1 
'''


2 
Here the policy used by all nodes to compute

3 
the routes' preference is defined...

4 
Change the function if you want to implement a different policy!

5 
The current policy implements exactly the 'binary' policy

6 
of the FabrikantRexford paper

7 

8 
The preference table in the policy function lists the binary labels of paths before

9 
X1 changes the advertisment of d.

10 
 X1 starts advertising d via the AS_PATH = 'X1' which initially costs 11

11 
 Then X1 will advertise d via the AS_PATH = 'P,X1' (prepending), which model the

12 
change of cost between X1 and d. The cost will become 01... the cost of those paths starting with 01

13 
...

14 
'''

15  
16 
#aspath = rxaaspath ^ rxID

17 
from util.routing_table import Route 
18 
import code # code.interact(local=dict(globals(), **locals())) 
19  
20  
21 
def policy(nodeID, route): 
22 
LOWEST_PREF = 0

23 
if 'AS_PATH' not in route.attr: 
24 
return LOWEST_PREF

25 
aspath = route.as_path() + "," + nodeID

26 
state = 'START'

27 
bs = ""

28 
#code.interact(local=dict(globals(), **locals()))

29 
for c in aspath.split(","): 
30 
if state == 'START' and 'X' in c: 
31 
bs += '1'

32 
state = 'A'

33 
elif state == 'START' and 'P' in c: 
34 
state = 'B'

35 
elif state == 'A' and 'X' in c: 
36 
bs += '1'

37 
elif state == 'A' and 'Y' in c: 
38 
state = 'B'

39 
elif state == 'B' and 'X' in c: 
40 
bs += '0'

41 
state = 'C'

42 
elif state == 'C' and 'X' in c: 
43 
bs += '1'

44 
state = 'A'

45 
#print('BS =', bs)

46 
return int('0b'+bs, 2) 
47  
48  
49 
if __name__ == "__main__": 
50 
route = ('dest', {'AS_PATH': 'X1,X2'}) 
51 
while(1): 
52 
cost = policy(route) 
53 
bincost = bin(cost)[2:] 
54 
print("The cost of", route[1]['AS_PATH'], "is:") 
55 
print("DEC =", cost, "\tBIN =", bincost) 
56 
print("insert new AS_PATH to continue.")

57 
route[1]['AS_PATH'] = input() 
58 
#code.interact(local=dict(globals(), **locals()))

59  
60 
'''# Preference Table

61 
prefTab = {

62 
'X1': 11,

63 
'X1,Y1': 10,

64 
'X1,X2': 111,

65 
'X1,X2,Y2': 110,

66 
'X1,Y1,X2': 101,

67 
'X1,Y1,X2,Y2': 100,

68 
'X1,X2,X3': 1111,

69 
'X1,X2,X3,Y3': 1110,

70 
'X1,X2,Y2,X3': 1101,

71 
'X1,X2,Y2,X3,Y3': 1100,

72 
'X1,Y1,X2,X3': 1011,

73 
'X1,Y1,X2,X3,Y3': 1010,

74 
'X1,Y1,X2,Y2,X3': 1001,

75 
'X1,Y1,X2,Y2,X3,Y3': 1000,

76 
}

77 

78 
if not aspath.startswith('X1,X1'):

79 
return int('0b'+str(prefTab[aspath]), 2)

80 
else:

81 
fakepath = ",".join(aspath.split(',')[1:])

82 
fakecost = str(prefTab[fakepath])

83 
#code.interact(local=dict(globals(), **locals()))

84 
return int('0b'+'0'+fakecost[1:], 2)'''
