howto/.ipynb_checkpoints/Bird-communities-checkpoint.md
... ...
@@ -0,0 +1,210 @@
1
+Bird is a commonly used BGP daemon. This page provides configuration and help for using BGP communities with Bird for dn42.
2
+
3
+Communities can be used to prioritize traffic based on different flags, in DN42 we are using communities to prioritize based on latency, bandwidth and encryption. Please note that everyone should be using community 64511.
4
+
5
+The community is applied to the route when it is imported and exported, therefore you need to change your bird configuration, in /etc/bird/peers4 if you followed the Bird guide.
6
+
7
+The filter helpers can be stored in a separate file, for example /etc/bird/community_filters.conf.
8
+
9
+Below, you will see an example config for peers4 based on the original filter implementation by Jplitza.
10
+
11
+To properly assign the right community to your peer, please reference the table below. If you are running your own network and peering internally, please also apply the communities inside your network.
12
+
13
+## BGP community criteria
14
+```
15
+(64511, 1) :: latency \in (0, 2.7ms]
16
+(64511, 2) :: latency \in (2.7ms, 7.3ms]
17
+(64511, 3) :: latency \in (7.3ms, 20ms]
18
+(64511, 4) :: latency \in (20ms, 55ms]
19
+(64511, 5) :: latency \in (55ms, 148ms]
20
+(64511, 6) :: latency \in (148ms, 403ms]
21
+(64511, 7) :: latency \in (403ms, 1097ms]
22
+(64511, 8) :: latency \in (1097ms, 2981ms]
23
+(64511, 9) :: latency > 2981ms
24
+(64511, x) :: latency \in [exp(x-1), exp(x)] ms (for x < 10)
25
+
26
+(64511, 21) :: bw >= 0.1mbit
27
+(64511, 22) :: bw >= 1mbit
28
+(64511, 23) :: bw >= 10mbit
29
+(64511, 24) :: bw >= 100mbit
30
+(64511, 25) :: bw >= 1000mbit
31
+(64511, 2x) :: bw >= 10^(x-2) mbit
32
+bw = min(up,down) for asymmetric connections
33
+
34
+(64511, 31) :: not encrypted
35
+(64511, 32) :: encrypted with unsafe vpn solution
36
+(64511, 33) :: encrypted with safe vpn solution (but no PFS - the usual OpenVPN p2p configuration falls in this category)
37
+(64511, 34) :: encrypted with safe vpn solution with PFS (Perfect Forward Secrecy)
38
+
39
+Propagation:
40
+- - for latency pick max(received_route.latency, link_latency)
41
+- - for encryption and bandwidth pick min between received BGP community and peer link
42
+```
43
+For example, if your peer is 12ms away and the link speed between you is 250Mbit/s and you are peering using OpenVPN P2P, then the community string would be (3, 24, 33).
44
+
45
+Two utilites which measure round trip time and calculate community values automatically are provided, written in [ruby](https://github.com/Mic92/bird-dn42/blob/master/bgp-community.rb) and [C](https://github.com/nixnodes/bird/blob/master/misc/dn42-comgen.c).
46
+
47
+```
48
+$ ruby bgp-community.rb --help
49
+USAGE: bgp-community.rb host mbit_speed unencrypted|unsafe|encrypted|pfs
50
+ -6, --ipv6 Assume ipv6 for ping
51
+$ ruby bgp-community.rb 212.129.13.123 300 encrypted
52
+ # 15 ms, 300 mbit/s, encrypted tunnel (updated: 2016-02-11)
53
+ import where dn42_import_filter(3,24,33);
54
+ export where dn42_export_filter(3,24,33);
55
+$ ruby bgp-community.rb -6 dn42-2.higgsboson.tk 1000 pfs
56
+ # 11 ms, 1000 mbit/s, pfs tunnel (updated: 2016-02-11)
57
+ import where dn42_import_filter(3,25,34);
58
+ export where dn42_export_filter(3,25,34);
59
+```
60
+
61
+### Route Origin
62
+There are two type of route origin: `region` and `country`
63
+
64
+#### Region
65
+The range `41~70` are assgined to the region property.
66
+According to [this mail](https://lists.nox.tf/pipermail/dn42/2015-December/001259.html) these are the communities for route origin(region):
67
+```
68
+(64511, 41) :: Europe
69
+(64511, 42) :: North America-E
70
+(64511, 43) :: North America-C
71
+(64511, 44) :: North America-W
72
+(64511, 45) :: Central America
73
+(64511, 46) :: South America-E
74
+(64511, 47) :: South America-W
75
+(64511, 48) :: Africa-N (above Sahara)
76
+(64511, 49) :: Africa-S (below Sahara)
77
+(64511, 50) :: Asia-S (IN,PK,BD)
78
+(64511, 51) :: Asia-SE (TH,SG,PH,ID,MY)
79
+(64511, 52) :: Asia-E (JP,CN,KR)
80
+(64511, 53) :: Pacific&Oceania (AU,NZ,FJ)
81
+(64511, 54) :: Antarctica
82
+(64511, 55) :: Asia-N (RU)
83
+(64511, 56) :: Asia-W (IR,TR,UAE)
84
+(64511, 57) :: Central Asia (AF,UZ,KZ)
85
+```
86
+
87
+#### Country
88
+The range `1000~1999` are assigned to the country property, we uses the country code from [ISO-3166-1 Numeric](https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.csv)
89
+The `1000 + country code` are the communities for route origin(country).
90
+```
91
+(64511, 1124) :: Canada
92
+(64511, 1156) :: China
93
+(64511, 1158) :: Taiwan
94
+(64511, 1250) :: France
95
+(64511, 1276) :: Germany
96
+(64511, 1344) :: Hong Kong
97
+(64511, 1392) :: Japan
98
+(64511, 1528) :: Netherlands
99
+(64511, 1578) :: Norway
100
+(64511, 1643) :: Russian Federation
101
+(64511, 1702) :: Singapore
102
+(64511, 1756) :: Switzerland
103
+(64511, 1826) :: United Kingdom
104
+(64511, 1840) :: United States of America
105
+# etc. Please follow the ISO-3166-1 Numeric standard
106
+# https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes/blob/master/all/all.csv
107
+```
108
+
109
+You need to add following lines to your config(s):
110
+- `define DN42_REGION = $VALUE_FROM_ABOVE` to your node's config (where OWNAS and OWNIP are set)
111
+- `if source = RTS_STATIC then bgp_community.add((64511, DN42_REGION));`
112
+just above `update_flags` in `dn42_export_filter` function
113
+- Unlike the other community values, **the DN42_REGION community should only be set on routes originating from your network!** (This is what the `source = RTS_STATIC` check does).
114
+ - Otherwise, if you export routes across multiple regions within your network, you may be sending incorrect origin information to other peers.
115
+
116
+
117
+## Example configurations
118
+```
119
+# /etc/bird/peers4/tombii.conf
120
+protocol bgp tombii from dnpeers {
121
+ neighbor 172.23.102.x as 4242420321;
122
+ import where dn42_import_filter(3,24,33);
123
+ export where dn42_export_filter(3,24,33);
124
+};
125
+```
126
+```
127
+#/etc/bird/community_filters.conf
128
+function update_latency(int link_latency) {
129
+ bgp_community.add((64511, link_latency));
130
+ if (64511, 9) ~ bgp_community then { bgp_community.delete([(64511, 1..8)]); return 9; }
131
+ else if (64511, 8) ~ bgp_community then { bgp_community.delete([(64511, 1..7)]); return 8; }
132
+ else if (64511, 7) ~ bgp_community then { bgp_community.delete([(64511, 1..6)]); return 7; }
133
+ else if (64511, 6) ~ bgp_community then { bgp_community.delete([(64511, 1..5)]); return 6; }
134
+ else if (64511, 5) ~ bgp_community then { bgp_community.delete([(64511, 1..4)]); return 5; }
135
+ else if (64511, 4) ~ bgp_community then { bgp_community.delete([(64511, 1..3)]); return 4; }
136
+ else if (64511, 3) ~ bgp_community then { bgp_community.delete([(64511, 1..2)]); return 3; }
137
+ else if (64511, 2) ~ bgp_community then { bgp_community.delete([(64511, 1..1)]); return 2; }
138
+ else return 1;
139
+}
140
+
141
+function update_bandwidth(int link_bandwidth) {
142
+ bgp_community.add((64511, link_bandwidth));
143
+ if (64511, 21) ~ bgp_community then { bgp_community.delete([(64511, 22..29)]); return 21; }
144
+ else if (64511, 22) ~ bgp_community then { bgp_community.delete([(64511, 23..29)]); return 22; }
145
+ else if (64511, 23) ~ bgp_community then { bgp_community.delete([(64511, 24..29)]); return 23; }
146
+ else if (64511, 24) ~ bgp_community then { bgp_community.delete([(64511, 25..29)]); return 24; }
147
+ else if (64511, 25) ~ bgp_community then { bgp_community.delete([(64511, 26..29)]); return 25; }
148
+ else if (64511, 26) ~ bgp_community then { bgp_community.delete([(64511, 27..29)]); return 26; }
149
+ else if (64511, 27) ~ bgp_community then { bgp_community.delete([(64511, 28..29)]); return 27; }
150
+ else if (64511, 28) ~ bgp_community then { bgp_community.delete([(64511, 29..29)]); return 28; }
151
+ else return 29;
152
+}
153
+
154
+function update_crypto(int link_crypto) {
155
+ bgp_community.add((64511, link_crypto));
156
+ if (64511, 31) ~ bgp_community then { bgp_community.delete([(64511, 32..34)]); return 31; }
157
+ else if (64511, 32) ~ bgp_community then { bgp_community.delete([(64511, 33..34)]); return 32; }
158
+ else if (64511, 33) ~ bgp_community then { bgp_community.delete([(64511, 34..34)]); return 33; }
159
+ else return 34;
160
+}
161
+
162
+function update_flags(int link_latency; int link_bandwidth; int link_crypto)
163
+int dn42_latency;
164
+int dn42_bandwidth;
165
+int dn42_crypto;
166
+{
167
+ dn42_latency = update_latency(link_latency);
168
+ dn42_bandwidth = update_bandwidth(link_bandwidth) - 20;
169
+ dn42_crypto = update_crypto(link_crypto) - 30;
170
+ # replace 4 with your calculated bandwidth value
171
+ if dn42_bandwidth > 4 then dn42_bandwidth = 4;
172
+ return true;
173
+}
174
+
175
+# Combines filter from local4.conf/local6.conf and filter4.conf/filter6.conf,
176
+# which means, these must included before this file
177
+
178
+function dn42_import_filter(int link_latency; int link_bandwidth; int link_crypto) {
179
+ if is_valid_network() && !is_self_net() then {
180
+ update_flags(link_latency, link_bandwidth, link_crypto);
181
+ accept;
182
+ }
183
+ reject;
184
+}
185
+
186
+function dn42_export_filter(int link_latency; int link_bandwidth; int link_crypto) {
187
+ if is_valid_network() then {
188
+ update_flags(link_latency, link_bandwidth, link_crypto);
189
+ accept;
190
+ }
191
+ reject;
192
+}
193
+
194
+```
195
+Please remember to include /etc/bird/community_filters.conf in your bird.conf/birdc6.conf
196
+```
197
+
198
+# local configuration
199
+######################
200
+include "bird/local4.conf";
201
+
202
+# filter helpers
203
+#################
204
+
205
+include "/etc/bird/filter4.conf";
206
+include "/etc/bird/community_filters.conf";
207
+```
208
+
209
+
210
+***
howto/Bird-communities.md
... ...
@@ -77,7 +77,7 @@ The communities for route origin region were first defined in [December 2015](ht
77 77
(64511, 49) :: Africa-S (below Sahara)
78 78
(64511, 50) :: Asia-S (IN,PK,BD)
79 79
(64511, 51) :: Asia-SE (TH,SG,PH,ID,MY)
80
-(64511, 52) :: Asia-E (JP,CN,KR)
80
+(64511, 52) :: Asia-E (JP,CN,KR,TW,HK)
81 81
(64511, 53) :: Pacific&Oceania (AU,NZ,FJ)
82 82
(64511, 54) :: Antarctica
83 83
(64511, 55) :: Asia-N (RU)