Compare commits
841 Commits
v1.6.7
...
burble.dn4
Author | SHA1 | Date | |
---|---|---|---|
256e198fee | |||
18853a82c6 | |||
e6133456c1 | |||
b86d3f9a2e | |||
910f9127f0 | |||
ce7a2736e9 | |||
397c52070a | |||
|
4aecc5f8cd | ||
|
3155bb34be | ||
|
82f19ba95e | ||
|
f1ffe6a231 | ||
|
5a6e8380f8 | ||
|
454ae30445 | ||
|
0a3db4c680 | ||
|
2f98153490 | ||
|
ae9ae864d3 | ||
|
94abefc00b | ||
|
0d1a11cca3 | ||
|
6489a2450e | ||
|
7be3af7fa6 | ||
|
9cf3d53311 | ||
|
211fe69c98 | ||
|
d3782c72b9 | ||
|
77ce849ecf | ||
|
714238716e | ||
|
00b85905b9 | ||
|
d06a875b04 | ||
|
5d414309ec | ||
|
df83f62697 | ||
|
a40ddf5c61 | ||
|
d774f6d721 | ||
|
910adaa08b | ||
|
17663b6a7c | ||
|
2a8cc7259e | ||
|
a141959f07 | ||
|
7a1f4baac1 | ||
|
4155104c90 | ||
|
21f9acd2a0 | ||
|
455c13dc99 | ||
|
ea3c6c1a15 | ||
|
9e2635505a | ||
|
61dae32b29 | ||
|
8cc5bb09e3 | ||
|
2465867712 | ||
|
62d57b9bdf | ||
|
0ef082c51e | ||
|
30b8468269 | ||
|
1678bc0746 | ||
|
c9ae81656f | ||
|
db2d29073a | ||
|
3347aaafec | ||
|
df65d519d6 | ||
|
00ddd18b02 | ||
|
6ea8a46ccb | ||
|
b962967e20 | ||
|
4a42e7e925 | ||
|
3b56bf8849 | ||
|
99ad208dd7 | ||
|
9d3fc3062b | ||
|
fc1e3211b1 | ||
|
6c11dbcf28 | ||
|
14ce8904e7 | ||
|
600eb695b1 | ||
|
dc8d9dec4a | ||
|
c0e1f534c9 | ||
|
c26c6bc2d7 | ||
|
a948cf9a5c | ||
|
4ef0a96639 | ||
|
82937b465b | ||
|
71e08edd94 | ||
|
63451c1961 | ||
|
f1b5f179db | ||
|
19f8f17320 | ||
|
fae5448134 | ||
|
eee8af4db2 | ||
|
4e8f8afc68 | ||
|
c1632ad0f3 | ||
|
1ca7665fa4 | ||
|
b729e731f9 | ||
|
ec430a7fee | ||
|
5fc8407177 | ||
|
b8bbbbaf56 | ||
|
f7c34aa227 | ||
|
e6785c469b | ||
|
82bfee76f0 | ||
|
b12442c985 | ||
|
048eb2ddf1 | ||
|
59238768b3 | ||
|
ea259d6201 | ||
|
0c3b8ffe25 | ||
|
cdde3550dc | ||
|
9ac13d7af2 | ||
|
e26a5195dd | ||
|
3bb10b4d31 | ||
|
258be56539 | ||
|
a7d9b8f116 | ||
|
0fa8bf91cd | ||
|
bbe49ae569 | ||
|
a08853a269 | ||
|
5f60d14ede | ||
|
b748220906 | ||
|
9e64ac4b7c | ||
|
dccee40826 | ||
|
baac700906 | ||
|
a0d0a71a18 | ||
|
a1b61a271a | ||
|
d65a926a67 | ||
|
30ba7c1661 | ||
|
bf9486bf20 | ||
|
17de3a023f | ||
|
8029ae527e | ||
|
d607205486 | ||
|
124d860f64 | ||
|
59a86cbc7c | ||
|
b465604eb1 | ||
|
716e11a584 | ||
|
3c838ad9fd | ||
|
a6548d5b5b | ||
|
fd9f0c0640 | ||
|
a109056145 | ||
|
2928c5bcc7 | ||
|
c9d11e6230 | ||
|
2755002890 | ||
|
d516c68ad8 | ||
|
dc042d87cb | ||
|
e2630a494e | ||
|
1ad98965c5 | ||
|
ead531ffef | ||
|
e6746da6de | ||
|
78e4a123bb | ||
|
757cab18d6 | ||
|
22c3cf955d | ||
|
3343088a71 | ||
|
ab089f4fb5 | ||
|
027a3e66f7 | ||
|
4bbc10614f | ||
|
7f9adafc10 | ||
|
9f2670277c | ||
|
7d767c5a3d | ||
|
ef8c45749c | ||
|
cc75b3e1dc | ||
|
90a9c97e38 | ||
|
3dabf7b8d0 | ||
|
3232d17186 | ||
|
c132acae36 | ||
|
d3aa4f2aed | ||
|
dfb3eb7716 | ||
|
4ab54f1aef | ||
|
ff2ca10cba | ||
|
21d09632a5 | ||
|
92249894b3 | ||
|
0adfa0ec07 | ||
|
6a314d26cb | ||
|
148bd9ee92 | ||
|
faa43a755e | ||
|
5176455f1a | ||
|
5ea39eaa96 | ||
|
af02b83b88 | ||
|
d033e6327d | ||
|
0f88200247 | ||
|
53401bef63 | ||
|
0b228fca04 | ||
|
becda5638a | ||
|
d54a69ac7f | ||
|
10c4cd9677 | ||
|
87512e9751 | ||
|
c00c20a799 | ||
|
26194bd684 | ||
|
6fbcd8914a | ||
|
a52476c9be | ||
|
0edf0c8cd9 | ||
|
08c4c9a30b | ||
|
be7c1aef42 | ||
|
498d8145c0 | ||
|
ec331acf48 | ||
|
5ce881be82 | ||
|
f9eb9b4cab | ||
|
4e23b49969 | ||
|
b000a94275 | ||
|
6c9cda6f92 | ||
|
eeb2c61653 | ||
|
843b10c8b0 | ||
|
9eace84342 | ||
|
cb2b6e0494 | ||
|
09ee846d92 | ||
|
759b204be3 | ||
|
cc95b4594a | ||
|
d6eea6caee | ||
|
368f70604f | ||
|
6dda6931d1 | ||
|
15a7583787 | ||
|
c41a914d6e | ||
|
24493e9169 | ||
|
4821251ebb | ||
|
ca2dacfcee | ||
|
ea0917bcba | ||
|
9c79022153 | ||
|
3f477ccb03 | ||
|
eb1e43a9af | ||
|
5235c3f78d | ||
|
532471967e | ||
|
452e90ba72 | ||
|
1127887a8b | ||
|
8388f5a7e1 | ||
|
56d8b1e7f6 | ||
|
32a254050d | ||
|
5ab3447de1 | ||
|
4fa0e472cf | ||
|
524d253853 | ||
|
a297a4f044 | ||
|
b7d7599ce3 | ||
|
dfe63ed84d | ||
|
70a4320bdd | ||
|
9f3e098320 | ||
|
ef113c6f72 | ||
|
e2b530aa72 | ||
|
f6a6a77640 | ||
|
8c42205e35 | ||
|
cc02da816f | ||
|
3ffb0c4f25 | ||
|
de41d24a3d | ||
|
543875e080 | ||
|
96e4d0960c | ||
|
8c703ecf73 | ||
|
2de1e2062e | ||
|
48addc88be | ||
|
3b62417c35 | ||
|
00284f0ed6 | ||
|
cec40a7467 | ||
|
18f70a6229 | ||
|
048c2f0e8c | ||
|
15b0a92294 | ||
|
d843c27478 | ||
|
39edf4abca | ||
|
cf7ff99513 | ||
|
2eaf65ec60 | ||
|
8235c4747d | ||
|
8263690e75 | ||
|
efd7c87b5b | ||
|
3782454e8d | ||
|
f634adc7dc | ||
|
c0999a149c | ||
|
c29d73a06a | ||
|
0da06b7103 | ||
|
1b9db6d4a7 | ||
|
547be53b8c | ||
|
1aec7112f7 | ||
|
bfa15a642f | ||
|
b2a4feeb4c | ||
|
422a933429 | ||
|
deb84d7989 | ||
|
e840cb9cd5 | ||
|
74a38adb6b | ||
|
1322e205e2 | ||
|
bb001af0e8 | ||
|
2872ab927e | ||
|
85840d4c03 | ||
|
05e3933c06 | ||
|
2ce25ebbef | ||
|
fa1e0ba354 | ||
|
eac9250fd5 | ||
|
8816b6cdd9 | ||
|
84ac62d396 | ||
|
0206c070ac | ||
|
3265c9169d | ||
|
78976974e7 | ||
|
263fa2c4a6 | ||
|
59d3a3611f | ||
|
84c58aabd0 | ||
|
550a6488c9 | ||
|
1187627a1d | ||
|
c376555cec | ||
|
b40c0f028f | ||
|
30667d5041 | ||
|
26bfe59f45 | ||
|
4212c0e7e5 | ||
|
f74d19765e | ||
|
236828d06f | ||
|
026bfedb33 | ||
|
9dac814ee8 | ||
|
bb57d9171f | ||
|
6c0f85d5de | ||
|
1a2ad348f6 | ||
|
8a68316eb9 | ||
|
63f49457dc | ||
|
64bb1346c7 | ||
|
a8ab54d18d | ||
|
63e7620462 | ||
|
2e0777317f | ||
|
a84b8b6ebb | ||
|
5c864e2cfa | ||
|
87c82334a7 | ||
|
bdf2e55d98 | ||
|
9106a750cd | ||
|
8a2cbb88d1 | ||
|
d35fb9d732 | ||
|
bd91338246 | ||
|
aa6c5f4d92 | ||
|
1757a6fce5 | ||
|
6479e403ef | ||
|
23e3b1e665 | ||
|
96d757c13f | ||
|
20c6ea70cc | ||
|
9eef9c648c | ||
|
dbbe4a783b | ||
|
7ff34ca2cb | ||
|
0b1e1e1a00 | ||
|
e0835db4f1 | ||
|
df092aa1de | ||
|
6ff8119765 | ||
|
4a50c8bd03 | ||
|
23ee6b1cd6 | ||
|
a22c3e5968 | ||
|
fe503c7c06 | ||
|
7078aa63ae | ||
|
2ab680c697 | ||
|
3a22a6e858 | ||
|
3c3605818f | ||
|
7e5f769d91 | ||
|
8d65add626 | ||
|
5d511948cd | ||
|
2f02c25e36 | ||
|
d638c1794a | ||
|
875cc073b0 | ||
|
9aa77fcceb | ||
|
e1ac6f1e30 | ||
|
a68442e056 | ||
|
b9deced219 | ||
|
f249d0b84c | ||
|
0d12aa4836 | ||
|
2915e711f7 | ||
|
99911873a1 | ||
|
7c36eb3e8b | ||
|
412614c700 | ||
|
93af78d2d2 | ||
|
ad702bae0c | ||
|
d1039926f5 | ||
|
32793ab685 | ||
|
d348a916f5 | ||
|
d4bf74816f | ||
|
ea4f55e3dc | ||
|
0b39b1cbb7 | ||
|
132529ce89 | ||
|
dd4d409551 | ||
|
de12cd18fb | ||
|
b256f24145 | ||
|
041608129a | ||
|
5289304519 | ||
|
87bd7cd7b0 | ||
|
75206f266f | ||
|
4f082dfa89 | ||
|
0a793ebc60 | ||
|
8bdb05edb2 | ||
|
c1e97169cd | ||
|
c0e958e022 | ||
|
713658798d | ||
|
9b46748d5b | ||
|
4c553c5a5b | ||
|
967b88d938 | ||
|
8436040735 | ||
|
ca2ee91a80 | ||
|
7f0ac73724 | ||
|
224b77d4f7 | ||
|
8e8b1fe48c | ||
|
c577493908 | ||
|
a8740d6c09 | ||
|
fc8df41ec6 | ||
|
7afa143886 | ||
|
f62a369fb4 | ||
|
25566c6810 | ||
|
aca8263926 | ||
|
02dcbf343d | ||
|
a946317fab | ||
|
ae294cc2d0 | ||
|
900fda4411 | ||
|
3a8ca7abbc | ||
|
bf8d7bba9e | ||
|
4a3f5b3617 | ||
|
1e958e52d3 | ||
|
cd16538fc9 | ||
|
6e8fb66859 | ||
|
52fdd1cb76 | ||
|
a9b97cbcb7 | ||
|
16605f2fda | ||
|
9c94583a3d | ||
|
267da8138d | ||
|
e1c275d87b | ||
|
f9b97f1c62 | ||
|
a8d0f2516c | ||
|
e85e37d91d | ||
|
7411b694c3 | ||
|
c65a9a05f9 | ||
|
e84c81b76f | ||
|
ee95f281f0 | ||
|
1c730ee761 | ||
|
b8a3608aa5 | ||
|
5a50a98980 | ||
|
9548888599 | ||
|
3e60932a28 | ||
|
d7e8f00e7e | ||
|
a1ee5eb2aa | ||
|
4d9049dc1a | ||
|
470740f97b | ||
|
e16b0aef31 | ||
|
4659b2ae45 | ||
|
c2d29dd197 | ||
|
82b742533b | ||
|
78131eee64 | ||
|
bda5863425 | ||
|
cea2e25f41 | ||
|
1cab2b4a7c | ||
|
337165959c | ||
|
cfa6ff9569 | ||
|
cb311b441a | ||
|
3a2a3c7325 | ||
|
1e0fccd1af | ||
|
532116e7e3 | ||
|
6b5ad2066a | ||
|
0f40405fc9 | ||
|
9e92f357be | ||
|
67d8665af5 | ||
|
682d3f7de0 | ||
|
01fd00f5ed | ||
|
d506263da7 | ||
|
baeacdcfd3 | ||
|
9a5ef043c1 | ||
|
265419a369 | ||
|
0e4920632a | ||
|
f26bf60fb5 | ||
|
3fda08e405 | ||
|
0642fb4d45 | ||
|
d73c4ac869 | ||
|
84661bf6da | ||
|
14375237f6 | ||
|
e2ae08694e | ||
|
66934aceff | ||
|
fc1b933304 | ||
|
863ecfc785 | ||
|
6712e77271 | ||
|
c68ba7d093 | ||
|
d0b4597842 | ||
|
f2d8e6801e | ||
|
69b2f63d9a | ||
|
e19d08055a | ||
|
0ac9cb2c1f | ||
|
df50598f1c | ||
|
83715aa829 | ||
|
41b83e52f7 | ||
|
01dd78f9e9 | ||
|
addb1bcd86 | ||
|
961671c0f5 | ||
|
0db7a1d69c | ||
|
d4cebc6bbe | ||
|
d50b0bc437 | ||
|
89b0af3978 | ||
|
a043f2d794 | ||
|
64c5ad58d2 | ||
|
765f400f6b | ||
|
7ffc0a6534 | ||
|
78ca6ea8f0 | ||
|
86b9e8e39a | ||
|
1279a83103 | ||
|
d33cf3f4c3 | ||
|
5bd734317c | ||
|
318acb0f6c | ||
|
0ed3129f6b | ||
|
8bd718b3ba | ||
|
092c493027 | ||
|
7b9b0c0a00 | ||
|
a81e18da25 | ||
|
8e86ffce82 | ||
|
93c1defdb0 | ||
|
daf113ac66 | ||
|
d8e816c150 | ||
|
586c1800c4 | ||
|
45f28d8581 | ||
|
da16b33ab9 | ||
|
c2fc4c10ac | ||
|
f851f0d7e3 | ||
|
1771f70d74 | ||
|
1ef23f05ee | ||
|
caa9d03d65 | ||
|
9c9050ff12 | ||
|
13c0be19d3 | ||
|
18b4f2082c | ||
|
ee7e2ffd26 | ||
|
c3becfe193 | ||
|
0ec6b5ecd3 | ||
|
478f9babed | ||
|
cff9e937fd | ||
|
31d6939cde | ||
|
906092534b | ||
|
feae132e0f | ||
|
b24b781117 | ||
|
eaf63d314d | ||
|
70fab17837 | ||
|
23b079043b | ||
|
29958745c8 | ||
|
823ad12191 | ||
|
4727d1db9d | ||
|
f3a8cf050e | ||
|
c408d807a3 | ||
|
a63d20aa87 | ||
|
157f6c2aad | ||
|
6807320a0f | ||
|
4d3d34f599 | ||
|
966602602a | ||
|
4841804fff | ||
|
a177e4dd04 | ||
|
3b522a1e5c | ||
|
89ac4dd3c4 | ||
|
bcb4af81fc | ||
|
364d5823ea | ||
|
159d619caf | ||
|
7a8ae228f9 | ||
|
8a871e890a | ||
|
e95705f00c | ||
|
d1ba927b36 | ||
|
f2f5a7d945 | ||
|
0575c7db72 | ||
|
d0f47327f8 | ||
|
4406281260 | ||
|
1561ee799c | ||
|
d5144ea9bf | ||
|
5ce7adfcf9 | ||
|
185a0a51f8 | ||
|
be17805c0b | ||
|
a82f692e58 | ||
|
28b3b55122 | ||
|
85ad5855a0 | ||
|
345e50d59f | ||
|
75d98b6013 | ||
|
ace3072e09 | ||
|
e5ff7929c4 | ||
|
def6efa1ef | ||
|
8adaf730c0 | ||
|
c591810d46 | ||
|
6f46465af1 | ||
|
c2febfa332 | ||
|
68d0048b3d | ||
|
3831b61966 | ||
|
4db4ac7243 | ||
|
4d36796131 | ||
|
2e507a7457 | ||
|
72163bd5f3 | ||
|
09c1e370b3 | ||
|
94f9be80c3 | ||
|
8f8671bcde | ||
|
9bd8cb7c3c | ||
|
d493d0f180 | ||
|
e62cd03307 | ||
|
4842eeaad3 | ||
|
a63e78c31a | ||
|
e87a95d97d | ||
|
3013fc57bd | ||
|
abd4367f48 | ||
|
c36a298c21 | ||
|
d807ea087f | ||
|
3e7923507b | ||
|
772beb7308 | ||
|
cb21c5ffa9 | ||
|
71c51aa4ab | ||
|
1e11918c8c | ||
|
ac48e72bf6 | ||
|
cb5df823ac | ||
|
b5257bea85 | ||
|
66acbc8d7f | ||
|
fa5c09a2e7 | ||
|
d4eada9e0f | ||
|
c99050cce2 | ||
|
cf3e3845c2 | ||
|
6b0f5f68a8 | ||
|
67a2eb9177 | ||
|
a32a7b58ce | ||
|
7fc55925be | ||
|
ed1d853e51 | ||
|
dea9886454 | ||
|
3e52d112d7 | ||
|
517d05dff1 | ||
|
eb95b5ec1a | ||
|
49c7ef3b21 | ||
|
ccee67ca3b | ||
|
830ba75e6d | ||
|
46434a3cad | ||
|
4ff15a75c5 | ||
|
cd80c9b070 | ||
|
a6f79ca57f | ||
|
574b232427 | ||
|
3b3b0910ff | ||
|
dbf1ed263c | ||
|
672fb78e12 | ||
|
268dc7c8b3 | ||
|
738a57b69b | ||
|
b47eaefe12 | ||
|
f00221fadb | ||
|
38f4721092 | ||
|
5ee69d11f2 | ||
|
8b58f565e4 | ||
|
6b5cd7c05f | ||
|
3e405fb188 | ||
|
cc881bd155 | ||
|
b32d557a6e | ||
|
ee528fbd5d | ||
|
92cc1e7457 | ||
|
c521b3ac32 | ||
|
d59c1a2958 | ||
|
21f4f0f4b0 | ||
|
d3fa9e84e9 | ||
|
212eda07c4 | ||
|
49fc021337 | ||
|
f047271cb9 | ||
|
025525266f | ||
|
28a7d3943e | ||
|
534215a18f | ||
|
7c454d9186 | ||
|
abae1cc58b | ||
|
153f02da3b | ||
|
4ae3ee1200 | ||
|
289c1a7968 | ||
|
2d7fb19c3c | ||
|
2549300b54 | ||
|
f097f7659c | ||
|
d3f4f92b0e | ||
|
15a4421f9c | ||
|
69f7399247 | ||
|
5a41eed26d | ||
|
afd9845e26 | ||
|
b4a33e21ea | ||
|
a1f5e514ef | ||
|
5220cb63e3 | ||
|
145ebfa1df | ||
|
b3c6273efa | ||
|
300bd0eb85 | ||
|
4324025f98 | ||
|
801fd81efe | ||
|
77810030d2 | ||
|
a1dc526760 | ||
|
4b2aef8857 | ||
|
6aaaa63519 | ||
|
0705a1c565 | ||
|
734e9fb8a9 | ||
|
bb7aa06a48 | ||
|
e521150b8f | ||
|
5a9169e152 | ||
|
7d5e61a66a | ||
|
271fa063a3 | ||
|
9b701e69cc | ||
|
d19617f06b | ||
|
144c10fad1 | ||
|
31874783c4 | ||
|
05d47bd53e | ||
|
b880e3ffae | ||
|
1c5b4c5d5b | ||
|
f8d44b01df | ||
|
fd1f355b7b | ||
|
71652572e3 | ||
|
525a88d879 | ||
|
5d6dc93043 | ||
|
95639d9577 | ||
|
8e25f7d229 | ||
|
92a85089b8 | ||
|
b644a490f0 | ||
|
1d21306785 | ||
|
e919601aaf | ||
|
5ca4bd5d90 | ||
|
a1de692a69 | ||
|
751fb2366c | ||
|
69fddac052 | ||
|
93a3661c15 | ||
|
2af807a83f | ||
|
a2fd34f81f | ||
|
b29499996b | ||
|
6f535924eb | ||
|
4278abfe27 | ||
|
3484cb9a65 | ||
|
7ee07a3c39 | ||
|
97e48b6a18 | ||
|
54635f435a | ||
|
711d617dc1 | ||
|
ffb38dfb8b | ||
|
2faf519cf9 | ||
|
bff21441dd | ||
|
ed6100441e | ||
|
6fe11c9941 | ||
|
c49e4a6565 | ||
|
7074be22f1 | ||
|
5dbeb87ec9 | ||
|
2282030b2a | ||
|
d1b8fe93f0 | ||
|
ef57b70fa5 | ||
|
01111fc42c | ||
|
1e37e35c3e | ||
|
ead7b8f498 | ||
|
61e501da89 | ||
|
3c74416465 | ||
|
a5d2a34497 | ||
|
8c9986d310 | ||
|
54334b5667 | ||
|
665be7f6bd | ||
|
7126cadf80 | ||
|
5ffb62dd03 | ||
|
da3cf9eae3 | ||
|
039a65d0e4 | ||
|
93f50ca317 | ||
|
b2b84359ab | ||
|
c609d03986 | ||
|
2be9218a3b | ||
|
62e64905b7 | ||
|
c259669fa3 | ||
|
82f42ea091 | ||
|
d8022d26fc | ||
|
5509e17d0c | ||
|
f8aad5d5b7 | ||
|
d311368bc5 | ||
|
d47c3d64b2 | ||
|
d14f8c3c45 | ||
|
f2010f9c65 | ||
|
33ad6e0188 | ||
|
ec5e5d23fa | ||
|
5b208e296f | ||
|
4e276a8920 | ||
|
256cc8ee08 | ||
|
eeba61ccd5 | ||
|
5e8df049fb | ||
|
2119ae74c2 | ||
|
5546aad260 | ||
|
66e5dc157a | ||
|
ac3ad139f6 | ||
|
b7605d5c95 | ||
|
c42ecaab8d | ||
|
a7848dd880 | ||
|
77234bbbde | ||
|
b94e5e58db | ||
|
ad88b94bca | ||
|
d15b0b0a1b | ||
|
af62c0f9f1 | ||
|
cdbe1defa4 | ||
|
f7f70bed8f | ||
|
cd6ca9b1f6 | ||
|
e58f8c28d2 | ||
|
69ae578450 | ||
|
f6e8e141df | ||
|
65d2a88dd2 | ||
|
2706747f66 | ||
|
5df4073c81 | ||
|
0eb7f17d9a | ||
|
bd7958776b | ||
|
1b7ddb0ea0 | ||
|
4abe781c27 | ||
|
012a0d6bf8 | ||
|
c39a1cb17e | ||
|
0ed1e85091 | ||
|
7dea7ccb10 | ||
|
4b135d0958 | ||
|
bb721f0d6d | ||
|
3ec0bedc60 | ||
|
45ec4ce82a | ||
|
5e3cd0e5b5 | ||
|
fa71b268a8 | ||
|
fd328869cc | ||
|
101c5a50aa | ||
|
9b0a0ba9e6 | ||
|
8860e991f6 | ||
|
cc5b93f72d | ||
|
5de0e848de | ||
|
044e123fd3 | ||
|
18c53c4567 | ||
|
925aa14912 | ||
|
d6f027ef34 | ||
|
379f995940 | ||
|
08b3a24da5 | ||
|
5af7b59660 | ||
|
d39d41fbda | ||
|
b66a9e2f33 | ||
|
659f80f262 | ||
|
776d6b2c05 | ||
|
af678af0d5 | ||
|
54ac0becee | ||
|
286e2011d2 | ||
|
0c6dfe5236 | ||
|
92912f063a | ||
|
a8caff322f | ||
|
7152e5efbb | ||
|
2003a18407 | ||
|
7a7ac65682 | ||
|
4bdf1881dc | ||
|
0c8c8151fc | ||
|
1a7daab126 | ||
|
43fd8fae52 | ||
|
062d18fdb1 | ||
|
a815d62d59 | ||
|
9a74622ca1 | ||
|
1bb3ecb2a5 | ||
|
2a013bb3a0 | ||
|
ade389b326 | ||
|
f4a60a9bc4 | ||
|
9f5782d969 | ||
|
966ca6143b | ||
|
d82987d12e | ||
|
74d9416763 | ||
|
2fad92144e | ||
|
3f35816136 | ||
|
0e965f6991 | ||
|
7eb9e79648 | ||
|
a4caa1c0a3 | ||
|
259052199b | ||
|
650b418942 | ||
|
0264ccf6f4 | ||
|
cb1bd816db | ||
|
513ad0a85e | ||
|
8109eb765f | ||
|
f9d729ab68 | ||
|
9a883adf95 | ||
|
a820ae1074 | ||
|
93e807292c | ||
|
6ffa8f5393 | ||
|
5b218c3d9b | ||
|
b9f5692186 | ||
|
de9b87f558 | ||
|
e691d16a63 | ||
|
74c838a870 | ||
|
9a70c8d6c3 | ||
|
04632fd77f | ||
|
70b90dde23 | ||
|
600998fcb1 | ||
|
67aa88336a | ||
|
0bf95f99e6 | ||
|
23c212e7f1 | ||
|
e92a4b855f | ||
|
d549b83fc2 | ||
|
0f7d5b1a88 | ||
|
29a6416276 | ||
|
7fd4143ead | ||
|
9656dce72e | ||
|
aedd3a6bab | ||
|
5e173e9f63 | ||
|
d7661fbe9d | ||
|
9b136840d9 | ||
|
04ae8ddaa1 | ||
|
d44e686e9b | ||
|
fe9f1a6ded |
7
.dir-locals.el
Normal file
7
.dir-locals.el
Normal file
@ -0,0 +1,7 @@
|
||||
; BIRD project coding conventions
|
||||
|
||||
((c-mode
|
||||
(c-file-style . "bsd")
|
||||
(c-basic-offset . 2)
|
||||
(fill-column . 80)
|
||||
(show-trailing-whitespace . t)))
|
57
.drone.yml
Normal file
57
.drone.yml
Normal file
@ -0,0 +1,57 @@
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: deploy
|
||||
|
||||
steps:
|
||||
|
||||
- name: build alpine
|
||||
image: c8n.io/simonburblecom/bird-build:alpine
|
||||
commands:
|
||||
- /build.sh
|
||||
|
||||
- name: build ubuntu
|
||||
image: c8n.io/simonburblecom/bird-build:ubuntu
|
||||
commands:
|
||||
- /build.sh
|
||||
|
||||
- name: upload
|
||||
image: plugins/s3
|
||||
settings:
|
||||
bucket: artifacts
|
||||
access_key:
|
||||
from_secret: MINIO_ACCESS_KEY
|
||||
secret_key:
|
||||
from_secret: MINIO_SECRET_KEY
|
||||
endpoint: https://minio.burble.dn42
|
||||
region: uk-lon3
|
||||
path_style: true
|
||||
source: artifacts/**/*
|
||||
strip_prefix: artifacts/
|
||||
target: /bird/${DRONE_BRANCH}
|
||||
|
||||
|
||||
image_pull_secrets:
|
||||
- CONFIG_JSON
|
||||
|
||||
---
|
||||
kind: secret
|
||||
name: CONFIG_JSON
|
||||
get:
|
||||
path: burble.dn42/kv/data/drone/docker
|
||||
name: configjson
|
||||
|
||||
---
|
||||
kind: secret
|
||||
name: MINIO_ACCESS_KEY
|
||||
get:
|
||||
path: burble.dn42/kv/data/drone/minio
|
||||
name: ACCESS_KEY
|
||||
|
||||
---
|
||||
kind: secret
|
||||
name: MINIO_SECRET_KEY
|
||||
get:
|
||||
path: burble.dn42/kv/data/drone/minio
|
||||
name: SECRET_KEY
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,4 @@
|
||||
/autom4te.cache/
|
||||
/doc/*.html
|
||||
/obj/
|
||||
/Makefile
|
||||
/bird
|
||||
@ -12,3 +11,4 @@
|
||||
/configure
|
||||
/sysdep/autoconf.h.in
|
||||
/sysdep/autoconf.h.in~
|
||||
/cscope.*
|
||||
|
602
.gitlab-ci.yml
602
.gitlab-ci.yml
@ -4,10 +4,12 @@ variables:
|
||||
GIT_STRATEGY: fetch
|
||||
DOCKER_CMD: docker --config="$HOME/.docker/$CI_JOB_ID/"
|
||||
IMG_BASE: registry.labs.nic.cz/labs/bird
|
||||
TOOLS_DIR: /var/lib/gitlab-runner/bird-tools
|
||||
|
||||
stages:
|
||||
- image
|
||||
- build
|
||||
- test
|
||||
|
||||
.docker: &docker_build
|
||||
stage: image
|
||||
@ -34,36 +36,46 @@ docker_debian-7-amd64:
|
||||
IMG_NAME: "debian-7-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-8-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-8-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-9-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-9-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-testing-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-testing-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-7-i386:
|
||||
variables:
|
||||
IMG_NAME: "debian-7-i386"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-8-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-8-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-8-i386:
|
||||
variables:
|
||||
IMG_NAME: "debian-8-i386"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-9-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-9-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-9-i386:
|
||||
variables:
|
||||
IMG_NAME: "debian-9-i386"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-10-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-10-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-10-i386:
|
||||
variables:
|
||||
IMG_NAME: "debian-10-i386"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-testing-amd64:
|
||||
variables:
|
||||
IMG_NAME: "debian-testing-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_debian-testing-i386:
|
||||
variables:
|
||||
IMG_NAME: "debian-testing-i386"
|
||||
@ -79,9 +91,29 @@ docker_fedora-26-amd64:
|
||||
IMG_NAME: "fedora-26-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_centos-6-amd64:
|
||||
docker_fedora-27-amd64:
|
||||
variables:
|
||||
IMG_NAME: "centos-6-amd64"
|
||||
IMG_NAME: "fedora-27-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_fedora-28-amd64:
|
||||
variables:
|
||||
IMG_NAME: "fedora-28-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_fedora-29-amd64:
|
||||
variables:
|
||||
IMG_NAME: "fedora-29-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_fedora-30-amd64:
|
||||
variables:
|
||||
IMG_NAME: "fedora-30-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_fedora-31-amd64:
|
||||
variables:
|
||||
IMG_NAME: "fedora-31-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_centos-7-amd64:
|
||||
@ -89,127 +121,43 @@ docker_centos-7-amd64:
|
||||
IMG_NAME: "centos-7-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_opensuse-42_3-amd64:
|
||||
docker_centos-8-amd64:
|
||||
variables:
|
||||
IMG_NAME: "opensuse-42.3-amd64"
|
||||
IMG_NAME: "centos-8-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_ubuntu-14_04-amd64:
|
||||
variables:
|
||||
IMG_NAME: "ubuntu-14.04-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
|
||||
docker_ubuntu-16_04-amd64:
|
||||
variables:
|
||||
IMG_NAME: "ubuntu-16.04-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_ubuntu-18_04-amd64:
|
||||
variables:
|
||||
IMG_NAME: "ubuntu-18.04-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
docker_ubuntu-19_10-amd64:
|
||||
variables:
|
||||
IMG_NAME: "ubuntu-19.10-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
.debian-7-i386: &debian-7-i386_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-7-i386
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
docker_opensuse-15.0-amd64:
|
||||
variables:
|
||||
IMG_NAME: "opensuse-15.0-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
.debian-8-i386: &debian-8-i386_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-8-i386
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
docker_opensuse-15.1-amd64:
|
||||
variables:
|
||||
IMG_NAME: "opensuse-15.1-amd64"
|
||||
<<: *docker_build
|
||||
|
||||
.debian-9-i386: &debian-9-i386_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-9-i386
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.debian-testing-i386: &debian-testing-i386_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-testing-i386
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.debian-7-amd64: &debian-7-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-7-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.debian-8-amd64: &debian-8-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-8-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.debian-9-amd64: &debian-9-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-9-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.debian-testing-amd64: &debian-testing-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:debian-testing-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.fedora-25-amd64: &fedora-25-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-25-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.fedora-26-amd64: &fedora-26-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-26-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.centos-6-amd64: ¢os-6-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:centos-6-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.centos-7-amd64: ¢os-7-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:centos-7-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.opensuse-42_3-amd64: &opensuse-42_3-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:opensuse-42.3-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.ubuntu-14_04-amd64: &ubuntu-14_04-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:ubuntu-14.04-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
.ubuntu-16_04-amd64: &ubuntu-16_04-amd64_env
|
||||
image: registry.labs.nic.cz/labs/bird:ubuntu-16.04-amd64
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
# TODO We want to copy these BSDs to our own virtual machines, to make sure someone doesn't update them by accident.
|
||||
# TODO We want to copy these BSDs to our own virtual machines, to make sure
|
||||
# someone doesn't update them by accident.
|
||||
.freebsd-11-i386: &freebsd-11-i386_env
|
||||
tags:
|
||||
- freebsd
|
||||
@ -223,223 +171,245 @@ docker_ubuntu-16_04-amd64:
|
||||
tags:
|
||||
- freebsd
|
||||
- amd64
|
||||
#only:
|
||||
#- master
|
||||
#- triggers
|
||||
#- tags
|
||||
|
||||
.build: &build_job
|
||||
.build: &build-base
|
||||
stage: build
|
||||
script:
|
||||
- autoreconf
|
||||
- ./configure --enable-ipv6=$IPV6 CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS"
|
||||
- ./configure CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS"
|
||||
# Detect which make is available
|
||||
- MAKE=make
|
||||
- which gmake 2>/dev/null >/dev/null && MAKE=gmake
|
||||
- $MAKE
|
||||
# Run tests if they are available (eg. don't fail if "check" isn't a valid make target)
|
||||
- $MAKE check || [ "$?" = 2 ]
|
||||
# Run tests if they are available
|
||||
- $MAKE check
|
||||
|
||||
.build-linux: &build-linux
|
||||
<<: *build-base
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
- amd64
|
||||
|
||||
build-debian-7-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-7-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-8-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-8-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-9-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-9-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-testing-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-testing-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-7-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-7-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-8-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-8-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-9-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-9-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-debian-testing-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-testing-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-fedora-25-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *fedora-25-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-fedora-25-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *fedora-25-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-fedora-26-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *fedora-26-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-fedora-26-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *fedora-26-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-centos-6-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *centos-6-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-centos-6-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *centos-6-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-centos-7-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *centos-7-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-centos-7-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *centos-7-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-opensuse-42_3-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *opensuse-42_3-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-opensuse-42_3-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *opensuse-42_3-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-ubuntu-14_04-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *ubuntu-14_04-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-ubuntu-14_04-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *ubuntu-14_04-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-ubuntu-16_04-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *ubuntu-16_04-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-ubuntu-16_04-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *ubuntu-16_04-amd64_env
|
||||
<<: *build_job
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-7-amd64
|
||||
|
||||
build-debian-7-i386:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-7-i386_env
|
||||
<<: *build_job
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-7-i386
|
||||
|
||||
build-debian-7-i386-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-7-i386_env
|
||||
<<: *build_job
|
||||
build-debian-8-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-8-amd64
|
||||
|
||||
build-debian-8-i386:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-8-i386_env
|
||||
<<: *build_job
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-8-i386
|
||||
|
||||
build-debian-8-i386-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-8-i386_env
|
||||
<<: *build_job
|
||||
build-debian-9-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-9-amd64
|
||||
|
||||
build-debian-9-i386:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-9-i386_env
|
||||
<<: *build_job
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-9-i386
|
||||
|
||||
build-debian-9-i386-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-9-i386_env
|
||||
<<: *build_job
|
||||
build-debian-10-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-10-amd64
|
||||
|
||||
build-debian-10-i386:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-10-i386
|
||||
|
||||
build-debian-testing-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-testing-amd64
|
||||
|
||||
build-debian-testing-i386:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *debian-testing-i386_env
|
||||
<<: *build_job
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:debian-testing-i386
|
||||
|
||||
build-debian-testing-i386-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *debian-testing-i386_env
|
||||
<<: *build_job
|
||||
build-fedora-25-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-25-amd64
|
||||
|
||||
build-fedora-26-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-26-amd64
|
||||
|
||||
build-fedora-27-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-27-amd64
|
||||
|
||||
build-fedora-28-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-28-amd64
|
||||
|
||||
build-fedora-29-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-29-amd64
|
||||
|
||||
build-fedora-30-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-30-amd64
|
||||
|
||||
build-fedora-31-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:fedora-31-amd64
|
||||
|
||||
build-centos-7-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:centos-7-amd64
|
||||
|
||||
build-centos-8-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:centos-8-amd64
|
||||
|
||||
build-ubuntu-14_04-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:ubuntu-14.04-amd64
|
||||
|
||||
build-ubuntu-16_04-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:ubuntu-16.04-amd64
|
||||
|
||||
build-ubuntu-18_04-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:ubuntu-18.04-amd64
|
||||
|
||||
build-ubuntu-19_04-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:ubuntu-19.04-amd64
|
||||
|
||||
build-opensuse-15.0-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:opensuse-15.0-amd64
|
||||
|
||||
build-opensuse-15.1-amd64:
|
||||
<<: *build-linux
|
||||
image: registry.labs.nic.cz/labs/bird:opensuse-15.1-amd64
|
||||
|
||||
build-freebsd-11-amd64:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *freebsd-11-amd64_env
|
||||
<<: *build_job
|
||||
|
||||
build-freebsd-11-amd64-v6:
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *freebsd-11-amd64_env
|
||||
<<: *build_job
|
||||
<<: *build-base
|
||||
tags:
|
||||
- freebsd
|
||||
- amd64
|
||||
|
||||
build-freebsd-11-i386:
|
||||
variables:
|
||||
IPV6: "no"
|
||||
<<: *freebsd-11-i386_env
|
||||
<<: *build_job
|
||||
<<: *build-base
|
||||
tags:
|
||||
- freebsd
|
||||
- i386
|
||||
|
||||
build-freebsd-i386-v6:
|
||||
build-birdlab:
|
||||
stage: build
|
||||
tags:
|
||||
- birdlab
|
||||
- amd64
|
||||
script:
|
||||
- DIR=$(pwd)
|
||||
- autoreconf
|
||||
- ./configure
|
||||
- make
|
||||
- cd $TOOLS_DIR
|
||||
- sudo git clean -fx
|
||||
- git pull --ff-only
|
||||
- mv $DIR/bird $DIR/birdc netlab/common
|
||||
|
||||
.test: &test-base
|
||||
stage: test
|
||||
needs: [build-birdlab]
|
||||
tags:
|
||||
- birdlab
|
||||
- amd64
|
||||
script:
|
||||
- cd $TOOLS_DIR/netlab
|
||||
- sudo ./stop
|
||||
- sudo ./runtest -m check $TEST_NAME
|
||||
|
||||
test-ospf-base:
|
||||
<<: *test-base
|
||||
variables:
|
||||
IPV6: "yes"
|
||||
<<: *freebsd-11-i386_env
|
||||
<<: *build_job
|
||||
TEST_NAME: cf-ospf-base
|
||||
|
||||
test-ospf-default:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-default
|
||||
|
||||
test-ospf-priority:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-priority
|
||||
|
||||
test-ospf-nbma:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-nbma
|
||||
|
||||
test-ospf-ptmp:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-ptmp
|
||||
|
||||
test-ospf-authentication:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-authentication
|
||||
|
||||
test-ospf-bfd:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-bfd
|
||||
|
||||
test-ospf-custom:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-custom
|
||||
|
||||
test-ospf-vrf:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ospf-vrf
|
||||
|
||||
test-bgp-base:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-bgp-base
|
||||
|
||||
test-bgp-auth:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-bgp-auth
|
||||
|
||||
test-bgp-int:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-bgp-int
|
||||
|
||||
test-bgp-merged:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-bgp-merged
|
||||
|
||||
test-ebgp-loop:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ebgp-loop
|
||||
|
||||
test-ebgp-star:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ebgp-star
|
||||
|
||||
test-ibgp-loop:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ibgp-loop
|
||||
|
||||
test-ibgp-star:
|
||||
<<: *test-base
|
||||
variables:
|
||||
TEST_NAME: cf-ibgp-flat
|
||||
|
1
INSTALL
1
INSTALL
@ -35,6 +35,7 @@ For compiling BIRD you need these programs and libraries:
|
||||
|
||||
- ncurses library
|
||||
- GNU Readline library
|
||||
- libssh library (optional, for RPKI-Router protocol)
|
||||
|
||||
For compiling BIRD documentation you also need:
|
||||
|
||||
|
236
Makefile.in
Normal file
236
Makefile.in
Normal file
@ -0,0 +1,236 @@
|
||||
# Makefile for the BIRD Internet Routing Daemon
|
||||
# (c) 1999--2000 Martin Mares <mj@ucw.cz>
|
||||
# (c) 2016 Jan Moskyto Matejka <mq@ucw.cz>
|
||||
|
||||
# Disable build-in rules
|
||||
MAKEFLAGS += -r
|
||||
|
||||
# Variable definitions
|
||||
CPPFLAGS=-I$(objdir) -I$(srcdir) @CPPFLAGS@
|
||||
CFLAGS=$(CPPFLAGS) @CFLAGS@
|
||||
LDFLAGS=@LDFLAGS@
|
||||
M4FLAGS=@M4FLAGS@
|
||||
BISONFLAGS=@BISONFLAGS@
|
||||
LIBS=@LIBS@
|
||||
DAEMON_LIBS=@DAEMON_LIBS@
|
||||
CLIENT_LIBS=@CLIENT_LIBS@
|
||||
CC=@CC@
|
||||
M4=@M4@
|
||||
BISON=@BISON@
|
||||
FLEX=@FLEX@
|
||||
RANLIB=@RANLIB@
|
||||
INSTALL=@INSTALL@
|
||||
INSTALL_PROGRAM=@INSTALL_PROGRAM@
|
||||
INSTALL_DATA=@INSTALL_DATA@
|
||||
|
||||
client=$(addprefix $(exedir)/,@CLIENT@)
|
||||
daemon=$(exedir)/bird
|
||||
protocols=@protocols@
|
||||
|
||||
prefix=@prefix@
|
||||
exec_prefix=@exec_prefix@
|
||||
bindir=@bindir@
|
||||
sbindir=@sbindir@
|
||||
sysconfdir=@sysconfdir@
|
||||
localstatedir=@localstatedir@
|
||||
runstatedir=@runstatedir@
|
||||
docdir=@prefix@/doc
|
||||
|
||||
srcdir := @srcdir@
|
||||
objdir := @objdir@
|
||||
exedir := @exedir@
|
||||
|
||||
git-label:=$(strip $(shell cd $(srcdir) && [ "$$(git rev-parse --show-toplevel)" = "$$(readlink -f .)" ] && git describe --always --dirty=-x 2>/dev/null))
|
||||
ifneq ($(git-label),)
|
||||
CFLAGS += -DGIT_LABEL="$(git-label)"
|
||||
endif
|
||||
|
||||
ifeq ($(objdir),.)
|
||||
objdir := $(realpath .)
|
||||
endif
|
||||
|
||||
ifeq ($(VERBOSE),)
|
||||
E:=@
|
||||
Q:=@
|
||||
else
|
||||
E:=@\#
|
||||
Q:=
|
||||
endif
|
||||
|
||||
ifneq ($(COLOR),)
|
||||
CFLAGS += -fdiagnostics-color=always
|
||||
endif
|
||||
|
||||
# Meta rules
|
||||
docgoals := docs userdocs progdocs
|
||||
testgoals := check test tests tests_run
|
||||
cleangoals := clean distclean testsclean
|
||||
.PHONY: all daemon cli $(docgoals) $(testgoals) $(cleangoals) tags cscope prepare
|
||||
|
||||
all: daemon cli
|
||||
|
||||
daemon: $(daemon)
|
||||
cli: $(client)
|
||||
|
||||
$(daemon): LIBS += $(DAEMON_LIBS)
|
||||
|
||||
# Include directories
|
||||
dirs := client conf doc filter lib nest test $(addprefix proto/,$(protocols)) @sysdep_dirs@
|
||||
|
||||
# conf/Makefile declarations needed for all other modules
|
||||
conf-lex-targets := $(addprefix $(objdir)/conf/,cf-lex.o)
|
||||
conf-y-targets := $(addprefix $(objdir)/conf/,cf-parse.y keywords.h commands.h)
|
||||
cf-local = $(conf-y-targets): $(s)config.Y
|
||||
|
||||
src-o-files = $(patsubst %.c,$(o)%.o,$(src))
|
||||
tests-target-files = $(patsubst %.c,$(o)%,$(tests_src))
|
||||
|
||||
all-daemon = $(daemon): $(obj)
|
||||
all-client = $(client): $(obj)
|
||||
|
||||
s = $(dir $(lastword $(MAKEFILE_LIST)))
|
||||
ifeq ($(srcdir),.)
|
||||
o = $(objdir)/$(s)
|
||||
else
|
||||
o = $(patsubst $(srcdir)%,$(objdir)%,$(s))
|
||||
endif
|
||||
|
||||
define clean_in =
|
||||
clean::
|
||||
rm -f $(addprefix $(o),$(1))
|
||||
endef
|
||||
|
||||
clean = $(eval $(call clean_in,$(1)))
|
||||
|
||||
# Include main Makefiles of the directories
|
||||
include $(addsuffix /Makefile,$(addprefix $(srcdir)/,$(dirs)))
|
||||
|
||||
# Generic rules
|
||||
# Object file rules
|
||||
$(objdir)/%.o: $(srcdir)/%.c | prepare
|
||||
$(E)echo CC -o $@ -c $<
|
||||
$(Q)$(CC) $(CFLAGS) -MMD -MP -o $@ -c $<
|
||||
|
||||
$(objdir)/%.o: $(objdir)/%.c | prepare
|
||||
$(E)echo CC -o $@ -c $<
|
||||
$(Q)$(CC) $(CFLAGS) -MMD -MP -o $@ -c $<
|
||||
|
||||
# Debug: Preprocessed source rules
|
||||
$(objdir)/%.E: $(srcdir)/%.c | prepare
|
||||
$(E)echo CC -o $@ -E $<
|
||||
$(Q)$(CC) $(CFLAGS) -MMD -MP -o $@ -E $<
|
||||
|
||||
$(objdir)/%.E: $(objdir)/%.c | prepare
|
||||
$(E)echo CC -o $@ -E $<
|
||||
$(Q)$(CC) $(CFLAGS) -MMD -MP -o $@ -E $<
|
||||
|
||||
# Debug: Assembler object rules
|
||||
$(objdir)/%.S: $(srcdir)/%.c | prepare
|
||||
$(E)echo CC -o $@ -S $<
|
||||
$(Q)$(CC) $(CFLAGS) -MMD -MP -o $@ -S $<
|
||||
|
||||
$(objdir)/%.S: $(objdir)/%.c | prepare
|
||||
$(E)echo CC -o $@ -S $<
|
||||
$(Q)$(CC) $(CFLAGS) -MMD -MP -o $@ -S $<
|
||||
|
||||
# Finally include the computed dependencies:
|
||||
DEPS = $(shell find $(objdir) -name '*.d')
|
||||
|
||||
# ## if there is at least one non-clean goal
|
||||
ifneq ($(filter-out $(cleangoals),$(MAKECMDGOALS)),)
|
||||
-include $(DEPS)
|
||||
endif
|
||||
|
||||
# ## if the implicit goal is called
|
||||
ifeq ($(MAKECMDGOALS),)
|
||||
-include $(DEPS)
|
||||
endif
|
||||
|
||||
# Rule for pre-generating all generated includables
|
||||
# before compiling any C file
|
||||
prepare: $(objdir)/sysdep/paths.h | $(objdir)/.dir-stamp
|
||||
|
||||
$(objdir)/.dir-stamp: Makefile
|
||||
$(E)echo MKDIR -p $(addprefix $(objdir)/,$(dirs) doc)
|
||||
$(Q)mkdir -p $(addprefix $(objdir)/,$(dirs) doc)
|
||||
$(Q)touch $@
|
||||
|
||||
$(client) $(daemon):
|
||||
$(E)echo LD $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
$(Q)$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
$(objdir)/sysdep/paths.h: Makefile
|
||||
echo >$@ "/* Generated by Makefile, don't edit manually! */"
|
||||
echo >>$@ "#define PATH_CONFIG_FILE \"@CONFIG_FILE@\""
|
||||
echo >>$@ "#define PATH_CONTROL_SOCKET \"@CONTROL_SOCKET@\""
|
||||
if test -n "@iproutedir@" ; then echo >>$@ "#define PATH_IPROUTE_DIR \"@iproutedir@\"" ; fi
|
||||
|
||||
# Unit tests rules
|
||||
|
||||
tests_targets_ok = $(addsuffix .ok,$(tests_targets))
|
||||
|
||||
$(tests_targets): %: %.o $(tests_objs) | prepare
|
||||
$(E)echo LD $(LDFLAGS) -o $@ $< "..." $(LIBS)
|
||||
$(Q)$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
# Hack to avoid problems with tests linking everything
|
||||
$(tests_targets): LIBS += $(DAEMON_LIBS)
|
||||
|
||||
$(tests_targets_ok): %.ok: %
|
||||
$(Q)$* 2>/dev/null && touch $*.ok
|
||||
|
||||
test: testsclean check
|
||||
check: tests tests_run
|
||||
tests: $(tests_targets)
|
||||
tests_run: $(tests_targets_ok)
|
||||
|
||||
STATIC_CHECKERS_ENABLE := nullability.NullableDereferenced nullability.NullablePassedToNonnull nullability.NullableReturnedFromNonnull optin.portability.UnixAPI valist.CopyToSelf valist.Uninitialized valist.Unterminated
|
||||
STATIC_CHECKERS_DISABLE := deadcode.DeadStores
|
||||
STATIC_SCAN_FLAGS := -o $(objdir)/static-scan/ $(addprefix -enable-checker ,$(STATIC_CHECKERS_ENABLE)) $(addprefix -disable-checker ,$(STATIC_CHECKERS_DISABLE))
|
||||
|
||||
static-scan:
|
||||
$(E)echo Running static code analysis
|
||||
$(Q)$(MAKE) clean
|
||||
$(Q)scan-build $(STATIC_SCAN_FLAGS) $(MAKE) -$(MAKEFLAGS)
|
||||
|
||||
tags:
|
||||
cd $(srcdir) ; etags -lc `find $(dirs) -name '*.[chY]'`
|
||||
|
||||
cscope:
|
||||
cd $(srcdir) ; find $(dirs) -name '*.[chY]' > cscope.files ; cscope -b
|
||||
|
||||
# Install
|
||||
|
||||
install: all
|
||||
$(INSTALL) -d $(DESTDIR)/$(sbindir) $(DESTDIR)/$(sysconfdir) $(DESTDIR)/$(runstatedir)
|
||||
for BIN in bird @CLIENT@ ; do \
|
||||
$(INSTALL_PROGRAM) $(exedir)/$$BIN $(DESTDIR)/$(sbindir)/$$BIN ; \
|
||||
done
|
||||
if ! test -f $(DESTDIR)/@CONFIG_FILE@ ; then \
|
||||
$(INSTALL_DATA) $(srcdir)/doc/bird.conf.example $(DESTDIR)/@CONFIG_FILE@ ; \
|
||||
else \
|
||||
echo "Not overwriting old bird.conf" ; \
|
||||
fi
|
||||
|
||||
install-docs:
|
||||
$(INSTALL) -d $(DESTDIR)/$(docdir)
|
||||
$(INSTALL_DATA) $(objdir)/doc/{bird,prog}{,-*}.html $(DESTDIR)/$(docdir)/
|
||||
|
||||
# Cleanup
|
||||
clean::
|
||||
rm -f $(objdir)/sysdep/paths.h
|
||||
rm -f $(addprefix $(exedir)/,bird birdc birdcl)
|
||||
find $(objdir) -name "*.[od]" -exec rm -f '{}' '+'
|
||||
|
||||
testsclean:
|
||||
rm -f $(tests_targets_ok)
|
||||
|
||||
ifeq ($(objdir),obj)
|
||||
distclean: clean
|
||||
rm -rf $(objdir)
|
||||
rm -f config.log config.status configure Makefile
|
||||
else
|
||||
distclean: clean
|
||||
rm -rf * .dir-stamp
|
||||
rm -f config.log config.status configure Makefile
|
||||
endif
|
200
NEWS
200
NEWS
@ -1,29 +1,191 @@
|
||||
Version 1.6.7 (2019-08-01)
|
||||
o BFD: Support for VRFs
|
||||
o Several bugfixes
|
||||
|
||||
Version 1.6.6 (2019-02-27)
|
||||
o Several bugfixes related to route propagation
|
||||
|
||||
Version 1.6.5 (2019-01-05)
|
||||
o MRT table dumps (RFC 6396)
|
||||
o BGP Long-lived graceful restart
|
||||
o Filter: Make ifname attribute modifiable
|
||||
o Improved keeping track of IPv6 link-local addresses
|
||||
Version 2.0.8 (2021-03-18)
|
||||
o Automatic channel reloads based on RPKI changes
|
||||
o Multiple static routes with the same network
|
||||
o Use bitmaps to keep track of exported routes
|
||||
o Per-channel debug flags
|
||||
o CLI commands show info from multiple protocols
|
||||
o Linux: IPv4 routes with IPv6 nexthops
|
||||
o Filter: Optimized redesign of prefix sets
|
||||
o Filter: Improved type checking of user filters
|
||||
o Filter: New src/dst accessors for Flowspec and SADR
|
||||
o Filter: New 'weight' route attribute
|
||||
o Filter: BGP path mask loop operator
|
||||
o Filter: Remove quitbird command
|
||||
o RIP: Demand circuit support (RFC 2091)
|
||||
o BGP: New 'allow as sets' and 'enforce first as' options
|
||||
o BGP: Support for BGP hostname capability
|
||||
o BGP: Support for MD5SIG with dynamic BGP
|
||||
o BFD: Optional separation of IPv4 / IPv6 BFD instances
|
||||
o BFD: Per-peer session options
|
||||
o RPKI: Allow build without libSSH
|
||||
o RPKI: New 'ignore max length' option
|
||||
o OSPF: Redesign of handling of unnumbered PtPs
|
||||
o OSPF: Allow key id 0 in authentication
|
||||
o Babel: Use onlink flag for routes with unreachable next hop
|
||||
o Many bugfixes
|
||||
|
||||
Version 1.6.4 (2018-03-22)
|
||||
Notes:
|
||||
|
||||
Automatic channel reloads based on RPKI changes are enabled by default,
|
||||
but require import table enabled when used in BGP import filter.
|
||||
|
||||
BIRD now uses bitmaps to keep track of exported routes instead of
|
||||
re-evaluation of export filters. That should improve speed and accuracy in
|
||||
route export handling during reconfiguration, but takes some more memory.
|
||||
|
||||
Per-channel debug logging and some CLI commands (like 'show ospf neighbors')
|
||||
defaulting to all protocol instances lead to some minor changes in log and
|
||||
CLI output. Caution is recommended when logs or CLI output are monitored by
|
||||
scripts.
|
||||
|
||||
|
||||
Version 2.0.7 (2019-10-11)
|
||||
o BGP: Accumulated IGP metric (RFC 7311)
|
||||
o Important filter reconfiguration bugfix
|
||||
o Several other bugfixes
|
||||
|
||||
Version 2.0.6 (2019-09-10)
|
||||
o RAdv: Solicited unicast RAs
|
||||
o BGP: Optional Adj-RIB-Out
|
||||
o BGP: Extended optional parameters length
|
||||
o Filter: Sets and set expressions in path masks
|
||||
o Several important bugfixes
|
||||
|
||||
Version 2.0.5 (2019-08-01)
|
||||
o OSPF Graceful restart (RFC 3623, RFC 5187)
|
||||
o BGP: Dynamic BGP
|
||||
o BGP: Promiscuous ASN mode
|
||||
o BGP: Mandatory option for channels
|
||||
o BFD: Support for VRFs
|
||||
o Graceful restart command
|
||||
o Redesigned filtering code
|
||||
o Many bugfixes
|
||||
|
||||
Notes:
|
||||
|
||||
Previous version introduced an error in handling of OSPF NSSA-LSA, causing
|
||||
compatibility issues with proper implementations. The error is fixed in this
|
||||
version, therefore there are compatibility issues in OSPF NSSA areas between
|
||||
this and previous version.
|
||||
|
||||
|
||||
Version 2.0.4 (2019-02-27)
|
||||
o OSPF: Opaque LSAs (RFC 5250)
|
||||
o OSPF: DN-bit handling (RFC 4576)
|
||||
o Preferred route counters are back
|
||||
o Important BGP bugfix
|
||||
o Several bugfixes related to route propagation
|
||||
o some minor bugfixes
|
||||
|
||||
Version 2.0.3 (2019-01-05)
|
||||
o MRT table dumps (RFC 6396)
|
||||
o BGP Long-lived graceful restart
|
||||
o BGP: Optional import table (Adj-RIB-In)
|
||||
o BGP: Extend 'next hop keep' and 'next hop self' options
|
||||
o BGP: Improved VRF support
|
||||
o OSPF: Authentication trailer for OSPFv3 (RFC 7166)
|
||||
o Babel: New option to randomize router ID
|
||||
o Filter: Custom route attributes
|
||||
o Filter: Support for src accessor to SADR source prefix
|
||||
o Filter: Support for VPN_RD sets
|
||||
o Filter: Make ifname attribute modifiable
|
||||
o Perf: Protocol to measure BIRD performance internally
|
||||
o More verbose error messages in config processing
|
||||
o Log file size limit / log rotation
|
||||
o Many bugfixes
|
||||
|
||||
Notes:
|
||||
|
||||
Export of routes to RS EBGP (route server) sessions from other sources than
|
||||
RS EBGP sessions was changed that ASN is no longer prepended to BGP_PATH in
|
||||
that case. The change does not affect regular BGP configurations or regular
|
||||
route servers that have only RS EBGP peers.
|
||||
|
||||
For BGP route servers and route reflectors, the default value of option
|
||||
'next hop keep' was changed to a more appropriate value.
|
||||
|
||||
Attributes for OSPF and Babel metrics are no longer reset when exported to
|
||||
these protocols and could be set anywhere in BIRD. As a result, OSPF metric is
|
||||
kept when a route is reannounced between OSPF instances. Also, when route is
|
||||
exported to OSPF with both ospf_metric1 and ospf_metric2 attributes it is now
|
||||
propagated as OSPF-E2 route instead of as OSPF-E1 route.
|
||||
|
||||
Compiling BIRD with --enable-debug no longer automatically activates debug
|
||||
mode (-d option) nor local mode (-l option). Also, debug mode with output to
|
||||
file (-D option) no longer not forces foreground mode (-f option).
|
||||
|
||||
The configure script now uses standard option --runstatedir, the old option
|
||||
--with-runtimedir is deprecated.
|
||||
|
||||
|
||||
Version 2.0.2 (2018-03-22)
|
||||
o Source-specific routing support for Linux kernel and Babel
|
||||
o BGP: New option 'disable after cease'
|
||||
o Filter: Allow silent filter execution
|
||||
o Filter: Fixed stack overflow in BGP mask expressions.
|
||||
o Several bugfixes
|
||||
|
||||
Notes:
|
||||
|
||||
Syntax prefix:netmask for IPv4 prefixes was dropped. Just use prefix/pxlen.
|
||||
|
||||
|
||||
Version 2.0.1 (2018-01-16)
|
||||
o Linux MPLS kernel support
|
||||
o Better handling of channels inherited from templates
|
||||
o Default EBGP Route Propagation Behavior without Policies (RFC 8212)
|
||||
o Many bugfixes
|
||||
|
||||
Notes:
|
||||
|
||||
To satisfy requirements of RFC 8212, external BGP protocols now require
|
||||
explicit configuration of import and export policies.
|
||||
|
||||
|
||||
Version 2.0.0 (2017-12-11)
|
||||
o Integrated IPv4 + IPv6 design
|
||||
o Support for MPLS next hops
|
||||
o Support for VPNv4 and VPNv6 networks
|
||||
o Microsecond timers infrastructure
|
||||
o Basic VRF support
|
||||
o Simplified autoconf scripts
|
||||
o BGP: Shutdown communication (RFC 8203)
|
||||
o Babel: Support for dual-stack IPv4/IPv6
|
||||
o Babel: Many improvements and bugfixes
|
||||
o Major BGP protocol redesign
|
||||
o Full support for Multiprotocol BGP
|
||||
o BGP multicast support (SAFI 2)
|
||||
o BGP flowspec support (RFC 5575)
|
||||
o BGP with MPLS labels (RFC 3107)
|
||||
o BGP MPLS/VPN support (RFC 4364)
|
||||
o BGP 6PE - IPv6 NLRI over IPv4 MPLS (RFC 4798)
|
||||
o BGP IPv4 NLRI with an IPv6 Next Hop (RFC 5549)
|
||||
o BGP Confederations (RFC 5065)
|
||||
o BGP Shutdown communication (RFC 8203)
|
||||
o BGP: Allow exchanging LOCAL_PREF with eBGP peers
|
||||
o BGP: Allow to specify interface for regular sessions
|
||||
o BGP: New option 'disable after cease'
|
||||
o OSPF: Support of address families in OSPFv3
|
||||
o OSPF: Enable ECMP and Link detection by default
|
||||
o RAdv: Support for more specific routes (RFC 4191)
|
||||
o RAdv: Proper handling of prefix retraction
|
||||
o Filter: Allow silent filter execution
|
||||
o Filter: Fixed stack overflow in BGP mask expressions
|
||||
o Several bug fixes
|
||||
o RIP: Enable ECMP and Link detection by default
|
||||
o Redesign of RPKI handling
|
||||
o New RPKI-Router protocol
|
||||
o Static: Minor overhaul
|
||||
o Static: Support for all new route types
|
||||
o Kenrel: Default Linux kernel metric changed to 32
|
||||
o Kernel: Fix IPv6 ECMP handling with Linux 4.11+
|
||||
o Update of show route command
|
||||
o BIRD client persistent history
|
||||
o New build system
|
||||
o Unit tests
|
||||
o ...
|
||||
|
||||
Notes:
|
||||
|
||||
Tables are now defined with appropriate net type keyword. Protocols and tables
|
||||
are now connected by explicit channels, most related protocol options (table,
|
||||
import, export, ...) are now channel options. See doc/bird.conf.example2 for
|
||||
configuration examples. Some options were removed/replaced.
|
||||
|
||||
|
||||
Version 1.6.3 (2016-12-21)
|
||||
o Large BGP communities
|
||||
|
2
README
2
README
@ -19,7 +19,7 @@ Public License.
|
||||
What do we support
|
||||
==================
|
||||
|
||||
o Both IPv4 and IPv6 (use --enable-ipv6 when configuring)
|
||||
o Both IPv4 and IPv6
|
||||
o Multiple routing tables
|
||||
o Border Gateway Protocol (BGPv4)
|
||||
o Routing Information Protocol (RIPv2, RIPng)
|
||||
|
98
aclocal.m4
vendored
98
aclocal.m4
vendored
@ -1,6 +1,25 @@
|
||||
dnl ** Additional Autoconf tests for BIRD configure script
|
||||
dnl ** (c) 1999 Martin Mares <mj@ucw.cz>
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_THREAD_LOCAL],
|
||||
[
|
||||
AC_CACHE_CHECK(
|
||||
[whether _Thread_local is known],
|
||||
[bird_cv_thread_local],
|
||||
AC_COMPILE_IFELSE([
|
||||
AC_LANG_PROGRAM(
|
||||
[
|
||||
_Thread_local static int x = 42;
|
||||
],
|
||||
[]
|
||||
)
|
||||
],
|
||||
[bird_cv_thread_local=yes],
|
||||
[bird_cv_thread_local=no]
|
||||
)
|
||||
)
|
||||
])
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_PTHREADS],
|
||||
[
|
||||
bird_tmp_cflags="$CFLAGS"
|
||||
@ -31,6 +50,40 @@ AC_DEFUN([BIRD_CHECK_PTHREADS],
|
||||
CFLAGS="$bird_tmp_cflags"
|
||||
])
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_MPLS_KERNEL],
|
||||
[
|
||||
AC_CACHE_CHECK(
|
||||
[for Linux MPLS headers],
|
||||
[bird_cv_mpls_kernel],
|
||||
[
|
||||
AC_COMPILE_IFELSE(
|
||||
[
|
||||
AC_LANG_PROGRAM(
|
||||
[
|
||||
#include <linux/lwtunnel.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
#include <sys/socket.h>
|
||||
void t(int arg);
|
||||
],
|
||||
[
|
||||
t(AF_MPLS);
|
||||
t(RTA_VIA);
|
||||
t(RTA_NEWDST);
|
||||
t(RTA_ENCAP_TYPE);
|
||||
t(RTA_ENCAP);
|
||||
struct rtvia rtvia;
|
||||
t(LWTUNNEL_ENCAP_MPLS);
|
||||
]
|
||||
)
|
||||
],
|
||||
[bird_cv_mpls_kernel=yes],
|
||||
[bird_cv_mpls_kernel=no]
|
||||
)
|
||||
]
|
||||
)
|
||||
])
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_ANDROID_GLOB],
|
||||
[
|
||||
AC_CACHE_CHECK(
|
||||
@ -97,6 +150,30 @@ AC_DEFUN([BIRD_CHECK_ANDROID_LOG],
|
||||
)
|
||||
])
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_LTO],
|
||||
[
|
||||
bird_tmp_cflags="$CFLAGS"
|
||||
bird_tmp_ldflags="$LDFLAGS"
|
||||
CFLAGS="-flto"
|
||||
LDFLAGS="-flto=4"
|
||||
|
||||
AC_CACHE_CHECK(
|
||||
[whether link time optimizer is available],
|
||||
[bird_cv_c_lto],
|
||||
[
|
||||
AC_LINK_IFELSE(
|
||||
[AC_LANG_PROGRAM()],
|
||||
[bird_cv_c_lto=yes],
|
||||
[bird_cv_c_lto=no]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
CFLAGS="$bird_tmp_cflags"
|
||||
LDFLAGS="$bird_tmp_ldflags"
|
||||
])
|
||||
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_GCC_OPTION],
|
||||
[
|
||||
bird_tmp_cflags="$CFLAGS"
|
||||
@ -141,3 +218,24 @@ AC_DEFUN([BIRD_CHECK_PROG_FLAVOR_GNU],
|
||||
)
|
||||
esac
|
||||
])
|
||||
|
||||
AC_DEFUN([BIRD_CHECK_BISON_VERSION],
|
||||
[
|
||||
$1=`bison --version | ( read line; echo ${line##* } )`
|
||||
case "$$1" in
|
||||
1.* | 2.0* | 2.1* | 2.2* | 2.3*)
|
||||
AC_MSG_ERROR([Provided Bison version $$1 is too old, need at least 2.4])
|
||||
;;
|
||||
2.*)
|
||||
bird_bison_synclines=no
|
||||
bird_bison_enhanced_error=no
|
||||
;;
|
||||
3.* | 4.* | 5.* | 6.* | 7.* | 8.* | 9.*)
|
||||
bird_bison_synclines=yes
|
||||
bird_bison_enhanced_error=yes
|
||||
;;
|
||||
*)
|
||||
AC_MSG_ERROR([Couldn't parse Bison version $$1. Call the developers for help.])
|
||||
;;
|
||||
esac
|
||||
])
|
||||
|
154
bird-gdb.py
Normal file
154
bird-gdb.py
Normal file
@ -0,0 +1,154 @@
|
||||
class BIRDPrinter:
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
@classmethod
|
||||
def lookup(cls, val):
|
||||
if val.type.code != cls.typeCode:
|
||||
return None
|
||||
if val.type.tag != cls.typeTag:
|
||||
return None
|
||||
|
||||
return cls(val)
|
||||
|
||||
|
||||
class BIRDFValPrinter(BIRDPrinter):
|
||||
"Print BIRD\s struct f_val"
|
||||
typeCode = gdb.TYPE_CODE_STRUCT
|
||||
typeTag = "f_val"
|
||||
|
||||
codemap = {
|
||||
"T_INT": "i",
|
||||
"T_BOOL": "i",
|
||||
"T_PAIR": "i",
|
||||
"T_QUAD": "i",
|
||||
"T_ENUM_RTS": "i",
|
||||
"T_ENUM_BGP_ORIGIN": "i",
|
||||
"T_ENUM_SCOPE": "i",
|
||||
"T_ENUM_RTC": "i",
|
||||
"T_ENUM_RTD": "i",
|
||||
"T_ENUM_ROA": "i",
|
||||
"T_ENUM_NETTYPE": "i",
|
||||
"T_ENUM_RA_PREFERENCE": "i",
|
||||
"T_ENUM_AF": "i",
|
||||
"T_IP": "ip",
|
||||
"T_NET": "net",
|
||||
"T_STRING": "s",
|
||||
"T_PATH_MASK": "path_mask",
|
||||
"T_PATH": "ad",
|
||||
"T_CLIST": "ad",
|
||||
"T_EC": "ec",
|
||||
"T_ECLIST": "ad",
|
||||
"T_LC": "lc",
|
||||
"T_LCLIST": "ad",
|
||||
"T_RD": "ec",
|
||||
"T_PATH_MASK_ITEM": "pmi",
|
||||
"T_SET": "t",
|
||||
"T_PREFIX_SET": "ti",
|
||||
}
|
||||
|
||||
def to_string(self):
|
||||
code = self.val['type']
|
||||
if code.type.code != gdb.TYPE_CODE_ENUM or code.type.tag != "f_type":
|
||||
raise Exception("Strange 'type' element in f_val")
|
||||
|
||||
if str(code) == "T_VOID":
|
||||
return "T_VOID"
|
||||
else:
|
||||
return "(%(c)s) %(v)s" % { "c": code, "v": self.val['val'][self.codemap[str(code)]] }
|
||||
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
class BIRDFValStackPrinter(BIRDPrinter):
|
||||
"Print BIRD's struct f_val_stack"
|
||||
typeCode = gdb.TYPE_CODE_STRUCT
|
||||
typeTag = "f_val_stack"
|
||||
|
||||
def to_string(self):
|
||||
cnt = self.val['cnt']
|
||||
return ("Value stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
|
||||
"\n\t".join([ (".val[%(n) 3d] = " % { "n": n}) + str(self.val['val'][n]) for n in range(cnt-1, -1, -1) ])
|
||||
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
class BIRDFInstPrinter(BIRDPrinter):
|
||||
"Print BIRD's struct f_inst"
|
||||
typeCode = gdb.TYPE_CODE_STRUCT
|
||||
typeTag = "f_inst"
|
||||
|
||||
def to_string(self):
|
||||
code = self.val['fi_code']
|
||||
if str(code) == "FI_NOP":
|
||||
return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
|
||||
return "%(code)s:\t%(lineno) 6dL\t%(size)6dS\tnext = %(next)s: .i_%(code)s = %(union)s" % {
|
||||
"code": str(code),
|
||||
"lineno": self.val['lineno'],
|
||||
"size": self.val['size'],
|
||||
"next": str(self.val['next']),
|
||||
"union": str(self.val['i_' + str(code)])
|
||||
}
|
||||
|
||||
# def children(self): # children iterator
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
class BIRDFLineItemPrinter(BIRDPrinter):
|
||||
"Print BIRD's struct f_line_item"
|
||||
typeCode = gdb.TYPE_CODE_STRUCT
|
||||
typeTag = "f_line_item"
|
||||
|
||||
def to_string(self):
|
||||
code = self.val['fi_code']
|
||||
if str(code) == "FI_NOP":
|
||||
return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
|
||||
return "%(code)s:\t%(lineno) 6dL\t%(flags)2dF: .i_%(code)s = %(union)s" % {
|
||||
"code": str(code),
|
||||
"lineno": self.val['lineno'],
|
||||
"flags": self.val['flags'],
|
||||
"union": str(self.val['i_' + str(code)])
|
||||
}
|
||||
|
||||
class BIRDFLinePrinter(BIRDPrinter):
|
||||
"Print BIRD's struct f_line"
|
||||
typeCode = gdb.TYPE_CODE_STRUCT
|
||||
typeTag = "f_line"
|
||||
|
||||
def to_string(self):
|
||||
cnt = self.val['len']
|
||||
return ("FLine (%(cnt)d, args=%(args)d): " % { "cnt": cnt, "args" : self.val['args'] } + \
|
||||
", ".join([
|
||||
".items[%(n) 3d] = %(code)s" % {
|
||||
"n": n,
|
||||
"code": str(self.val['items'][n]['fi_code']),
|
||||
} if n % 8 == 0 else str(self.val['items'][n]['fi_code']) for n in range(cnt)]))
|
||||
|
||||
|
||||
class BIRDFExecStackPrinter(BIRDPrinter):
|
||||
"Print BIRD's struct f_exec_stack"
|
||||
typeCode = gdb.TYPE_CODE_STRUCT
|
||||
typeTag = "f_exec_stack"
|
||||
|
||||
def to_string(self):
|
||||
cnt = self.val['cnt']
|
||||
return ("Exec stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
|
||||
"\n\t".join([ ".item[%(n) 3d] = %(retflag)d V%(ventry) 3d P%(pos) 4d %(line)s" % {
|
||||
"retflag": self.val['item'][n]['emask'],
|
||||
"ventry": self.val['item'][n]['ventry'],
|
||||
"pos": self.val['item'][n]['pos'],
|
||||
"line": str(self.val['item'][n]['line'].dereference()),
|
||||
"n": n
|
||||
} for n in range(cnt-1, -1, -1) ])
|
||||
|
||||
def register_printers(objfile):
|
||||
objfile.pretty_printers.append(BIRDFInstPrinter.lookup)
|
||||
objfile.pretty_printers.append(BIRDFValPrinter.lookup)
|
||||
objfile.pretty_printers.append(BIRDFValStackPrinter.lookup)
|
||||
objfile.pretty_printers.append(BIRDFLineItemPrinter.lookup)
|
||||
objfile.pretty_printers.append(BIRDFLinePrinter.lookup)
|
||||
objfile.pretty_printers.append(BIRDFExecStackPrinter.lookup)
|
||||
|
||||
register_printers(gdb.current_objfile())
|
||||
|
||||
print("BIRD pretty printers loaded OK.")
|
58
bird.conf
58
bird.conf
@ -1,44 +1,58 @@
|
||||
/*
|
||||
* This is an example configuration file.
|
||||
* This is a simple example configuration file with no aim for completeness.
|
||||
* See documentation for full description.
|
||||
*/
|
||||
|
||||
# Yet another comment
|
||||
|
||||
# Router ID in IPv4 format
|
||||
router id 62.168.0.1;
|
||||
|
||||
define xyzzy = (120+10);
|
||||
# Load device information from kernel.
|
||||
protocol device {}
|
||||
|
||||
protocol device {
|
||||
# Generate direct routes for interfaces. Useful on BSD.
|
||||
protocol direct {
|
||||
ipv4;
|
||||
disabled;
|
||||
# interface "eth*", "ppp*";
|
||||
}
|
||||
|
||||
protocol direct {
|
||||
ipv6;
|
||||
disabled;
|
||||
}
|
||||
|
||||
# Feed routes to kernel FIB
|
||||
protocol kernel {
|
||||
disabled;
|
||||
# learn; # Learn all routes from the kernel
|
||||
ipv4 { export all; import all; };
|
||||
learn; # Learn all routes from the kernel
|
||||
# scan time 10; # Scan kernel tables every 10 seconds
|
||||
}
|
||||
|
||||
protocol kernel {
|
||||
ipv6 { import all; };
|
||||
learn;
|
||||
}
|
||||
|
||||
# Static route feed
|
||||
protocol static {
|
||||
# disabled;
|
||||
ipv4 { export all; };
|
||||
route 10.0.0.0/24 via 55.55.55.44;
|
||||
route 10.10.0.0/16 blackhole;
|
||||
route 10.20.0.0/20 unreachable;
|
||||
route 10.30.50.0/28 prohibit;
|
||||
}
|
||||
|
||||
route fec0:2::/64 blackhole;
|
||||
route fec0:3::/64 unreachable;
|
||||
route fec0:4::/64 prohibit;
|
||||
|
||||
# route 0.0.0.0/0 via 195.113.31.113;
|
||||
# route 62.168.0.0/25 unreachable;
|
||||
# route 1.2.3.4/32 via 195.113.31.124;
|
||||
# route 10.0.0.0/8 unreachable;
|
||||
# route 10.1.1.0:255.255.255.0 via 62.168.0.3;
|
||||
# route 10.1.2.0:255.255.255.0 via 62.168.0.3;
|
||||
# route 10.1.3.0:255.255.255.0 via 62.168.0.4;
|
||||
# route 10.2.0.0/24 via "arc0";
|
||||
export all;
|
||||
protocol static {
|
||||
ipv6 { export all; };
|
||||
route 2001:db8:1::/48 via 5555::6666;
|
||||
route 2001:db8:2::/48 blackhole;
|
||||
route 2001:db8:3::/48 prohibit;
|
||||
route 2001:db8:4::/48 unreachable;
|
||||
}
|
||||
|
||||
protocol rip {
|
||||
ipv4;
|
||||
}
|
||||
|
||||
protocol rip ng {
|
||||
ipv6;
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
source=commands.c util.c client.c
|
||||
root-rel=../
|
||||
dir-name=client
|
||||
src := commands.c util.c client.c
|
||||
obj := $(src-o-files)
|
||||
|
||||
clients := $(client) birdcl
|
||||
$(all-client)
|
||||
$(conf-y-targets): $(s)cmds.Y
|
||||
|
||||
source-dep := $(source) $(addsuffix .c,$(clients))
|
||||
$(exedir)/birdc: $(o)birdc.o
|
||||
$(exedir)/birdc: LIBS += $(CLIENT_LIBS)
|
||||
|
||||
subdir: $(addsuffix .o,$(clients))
|
||||
|
||||
include ../Rules
|
||||
$(exedir)/birdcl: $(o)birdcl.o
|
||||
|
@ -29,6 +29,9 @@ static int prompt_active;
|
||||
extern int _rl_vis_botlin;
|
||||
extern void _rl_move_vert(int);
|
||||
|
||||
#define HISTORY "/.birdc_history"
|
||||
static char *history_file;
|
||||
|
||||
static void
|
||||
add_history_dedup(char *cmd)
|
||||
{
|
||||
@ -137,9 +140,25 @@ input_help(int arg, int key UNUSED)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
history_init(void)
|
||||
{
|
||||
const char *homedir = getenv("HOME");
|
||||
if (!homedir)
|
||||
homedir = ".";
|
||||
history_file = malloc(strlen(homedir) + sizeof(HISTORY));
|
||||
if (!history_file)
|
||||
die("couldn't alloc enough memory for history file name");
|
||||
|
||||
sprintf(history_file, "%s%s", homedir, HISTORY);
|
||||
read_history(history_file);
|
||||
}
|
||||
|
||||
void
|
||||
input_init(void)
|
||||
{
|
||||
if (interactive)
|
||||
history_init();
|
||||
rl_readline_name = "birdc";
|
||||
rl_add_defun("bird-complete", input_complete, '\t');
|
||||
rl_add_defun("bird-help", input_help, '?');
|
||||
@ -217,5 +236,7 @@ cleanup(void)
|
||||
return;
|
||||
|
||||
input_hide();
|
||||
if (interactive)
|
||||
write_history(history_file);
|
||||
rl_callback_handler_remove();
|
||||
}
|
||||
|
@ -7,8 +7,8 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/resource.h"
|
||||
|
@ -1,33 +1,34 @@
|
||||
source=cf-parse.tab.c cf-lex.c conf.c
|
||||
root-rel=../
|
||||
src := cf-parse.tab.c cf-lex.c conf.c
|
||||
obj := $(src-o-files)
|
||||
|
||||
include ../Rules
|
||||
$(all-daemon)
|
||||
|
||||
conf-src=$(srcdir)/conf
|
||||
conf-fragments=$(conf-src)/confbase.Y @CONFS@ $(addsuffix /config.Y,$(static-dir-paths))
|
||||
tests_objs := $(tests_objs) $(src-o-files)
|
||||
|
||||
ifdef DEBUG
|
||||
BISON_DEBUG=-t
|
||||
#FLEX_DEBUG=-d
|
||||
endif
|
||||
|
||||
cf-parse.tab.h: cf-parse.tab.c
|
||||
$(o)cf-parse.y: $(s)gen_parser.m4
|
||||
$(o)keywords.h: $(s)gen_keywords.m4
|
||||
$(o)commands.h: $(s)gen_commands.m4
|
||||
|
||||
cf-parse.tab.c: cf-parse.y
|
||||
$(BISON) -bcf-parse -dv -pcf_ $(BISON_DEBUG) cf-parse.y
|
||||
$(conf-y-targets): $(s)confbase.Y $(s)flowspec.Y
|
||||
$(M4) $(M4FLAGS) -P $(if $(word 2,$(filter %.m4,$^)),$(error "Too many M4 scripts for one target"),$(filter %.m4,$^)) $(filter %.Y,$^) >$@
|
||||
|
||||
cf-parse.y: $(conf-fragments) $(conf-src)/gen_parser.m4
|
||||
$(M4) -P $(conf-src)/gen_parser.m4 $(conf-fragments) >cf-parse.y
|
||||
$(o)cf-parse.tab.h: $(o)cf-parse.tab.c
|
||||
|
||||
keywords.h: $(conf-fragments) $(conf-src)/gen_keywords.m4
|
||||
$(M4) -P $(conf-src)/gen_keywords.m4 $(conf-fragments) >keywords.h
|
||||
$(o)cf-parse.tab.c: $(o)cf-parse.y
|
||||
$(BISON) $(BISON_DEBUG) $(BISONFLAGS) -dv -pcf_ -b $(@:.tab.c=) $<
|
||||
|
||||
commands.h: $(conf-fragments) $(conf-src)/gen_commands.m4 $(srcdir)/client/cmds.m4
|
||||
$(M4) -P $(conf-src)/gen_commands.m4 $(srcdir)/client/cmds.m4 $(conf-fragments) | sort >commands.h
|
||||
$(o)cf-lex.c: $(s)cf-lex.l
|
||||
$(FLEX) $(FLEX_DEBUG) -f -s -B -8 -Pcf_ -o$@ $<
|
||||
|
||||
cf-lex.c: cf-lex.l
|
||||
$(FLEX) $(FLEX_DEBUG) -s -B -8 -ocf-lex.c -Pcf_ cf-lex.l
|
||||
$(o)cf-lex.o: CFLAGS+=-Wno-sign-compare -Wno-unused-function
|
||||
|
||||
depend: keywords.h commands.h cf-parse.tab.c cf-lex.c
|
||||
prepare: $(o)keywords.h $(o)commands.h $(o)cf-parse.tab.h
|
||||
|
||||
cf-lex.o: CFLAGS+=-Wno-sign-compare -Wno-unused-function
|
||||
$(addprefix $(o), cf-parse.y keywords.h commands.h cf-parse.tab.h cf-parse.tab.c cf-lex.c): $(objdir)/.dir-stamp
|
||||
|
||||
$(call clean,cf-parse.tab.h cf-parse.tab.c cf-parse.y keywords.h commands.h cf-lex.c cf-parse.output)
|
||||
|
312
conf/cf-lex.l
312
conf/cf-lex.l
@ -45,6 +45,7 @@
|
||||
#include "nest/route.h"
|
||||
#include "nest/protocol.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/f-inst.h"
|
||||
#include "conf/conf.h"
|
||||
#include "conf/cf-parse.tab.h"
|
||||
#include "lib/string.h"
|
||||
@ -64,7 +65,7 @@ struct keyword {
|
||||
#endif
|
||||
|
||||
|
||||
static uint cf_hash(byte *c);
|
||||
static uint cf_hash(const byte *c);
|
||||
|
||||
#define KW_KEY(n) n->name
|
||||
#define KW_NEXT(n) n->next
|
||||
@ -87,7 +88,7 @@ HASH_DEFINE_REHASH_FN(SYM, struct symbol)
|
||||
HASH(struct keyword) kw_hash;
|
||||
|
||||
|
||||
static struct sym_scope *conf_this_scope;
|
||||
struct sym_scope *conf_this_scope;
|
||||
|
||||
linpool *cfg_mem;
|
||||
|
||||
@ -95,6 +96,15 @@ int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
|
||||
struct include_file_stack *ifs;
|
||||
static struct include_file_stack *ifs_head;
|
||||
|
||||
#define QUOTED_BUFFER_SIZE 4096
|
||||
static BUFFER_(char) quoted_buffer;
|
||||
static char quoted_buffer_data[QUOTED_BUFFER_SIZE];
|
||||
static inline void quoted_buffer_init(void) {
|
||||
quoted_buffer.used = 0;
|
||||
quoted_buffer.size = QUOTED_BUFFER_SIZE;
|
||||
quoted_buffer.data = quoted_buffer_data;
|
||||
}
|
||||
|
||||
#define MAX_INCLUDE_DEPTH 8
|
||||
|
||||
#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd);
|
||||
@ -105,6 +115,8 @@ static struct include_file_stack *ifs_head;
|
||||
static void cf_include(char *arg, int alen);
|
||||
static int check_eof(void);
|
||||
|
||||
static enum yytokentype cf_lex_symbol(const char *data);
|
||||
|
||||
%}
|
||||
|
||||
%option noyywrap
|
||||
@ -112,24 +124,26 @@ static int check_eof(void);
|
||||
%option nounput
|
||||
%option noreject
|
||||
|
||||
%x COMMENT CCOMM CLI
|
||||
%x COMMENT CCOMM CLI QUOTED APOSTROPHED INCLUDE
|
||||
|
||||
ALPHA [a-zA-Z_]
|
||||
DIGIT [0-9]
|
||||
XIGIT [0-9a-fA-F]
|
||||
ALNUM [a-zA-Z_0-9]
|
||||
WHITE [ \t]
|
||||
include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
|
||||
|
||||
%%
|
||||
{include} {
|
||||
char *start, *end;
|
||||
|
||||
^{WHITE}*include{WHITE}*\" {
|
||||
if (!ifs->depth)
|
||||
cf_error("Include not allowed in CLI");
|
||||
|
||||
start = strchr(yytext, '"');
|
||||
start++;
|
||||
BEGIN(INCLUDE);
|
||||
}
|
||||
|
||||
<INCLUDE>[^"\n]+["]{WHITE}*; {
|
||||
char *start, *end;
|
||||
|
||||
start = yytext;
|
||||
|
||||
end = strchr(start, '"');
|
||||
*end = 0;
|
||||
@ -138,38 +152,121 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
|
||||
cf_error("Include with empty argument");
|
||||
|
||||
cf_include(start, end-start);
|
||||
|
||||
BEGIN(INITIAL);
|
||||
}
|
||||
|
||||
<INCLUDE>["] cf_error("Include with empty argument");
|
||||
<INCLUDE>. cf_error("Unterminated include");
|
||||
<INCLUDE>\n cf_error("Unterminated include");
|
||||
<INCLUDE><<EOF>> cf_error("Unterminated include");
|
||||
|
||||
|
||||
{DIGIT}+:{DIGIT}+ {
|
||||
uint len1 UNUSED, len2;
|
||||
u64 l;
|
||||
char *e;
|
||||
|
||||
errno = 0;
|
||||
l = bstrtoul10(yytext, &e);
|
||||
if (!e || (*e != ':') || (errno == ERANGE) || (l >> 32))
|
||||
cf_error("ASN out of range");
|
||||
|
||||
if (l >> 16)
|
||||
{
|
||||
len1 = 32;
|
||||
len2 = 16;
|
||||
cf_lval.i64 = (2ULL << 48) | (((u64) l) << len2);
|
||||
}
|
||||
else
|
||||
{
|
||||
len1 = 16;
|
||||
len2 = 32;
|
||||
cf_lval.i64 = 0 | (((u64) l) << len2);
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
l = bstrtoul10(e+1, &e);
|
||||
if (!e || *e || (errno == ERANGE) || (l >> len2))
|
||||
cf_error("Number out of range");
|
||||
cf_lval.i64 |= l;
|
||||
|
||||
return VPN_RD;
|
||||
}
|
||||
|
||||
[02]:{DIGIT}+:{DIGIT}+ {
|
||||
uint len1, len2;
|
||||
u64 l;
|
||||
char *e;
|
||||
|
||||
if (yytext[0] == '0')
|
||||
{
|
||||
cf_lval.i64 = 0;
|
||||
len1 = 16;
|
||||
len2 = 32;
|
||||
}
|
||||
else
|
||||
{
|
||||
cf_lval.i64 = 2ULL << 48;
|
||||
len1 = 32;
|
||||
len2 = 16;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
l = bstrtoul10(yytext+2, &e);
|
||||
if (!e || (*e != ':') || (errno == ERANGE) || (l >> len1))
|
||||
cf_error("ASN out of range");
|
||||
cf_lval.i64 |= ((u64) l) << len2;
|
||||
|
||||
errno = 0;
|
||||
l = bstrtoul10(e+1, &e);
|
||||
if (!e || *e || (errno == ERANGE) || (l >> len2))
|
||||
cf_error("Number out of range");
|
||||
cf_lval.i64 |= l;
|
||||
|
||||
return VPN_RD;
|
||||
}
|
||||
|
||||
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+:{DIGIT}+ {
|
||||
unsigned long int l;
|
||||
ip4_addr ip4;
|
||||
char *e;
|
||||
|
||||
cf_lval.i64 = 1ULL << 48;
|
||||
|
||||
e = strchr(yytext, ':');
|
||||
*e++ = '\0';
|
||||
if (!ip4_pton(yytext, &ip4))
|
||||
cf_error("Invalid IPv4 address %s in Route Distinguisher", yytext);
|
||||
cf_lval.i64 |= ((u64) ip4_to_u32(ip4)) << 16;
|
||||
|
||||
errno = 0;
|
||||
l = bstrtoul10(e, &e);
|
||||
if (!e || *e || (errno == ERANGE) || (l >> 16))
|
||||
cf_error("Number out of range");
|
||||
cf_lval.i64 |= l;
|
||||
|
||||
return VPN_RD;
|
||||
}
|
||||
|
||||
{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+ {
|
||||
ip4_addr a;
|
||||
if (!ip4_pton(yytext, &a))
|
||||
if (!ip4_pton(yytext, &cf_lval.ip4))
|
||||
cf_error("Invalid IPv4 address %s", yytext);
|
||||
|
||||
#ifdef IPV6
|
||||
cf_lval.i32 = ip4_to_u32(a);
|
||||
return RTRID;
|
||||
#else
|
||||
cf_lval.a = ipa_from_ip4(a);
|
||||
return IPA;
|
||||
#endif
|
||||
return IP4;
|
||||
}
|
||||
|
||||
({XIGIT}*::|({XIGIT}*:){3,})({XIGIT}*|{DIGIT}+\.{DIGIT}+\.{DIGIT}+\.{DIGIT}+) {
|
||||
#ifdef IPV6
|
||||
if (ipa_pton(yytext, &cf_lval.a))
|
||||
return IPA;
|
||||
cf_error("Invalid IPv6 address %s", yytext);
|
||||
#else
|
||||
cf_error("This is an IPv4 router, therefore IPv6 addresses are not supported");
|
||||
#endif
|
||||
if (!ip6_pton(yytext, &cf_lval.ip6))
|
||||
cf_error("Invalid IPv6 address %s", yytext);
|
||||
return IP6;
|
||||
}
|
||||
|
||||
0x{XIGIT}+ {
|
||||
char *e;
|
||||
unsigned long int l;
|
||||
errno = 0;
|
||||
l = strtoul(yytext+2, &e, 16);
|
||||
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
|
||||
l = bstrtoul16(yytext+2, &e);
|
||||
if (!e || *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
|
||||
cf_error("Number out of range");
|
||||
cf_lval.i = l;
|
||||
return NUM;
|
||||
@ -179,8 +276,8 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
|
||||
char *e;
|
||||
unsigned long int l;
|
||||
errno = 0;
|
||||
l = strtoul(yytext, &e, 10);
|
||||
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
|
||||
l = bstrtoul10(yytext, &e);
|
||||
if (!e || *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
|
||||
cf_error("Number out of range");
|
||||
cf_lval.i = l;
|
||||
return NUM;
|
||||
@ -191,26 +288,23 @@ else: {
|
||||
return ELSECOL;
|
||||
}
|
||||
|
||||
({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
|
||||
if(*yytext == '\'') {
|
||||
yytext[yyleng-1] = 0;
|
||||
yytext++;
|
||||
}
|
||||
['] {
|
||||
BEGIN(APOSTROPHED);
|
||||
quoted_buffer_init();
|
||||
}
|
||||
|
||||
struct keyword *k = HASH_FIND(kw_hash, KW, yytext);
|
||||
if (k)
|
||||
{
|
||||
if (k->value > 0)
|
||||
return k->value;
|
||||
else
|
||||
{
|
||||
cf_lval.i = -k->value;
|
||||
return ENUM;
|
||||
}
|
||||
}
|
||||
<APOSTROPHED>{ALNUM}|[-]|[.:] BUFFER_PUSH(quoted_buffer) = yytext[0];
|
||||
<APOSTROPHED>\n cf_error("Unterminated symbol");
|
||||
<APOSTROPHED><<EOF>> cf_error("Unterminated symbol");
|
||||
<APOSTROPHED>['] {
|
||||
BEGIN(INITIAL);
|
||||
BUFFER_PUSH(quoted_buffer) = 0;
|
||||
return cf_lex_symbol(quoted_buffer_data);
|
||||
}
|
||||
<APOSTROPHED>. cf_error("Invalid character in apostrophed symbol");
|
||||
|
||||
cf_lval.s = cf_get_symbol(yytext);
|
||||
return SYM;
|
||||
({ALPHA}{ALNUM}*) {
|
||||
return cf_lex_symbol(yytext);
|
||||
}
|
||||
|
||||
<CLI>(.|\n) {
|
||||
@ -226,13 +320,21 @@ else: {
|
||||
return yytext[0];
|
||||
}
|
||||
|
||||
["][^"\n]*["] {
|
||||
yytext[yyleng-1] = 0;
|
||||
cf_lval.t = cfg_strdup(yytext+1);
|
||||
["] {
|
||||
BEGIN(QUOTED);
|
||||
quoted_buffer_init();
|
||||
}
|
||||
|
||||
<QUOTED>\n cf_error("Unterminated string");
|
||||
<QUOTED><<EOF>> cf_error("Unterminated string");
|
||||
<QUOTED>["] {
|
||||
BEGIN(INITIAL);
|
||||
BUFFER_PUSH(quoted_buffer) = 0;
|
||||
cf_lval.t = cfg_strdup(quoted_buffer_data);
|
||||
return TEXT;
|
||||
}
|
||||
|
||||
["][^"\n]*\n cf_error("Unterminated string");
|
||||
<QUOTED>. BUFFER_PUSH(quoted_buffer) = yytext[0];
|
||||
|
||||
<INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; }
|
||||
|
||||
@ -273,7 +375,7 @@ else: {
|
||||
%%
|
||||
|
||||
static uint
|
||||
cf_hash(byte *c)
|
||||
cf_hash(const byte *c)
|
||||
{
|
||||
uint h = 13 << 24;
|
||||
|
||||
@ -282,7 +384,6 @@ cf_hash(byte *c)
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* IFS stack - it contains structures needed for recursive processing
|
||||
* of include in config files. On the top of the stack is a structure
|
||||
@ -443,7 +544,7 @@ check_eof(void)
|
||||
}
|
||||
|
||||
static struct symbol *
|
||||
cf_new_symbol(byte *c)
|
||||
cf_new_symbol(const byte *c)
|
||||
{
|
||||
struct symbol *s;
|
||||
|
||||
@ -451,11 +552,8 @@ cf_new_symbol(byte *c)
|
||||
if (l > SYM_MAX_LEN)
|
||||
cf_error("Symbol too long");
|
||||
|
||||
s = cfg_alloc(sizeof(struct symbol) + l);
|
||||
s->scope = conf_this_scope;
|
||||
s->class = SYM_VOID;
|
||||
s->def = NULL;
|
||||
s->aux = 0;
|
||||
s = cfg_allocz(sizeof(struct symbol) + l + 1);
|
||||
*s = (struct symbol) { .scope = conf_this_scope, .class = SYM_VOID, };
|
||||
strcpy(s->name, c);
|
||||
|
||||
if (!new_config->sym_hash.data)
|
||||
@ -463,6 +561,8 @@ cf_new_symbol(byte *c)
|
||||
|
||||
HASH_INSERT2(new_config->sym_hash, SYM, new_config->pool, s);
|
||||
|
||||
add_tail(&(new_config->symbols), &(s->n));
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -478,7 +578,7 @@ cf_new_symbol(byte *c)
|
||||
* signify no match.
|
||||
*/
|
||||
struct symbol *
|
||||
cf_find_symbol(struct config *cfg, byte *c)
|
||||
cf_find_symbol(const struct config *cfg, const byte *c)
|
||||
{
|
||||
struct symbol *s;
|
||||
|
||||
@ -486,6 +586,7 @@ cf_find_symbol(struct config *cfg, byte *c)
|
||||
(s = HASH_FIND(cfg->sym_hash, SYM, c, 1)))
|
||||
return s;
|
||||
|
||||
/* In CLI command parsing, fallback points to the current config, otherwise it is NULL. */
|
||||
if (cfg->fallback &&
|
||||
cfg->fallback->sym_hash.data &&
|
||||
(s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, 1)))
|
||||
@ -504,11 +605,33 @@ cf_find_symbol(struct config *cfg, byte *c)
|
||||
* existing symbol is found.
|
||||
*/
|
||||
struct symbol *
|
||||
cf_get_symbol(byte *c)
|
||||
cf_get_symbol(const byte *c)
|
||||
{
|
||||
return cf_find_symbol(new_config, c) ?: cf_new_symbol(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* cf_localize_symbol - get the local instance of given symbol
|
||||
* @sym: the symbol to localize
|
||||
*
|
||||
* This functions finds the symbol that is local to current scope
|
||||
* for purposes of cf_define_symbol().
|
||||
*/
|
||||
struct symbol *
|
||||
cf_localize_symbol(struct symbol *sym)
|
||||
{
|
||||
/* If the symbol type is void, it has been recently allocated just in this scope. */
|
||||
if (!sym->class)
|
||||
return sym;
|
||||
|
||||
/* If the scope is the current, it is already defined in this scope. */
|
||||
if (sym->scope == conf_this_scope)
|
||||
cf_error("Symbol already defined");
|
||||
|
||||
/* Not allocated here yet, doing it now. */
|
||||
return cf_new_symbol(sym->name);
|
||||
}
|
||||
|
||||
struct symbol *
|
||||
cf_default_name(char *template, int *counter)
|
||||
{
|
||||
@ -528,33 +651,32 @@ cf_default_name(char *template, int *counter)
|
||||
cf_error("Unable to generate default name");
|
||||
}
|
||||
|
||||
/**
|
||||
* cf_define_symbol - define meaning of a symbol
|
||||
* @sym: symbol to be defined
|
||||
* @type: symbol class to assign
|
||||
* @def: class dependent data
|
||||
*
|
||||
* Defines new meaning of a symbol. If the symbol is an undefined
|
||||
* one (%SYM_VOID), it's just re-defined to the new type. If it's defined
|
||||
* in different scope, a new symbol in current scope is created and the
|
||||
* meaning is assigned to it. If it's already defined in the current scope,
|
||||
* an error is reported via cf_error().
|
||||
*
|
||||
* Result: Pointer to the newly defined symbol. If we are in the top-level
|
||||
* scope, it's the same @sym as passed to the function.
|
||||
*/
|
||||
struct symbol *
|
||||
cf_define_symbol(struct symbol *sym, int type, void *def)
|
||||
static enum yytokentype
|
||||
cf_lex_symbol(const char *data)
|
||||
{
|
||||
if (sym->class)
|
||||
/* Have we defined such a symbol? */
|
||||
struct symbol *sym = cf_get_symbol(data);
|
||||
cf_lval.s = sym;
|
||||
|
||||
if (sym->class != SYM_VOID)
|
||||
return CF_SYM_KNOWN;
|
||||
|
||||
/* Is it a keyword? */
|
||||
struct keyword *k = HASH_FIND(kw_hash, KW, data);
|
||||
if (k)
|
||||
{
|
||||
if (k->value > 0)
|
||||
return k->value;
|
||||
else
|
||||
{
|
||||
if (sym->scope == conf_this_scope)
|
||||
cf_error("Symbol already defined");
|
||||
sym = cf_new_symbol(sym->name);
|
||||
cf_lval.i = -k->value;
|
||||
return ENUM;
|
||||
}
|
||||
sym->class = type;
|
||||
sym->def = def;
|
||||
return sym;
|
||||
}
|
||||
|
||||
/* OK, undefined symbol */
|
||||
cf_lval.s = sym;
|
||||
return CF_SYM_UNDEFINED;
|
||||
}
|
||||
|
||||
static void
|
||||
@ -597,7 +719,8 @@ cf_lex_init(int is_cli, struct config *c)
|
||||
else
|
||||
BEGIN(INITIAL);
|
||||
|
||||
conf_this_scope = cfg_allocz(sizeof(struct sym_scope));
|
||||
c->root_scope = cfg_allocz(sizeof(struct sym_scope));
|
||||
conf_this_scope = c->root_scope;
|
||||
conf_this_scope->active = 1;
|
||||
}
|
||||
|
||||
@ -614,12 +737,13 @@ cf_lex_init(int is_cli, struct config *c)
|
||||
void
|
||||
cf_push_scope(struct symbol *sym)
|
||||
{
|
||||
struct sym_scope *s = cfg_alloc(sizeof(struct sym_scope));
|
||||
struct sym_scope *s = cfg_allocz(sizeof(struct sym_scope));
|
||||
|
||||
s->next = conf_this_scope;
|
||||
conf_this_scope = s;
|
||||
s->active = 1;
|
||||
s->name = sym;
|
||||
s->slots = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -634,6 +758,7 @@ cf_pop_scope(void)
|
||||
{
|
||||
conf_this_scope->active = 0;
|
||||
conf_this_scope = conf_this_scope->next;
|
||||
|
||||
ASSERT(conf_this_scope);
|
||||
}
|
||||
|
||||
@ -647,9 +772,6 @@ cf_pop_scope(void)
|
||||
char *
|
||||
cf_symbol_class_name(struct symbol *sym)
|
||||
{
|
||||
if (cf_symbol_is_constant(sym))
|
||||
return "constant";
|
||||
|
||||
switch (sym->class)
|
||||
{
|
||||
case SYM_VOID:
|
||||
@ -664,8 +786,12 @@ cf_symbol_class_name(struct symbol *sym)
|
||||
return "filter";
|
||||
case SYM_TABLE:
|
||||
return "routing table";
|
||||
case SYM_ROA:
|
||||
return "ROA table";
|
||||
case SYM_ATTRIBUTE:
|
||||
return "custom attribute";
|
||||
case SYM_CONSTANT_RANGE:
|
||||
return "constant";
|
||||
case SYM_VARIABLE_RANGE:
|
||||
return "variable";
|
||||
default:
|
||||
return "unknown type";
|
||||
}
|
||||
|
81
conf/conf.c
81
conf/conf.c
@ -55,6 +55,8 @@
|
||||
#include "lib/timer.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "sysdep/unix/unix.h"
|
||||
|
||||
|
||||
static jmp_buf conf_jmpbuf;
|
||||
|
||||
@ -85,10 +87,10 @@ int undo_available; /* Undo was not requested from last reconfiguration */
|
||||
* further use. Returns a pointer to the structure.
|
||||
*/
|
||||
struct config *
|
||||
config_alloc(const byte *name)
|
||||
config_alloc(const char *name)
|
||||
{
|
||||
pool *p = rp_new(&root_pool, "Config");
|
||||
linpool *l = lp_new(p, 4080);
|
||||
linpool *l = lp_new_default(p);
|
||||
struct config *c = lp_allocz(l, sizeof(struct config));
|
||||
|
||||
/* Duplication of name string in local linear pool */
|
||||
@ -96,13 +98,15 @@ config_alloc(const byte *name)
|
||||
char *ndup = lp_allocu(l, nlen);
|
||||
memcpy(ndup, name, nlen);
|
||||
|
||||
init_list(&c->tests);
|
||||
init_list(&c->symbols);
|
||||
c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
|
||||
c->pool = p;
|
||||
c->mem = l;
|
||||
c->file_name = ndup;
|
||||
c->load_time = now;
|
||||
c->tf_route = c->tf_proto = (struct timeformat){"%T", "%F", 20*3600};
|
||||
c->tf_base = c->tf_log = (struct timeformat){"%F %T", NULL, 0};
|
||||
c->load_time = current_time();
|
||||
c->tf_route = c->tf_proto = TM_ISO_SHORT_MS;
|
||||
c->tf_base = c->tf_log = TM_ISO_LONG_MS;
|
||||
c->gr_wait = DEFAULT_GR_WAIT;
|
||||
|
||||
return c;
|
||||
@ -135,15 +139,16 @@ config_parse(struct config *c)
|
||||
sysdep_preconfig(c);
|
||||
protos_preconfig(c);
|
||||
rt_preconfig(c);
|
||||
roa_preconfig(c);
|
||||
cf_parse();
|
||||
protos_postconfig(c);
|
||||
|
||||
if (EMPTY_LIST(c->protos))
|
||||
cf_error("No protocol is specified in the config file");
|
||||
#ifdef IPV6
|
||||
|
||||
/*
|
||||
if (!c->router_id)
|
||||
cf_error("Router ID must be configured manually on IPv6 routers");
|
||||
#endif
|
||||
cf_error("Router ID must be configured manually");
|
||||
*/
|
||||
|
||||
done = 1;
|
||||
|
||||
cleanup:
|
||||
@ -213,14 +218,17 @@ config_del_obstacle(struct config *c)
|
||||
static int
|
||||
global_commit(struct config *new, struct config *old)
|
||||
{
|
||||
if (!new->hostname)
|
||||
{
|
||||
new->hostname = get_hostname(new->mem);
|
||||
|
||||
if (!new->hostname)
|
||||
log(L_WARN "Cannot determine hostname");
|
||||
}
|
||||
|
||||
if (!old)
|
||||
return 0;
|
||||
|
||||
if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
|
||||
(old->listen_bgp_port != new->listen_bgp_port) ||
|
||||
(old->listen_bgp_flags != new->listen_bgp_flags))
|
||||
log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
|
||||
|
||||
if (!new->router_id)
|
||||
{
|
||||
new->router_id = old->router_id;
|
||||
@ -260,13 +268,14 @@ config_do_commit(struct config *c, int type)
|
||||
if (old_config)
|
||||
old_config->obstacle_count++;
|
||||
|
||||
DBG("filter_commit\n");
|
||||
filter_commit(c, old_config);
|
||||
DBG("sysdep_commit\n");
|
||||
int force_restart = sysdep_commit(c, old_config);
|
||||
DBG("global_commit\n");
|
||||
force_restart |= global_commit(c, old_config);
|
||||
DBG("rt_commit\n");
|
||||
rt_commit(c, old_config);
|
||||
roa_commit(c, old_config);
|
||||
DBG("protos_commit\n");
|
||||
protos_commit(c, old_config, force_restart, type);
|
||||
|
||||
@ -305,7 +314,7 @@ config_done(void *unused UNUSED)
|
||||
* config_commit - commit a configuration
|
||||
* @c: new configuration
|
||||
* @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
|
||||
* @timeout: timeout for undo (or 0 for no timeout)
|
||||
* @timeout: timeout for undo (in seconds; or 0 for no timeout)
|
||||
*
|
||||
* When a configuration is parsed and prepared for use, the
|
||||
* config_commit() function starts the process of reconfiguration.
|
||||
@ -329,7 +338,7 @@ config_done(void *unused UNUSED)
|
||||
* are accepted.
|
||||
*/
|
||||
int
|
||||
config_commit(struct config *c, int type, int timeout)
|
||||
config_commit(struct config *c, int type, uint timeout)
|
||||
{
|
||||
if (shutting_down)
|
||||
{
|
||||
@ -338,8 +347,8 @@ config_commit(struct config *c, int type, int timeout)
|
||||
}
|
||||
|
||||
undo_available = 1;
|
||||
if (timeout > 0)
|
||||
tm_start(config_timer, timeout);
|
||||
if (timeout)
|
||||
tm_start(config_timer, timeout S);
|
||||
else
|
||||
tm_stop(config_timer);
|
||||
|
||||
@ -447,10 +456,28 @@ config_undo(void)
|
||||
return CONF_PROGRESS;
|
||||
}
|
||||
|
||||
int
|
||||
config_status(void)
|
||||
{
|
||||
if (shutting_down)
|
||||
return CONF_SHUTDOWN;
|
||||
|
||||
if (configuring)
|
||||
return future_cftype ? CONF_QUEUED : CONF_PROGRESS;
|
||||
|
||||
return CONF_DONE;
|
||||
}
|
||||
|
||||
btime
|
||||
config_timer_status(void)
|
||||
{
|
||||
return tm_active(config_timer) ? tm_remains(config_timer) : -1;
|
||||
}
|
||||
|
||||
extern void cmd_reconfig_undo_notify(void);
|
||||
|
||||
static void
|
||||
config_timeout(struct timer *t UNUSED)
|
||||
config_timeout(timer *t UNUSED)
|
||||
{
|
||||
log(L_INFO "Config timeout expired, starting undo");
|
||||
cmd_reconfig_undo_notify();
|
||||
@ -477,19 +504,24 @@ config_init(void)
|
||||
* for switching to an empty configuration.
|
||||
*/
|
||||
void
|
||||
order_shutdown(void)
|
||||
order_shutdown(int gr)
|
||||
{
|
||||
struct config *c;
|
||||
|
||||
if (shutting_down)
|
||||
return;
|
||||
|
||||
log(L_INFO "Shutting down");
|
||||
if (!gr)
|
||||
log(L_INFO "Shutting down");
|
||||
else
|
||||
log(L_INFO "Shutting down for graceful restart");
|
||||
|
||||
c = lp_alloc(config->mem, sizeof(struct config));
|
||||
memcpy(c, config, sizeof(struct config));
|
||||
init_list(&c->protos);
|
||||
init_list(&c->tables);
|
||||
c->shutdown = 1;
|
||||
c->gr_down = gr;
|
||||
|
||||
config_commit(c, RECONFIG_HARD, 0);
|
||||
shutting_down = 1;
|
||||
@ -504,7 +536,7 @@ order_shutdown(void)
|
||||
* error in the configuration.
|
||||
*/
|
||||
void
|
||||
cf_error(char *msg, ...)
|
||||
cf_error(const char *msg, ...)
|
||||
{
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
@ -550,6 +582,7 @@ cfg_copy_list(list *dest, list *src, unsigned node_size)
|
||||
{
|
||||
dn = cfg_alloc(node_size);
|
||||
memcpy(dn, sn, node_size);
|
||||
memset(dn, 0, sizeof(node));
|
||||
add_tail(dest, dn);
|
||||
}
|
||||
}
|
||||
|
104
conf/conf.h
104
conf/conf.h
@ -9,10 +9,11 @@
|
||||
#ifndef _BIRD_CONF_H_
|
||||
#define _BIRD_CONF_H_
|
||||
|
||||
#include "sysdep/config.h"
|
||||
#include "lib/ip.h"
|
||||
#include "lib/hash.h"
|
||||
#include "lib/resource.h"
|
||||
#include "lib/timer.h"
|
||||
#include "lib/hash.h"
|
||||
|
||||
|
||||
/* Configuration structure */
|
||||
|
||||
@ -21,25 +22,25 @@ struct config {
|
||||
linpool *mem; /* Linear pool containing configuration data */
|
||||
list protos; /* Configured protocol instances (struct proto_config) */
|
||||
list tables; /* Configured routing tables (struct rtable_config) */
|
||||
list roa_tables; /* Configured ROA tables (struct roa_table_config) */
|
||||
list logfiles; /* Configured log files (sysdep) */
|
||||
list tests; /* Configured unit tests (f_bt_test_suite) */
|
||||
list symbols; /* Configured symbols in config order */
|
||||
|
||||
int mrtdump_file; /* Configured MRTDump file (sysdep, fd in unix) */
|
||||
char *syslog_name; /* Name used for syslog (NULL -> no syslog) */
|
||||
struct rtable_config *master_rtc; /* Configuration of master routing table */
|
||||
const char *syslog_name; /* Name used for syslog (NULL -> no syslog) */
|
||||
struct rtable_config *def_tables[NET_MAX]; /* Default routing tables for each network */
|
||||
struct iface_patt *router_id_from; /* Configured list of router ID iface patterns */
|
||||
|
||||
u32 router_id; /* Our Router ID */
|
||||
ip_addr listen_bgp_addr; /* Listening BGP socket should use this address */
|
||||
unsigned listen_bgp_port; /* Listening BGP socket should use this port (0 is default) */
|
||||
u32 listen_bgp_flags; /* Listening BGP socket should use these flags */
|
||||
unsigned proto_default_debug; /* Default protocol debug mask */
|
||||
unsigned proto_default_mrtdump; /* Default protocol mrtdump mask */
|
||||
u32 proto_default_debug; /* Default protocol debug mask */
|
||||
u32 proto_default_mrtdump; /* Default protocol mrtdump mask */
|
||||
u32 channel_default_debug; /* Default channel debug mask */
|
||||
struct timeformat tf_route; /* Time format for 'show route' */
|
||||
struct timeformat tf_proto; /* Time format for 'show protocol' */
|
||||
struct timeformat tf_log; /* Time format for the logfile */
|
||||
struct timeformat tf_base; /* Time format for other purposes */
|
||||
u32 gr_wait; /* Graceful restart wait timeout */
|
||||
u32 gr_wait; /* Graceful restart wait timeout (sec) */
|
||||
const char *hostname; /* Hostname */
|
||||
|
||||
int cli_debug; /* Tracing of CLI connections and commands */
|
||||
int latency_debug; /* I/O loop tracks duration of each event */
|
||||
@ -54,27 +55,31 @@ struct config {
|
||||
int file_fd; /* File descriptor of main configuration file */
|
||||
HASH(struct symbol) sym_hash; /* Lexer: symbol hash table */
|
||||
struct config *fallback; /* Link to regular config for CLI parsing */
|
||||
struct sym_scope *root_scope; /* Scope for root symbols */
|
||||
int obstacle_count; /* Number of items blocking freeing of this config */
|
||||
int shutdown; /* This is a pseudo-config for daemon shutdown */
|
||||
bird_clock_t load_time; /* When we've got this configuration */
|
||||
int gr_down; /* This is a pseudo-config for graceful restart */
|
||||
btime load_time; /* When we've got this configuration */
|
||||
};
|
||||
|
||||
/* Please don't use these variables in protocols. Use proto_config->global instead. */
|
||||
extern struct config *config; /* Currently active configuration */
|
||||
extern struct config *new_config; /* Configuration being parsed */
|
||||
|
||||
struct config *config_alloc(const byte *name);
|
||||
struct config *config_alloc(const char *name);
|
||||
int config_parse(struct config *);
|
||||
int cli_parse(struct config *);
|
||||
void config_free(struct config *);
|
||||
int config_commit(struct config *, int type, int timeout);
|
||||
int config_commit(struct config *, int type, uint timeout);
|
||||
int config_confirm(void);
|
||||
int config_undo(void);
|
||||
int config_status(void);
|
||||
btime config_timer_status(void);
|
||||
void config_init(void);
|
||||
void cf_error(char *msg, ...) NORET;
|
||||
void cf_error(const char *msg, ...) NORET;
|
||||
void config_add_obstacle(struct config *);
|
||||
void config_del_obstacle(struct config *);
|
||||
void order_shutdown(void);
|
||||
void order_shutdown(int gr);
|
||||
|
||||
#define RECONFIG_NONE 0
|
||||
#define RECONFIG_HARD 1
|
||||
@ -105,18 +110,29 @@ void cfg_copy_list(list *dest, list *src, unsigned node_size);
|
||||
extern int (*cf_read_hook)(byte *buf, uint max, int fd);
|
||||
|
||||
struct symbol {
|
||||
node n; /* In list of symbols in config */
|
||||
struct symbol *next;
|
||||
struct sym_scope *scope;
|
||||
int class;
|
||||
int aux;
|
||||
void *aux2;
|
||||
void *def;
|
||||
char name[1];
|
||||
int class; /* SYM_* */
|
||||
uint flags; /* SYM_FLAG_* */
|
||||
|
||||
union {
|
||||
struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */
|
||||
const struct f_line *function; /* For SYM_FUNCTION */
|
||||
const struct filter *filter; /* For SYM_FILTER */
|
||||
struct rtable_config *table; /* For SYM_TABLE */
|
||||
struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */
|
||||
struct f_val *val; /* For SYM_CONSTANT */
|
||||
uint offset; /* For SYM_VARIABLE */
|
||||
};
|
||||
|
||||
char name[0];
|
||||
};
|
||||
|
||||
struct sym_scope {
|
||||
struct sym_scope *next; /* Next on scope stack */
|
||||
struct symbol *name; /* Name of this scope */
|
||||
uint slots; /* Variable slots */
|
||||
int active; /* Currently entered */
|
||||
};
|
||||
|
||||
@ -129,13 +145,18 @@ struct sym_scope {
|
||||
#define SYM_FUNCTION 3
|
||||
#define SYM_FILTER 4
|
||||
#define SYM_TABLE 5
|
||||
#define SYM_ROA 6
|
||||
#define SYM_ATTRIBUTE 6
|
||||
|
||||
#define SYM_VARIABLE 0x100 /* 0x100-0x1ff are variable types */
|
||||
#define SYM_VARIABLE_RANGE SYM_VARIABLE ... (SYM_VARIABLE | 0xff)
|
||||
#define SYM_CONSTANT 0x200 /* 0x200-0x2ff are variable types */
|
||||
#define SYM_CONSTANT_RANGE SYM_CONSTANT ... (SYM_CONSTANT | 0xff)
|
||||
|
||||
#define SYM_TYPE(s) (((struct f_val *) (s)->def)->type)
|
||||
#define SYM_VAL(s) (((struct f_val *) (s)->def)->val)
|
||||
#define SYM_TYPE(s) ((s)->val->type)
|
||||
#define SYM_VAL(s) ((s)->val->val)
|
||||
|
||||
/* Symbol flags */
|
||||
#define SYM_FLAG_SAME 0x1 /* For SYM_FUNCTION and SYM_FILTER */
|
||||
|
||||
struct include_file_stack {
|
||||
void *buffer; /* Internal lexer state */
|
||||
@ -152,25 +173,46 @@ struct include_file_stack {
|
||||
|
||||
extern struct include_file_stack *ifs;
|
||||
|
||||
extern struct sym_scope *conf_this_scope;
|
||||
|
||||
int cf_lex(void);
|
||||
void cf_lex_init(int is_cli, struct config *c);
|
||||
void cf_lex_unwind(void);
|
||||
|
||||
struct symbol *cf_find_symbol(struct config *cfg, byte *c);
|
||||
struct symbol *cf_find_symbol(const struct config *cfg, const byte *c);
|
||||
|
||||
struct symbol *cf_get_symbol(byte *c);
|
||||
struct symbol *cf_get_symbol(const byte *c);
|
||||
struct symbol *cf_default_name(char *template, int *counter);
|
||||
struct symbol *cf_define_symbol(struct symbol *symbol, int type, void *def);
|
||||
struct symbol *cf_localize_symbol(struct symbol *sym);
|
||||
|
||||
/**
|
||||
* cf_define_symbol - define meaning of a symbol
|
||||
* @sym: symbol to be defined
|
||||
* @type: symbol class to assign
|
||||
* @def: class dependent data
|
||||
*
|
||||
* Defines new meaning of a symbol. If the symbol is an undefined
|
||||
* one (%SYM_VOID), it's just re-defined to the new type. If it's defined
|
||||
* in different scope, a new symbol in current scope is created and the
|
||||
* meaning is assigned to it. If it's already defined in the current scope,
|
||||
* an error is reported via cf_error().
|
||||
*
|
||||
* Result: Pointer to the newly defined symbol. If we are in the top-level
|
||||
* scope, it's the same @sym as passed to the function.
|
||||
*/
|
||||
#define cf_define_symbol(osym_, type_, var_, def_) ({ \
|
||||
struct symbol *sym_ = cf_localize_symbol(osym_); \
|
||||
sym_->class = type_; \
|
||||
sym_->var_ = def_; \
|
||||
sym_; })
|
||||
|
||||
void cf_push_scope(struct symbol *);
|
||||
void cf_pop_scope(void);
|
||||
char *cf_symbol_class_name(struct symbol *sym);
|
||||
|
||||
static inline int cf_symbol_is_constant(struct symbol *sym)
|
||||
{ return (sym->class & 0xff00) == SYM_CONSTANT; }
|
||||
|
||||
|
||||
/* Parser */
|
||||
|
||||
extern char *cf_text;
|
||||
int cf_parse(void);
|
||||
|
||||
/* Sysdep hooks */
|
||||
|
299
conf/confbase.Y
299
conf/confbase.Y
@ -19,6 +19,7 @@ CF_HDR
|
||||
#include "nest/protocol.h"
|
||||
#include "nest/iface.h"
|
||||
#include "nest/route.h"
|
||||
#include "nest/bfd.h"
|
||||
#include "nest/cli.h"
|
||||
#include "filter/filter.h"
|
||||
|
||||
@ -27,61 +28,92 @@ CF_HDR
|
||||
CF_DEFINES
|
||||
|
||||
static void
|
||||
check_u16(unsigned val)
|
||||
check_u16(uint val)
|
||||
{
|
||||
if (val > 0xFFFF)
|
||||
cf_error("Value %d out of range (0-65535)", val);
|
||||
cf_error("Value %u out of range (0-65535)", val);
|
||||
}
|
||||
|
||||
#define cf_assert(cond, ...) do { if (!(cond)) cf_error(__VA_ARGS__); } while (0)
|
||||
static inline void cf_assert_symbol(const struct symbol *sym, uint class) {
|
||||
switch (class) {
|
||||
case SYM_PROTO: cf_assert(sym->class == SYM_PROTO, "Protocol name required"); break;
|
||||
case SYM_TEMPLATE: cf_assert(sym->class == SYM_TEMPLATE, "Protocol template name required"); break;
|
||||
case SYM_FUNCTION: cf_assert(sym->class == SYM_FUNCTION, "Function name required"); break;
|
||||
case SYM_FILTER: cf_assert(sym->class == SYM_FILTER, "Filter name required"); break;
|
||||
case SYM_TABLE: cf_assert(sym->class == SYM_TABLE, "Table name required"); break;
|
||||
case SYM_ATTRIBUTE: cf_assert(sym->class == SYM_ATTRIBUTE, "Custom attribute name required"); break;
|
||||
case SYM_VARIABLE: cf_assert((sym->class & ~0xff) == SYM_VARIABLE, "Variable name required"); break;
|
||||
case SYM_CONSTANT: cf_assert((sym->class & ~0xff) == SYM_CONSTANT, "Constant name required"); break;
|
||||
default: bug("This shall not happen");
|
||||
}
|
||||
}
|
||||
|
||||
CF_DECLS
|
||||
|
||||
%union {
|
||||
int i;
|
||||
uint i;
|
||||
u32 i32;
|
||||
u64 i64;
|
||||
ip_addr a;
|
||||
ip4_addr ip4;
|
||||
ip6_addr ip6;
|
||||
net_addr net;
|
||||
net_addr *net_ptr;
|
||||
struct symbol *s;
|
||||
char *t;
|
||||
const char *t;
|
||||
struct rtable_config *r;
|
||||
struct channel_config *cc;
|
||||
struct channel *c;
|
||||
struct f_inst *x;
|
||||
struct {
|
||||
struct f_inst *begin, *end;
|
||||
} xp;
|
||||
enum filter_return fret;
|
||||
enum ec_subtype ecs;
|
||||
struct f_dynamic_attr fda;
|
||||
struct f_static_attr fsa;
|
||||
struct filter *f;
|
||||
struct f_lval flv;
|
||||
struct f_line *fl;
|
||||
const struct filter *f;
|
||||
struct f_tree *e;
|
||||
struct f_trie *trie;
|
||||
struct f_val v;
|
||||
struct f_path_mask *h;
|
||||
struct password_item *p;
|
||||
struct rt_show_data *ra;
|
||||
struct roa_show_data *ro;
|
||||
struct sym_show_data *sd;
|
||||
struct lsadb_show_data *ld;
|
||||
struct mrt_dump_data *md;
|
||||
struct iface *iface;
|
||||
struct roa_table *rot;
|
||||
void *g;
|
||||
bird_clock_t time;
|
||||
struct prefix px;
|
||||
btime time;
|
||||
struct f_prefix px;
|
||||
struct proto_spec ps;
|
||||
struct channel_limit cl;
|
||||
struct timeformat *tf;
|
||||
mpls_label_stack *mls;
|
||||
}
|
||||
|
||||
%token END CLI_MARKER INVALID_TOKEN ELSECOL DDOT
|
||||
%token GEQ LEQ NEQ AND OR
|
||||
%token PO PC
|
||||
%token <i> NUM ENUM
|
||||
%token <i32> RTRID
|
||||
%token <a> IPA
|
||||
%token <s> SYM
|
||||
%token <ip4> IP4
|
||||
%token <ip6> IP6
|
||||
%token <i64> VPN_RD
|
||||
%token <s> CF_SYM_KNOWN CF_SYM_UNDEFINED
|
||||
%token <t> TEXT
|
||||
%type <iface> ipa_scope
|
||||
|
||||
%type <i> expr bool pxlen
|
||||
%type <i32> expr_us
|
||||
%type <time> datetime
|
||||
%type <i> expr bool pxlen4
|
||||
%type <time> expr_us time
|
||||
%type <a> ipa
|
||||
%type <px> prefix prefix_or_ipa
|
||||
%type <net> net_ip4_ net_ip6_ net_ip6 net_ip_ net_ip net_or_ipa
|
||||
%type <net_ptr> net_ net_any net_vpn4_ net_vpn6_ net_vpn_ net_roa4_ net_roa6_ net_roa_ net_ip6_sadr_ net_mpls_
|
||||
%type <mls> label_stack_start label_stack
|
||||
|
||||
%type <t> text opttext
|
||||
%type <t> text_or_none
|
||||
%type <s> symbol
|
||||
|
||||
%nonassoc PREFIX_DUMMY
|
||||
%left AND OR
|
||||
@ -91,7 +123,9 @@ CF_DECLS
|
||||
%left '!'
|
||||
%nonassoc '.'
|
||||
|
||||
CF_KEYWORDS(DEFINE, ON, OFF, YES, NO, S, MS, US, PORT)
|
||||
%start config
|
||||
|
||||
CF_KEYWORDS(DEFINE, ON, OFF, YES, NO, S, MS, US, PORT, VPN, MPLS, FROM)
|
||||
|
||||
CF_GRAMMAR
|
||||
|
||||
@ -106,37 +140,36 @@ conf_entries:
|
||||
| conf_entries conf
|
||||
;
|
||||
|
||||
CF_ADDTO(conf, ';')
|
||||
conf: ';' ;
|
||||
|
||||
|
||||
/* Constant expressions */
|
||||
|
||||
CF_ADDTO(conf, definition)
|
||||
conf: definition ;
|
||||
|
||||
definition:
|
||||
DEFINE SYM '=' term ';' {
|
||||
struct f_val *val = cfg_alloc(sizeof(struct f_val));
|
||||
*val = f_eval($4, cfg_mem);
|
||||
if (val->type == T_RETURN) cf_error("Runtime error");
|
||||
cf_define_symbol($2, SYM_CONSTANT | val->type, val);
|
||||
DEFINE symbol '=' term ';' {
|
||||
struct f_val *val = cfg_allocz(sizeof(struct f_val));
|
||||
if (f_eval(f_linearize($4), cfg_mem, val) > F_RETURN) cf_error("Runtime error");
|
||||
cf_define_symbol($2, SYM_CONSTANT | val->type, val, val);
|
||||
}
|
||||
;
|
||||
|
||||
expr:
|
||||
NUM
|
||||
| '(' term ')' { $$ = f_eval_int($2); }
|
||||
| SYM {
|
||||
if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number expected");
|
||||
| '(' term ')' { $$ = f_eval_int(f_linearize($2)); }
|
||||
| CF_SYM_KNOWN {
|
||||
if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number constant expected");
|
||||
$$ = SYM_VAL($1).i; }
|
||||
;
|
||||
|
||||
|
||||
expr_us:
|
||||
expr S { $$ = (u32) $1 * 1000000; }
|
||||
| expr MS { $$ = (u32) $1 * 1000; }
|
||||
| expr US { $$ = (u32) $1 * 1; }
|
||||
expr S { $$ = $1 S_; }
|
||||
| expr MS { $$ = $1 MS_; }
|
||||
| expr US { $$ = $1 US_; }
|
||||
;
|
||||
|
||||
/* expr_u16: expr { check_u16($1); $$ = $1; }; */
|
||||
symbol: CF_SYM_UNDEFINED | CF_SYM_KNOWN ;
|
||||
|
||||
/* Switches */
|
||||
|
||||
@ -149,56 +182,196 @@ bool:
|
||||
| /* Silence means agreement */ { $$ = 1; }
|
||||
;
|
||||
|
||||
/* Addresses, prefixes and netmasks */
|
||||
|
||||
/* Addresses */
|
||||
|
||||
ipa:
|
||||
IPA
|
||||
| SYM {
|
||||
if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address expected");
|
||||
$$ = SYM_VAL($1).px.ip;
|
||||
IP4 { $$ = ipa_from_ip4($1); }
|
||||
| IP6 { $$ = ipa_from_ip6($1); }
|
||||
| CF_SYM_KNOWN {
|
||||
if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address constant expected");
|
||||
$$ = SYM_VAL($1).ip;
|
||||
}
|
||||
;
|
||||
|
||||
ipa_scope:
|
||||
/* empty */ { $$ = NULL; }
|
||||
| '%' SYM { $$ = if_get_by_name($2->name); }
|
||||
| '%' symbol { $$ = if_get_by_name($2->name); }
|
||||
;
|
||||
|
||||
prefix:
|
||||
ipa pxlen {
|
||||
if (!ip_is_prefix($1, $2)) cf_error("Invalid prefix");
|
||||
$$.addr = $1; $$.len = $2;
|
||||
}
|
||||
;
|
||||
|
||||
prefix_or_ipa:
|
||||
prefix
|
||||
| ipa { $$.addr = $1; $$.len = BITS_PER_IP_ADDRESS; }
|
||||
;
|
||||
/* Networks - internal */
|
||||
|
||||
pxlen:
|
||||
'/' expr {
|
||||
if ($2 < 0 || $2 > BITS_PER_IP_ADDRESS) cf_error("Invalid prefix length %d", $2);
|
||||
pxlen4:
|
||||
'/' NUM {
|
||||
if ($2 > IP4_MAX_PREFIX_LENGTH) cf_error("Invalid prefix length %u", $2);
|
||||
$$ = $2;
|
||||
}
|
||||
| ':' ipa {
|
||||
$$ = ipa_masklen($2);
|
||||
if ($$ < 0) cf_error("Invalid netmask %I", $2);
|
||||
;
|
||||
|
||||
net_ip4_: IP4 pxlen4
|
||||
{
|
||||
net_fill_ip4(&($$), $1, $2);
|
||||
|
||||
net_addr_ip4 *n = (void *) &($$);
|
||||
if (!net_validate_ip4(n))
|
||||
cf_error("Invalid IPv4 prefix %I4/%d, maybe you wanted %I4/%d",
|
||||
n->prefix, n->pxlen, ip4_and(n->prefix, ip4_mkmask(n->pxlen)), n->pxlen);
|
||||
};
|
||||
|
||||
net_ip6_: IP6 '/' NUM
|
||||
{
|
||||
if ($3 > IP6_MAX_PREFIX_LENGTH)
|
||||
cf_error("Invalid prefix length %u", $3);
|
||||
|
||||
net_fill_ip6(&($$), $1, $3);
|
||||
|
||||
net_addr_ip6 *n = (void *) &($$);
|
||||
if (!net_validate_ip6(n))
|
||||
cf_error("Invalid IPv6 prefix %I6/%d, maybe you wanted %I6/%d",
|
||||
n->prefix, n->pxlen, ip6_and(n->prefix, ip6_mkmask(n->pxlen)), n->pxlen);
|
||||
};
|
||||
|
||||
net_ip6_sadr_: IP6 '/' NUM FROM IP6 '/' NUM
|
||||
{
|
||||
if ($3 > IP6_MAX_PREFIX_LENGTH)
|
||||
cf_error("Invalid prefix length %u", $3);
|
||||
|
||||
if ($7 > IP6_MAX_PREFIX_LENGTH)
|
||||
cf_error("Invalid prefix length %u", $7);
|
||||
|
||||
$$ = cfg_alloc(sizeof(net_addr_ip6_sadr));
|
||||
net_fill_ip6_sadr($$, $1, $3, $5, $7);
|
||||
|
||||
net_addr_ip6_sadr *n = (void *) $$;
|
||||
if (!net_validate_ip6_sadr(n))
|
||||
cf_error("Invalid SADR IPv6 prefix %I6/%d from %I6/%d, maybe you wanted %I6/%d from %I6/%d",
|
||||
n->dst_prefix, n->dst_pxlen, n->src_prefix, n->src_pxlen,
|
||||
ip6_and(n->dst_prefix, ip6_mkmask(n->dst_pxlen)), n->dst_pxlen,
|
||||
ip6_and(n->src_prefix, ip6_mkmask(n->src_pxlen)), n->src_pxlen);
|
||||
};
|
||||
|
||||
net_vpn4_: VPN_RD net_ip4_
|
||||
{
|
||||
$$ = cfg_alloc(sizeof(net_addr_vpn4));
|
||||
net_fill_vpn4($$, net4_prefix(&$2), net4_pxlen(&$2), $1);
|
||||
}
|
||||
|
||||
net_vpn6_: VPN_RD net_ip6_
|
||||
{
|
||||
$$ = cfg_alloc(sizeof(net_addr_vpn6));
|
||||
net_fill_vpn6($$, net6_prefix(&$2), net6_pxlen(&$2), $1);
|
||||
}
|
||||
|
||||
net_roa4_: net_ip4_ MAX NUM AS NUM
|
||||
{
|
||||
$$ = cfg_alloc(sizeof(net_addr_roa4));
|
||||
net_fill_roa4($$, net4_prefix(&$1), net4_pxlen(&$1), $3, $5);
|
||||
if ($3 < net4_pxlen(&$1) || $3 > IP4_MAX_PREFIX_LENGTH)
|
||||
cf_error("Invalid max prefix length %u", $3);
|
||||
};
|
||||
|
||||
net_roa6_: net_ip6_ MAX NUM AS NUM
|
||||
{
|
||||
$$ = cfg_alloc(sizeof(net_addr_roa6));
|
||||
net_fill_roa6($$, net6_prefix(&$1), net6_pxlen(&$1), $3, $5);
|
||||
if ($3 < net6_pxlen(&$1) || $3 > IP6_MAX_PREFIX_LENGTH)
|
||||
cf_error("Invalid max prefix length %u", $3);
|
||||
};
|
||||
|
||||
net_mpls_: MPLS NUM
|
||||
{
|
||||
$$ = cfg_alloc(sizeof(net_addr_roa6));
|
||||
net_fill_mpls($$, $2);
|
||||
}
|
||||
|
||||
net_ip_: net_ip4_ | net_ip6_ ;
|
||||
net_vpn_: net_vpn4_ | net_vpn6_ ;
|
||||
net_roa_: net_roa4_ | net_roa6_ ;
|
||||
|
||||
net_:
|
||||
net_ip_ { $$ = cfg_alloc($1.length); net_copy($$, &($1)); }
|
||||
| net_vpn_
|
||||
| net_roa_
|
||||
| net_flow_
|
||||
| net_ip6_sadr_
|
||||
| net_mpls_
|
||||
;
|
||||
|
||||
|
||||
/* Networks - regular */
|
||||
|
||||
net_ip6:
|
||||
net_ip6_
|
||||
| CF_SYM_KNOWN {
|
||||
if (($1->class != (SYM_CONSTANT | T_NET)) || (SYM_VAL($1).net->type != NET_IP6))
|
||||
cf_error("IPv6 network constant expected");
|
||||
$$ = * SYM_VAL($1).net;
|
||||
}
|
||||
;
|
||||
|
||||
datetime:
|
||||
net_ip:
|
||||
net_ip_
|
||||
| CF_SYM_KNOWN {
|
||||
if (($1->class != (SYM_CONSTANT | T_NET)) || !net_is_ip(SYM_VAL($1).net))
|
||||
cf_error("IP network constant expected");
|
||||
$$ = * SYM_VAL($1).net;
|
||||
}
|
||||
;
|
||||
|
||||
net_any:
|
||||
net_
|
||||
| CF_SYM_KNOWN {
|
||||
if ($1->class != (SYM_CONSTANT | T_NET))
|
||||
cf_error("Network constant expected");
|
||||
$$ = (net_addr *) SYM_VAL($1).net; /* Avoid const warning */
|
||||
}
|
||||
;
|
||||
|
||||
net_or_ipa:
|
||||
net_ip4_
|
||||
| net_ip6_
|
||||
| IP4 { net_fill_ip4(&($$), $1, IP4_MAX_PREFIX_LENGTH); }
|
||||
| IP6 { net_fill_ip6(&($$), $1, IP6_MAX_PREFIX_LENGTH); }
|
||||
| CF_SYM_KNOWN {
|
||||
if ($1->class == (SYM_CONSTANT | T_IP))
|
||||
net_fill_ip_host(&($$), SYM_VAL($1).ip);
|
||||
else if (($1->class == (SYM_CONSTANT | T_NET)) && net_is_ip(SYM_VAL($1).net))
|
||||
$$ = * SYM_VAL($1).net;
|
||||
else
|
||||
cf_error("IP address or network constant expected");
|
||||
}
|
||||
;
|
||||
|
||||
label_stack_start: NUM
|
||||
{
|
||||
$$ = cfg_allocz(sizeof(mpls_label_stack));
|
||||
$$->len = 1;
|
||||
$$->stack[0] = $1;
|
||||
};
|
||||
|
||||
label_stack:
|
||||
label_stack_start
|
||||
| label_stack '/' NUM {
|
||||
if ($1->len >= MPLS_MAX_LABEL_STACK)
|
||||
cf_error("Too many labels in stack");
|
||||
$1->stack[$1->len++] = $3;
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
time:
|
||||
TEXT {
|
||||
$$ = tm_parse_datetime($1);
|
||||
$$ = tm_parse_time($1);
|
||||
if (!$$)
|
||||
cf_error("Invalid date and time");
|
||||
cf_error("Invalid date/time");
|
||||
}
|
||||
;
|
||||
|
||||
text:
|
||||
TEXT
|
||||
| SYM {
|
||||
if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String expected");
|
||||
| CF_SYM_KNOWN {
|
||||
if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String constant expected");
|
||||
$$ = SYM_VAL($1).s;
|
||||
}
|
||||
;
|
||||
@ -208,10 +381,6 @@ opttext:
|
||||
| /* empty */ { $$ = NULL; }
|
||||
;
|
||||
|
||||
text_or_none:
|
||||
TEXT { $$ = $1; }
|
||||
| { $$ = NULL; }
|
||||
;
|
||||
|
||||
CF_CODE
|
||||
|
||||
|
209
conf/flowspec.Y
Normal file
209
conf/flowspec.Y
Normal file
@ -0,0 +1,209 @@
|
||||
/*
|
||||
* BIRD -- Flow specification (RFC 5575) grammar
|
||||
*
|
||||
* (c) 2016 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
CF_HDR
|
||||
|
||||
#define PARSER 1
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/flowspec.h"
|
||||
|
||||
|
||||
CF_DEFINES
|
||||
|
||||
struct flow_builder *this_flow;
|
||||
|
||||
|
||||
CF_DECLS
|
||||
|
||||
%type <i32> flow_num_op flow_srcdst flow_logic_op flow_num_type_ flow_frag_val flow_neg
|
||||
%type <net_ptr> net_flow4_ net_flow6_ net_flow_
|
||||
|
||||
CF_KEYWORDS(FLOW4, FLOW6, DST, SRC, PROTO, NEXT, HEADER, DPORT, SPORT, ICMP,
|
||||
TYPE, CODE, TCP, FLAGS, LENGTH, DSCP, DONT_FRAGMENT, IS_FRAGMENT,
|
||||
FIRST_FRAGMENT, LAST_FRAGMENT, FRAGMENT, LABEL, OFFSET)
|
||||
|
||||
|
||||
CF_GRAMMAR
|
||||
|
||||
/* Network Flow Specification */
|
||||
|
||||
flow_num_op:
|
||||
TRUE { $$ = FLOW_OP_TRUE; }
|
||||
| '=' { $$ = FLOW_OP_EQ; }
|
||||
| NEQ { $$ = FLOW_OP_NEQ; }
|
||||
| '<' { $$ = FLOW_OP_LT; }
|
||||
| LEQ { $$ = FLOW_OP_LEQ; }
|
||||
| '>' { $$ = FLOW_OP_GT; }
|
||||
| GEQ { $$ = FLOW_OP_GEQ; }
|
||||
| FALSE { $$ = FLOW_OP_FALSE; }
|
||||
;
|
||||
|
||||
flow_logic_op:
|
||||
OR { $$ = FLOW_OP_OR; }
|
||||
| AND { $$ = FLOW_OP_AND; }
|
||||
;
|
||||
|
||||
flow_num_type_:
|
||||
PROTO { $$ = FLOW_TYPE_IP_PROTOCOL; }
|
||||
| NEXT HEADER { $$ = FLOW_TYPE_NEXT_HEADER; }
|
||||
| PORT { $$ = FLOW_TYPE_PORT; }
|
||||
| DPORT { $$ = FLOW_TYPE_DST_PORT; }
|
||||
| SPORT { $$ = FLOW_TYPE_SRC_PORT; }
|
||||
| ICMP TYPE { $$ = FLOW_TYPE_ICMP_TYPE; }
|
||||
| ICMP CODE { $$ = FLOW_TYPE_ICMP_CODE; }
|
||||
| LENGTH { $$ = FLOW_TYPE_PACKET_LENGTH; }
|
||||
| DSCP { $$ = FLOW_TYPE_DSCP; }
|
||||
;
|
||||
|
||||
flow_num_type: flow_num_type_{ flow_builder_set_type(this_flow, $1); };
|
||||
flow_flag_type: TCP FLAGS { flow_builder_set_type(this_flow, FLOW_TYPE_TCP_FLAGS); };
|
||||
flow_frag_type: FRAGMENT { flow_builder_set_type(this_flow, FLOW_TYPE_FRAGMENT); };
|
||||
flow_label_type: LABEL { flow_builder_set_type(this_flow, FLOW_TYPE_LABEL); };
|
||||
|
||||
flow_srcdst:
|
||||
DST { $$ = FLOW_TYPE_DST_PREFIX; }
|
||||
| SRC { $$ = FLOW_TYPE_SRC_PREFIX; }
|
||||
;
|
||||
|
||||
flow_num_opts:
|
||||
flow_num_op expr {
|
||||
flow_check_cf_value_length(this_flow, $2);
|
||||
flow_builder_add_op_val(this_flow, $1, $2);
|
||||
}
|
||||
| flow_num_opts flow_logic_op flow_num_op expr {
|
||||
flow_check_cf_value_length(this_flow, $4);
|
||||
flow_builder_add_op_val(this_flow, $2 | $3, $4);
|
||||
}
|
||||
| flow_num_opt_ext
|
||||
| flow_num_opts OR flow_num_opt_ext
|
||||
;
|
||||
|
||||
flow_num_opt_ext_expr:
|
||||
expr {
|
||||
flow_check_cf_value_length(this_flow, $1);
|
||||
flow_builder_add_op_val(this_flow, FLOW_OP_EQ, $1);
|
||||
}
|
||||
| expr DDOT expr {
|
||||
flow_check_cf_value_length(this_flow, $1);
|
||||
flow_check_cf_value_length(this_flow, $3);
|
||||
flow_builder_add_op_val(this_flow, FLOW_OP_GEQ, $1);
|
||||
flow_builder_add_op_val(this_flow, FLOW_OP_AND | FLOW_OP_LEQ, $3);
|
||||
}
|
||||
;
|
||||
|
||||
flow_num_opt_ext:
|
||||
flow_num_opt_ext_expr
|
||||
| flow_num_opt_ext ',' flow_num_opt_ext_expr
|
||||
;
|
||||
|
||||
flow_bmk_opts:
|
||||
flow_neg expr '/' expr {
|
||||
flow_check_cf_bmk_values(this_flow, $1, $2, $4);
|
||||
flow_builder_add_val_mask(this_flow, $1, $2, $4);
|
||||
}
|
||||
| flow_bmk_opts flow_logic_op flow_neg expr '/' expr {
|
||||
flow_check_cf_bmk_values(this_flow, $3, $4, $6);
|
||||
flow_builder_add_val_mask(this_flow, $2 | $3, $4, $6);
|
||||
}
|
||||
| flow_bmk_opts ',' flow_neg expr '/' expr {
|
||||
flow_check_cf_bmk_values(this_flow, $3, $4, $6);
|
||||
flow_builder_add_val_mask(this_flow, 0x40 | $3, $4, $6); /* AND */
|
||||
}
|
||||
;
|
||||
|
||||
flow_neg:
|
||||
/* empty */ { $$ = 0x00; }
|
||||
| '!' { $$ = 0x02; }
|
||||
;
|
||||
|
||||
flow_frag_val:
|
||||
DONT_FRAGMENT { $$ = 1; }
|
||||
| IS_FRAGMENT { $$ = 2; }
|
||||
| FIRST_FRAGMENT { $$ = 4; }
|
||||
| LAST_FRAGMENT { $$ = 8; }
|
||||
;
|
||||
|
||||
flow_frag_opts:
|
||||
flow_neg flow_frag_val {
|
||||
flow_builder_add_val_mask(this_flow, 0, ($1 ? 0 : $2), $2);
|
||||
}
|
||||
| flow_frag_opts flow_logic_op flow_neg flow_frag_val {
|
||||
flow_builder_add_val_mask(this_flow, $2, ($3 ? 0 : $4), $4);
|
||||
}
|
||||
| flow_frag_opts ',' flow_neg flow_frag_val {
|
||||
flow_builder_add_val_mask(this_flow, 0x40, ($3 ? 0 : $4), $4); /* AND */
|
||||
}
|
||||
;
|
||||
|
||||
flow4_item:
|
||||
flow_srcdst net_ip {
|
||||
flow_builder_set_type(this_flow, $1);
|
||||
flow_builder4_add_pfx(this_flow, (net_addr_ip4 *) &($2));
|
||||
}
|
||||
| flow_num_type flow_num_opts
|
||||
| flow_flag_type flow_bmk_opts
|
||||
| flow_frag_type flow_frag_opts
|
||||
;
|
||||
|
||||
flow6_item:
|
||||
flow_srcdst net_ip6 {
|
||||
flow_builder_set_type(this_flow, $1);
|
||||
flow_builder6_add_pfx(this_flow, (net_addr_ip6 *) &($2), 0);
|
||||
}
|
||||
| flow_srcdst net_ip6 OFFSET NUM {
|
||||
if ($4 > $2.pxlen)
|
||||
cf_error("Prefix offset is higher than prefix length");
|
||||
flow_builder_set_type(this_flow, $1);
|
||||
flow_builder6_add_pfx(this_flow, (net_addr_ip6 *) &($2), $4);
|
||||
}
|
||||
| flow_num_type flow_num_opts
|
||||
| flow_flag_type flow_bmk_opts
|
||||
| flow_frag_type flow_frag_opts
|
||||
| flow_label_type flow_bmk_opts
|
||||
;
|
||||
|
||||
flow4_opts:
|
||||
/* empty */
|
||||
| flow4_opts flow4_item ';'
|
||||
;
|
||||
|
||||
flow6_opts:
|
||||
/* empty */
|
||||
| flow6_opts flow6_item ';'
|
||||
;
|
||||
|
||||
flow_builder_init:
|
||||
{
|
||||
if (this_flow == NULL)
|
||||
this_flow = flow_builder_init(&root_pool);
|
||||
else
|
||||
flow_builder_clear(this_flow);
|
||||
};
|
||||
|
||||
flow_builder_set_ipv4: { this_flow->ipv6 = 0; };
|
||||
flow_builder_set_ipv6: { this_flow->ipv6 = 1; };
|
||||
|
||||
net_flow4_: FLOW4 '{' flow_builder_init flow_builder_set_ipv4 flow4_opts '}'
|
||||
{
|
||||
$$ = (net_addr *) flow_builder4_finalize(this_flow, cfg_mem);
|
||||
flow4_validate_cf((net_addr_flow4 *) $$);
|
||||
};
|
||||
|
||||
net_flow6_: FLOW6 '{' flow_builder_init flow_builder_set_ipv6 flow6_opts '}'
|
||||
{
|
||||
$$ = (net_addr *) flow_builder6_finalize(this_flow, cfg_mem);
|
||||
flow6_validate_cf((net_addr_flow6 *) $$);
|
||||
};
|
||||
|
||||
net_flow_: net_flow4_ | net_flow6_ ;
|
||||
|
||||
|
||||
CF_CODE
|
||||
|
||||
CF_END
|
@ -34,9 +34,11 @@ m4_define(CF_CLI, `CF_KEYWORDS(m4_translit($1, [[ ]], [[,]]))
|
||||
')
|
||||
|
||||
# Enums are translated to C initializers: use CF_ENUM(typename, prefix, values)
|
||||
m4_define(CF_enum, `m4_divert(1){ "CF_enum_prefix[[]]$1", -((CF_enum_type<<16) | CF_enum_prefix[[]]$1), NULL },
|
||||
# For different prefix: CF_ENUM_PX(typename, external prefix, C prefix, values)
|
||||
m4_define(CF_enum, `m4_divert(1){ "CF_enum_prefix_ext[[]]$1", -((CF_enum_type<<16) | CF_enum_prefix_int[[]]$1), NULL },
|
||||
m4_divert(-1)')
|
||||
m4_define(CF_ENUM, `m4_define([[CF_enum_type]],$1)m4_define([[CF_enum_prefix]],$2)CF_iterate([[CF_enum]], [[m4_shift(m4_shift($@))]])DNL')
|
||||
m4_define(CF_ENUM, `m4_define([[CF_enum_type]],$1)m4_define([[CF_enum_prefix_ext]],$2)m4_define([[CF_enum_prefix_int]],$2)CF_iterate([[CF_enum]], [[m4_shift(m4_shift($@))]])DNL')
|
||||
m4_define(CF_ENUM_PX, `m4_define([[CF_enum_type]],$1)m4_define([[CF_enum_prefix_ext]],$2)m4_define([[CF_enum_prefix_int]],$3)CF_iterate([[CF_enum]], [[m4_shift(m4_shift(m4_shift($@)))]])DNL')
|
||||
|
||||
# After all configuration templates end, we generate the
|
||||
m4_m4wrap(`
|
||||
|
@ -34,21 +34,17 @@ m4_define(CF_keywd, `m4_ifdef([[CF_tok_$1]],,[[m4_define([[CF_tok_$1]],1)m4_defi
|
||||
m4_define(CF_KEYWORDS, `m4_define([[CF_toks]],[[]])CF_iterate([[CF_keywd]], [[$@]])m4_ifelse(CF_toks,,,%token[[]]CF_toks
|
||||
)DNL')
|
||||
|
||||
# Dynamic syntax rules
|
||||
m4_define(CF_dyn_rules,)
|
||||
m4_define(CF_ADDTO, `m4_define([[CF_rule_$1]],m4_ifdef([[CF_rule_$1]],CF_rule_$1 | ,[[m4_define([[CF_dyn_rules]],CF_dyn_rules[[CF_RULE($1)
|
||||
]])]])$2)DNL')
|
||||
|
||||
# CLI commands
|
||||
m4_define(CF_CLI, `m4_define([[CF_cmd]], cmd_[[]]m4_translit($1, [[ ]], _))DNL
|
||||
m4_divert(2)CF_KEYWORDS(m4_translit($1, [[ ]], [[,]]))
|
||||
m4_divert(3)CF_ADDTO(cli_cmd, CF_cmd)
|
||||
m4_divert(3)cli_cmd: CF_cmd
|
||||
CF_cmd: $1 $2 END')
|
||||
m4_define(CF_CLI_CMD, `')
|
||||
m4_define(CF_CLI_HELP, `')
|
||||
|
||||
# ENUM declarations are ignored
|
||||
m4_define(CF_ENUM, `')
|
||||
m4_define(CF_ENUM_PX, `')
|
||||
|
||||
# After all configuration templates end, we finally generate the grammar file.
|
||||
m4_m4wrap(`
|
||||
@ -62,10 +58,6 @@ m4_undivert(2)DNL
|
||||
%%
|
||||
m4_undivert(3)DNL
|
||||
|
||||
/* Dynamic rules */
|
||||
|
||||
m4_define(CF_RULE, [[$1: CF_rule_$1 ;]])
|
||||
CF_dyn_rules
|
||||
%%
|
||||
m4_undivert(4)DNL
|
||||
')
|
||||
|
242
configure.ac
242
configure.ac
@ -18,10 +18,16 @@ AC_ARG_ENABLE([debug],
|
||||
[enable_debug=no]
|
||||
)
|
||||
|
||||
AC_ARG_ENABLE([ipv6],
|
||||
[AS_HELP_STRING([--enable-ipv6], [enable building of IPv6 version @<:@no@:>@])],
|
||||
AC_ARG_ENABLE([debug-generated],
|
||||
[AS_HELP_STRING([--enable-debug-generated], [enable this to abstain from generating #line @<:@no@:>@])],
|
||||
[],
|
||||
[enable_ipv6=no]
|
||||
[enable_debug_generated=no]
|
||||
)
|
||||
|
||||
AC_ARG_ENABLE([debug-expensive],
|
||||
[AS_HELP_STRING([--enable-debug-expensive], [enable expensive consistency checks (implies --enable-debug) @<:@no@:>@])],
|
||||
[],
|
||||
[enable_debug_expensive=no]
|
||||
)
|
||||
|
||||
AC_ARG_ENABLE([memcheck],
|
||||
@ -36,17 +42,24 @@ AC_ARG_ENABLE([pthreads],
|
||||
[enable_pthreads=try]
|
||||
)
|
||||
|
||||
AC_ARG_ENABLE([libssh],
|
||||
[AS_HELP_STRING([--enable-libssh], [enable LibSSH support in RPKI @<:@try@:>@])],
|
||||
[],
|
||||
[enable_libssh=try]
|
||||
)
|
||||
|
||||
AC_ARG_ENABLE([mpls-kernel],
|
||||
[AS_HELP_STRING([--enable-mpls-kernel], [enable MPLS support in kernel protocol @<:@try@:>@])],
|
||||
[],
|
||||
[enable_mpls_kernel=try]
|
||||
)
|
||||
|
||||
AC_ARG_WITH([protocols],
|
||||
[AS_HELP_STRING([--with-protocols=LIST], [include specified routing protocols @<:@all@:>@])],
|
||||
[],
|
||||
[with_protocols="all"]
|
||||
)
|
||||
|
||||
AC_ARG_WITH([suffix],
|
||||
[AS_HELP_STRING([--with-suffix=STRING], [use specified suffix for BIRD files @<:@6 for IPv6@:>@])],
|
||||
[given_suffix="yes"]
|
||||
)
|
||||
|
||||
AC_ARG_WITH([sysconfig],
|
||||
[AS_HELP_STRING([--with-sysconfig=FILE], [use specified BIRD system configuration file])]
|
||||
)
|
||||
@ -65,58 +78,32 @@ AC_ARG_VAR([FLEX], [location of the Flex program])
|
||||
AC_ARG_VAR([BISON], [location of the Bison program])
|
||||
AC_ARG_VAR([M4], [location of the M4 program])
|
||||
|
||||
if test "$enable_debug_expensive" = yes; then
|
||||
enable_debug=yes
|
||||
fi
|
||||
|
||||
if test "$srcdir" = . ; then
|
||||
# Building in current directory => create obj directory holding all objects
|
||||
objdir=obj
|
||||
mkdir -p obj
|
||||
srcdir_rel=..
|
||||
makefiles="Makefile:tools/Makefile-top.in obj/Makefile:tools/Makefile.in obj/Rules:tools/Rules.in"
|
||||
exedir=..
|
||||
else
|
||||
# Building in separate directory
|
||||
objdir=.
|
||||
srcdir_rel=$srcdir
|
||||
makefiles="Makefile:tools/Makefile.in Rules:tools/Rules.in"
|
||||
exedir=.
|
||||
fi
|
||||
|
||||
case $srcdir_rel in
|
||||
/*) srcdir_rel_mf=$srcdir_rel ;;
|
||||
*) srcdir_rel_mf="\$(root-rel)$srcdir_rel" ;;
|
||||
esac
|
||||
exedir=.
|
||||
|
||||
AC_SUBST([objdir])
|
||||
AC_SUBST([exedir])
|
||||
AC_SUBST([srcdir_rel_mf])
|
||||
AC_SUBST([srcdir])
|
||||
|
||||
# Workaround for older Autoconfs that do not define runstatedir
|
||||
AS_IF([test -z "${runstatedir}"], [runstatedir='${localstatedir}/run'])
|
||||
AC_SUBST([runstatedir])
|
||||
|
||||
|
||||
if test "$enable_ipv6" = yes ; then
|
||||
ip=ipv6
|
||||
SUFFIX=6
|
||||
proto_radv=radv
|
||||
else
|
||||
ip=ipv4
|
||||
SUFFIX=""
|
||||
fi
|
||||
|
||||
if test "$given_suffix" = yes ; then
|
||||
SUFFIX="$with_suffix"
|
||||
fi
|
||||
AC_SUBST([SUFFIX])
|
||||
|
||||
if test "$enable_debug" = yes ; then
|
||||
CONFIG_FILE="bird$SUFFIX.conf"
|
||||
CONTROL_SOCKET="bird$SUFFIX.ctl"
|
||||
else
|
||||
CONFIG_FILE="\$(sysconfdir)/bird$SUFFIX.conf"
|
||||
CONTROL_SOCKET="\$(runstatedir)/bird$SUFFIX.ctl"
|
||||
fi
|
||||
CONFIG_FILE="\$(sysconfdir)/bird.conf"
|
||||
AC_SUBST([CONFIG_FILE])
|
||||
|
||||
CONTROL_SOCKET="\$(runstatedir)/bird.ctl"
|
||||
AC_SUBST([CONTROL_SOCKET])
|
||||
|
||||
AC_SEARCH_LIBS([clock_gettime], [rt posix4],
|
||||
@ -132,10 +119,16 @@ if test "$ac_test_CFLAGS" != set ; then
|
||||
fi
|
||||
|
||||
AC_PROG_CC
|
||||
AC_PROG_CC_C99
|
||||
if test -z "$GCC" ; then
|
||||
AC_MSG_ERROR([This program requires the GNU C Compiler.])
|
||||
fi
|
||||
|
||||
BIRD_CHECK_THREAD_LOCAL
|
||||
if test "$bird_cv_thread_local" = yes ; then
|
||||
AC_DEFINE([HAVE_THREAD_LOCAL], [1], [Define to 1 if _Thread_local is available])
|
||||
fi
|
||||
|
||||
if test "$enable_pthreads" != no ; then
|
||||
BIRD_CHECK_PTHREADS
|
||||
|
||||
@ -153,21 +146,35 @@ if test "$enable_pthreads" != no ; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# This is assumed to be necessary for proper BIRD build
|
||||
CFLAGS="$CFLAGS -fno-strict-aliasing -fno-strict-overflow"
|
||||
|
||||
if test "$bird_cflags_default" = yes ; then
|
||||
BIRD_CHECK_GCC_OPTION([bird_cv_c_option_wno_pointer_sign], [-Wno-pointer-sign], [-Wall])
|
||||
BIRD_CHECK_GCC_OPTION([bird_cv_c_option_wno_missing_init], [-Wno-missing-field-initializers], [-Wall -Wextra])
|
||||
BIRD_CHECK_GCC_OPTION([bird_cv_c_option_fno_strict_aliasing], [-fno-strict-aliasing])
|
||||
BIRD_CHECK_GCC_OPTION([bird_cv_c_option_fno_strict_overflow], [-fno-strict-overflow])
|
||||
|
||||
if test "$enable_debug" = no; then
|
||||
BIRD_CHECK_LTO
|
||||
fi
|
||||
|
||||
if test "$bird_cv_c_lto" = yes; then
|
||||
CFLAGS="$CFLAGS -flto"
|
||||
LDFLAGS="$LDFLAGS -flto=4 -g"
|
||||
else
|
||||
LDFLAGS="$LDFLAGS -g"
|
||||
fi
|
||||
|
||||
CFLAGS="$CFLAGS -Wall -Wextra -Wstrict-prototypes -Wno-parentheses"
|
||||
BIRD_ADD_GCC_OPTION([bird_cv_c_option_wno_pointer_sign], [-Wno-pointer-sign])
|
||||
BIRD_ADD_GCC_OPTION([bird_cv_c_option_wno_missing_init], [-Wno-missing-field-initializers])
|
||||
BIRD_ADD_GCC_OPTION([bird_cv_c_option_fno_strict_aliasing], [-fno-strict-aliasing])
|
||||
BIRD_ADD_GCC_OPTION([bird_cv_c_option_fno_strict_overflow], [-fno-strict-overflow])
|
||||
fi
|
||||
|
||||
|
||||
AC_MSG_CHECKING([CFLAGS])
|
||||
AC_MSG_RESULT([$CFLAGS])
|
||||
|
||||
AC_MSG_CHECKING([LDFLAGS])
|
||||
AC_MSG_RESULT([$LDFLAGS])
|
||||
|
||||
AC_PROG_CPP
|
||||
AC_PROG_INSTALL
|
||||
@ -180,6 +187,20 @@ test -z "$FLEX" && AC_MSG_ERROR([Flex is missing.])
|
||||
test -z "$BISON" && AC_MSG_ERROR([Bison is missing.])
|
||||
test -z "$M4" && AC_MSG_ERROR([M4 is missing.])
|
||||
|
||||
AC_MSG_CHECKING([bison version])
|
||||
BIRD_CHECK_BISON_VERSION(BISON_VERSION)
|
||||
AC_MSG_RESULT([$BISON_VERSION])
|
||||
if test "$bird_bison_synclines" = yes && test "$enable_debug_generated" = no; then
|
||||
M4FLAGS="$M4FLAGS -s"
|
||||
fi
|
||||
|
||||
if test "$bird_bison_enhanced_error" = yes; then
|
||||
BISONFLAGS="$BISONFLAGS -Dparse.lac=full -Dparse.error=verbose"
|
||||
fi
|
||||
|
||||
AC_SUBST([M4FLAGS])
|
||||
AC_SUBST([BISONFLAGS])
|
||||
|
||||
BIRD_CHECK_PROG_FLAVOR_GNU([$M4],
|
||||
[],
|
||||
[AC_MSG_ERROR([Provided M4 is not GNU M4.])]
|
||||
@ -197,58 +218,35 @@ if test -n "$with_sysconfig" -a "$with_sysconfig" != no ; then
|
||||
elif test -f sysconfig.h ; then
|
||||
sysdesc=sysconfig
|
||||
else
|
||||
case "$ip:$host_os" in
|
||||
ipv6:linux*)
|
||||
sysdesc=linux-v6
|
||||
default_iproutedir="/etc/iproute2"
|
||||
;;
|
||||
ipv4:linux*)
|
||||
case "$host_os" in
|
||||
linux*)
|
||||
sysdesc=linux
|
||||
default_iproutedir="/etc/iproute2"
|
||||
;;
|
||||
ipv6:netbsd*)
|
||||
sysdesc=bsd-v6
|
||||
CPPFLAGS="$CPPFLAGS -I/usr/pkg/include"
|
||||
LDFLAGS="$LDFLAGS -L/usr/pkg/lib -R/usr/pkg/lib"
|
||||
;;
|
||||
ipv4:netbsd*)
|
||||
sysdesc=bsd
|
||||
CPPFLAGS="$CPPFLAGS -I/usr/pkg/include"
|
||||
LDFLAGS="$LDFLAGS -L/usr/pkg/lib -R/usr/pkg/lib"
|
||||
;;
|
||||
ipv6:freebsd*)
|
||||
sysdesc=bsd-v6
|
||||
CPPFLAGS="$CPPFLAGS -I/usr/local/include"
|
||||
LDFLAGS="$LDFLAGS -L/usr/local/lib"
|
||||
;;
|
||||
ipv4:freebsd*)
|
||||
freebsd*)
|
||||
sysdesc=bsd
|
||||
CPPFLAGS="$CPPFLAGS -I/usr/local/include"
|
||||
LDFLAGS="$LDFLAGS -L/usr/local/lib"
|
||||
;;
|
||||
ipv6:dragonfly*)
|
||||
sysdesc=bsd-v6
|
||||
;;
|
||||
ipv4:dragonfly*)
|
||||
kfreebsd*)
|
||||
sysdesc=bsd
|
||||
;;
|
||||
ipv6:kfreebsd*)
|
||||
sysdesc=bsd-v6
|
||||
netbsd*)
|
||||
sysdesc=bsd
|
||||
CPPFLAGS="$CPPFLAGS -I/usr/pkg/include"
|
||||
LDFLAGS="$LDFLAGS -L/usr/pkg/lib -R/usr/pkg/lib"
|
||||
;;
|
||||
ipv4:kfreebsd*)
|
||||
openbsd*)
|
||||
sysdesc=bsd
|
||||
;;
|
||||
ipv6:openbsd*)
|
||||
sysdesc=bsd-v6
|
||||
;;
|
||||
ipv4:openbsd*)
|
||||
dragonfly*)
|
||||
sysdesc=bsd
|
||||
;;
|
||||
*)
|
||||
AC_MSG_ERROR([Cannot determine correct system configuration. Please use --with-sysconfig to set it manually.])
|
||||
;;
|
||||
esac
|
||||
sysdesc=$srcdir/sysdep/cf/$sysdesc.h
|
||||
esac
|
||||
sysdesc=$srcdir/sysdep/cf/$sysdesc.h
|
||||
fi
|
||||
AC_MSG_CHECKING([which OS configuration should we use])
|
||||
AC_MSG_RESULT([$sysdesc])
|
||||
@ -259,7 +257,7 @@ sysname=`echo $sysdesc | sed 's/\.h$//'`
|
||||
AC_DEFINE_UNQUOTED([SYSCONF_INCLUDE], ["$sysdesc"], [Which sysdep header to include])
|
||||
|
||||
AC_MSG_CHECKING([system-dependent directories])
|
||||
sysdep_dirs="`sed <$sysdesc '/^Link: /!d;s/^Link: \(.*\)$/\1/' | tr '\012' ' '` lib"
|
||||
sysdep_dirs="`sed <$sysdesc '/^Link: /!d;s/^Link: \(.*\)$/\1/' | tr '\012' ' '`"
|
||||
AC_MSG_RESULT([$sysdep_dirs])
|
||||
AC_SUBST([sysdep_dirs])
|
||||
|
||||
@ -272,10 +270,42 @@ fi
|
||||
|
||||
AC_SUBST([iproutedir])
|
||||
|
||||
all_protocols="$proto_bfd bgp mrt ospf pipe $proto_radv rip static"
|
||||
if test "$ip" = ipv6 ; then
|
||||
all_protocols="$all_protocols babel"
|
||||
DAEMON_LIBS=
|
||||
AC_SUBST(DAEMON_LIBS)
|
||||
|
||||
if test "$enable_libssh" != no ; then
|
||||
AC_CHECK_HEADER([libssh/libssh.h], [true], [fail=yes], [ ])
|
||||
AC_CHECK_LIB([ssh], [ssh_connect], [true], [fail=yes])
|
||||
|
||||
if test "$fail" != yes ; then
|
||||
AC_DEFINE([HAVE_LIBSSH], [1], [Define to 1 if you have the `ssh' library (-lssh).])
|
||||
DAEMON_LIBS="-lssh $DAEMON_LIBS"
|
||||
enable_libssh=yes
|
||||
else
|
||||
if test "$enable_libssh" = yes ; then
|
||||
AC_MSG_ERROR([LibSSH not available.])
|
||||
else
|
||||
enable_libssh=no
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if test "$enable_mpls_kernel" != no ; then
|
||||
BIRD_CHECK_MPLS_KERNEL
|
||||
|
||||
if test "$bird_cv_mpls_kernel" = yes ; then
|
||||
AC_DEFINE([HAVE_MPLS_KERNEL], [1], [Define to 1 if kernel is MPLS capable])
|
||||
elif test "$enable_mpls_kernel" = yes ; then
|
||||
AC_MSG_ERROR([Kernel MPLS support not found.])
|
||||
fi
|
||||
|
||||
if test "$enable_mpls_kernel" = try ; then
|
||||
enable_mpls_kernel="$bird_cv_mpls_kernel"
|
||||
fi
|
||||
fi
|
||||
|
||||
all_protocols="$proto_bfd babel bgp mrt ospf perf pipe radv rip rpki static"
|
||||
|
||||
all_protocols=`echo $all_protocols | sed 's/ /,/g'`
|
||||
|
||||
if test "$with_protocols" = all ; then
|
||||
@ -290,6 +320,7 @@ AH_TEMPLATE([CONFIG_OSPF], [OSPF protocol])
|
||||
AH_TEMPLATE([CONFIG_PIPE], [Pipe protocol])
|
||||
AH_TEMPLATE([CONFIG_RADV], [RAdv protocol])
|
||||
AH_TEMPLATE([CONFIG_RIP], [RIP protocol])
|
||||
AH_TEMPLATE([CONFIG_RPKI], [RPKI protocol])
|
||||
AH_TEMPLATE([CONFIG_STATIC], [Static protocol])
|
||||
|
||||
AC_MSG_CHECKING([protocols])
|
||||
@ -306,7 +337,7 @@ AC_MSG_RESULT([ok])
|
||||
AC_SUBST([protocols])
|
||||
|
||||
case $sysdesc in
|
||||
*/linux*|*/linux-v6*)
|
||||
*/linux*)
|
||||
AC_CHECK_HEADER([linux/rtnetlink.h],
|
||||
[],
|
||||
[AC_MSG_ERROR([Appropriate version of Linux kernel headers not found.])],
|
||||
@ -344,18 +375,38 @@ fi
|
||||
|
||||
if test "$enable_debug" = yes ; then
|
||||
AC_DEFINE([DEBUGGING], [1], [Define to 1 if debugging is enabled])
|
||||
LDFLAGS="$LDFLAGS -rdynamic"
|
||||
CFLAGS="$CFLAGS -O0 -ggdb -g3"
|
||||
|
||||
BIRD_CHECK_GCC_OPTION([bird_cv_c_option_dwarf4], [-gdwarf-4], [])
|
||||
BIRD_ADD_GCC_OPTION([bird_cv_c_option_dwarf4], [-gdwarf-4])
|
||||
|
||||
AC_CHECK_HEADER([execinfo.h],
|
||||
[
|
||||
AC_DEFINE([HAVE_EXECINFO_H], [1], [Define to 1 if you have the <execinfo.h> header file.])
|
||||
AC_SEARCH_LIBS([backtrace], [execinfo],
|
||||
[],
|
||||
[AC_MSG_ERROR([Function backtrace not available.])]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
if test "$enable_memcheck" = yes ; then
|
||||
AC_CHECK_LIB([dmalloc], [dmalloc_debug])
|
||||
if test $ac_cv_lib_dmalloc_dmalloc_debug != yes ; then
|
||||
AC_CHECK_LIB([efence], [malloc])
|
||||
fi
|
||||
fi
|
||||
|
||||
if test "enable_debug_expensive" = yes ; then
|
||||
AC_DEFINE([ENABLE_EXPENSIVE_CHECKS], [1], [Define to 1 if you want to run expensive consistency checks.])
|
||||
fi
|
||||
fi
|
||||
|
||||
CLIENT=
|
||||
CLIENT=birdcl
|
||||
CLIENT_LIBS=
|
||||
if test "$enable_client" = yes ; then
|
||||
CLIENT=birdc
|
||||
CLIENT="$CLIENT birdc"
|
||||
BASE_LIBS="$LIBS"
|
||||
LIBS=""
|
||||
|
||||
@ -402,20 +453,9 @@ AC_SUBST([CLIENT_LIBS])
|
||||
|
||||
mkdir -p $objdir/sysdep
|
||||
AC_CONFIG_HEADERS([$objdir/sysdep/autoconf.h:sysdep/autoconf.h.in])
|
||||
AC_CONFIG_COMMANDS([merge],
|
||||
[ export CPP="$CPP"; sh $srcdir/tools/mergedirs $srcdir $srcdir_rel $objdir $sysdep_dirs ],
|
||||
[
|
||||
srcdir=$srcdir
|
||||
srcdir_rel=$srcdir_rel
|
||||
objdir=$objdir
|
||||
sysdep_dirs="$sysdep_dirs"
|
||||
]
|
||||
)
|
||||
AC_CONFIG_FILES([$makefiles])
|
||||
AC_CONFIG_FILES([Makefile:Makefile.in])
|
||||
AC_OUTPUT
|
||||
|
||||
rm -f $objdir/sysdep/paths.h
|
||||
|
||||
AC_MSG_RESULT()
|
||||
AC_MSG_RESULT([BIRD was configured with the following options:])
|
||||
AC_MSG_RESULT([ Source directory: $srcdir])
|
||||
@ -425,6 +465,8 @@ AC_MSG_RESULT([ System configuration: $sysdesc])
|
||||
AC_MSG_RESULT([ Debugging: $enable_debug])
|
||||
AC_MSG_RESULT([ POSIX threads: $enable_pthreads])
|
||||
AC_MSG_RESULT([ Routing protocols: $protocols])
|
||||
AC_MSG_RESULT([ LibSSH support in RPKI: $enable_libssh])
|
||||
AC_MSG_RESULT([ Kernel MPLS support: $enable_mpls_kernel])
|
||||
AC_MSG_RESULT([ Client: $enable_client])
|
||||
|
||||
rm -f $objdir/.*-stamp
|
||||
|
77
doc/Makefile
77
doc/Makefile
@ -1,52 +1,49 @@
|
||||
root-rel=../
|
||||
dir-name=doc
|
||||
|
||||
ifneq ($(wildcard ../Rules),)
|
||||
include ../Rules
|
||||
else
|
||||
srcdir=$(shell cd $(root-rel) ; pwd)
|
||||
srcdir_abs=$(srcdir)
|
||||
endif
|
||||
|
||||
export TEXINPUTS := $(TEXINPUTS):$(srcdir_abs)/doc/tex
|
||||
|
||||
# Force rebuilds
|
||||
.PHONY: prog.sgml bird.sgml
|
||||
.PHONY: progspell docs progdocs userdocs
|
||||
|
||||
doc-srcdir := $(shell cd $(s) && pwd)
|
||||
sgml2 := $(doc-srcdir)/sgml2
|
||||
|
||||
docs: progdocs userdocs
|
||||
progdocs: prog.html prog.pdf
|
||||
userdocs: bird.html bird.pdf
|
||||
|
||||
prog.sgml:
|
||||
$(srcdir)/tools/progdoc $(srcdir_abs)
|
||||
doc-fmt = $(1): $(o)prog.$(1) $(o)bird.$(1)
|
||||
$(call doc-fmt,html)
|
||||
$(call doc-fmt,dvi)
|
||||
$(call doc-fmt,ps)
|
||||
$(call doc-fmt,pdf)
|
||||
|
||||
%.html: %.sgml
|
||||
./sgml2html $<
|
||||
progdocs: $(o)prog.html $(o)prog.pdf
|
||||
userdocs: $(o)bird.html $(o)bird.pdf
|
||||
progspell: $(o)prog.spell
|
||||
|
||||
%.dvi: %.tex
|
||||
latex $<
|
||||
latex $<
|
||||
$(o)prog.sgml: $(srcdir)/tools/progdoc $(objdir)/.dir-stamp
|
||||
$(srcdir)/tools/progdoc $(srcdir) $@
|
||||
|
||||
%.ps: %.dvi
|
||||
$(o)%.sgml: $(s)%.sgml $(objdir)/.dir-stamp
|
||||
cp $< $@
|
||||
|
||||
$(o)%.html: $(o)%.sgml
|
||||
cd $(dir $@) && $(sgml2)html $(notdir $<)
|
||||
|
||||
$(o)%.tex: $(o)%.sgml
|
||||
cd $(dir $@) && $(sgml2)latex --output=tex $(notdir $<)
|
||||
|
||||
$(o)%.dvi: $(o)%.tex
|
||||
cd $(dir $@) && TEXINPUTS=$(TEXINPUTS):$(doc-srcdir)/tex latex $(notdir $<)
|
||||
cd $(dir $@) && TEXINPUTS=$(TEXINPUTS):$(doc-srcdir)/tex latex $(notdir $<)
|
||||
|
||||
$(o)%.ps: $(o)%.dvi
|
||||
dvips -D600 -ta4 -o $@ $<
|
||||
|
||||
%.pdf: %.tex
|
||||
pdflatex $<
|
||||
pdflatex $<
|
||||
$(o)%.pdf: $(o)%.tex
|
||||
TEXINPUTS=$(TEXINPUTS):$(doc-srcdir)/tex pdflatex -output-directory=$(dir $@) $<
|
||||
TEXINPUTS=$(TEXINPUTS):$(doc-srcdir)/tex pdflatex -output-directory=$(dir $@) $<
|
||||
|
||||
%.tex: %.sgml
|
||||
./sgml2latex --output=tex $<
|
||||
$(o)%.txt: $(o)%.sgml
|
||||
cd $(dir $@) && $(sgml2)txt $(notdir $<)
|
||||
|
||||
%.txt: %.sgml
|
||||
./sgml2txt $<
|
||||
$(o)prog.spell: $(o)prog.sgml $(s)prog-spell.sed
|
||||
sed -f $(lastword $^) <$< >$@
|
||||
ispell $@
|
||||
|
||||
progspell: prog.sgml
|
||||
sed -f prog-spell.sed <prog.sgml >prog.spell
|
||||
ispell prog.spell
|
||||
|
||||
clean:
|
||||
rm -f *.tex *.dvi *.log *.txt *.aux *.toc *.spell
|
||||
rm -f prog.sgml
|
||||
|
||||
distclean: clean
|
||||
rm -f *.html *.ps
|
||||
$(call clean,prog.spell $(addprefix *.,html dvi ps pdf sgml tex txt aux log toc))
|
||||
|
@ -1,221 +1,204 @@
|
||||
/*
|
||||
* This is an example configuration file.
|
||||
*/
|
||||
|
||||
# Yes, even shell-like comments work...
|
||||
# This is a basic configuration file, which contains boilerplate options and
|
||||
# some basic examples. It allows the BIRD daemon to start but will not cause
|
||||
# anything else to happen.
|
||||
#
|
||||
# Please refer to the BIRD User's Guide documentation, which is also available
|
||||
# online at http://bird.network.cz/ in HTML format, for more information on
|
||||
# configuring BIRD and adding routing protocols.
|
||||
|
||||
# Configure logging
|
||||
#log syslog { debug, trace, info, remote, warning, error, auth, fatal, bug };
|
||||
#log stderr all;
|
||||
#log "tmp" all;
|
||||
log syslog all;
|
||||
# log "/var/log/bird.log" { debug, trace, info, remote, warning, error, auth, fatal, bug };
|
||||
|
||||
# Override router ID
|
||||
#router id 198.51.100.1;
|
||||
# Set router ID. It is a unique identification of your router, usually one of
|
||||
# IPv4 addresses of the router. It is recommended to configure it explicitly.
|
||||
# router id 198.51.100.1;
|
||||
|
||||
# You can define your own symbols...
|
||||
#define xyzzy = (120+10);
|
||||
#define '1a-a1' = (30+40);
|
||||
|
||||
# Define a route filter...
|
||||
#filter test_filter {
|
||||
# if net ~ 10.0.0.0/16 then accept;
|
||||
# else reject;
|
||||
#}
|
||||
|
||||
#filter sink { reject; }
|
||||
#filter okay { accept; }
|
||||
|
||||
#include "filters.conf";
|
||||
|
||||
# Define another routing table
|
||||
#table testable;
|
||||
|
||||
# Turn on global debugging of all protocols
|
||||
#debug protocols all;
|
||||
# Turn on global debugging of all protocols (all messages or just selected classes)
|
||||
# debug protocols all;
|
||||
# debug protocols { events, states };
|
||||
|
||||
# Turn on internal watchdog
|
||||
#watchdog warning 5 s;
|
||||
#watchdog timeout 30 s;
|
||||
# watchdog warning 5 s;
|
||||
# watchdog timeout 30 s;
|
||||
|
||||
# The direct protocol automatically generates device routes to
|
||||
# all network interfaces. Can exist in as many instances as you wish
|
||||
# if you want to populate multiple routing tables with device routes.
|
||||
#protocol direct {
|
||||
# interface "-eth*", "*"; # Restrict network interfaces it works with
|
||||
#}
|
||||
# You can define your own constants
|
||||
# define my_asn = 65000;
|
||||
# define my_addr = 198.51.100.1;
|
||||
|
||||
# This pseudo-protocol performs synchronization between BIRD's routing
|
||||
# tables and the kernel. If your kernel supports multiple routing tables
|
||||
# (as Linux 2.2.x does), you can run multiple instances of the kernel
|
||||
# protocol and synchronize different kernel tables with different BIRD tables.
|
||||
protocol kernel {
|
||||
# learn; # Learn all alien routes from the kernel
|
||||
persist; # Don't remove routes on bird shutdown
|
||||
scan time 20; # Scan kernel routing table every 20 seconds
|
||||
# import none; # Default is import all
|
||||
export all; # Default is export none
|
||||
# kernel table 5; # Kernel table to synchronize with (default: main)
|
||||
}
|
||||
# Tables master4 and master6 are defined by default
|
||||
# ipv4 table master4;
|
||||
# ipv6 table master6;
|
||||
|
||||
# This pseudo-protocol watches all interface up/down events.
|
||||
# Define more tables, e.g. for policy routing or as MRIB
|
||||
# ipv4 table mrib4;
|
||||
# ipv6 table mrib6;
|
||||
|
||||
# The Device protocol is not a real routing protocol. It does not generate any
|
||||
# routes and it only serves as a module for getting information about network
|
||||
# interfaces from the kernel. It is necessary in almost any configuration.
|
||||
protocol device {
|
||||
scan time 10; # Scan interfaces every 10 seconds
|
||||
}
|
||||
|
||||
# Static routes (again, there can be multiple instances, so that you
|
||||
# can disable/enable various groups of static routes on the fly).
|
||||
# The direct protocol is not a real routing protocol. It automatically generates
|
||||
# direct routes to all network interfaces. Can exist in as many instances as you
|
||||
# wish if you want to populate multiple routing tables with direct routes.
|
||||
protocol direct {
|
||||
disabled; # Disable by default
|
||||
ipv4; # Connect to default IPv4 table
|
||||
ipv6; # ... and to default IPv6 table
|
||||
}
|
||||
|
||||
# The Kernel protocol is not a real routing protocol. Instead of communicating
|
||||
# with other routers in the network, it performs synchronization of BIRD
|
||||
# routing tables with the OS kernel. One instance per table.
|
||||
protocol kernel {
|
||||
ipv4 { # Connect protocol to IPv4 table by channel
|
||||
# table master4; # Default IPv4 table is master4
|
||||
# import all; # Import to table, default is import all
|
||||
export all; # Export to protocol. default is export none
|
||||
};
|
||||
# learn; # Learn alien routes from the kernel
|
||||
# kernel table 10; # Kernel table to synchronize with (default: main)
|
||||
}
|
||||
|
||||
# Another instance for IPv6, skipping default options
|
||||
protocol kernel {
|
||||
ipv6 { export all; };
|
||||
}
|
||||
|
||||
# Static routes (Again, there can be multiple instances, for different address
|
||||
# families and to disable/enable various groups of static routes on the fly).
|
||||
protocol static {
|
||||
# disabled; # Disable by default
|
||||
# table testable; # Connect to a non-default table
|
||||
# preference 1000; # Default preference of routes
|
||||
# debug { states, routes, filters, interfaces, events, packets };
|
||||
# debug all;
|
||||
# route 0.0.0.0/0 via 198.51.100.13;
|
||||
# route 198.51.100.0/25 unreachable;
|
||||
ipv4; # Again, IPv4 channel with default options
|
||||
|
||||
# route 0.0.0.0/0 via 198.51.100.10;
|
||||
# route 192.0.2.0/24 blackhole;
|
||||
# route 10.0.0.0/8 unreachable;
|
||||
# route 10.1.1.0:255.255.255.0 via 198.51.100.3;
|
||||
# route 10.1.2.0:255.255.255.0 via 198.51.100.3;
|
||||
# route 10.1.3.0:255.255.255.0 via 198.51.100.4;
|
||||
# route 10.2.0.0/24 via "arc0";
|
||||
# route 10.2.0.0/24 via "eth0";
|
||||
# # Static routes can be defined with optional attributes
|
||||
# route 10.1.1.0/24 via 198.51.100.3 { rip_metric = 3; };
|
||||
# route 10.1.2.0/24 via 198.51.100.3 { ospf_metric1 = 100; };
|
||||
# route 10.1.3.0/24 via 198.51.100.4 { ospf_metric2 = 100; };
|
||||
}
|
||||
|
||||
# Pipe protocol connects two routing tables... Beware of loops.
|
||||
#protocol pipe {
|
||||
# peer table testable;
|
||||
# Define what routes do we export to this protocol / import from it.
|
||||
# import all; # default is all
|
||||
# export all; # default is none
|
||||
# import none; # If you wish to disable imports
|
||||
# import filter test_filter; # Use named filter
|
||||
# import where source = RTS_DEVICE; # Use explicit filter
|
||||
#}
|
||||
# Pipe protocol connects two routing tables. Beware of loops.
|
||||
# protocol pipe {
|
||||
# table master4; # No ipv4/ipv6 channel definition like in other protocols
|
||||
# peer table mrib4;
|
||||
# import all; # Direction peer table -> table
|
||||
# export all; # Direction table -> peer table
|
||||
# }
|
||||
|
||||
# RIP aka Rest In Pieces...
|
||||
#protocol rip MyRIP { # You can also use an explicit name
|
||||
# preference xyzzy;
|
||||
# debug all;
|
||||
# port 1520;
|
||||
# period 7;
|
||||
# infinity 16;
|
||||
# garbage time 60;
|
||||
# interface "*" { mode broadcast; };
|
||||
# honor neighbor; # To whom do we agree to send the routing table
|
||||
# honor always;
|
||||
# honor never;
|
||||
# passwords {
|
||||
# password "nazdar";
|
||||
# RIP example, both RIP and RIPng are supported
|
||||
# protocol rip {
|
||||
# ipv4 {
|
||||
# # Export direct, static routes and ones from RIP itself
|
||||
# import all;
|
||||
# export where source ~ [ RTS_DEVICE, RTS_STATIC, RTS_RIP ];
|
||||
# };
|
||||
# authentication none;
|
||||
# import filter { print "importing"; accept; };
|
||||
# export filter { print "exporting"; accept; };
|
||||
#}
|
||||
# interface "eth*" {
|
||||
# update time 10; # Default period is 30
|
||||
# timeout time 60; # Default timeout is 180
|
||||
# authentication cryptographic; # No authentication by default
|
||||
# password "hello" { algorithm hmac sha256; }; # Default is MD5
|
||||
# };
|
||||
# }
|
||||
|
||||
#protocol ospf MyOSPF {
|
||||
# tick 2;
|
||||
# rfc1583compat yes;
|
||||
# area 0.0.0.0 {
|
||||
# stub no;
|
||||
# OSPF example, both OSPFv2 and OSPFv3 are supported
|
||||
# protocol ospf v3 {
|
||||
# ipv6 {
|
||||
# import all;
|
||||
# export where source = RTS_STATIC;
|
||||
# };
|
||||
# area 0 {
|
||||
# interface "eth*" {
|
||||
# hello 9;
|
||||
# retransmit 6;
|
||||
# cost 10;
|
||||
# transmit delay 5;
|
||||
# dead count 5;
|
||||
# wait 50;
|
||||
# type broadcast;
|
||||
# authentication simple;
|
||||
# password "pass";
|
||||
# type broadcast; # Detected by default
|
||||
# cost 10; # Interface metric
|
||||
# hello 5; # Default hello perid 10 is too long
|
||||
# };
|
||||
# interface "arc0" {
|
||||
# rx buffer large;
|
||||
# type nonbroadcast;
|
||||
# poll 14;
|
||||
# dead 75;
|
||||
# neighbors {
|
||||
# 10.1.1.2 eligible;
|
||||
# 10.1.1.4;
|
||||
# };
|
||||
# strict nonbroadcast yes;
|
||||
# interface "tun*" {
|
||||
# type ptp; # PtP mode, avoids DR selection
|
||||
# cost 100; # Interface metric
|
||||
# hello 5; # Default hello perid 10 is too long
|
||||
# };
|
||||
# interface "xxx0" {
|
||||
# passwords {
|
||||
# password "abc" {
|
||||
# id 1;
|
||||
# generate to "22-04-2003 11:00:06";
|
||||
# accept to "17-01-2004 12:01:05";
|
||||
# };
|
||||
# password "def" {
|
||||
# id 2;
|
||||
# generate from "22-04-2003 11:00:07";
|
||||
# accept from "17-01-2003 12:01:05";
|
||||
# };
|
||||
# };
|
||||
# authentication cryptographic;
|
||||
# interface "dummy0" {
|
||||
# stub; # Stub interface, just propagate it
|
||||
# };
|
||||
# };
|
||||
# area 20 {
|
||||
# stub 1;
|
||||
# interface "ppp1" {
|
||||
# hello 8;
|
||||
# authentication none;
|
||||
# };
|
||||
# interface "fr*";
|
||||
# virtual link 192.168.0.1 {
|
||||
# password "sdsdffsdfg";
|
||||
# authentication cryptographic;
|
||||
# };
|
||||
# };
|
||||
#}
|
||||
|
||||
# Define simple filter as an example for BGP import filter
|
||||
# See https://gitlab.labs.nic.cz/labs/bird/wikis/BGP_filtering for more examples
|
||||
# filter rt_import
|
||||
# {
|
||||
# if bgp_path.first != 64496 then accept;
|
||||
# if bgp_path.len > 64 then accept;
|
||||
# if bgp_next_hop != from then accept;
|
||||
# reject;
|
||||
# }
|
||||
|
||||
#protocol bgp {
|
||||
# disabled;
|
||||
# BGP example, explicit name 'uplink1' is used instead of default 'bgp1'
|
||||
# protocol bgp uplink1 {
|
||||
# description "My BGP uplink";
|
||||
# local as 65000;
|
||||
# neighbor 198.51.100.130 as 64496;
|
||||
# multihop;
|
||||
# hold time 240;
|
||||
# startup hold time 240;
|
||||
# connect retry time 120;
|
||||
# keepalive time 80; # defaults to hold time / 3
|
||||
# start delay time 5; # How long do we wait before initial connect
|
||||
# error wait time 60, 300;# Minimum and maximum time we wait after an error (when consecutive
|
||||
# # errors occur, we increase the delay exponentially ...
|
||||
# error forget time 300; # ... until this timeout expires)
|
||||
# disable after error; # Disable the protocol automatically when an error occurs
|
||||
# next hop self; # Disable next hop processing and always advertise our local address as nexthop
|
||||
# path metric 1; # Prefer routes with shorter paths (like Cisco does)
|
||||
# default bgp_med 0; # MED value we use for comparison when none is defined
|
||||
# default bgp_local_pref 0; # The same for local preference
|
||||
# source address 198.51.100.14; # What local address we use for the TCP connection
|
||||
# local 198.51.100.1 as 65000;
|
||||
# neighbor 198.51.100.10 as 64496;
|
||||
# hold time 90; # Default is 240
|
||||
# password "secret"; # Password used for MD5 authentication
|
||||
# rr client; # I am a route reflector and the neighor is my client
|
||||
# rr cluster id 1.0.0.1; # Use this value for cluster id instead of my router id
|
||||
# export where source=RTS_STATIC;
|
||||
# export filter {
|
||||
# if source = RTS_STATIC then {
|
||||
# bgp_community = -empty-; bgp_community = add(bgp_community,(65000,5678));
|
||||
# bgp_origin = 0;
|
||||
# bgp_community = -empty-; bgp_community.add((65000,5678));
|
||||
# if (65000,64501) ~ bgp_community then
|
||||
# bgp_community.add((0, 1));
|
||||
# if bgp_path ~ [= 65000 =] then
|
||||
# bgp_path.prepend(65000);
|
||||
# accept;
|
||||
# }
|
||||
# reject;
|
||||
#
|
||||
# ipv4 { # regular IPv4 unicast (1/1)
|
||||
# import filter rt_import;
|
||||
# export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
# };
|
||||
#
|
||||
# ipv6 { # regular IPv6 unicast (2/1)
|
||||
# import filter rt_import;
|
||||
# export filter { # The same as 'where' expression above
|
||||
# if source ~ [ RTS_STATIC, RTS_BGP ]
|
||||
# then accept;
|
||||
# else reject;
|
||||
# };
|
||||
# };
|
||||
#
|
||||
# ipv4 multicast { # IPv4 multicast topology (1/2)
|
||||
# table mrib4; # explicit IPv4 table
|
||||
# import filter rt_import;
|
||||
# export all;
|
||||
# };
|
||||
#
|
||||
# ipv6 multicast { # IPv6 multicast topology (2/2)
|
||||
# table mrib6; # explicit IPv6 table
|
||||
# import filter rt_import;
|
||||
# export all;
|
||||
# };
|
||||
#}
|
||||
#
|
||||
# Template usage example
|
||||
#template bgp rr_client {
|
||||
# disabled;
|
||||
# local as 65000;
|
||||
# multihop;
|
||||
|
||||
# Template example. Using templates to define IBGP route reflector clients.
|
||||
# template bgp rr_clients {
|
||||
# local 10.0.0.1 as 65000;
|
||||
# neighbor as 65000;
|
||||
# rr client;
|
||||
# rr cluster id 1.0.0.1;
|
||||
#}
|
||||
#
|
||||
#protocol bgp rr_abcd from rr_client {
|
||||
# neighbor 10.1.4.7 as 65000;
|
||||
#}
|
||||
# ipv4 {
|
||||
# import all;
|
||||
# export where source = RTS_BGP;
|
||||
# };
|
||||
#
|
||||
# ipv6 {
|
||||
# import all;
|
||||
# export where source = RTS_BGP;
|
||||
# };
|
||||
# }
|
||||
#
|
||||
# protocol bgp client1 from rr_clients {
|
||||
# neighbor 10.0.1.1;
|
||||
# }
|
||||
#
|
||||
# protocol bgp client2 from rr_clients {
|
||||
# neighbor 10.0.2.1;
|
||||
# }
|
||||
#
|
||||
# protocol bgp client3 from rr_clients {
|
||||
# neighbor 10.0.3.1;
|
||||
# }
|
||||
|
332
doc/bird.conf.example2
Normal file
332
doc/bird.conf.example2
Normal file
@ -0,0 +1,332 @@
|
||||
/*
|
||||
* This is an example configuration file for MP-BGP setting
|
||||
*/
|
||||
|
||||
|
||||
log "bird.log" all;
|
||||
# debug protocols all;
|
||||
|
||||
router id 192.168.1.1;
|
||||
|
||||
ipv4 table master4;
|
||||
ipv6 table master6;
|
||||
|
||||
ipv4 table mcast4;
|
||||
ipv6 table mcast6;
|
||||
|
||||
ipv4 table mtab4;
|
||||
ipv6 table mtab6;
|
||||
|
||||
vpn4 table vpntab4;
|
||||
vpn6 table vpntab6;
|
||||
|
||||
vpn4 table vpn4mc;
|
||||
vpn6 table vpn6mc;
|
||||
|
||||
flow4 table flowtab4;
|
||||
flow6 table flowtab6;
|
||||
|
||||
|
||||
protocol device {
|
||||
}
|
||||
|
||||
protocol kernel kernel4 {
|
||||
ipv4 {
|
||||
export all;
|
||||
};
|
||||
}
|
||||
|
||||
protocol kernel kernel6 {
|
||||
ipv6 {
|
||||
export all;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
protocol static static4 {
|
||||
ipv4;
|
||||
|
||||
route 10.10.0.0/24 via 192.168.1.2;
|
||||
route 10.10.1.0/24 via 192.168.1.2 { bgp_large_community.add((10,20,30)); bgp_large_community.add((10,(20*3),10)); };
|
||||
}
|
||||
|
||||
protocol static static6 {
|
||||
ipv6;
|
||||
|
||||
route 2001:db8:10:10::/64 via 2001:db8:1:1::10;
|
||||
route 2001:db8:10:11::/64 via 2001:db8:1:1::10;
|
||||
|
||||
route 2001:db8:1:1::/64 via fe80::ec9b:67ff:fe60:fd5d % ve1;
|
||||
}
|
||||
|
||||
# VPNv4 routes with MPLS labels
|
||||
protocol static statvpn4 {
|
||||
vpn4;
|
||||
|
||||
route 10:10 10.20.0.0/24 via 192.168.1.2 mpls 210;
|
||||
route 10:10 10.20.1.0/24 via 192.168.1.2 mpls 210;
|
||||
route 10:20 10.20.0.0/24 via 192.168.1.2 mpls 220;
|
||||
route 10:20 10.20.1.0/24 via 192.168.1.2 mpls 220;
|
||||
}
|
||||
|
||||
protocol static statvpn6 {
|
||||
vpn6;
|
||||
|
||||
route 10:10 2001:db8:20:10::/64 via 2001:db8:1:1::10 mpls 200/210;
|
||||
route 10:10 2001:db8:20:11::/64 via 2001:db8:1:1::10 mpls 200/210;
|
||||
route 10:20 2001:db8:20:10::/64 via 2001:db8:1:1::10 mpls 200/220;
|
||||
route 10:20 2001:db8:20:11::/64 via 2001:db8:1:1::10 mpls 200/220;
|
||||
}
|
||||
|
||||
# RFC 5575 flow specification
|
||||
protocol static flowstat4 {
|
||||
flow4;
|
||||
|
||||
route flow4 {
|
||||
dst 10.0.0.0/8;
|
||||
proto = 23;
|
||||
dport > 24 && < 30 || 40..50,60..70,80;
|
||||
sport > 24 && < 30 || = 40 || 50,60..70,80;
|
||||
icmp type 80;
|
||||
icmp code 90;
|
||||
tcp flags 0x03/0x0f;
|
||||
length 2048..65535;
|
||||
dscp = 63;
|
||||
fragment dont_fragment, is_fragment || !first_fragment;
|
||||
};
|
||||
|
||||
route flow4 {
|
||||
dst 11.0.0.0/8;
|
||||
proto = 0x12;
|
||||
sport > 0x5678 && < 0x9abc || 0xdef0 || 0x1234,0x5678,0x9abc..0xdef0;
|
||||
dport = 50;
|
||||
tcp flags 0x000/0xf00;
|
||||
};
|
||||
|
||||
route flow4 {
|
||||
dst 12.0.0.0/32;
|
||||
tcp flags ! 0/0x999;
|
||||
};
|
||||
|
||||
route flow4 {
|
||||
dst 220.0.254.0/24;
|
||||
tcp flags 0x99/0x999;
|
||||
};
|
||||
|
||||
route flow4 {
|
||||
dst 220.0.254.192/28;
|
||||
tcp flags ! 0xfff/0xfff;
|
||||
};
|
||||
|
||||
route flow4 {
|
||||
dst 15.0.0.0/8;
|
||||
tcp flags ! 0x999/0x999;
|
||||
};
|
||||
}
|
||||
|
||||
protocol static flowstat6 {
|
||||
flow6;
|
||||
|
||||
route flow6 {
|
||||
dst fec0:1122:3344:5566::1/128;
|
||||
src 0000:0000:0000:0001:1234:5678:9800:0000/101 offset 63;
|
||||
next header = 23;
|
||||
sport 24..30, 42 || 50,60,70..80;
|
||||
dport = 50;
|
||||
tcp flags 0x03/0x0f, !0/0xff || 0x33/0x33;
|
||||
fragment !is_fragment || !first_fragment;
|
||||
label 0xaaaa/0xaaaa && 0x33/0x33;
|
||||
};
|
||||
|
||||
route flow6 {
|
||||
dst fec0:1122:3344:5566::1/128;
|
||||
src ::1:1234:5678:9800:0/101 offset 63;
|
||||
next header = 23;
|
||||
dport = 50;
|
||||
sport > 24 && < 30 || = 40 || = 50 || = 60 || >= 70 && <= 80;
|
||||
tcp flags 0x3/0x3 && 0x0/0xc;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
protocol pipe {
|
||||
table master4;
|
||||
peer table mcast4;
|
||||
import none;
|
||||
export where source = RTS_OSPF;
|
||||
}
|
||||
|
||||
protocol pipe {
|
||||
table master6;
|
||||
peer table mcast6;
|
||||
import none;
|
||||
export where source = RTS_OSPF;
|
||||
}
|
||||
|
||||
protocol ospf v2 ospf4 {
|
||||
ipv4 {
|
||||
import all;
|
||||
# export where source = RTS_STATIC;
|
||||
};
|
||||
|
||||
area 0 {
|
||||
interface "ve0" { stub; };
|
||||
interface "ve1" { hello 5; type ptp; };
|
||||
interface "ve2" { hello 5; type bcast; ttl security; };
|
||||
interface "ve3" { hello 5; type bcast; ttl security; };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
protocol ospf v3 ospf6 {
|
||||
ipv6 {
|
||||
import all;
|
||||
# export where source = RTS_STATIC;
|
||||
};
|
||||
|
||||
area 0 {
|
||||
interface "ve0" { stub; };
|
||||
interface "ve1" { hello 5; type ptp; };
|
||||
interface "ve2" { hello 5; type bcast; };
|
||||
};
|
||||
}
|
||||
|
||||
protocol bgp {
|
||||
local 192.168.11.1 as 1000;
|
||||
neighbor 192.168.11.2 as 2000;
|
||||
# local 192.168.1.1 as 1000;
|
||||
# neighbor 192.168.2.1 as 2000;
|
||||
# multihop;
|
||||
# rr client;
|
||||
# strict bind;
|
||||
# debug all;
|
||||
|
||||
# regular IPv4 unicast (1/1)
|
||||
ipv4 {
|
||||
# connects to master4 table by default
|
||||
import all;
|
||||
export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
};
|
||||
|
||||
# regular IPv6 unicast (2/1)
|
||||
ipv6 {
|
||||
# connects to master6 table by default
|
||||
import all;
|
||||
export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
# next hop address 2001:db8:1:1::1;
|
||||
};
|
||||
|
||||
# IPv4 multicast topology (1/2)
|
||||
ipv4 multicast {
|
||||
# explicit IPv4 table
|
||||
table mcast4;
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# IPv6 multicast topology (2/2)
|
||||
ipv6 multicast {
|
||||
# explicit IPv6 table
|
||||
table mcast6;
|
||||
import all;
|
||||
export all;
|
||||
# next hop address 2001:db8:1:1::1;
|
||||
};
|
||||
|
||||
# IPv4 with MPLS labels (1/4)
|
||||
ipv4 mpls {
|
||||
# explicit IPv4 table
|
||||
table mtab4;
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# IPv6 with MPLS labels (2/4)
|
||||
ipv6 mpls {
|
||||
# explicit IPv6 table
|
||||
table mtab6;
|
||||
import all;
|
||||
export all;
|
||||
# allows IPv4 next hops (6PE)
|
||||
# extended next hop;
|
||||
};
|
||||
|
||||
# VPNv4 with MPLS labels (1/128)
|
||||
vpn4 mpls {
|
||||
# connects to vpntab4 table by default
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# VPNv6 with MPLS labels (2/128)
|
||||
vpn6 mpls {
|
||||
# connects to vpntab6 table by default
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# VPNv4 multicast topology (1/129)
|
||||
vpn4 multicast {
|
||||
table vpn4mc;
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# VPNv6 multicast topology (2/129)
|
||||
vpn6 multicast {
|
||||
table vpn6mc;
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# IPv4 Flowspec (1/133)
|
||||
flow4 {
|
||||
# connects to flowtab4 table by default
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
|
||||
# IPv6 Flowspec (2/133)
|
||||
flow6 {
|
||||
# connects to flowtab6 table by default
|
||||
import all;
|
||||
export all;
|
||||
};
|
||||
}
|
||||
|
||||
protocol bgp {
|
||||
local 192.168.1.1 as 1000;
|
||||
neighbor 192.168.3.1 as 1000;
|
||||
multihop;
|
||||
rr client;
|
||||
|
||||
ipv4 {
|
||||
import all;
|
||||
export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
};
|
||||
|
||||
ipv6 {
|
||||
import all;
|
||||
export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
next hop address 2001:db8:1:1::1;
|
||||
};
|
||||
}
|
||||
|
||||
protocol bgp {
|
||||
local 2001:db8:1:1::1 as 1000;
|
||||
neighbor 2001:db8:4:1::1 as 1000;
|
||||
multihop;
|
||||
rr client;
|
||||
|
||||
ipv4 {
|
||||
import all;
|
||||
export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
next hop address 192.168.4.1;
|
||||
};
|
||||
|
||||
ipv6 {
|
||||
import all;
|
||||
export where source ~ [ RTS_STATIC, RTS_BGP ];
|
||||
};
|
||||
}
|
||||
|
2311
doc/bird.sgml
2311
doc/bird.sgml
File diff suppressed because it is too large
Load Diff
@ -33,6 +33,7 @@ Reply codes of BIRD command-line interface
|
||||
0022 Undo scheduled
|
||||
0023 Evaluation of expression
|
||||
0024 Graceful restart status report
|
||||
0025 Graceful restart ordered
|
||||
|
||||
1000 BIRD version
|
||||
1001 Interface list
|
||||
|
@ -1,5 +1,24 @@
|
||||
source=f-util.c filter.c tree.c trie.c
|
||||
root-rel=../
|
||||
dir-name=filter
|
||||
src := filter.c data.c f-util.c tree.c trie.c inst-gen.c
|
||||
obj := $(src-o-files)
|
||||
$(all-daemon)
|
||||
$(cf-local)
|
||||
|
||||
include ../Rules
|
||||
#M4FLAGS_FILTERS=$(filter-out -s,$(M4FLAGS))
|
||||
M4FLAGS_FILTERS=$(M4FLAGS)
|
||||
|
||||
$(o)inst-gen.h: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp
|
||||
$(M4) $(M4FLAGS_FILTERS) -DTARGET=H -P $^ >$@
|
||||
|
||||
$(o)inst-gen.c: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp
|
||||
$(M4) $(M4FLAGS_FILTERS) -DTARGET=C -P $^ >$@
|
||||
|
||||
$(o)inst-interpret.c: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp
|
||||
$(M4) $(M4FLAGS_FILTERS) -DTARGET=I -P $^ >$@
|
||||
|
||||
prepare: $(o)inst-interpret.c $(o)inst-gen.h
|
||||
|
||||
tests_src := tree_test.c filter_test.c trie_test.c
|
||||
tests_targets := $(tests_targets) $(tests-target-files)
|
||||
tests_objs := $(tests_objs) $(src-o-files)
|
||||
|
||||
$(call clean,inst-gen.h inst-gen.c inst-interpret.c)
|
||||
|
926
filter/config.Y
926
filter/config.Y
File diff suppressed because it is too large
Load Diff
601
filter/data.c
Normal file
601
filter/data.c
Normal file
@ -0,0 +1,601 @@
|
||||
/*
|
||||
* Filters: utility functions
|
||||
*
|
||||
* (c) 1998 Pavel Machek <pavel@ucw.cz>
|
||||
* (c) 2019 Maria Matejka <mq@jmq.cz>
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/lists.h"
|
||||
#include "lib/resource.h"
|
||||
#include "lib/socket.h"
|
||||
#include "lib/string.h"
|
||||
#include "lib/unaligned.h"
|
||||
#include "lib/net.h"
|
||||
#include "lib/ip.h"
|
||||
#include "nest/route.h"
|
||||
#include "nest/protocol.h"
|
||||
#include "nest/iface.h"
|
||||
#include "nest/attrs.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/f-inst.h"
|
||||
#include "filter/data.h"
|
||||
|
||||
static const char * const f_type_str[] = {
|
||||
[T_VOID] = "void",
|
||||
|
||||
[T_INT] = "int",
|
||||
[T_BOOL] = "bool",
|
||||
[T_PAIR] = "pair",
|
||||
[T_QUAD] = "quad",
|
||||
|
||||
[T_ENUM_RTS] = "enum rts",
|
||||
[T_ENUM_BGP_ORIGIN] = "enum bgp_origin",
|
||||
[T_ENUM_SCOPE] = "enum scope",
|
||||
[T_ENUM_RTC] = "enum rtc",
|
||||
[T_ENUM_RTD] = "enum rtd",
|
||||
[T_ENUM_ROA] = "enum roa",
|
||||
[T_ENUM_NETTYPE] = "enum nettype",
|
||||
[T_ENUM_RA_PREFERENCE] = "enum ra_preference",
|
||||
[T_ENUM_AF] = "enum af",
|
||||
|
||||
[T_IP] = "ip",
|
||||
[T_NET] = "prefix",
|
||||
[T_STRING] = "string",
|
||||
[T_PATH_MASK] = "bgpmask",
|
||||
[T_PATH] = "bgppath",
|
||||
[T_CLIST] = "clist",
|
||||
[T_EC] = "ec",
|
||||
[T_ECLIST] = "eclist",
|
||||
[T_LC] = "lc",
|
||||
[T_LCLIST] = "lclist",
|
||||
[T_RD] = "rd",
|
||||
};
|
||||
|
||||
const char *
|
||||
f_type_name(enum f_type t)
|
||||
{
|
||||
if (t < ARRAY_SIZE(f_type_str))
|
||||
return f_type_str[t] ?: "?";
|
||||
|
||||
if ((t == T_SET) || (t == T_PREFIX_SET))
|
||||
return "set";
|
||||
|
||||
return "?";
|
||||
}
|
||||
|
||||
const struct f_val f_const_empty_path = {
|
||||
.type = T_PATH,
|
||||
.val.ad = &null_adata,
|
||||
}, f_const_empty_clist = {
|
||||
.type = T_CLIST,
|
||||
.val.ad = &null_adata,
|
||||
}, f_const_empty_eclist = {
|
||||
.type = T_ECLIST,
|
||||
.val.ad = &null_adata,
|
||||
}, f_const_empty_lclist = {
|
||||
.type = T_LCLIST,
|
||||
.val.ad = &null_adata,
|
||||
};
|
||||
|
||||
static struct adata *
|
||||
adata_empty(struct linpool *pool, int l)
|
||||
{
|
||||
struct adata *res = lp_alloc(pool, sizeof(struct adata) + l);
|
||||
res->length = l;
|
||||
return res;
|
||||
}
|
||||
|
||||
static void
|
||||
pm_format(const struct f_path_mask *p, buffer *buf)
|
||||
{
|
||||
int loop = 0;
|
||||
|
||||
buffer_puts(buf, "[= ");
|
||||
|
||||
for (uint i=0; i<p->len; i++)
|
||||
{
|
||||
switch(p->item[i].kind)
|
||||
{
|
||||
case PM_ASN:
|
||||
buffer_print(buf, "%u ", p->item[i].asn);
|
||||
break;
|
||||
|
||||
case PM_QUESTION:
|
||||
buffer_puts(buf, "? ");
|
||||
break;
|
||||
|
||||
case PM_ASTERISK:
|
||||
buffer_puts(buf, "* ");
|
||||
break;
|
||||
|
||||
case PM_LOOP:
|
||||
loop = 1;
|
||||
break;
|
||||
|
||||
case PM_ASN_RANGE:
|
||||
buffer_print(buf, "%u..%u ", p->item[i].from, p->item[i].to);
|
||||
break;
|
||||
|
||||
case PM_ASN_SET:
|
||||
tree_format(p->item[i].set, buf);
|
||||
buffer_puts(buf, " ");
|
||||
break;
|
||||
|
||||
case PM_ASN_EXPR:
|
||||
ASSERT(0);
|
||||
}
|
||||
|
||||
if (loop && (p->item[i].kind != PM_LOOP))
|
||||
{
|
||||
buffer_puts(buf, "+ ");
|
||||
loop = 0;
|
||||
}
|
||||
}
|
||||
|
||||
buffer_puts(buf, "=]");
|
||||
}
|
||||
|
||||
static inline int
|
||||
lcomm_cmp(lcomm v1, lcomm v2)
|
||||
{
|
||||
if (v1.asn != v2.asn)
|
||||
return (v1.asn > v2.asn) ? 1 : -1;
|
||||
if (v1.ldp1 != v2.ldp1)
|
||||
return (v1.ldp1 > v2.ldp1) ? 1 : -1;
|
||||
if (v1.ldp2 != v2.ldp2)
|
||||
return (v1.ldp2 > v2.ldp2) ? 1 : -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* val_compare - compare two values
|
||||
* @v1: first value
|
||||
* @v2: second value
|
||||
*
|
||||
* Compares two values and returns -1, 0, 1 on <, =, > or F_CMP_ERROR on
|
||||
* error. Tree module relies on this giving consistent results so
|
||||
* that it can be used for building balanced trees.
|
||||
*/
|
||||
int
|
||||
val_compare(const struct f_val *v1, const struct f_val *v2)
|
||||
{
|
||||
if (v1->type != v2->type) {
|
||||
if (v1->type == T_VOID) /* Hack for else */
|
||||
return -1;
|
||||
if (v2->type == T_VOID)
|
||||
return 1;
|
||||
|
||||
/* IP->Quad implicit conversion */
|
||||
if ((v1->type == T_QUAD) && val_is_ip4(v2))
|
||||
return uint_cmp(v1->val.i, ipa_to_u32(v2->val.ip));
|
||||
if (val_is_ip4(v1) && (v2->type == T_QUAD))
|
||||
return uint_cmp(ipa_to_u32(v1->val.ip), v2->val.i);
|
||||
|
||||
debug( "Types do not match in val_compare\n" );
|
||||
return F_CMP_ERROR;
|
||||
}
|
||||
|
||||
switch (v1->type) {
|
||||
case T_VOID:
|
||||
return 0;
|
||||
case T_ENUM:
|
||||
case T_INT:
|
||||
case T_BOOL:
|
||||
case T_PAIR:
|
||||
case T_QUAD:
|
||||
return uint_cmp(v1->val.i, v2->val.i);
|
||||
case T_EC:
|
||||
case T_RD:
|
||||
return u64_cmp(v1->val.ec, v2->val.ec);
|
||||
case T_LC:
|
||||
return lcomm_cmp(v1->val.lc, v2->val.lc);
|
||||
case T_IP:
|
||||
return ipa_compare(v1->val.ip, v2->val.ip);
|
||||
case T_NET:
|
||||
return net_compare(v1->val.net, v2->val.net);
|
||||
case T_STRING:
|
||||
return strcmp(v1->val.s, v2->val.s);
|
||||
default:
|
||||
return F_CMP_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int
|
||||
pmi_same(const struct f_path_mask_item *mi1, const struct f_path_mask_item *mi2)
|
||||
{
|
||||
if (mi1->kind != mi2->kind)
|
||||
return 0;
|
||||
|
||||
switch (mi1->kind) {
|
||||
case PM_ASN:
|
||||
if (mi1->asn != mi2->asn)
|
||||
return 0;
|
||||
break;
|
||||
case PM_ASN_EXPR:
|
||||
if (!f_same(mi1->expr, mi2->expr))
|
||||
return 0;
|
||||
break;
|
||||
case PM_ASN_RANGE:
|
||||
if (mi1->from != mi2->from)
|
||||
return 0;
|
||||
if (mi1->to != mi2->to)
|
||||
return 0;
|
||||
break;
|
||||
case PM_ASN_SET:
|
||||
if (!same_tree(mi1->set, mi2->set))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
pm_same(const struct f_path_mask *m1, const struct f_path_mask *m2)
|
||||
{
|
||||
if (m1->len != m2->len)
|
||||
return 0;
|
||||
|
||||
for (uint i=0; i<m1->len; i++)
|
||||
if (!pmi_same(&(m1->item[i]), &(m2->item[i])))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* val_same - compare two values
|
||||
* @v1: first value
|
||||
* @v2: second value
|
||||
*
|
||||
* Compares two values and returns 1 if they are same and 0 if not.
|
||||
* Comparison of values of different types is valid and returns 0.
|
||||
*/
|
||||
int
|
||||
val_same(const struct f_val *v1, const struct f_val *v2)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = val_compare(v1, v2);
|
||||
if (rc != F_CMP_ERROR)
|
||||
return !rc;
|
||||
|
||||
if (v1->type != v2->type)
|
||||
return 0;
|
||||
|
||||
switch (v1->type) {
|
||||
case T_PATH_MASK:
|
||||
return pm_same(v1->val.path_mask, v2->val.path_mask);
|
||||
case T_PATH_MASK_ITEM:
|
||||
return pmi_same(&(v1->val.pmi), &(v2->val.pmi));
|
||||
case T_PATH:
|
||||
case T_CLIST:
|
||||
case T_ECLIST:
|
||||
case T_LCLIST:
|
||||
return adata_same(v1->val.ad, v2->val.ad);
|
||||
case T_SET:
|
||||
return same_tree(v1->val.t, v2->val.t);
|
||||
case T_PREFIX_SET:
|
||||
return trie_same(v1->val.ti, v2->val.ti);
|
||||
default:
|
||||
bug("Invalid type in val_same(): %x", v1->type);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
clist_set_type(const struct f_tree *set, struct f_val *v)
|
||||
{
|
||||
switch (set->from.type)
|
||||
{
|
||||
case T_PAIR:
|
||||
v->type = T_PAIR;
|
||||
return 1;
|
||||
|
||||
case T_QUAD:
|
||||
v->type = T_QUAD;
|
||||
return 1;
|
||||
|
||||
case T_IP:
|
||||
if (val_is_ip4(&(set->from)) && val_is_ip4(&(set->to)))
|
||||
{
|
||||
v->type = T_QUAD;
|
||||
return 1;
|
||||
}
|
||||
/* Fall through */
|
||||
default:
|
||||
v->type = T_VOID;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
clist_match_set(const struct adata *clist, const struct f_tree *set)
|
||||
{
|
||||
if (!clist)
|
||||
return 0;
|
||||
|
||||
struct f_val v;
|
||||
if (!clist_set_type(set, &v))
|
||||
return F_CMP_ERROR;
|
||||
|
||||
u32 *l = (u32 *) clist->data;
|
||||
u32 *end = l + clist->length/4;
|
||||
|
||||
while (l < end) {
|
||||
v.val.i = *l++;
|
||||
if (find_tree(set, &v))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
eclist_match_set(const struct adata *list, const struct f_tree *set)
|
||||
{
|
||||
if (!list)
|
||||
return 0;
|
||||
|
||||
if (!eclist_set_type(set))
|
||||
return F_CMP_ERROR;
|
||||
|
||||
struct f_val v;
|
||||
u32 *l = int_set_get_data(list);
|
||||
int len = int_set_get_size(list);
|
||||
int i;
|
||||
|
||||
v.type = T_EC;
|
||||
for (i = 0; i < len; i += 2) {
|
||||
v.val.ec = ec_get(l, i);
|
||||
if (find_tree(set, &v))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
lclist_match_set(const struct adata *list, const struct f_tree *set)
|
||||
{
|
||||
if (!list)
|
||||
return 0;
|
||||
|
||||
if (!lclist_set_type(set))
|
||||
return F_CMP_ERROR;
|
||||
|
||||
struct f_val v;
|
||||
u32 *l = int_set_get_data(list);
|
||||
int len = int_set_get_size(list);
|
||||
int i;
|
||||
|
||||
v.type = T_LC;
|
||||
for (i = 0; i < len; i += 3) {
|
||||
v.val.lc = lc_get(l, i);
|
||||
if (find_tree(set, &v))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct adata *
|
||||
clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos)
|
||||
{
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
int tree = (set->type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
|
||||
struct f_val v;
|
||||
if (tree)
|
||||
clist_set_type(set->val.t, &v);
|
||||
else
|
||||
v.type = T_PAIR;
|
||||
|
||||
int len = int_set_get_size(list);
|
||||
u32 *l = int_set_get_data(list);
|
||||
u32 tmp[len];
|
||||
u32 *k = tmp;
|
||||
u32 *end = l + len;
|
||||
|
||||
while (l < end) {
|
||||
v.val.i = *l++;
|
||||
/* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
|
||||
if ((tree ? !!find_tree(set->val.t, &v) : int_set_contains(set->val.ad, v.val.i)) == pos)
|
||||
*k++ = v.val.i;
|
||||
}
|
||||
|
||||
uint nl = (k - tmp) * sizeof(u32);
|
||||
if (nl == list->length)
|
||||
return list;
|
||||
|
||||
struct adata *res = adata_empty(pool, nl);
|
||||
memcpy(res->data, tmp, nl);
|
||||
return res;
|
||||
}
|
||||
|
||||
const struct adata *
|
||||
eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos)
|
||||
{
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
int tree = (set->type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
|
||||
struct f_val v;
|
||||
|
||||
int len = int_set_get_size(list);
|
||||
u32 *l = int_set_get_data(list);
|
||||
u32 tmp[len];
|
||||
u32 *k = tmp;
|
||||
int i;
|
||||
|
||||
v.type = T_EC;
|
||||
for (i = 0; i < len; i += 2) {
|
||||
v.val.ec = ec_get(l, i);
|
||||
/* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
|
||||
if ((tree ? !!find_tree(set->val.t, &v) : ec_set_contains(set->val.ad, v.val.ec)) == pos) {
|
||||
*k++ = l[i];
|
||||
*k++ = l[i+1];
|
||||
}
|
||||
}
|
||||
|
||||
uint nl = (k - tmp) * sizeof(u32);
|
||||
if (nl == list->length)
|
||||
return list;
|
||||
|
||||
struct adata *res = adata_empty(pool, nl);
|
||||
memcpy(res->data, tmp, nl);
|
||||
return res;
|
||||
}
|
||||
|
||||
const struct adata *
|
||||
lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos)
|
||||
{
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
int tree = (set->type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
|
||||
struct f_val v;
|
||||
|
||||
int len = int_set_get_size(list);
|
||||
u32 *l = int_set_get_data(list);
|
||||
u32 tmp[len];
|
||||
u32 *k = tmp;
|
||||
int i;
|
||||
|
||||
v.type = T_LC;
|
||||
for (i = 0; i < len; i += 3) {
|
||||
v.val.lc = lc_get(l, i);
|
||||
/* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
|
||||
if ((tree ? !!find_tree(set->val.t, &v) : lc_set_contains(set->val.ad, v.val.lc)) == pos)
|
||||
k = lc_copy(k, l+i);
|
||||
}
|
||||
|
||||
uint nl = (k - tmp) * sizeof(u32);
|
||||
if (nl == list->length)
|
||||
return list;
|
||||
|
||||
struct adata *res = adata_empty(pool, nl);
|
||||
memcpy(res->data, tmp, nl);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* val_in_range - implement |~| operator
|
||||
* @v1: element
|
||||
* @v2: set
|
||||
*
|
||||
* Checks if @v1 is element (|~| operator) of @v2.
|
||||
*/
|
||||
int
|
||||
val_in_range(const struct f_val *v1, const struct f_val *v2)
|
||||
{
|
||||
if ((v1->type == T_PATH) && (v2->type == T_PATH_MASK))
|
||||
return as_path_match(v1->val.ad, v2->val.path_mask);
|
||||
|
||||
if ((v1->type == T_INT) && (v2->type == T_PATH))
|
||||
return as_path_contains(v2->val.ad, v1->val.i, 1);
|
||||
|
||||
if (((v1->type == T_PAIR) || (v1->type == T_QUAD)) && (v2->type == T_CLIST))
|
||||
return int_set_contains(v2->val.ad, v1->val.i);
|
||||
/* IP->Quad implicit conversion */
|
||||
if (val_is_ip4(v1) && (v2->type == T_CLIST))
|
||||
return int_set_contains(v2->val.ad, ipa_to_u32(v1->val.ip));
|
||||
|
||||
if ((v1->type == T_EC) && (v2->type == T_ECLIST))
|
||||
return ec_set_contains(v2->val.ad, v1->val.ec);
|
||||
|
||||
if ((v1->type == T_LC) && (v2->type == T_LCLIST))
|
||||
return lc_set_contains(v2->val.ad, v1->val.lc);
|
||||
|
||||
if ((v1->type == T_STRING) && (v2->type == T_STRING))
|
||||
return patmatch(v2->val.s, v1->val.s);
|
||||
|
||||
if ((v1->type == T_IP) && (v2->type == T_NET))
|
||||
return ipa_in_netX(v1->val.ip, v2->val.net);
|
||||
|
||||
if ((v1->type == T_NET) && (v2->type == T_NET))
|
||||
return net_in_netX(v1->val.net, v2->val.net);
|
||||
|
||||
if ((v1->type == T_NET) && (v2->type == T_PREFIX_SET))
|
||||
return trie_match_net(v2->val.ti, v1->val.net);
|
||||
|
||||
if (v2->type != T_SET)
|
||||
return F_CMP_ERROR;
|
||||
|
||||
/* With integrated Quad<->IP implicit conversion */
|
||||
if ((v1->type == v2->val.t->from.type) ||
|
||||
((v1->type == T_QUAD) && val_is_ip4(&(v2->val.t->from)) && val_is_ip4(&(v2->val.t->to))))
|
||||
return !!find_tree(v2->val.t, v1);
|
||||
|
||||
if (v1->type == T_CLIST)
|
||||
return clist_match_set(v1->val.ad, v2->val.t);
|
||||
|
||||
if (v1->type == T_ECLIST)
|
||||
return eclist_match_set(v1->val.ad, v2->val.t);
|
||||
|
||||
if (v1->type == T_LCLIST)
|
||||
return lclist_match_set(v1->val.ad, v2->val.t);
|
||||
|
||||
if (v1->type == T_PATH)
|
||||
return as_path_match_set(v1->val.ad, v2->val.t);
|
||||
|
||||
return F_CMP_ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* val_format - format filter value
|
||||
*/
|
||||
void
|
||||
val_format(const struct f_val *v, buffer *buf)
|
||||
{
|
||||
char buf2[1024];
|
||||
switch (v->type)
|
||||
{
|
||||
case T_VOID: buffer_puts(buf, "(void)"); return;
|
||||
case T_BOOL: buffer_puts(buf, v->val.i ? "TRUE" : "FALSE"); return;
|
||||
case T_INT: buffer_print(buf, "%u", v->val.i); return;
|
||||
case T_STRING: buffer_print(buf, "%s", v->val.s); return;
|
||||
case T_IP: buffer_print(buf, "%I", v->val.ip); return;
|
||||
case T_NET: buffer_print(buf, "%N", v->val.net); return;
|
||||
case T_PAIR: buffer_print(buf, "(%u,%u)", v->val.i >> 16, v->val.i & 0xffff); return;
|
||||
case T_QUAD: buffer_print(buf, "%R", v->val.i); return;
|
||||
case T_EC: ec_format(buf2, v->val.ec); buffer_print(buf, "%s", buf2); return;
|
||||
case T_LC: lc_format(buf2, v->val.lc); buffer_print(buf, "%s", buf2); return;
|
||||
case T_RD: rd_format(v->val.ec, buf2, 1024); buffer_print(buf, "%s", buf2); return;
|
||||
case T_PREFIX_SET: trie_format(v->val.ti, buf); return;
|
||||
case T_SET: tree_format(v->val.t, buf); return;
|
||||
case T_ENUM: buffer_print(buf, "(enum %x)%u", v->type, v->val.i); return;
|
||||
case T_PATH: as_path_format(v->val.ad, buf2, 1000); buffer_print(buf, "(path %s)", buf2); return;
|
||||
case T_CLIST: int_set_format(v->val.ad, 1, -1, buf2, 1000); buffer_print(buf, "(clist %s)", buf2); return;
|
||||
case T_ECLIST: ec_set_format(v->val.ad, -1, buf2, 1000); buffer_print(buf, "(eclist %s)", buf2); return;
|
||||
case T_LCLIST: lc_set_format(v->val.ad, -1, buf2, 1000); buffer_print(buf, "(lclist %s)", buf2); return;
|
||||
case T_PATH_MASK: pm_format(v->val.path_mask, buf); return;
|
||||
default: buffer_print(buf, "[unknown type %x]", v->type); return;
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
val_format_str(struct linpool *lp, const struct f_val *v) {
|
||||
buffer b;
|
||||
LOG_BUFFER_INIT(b);
|
||||
val_format(v, &b);
|
||||
return lp_strdup(lp, b.start);
|
||||
}
|
||||
|
||||
|
||||
static char val_dump_buffer[1024];
|
||||
const char *
|
||||
val_dump(const struct f_val *v) {
|
||||
static buffer b = {
|
||||
.start = val_dump_buffer,
|
||||
.end = val_dump_buffer + 1024,
|
||||
};
|
||||
b.pos = b.start;
|
||||
val_format(v, &b);
|
||||
return val_dump_buffer;
|
||||
}
|
||||
|
224
filter/data.h
Normal file
224
filter/data.h
Normal file
@ -0,0 +1,224 @@
|
||||
/*
|
||||
* BIRD Internet Routing Daemon -- Dynamic data structures
|
||||
*
|
||||
* (c) 1999 Pavel Machek <pavel@ucw.cz>
|
||||
* (c) 2018--2019 Maria Matejka <mq@jmq.cz>
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_FILTER_DATA_H_
|
||||
#define _BIRD_FILTER_DATA_H_
|
||||
|
||||
#include "nest/bird.h"
|
||||
|
||||
/* Type numbers must be in 0..0xff range */
|
||||
#define T_MASK 0xff
|
||||
|
||||
/* Internal types */
|
||||
enum f_type {
|
||||
/* Nothing. Simply nothing. */
|
||||
T_VOID = 0,
|
||||
|
||||
/* User visible types, which fit in int */
|
||||
T_INT = 0x10,
|
||||
T_BOOL = 0x11,
|
||||
T_PAIR = 0x12, /* Notice that pair is stored as integer: first << 16 | second */
|
||||
T_QUAD = 0x13,
|
||||
|
||||
/* Put enumerational types in 0x30..0x3f range */
|
||||
T_ENUM_LO = 0x30,
|
||||
T_ENUM_HI = 0x3f,
|
||||
|
||||
T_ENUM_RTS = 0x30,
|
||||
T_ENUM_BGP_ORIGIN = 0x31,
|
||||
T_ENUM_SCOPE = 0x32,
|
||||
T_ENUM_RTC = 0x33,
|
||||
T_ENUM_RTD = 0x34,
|
||||
T_ENUM_ROA = 0x35,
|
||||
T_ENUM_NETTYPE = 0x36,
|
||||
T_ENUM_RA_PREFERENCE = 0x37,
|
||||
T_ENUM_AF = 0x38,
|
||||
|
||||
/* new enums go here */
|
||||
T_ENUM_EMPTY = 0x3f, /* Special hack for atomic_aggr */
|
||||
|
||||
#define T_ENUM T_ENUM_LO ... T_ENUM_HI
|
||||
|
||||
/* Bigger ones */
|
||||
T_IP = 0x20,
|
||||
T_NET = 0x21,
|
||||
T_STRING = 0x22,
|
||||
T_PATH_MASK = 0x23, /* mask for BGP path */
|
||||
T_PATH = 0x24, /* BGP path */
|
||||
T_CLIST = 0x25, /* Community list */
|
||||
T_EC = 0x26, /* Extended community value, u64 */
|
||||
T_ECLIST = 0x27, /* Extended community list */
|
||||
T_LC = 0x28, /* Large community value, lcomm */
|
||||
T_LCLIST = 0x29, /* Large community list */
|
||||
T_RD = 0x2a, /* Route distinguisher for VPN addresses */
|
||||
T_PATH_MASK_ITEM = 0x2b, /* Path mask item for path mask constructors */
|
||||
|
||||
T_SET = 0x80,
|
||||
T_PREFIX_SET = 0x81,
|
||||
} PACKED;
|
||||
|
||||
/* Filter value; size of this affects filter memory consumption */
|
||||
struct f_val {
|
||||
enum f_type type; /* T_* */
|
||||
union {
|
||||
uint i;
|
||||
u64 ec;
|
||||
lcomm lc;
|
||||
ip_addr ip;
|
||||
const net_addr *net;
|
||||
const char *s;
|
||||
const struct f_tree *t;
|
||||
const struct f_trie *ti;
|
||||
const struct adata *ad;
|
||||
const struct f_path_mask *path_mask;
|
||||
struct f_path_mask_item pmi;
|
||||
} val;
|
||||
};
|
||||
|
||||
/* Dynamic attribute definition (eattrs) */
|
||||
struct f_dynamic_attr {
|
||||
u8 type; /* EA type (EAF_*) */
|
||||
u8 bit; /* For bitfield accessors */
|
||||
enum f_type f_type; /* Filter type */
|
||||
uint ea_code; /* EA code */
|
||||
};
|
||||
|
||||
enum f_sa_code {
|
||||
SA_FROM = 1,
|
||||
SA_GW,
|
||||
SA_NET,
|
||||
SA_PROTO,
|
||||
SA_SOURCE,
|
||||
SA_SCOPE,
|
||||
SA_DEST,
|
||||
SA_IFNAME,
|
||||
SA_IFINDEX,
|
||||
SA_WEIGHT,
|
||||
} PACKED;
|
||||
|
||||
/* Static attribute definition (members of struct rta) */
|
||||
struct f_static_attr {
|
||||
enum f_type f_type; /* Filter type */
|
||||
enum f_sa_code sa_code; /* Static attribute id */
|
||||
int readonly:1; /* Don't allow writing */
|
||||
};
|
||||
|
||||
/* Filter l-value type */
|
||||
enum f_lval_type {
|
||||
F_LVAL_VARIABLE,
|
||||
F_LVAL_PREFERENCE,
|
||||
F_LVAL_SA,
|
||||
F_LVAL_EA,
|
||||
};
|
||||
|
||||
/* Filter l-value */
|
||||
struct f_lval {
|
||||
enum f_lval_type type;
|
||||
union {
|
||||
struct symbol *sym;
|
||||
struct f_dynamic_attr da;
|
||||
struct f_static_attr sa;
|
||||
};
|
||||
};
|
||||
|
||||
/* IP prefix range structure */
|
||||
struct f_prefix {
|
||||
net_addr net; /* The matching prefix must match this net */
|
||||
u8 lo, hi; /* And its length must fit between lo and hi */
|
||||
};
|
||||
|
||||
struct f_tree {
|
||||
struct f_tree *left, *right;
|
||||
struct f_val from, to;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct f_trie_node4
|
||||
{
|
||||
ip4_addr addr, mask, accept;
|
||||
uint plen;
|
||||
struct f_trie_node4 *c[2];
|
||||
};
|
||||
|
||||
struct f_trie_node6
|
||||
{
|
||||
ip6_addr addr, mask, accept;
|
||||
uint plen;
|
||||
struct f_trie_node6 *c[2];
|
||||
};
|
||||
|
||||
struct f_trie_node
|
||||
{
|
||||
union {
|
||||
struct f_trie_node4 v4;
|
||||
struct f_trie_node6 v6;
|
||||
};
|
||||
};
|
||||
|
||||
struct f_trie
|
||||
{
|
||||
linpool *lp;
|
||||
u8 zero;
|
||||
s8 ipv4; /* -1 for undefined / empty */
|
||||
u16 data_size; /* Additional data for each trie node */
|
||||
struct f_trie_node root; /* Root trie node */
|
||||
};
|
||||
|
||||
struct f_tree *f_new_tree(void);
|
||||
struct f_tree *build_tree(struct f_tree *);
|
||||
const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
|
||||
int same_tree(const struct f_tree *t0, const struct f_tree *t2);
|
||||
void tree_format(const struct f_tree *t, buffer *buf);
|
||||
void tree_walk(const struct f_tree *t, void (*hook)(const struct f_tree *, void *), void *data);
|
||||
|
||||
struct f_trie *f_new_trie(linpool *lp, uint data_size);
|
||||
void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
|
||||
int trie_match_net(const struct f_trie *t, const net_addr *n);
|
||||
int trie_same(const struct f_trie *t1, const struct f_trie *t2);
|
||||
void trie_format(const struct f_trie *t, buffer *buf);
|
||||
|
||||
#define F_CMP_ERROR 999
|
||||
|
||||
const char *f_type_name(enum f_type t);
|
||||
|
||||
int val_same(const struct f_val *v1, const struct f_val *v2);
|
||||
int val_compare(const struct f_val *v1, const struct f_val *v2);
|
||||
void val_format(const struct f_val *v, buffer *buf);
|
||||
char *val_format_str(struct linpool *lp, const struct f_val *v);
|
||||
const char *val_dump(const struct f_val *v);
|
||||
|
||||
static inline int val_is_ip4(const struct f_val *v)
|
||||
{ return (v->type == T_IP) && ipa_is_ip4(v->val.ip); }
|
||||
int val_in_range(const struct f_val *v1, const struct f_val *v2);
|
||||
|
||||
int clist_set_type(const struct f_tree *set, struct f_val *v);
|
||||
static inline int eclist_set_type(const struct f_tree *set)
|
||||
{ return set->from.type == T_EC; }
|
||||
static inline int lclist_set_type(const struct f_tree *set)
|
||||
{ return set->from.type == T_LC; }
|
||||
|
||||
const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
|
||||
const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
|
||||
const struct adata *lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
|
||||
|
||||
|
||||
/* Special undef value for paths and clists */
|
||||
static inline int
|
||||
undef_value(struct f_val v)
|
||||
{
|
||||
return ((v.type == T_PATH) || (v.type == T_CLIST) ||
|
||||
(v.type == T_ECLIST) || (v.type == T_LCLIST)) &&
|
||||
(v.val.ad == &null_adata);
|
||||
}
|
||||
|
||||
extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist;
|
||||
|
||||
enum filter_return f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres);
|
||||
|
||||
#endif
|
673
filter/decl.m4
Normal file
673
filter/decl.m4
Normal file
@ -0,0 +1,673 @@
|
||||
m4_divert(-1)m4_dnl
|
||||
#
|
||||
# BIRD -- Construction of per-instruction structures
|
||||
#
|
||||
# (c) 2018 Maria Matejka <mq@jmq.cz>
|
||||
#
|
||||
# Can be freely distributed and used under the terms of the GNU GPL.
|
||||
#
|
||||
# THIS IS A M4 MACRO FILE GENERATING 3 FILES ALTOGETHER.
|
||||
# KEEP YOUR HANDS OFF UNLESS YOU KNOW WHAT YOU'RE DOING.
|
||||
# EDITING AND DEBUGGING THIS FILE MAY DAMAGE YOUR BRAIN SERIOUSLY.
|
||||
#
|
||||
# But you're welcome to read and edit and debug if you aren't scared.
|
||||
#
|
||||
# Uncomment the following line to get exhaustive debug output.
|
||||
# m4_debugmode(aceflqtx)
|
||||
#
|
||||
# How it works:
|
||||
# 1) Instruction to code conversion (uses diversions 100..199)
|
||||
# 2) Code wrapping (uses diversions 1..99)
|
||||
# 3) Final preparation (uses diversions 200..299)
|
||||
# 4) Shipout
|
||||
#
|
||||
# See below for detailed description.
|
||||
#
|
||||
#
|
||||
# 1) Instruction to code conversion
|
||||
# The code provided in f-inst.c between consecutive INST() calls
|
||||
# is interleaved for many different places. It is here processed
|
||||
# and split into separate instances where split-by-instruction
|
||||
# happens. These parts are stored in temporary diversions listed:
|
||||
#
|
||||
# 101 content of per-inst struct
|
||||
# 102 constructor arguments
|
||||
# 103 constructor body
|
||||
# 104 dump line item content
|
||||
# (there may be nothing in dump-line content and
|
||||
# it must be handled specially in phase 2)
|
||||
# 105 linearize body
|
||||
# 106 comparator body
|
||||
# 107 struct f_line_item content
|
||||
# 108 interpreter body
|
||||
# 109 iterator body
|
||||
#
|
||||
# Here are macros to allow you to _divert to the right directions.
|
||||
m4_define(FID_STRUCT_IN, `m4_divert(101)')
|
||||
m4_define(FID_NEW_ARGS, `m4_divert(102)')
|
||||
m4_define(FID_NEW_BODY, `m4_divert(103)')
|
||||
m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])')
|
||||
m4_define(FID_LINEARIZE_BODY, `m4_divert(105)')
|
||||
m4_define(FID_SAME_BODY, `m4_divert(106)')
|
||||
m4_define(FID_LINE_IN, `m4_divert(107)')
|
||||
m4_define(FID_INTERPRET_BODY, `m4_divert(108)')
|
||||
m4_define(FID_ITERATE_BODY, `m4_divert(109)')
|
||||
|
||||
# Sometimes you want slightly different code versions in different
|
||||
# outputs.
|
||||
# Use FID_HIC(code for inst-gen.h, code for inst-gen.c, code for inst-interpret.c)
|
||||
# and put it into [[ ]] quotes if it shall contain commas.
|
||||
m4_define(FID_HIC, `m4_ifelse(TARGET, [[H]], [[$1]], TARGET, [[I]], [[$2]], TARGET, [[C]], [[$3]])')
|
||||
|
||||
# In interpreter code, this is quite common.
|
||||
m4_define(FID_INTERPRET_EXEC, `FID_HIC(,[[FID_INTERPRET_BODY()]],[[m4_divert(-1)]])')
|
||||
m4_define(FID_INTERPRET_NEW, `FID_HIC(,[[m4_divert(-1)]],[[FID_INTERPRET_BODY()]])')
|
||||
|
||||
# If the instruction is never converted to constant, the interpret
|
||||
# code is not produced at all for constructor
|
||||
m4_define(NEVER_CONSTANT, `m4_define([[INST_NEVER_CONSTANT]])')
|
||||
m4_define(FID_IFCONST, `m4_ifdef([[INST_NEVER_CONSTANT]],[[$2]],[[$1]])')
|
||||
|
||||
# If the instruction has some attributes (here called members),
|
||||
# these are typically carried with the instruction from constructor
|
||||
# to interpreter. This yields a line of code everywhere on the path.
|
||||
# FID_MEMBER is a macro to help with this task.
|
||||
m4_define(FID_MEMBER, `m4_dnl
|
||||
FID_LINE_IN()m4_dnl
|
||||
$1 $2;
|
||||
FID_STRUCT_IN()m4_dnl
|
||||
$1 $2;
|
||||
FID_NEW_ARGS()m4_dnl
|
||||
, $1 $2
|
||||
FID_NEW_BODY()m4_dnl
|
||||
whati->$2 = $2;
|
||||
FID_LINEARIZE_BODY()m4_dnl
|
||||
item->$2 = whati->$2;
|
||||
m4_ifelse($3,,,[[
|
||||
FID_SAME_BODY()m4_dnl
|
||||
if ($3) return 0;
|
||||
]])
|
||||
m4_ifelse($4,,,[[
|
||||
FID_DUMP_BODY()m4_dnl
|
||||
debug("%s" $4 "\n", INDENT, $5);
|
||||
]])
|
||||
FID_INTERPRET_EXEC()m4_dnl
|
||||
const $1 $2 = whati->$2
|
||||
FID_INTERPRET_BODY')
|
||||
|
||||
# Instruction arguments are needed only until linearization is done.
|
||||
# This puts the arguments into the filter line to be executed before
|
||||
# the instruction itself.
|
||||
#
|
||||
# To achieve this, ARG_ANY must be called before anything writes into
|
||||
# the instruction line as it moves the instruction pointer forward.
|
||||
m4_define(ARG_ANY, `
|
||||
FID_STRUCT_IN()m4_dnl
|
||||
struct f_inst * f$1;
|
||||
FID_NEW_ARGS()m4_dnl
|
||||
, struct f_inst * f$1
|
||||
FID_NEW_BODY()m4_dnl
|
||||
whati->f$1 = f$1;
|
||||
for (const struct f_inst *child = f$1; child; child = child->next) {
|
||||
what->size += child->size;
|
||||
FID_IFCONST([[
|
||||
if (child->fi_code != FI_CONSTANT)
|
||||
constargs = 0;
|
||||
]])
|
||||
}
|
||||
FID_LINEARIZE_BODY
|
||||
pos = linearize(dest, whati->f$1, pos);
|
||||
FID_INTERPRET_BODY()')
|
||||
|
||||
# Some instructions accept variable number of arguments.
|
||||
m4_define(VARARG, `
|
||||
FID_NEW_ARGS()m4_dnl
|
||||
, struct f_inst * fvar
|
||||
FID_STRUCT_IN()m4_dnl
|
||||
struct f_inst * fvar;
|
||||
uint varcount;
|
||||
FID_LINE_IN()m4_dnl
|
||||
uint varcount;
|
||||
FID_NEW_BODY()m4_dnl
|
||||
whati->varcount = 0;
|
||||
whati->fvar = fvar;
|
||||
for (const struct f_inst *child = fvar; child; child = child->next, whati->varcount++) {
|
||||
what->size += child->size;
|
||||
FID_IFCONST([[
|
||||
if (child->fi_code != FI_CONSTANT)
|
||||
constargs = 0;
|
||||
]])
|
||||
}
|
||||
FID_IFCONST([[
|
||||
const struct f_inst **items = NULL;
|
||||
if (constargs && whati->varcount) {
|
||||
items = alloca(whati->varcount * sizeof(struct f_inst *));
|
||||
const struct f_inst *child = fvar;
|
||||
for (uint i=0; child; i++)
|
||||
child = (items[i] = child)->next;
|
||||
}
|
||||
]])
|
||||
FID_LINEARIZE_BODY()m4_dnl
|
||||
pos = linearize(dest, whati->fvar, pos);
|
||||
item->varcount = whati->varcount;
|
||||
FID_DUMP_BODY()m4_dnl
|
||||
debug("%snumber of varargs %u\n", INDENT, item->varcount);
|
||||
FID_SAME_BODY()m4_dnl
|
||||
if (f1->varcount != f2->varcount) return 0;
|
||||
FID_INTERPRET_BODY()
|
||||
FID_HIC(,[[
|
||||
if (fstk->vcnt < whati->varcount) runtime("Stack underflow");
|
||||
fstk->vcnt -= whati->varcount;
|
||||
]],)
|
||||
')
|
||||
|
||||
# Some arguments need to check their type. After that, ARG_ANY is called.
|
||||
m4_define(ARG, `ARG_ANY($1) ARG_TYPE($1,$2)')
|
||||
m4_define(ARG_TYPE, `ARG_TYPE_STATIC($1,$2) ARG_TYPE_DYNAMIC($1,$2)')
|
||||
|
||||
m4_define(ARG_TYPE_STATIC, `
|
||||
FID_NEW_BODY()m4_dnl
|
||||
if (f$1->type && (f$1->type != ($2)) && !f_const_promotion(f$1, ($2)))
|
||||
cf_error("Argument $1 of %s must be of type %s, got type %s",
|
||||
f_instruction_name(what->fi_code), f_type_name($2), f_type_name(f$1->type));
|
||||
FID_INTERPRET_BODY()')
|
||||
|
||||
m4_define(ARG_TYPE_DYNAMIC, `
|
||||
FID_INTERPRET_EXEC()m4_dnl
|
||||
if (v$1.type != ($2))
|
||||
runtime("Argument $1 of %s must be of type %s, got type %s",
|
||||
f_instruction_name(what->fi_code), f_type_name($2), f_type_name(v$1.type));
|
||||
FID_INTERPRET_BODY()')
|
||||
|
||||
m4_define(ARG_SAME_TYPE, `
|
||||
FID_NEW_BODY()m4_dnl
|
||||
if (f$1->type && f$2->type && (f$1->type != f$2->type) &&
|
||||
!f_const_promotion(f$2, f$1->type) && !f_const_promotion(f$1, f$2->type))
|
||||
cf_error("Arguments $1 and $2 of %s must be of the same type", f_instruction_name(what->fi_code));
|
||||
FID_INTERPRET_BODY()')
|
||||
|
||||
# Executing another filter line. This replaces the recursion
|
||||
# that was needed in the former implementation.
|
||||
m4_define(LINEX, `FID_INTERPRET_EXEC()LINEX_($1)FID_INTERPRET_NEW()return $1 FID_INTERPRET_BODY()')
|
||||
m4_define(LINEX_, `do {
|
||||
fstk->estk[fstk->ecnt].pos = 0;
|
||||
fstk->estk[fstk->ecnt].line = $1;
|
||||
fstk->estk[fstk->ecnt].ventry = fstk->vcnt;
|
||||
fstk->estk[fstk->ecnt].vbase = fstk->estk[fstk->ecnt-1].vbase;
|
||||
fstk->estk[fstk->ecnt].emask = 0;
|
||||
fstk->ecnt++;
|
||||
} while (0)')
|
||||
|
||||
m4_define(LINE, `
|
||||
FID_LINE_IN()m4_dnl
|
||||
const struct f_line * fl$1;
|
||||
FID_STRUCT_IN()m4_dnl
|
||||
struct f_inst * f$1;
|
||||
FID_NEW_ARGS()m4_dnl
|
||||
, struct f_inst * f$1
|
||||
FID_NEW_BODY()m4_dnl
|
||||
whati->f$1 = f$1;
|
||||
FID_DUMP_BODY()m4_dnl
|
||||
f_dump_line(item->fl$1, indent + 1);
|
||||
FID_LINEARIZE_BODY()m4_dnl
|
||||
item->fl$1 = f_linearize(whati->f$1);
|
||||
FID_SAME_BODY()m4_dnl
|
||||
if (!f_same(f1->fl$1, f2->fl$1)) return 0;
|
||||
FID_ITERATE_BODY()m4_dnl
|
||||
if (whati->fl$1) BUFFER_PUSH(fit->lines) = whati->fl$1;
|
||||
FID_INTERPRET_EXEC()m4_dnl
|
||||
do { if (whati->fl$1) {
|
||||
LINEX_(whati->fl$1);
|
||||
} } while(0)
|
||||
FID_INTERPRET_NEW()m4_dnl
|
||||
return whati->f$1
|
||||
FID_INTERPRET_BODY()')
|
||||
|
||||
# Some of the instructions have a result. These constructions
|
||||
# state the result and put it to the right place.
|
||||
m4_define(RESULT, `RESULT_TYPE([[$1]]) RESULT_([[$1]],[[$2]],[[$3]])')
|
||||
m4_define(RESULT_, `RESULT_VAL([[ (struct f_val) { .type = $1, .val.$2 = $3 } ]])')
|
||||
m4_define(RESULT_VAL, `FID_HIC(, [[do { res = $1; fstk->vcnt++; } while (0)]],
|
||||
[[return fi_constant(what, $1)]])')
|
||||
m4_define(RESULT_VOID, `RESULT_VAL([[ (struct f_val) { .type = T_VOID } ]])')
|
||||
|
||||
m4_define(ERROR,
|
||||
`m4_errprint(m4___file__:m4___line__: $*
|
||||
)m4_m4exit(1)')
|
||||
|
||||
# This macro specifies result type and makes there are no conflicting definitions
|
||||
m4_define(RESULT_TYPE,
|
||||
`m4_ifdef([[INST_RESULT_TYPE]],
|
||||
[[m4_ifelse(INST_RESULT_TYPE,$1,,[[ERROR([[Multiple type definitons]])]])]],
|
||||
[[m4_define(INST_RESULT_TYPE,$1) RESULT_TYPE_($1)]])')
|
||||
|
||||
m4_define(RESULT_TYPE_, `
|
||||
FID_NEW_BODY()m4_dnl
|
||||
what->type = $1;
|
||||
FID_INTERPRET_BODY()')
|
||||
|
||||
# Some common filter instruction members
|
||||
m4_define(SYMBOL, `FID_MEMBER(struct symbol *, sym, [[strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)]], "symbol %s", item->sym->name)')
|
||||
m4_define(RTC, `FID_MEMBER(struct rtable_config *, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], "route table %s", item->rtc->name)')
|
||||
m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, f1->sa.sa_code != f2->sa.sa_code,,)')
|
||||
m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, f1->da.ea_code != f2->da.ea_code,,)')
|
||||
m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access"); } while (0)]],NEVER_CONSTANT())')
|
||||
|
||||
# 2) Code wrapping
|
||||
# The code produced in 1xx temporary diversions is a raw code without
|
||||
# any auxiliary commands and syntactical structures around. When the
|
||||
# instruction is done, INST_FLUSH is called. More precisely, it is called
|
||||
# at the beginning of INST() call and at the end of file.
|
||||
#
|
||||
# INST_FLUSH picks all the temporary diversions, wraps their content
|
||||
# into appropriate headers and structures and saves them into global
|
||||
# diversions listed:
|
||||
#
|
||||
# 4 enum fi_code
|
||||
# 5 enum fi_code to string
|
||||
# 6 dump line item
|
||||
# 7 dump line item callers
|
||||
# 8 linearize
|
||||
# 9 same (filter comparator)
|
||||
# 10 iterate
|
||||
# 1 union in struct f_inst
|
||||
# 3 constructors + interpreter
|
||||
#
|
||||
# These global diversions contain blocks of code that can be directly
|
||||
# put into the final file, yet it still can't be written out now as
|
||||
# every instruction writes to all of these diversions.
|
||||
|
||||
# Code wrapping diversion names. Here we want an explicit newline
|
||||
# after the C comment.
|
||||
m4_define(FID_ZONE, `m4_divert($1) /* $2 for INST_NAME() */
|
||||
')
|
||||
m4_define(FID_INST, `FID_ZONE(1, Instruction structure for config)')
|
||||
m4_define(FID_LINE, `FID_ZONE(2, Instruction structure for interpreter)')
|
||||
m4_define(FID_NEW, `FID_ZONE(3, Constructor)')
|
||||
m4_define(FID_ENUM, `FID_ZONE(4, Code enum)')
|
||||
m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
|
||||
m4_define(FID_DUMP, `FID_ZONE(6, Dump line)')
|
||||
m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)')
|
||||
m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)')
|
||||
m4_define(FID_SAME, `FID_ZONE(9, Comparison)')
|
||||
m4_define(FID_ITERATE, `FID_ZONE(10, Iteration)')
|
||||
|
||||
# This macro does all the code wrapping. See inline comments.
|
||||
m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
|
||||
FID_ENUM()m4_dnl Contents of enum fi_code { ... }
|
||||
INST_NAME(),
|
||||
FID_ENUM_STR()m4_dnl Contents of const char * indexed by enum fi_code
|
||||
[INST_NAME()] = "INST_NAME()",
|
||||
FID_INST()m4_dnl Anonymous structure inside struct f_inst
|
||||
struct {
|
||||
m4_undivert(101)m4_dnl
|
||||
} i_[[]]INST_NAME();
|
||||
FID_LINE()m4_dnl Anonymous structure inside struct f_line_item
|
||||
struct {
|
||||
m4_undivert(107)m4_dnl
|
||||
} i_[[]]INST_NAME();
|
||||
FID_NEW()m4_dnl Constructor and interpreter code together
|
||||
FID_HIC(
|
||||
[[m4_dnl Public declaration of constructor in H file
|
||||
struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
|
||||
m4_undivert(102)m4_dnl
|
||||
);]],
|
||||
[[m4_dnl The one case in The Big Switch inside interpreter
|
||||
case INST_NAME():
|
||||
#define whati (&(what->i_]]INST_NAME()[[))
|
||||
m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (fstk->vcnt < INST_INVAL()) runtime("Stack underflow"); fstk->vcnt -= INST_INVAL(); ]])
|
||||
m4_undivert(108)m4_dnl
|
||||
#undef whati
|
||||
break;
|
||||
]],
|
||||
[[m4_dnl Constructor itself
|
||||
struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
|
||||
m4_undivert(102)m4_dnl
|
||||
)
|
||||
{
|
||||
/* Allocate the structure */
|
||||
struct f_inst *what = fi_new(fi_code);
|
||||
FID_IFCONST([[uint constargs = 1;]])
|
||||
|
||||
/* Initialize all the members */
|
||||
#define whati (&(what->i_]]INST_NAME()[[))
|
||||
m4_undivert(103)m4_dnl
|
||||
|
||||
/* If not constant, return the instruction itself */
|
||||
FID_IFCONST([[if (!constargs)]])
|
||||
return what;
|
||||
|
||||
/* Try to pre-calculate the result */
|
||||
FID_IFCONST([[m4_undivert(108)]])m4_dnl
|
||||
#undef whati
|
||||
}
|
||||
]])
|
||||
|
||||
FID_DUMP_CALLER()m4_dnl Case in another big switch used in instruction dumping (debug)
|
||||
case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break;
|
||||
|
||||
FID_DUMP()m4_dnl The dumper itself
|
||||
m4_ifdef([[FID_DUMP_BODY_EXISTS]],
|
||||
[[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item_, const int indent)]],
|
||||
[[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item UNUSED, const int indent UNUSED)]])
|
||||
m4_undefine([[FID_DUMP_BODY_EXISTS]])
|
||||
{
|
||||
#define item (&(item_->i_]]INST_NAME()[[))
|
||||
m4_undivert(104)m4_dnl
|
||||
#undef item
|
||||
}
|
||||
|
||||
FID_LINEARIZE()m4_dnl The linearizer
|
||||
case INST_NAME(): {
|
||||
#define whati (&(what->i_]]INST_NAME()[[))
|
||||
#define item (&(dest->items[pos].i_]]INST_NAME()[[))
|
||||
m4_undivert(105)m4_dnl
|
||||
#undef whati
|
||||
#undef item
|
||||
dest->items[pos].fi_code = what->fi_code;
|
||||
dest->items[pos].lineno = what->lineno;
|
||||
break;
|
||||
}
|
||||
|
||||
FID_SAME()m4_dnl This code compares two f_line"s while reconfiguring
|
||||
case INST_NAME():
|
||||
#define f1 (&(f1_->i_]]INST_NAME()[[))
|
||||
#define f2 (&(f2_->i_]]INST_NAME()[[))
|
||||
m4_undivert(106)m4_dnl
|
||||
#undef f1
|
||||
#undef f2
|
||||
break;
|
||||
|
||||
FID_ITERATE()m4_dnl The iterator
|
||||
case INST_NAME():
|
||||
#define whati (&(what->i_]]INST_NAME()[[))
|
||||
m4_undivert(109)m4_dnl
|
||||
#undef whati
|
||||
break;
|
||||
|
||||
m4_divert(-1)FID_FLUSH(101,200)m4_dnl And finally this flushes all the unused diversions
|
||||
]])')
|
||||
|
||||
m4_define(INST, `m4_dnl This macro is called on beginning of each instruction.
|
||||
INST_FLUSH()m4_dnl First, old data is flushed
|
||||
m4_define([[INST_NAME]], [[$1]])m4_dnl Then we store instruction name,
|
||||
m4_define([[INST_INVAL]], [[$2]])m4_dnl instruction input value count,
|
||||
m4_undefine([[INST_NEVER_CONSTANT]])m4_dnl reset NEVER_CONSTANT trigger,
|
||||
m4_undefine([[INST_RESULT_TYPE]])m4_dnl and reset RESULT_TYPE value.
|
||||
FID_INTERPRET_BODY()m4_dnl By default, every code is interpreter code.
|
||||
')
|
||||
|
||||
# 3) Final preparation
|
||||
#
|
||||
# Now we prepare all the code around the global diversions.
|
||||
# It must be here, not in m4wrap, as we want M4 to mark the code
|
||||
# by #line directives correctly, not to claim that every single line
|
||||
# is at the beginning of the m4wrap directive.
|
||||
#
|
||||
# This part is split by the final file.
|
||||
# H for inst-gen.h
|
||||
# I for inst-interpret.c
|
||||
# C for inst-gen.c
|
||||
#
|
||||
# So we in cycle:
|
||||
# A. open a diversion
|
||||
# B. send there some code
|
||||
# C. close that diversion
|
||||
# D. flush a global diversion
|
||||
# E. open another diversion and goto B.
|
||||
#
|
||||
# Final diversions
|
||||
# 200+ completed text before it is flushed to output
|
||||
|
||||
# This is a list of output diversions
|
||||
m4_define(FID_WR_PUT_LIST)
|
||||
|
||||
# This macro does the steps C to E, see before.
|
||||
m4_define(FID_WR_PUT_ALSO, `m4_define([[FID_WR_PUT_LIST]],FID_WR_PUT_LIST()[[FID_WR_DPUT(]]FID_WR_DIDX[[)FID_WR_DPUT(]]$1[[)]])m4_define([[FID_WR_DIDX]],m4_eval(FID_WR_DIDX+1))m4_divert(FID_WR_DIDX)')
|
||||
|
||||
# These macros do the splitting between H/I/C
|
||||
m4_define(FID_WR_DIRECT, `m4_ifelse(TARGET,[[$1]],[[FID_WR_INIT()]],[[FID_WR_STOP()]])')
|
||||
m4_define(FID_WR_INIT, `m4_define([[FID_WR_DIDX]],200)m4_define([[FID_WR_PUT]],[[FID_WR_PUT_ALSO($]][[@)]])m4_divert(200)')
|
||||
m4_define(FID_WR_STOP, `m4_define([[FID_WR_PUT]])m4_divert(-1)')
|
||||
|
||||
# Here is the direct code to be put into the output files
|
||||
# together with the undiversions, being hidden under FID_WR_PUT()
|
||||
|
||||
m4_changequote([[,]])
|
||||
FID_WR_DIRECT(I)
|
||||
FID_WR_PUT(3)
|
||||
FID_WR_DIRECT(C)
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 6
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmisleading-indentation"
|
||||
#endif
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/f-inst.h"
|
||||
|
||||
/* Instruction codes to string */
|
||||
static const char * const f_instruction_name_str[] = {
|
||||
FID_WR_PUT(5)
|
||||
};
|
||||
|
||||
const char *
|
||||
f_instruction_name_(enum f_instruction_code fi)
|
||||
{
|
||||
if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
|
||||
return f_instruction_name_str[fi];
|
||||
else
|
||||
bug("Got unknown instruction code: %d", fi);
|
||||
}
|
||||
|
||||
static inline struct f_inst *
|
||||
fi_new(enum f_instruction_code fi_code)
|
||||
{
|
||||
struct f_inst *what = cfg_allocz(sizeof(struct f_inst));
|
||||
what->lineno = ifs->lino;
|
||||
what->size = 1;
|
||||
what->fi_code = fi_code;
|
||||
return what;
|
||||
}
|
||||
|
||||
static inline struct f_inst *
|
||||
fi_constant(struct f_inst *what, struct f_val val)
|
||||
{
|
||||
what->fi_code = FI_CONSTANT;
|
||||
what->i_FI_CONSTANT.val = val;
|
||||
return what;
|
||||
}
|
||||
|
||||
static int
|
||||
f_const_promotion(struct f_inst *arg, enum f_type want)
|
||||
{
|
||||
if (arg->fi_code != FI_CONSTANT)
|
||||
return 0;
|
||||
|
||||
struct f_val *c = &arg->i_FI_CONSTANT.val;
|
||||
|
||||
if ((c->type == T_IP) && ipa_is_ip4(c->val.ip) && (want == T_QUAD)) {
|
||||
*c = (struct f_val) {
|
||||
.type = T_QUAD,
|
||||
.val.i = ipa_to_u32(c->val.ip),
|
||||
};
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define v1 whati->f1->i_FI_CONSTANT.val
|
||||
#define v2 whati->f2->i_FI_CONSTANT.val
|
||||
#define v3 whati->f3->i_FI_CONSTANT.val
|
||||
#define vv(i) items[i]->i_FI_CONSTANT.val
|
||||
#define runtime(fmt, ...) cf_error("filter preevaluation, line %d: " fmt, ifs->lino, ##__VA_ARGS__)
|
||||
#define fpool cfg_mem
|
||||
#define falloc(size) cfg_alloc(size)
|
||||
/* Instruction constructors */
|
||||
FID_WR_PUT(3)
|
||||
#undef v1
|
||||
#undef v2
|
||||
#undef v3
|
||||
#undef vv
|
||||
|
||||
/* Line dumpers */
|
||||
#define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1)
|
||||
static const char f_dump_line_indent_str[] = " ";
|
||||
|
||||
FID_WR_PUT(6)
|
||||
|
||||
void f_dump_line(const struct f_line *dest, uint indent)
|
||||
{
|
||||
if (!dest) {
|
||||
debug("%sNo filter line (NULL)\n", INDENT);
|
||||
return;
|
||||
}
|
||||
debug("%sFilter line %p (len=%u)\n", INDENT, dest, dest->len);
|
||||
for (uint i=0; i<dest->len; i++) {
|
||||
const struct f_line_item *item = &dest->items[i];
|
||||
debug("%sInstruction %s at line %u\n", INDENT, f_instruction_name_(item->fi_code), item->lineno);
|
||||
switch (item->fi_code) {
|
||||
FID_WR_PUT(7)
|
||||
default: bug("Unknown instruction %x in f_dump_line", item->fi_code);
|
||||
}
|
||||
}
|
||||
debug("%sFilter line %p dump done\n", INDENT, dest);
|
||||
}
|
||||
|
||||
/* Linearize */
|
||||
static uint
|
||||
linearize(struct f_line *dest, const struct f_inst *what, uint pos)
|
||||
{
|
||||
for ( ; what; what = what->next) {
|
||||
switch (what->fi_code) {
|
||||
FID_WR_PUT(8)
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
struct f_line *
|
||||
f_linearize_concat(const struct f_inst * const inst[], uint count)
|
||||
{
|
||||
uint len = 0;
|
||||
for (uint i=0; i<count; i++)
|
||||
for (const struct f_inst *what = inst[i]; what; what = what->next)
|
||||
len += what->size;
|
||||
|
||||
struct f_line *out = cfg_allocz(sizeof(struct f_line) + sizeof(struct f_line_item)*len);
|
||||
|
||||
for (uint i=0; i<count; i++)
|
||||
out->len = linearize(out, inst[i], out->len);
|
||||
|
||||
#ifdef LOCAL_DEBUG
|
||||
f_dump_line(out, 0);
|
||||
#endif
|
||||
return out;
|
||||
}
|
||||
|
||||
/* Filter line comparison */
|
||||
int
|
||||
f_same(const struct f_line *fl1, const struct f_line *fl2)
|
||||
{
|
||||
if ((!fl1) && (!fl2))
|
||||
return 1;
|
||||
if ((!fl1) || (!fl2))
|
||||
return 0;
|
||||
if (fl1->len != fl2->len)
|
||||
return 0;
|
||||
for (uint i=0; i<fl1->len; i++) {
|
||||
#define f1_ (&(fl1->items[i]))
|
||||
#define f2_ (&(fl2->items[i]))
|
||||
if (f1_->fi_code != f2_->fi_code)
|
||||
return 0;
|
||||
if (f1_->flags != f2_->flags)
|
||||
return 0;
|
||||
|
||||
switch(f1_->fi_code) {
|
||||
FID_WR_PUT(9)
|
||||
}
|
||||
}
|
||||
#undef f1_
|
||||
#undef f2_
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* Part of FI_SWITCH filter iterator */
|
||||
static void
|
||||
f_add_tree_lines(const struct f_tree *t, void *fit_)
|
||||
{
|
||||
struct filter_iterator * fit = fit_;
|
||||
|
||||
if (t->data)
|
||||
BUFFER_PUSH(fit->lines) = t->data;
|
||||
}
|
||||
|
||||
/* Filter line iterator */
|
||||
void
|
||||
f_add_lines(const struct f_line_item *what, struct filter_iterator *fit)
|
||||
{
|
||||
switch(what->fi_code) {
|
||||
FID_WR_PUT(10)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 6
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
FID_WR_DIRECT(H)
|
||||
/* Filter instruction codes */
|
||||
enum f_instruction_code {
|
||||
FID_WR_PUT(4)m4_dnl
|
||||
} PACKED;
|
||||
|
||||
/* Filter instruction structure for config */
|
||||
struct f_inst {
|
||||
struct f_inst *next; /* Next instruction */
|
||||
enum f_instruction_code fi_code; /* Instruction code */
|
||||
enum f_type type; /* Type of returned value, if known */
|
||||
int size; /* How many instructions are underneath */
|
||||
int lineno; /* Line number */
|
||||
union {
|
||||
FID_WR_PUT(1)m4_dnl
|
||||
};
|
||||
};
|
||||
|
||||
/* Filter line item */
|
||||
struct f_line_item {
|
||||
enum f_instruction_code fi_code; /* What to do */
|
||||
enum f_instruction_flags flags; /* Flags, instruction-specific */
|
||||
uint lineno; /* Where */
|
||||
union {
|
||||
FID_WR_PUT(2)m4_dnl
|
||||
};
|
||||
};
|
||||
|
||||
/* Instruction constructors */
|
||||
FID_WR_PUT(3)
|
||||
m4_divert(-1)
|
||||
|
||||
# 4) Shipout
|
||||
#
|
||||
# Everything is prepared in FID_WR_PUT_LIST now. Let's go!
|
||||
|
||||
m4_changequote(`,')
|
||||
|
||||
# Flusher auxiliary macro
|
||||
m4_define(FID_FLUSH, `m4_ifelse($1,$2,,[[m4_undivert($1)FID_FLUSH(m4_eval($1+1),$2)]])')
|
||||
|
||||
# Defining the macro used in FID_WR_PUT_LIST
|
||||
m4_define(FID_WR_DPUT, `m4_undivert($1)')
|
||||
|
||||
# After the code is read and parsed, we:
|
||||
m4_m4wrap(`INST_FLUSH()m4_divert(0)FID_WR_PUT_LIST()m4_divert(-1)FID_FLUSH(1,200)')
|
||||
|
||||
m4_changequote([[,]])
|
||||
# And now M4 is going to parse f-inst.c, fill the diversions
|
||||
# and after the file is done, the content of m4_m4wrap (see before)
|
||||
# is executed.
|
1269
filter/f-inst.c
Normal file
1269
filter/f-inst.c
Normal file
File diff suppressed because it is too large
Load Diff
112
filter/f-inst.h
Normal file
112
filter/f-inst.h
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* BIRD Internet Routing Daemon -- Filter instructions
|
||||
*
|
||||
* (c) 1999 Pavel Machek <pavel@ucw.cz>
|
||||
* (c) 2018--2019 Maria Matejka <mq@jmq.cz>
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*
|
||||
* Filter interpreter data structures and internal API.
|
||||
* See filter/f-inst.c for documentation.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_F_INST_H_
|
||||
#define _BIRD_F_INST_H_
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
#include "lib/buffer.h"
|
||||
#include "lib/flowspec.h"
|
||||
|
||||
/* Flags for instructions */
|
||||
enum f_instruction_flags {
|
||||
FIF_PRINTED = 1, /* FI_PRINT_AND_DIE: message put in buffer */
|
||||
} PACKED;
|
||||
|
||||
/* Include generated filter instruction declarations */
|
||||
#include "filter/inst-gen.h"
|
||||
|
||||
#define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
/* Convert the instruction back to the enum name */
|
||||
const char *f_instruction_name_(enum f_instruction_code fi);
|
||||
static inline const char *f_instruction_name(enum f_instruction_code fi)
|
||||
{ return f_instruction_name_(fi) + 3; }
|
||||
|
||||
/* Filter structures for execution */
|
||||
/* Line of instructions to be unconditionally executed one after another */
|
||||
struct f_line {
|
||||
uint len; /* Line length */
|
||||
u8 args; /* Function: Args required */
|
||||
u8 vars;
|
||||
struct f_line_item items[0]; /* The items themselves */
|
||||
};
|
||||
|
||||
/* Convert the f_inst infix tree to the f_line structures */
|
||||
struct f_line *f_linearize_concat(const struct f_inst * const inst[], uint count);
|
||||
static inline struct f_line *f_linearize(const struct f_inst *root)
|
||||
{ return f_linearize_concat(&root, 1); }
|
||||
|
||||
void f_dump_line(const struct f_line *, uint indent);
|
||||
|
||||
|
||||
/* Recursive iteration over filter instructions */
|
||||
|
||||
struct filter_iterator {
|
||||
BUFFER_(const struct f_line *) lines;
|
||||
};
|
||||
|
||||
void f_add_lines(const struct f_line_item *what, struct filter_iterator *fit);
|
||||
|
||||
#define FILTER_ITERATE_INIT(fit, filter, pool) \
|
||||
({ \
|
||||
BUFFER_INIT((fit)->lines, (pool), 32); \
|
||||
BUFFER_PUSH((fit)->lines) = (filter)->root; \
|
||||
})
|
||||
|
||||
#define FILTER_ITERATE(fit, fi) ({ \
|
||||
const struct f_line *fl_; \
|
||||
while (!BUFFER_EMPTY((fit)->lines)) \
|
||||
{ \
|
||||
BUFFER_POP((fit)->lines); \
|
||||
fl_ = (fit)->lines.data[(fit)->lines.used]; \
|
||||
for (uint i_ = 0; i_ < fl_->len; i_++) \
|
||||
{ \
|
||||
const struct f_line_item *fi = &fl_->items[i_]; \
|
||||
f_add_lines(fi, (fit));
|
||||
|
||||
#define FILTER_ITERATE_END } } })
|
||||
|
||||
#define FILTER_ITERATE_CLEANUP(fit) \
|
||||
({ \
|
||||
mb_free((fit)->lines.data); \
|
||||
memset((fit), 0, sizeof(struct filter_iterator)); \
|
||||
})
|
||||
|
||||
|
||||
struct filter *f_new_where(struct f_inst *);
|
||||
static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
|
||||
{ return (struct f_dynamic_attr) { .type = type, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
|
||||
static inline struct f_dynamic_attr f_new_dynamic_attr_bit(u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
|
||||
{ return (struct f_dynamic_attr) { .type = EAF_TYPE_BITFIELD, .bit = bit, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
|
||||
static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
|
||||
{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
|
||||
struct f_inst *f_generate_complex(enum f_instruction_code fi_code, struct f_dynamic_attr da, struct f_inst *argument);
|
||||
struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
|
||||
|
||||
/* Hook for call bt_assert() function in configuration */
|
||||
extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
|
||||
|
||||
/* Bird Tests */
|
||||
struct f_bt_test_suite {
|
||||
node n; /* Node in config->tests */
|
||||
const struct f_line *fn; /* Root of function */
|
||||
const struct f_line *cmp; /* Compare to this function */
|
||||
const char *fn_name; /* Name of test */
|
||||
const char *dsc; /* Description */
|
||||
int result; /* Desired result */
|
||||
};
|
||||
|
||||
#endif
|
229
filter/f-util.c
229
filter/f-util.c
@ -10,83 +10,174 @@
|
||||
#include "nest/bird.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/f-inst.h"
|
||||
#include "lib/idm.h"
|
||||
#include "nest/protocol.h"
|
||||
#include "nest/route.h"
|
||||
|
||||
#define P(a,b) ((a<<8) | b)
|
||||
|
||||
struct f_inst *
|
||||
f_new_inst(enum f_instruction_code fi_code)
|
||||
{
|
||||
struct f_inst * ret;
|
||||
ret = cfg_allocz(sizeof(struct f_inst));
|
||||
ret->fi_code = fi_code;
|
||||
ret->lineno = ifs->lino;
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct f_inst *
|
||||
f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da)
|
||||
{
|
||||
struct f_inst *ret = f_new_inst(fi_code);
|
||||
ret->aux = da.type;
|
||||
ret->a2.i = da.ea_code;
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct f_inst *
|
||||
f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa)
|
||||
{
|
||||
struct f_inst *ret = f_new_inst(fi_code);
|
||||
ret->aux = sa.f_type;
|
||||
ret->a2.i = sa.sa_code;
|
||||
ret->a1.i = sa.readonly;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate set_dynamic( operation( get_dynamic(), argument ) )
|
||||
*/
|
||||
struct f_inst *
|
||||
f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument)
|
||||
{
|
||||
struct f_inst *set_dyn = f_new_inst_da(FI_EA_SET, da),
|
||||
*oper = f_new_inst(operation),
|
||||
*get_dyn = f_new_inst_da(FI_EA_GET, da);
|
||||
|
||||
oper->aux = operation_aux;
|
||||
oper->a1.p = get_dyn;
|
||||
oper->a2.p = argument;
|
||||
|
||||
set_dyn->a1.p = oper;
|
||||
return set_dyn;
|
||||
}
|
||||
|
||||
|
||||
struct f_inst *
|
||||
f_generate_roa_check(struct symbol *sym, struct f_inst *prefix, struct f_inst *asn)
|
||||
{
|
||||
struct f_inst_roa_check *ret = cfg_allocz(sizeof(struct f_inst_roa_check));
|
||||
ret->i.fi_code = FI_ROA_CHECK;
|
||||
ret->i.lineno = ifs->lino;
|
||||
ret->i.arg1 = prefix;
|
||||
ret->i.arg2 = asn;
|
||||
/* prefix == NULL <-> asn == NULL */
|
||||
|
||||
if ((sym->class != SYM_ROA) || ! sym->def)
|
||||
cf_error("%s is not a ROA table", sym->name);
|
||||
ret->rtc = sym->def;
|
||||
|
||||
return &ret->i;
|
||||
}
|
||||
|
||||
char *
|
||||
filter_name(struct filter *filter)
|
||||
const char *
|
||||
filter_name(const struct filter *filter)
|
||||
{
|
||||
if (!filter)
|
||||
return "ACCEPT";
|
||||
else if (filter == FILTER_REJECT)
|
||||
return "REJECT";
|
||||
else if (!filter->name)
|
||||
else if (!filter->sym)
|
||||
return "(unnamed)";
|
||||
else
|
||||
return filter->name;
|
||||
return filter->sym->name;
|
||||
}
|
||||
|
||||
struct filter *f_new_where(struct f_inst *where)
|
||||
{
|
||||
struct f_inst *cond = f_new_inst(FI_CONDITION, where,
|
||||
f_new_inst(FI_DIE, F_ACCEPT),
|
||||
f_new_inst(FI_DIE, F_REJECT));
|
||||
|
||||
struct filter *f = cfg_allocz(sizeof(struct filter));
|
||||
f->root = f_linearize(cond);
|
||||
return f;
|
||||
}
|
||||
|
||||
#define CA_KEY(n) n->name, n->fda.type
|
||||
#define CA_NEXT(n) n->next
|
||||
#define CA_EQ(na,ta,nb,tb) (!strcmp(na,nb) && (ta == tb))
|
||||
#define CA_FN(n,t) (mem_hash(n, strlen(n)) ^ (t*0xaae99453U))
|
||||
#define CA_ORDER 8 /* Fixed */
|
||||
|
||||
struct ca_storage {
|
||||
struct ca_storage *next;
|
||||
struct f_dynamic_attr fda;
|
||||
u32 uc;
|
||||
char name[0];
|
||||
};
|
||||
|
||||
HASH(struct ca_storage) ca_hash;
|
||||
|
||||
static struct idm ca_idm;
|
||||
static struct ca_storage **ca_storage;
|
||||
static uint ca_storage_max;
|
||||
|
||||
static void
|
||||
ca_free(resource *r)
|
||||
{
|
||||
struct custom_attribute *ca = (void *) r;
|
||||
struct ca_storage *cas = HASH_FIND(ca_hash, CA, ca->name, ca->fda->type);
|
||||
ASSERT(cas);
|
||||
|
||||
ca->name = NULL;
|
||||
ca->fda = NULL;
|
||||
if (!--cas->uc) {
|
||||
uint id = EA_CUSTOM_ID(cas->fda.ea_code);
|
||||
idm_free(&ca_idm, id);
|
||||
HASH_REMOVE(ca_hash, CA, cas);
|
||||
ca_storage[id] = NULL;
|
||||
mb_free(cas);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
ca_dump(resource *r)
|
||||
{
|
||||
struct custom_attribute *ca = (void *) r;
|
||||
debug("name \"%s\" id 0x%04x ea_type 0x%02x f_type 0x%02x\n",
|
||||
ca->name, ca->fda->ea_code, ca->fda->type, ca->fda->f_type);
|
||||
}
|
||||
|
||||
static struct resclass ca_class = {
|
||||
.name = "Custom attribute",
|
||||
.size = sizeof(struct custom_attribute),
|
||||
.free = ca_free,
|
||||
.dump = ca_dump,
|
||||
.lookup = NULL,
|
||||
.memsize = NULL,
|
||||
};
|
||||
|
||||
struct custom_attribute *
|
||||
ca_lookup(pool *p, const char *name, int f_type)
|
||||
{
|
||||
int ea_type;
|
||||
|
||||
switch (f_type) {
|
||||
case T_INT:
|
||||
ea_type = EAF_TYPE_INT;
|
||||
break;
|
||||
case T_IP:
|
||||
ea_type = EAF_TYPE_IP_ADDRESS;
|
||||
break;
|
||||
case T_QUAD:
|
||||
ea_type = EAF_TYPE_ROUTER_ID;
|
||||
break;
|
||||
case T_PATH:
|
||||
ea_type = EAF_TYPE_AS_PATH;
|
||||
break;
|
||||
case T_CLIST:
|
||||
ea_type = EAF_TYPE_INT_SET;
|
||||
break;
|
||||
case T_ECLIST:
|
||||
ea_type = EAF_TYPE_EC_SET;
|
||||
break;
|
||||
case T_LCLIST:
|
||||
ea_type = EAF_TYPE_LC_SET;
|
||||
break;
|
||||
default:
|
||||
cf_error("Custom route attribute of unsupported type");
|
||||
}
|
||||
|
||||
static int inited = 0;
|
||||
if (!inited) {
|
||||
idm_init(&ca_idm, &root_pool, 8);
|
||||
HASH_INIT(ca_hash, &root_pool, CA_ORDER);
|
||||
|
||||
ca_storage_max = 256;
|
||||
ca_storage = mb_allocz(&root_pool, sizeof(struct ca_storage *) * ca_storage_max);
|
||||
|
||||
inited++;
|
||||
}
|
||||
|
||||
struct ca_storage *cas = HASH_FIND(ca_hash, CA, name, ea_type);
|
||||
if (cas) {
|
||||
cas->uc++;
|
||||
} else {
|
||||
|
||||
uint id = idm_alloc(&ca_idm);
|
||||
|
||||
if (id >= EA_CUSTOM_BIT)
|
||||
cf_error("Too many custom attributes.");
|
||||
|
||||
if (id >= ca_storage_max) {
|
||||
ca_storage_max *= 2;
|
||||
ca_storage = mb_realloc(ca_storage, sizeof(struct ca_storage *) * ca_storage_max * 2);
|
||||
}
|
||||
|
||||
cas = mb_allocz(&root_pool, sizeof(struct ca_storage) + strlen(name) + 1);
|
||||
cas->fda = f_new_dynamic_attr(ea_type, f_type, EA_CUSTOM(id));
|
||||
cas->uc = 1;
|
||||
|
||||
strcpy(cas->name, name);
|
||||
ca_storage[id] = cas;
|
||||
|
||||
HASH_INSERT(ca_hash, CA, cas);
|
||||
}
|
||||
|
||||
struct custom_attribute *ca = ralloc(p, &ca_class);
|
||||
ca->fda = &(cas->fda);
|
||||
ca->name = cas->name;
|
||||
return ca;
|
||||
}
|
||||
|
||||
const char *
|
||||
ea_custom_name(uint ea)
|
||||
{
|
||||
uint id = EA_CUSTOM_ID(ea);
|
||||
if (id >= ca_storage_max)
|
||||
return NULL;
|
||||
|
||||
if (!ca_storage[id])
|
||||
return NULL;
|
||||
|
||||
return ca_storage[id]->name;
|
||||
}
|
||||
|
||||
|
1976
filter/filter.c
1976
filter/filter.c
File diff suppressed because it is too large
Load Diff
320
filter/filter.h
320
filter/filter.h
@ -2,6 +2,7 @@
|
||||
* BIRD Internet Routing Daemon -- Filters
|
||||
*
|
||||
* (c) 1999 Pavel Machek <pavel@ucw.cz>
|
||||
* (c) 2018--2019 Maria Matejka <mq@jmq.cz>
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
@ -11,296 +12,71 @@
|
||||
|
||||
#include "lib/resource.h"
|
||||
#include "lib/ip.h"
|
||||
#include "lib/macro.h"
|
||||
#include "nest/route.h"
|
||||
#include "nest/attrs.h"
|
||||
|
||||
/* Filter instruction types */
|
||||
|
||||
#define FI__TWOCHAR(a,b) ((a<<8) | b)
|
||||
#define FI__LIST \
|
||||
F(FI_ADD, 0, '+') \
|
||||
F(FI_SUBTRACT, 0, '-') \
|
||||
F(FI_MULTIPLY, 0, '*') \
|
||||
F(FI_DIVIDE, 0, '/') \
|
||||
F(FI_AND, 0, '&') \
|
||||
F(FI_OR, 0, '|') \
|
||||
F(FI_PAIR_CONSTRUCT, 'm', 'p') \
|
||||
F(FI_EC_CONSTRUCT, 'm', 'c') \
|
||||
F(FI_LC_CONSTRUCT, 'm', 'l') \
|
||||
F(FI_PATHMASK_CONSTRUCT, 'm', 'P') \
|
||||
F(FI_NEQ, '!', '=') \
|
||||
F(FI_EQ, '=', '=') \
|
||||
F(FI_LT, 0, '<') \
|
||||
F(FI_LTE, '<', '=') \
|
||||
F(FI_NOT, 0, '!') \
|
||||
F(FI_MATCH, 0, '~') \
|
||||
F(FI_NOT_MATCH, '!', '~') \
|
||||
F(FI_DEFINED, 'd', 'e') \
|
||||
F(FI_SET, 0, 's') \
|
||||
F(FI_CONSTANT, 0, 'c') \
|
||||
F(FI_VARIABLE, 0, 'V') \
|
||||
F(FI_CONSTANT_INDIRECT, 0, 'C') \
|
||||
F(FI_PRINT, 0, 'p') \
|
||||
F(FI_CONDITION, 0, '?') \
|
||||
F(FI_NOP, 0, '0') \
|
||||
F(FI_PRINT_AND_DIE, 'p', ',') \
|
||||
F(FI_RTA_GET, 0, 'a') \
|
||||
F(FI_RTA_SET, 'a', 'S') \
|
||||
F(FI_EA_GET, 'e', 'a') \
|
||||
F(FI_EA_SET, 'e', 'S') \
|
||||
F(FI_PREF_GET, 0, 'P') \
|
||||
F(FI_PREF_SET, 'P', 'S') \
|
||||
F(FI_LENGTH, 0, 'L') \
|
||||
F(FI_IP, 'c', 'p') \
|
||||
F(FI_AS_PATH_FIRST, 'a', 'f') \
|
||||
F(FI_AS_PATH_LAST, 'a', 'l') \
|
||||
F(FI_AS_PATH_LAST_NAG, 'a', 'L') \
|
||||
F(FI_RETURN, 0, 'r') \
|
||||
F(FI_CALL, 'c', 'a') \
|
||||
F(FI_CLEAR_LOCAL_VARS, 'c', 'V') \
|
||||
F(FI_SWITCH, 'S', 'W') \
|
||||
F(FI_IP_MASK, 'i', 'M') \
|
||||
F(FI_EMPTY, 0, 'E') \
|
||||
F(FI_PATH_PREPEND, 'A', 'p') \
|
||||
F(FI_CLIST_ADD_DEL, 'C', 'a') \
|
||||
F(FI_ROA_CHECK, 'R', 'C')
|
||||
|
||||
enum f_instruction_code {
|
||||
#define F(c,a,b) \
|
||||
c = FI__TWOCHAR(a,b),
|
||||
FI__LIST
|
||||
#undef F
|
||||
} PACKED;
|
||||
|
||||
struct f_inst { /* Instruction */
|
||||
struct f_inst *next; /* Structure is 16 bytes, anyway */
|
||||
enum f_instruction_code fi_code;
|
||||
u16 aux;
|
||||
union {
|
||||
int i;
|
||||
void *p;
|
||||
} a1;
|
||||
union {
|
||||
int i;
|
||||
void *p;
|
||||
} a2;
|
||||
int lineno;
|
||||
/* Possible return values of filter execution */
|
||||
enum filter_return {
|
||||
F_NOP = 0,
|
||||
F_NONL,
|
||||
F_RETURN,
|
||||
F_ACCEPT, /* Need to preserve ordering: accepts < rejects! */
|
||||
F_REJECT,
|
||||
F_ERROR,
|
||||
};
|
||||
|
||||
#define arg1 a1.p
|
||||
#define arg2 a2.p
|
||||
static inline const char *filter_return_str(const enum filter_return fret) {
|
||||
switch (fret) {
|
||||
#define FRS(x) case x: return #x
|
||||
FRS(F_NOP);
|
||||
FRS(F_NONL);
|
||||
FRS(F_RETURN);
|
||||
FRS(F_ACCEPT);
|
||||
FRS(F_REJECT);
|
||||
FRS(F_ERROR);
|
||||
#undef FRS
|
||||
default: bug("This shall not happen");
|
||||
}
|
||||
}
|
||||
|
||||
/* Not enough fields in f_inst for three args used by roa_check() */
|
||||
struct f_inst_roa_check {
|
||||
struct f_inst i;
|
||||
struct roa_table_config *rtc;
|
||||
};
|
||||
|
||||
struct f_inst3 {
|
||||
struct f_inst i;
|
||||
union {
|
||||
int i;
|
||||
void *p;
|
||||
} a3;
|
||||
};
|
||||
|
||||
#define INST3(x) (((struct f_inst3 *) x)->a3)
|
||||
|
||||
|
||||
struct f_prefix {
|
||||
ip_addr ip;
|
||||
int len;
|
||||
#define LEN_MASK 0xff
|
||||
#define LEN_PLUS 0x1000000
|
||||
#define LEN_MINUS 0x2000000
|
||||
#define LEN_RANGE 0x4000000
|
||||
/* If range then prefix must be in range (len >> 16 & 0xff, len >> 8 & 0xff) */
|
||||
};
|
||||
|
||||
struct f_val {
|
||||
int type;
|
||||
union {
|
||||
uint i;
|
||||
u64 ec;
|
||||
lcomm lc;
|
||||
/* ip_addr ip; Folded into prefix */
|
||||
struct f_prefix px;
|
||||
char *s;
|
||||
struct f_tree *t;
|
||||
struct f_trie *ti;
|
||||
struct adata *ad;
|
||||
struct f_path_mask *path_mask;
|
||||
} val;
|
||||
};
|
||||
|
||||
struct f_dynamic_attr {
|
||||
int type;
|
||||
int f_type;
|
||||
int ea_code;
|
||||
};
|
||||
|
||||
struct f_static_attr {
|
||||
int f_type;
|
||||
int sa_code;
|
||||
int readonly;
|
||||
};
|
||||
struct f_val;
|
||||
|
||||
/* The filter encapsulating structure to be pointed-to from outside */
|
||||
struct f_line;
|
||||
struct filter {
|
||||
char *name;
|
||||
struct f_inst *root;
|
||||
struct symbol *sym;
|
||||
const struct f_line *root;
|
||||
};
|
||||
|
||||
struct f_inst *f_new_inst(enum f_instruction_code fi_code);
|
||||
struct f_inst *f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da);
|
||||
struct f_inst *f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa);
|
||||
static inline struct f_dynamic_attr f_new_dynamic_attr(int type, int f_type, int code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
|
||||
{ return (struct f_dynamic_attr) { .type = type, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
|
||||
static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
|
||||
{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
|
||||
struct f_tree *f_new_tree(void);
|
||||
struct f_inst *f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument);
|
||||
struct f_inst *f_generate_roa_check(struct symbol *sym, struct f_inst *prefix, struct f_inst *asn);
|
||||
|
||||
|
||||
struct f_tree *build_tree(struct f_tree *);
|
||||
struct f_tree *find_tree(struct f_tree *t, struct f_val val);
|
||||
int same_tree(struct f_tree *t1, struct f_tree *t2);
|
||||
void tree_format(struct f_tree *t, buffer *buf);
|
||||
|
||||
struct f_trie *f_new_trie(linpool *lp, uint node_size);
|
||||
void *trie_add_prefix(struct f_trie *t, ip_addr px, int plen, int l, int h);
|
||||
int trie_match_prefix(struct f_trie *t, ip_addr px, int plen);
|
||||
int trie_same(struct f_trie *t1, struct f_trie *t2);
|
||||
void trie_format(struct f_trie *t, buffer *buf);
|
||||
|
||||
void fprefix_get_bounds(struct f_prefix *px, int *l, int *h);
|
||||
|
||||
static inline void
|
||||
trie_add_fprefix(struct f_trie *t, struct f_prefix *px)
|
||||
{
|
||||
int l, h;
|
||||
fprefix_get_bounds(px, &l, &h);
|
||||
trie_add_prefix(t, px->ip, px->len & LEN_MASK, l, h);
|
||||
}
|
||||
|
||||
static inline int
|
||||
trie_match_fprefix(struct f_trie *t, struct f_prefix *px)
|
||||
{
|
||||
return trie_match_prefix(t, px->ip, px->len & LEN_MASK);
|
||||
}
|
||||
|
||||
|
||||
struct ea_list;
|
||||
struct rte;
|
||||
|
||||
int f_run(struct filter *filter, struct rte **rte, struct ea_list **tmp_attrs, struct linpool *tmp_pool, int flags);
|
||||
struct f_val f_eval_rte(struct f_inst *expr, struct rte **rte, struct linpool *tmp_pool);
|
||||
struct f_val f_eval(struct f_inst *expr, struct linpool *tmp_pool);
|
||||
uint f_eval_int(struct f_inst *expr);
|
||||
enum filter_return f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags);
|
||||
enum filter_return f_eval_rte(const struct f_line *expr, struct rte **rte, struct linpool *tmp_pool);
|
||||
uint f_eval_int(const struct f_line *expr);
|
||||
enum filter_return f_eval_buf(const struct f_line *expr, struct linpool *tmp_pool, buffer *buf);
|
||||
|
||||
char *filter_name(struct filter *filter);
|
||||
int filter_same(struct filter *new, struct filter *old);
|
||||
const char *filter_name(const struct filter *filter);
|
||||
int filter_same(const struct filter *new, const struct filter *old);
|
||||
int f_same(const struct f_line *f1, const struct f_line *f2);
|
||||
|
||||
int i_same(struct f_inst *f1, struct f_inst *f2);
|
||||
void filter_commit(struct config *new, struct config *old);
|
||||
|
||||
int val_compare(struct f_val v1, struct f_val v2);
|
||||
int val_same(struct f_val v1, struct f_val v2);
|
||||
|
||||
void val_format(struct f_val v, buffer *buf);
|
||||
|
||||
|
||||
#define F_NOP 0
|
||||
#define F_NONL 1
|
||||
#define F_ACCEPT 2 /* Need to preserve ordering: accepts < rejects! */
|
||||
#define F_REJECT 3
|
||||
#define F_ERROR 4
|
||||
#define F_QUITBIRD 5
|
||||
void filters_dump_all(void);
|
||||
|
||||
#define FILTER_ACCEPT NULL
|
||||
#define FILTER_REJECT ((void *) 1)
|
||||
#define FILTER_REJECT ((struct filter *) 1)
|
||||
#define FILTER_UNDEF ((struct filter *) 2) /* Used in BGP */
|
||||
|
||||
/* Type numbers must be in 0..0xff range */
|
||||
#define T_MASK 0xff
|
||||
|
||||
/* Internal types */
|
||||
/* Do not use type of zero, that way we'll see errors easier. */
|
||||
#define T_VOID 1
|
||||
|
||||
/* User visible types, which fit in int */
|
||||
#define T_INT 0x10
|
||||
#define T_BOOL 0x11
|
||||
#define T_PAIR 0x12 /* Notice that pair is stored as integer: first << 16 | second */
|
||||
#define T_QUAD 0x13
|
||||
|
||||
/* Put enumerational types in 0x30..0x3f range */
|
||||
#define T_ENUM_LO 0x30
|
||||
#define T_ENUM_HI 0x3f
|
||||
|
||||
#define T_ENUM_RTS 0x30
|
||||
#define T_ENUM_BGP_ORIGIN 0x31
|
||||
#define T_ENUM_SCOPE 0x32
|
||||
#define T_ENUM_RTC 0x33
|
||||
#define T_ENUM_RTD 0x34
|
||||
#define T_ENUM_ROA 0x35
|
||||
#define T_ENUM_RA_PREFERENCE 0x36
|
||||
/* new enums go here */
|
||||
#define T_ENUM_EMPTY 0x3f /* Special hack for atomic_aggr */
|
||||
|
||||
#define T_ENUM T_ENUM_LO ... T_ENUM_HI
|
||||
|
||||
/* Bigger ones */
|
||||
#define T_IP 0x20
|
||||
#define T_PREFIX 0x21
|
||||
#define T_STRING 0x22
|
||||
#define T_PATH_MASK 0x23 /* mask for BGP path */
|
||||
#define T_PATH 0x24 /* BGP path */
|
||||
#define T_CLIST 0x25 /* Community list */
|
||||
#define T_EC 0x26 /* Extended community value, u64 */
|
||||
#define T_ECLIST 0x27 /* Extended community list */
|
||||
#define T_LC 0x28 /* Large community value, lcomm */
|
||||
#define T_LCLIST 0x29 /* Large community list */
|
||||
|
||||
#define T_RETURN 0x40
|
||||
#define T_SET 0x80
|
||||
#define T_PREFIX_SET 0x81
|
||||
|
||||
|
||||
#define SA_FROM 1
|
||||
#define SA_GW 2
|
||||
#define SA_NET 3
|
||||
#define SA_PROTO 4
|
||||
#define SA_SOURCE 5
|
||||
#define SA_SCOPE 6
|
||||
#define SA_CAST 7
|
||||
#define SA_DEST 8
|
||||
#define SA_IFNAME 9
|
||||
#define SA_IFINDEX 10
|
||||
|
||||
|
||||
struct f_tree {
|
||||
struct f_tree *left, *right;
|
||||
struct f_val from, to;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct f_trie_node
|
||||
{
|
||||
ip_addr addr, mask, accept;
|
||||
int plen;
|
||||
struct f_trie_node *c[2];
|
||||
};
|
||||
|
||||
struct f_trie
|
||||
{
|
||||
linpool *lp;
|
||||
int zero;
|
||||
uint node_size;
|
||||
struct f_trie_node root[0]; /* Root trie node follows */
|
||||
};
|
||||
|
||||
#define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
|
||||
|
||||
#define FF_FORCE_TMPATTR 1 /* Force all attributes to be temporary */
|
||||
#define FF_SILENT 2 /* Silent filter execution */
|
||||
|
||||
/* Custom route attributes */
|
||||
struct custom_attribute {
|
||||
resource r;
|
||||
struct f_dynamic_attr *fda;
|
||||
const char *name;
|
||||
};
|
||||
|
||||
struct custom_attribute *ca_lookup(pool *p, const char *name, int ea_type);
|
||||
|
||||
#endif
|
||||
|
91
filter/filter_test.c
Normal file
91
filter/filter_test.c
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Filters: Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "test/bt-utils.h"
|
||||
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
#include "filter/f-inst.h"
|
||||
#include "conf/conf.h"
|
||||
|
||||
#define BT_CONFIG_FILE "filter/test.conf"
|
||||
|
||||
|
||||
static int
|
||||
t_reconfig(void)
|
||||
{
|
||||
if (!bt_config_file_parse(BT_CONFIG_FILE))
|
||||
return 0;
|
||||
|
||||
struct symbol *s;
|
||||
WALK_LIST(s, config->symbols)
|
||||
if ((s->class == SYM_FUNCTION) || (s->class == SYM_FILTER))
|
||||
bt_assert_msg((s->flags & SYM_FLAG_SAME), "Symbol %s same check", s->name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
run_function(const void *arg)
|
||||
{
|
||||
const struct f_bt_test_suite *t = arg;
|
||||
|
||||
if (t->cmp)
|
||||
return t->result == f_same(t->fn, t->cmp);
|
||||
|
||||
linpool *tmp = lp_new_default(&root_pool);
|
||||
enum filter_return fret = f_eval(t->fn, tmp, NULL);
|
||||
rfree(tmp);
|
||||
|
||||
return (fret < F_REJECT);
|
||||
}
|
||||
|
||||
static void
|
||||
bt_assert_filter(int result, const struct f_line_item *assert)
|
||||
{
|
||||
int bt_suit_case_result = 1;
|
||||
if (!result)
|
||||
{
|
||||
bt_result = 0;
|
||||
bt_suite_result = 0;
|
||||
bt_suit_case_result = 0;
|
||||
}
|
||||
|
||||
bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)",
|
||||
assert->lineno, assert->i_FI_ASSERT.s);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
bt_bird_init();
|
||||
|
||||
bt_assert_hook = bt_assert_filter;
|
||||
|
||||
/* Initial test.conf parsing, must be done here */
|
||||
if (!bt_config_file_parse(BT_CONFIG_FILE))
|
||||
abort();
|
||||
|
||||
bt_test_suite(t_reconfig, "Testing reconfiguration");
|
||||
|
||||
struct f_bt_test_suite *t;
|
||||
WALK_LIST(t, config->tests)
|
||||
bt_test_suite_base(run_function, t->fn_name, t, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
|
||||
|
||||
bt_bird_cleanup();
|
||||
return bt_exit_value();
|
||||
}
|
23
filter/test-reconf-begin.conf
Normal file
23
filter/test-reconf-begin.conf
Normal file
@ -0,0 +1,23 @@
|
||||
router id 1.1.1.1;
|
||||
protocol device {}
|
||||
|
||||
function a() {
|
||||
return true;
|
||||
}
|
||||
|
||||
function b() {
|
||||
return a();
|
||||
}
|
||||
|
||||
function c() {
|
||||
return b();
|
||||
}
|
||||
|
||||
filter d {
|
||||
if c() then accept; else reject;
|
||||
}
|
||||
|
||||
protocol static {
|
||||
ipv4 { import filter d; };
|
||||
route 10.0.0.0/24 unreachable;
|
||||
}
|
23
filter/test-reconf-end.conf
Normal file
23
filter/test-reconf-end.conf
Normal file
@ -0,0 +1,23 @@
|
||||
router id 1.1.1.1;
|
||||
protocol device {}
|
||||
|
||||
function a() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function b() {
|
||||
return a();
|
||||
}
|
||||
|
||||
function c() {
|
||||
return b();
|
||||
}
|
||||
|
||||
filter d {
|
||||
if c() then accept; else reject;
|
||||
}
|
||||
|
||||
protocol static {
|
||||
ipv4 { import filter d; };
|
||||
route 10.0.0.0/24 unreachable;
|
||||
}
|
1618
filter/test.conf
1618
filter/test.conf
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,3 @@
|
||||
|
||||
print "Entering include";
|
||||
print "Should be 2: ", 1+1;
|
||||
print "Leaving include";
|
||||
|
||||
bt_assert(1+1 = 2);
|
||||
i = 42;
|
||||
|
@ -18,6 +18,7 @@ protocol direct {
|
||||
|
||||
protocol kernel {
|
||||
disabled;
|
||||
ipv4; # Must be specified at least one channel
|
||||
# learn; # Learn all routes from the kernel
|
||||
# scan time 10; # Scan kernel tables every 10 seconds
|
||||
}
|
||||
@ -25,51 +26,56 @@ protocol kernel {
|
||||
protocol static {
|
||||
# disabled;
|
||||
|
||||
import filter { print "ahoj";
|
||||
print source;
|
||||
if source = RTS_STATIC then {
|
||||
print "It is from static";
|
||||
}
|
||||
print from;
|
||||
from = 1.2.3.4;
|
||||
print from;
|
||||
print scope;
|
||||
scope = SCOPE_HOST;
|
||||
print scope;
|
||||
if !(scope ~ [ SCOPE_HOST, SCOPE_SITE ]) then {
|
||||
print "Failed in test";
|
||||
quitbird;
|
||||
}
|
||||
|
||||
preference = 15;
|
||||
print preference;
|
||||
preference = 29;
|
||||
print preference;
|
||||
rip_metric = 1;
|
||||
print rip_metric;
|
||||
rip_metric = rip_metric + 5;
|
||||
print rip_metric;
|
||||
bgp_community = - empty - ;
|
||||
print "nazdar";
|
||||
bgp_community = add(bgp_community, (1,2));
|
||||
print "cau";
|
||||
bgp_community = add(bgp_community, (2,3));
|
||||
bgp_community.add((4,5));
|
||||
print "community = ", bgp_community;
|
||||
bgp_community.delete((2,3));
|
||||
print "community = ", bgp_community;
|
||||
bgp_community.empty;
|
||||
print "community = ", bgp_community;
|
||||
print "done";
|
||||
};
|
||||
ipv4 {
|
||||
export all;
|
||||
|
||||
import filter {
|
||||
print "ahoj";
|
||||
print source;
|
||||
if source = RTS_STATIC then {
|
||||
print "It is from static";
|
||||
}
|
||||
print from;
|
||||
from = 1.2.3.4;
|
||||
print from;
|
||||
print scope;
|
||||
scope = SCOPE_HOST;
|
||||
print scope;
|
||||
if !(scope ~ [ SCOPE_HOST, SCOPE_SITE ]) then {
|
||||
print "Failed in test";
|
||||
}
|
||||
|
||||
preference = 15;
|
||||
print preference;
|
||||
preference = 29;
|
||||
print preference;
|
||||
rip_metric = 1;
|
||||
print rip_metric;
|
||||
rip_metric = rip_metric + 5;
|
||||
print rip_metric;
|
||||
|
||||
bgp_community = -empty-;
|
||||
print "hi";
|
||||
bgp_community = add(bgp_community, (1,2));
|
||||
print "hello";
|
||||
bgp_community = add(bgp_community, (2,3));
|
||||
bgp_community.add((4,5));
|
||||
print "community = ", bgp_community;
|
||||
bgp_community.delete((2,3));
|
||||
print "community = ", bgp_community;
|
||||
bgp_community.empty;
|
||||
print "community = ", bgp_community;
|
||||
print "done";
|
||||
|
||||
accept;
|
||||
};
|
||||
};
|
||||
route 0.0.0.0/0 via 195.113.31.113;
|
||||
route 62.168.0.0/25 reject;
|
||||
route 1.2.3.4/32 via 195.113.31.124;
|
||||
# route 10.0.0.0/8 reject;
|
||||
# route 10.1.1.0:255.255.255.0 via 62.168.0.3;
|
||||
# route 10.1.2.0:255.255.255.0 via 62.168.0.3;
|
||||
# route 10.1.3.0:255.255.255.0 via 62.168.0.4;
|
||||
# route 10.2.0.0/24 via "arc0";
|
||||
export all;
|
||||
route 10.0.0.0/8 reject;
|
||||
route 10.1.1.0/24 via 62.168.0.3;
|
||||
route 10.1.2.0/24 via 62.168.0.3;
|
||||
route 10.1.3.0/24 via 62.168.0.4;
|
||||
route 10.2.0.0/24 via "arc0";
|
||||
}
|
||||
|
@ -1,182 +0,0 @@
|
||||
/*
|
||||
* This is an example configuration file.
|
||||
* FIXME: add all examples from docs here.
|
||||
*/
|
||||
|
||||
# Yet another comment
|
||||
|
||||
router id 62.168.0.1;
|
||||
|
||||
define xyzzy = (120+10);
|
||||
|
||||
function callme(int arg1; int arg2)
|
||||
int local1;
|
||||
int local2;
|
||||
int i;
|
||||
{
|
||||
printn "Function callme called arguments ", arg1, " and ", arg2, ":" ;
|
||||
i = arg2;
|
||||
|
||||
case arg1 {
|
||||
2: print "dva"; print "jeste jednou dva";
|
||||
3 .. 5: print "tri az pet";
|
||||
else: print "neco jineho";
|
||||
}
|
||||
}
|
||||
|
||||
function fifteen()
|
||||
{
|
||||
print "fifteen called";
|
||||
return 15;
|
||||
}
|
||||
|
||||
function paths()
|
||||
bgpmask pm1;
|
||||
bgpmask pm2;
|
||||
bgppath p2;
|
||||
clist l;
|
||||
{
|
||||
pm1 = / 4 3 2 1 /;
|
||||
pm2 = [= 4 3 2 1 =];
|
||||
print "Testing path masks: ", pm1, " ", pm2;
|
||||
p2 = prepend( + empty +, 1 );
|
||||
p2 = prepend( p2, 2 );
|
||||
p2 = prepend( p2, 3 );
|
||||
p2 = prepend( p2, 4 );
|
||||
print "Testing paths: ", p2;
|
||||
print "Should be true: ", p2 ~ pm1, " ", p2 ~ pm2;
|
||||
print "4 = ", p2.len;
|
||||
p2 = prepend( p2, 5 );
|
||||
print "Should be false: ", p2 ~ pm1, " ", p2 ~ pm2;
|
||||
print "Should be true: ", p2 ~ / ? 4 3 2 1 /, " ", p2, " ", / ? 4 3 2 1 /;
|
||||
print "Should be true: ", p2 ~ [= * 4 3 * 1 =], " ", p2, " ", [= * 4 3 * 1 =];
|
||||
print "5 = ", p2.len;
|
||||
|
||||
pm1 = [= 1 2 * 3 4 5 =];
|
||||
p2 = prepend( + empty +, 5 );
|
||||
p2 = prepend( p2, 4 );
|
||||
p2 = prepend( p2, 3 );
|
||||
p2 = prepend( p2, 3 );
|
||||
p2 = prepend( p2, 2 );
|
||||
p2 = prepend( p2, 1 );
|
||||
print "Should be true: ", p2 ~ pm1, " ", p2, " ", pm1;
|
||||
|
||||
l = - empty -;
|
||||
l = add( l, (1,2) );
|
||||
l = add( l, (2,3) );
|
||||
print "Community list (1,2) (2,3) ", l;
|
||||
print "Should be true: ", (2,3) ~ l;
|
||||
l = delete( l, (2,3) );
|
||||
print "Community list (1,2) ", l;
|
||||
print "Should be false: ", (2,3) ~ l;
|
||||
}
|
||||
|
||||
function bla()
|
||||
{
|
||||
print "fifteen called";
|
||||
return 15;
|
||||
}
|
||||
|
||||
define four=4;
|
||||
|
||||
function test_pxset(prefix set pxs)
|
||||
{
|
||||
print " must be true: ", 1000::/8 ~ pxs, ",", 1000::/10 ~ pxs, ",", 1000::/12 ~ pxs, ",",
|
||||
2000::/24 ~ pxs, ",", 2000:4000::/24 ~ pxs, ",", 2000::/26 ~ pxs, ",",
|
||||
2000:8000::/26 ~ pxs, ",", 2000::/28 ~ pxs, ",", 2000:FFF0::/28 ~ pxs;
|
||||
print " must be false: ", 1000::/7 ~ pxs, ",", 1000::/13 ~ pxs, ",", 1000::/16 ~ pxs, ",",
|
||||
2000::/16 ~ pxs, ",", 2000::/23 ~ pxs, ",", 2000::/29 ~ pxs, ",",
|
||||
1100::/10 ~ pxs, ",", 2010::/26 ~ pxs;
|
||||
}
|
||||
|
||||
function __startup()
|
||||
int i;
|
||||
bool b;
|
||||
prefix px;
|
||||
ip p;
|
||||
pair pp;
|
||||
int set is;
|
||||
prefix set pxs;
|
||||
string s;
|
||||
{
|
||||
print "Testing filter language:";
|
||||
i = four;
|
||||
i = 12*100 + 60/2 + i;
|
||||
i = ( i + 0 );
|
||||
print " arithmetics: 1234 = ", i;
|
||||
printn " if statements ";
|
||||
print "what happens here?";
|
||||
printn ".";
|
||||
if (i = 4) then { print "*** FAIL: if 0"; quitbird; } else printn ".";
|
||||
# if !(i = 3) then { print "*** FAIL: if 0"; quitbird; } else printn ".";
|
||||
if 1234 = i then printn "."; else { print "*** FAIL: if 1 else"; }
|
||||
# if 1 <= 1 then printn "."; else { print "*** FAIL: test 3"; }
|
||||
if 1234 < 1234 then { print "*** FAIL: test 4"; quitbird; } else print "ok";
|
||||
is = [ 2, 3, 4, 7..11 ];
|
||||
print " must be true: ", 1180::/16 ~ [ 1100::/8{ 15 , 17 } ];
|
||||
print " data types; must be true: ", 12::34 = 12::34, ",", 1 ~ [1,2,3], ",", 5 ~ [1..20], ",", 10 ~ is, ",", 2 ~ [ 1, 2, 3 ], ",", 5 ~ [ 4 .. 7 ], ",", 12::34 ~ [ 12::33..12::35 ], ",", 1020::34 ~ 1000::/8, ",", 1000::/8 ~ 1000::/8, ",", 1000::/8 ~ [ 1000::/8+ ];
|
||||
print " must be true: ", true && true, ",", true || false;
|
||||
|
||||
# print " must be true: ", defined(1), ",", defined(1.2.3.4), ",", 1 != 2, ",", 1 <= 2;
|
||||
print " data types: must be false: ", 1 ~ [ 2, 3, 4 ], ",", 5 ~ is, ",", 12::34 ~ [ 12::33, 12::35 ], ",", (1,2) > (2,2), ",", (1,1) > (1,1), ",", 1000::/9 ~ [ 1000::/8- ], ",", 1000::/17 ~ [ 1000::/8{ 15 , 16 } ], ",", true && false;
|
||||
|
||||
px = 1020::/18;
|
||||
print "Testing prefixes: 1020::/18 = ", px;
|
||||
p = 1234:5678::;
|
||||
print "Testing mask : 1200:: = ", p.mask(8);
|
||||
|
||||
pp = (1, 2);
|
||||
print "Testing pairs: (1,2) = ", (1,2), " = ", pp;
|
||||
print "Testing enums: ", RTS_DUMMY, " ", RTS_STATIC;
|
||||
|
||||
s = "Hello";
|
||||
print "Testing string: ", s, " true: ", s ~ "Hell*", " false: ", s ~ "ell*";
|
||||
|
||||
b = true;
|
||||
print "Testing bool: ", b, ", ", !b;
|
||||
|
||||
pxs = [ 1102::/16, 1104::/16+];
|
||||
print "Testing prefix sets: ";
|
||||
print pxs;
|
||||
print " must be true: ", 1102::/16 ~ pxs, ",", 1104::/16 ~ pxs, ",", 1104::/18 ~ pxs, ",", 1104::/32 ~ pxs;
|
||||
print " must be false: ", 1101::/16 ~ pxs, ",", 1103::/16 ~ pxs, ",", 1102::/15 ~ pxs, ",", 1102::/17 ~ pxs, ",",
|
||||
1102::/32 ~ pxs, ",", 1104::/15 ~ pxs;
|
||||
|
||||
test_pxset([ 1000::/16{8,12}, 2000::/16{24,28} ]);
|
||||
print "What will this do? ", [ 1, 2, 1, 1, 1, 3, 4, 1, 1, 1, 5 ];
|
||||
|
||||
print "Testing functions...";
|
||||
# callme ( 1, 2 );
|
||||
callme ( 2, 2 );
|
||||
callme ( 2, 2 );
|
||||
callme ( 3, 2 );
|
||||
callme ( 4, 2 );
|
||||
callme ( 7, 2 );
|
||||
|
||||
i = fifteen();
|
||||
print "Testing function calls: 15 = ", i;
|
||||
|
||||
paths();
|
||||
|
||||
print "done";
|
||||
quitbird;
|
||||
# print "*** FAIL: this is unreachable";
|
||||
}
|
||||
|
||||
filter testf
|
||||
int j;
|
||||
{
|
||||
print "Heya, filtering route to ", net.ip, " prefixlen ", net.len, " source ", source;
|
||||
print "This route was from ", from;
|
||||
j = 7;
|
||||
j = 17;
|
||||
if rip_metric > 15 then {
|
||||
reject "RIP Metric is more than infinity";
|
||||
}
|
||||
rip_metric = 14;
|
||||
unset(rip_metric);
|
||||
|
||||
accept "ok I take that";
|
||||
}
|
||||
|
||||
eval __startup();
|
@ -10,6 +10,7 @@
|
||||
#include "nest/bird.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
|
||||
/**
|
||||
* find_tree
|
||||
@ -23,15 +24,15 @@
|
||||
* Both set matching and |switch() { }| construction is implemented using this function,
|
||||
* thus both are as fast as they can be.
|
||||
*/
|
||||
struct f_tree *
|
||||
find_tree(struct f_tree *t, struct f_val val)
|
||||
const struct f_tree *
|
||||
find_tree(const struct f_tree *t, const struct f_val *val)
|
||||
{
|
||||
if (!t)
|
||||
return NULL;
|
||||
if ((val_compare(t->from, val) != 1) &&
|
||||
(val_compare(t->to, val) != -1))
|
||||
if ((val_compare(&(t->from), val) != 1) &&
|
||||
(val_compare(&(t->to), val) != -1))
|
||||
return t;
|
||||
if (val_compare(t->from, val) == -1)
|
||||
if (val_compare(&(t->from), val) == -1)
|
||||
return find_tree(t->right, val);
|
||||
else
|
||||
return find_tree(t->left, val);
|
||||
@ -56,7 +57,7 @@ build_tree_rec(struct f_tree **buf, int l, int h)
|
||||
static int
|
||||
tree_compare(const void *p1, const void *p2)
|
||||
{
|
||||
return val_compare((* (struct f_tree **) p1)->from, (* (struct f_tree **) p2)->from);
|
||||
return val_compare(&((* (struct f_tree **) p1)->from), &((* (struct f_tree **) p2)->from));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,12 +103,7 @@ build_tree(struct f_tree *from)
|
||||
struct f_tree *
|
||||
f_new_tree(void)
|
||||
{
|
||||
struct f_tree * ret;
|
||||
ret = cfg_alloc(sizeof(struct f_tree));
|
||||
ret->left = ret->right = NULL;
|
||||
ret->from.type = ret->to.type = T_VOID;
|
||||
ret->from.val.i = ret->to.val.i = 0;
|
||||
ret->data = NULL;
|
||||
struct f_tree *ret = cfg_allocz(sizeof(struct f_tree));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -119,39 +115,39 @@ f_new_tree(void)
|
||||
* Compares two trees and returns 1 if they are same
|
||||
*/
|
||||
int
|
||||
same_tree(struct f_tree *t1, struct f_tree *t2)
|
||||
same_tree(const struct f_tree *t1, const struct f_tree *t2)
|
||||
{
|
||||
if ((!!t1) != (!!t2))
|
||||
return 0;
|
||||
if (!t1)
|
||||
return 1;
|
||||
if (val_compare(t1->from, t2->from))
|
||||
if (val_compare(&(t1->from), &(t2->from)))
|
||||
return 0;
|
||||
if (val_compare(t1->to, t2->to))
|
||||
if (val_compare(&(t1->to), &(t2->to)))
|
||||
return 0;
|
||||
if (!same_tree(t1->left, t2->left))
|
||||
return 0;
|
||||
if (!same_tree(t1->right, t2->right))
|
||||
return 0;
|
||||
if (!i_same(t1->data, t2->data))
|
||||
if (!f_same(t1->data, t2->data))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
tree_node_format(struct f_tree *t, buffer *buf)
|
||||
tree_node_format(const struct f_tree *t, buffer *buf)
|
||||
{
|
||||
if (t == NULL)
|
||||
return;
|
||||
|
||||
tree_node_format(t->left, buf);
|
||||
|
||||
val_format(t->from, buf);
|
||||
if (val_compare(t->from, t->to) != 0)
|
||||
val_format(&(t->from), buf);
|
||||
if (val_compare(&(t->from), &(t->to)) != 0)
|
||||
{
|
||||
buffer_puts(buf, "..");
|
||||
val_format(t->to, buf);
|
||||
val_format(&(t->to), buf);
|
||||
}
|
||||
buffer_puts(buf, ", ");
|
||||
|
||||
@ -159,7 +155,7 @@ tree_node_format(struct f_tree *t, buffer *buf)
|
||||
}
|
||||
|
||||
void
|
||||
tree_format(struct f_tree *t, buffer *buf)
|
||||
tree_format(const struct f_tree *t, buffer *buf)
|
||||
{
|
||||
buffer_puts(buf, "[");
|
||||
|
||||
@ -174,3 +170,14 @@ tree_format(struct f_tree *t, buffer *buf)
|
||||
|
||||
buffer_puts(buf, "]");
|
||||
}
|
||||
|
||||
void
|
||||
tree_walk(const struct f_tree *t, void (*hook)(const struct f_tree *, void *), void *data)
|
||||
{
|
||||
if (!t)
|
||||
return;
|
||||
|
||||
tree_walk(t->left, hook, data);
|
||||
hook(t, data);
|
||||
tree_walk(t->right, hook, data);
|
||||
}
|
||||
|
305
filter/tree_test.c
Normal file
305
filter/tree_test.c
Normal file
@ -0,0 +1,305 @@
|
||||
/*
|
||||
* Filters: Utility Functions Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "test/bt-utils.h"
|
||||
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
#include "conf/conf.h"
|
||||
|
||||
#define MAX_TREE_HEIGHT 13
|
||||
|
||||
static void
|
||||
start_conf_env(void)
|
||||
{
|
||||
bt_bird_init();
|
||||
|
||||
pool *p = rp_new(&root_pool, "helper_pool");
|
||||
linpool *l = lp_new_default(p);
|
||||
cfg_mem = l;
|
||||
}
|
||||
|
||||
static struct f_tree *
|
||||
new_tree(uint id)
|
||||
{
|
||||
struct f_tree *tree = f_new_tree();
|
||||
tree->from.type = tree->to.type = T_INT;
|
||||
tree->from.val.i = tree->to.val.i = id;
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
/*
|
||||
* Show subtree in infix notation
|
||||
*/
|
||||
static void
|
||||
show_subtree(struct f_tree *node)
|
||||
{
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
show_subtree(node->left);
|
||||
|
||||
if (node->from.val.i == node->to.val.i)
|
||||
bt_debug("%u ", node->from.val.i);
|
||||
else
|
||||
bt_debug("%u..%u ", node->from.val.i, node->to.val.i);
|
||||
|
||||
show_subtree(node->right);
|
||||
}
|
||||
|
||||
static void
|
||||
show_tree2(struct f_tree *root_node, const char *tree_name)
|
||||
{
|
||||
bt_debug("%s: \n", tree_name);
|
||||
bt_debug("[ ");
|
||||
show_subtree(root_node);
|
||||
bt_debug("]\n\n");
|
||||
}
|
||||
|
||||
#define show_tree(tree) show_tree2(tree, #tree);
|
||||
|
||||
static uint
|
||||
get_nodes_count_full_bin_tree(uint height)
|
||||
{
|
||||
return (bt_naive_pow(2, height+1) - 1);
|
||||
}
|
||||
|
||||
static struct f_tree *
|
||||
get_balanced_full_subtree(uint height, uint idx)
|
||||
{
|
||||
struct f_tree *node = new_tree(idx);
|
||||
if (height > 0)
|
||||
{
|
||||
uint nodes_in_subtree = get_nodes_count_full_bin_tree(--height);
|
||||
node->left = get_balanced_full_subtree(height, idx - nodes_in_subtree/2 - 1);
|
||||
node->right = get_balanced_full_subtree(height, idx + nodes_in_subtree/2 + 1);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
static struct f_tree *
|
||||
get_balanced_full_tree(uint height)
|
||||
{
|
||||
return get_balanced_full_subtree(height, get_nodes_count_full_bin_tree(height)/2);
|
||||
}
|
||||
|
||||
static struct f_tree *
|
||||
get_degenerated_left_tree(uint nodes_count)
|
||||
{
|
||||
struct f_tree *old = NULL;
|
||||
struct f_tree *new = NULL;
|
||||
uint i;
|
||||
|
||||
for (i = 0; i < nodes_count; i++)
|
||||
{
|
||||
old = new;
|
||||
new = new_tree(nodes_count-1-i);
|
||||
new->left = old;
|
||||
}
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
static struct f_tree *
|
||||
get_random_degenerated_left_tree(uint nodes_count)
|
||||
{
|
||||
struct f_tree *tree = get_degenerated_left_tree(nodes_count);
|
||||
|
||||
size_t avaible_indexes_size = nodes_count * sizeof(byte);
|
||||
byte *avaible_indexes = malloc(avaible_indexes_size);
|
||||
memset(avaible_indexes, 0, avaible_indexes_size);
|
||||
|
||||
struct f_tree *n;
|
||||
for (n = tree; n; n = n->left)
|
||||
{
|
||||
uint selected_idx;
|
||||
do
|
||||
{
|
||||
selected_idx = bt_random() % nodes_count;
|
||||
} while(avaible_indexes[selected_idx] != 0);
|
||||
|
||||
avaible_indexes[selected_idx] = 1;
|
||||
n->from.type = n->to.type = T_INT;
|
||||
n->from.val.i = n->to.val.i = selected_idx;
|
||||
}
|
||||
|
||||
free(avaible_indexes);
|
||||
return tree;
|
||||
}
|
||||
|
||||
static struct f_tree *
|
||||
get_balanced_tree_with_ranged_values(uint nodes_count)
|
||||
{
|
||||
struct f_tree *tree = get_degenerated_left_tree(nodes_count);
|
||||
|
||||
uint idx = 0;
|
||||
struct f_tree *n;
|
||||
for (n = tree; n; n = n->left)
|
||||
{
|
||||
n->from.type = n->to.type = T_INT;
|
||||
n->from.val.i = idx;
|
||||
idx += (uint)bt_random() / nodes_count; /* (... / nodes_count) preventing overflow an uint idx */
|
||||
n->to.val.i = idx++;
|
||||
}
|
||||
|
||||
return build_tree(tree);
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
t_balancing(void)
|
||||
{
|
||||
start_conf_env();
|
||||
|
||||
uint height;
|
||||
for (height = 1; height < MAX_TREE_HEIGHT; height++)
|
||||
{
|
||||
uint nodes_count = get_nodes_count_full_bin_tree(height);
|
||||
|
||||
struct f_tree *simple_degenerated_tree = get_degenerated_left_tree(nodes_count);
|
||||
show_tree(simple_degenerated_tree);
|
||||
|
||||
struct f_tree *expected_balanced_tree = get_balanced_full_tree(height);
|
||||
show_tree(expected_balanced_tree);
|
||||
|
||||
struct f_tree *balanced_tree_from_simple = build_tree(simple_degenerated_tree);
|
||||
show_tree(balanced_tree_from_simple);
|
||||
|
||||
bt_assert(same_tree(balanced_tree_from_simple, expected_balanced_tree));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
t_balancing_random(void)
|
||||
{
|
||||
start_conf_env();
|
||||
|
||||
uint height;
|
||||
for (height = 1; height < MAX_TREE_HEIGHT; height++)
|
||||
{
|
||||
uint nodes_count = get_nodes_count_full_bin_tree(height);
|
||||
|
||||
struct f_tree *expected_balanced_tree = get_balanced_full_tree(height);
|
||||
|
||||
uint i;
|
||||
for(i = 0; i < 10; i++)
|
||||
{
|
||||
struct f_tree *random_degenerated_tree = get_random_degenerated_left_tree(nodes_count);
|
||||
show_tree(random_degenerated_tree);
|
||||
|
||||
struct f_tree *balanced_tree_from_random = build_tree(random_degenerated_tree);
|
||||
|
||||
show_tree(expected_balanced_tree);
|
||||
show_tree(balanced_tree_from_random);
|
||||
|
||||
bt_assert(same_tree(balanced_tree_from_random, expected_balanced_tree));
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_find(void)
|
||||
{
|
||||
start_conf_env();
|
||||
|
||||
uint height;
|
||||
for (height = 1; height < MAX_TREE_HEIGHT; height++)
|
||||
{
|
||||
uint nodes_count = get_nodes_count_full_bin_tree(height);
|
||||
|
||||
struct f_tree *tree = get_balanced_full_tree(height);
|
||||
show_tree(tree);
|
||||
|
||||
struct f_val looking_up_value = {
|
||||
.type = T_INT
|
||||
};
|
||||
for(looking_up_value.val.i = 0; looking_up_value.val.i < nodes_count; looking_up_value.val.i++)
|
||||
{
|
||||
const struct f_tree *found_tree = find_tree(tree, &looking_up_value);
|
||||
bt_assert((val_compare(&looking_up_value, &(found_tree->from)) == 0) && (val_compare(&looking_up_value, &(found_tree->to)) == 0));
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint
|
||||
get_max_value_in_unbalanced_tree(struct f_tree *node, uint max)
|
||||
{
|
||||
if (!node)
|
||||
return max;
|
||||
|
||||
if (node->to.val.i > max)
|
||||
max = node->to.val.i;
|
||||
|
||||
uint max_left = get_max_value_in_unbalanced_tree(node->left, max);
|
||||
if (max_left > max)
|
||||
max = max_left;
|
||||
|
||||
uint max_right = get_max_value_in_unbalanced_tree(node->right, max);
|
||||
if (max_right > max)
|
||||
max = max_right;
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
static int
|
||||
t_find_ranges(void)
|
||||
{
|
||||
start_conf_env();
|
||||
|
||||
uint height;
|
||||
for (height = 1; height < MAX_TREE_HEIGHT; height++)
|
||||
{
|
||||
uint nodes_count = get_nodes_count_full_bin_tree(height);
|
||||
|
||||
struct f_tree *tree = get_balanced_tree_with_ranged_values(nodes_count);
|
||||
uint max_value = get_max_value_in_unbalanced_tree(tree, 0);
|
||||
|
||||
show_tree(tree);
|
||||
|
||||
bt_debug("max_value: %u \n", max_value);
|
||||
|
||||
struct f_val needle = {
|
||||
.type = T_INT
|
||||
};
|
||||
uint *i = &needle.val.i;
|
||||
|
||||
for(*i = 0; *i <= max_value; *i += (uint)bt_random()/nodes_count)
|
||||
{
|
||||
const struct f_tree *found_tree = find_tree(tree, &needle);
|
||||
bt_debug("searching: %u \n", *i);
|
||||
bt_assert(
|
||||
(val_compare(&needle, &(found_tree->from)) == 0) || (val_compare(&needle, &(found_tree->to)) == 0) ||
|
||||
((val_compare(&needle, &(found_tree->from)) == 1) && (val_compare(&needle, &(found_tree->to)) == -1))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_balancing, "Balancing strong unbalanced trees");
|
||||
bt_test_suite(t_balancing_random, "Balancing random unbalanced trees");
|
||||
bt_test_suite(t_find, "Finding values in trees");
|
||||
bt_test_suite(t_find_ranges, "Finding values in trees with random ranged values");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
333
filter/trie.c
333
filter/trie.c
@ -73,26 +73,45 @@
|
||||
#include "lib/string.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
|
||||
|
||||
/*
|
||||
* In the trie_add_prefix(), we use ip_addr (assuming that it is the same as
|
||||
* ip6_addr) to handle both IPv4 and IPv6 prefixes. In contrast to rest of the
|
||||
* BIRD, IPv4 addresses are just zero-padded from right. That is why we have
|
||||
* ipt_from_ip4() and ipt_to_ip4() macros below.
|
||||
*/
|
||||
|
||||
#define ipa_mkmask(x) ip6_mkmask(x)
|
||||
#define ipa_masklen(x) ip6_masklen(&x)
|
||||
#define ipa_pxlen(x,y) ip6_pxlen(x,y)
|
||||
#define ipa_getbit(x,n) ip6_getbit(x,n)
|
||||
|
||||
#define ipt_from_ip4(x) _MI6(_I(x), 0, 0, 0)
|
||||
#define ipt_to_ip4(x) _MI4(_I0(x))
|
||||
|
||||
|
||||
/**
|
||||
* f_new_trie - allocates and returns a new empty trie
|
||||
* @lp: linear pool to allocate items from
|
||||
* @node_size: node size to be used (&f_trie_node and user data)
|
||||
* @data_size: user data attached to node
|
||||
*/
|
||||
struct f_trie *
|
||||
f_new_trie(linpool *lp, uint node_size)
|
||||
f_new_trie(linpool *lp, uint data_size)
|
||||
{
|
||||
struct f_trie * ret;
|
||||
ret = lp_allocz(lp, sizeof(struct f_trie) + node_size);
|
||||
ret = lp_allocz(lp, sizeof(struct f_trie) + data_size);
|
||||
ret->lp = lp;
|
||||
ret->node_size = node_size;
|
||||
ret->ipv4 = -1;
|
||||
ret->data_size = data_size;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline struct f_trie_node *
|
||||
new_node(struct f_trie *t, int plen, ip_addr paddr, ip_addr pmask, ip_addr amask)
|
||||
static inline struct f_trie_node4 *
|
||||
new_node4(struct f_trie *t, int plen, ip4_addr paddr, ip4_addr pmask, ip4_addr amask)
|
||||
{
|
||||
struct f_trie_node *n = lp_allocz(t->lp, t->node_size);
|
||||
struct f_trie_node4 *n = lp_allocz(t->lp, sizeof(struct f_trie_node4) + t->data_size);
|
||||
n->plen = plen;
|
||||
n->addr = paddr;
|
||||
n->mask = pmask;
|
||||
@ -100,32 +119,90 @@ new_node(struct f_trie *t, int plen, ip_addr paddr, ip_addr pmask, ip_addr amask
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline void
|
||||
attach_node(struct f_trie_node *parent, struct f_trie_node *child)
|
||||
static inline struct f_trie_node6 *
|
||||
new_node6(struct f_trie *t, int plen, ip6_addr paddr, ip6_addr pmask, ip6_addr amask)
|
||||
{
|
||||
parent->c[ipa_getbit(child->addr, parent->plen) ? 1 : 0] = child;
|
||||
struct f_trie_node6 *n = lp_allocz(t->lp, sizeof(struct f_trie_node6) + t->data_size);
|
||||
n->plen = plen;
|
||||
n->addr = paddr;
|
||||
n->mask = pmask;
|
||||
n->accept = amask;
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline struct f_trie_node *
|
||||
new_node(struct f_trie *t, int plen, ip_addr paddr, ip_addr pmask, ip_addr amask)
|
||||
{
|
||||
if (t->ipv4)
|
||||
return (struct f_trie_node *) new_node4(t, plen, ipt_to_ip4(paddr), ipt_to_ip4(pmask), ipt_to_ip4(amask));
|
||||
else
|
||||
return (struct f_trie_node *) new_node6(t, plen, ipa_to_ip6(paddr), ipa_to_ip6(pmask), ipa_to_ip6(amask));
|
||||
}
|
||||
|
||||
static inline void
|
||||
attach_node4(struct f_trie_node4 *parent, struct f_trie_node4 *child)
|
||||
{
|
||||
parent->c[ip4_getbit(child->addr, parent->plen) ? 1 : 0] = child;
|
||||
}
|
||||
|
||||
static inline void
|
||||
attach_node6(struct f_trie_node6 *parent, struct f_trie_node6 *child)
|
||||
{
|
||||
parent->c[ip6_getbit(child->addr, parent->plen) ? 1 : 0] = child;
|
||||
}
|
||||
|
||||
static inline void
|
||||
attach_node(struct f_trie_node *parent, struct f_trie_node *child, int v4)
|
||||
{
|
||||
if (v4)
|
||||
attach_node4(&parent->v4, &child->v4);
|
||||
else
|
||||
attach_node6(&parent->v6, &child->v6);
|
||||
}
|
||||
|
||||
#define GET_ADDR(N,F,X) ((X) ? ipt_from_ip4((N)->v4.F) : ipa_from_ip6((N)->v6.F))
|
||||
#define SET_ADDR(N,F,X,V) ({ if (X) (N)->v4.F =ipt_to_ip4(V); else (N)->v6.F =ipa_to_ip6(V); })
|
||||
|
||||
#define GET_CHILD(N,F,X,I) ((X) ? (struct f_trie_node *) (N)->v4.c[I] : (struct f_trie_node *) (N)->v6.c[I])
|
||||
/**
|
||||
* trie_add_prefix
|
||||
* @t: trie to add to
|
||||
* @px: prefix address
|
||||
* @plen: prefix length
|
||||
* @net: IP network prefix
|
||||
* @l: prefix lower bound
|
||||
* @h: prefix upper bound
|
||||
*
|
||||
* Adds prefix (prefix pattern) @px/@plen to trie @t. @l and @h are lower
|
||||
* Adds prefix (prefix pattern) @n to trie @t. @l and @h are lower
|
||||
* and upper bounds on accepted prefix lengths, both inclusive.
|
||||
* 0 <= l, h <= 32 (128 for IPv6).
|
||||
*
|
||||
* Returns a pointer to the allocated node. The function can return a pointer to
|
||||
* an existing node if @px and @plen are the same. If px/plen == 0/0 (or ::/0),
|
||||
* a pointer to the root node is returned.
|
||||
* a pointer to the root node is returned. Returns NULL when called with
|
||||
* mismatched IPv4/IPv6 net type.
|
||||
*/
|
||||
|
||||
void *
|
||||
trie_add_prefix(struct f_trie *t, ip_addr px, int plen, int l, int h)
|
||||
trie_add_prefix(struct f_trie *t, const net_addr *net, uint l, uint h)
|
||||
{
|
||||
uint plen = net_pxlen(net);
|
||||
ip_addr px;
|
||||
int v4;
|
||||
|
||||
switch (net->type)
|
||||
{
|
||||
case NET_IP4: px = ipt_from_ip4(net4_prefix(net)); v4 = 1; break;
|
||||
case NET_IP6: px = ipa_from_ip6(net6_prefix(net)); v4 = 0; break;
|
||||
default: bug("invalid type");
|
||||
}
|
||||
|
||||
if (t->ipv4 != v4)
|
||||
{
|
||||
if (t->ipv4 < 0)
|
||||
t->ipv4 = v4;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (l == 0)
|
||||
t->zero = 1;
|
||||
else
|
||||
@ -138,111 +215,171 @@ trie_add_prefix(struct f_trie *t, ip_addr px, int plen, int l, int h)
|
||||
ip_addr pmask = ipa_mkmask(plen);
|
||||
ip_addr paddr = ipa_and(px, pmask);
|
||||
struct f_trie_node *o = NULL;
|
||||
struct f_trie_node *n = t->root;
|
||||
struct f_trie_node *n = &t->root;
|
||||
|
||||
while(n)
|
||||
while (n)
|
||||
{
|
||||
ip_addr cmask = ipa_and(n->mask, pmask);
|
||||
ip_addr naddr = GET_ADDR(n, addr, v4);
|
||||
ip_addr nmask = GET_ADDR(n, mask, v4);
|
||||
ip_addr accept = GET_ADDR(n, accept, v4);
|
||||
ip_addr cmask = ipa_and(nmask, pmask);
|
||||
uint nlen = v4 ? n->v4.plen : n->v6.plen;
|
||||
|
||||
if (ipa_compare(ipa_and(paddr, cmask), ipa_and(n->addr, cmask)))
|
||||
{
|
||||
if (ipa_compare(ipa_and(paddr, cmask), ipa_and(naddr, cmask)))
|
||||
{
|
||||
/* We are out of path - we have to add branching node 'b'
|
||||
between node 'o' and node 'n', and attach new node 'a'
|
||||
as the other child of 'b'. */
|
||||
int blen = ipa_pxlen(paddr, n->addr);
|
||||
int blen = ipa_pxlen(paddr, naddr);
|
||||
ip_addr bmask = ipa_mkmask(blen);
|
||||
ip_addr baddr = ipa_and(px, bmask);
|
||||
|
||||
/* Merge accept masks from children to get accept mask for node 'b' */
|
||||
ip_addr baccm = ipa_and(ipa_or(amask, n->accept), bmask);
|
||||
ip_addr baccm = ipa_and(ipa_or(amask, accept), bmask);
|
||||
|
||||
struct f_trie_node *a = new_node(t, plen, paddr, pmask, amask);
|
||||
struct f_trie_node *b = new_node(t, blen, baddr, bmask, baccm);
|
||||
attach_node(o, b);
|
||||
attach_node(b, n);
|
||||
attach_node(b, a);
|
||||
attach_node(o, b, v4);
|
||||
attach_node(b, n, v4);
|
||||
attach_node(b, a, v4);
|
||||
return a;
|
||||
}
|
||||
|
||||
if (plen < n->plen)
|
||||
if (plen < nlen)
|
||||
{
|
||||
/* We add new node 'a' between node 'o' and node 'n' */
|
||||
amask = ipa_or(amask, ipa_and(n->accept, pmask));
|
||||
amask = ipa_or(amask, ipa_and(accept, pmask));
|
||||
struct f_trie_node *a = new_node(t, plen, paddr, pmask, amask);
|
||||
attach_node(o, a);
|
||||
attach_node(a, n);
|
||||
attach_node(o, a, v4);
|
||||
attach_node(a, n, v4);
|
||||
return a;
|
||||
}
|
||||
|
||||
if (plen == n->plen)
|
||||
if (plen == nlen)
|
||||
{
|
||||
/* We already found added node in trie. Just update accept mask */
|
||||
n->accept = ipa_or(n->accept, amask);
|
||||
accept = ipa_or(accept, amask);
|
||||
SET_ADDR(n, accept, v4, accept);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Update accept mask part M2 and go deeper */
|
||||
n->accept = ipa_or(n->accept, ipa_and(amask, n->mask));
|
||||
accept = ipa_or(accept, ipa_and(amask, nmask));
|
||||
SET_ADDR(n, accept, v4, accept);
|
||||
|
||||
/* n->plen < plen and plen <= 32 (128) */
|
||||
o = n;
|
||||
n = n->c[ipa_getbit(paddr, n->plen) ? 1 : 0];
|
||||
n = GET_CHILD(n, c, v4, ipa_getbit(paddr, nlen) ? 1 : 0);
|
||||
}
|
||||
|
||||
/* We add new tail node 'a' after node 'o' */
|
||||
struct f_trie_node *a = new_node(t, plen, paddr, pmask, amask);
|
||||
attach_node(o, a);
|
||||
attach_node(o, a, v4);
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* trie_match_prefix
|
||||
* @t: trie
|
||||
* @px: prefix address
|
||||
* @plen: prefix length
|
||||
*
|
||||
* Tries to find a matching prefix pattern in the trie such that
|
||||
* prefix @px/@plen matches that prefix pattern. Returns 1 if there
|
||||
* is such prefix pattern in the trie.
|
||||
*/
|
||||
int
|
||||
trie_match_prefix(struct f_trie *t, ip_addr px, int plen)
|
||||
static int
|
||||
trie_match_net4(const struct f_trie *t, ip4_addr px, uint plen)
|
||||
{
|
||||
ip_addr pmask = ipa_mkmask(plen);
|
||||
ip_addr paddr = ipa_and(px, pmask);
|
||||
ip4_addr pmask = ip4_mkmask(plen);
|
||||
ip4_addr paddr = ip4_and(px, pmask);
|
||||
|
||||
if (plen == 0)
|
||||
return t->zero;
|
||||
|
||||
int plentest = plen - 1;
|
||||
struct f_trie_node *n = t->root;
|
||||
const struct f_trie_node4 *n = &t->root.v4;
|
||||
|
||||
while(n)
|
||||
{
|
||||
ip_addr cmask = ipa_and(n->mask, pmask);
|
||||
while (n)
|
||||
{
|
||||
ip4_addr cmask = ip4_and(n->mask, pmask);
|
||||
|
||||
/* We are out of path */
|
||||
if (ipa_compare(ipa_and(paddr, cmask), ipa_and(n->addr, cmask)))
|
||||
return 0;
|
||||
/* We are out of path */
|
||||
if (ip4_compare(ip4_and(paddr, cmask), ip4_and(n->addr, cmask)))
|
||||
return 0;
|
||||
|
||||
/* Check accept mask */
|
||||
if (ipa_getbit(n->accept, plentest))
|
||||
return 1;
|
||||
/* Check accept mask */
|
||||
if (ip4_getbit(n->accept, plentest))
|
||||
return 1;
|
||||
|
||||
/* We finished trie walk and still no match */
|
||||
if (plen <= n->plen)
|
||||
return 0;
|
||||
/* We finished trie walk and still no match */
|
||||
if (plen <= n->plen)
|
||||
return 0;
|
||||
|
||||
/* Choose children */
|
||||
n = n->c[(ipa_getbit(paddr, n->plen)) ? 1 : 0];
|
||||
}
|
||||
/* Choose children */
|
||||
n = n->c[(ip4_getbit(paddr, n->plen)) ? 1 : 0];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
trie_node_same(struct f_trie_node *t1, struct f_trie_node *t2)
|
||||
trie_match_net6(const struct f_trie *t, ip6_addr px, uint plen)
|
||||
{
|
||||
ip6_addr pmask = ip6_mkmask(plen);
|
||||
ip6_addr paddr = ip6_and(px, pmask);
|
||||
|
||||
if (plen == 0)
|
||||
return t->zero;
|
||||
|
||||
int plentest = plen - 1;
|
||||
const struct f_trie_node6 *n = &t->root.v6;
|
||||
|
||||
while (n)
|
||||
{
|
||||
ip6_addr cmask = ip6_and(n->mask, pmask);
|
||||
|
||||
/* We are out of path */
|
||||
if (ip6_compare(ip6_and(paddr, cmask), ip6_and(n->addr, cmask)))
|
||||
return 0;
|
||||
|
||||
/* Check accept mask */
|
||||
if (ip6_getbit(n->accept, plentest))
|
||||
return 1;
|
||||
|
||||
/* We finished trie walk and still no match */
|
||||
if (plen <= n->plen)
|
||||
return 0;
|
||||
|
||||
/* Choose children */
|
||||
n = n->c[(ip6_getbit(paddr, n->plen)) ? 1 : 0];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* trie_match_net
|
||||
* @t: trie
|
||||
* @n: net address
|
||||
*
|
||||
* Tries to find a matching net in the trie such that
|
||||
* prefix @n matches that prefix pattern. Returns 1 if there
|
||||
* is such prefix pattern in the trie.
|
||||
*/
|
||||
int
|
||||
trie_match_net(const struct f_trie *t, const net_addr *n)
|
||||
{
|
||||
switch (n->type)
|
||||
{
|
||||
case NET_IP4:
|
||||
case NET_VPN4:
|
||||
case NET_ROA4:
|
||||
return t->ipv4 ? trie_match_net4(t, net4_prefix(n), net_pxlen(n)) : 0;
|
||||
|
||||
case NET_IP6:
|
||||
case NET_VPN6:
|
||||
case NET_ROA6:
|
||||
return !t->ipv4 ? trie_match_net6(t, net6_prefix(n), net_pxlen(n)) : 0;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
trie_node_same4(const struct f_trie_node4 *t1, const struct f_trie_node4 *t2)
|
||||
{
|
||||
if ((t1 == NULL) && (t2 == NULL))
|
||||
return 1;
|
||||
@ -251,11 +388,28 @@ trie_node_same(struct f_trie_node *t1, struct f_trie_node *t2)
|
||||
return 0;
|
||||
|
||||
if ((t1->plen != t2->plen) ||
|
||||
(! ipa_equal(t1->addr, t2->addr)) ||
|
||||
(! ipa_equal(t1->accept, t2->accept)))
|
||||
(! ip4_equal(t1->addr, t2->addr)) ||
|
||||
(! ip4_equal(t1->accept, t2->accept)))
|
||||
return 0;
|
||||
|
||||
return trie_node_same(t1->c[0], t2->c[0]) && trie_node_same(t1->c[1], t2->c[1]);
|
||||
return trie_node_same4(t1->c[0], t2->c[0]) && trie_node_same4(t1->c[1], t2->c[1]);
|
||||
}
|
||||
|
||||
static int
|
||||
trie_node_same6(const struct f_trie_node6 *t1, const struct f_trie_node6 *t2)
|
||||
{
|
||||
if ((t1 == NULL) && (t2 == NULL))
|
||||
return 1;
|
||||
|
||||
if ((t1 == NULL) || (t2 == NULL))
|
||||
return 0;
|
||||
|
||||
if ((t1->plen != t2->plen) ||
|
||||
(! ip6_equal(t1->addr, t2->addr)) ||
|
||||
(! ip6_equal(t1->accept, t2->accept)))
|
||||
return 0;
|
||||
|
||||
return trie_node_same6(t1->c[0], t2->c[0]) && trie_node_same6(t1->c[1], t2->c[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -266,22 +420,41 @@ trie_node_same(struct f_trie_node *t1, struct f_trie_node *t2)
|
||||
* Compares two tries and returns 1 if they are same
|
||||
*/
|
||||
int
|
||||
trie_same(struct f_trie *t1, struct f_trie *t2)
|
||||
trie_same(const struct f_trie *t1, const struct f_trie *t2)
|
||||
{
|
||||
return (t1->zero == t2->zero) && trie_node_same(t1->root, t2->root);
|
||||
if ((t1->zero != t2->zero) || (t1->ipv4 != t2->ipv4))
|
||||
return 0;
|
||||
|
||||
if (t1->ipv4)
|
||||
return trie_node_same4(&t1->root.v4, &t2->root.v4);
|
||||
else
|
||||
return trie_node_same6(&t1->root.v6, &t2->root.v6);
|
||||
}
|
||||
|
||||
static void
|
||||
trie_node_format(struct f_trie_node *t, buffer *buf)
|
||||
trie_node_format4(const struct f_trie_node4 *t, buffer *buf)
|
||||
{
|
||||
if (t == NULL)
|
||||
return;
|
||||
|
||||
if (ipa_nonzero(t->accept))
|
||||
buffer_print(buf, "%I/%d{%I}, ", t->addr, t->plen, t->accept);
|
||||
if (ip4_nonzero(t->accept))
|
||||
buffer_print(buf, "%I4/%d{%I4}, ", t->addr, t->plen, t->accept);
|
||||
|
||||
trie_node_format(t->c[0], buf);
|
||||
trie_node_format(t->c[1], buf);
|
||||
trie_node_format4(t->c[0], buf);
|
||||
trie_node_format4(t->c[1], buf);
|
||||
}
|
||||
|
||||
static void
|
||||
trie_node_format6(const struct f_trie_node6 *t, buffer *buf)
|
||||
{
|
||||
if (t == NULL)
|
||||
return;
|
||||
|
||||
if (ip6_nonzero(t->accept))
|
||||
buffer_print(buf, "%I6/%d{%I6}, ", t->addr, t->plen, t->accept);
|
||||
|
||||
trie_node_format6(t->c[0], buf);
|
||||
trie_node_format6(t->c[1], buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -292,13 +465,17 @@ trie_node_format(struct f_trie_node *t, buffer *buf)
|
||||
* Prints the trie to the supplied buffer.
|
||||
*/
|
||||
void
|
||||
trie_format(struct f_trie *t, buffer *buf)
|
||||
trie_format(const struct f_trie *t, buffer *buf)
|
||||
{
|
||||
buffer_puts(buf, "[");
|
||||
|
||||
if (t->zero)
|
||||
buffer_print(buf, "%I/%d, ", IPA_NONE, 0);
|
||||
trie_node_format(t->root, buf);
|
||||
buffer_print(buf, "%I/%d, ", t->ipv4 ? IPA_NONE4 : IPA_NONE6, 0);
|
||||
|
||||
if (t->ipv4)
|
||||
trie_node_format4(&t->root.v4, buf);
|
||||
else
|
||||
trie_node_format6(&t->root.v6, buf);
|
||||
|
||||
if (buf->pos == buf->end)
|
||||
return;
|
||||
|
186
filter/trie_test.c
Normal file
186
filter/trie_test.c
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Filters: Utility Functions Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "test/bt-utils.h"
|
||||
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
#include "conf/conf.h"
|
||||
|
||||
#define TESTS_NUM 10
|
||||
#define PREFIXES_NUM 10
|
||||
#define PREFIX_TESTS_NUM 10000
|
||||
|
||||
#define BIG_BUFFER_SIZE 10000
|
||||
|
||||
/* Wrapping structure for storing f_prefixes structures in list */
|
||||
struct f_prefix_node {
|
||||
node n;
|
||||
struct f_prefix prefix;
|
||||
};
|
||||
|
||||
static u32
|
||||
xrandom(u32 max)
|
||||
{
|
||||
return (bt_random() % max);
|
||||
}
|
||||
|
||||
static int
|
||||
is_prefix_included(list *prefixes, struct f_prefix *needle)
|
||||
{
|
||||
struct f_prefix_node *n;
|
||||
WALK_LIST(n, *prefixes)
|
||||
{
|
||||
ip6_addr cmask = ip6_mkmask(MIN(n->prefix.net.pxlen, needle->net.pxlen));
|
||||
|
||||
ip6_addr ip = net6_prefix(&n->prefix.net);
|
||||
ip6_addr needle_ip = net6_prefix(&needle->net);
|
||||
|
||||
if ((ipa_compare(ipa_and(ip, cmask), ipa_and(needle_ip, cmask)) == 0) &&
|
||||
(n->prefix.lo <= needle->net.pxlen) && (needle->net.pxlen <= n->prefix.hi))
|
||||
{
|
||||
bt_debug("FOUND\t" PRIip6 "/%d %d-%d\n", ARGip6(net6_prefix(&n->prefix.net)), n->prefix.net.pxlen, n->prefix.lo, n->prefix.hi);
|
||||
return 1; /* OK */
|
||||
}
|
||||
}
|
||||
return 0; /* FAIL */
|
||||
}
|
||||
|
||||
static struct f_prefix
|
||||
get_random_ip6_prefix(void)
|
||||
{
|
||||
struct f_prefix p;
|
||||
u8 pxlen = xrandom(120)+8;
|
||||
ip6_addr ip6 = ip6_build(bt_random(),bt_random(),bt_random(),bt_random());
|
||||
net_addr_ip6 net6 = NET_ADDR_IP6(ip6, pxlen);
|
||||
|
||||
p.net = *((net_addr*) &net6);
|
||||
|
||||
if (bt_random() % 2)
|
||||
{
|
||||
p.lo = 0;
|
||||
p.hi = p.net.pxlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
p.lo = p.net.pxlen;
|
||||
p.hi = net_max_prefix_length[p.net.type];
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
static void
|
||||
generate_random_ipv6_prefixes(list *prefixes)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < PREFIXES_NUM; i++)
|
||||
{
|
||||
struct f_prefix f = get_random_ip6_prefix();
|
||||
|
||||
struct f_prefix_node *px = calloc(1, sizeof(struct f_prefix_node));
|
||||
px->prefix = f;
|
||||
|
||||
bt_debug("ADD\t" PRIip6 "/%d %d-%d\n", ARGip6(net6_prefix(&px->prefix.net)), px->prefix.net.pxlen, px->prefix.lo, px->prefix.hi);
|
||||
add_tail(prefixes, &px->n);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
t_match_net(void)
|
||||
{
|
||||
bt_bird_init();
|
||||
bt_config_parse(BT_CONFIG_SIMPLE);
|
||||
|
||||
uint round;
|
||||
for (round = 0; round < TESTS_NUM; round++)
|
||||
{
|
||||
list prefixes; /* of structs f_extended_prefix */
|
||||
init_list(&prefixes);
|
||||
struct f_trie *trie = f_new_trie(config->mem, 0);
|
||||
|
||||
generate_random_ipv6_prefixes(&prefixes);
|
||||
struct f_prefix_node *n;
|
||||
WALK_LIST(n, prefixes)
|
||||
{
|
||||
trie_add_prefix(trie, &n->prefix.net, n->prefix.lo, n->prefix.hi);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < PREFIX_TESTS_NUM; i++)
|
||||
{
|
||||
struct f_prefix f = get_random_ip6_prefix();
|
||||
bt_debug("TEST\t" PRIip6 "/%d\n", ARGip6(net6_prefix(&f.net)), f.net.pxlen);
|
||||
|
||||
int should_be = is_prefix_included(&prefixes, &f);
|
||||
int is_there = trie_match_net(trie, &f.net);
|
||||
bt_assert_msg(should_be == is_there, "Prefix " PRIip6 "/%d %s", ARGip6(net6_prefix(&f.net)), f.net.pxlen, (should_be ? "should be found in trie" : "should not be found in trie"));
|
||||
}
|
||||
|
||||
struct f_prefix_node *nxt;
|
||||
WALK_LIST_DELSAFE(n, nxt, prefixes)
|
||||
{
|
||||
free(n);
|
||||
}
|
||||
}
|
||||
|
||||
bt_bird_cleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_trie_same(void)
|
||||
{
|
||||
bt_bird_init();
|
||||
bt_config_parse(BT_CONFIG_SIMPLE);
|
||||
|
||||
int round;
|
||||
for (round = 0; round < TESTS_NUM*4; round++)
|
||||
{
|
||||
struct f_trie * trie1 = f_new_trie(config->mem, 0);
|
||||
struct f_trie * trie2 = f_new_trie(config->mem, 0);
|
||||
|
||||
list prefixes; /* a list of f_extended_prefix structures */
|
||||
init_list(&prefixes);
|
||||
int i;
|
||||
for (i = 0; i < 100; i++)
|
||||
generate_random_ipv6_prefixes(&prefixes);
|
||||
|
||||
struct f_prefix_node *n;
|
||||
WALK_LIST(n, prefixes)
|
||||
{
|
||||
trie_add_prefix(trie1, &n->prefix.net, n->prefix.lo, n->prefix.hi);
|
||||
}
|
||||
WALK_LIST_BACKWARDS(n, prefixes)
|
||||
{
|
||||
trie_add_prefix(trie2, &n->prefix.net, n->prefix.lo, n->prefix.hi);
|
||||
}
|
||||
|
||||
bt_assert(trie_same(trie1, trie2));
|
||||
|
||||
struct f_prefix_node *nxt;
|
||||
WALK_LIST_DELSAFE(n, nxt, prefixes)
|
||||
{
|
||||
free(n);
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_match_net, "Testing random prefix matching");
|
||||
bt_test_suite(t_trie_same, "A trie filled forward should be same with a trie filled backward.");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
1
lib/Doc
1
lib/Doc
@ -3,6 +3,7 @@ S ip.c
|
||||
S lists.c
|
||||
S checksum.c bitops.c patmatch.c printf.c xmalloc.c tbf.c
|
||||
S mac.c
|
||||
S flowspec.c
|
||||
D resource.sgml
|
||||
S resource.c
|
||||
S mempool.c
|
||||
|
7
lib/Makefile
Normal file
7
lib/Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
src := bitmap.c bitops.c checksum.c event.c flowspec.c idm.c ip.c lists.c mac.c md5.c mempool.c net.c patmatch.c printf.c resource.c sha1.c sha256.c sha512.c slab.c slists.c strtoul.c tbf.c timer.c xmalloc.c
|
||||
obj := $(src-o-files)
|
||||
$(all-daemon)
|
||||
|
||||
tests_src := bitmap_test.c heap_test.c buffer_test.c event_test.c flowspec_test.c bitops_test.c patmatch_test.c fletcher16_test.c slist_test.c checksum_test.c lists_test.c mac_test.c ip_test.c hash_test.c printf_test.c
|
||||
tests_targets := $(tests_targets) $(tests-target-files)
|
||||
tests_objs := $(tests_objs) $(src-o-files)
|
35
lib/Modules
35
lib/Modules
@ -1,35 +0,0 @@
|
||||
sha256.c
|
||||
sha256.h
|
||||
sha512.c
|
||||
sha512.h
|
||||
sha1.c
|
||||
sha1.h
|
||||
birdlib.h
|
||||
bitops.c
|
||||
bitops.h
|
||||
ip.h
|
||||
ip.c
|
||||
lists.c
|
||||
lists.h
|
||||
mac.c
|
||||
mac.h
|
||||
md5.c
|
||||
md5.h
|
||||
mempool.c
|
||||
resource.c
|
||||
resource.h
|
||||
slab.c
|
||||
socket.h
|
||||
tbf.c
|
||||
unaligned.h
|
||||
xmalloc.c
|
||||
printf.c
|
||||
string.h
|
||||
patmatch.c
|
||||
slists.c
|
||||
slists.h
|
||||
event.c
|
||||
event.h
|
||||
checksum.c
|
||||
checksum.h
|
||||
alloca.h
|
@ -15,4 +15,6 @@
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#define allocz(len) ({ void *_x = alloca(len); memset(_x, 0, len); _x; })
|
||||
|
||||
#endif
|
||||
|
@ -9,8 +9,7 @@
|
||||
#ifndef _BIRD_BIRDLIB_H_
|
||||
#define _BIRD_BIRDLIB_H_
|
||||
|
||||
#include "timer.h"
|
||||
#include "alloca.h"
|
||||
#include "lib/alloca.h"
|
||||
|
||||
/* Ugly structure offset handling macros */
|
||||
|
||||
@ -39,7 +38,13 @@ struct align_probe { char x; long int y; };
|
||||
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
|
||||
#define BYTES(n) ((((uint) (n)) + 7) / 8)
|
||||
#define CALL(fn, args...) ({ if (fn) fn(args); })
|
||||
#define ADVANCE(w, r, l) ({ r -= l; w += l; })
|
||||
#define ADVANCE(w, r, l) ({ r -= (l); w += (l); })
|
||||
|
||||
static inline int uint_cmp(uint i1, uint i2)
|
||||
{ return (int)(i1 > i2) - (int)(i1 < i2); }
|
||||
|
||||
static inline int u64_cmp(u64 i1, u64 i2)
|
||||
{ return (int)(i1 > i2) - (int)(i1 < i2); }
|
||||
|
||||
|
||||
/* Bitfield macros */
|
||||
@ -51,38 +56,36 @@ struct align_probe { char x; long int y; };
|
||||
#define BIT32_CLR(b,p) ((b)[(p)/32] &= ~BIT32_VAL(p))
|
||||
#define BIT32_ZERO(b,l) memset((b), 0, (l)/8)
|
||||
|
||||
/* The same, but counting bits from MSB */
|
||||
#define BIT32R_VAL(p) ((((u32) 1) << 31) >> ((p) % 32))
|
||||
#define BIT32R_TEST(b,p) ((b)[(p)/32] & BIT32R_VAL(p))
|
||||
#define BIT32R_SET(b,p) ((b)[(p)/32] |= BIT32R_VAL(p))
|
||||
#define BIT32R_CLR(b,p) ((b)[(p)/32] &= ~BIT32R_VAL(p))
|
||||
#define BIT32R_ZERO(b,l) memset((b), 0, (l)/8)
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void *) 0)
|
||||
#endif
|
||||
|
||||
#ifndef IPV6
|
||||
#define IP_VERSION 4
|
||||
#else
|
||||
#define IP_VERSION 6
|
||||
#endif
|
||||
|
||||
|
||||
/* Macros for gcc attributes */
|
||||
|
||||
#define NORET __attribute__((noreturn))
|
||||
#define UNUSED __attribute__((unused))
|
||||
#define PACKED __attribute__((packed))
|
||||
#define NONNULL(...) __attribute__((nonnull((__VA_ARGS__))))
|
||||
|
||||
#ifdef IPV6
|
||||
#define UNUSED4
|
||||
#define UNUSED6 UNUSED
|
||||
#else
|
||||
#define UNUSED4 UNUSED
|
||||
#define UNUSED6
|
||||
#ifndef HAVE_THREAD_LOCAL
|
||||
#define _Thread_local
|
||||
#endif
|
||||
|
||||
/* Microsecond time */
|
||||
|
||||
typedef s64 btime;
|
||||
//typedef s64 bird_clock_t;
|
||||
|
||||
#define S_ *1000000
|
||||
#define MS_ *1000
|
||||
#define US_ *1
|
||||
#define S_ * (btime) 1000000
|
||||
#define MS_ * (btime) 1000
|
||||
#define US_ * (btime) 1
|
||||
#define TO_S /1000000
|
||||
#define TO_MS /1000
|
||||
#define TO_US /1
|
||||
@ -91,39 +94,26 @@ typedef s64 btime;
|
||||
#define S S_
|
||||
#define MS MS_
|
||||
#define US US_
|
||||
#define NS /1000
|
||||
#endif
|
||||
|
||||
#define TIME_INFINITY ((s64) 0x7fffffffffffffff)
|
||||
|
||||
|
||||
/* Rate limiting */
|
||||
|
||||
struct tbf {
|
||||
bird_clock_t timestamp; /* Last update */
|
||||
u16 count; /* Available tokens */
|
||||
btime timestamp; /* Last update */
|
||||
u64 count; /* Available micro-tokens */
|
||||
u16 burst; /* Max number of tokens */
|
||||
u16 rate; /* Rate of replenishment */
|
||||
u16 mark; /* Whether last op was limited */
|
||||
u16 rate; /* Rate of replenishment (tokens / sec) */
|
||||
u32 drop; /* Number of failed request since last successful */
|
||||
};
|
||||
|
||||
/* Default TBF values for rate limiting log messages */
|
||||
#define TBF_DEFAULT_LOG_LIMITS { .rate = 1, .burst = 5 }
|
||||
|
||||
void tbf_update(struct tbf *f);
|
||||
|
||||
static inline int
|
||||
tbf_limit(struct tbf *f)
|
||||
{
|
||||
tbf_update(f);
|
||||
|
||||
if (!f->count)
|
||||
{
|
||||
f->mark = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
f->count--;
|
||||
f->mark = 0;
|
||||
return 0;
|
||||
}
|
||||
int tbf_limit(struct tbf *f);
|
||||
|
||||
|
||||
/* Logging and dying */
|
||||
@ -163,7 +153,7 @@ void bug(const char *msg, ...) NORET;
|
||||
#define L_FATAL "\010" /* Fatal errors */
|
||||
#define L_BUG "\011" /* BIRD bugs */
|
||||
|
||||
void debug(const char *msg, ...); /* Printf to debug output */
|
||||
void debug(const char *msg, ...); /* Printf to debug output */
|
||||
|
||||
/* Debugging */
|
||||
|
||||
@ -173,10 +163,30 @@ void debug(const char *msg, ...); /* Printf to debug output */
|
||||
#define DBG(x, y...) do { } while(0)
|
||||
#endif
|
||||
|
||||
#define ASSERT_DIE(x) do { if (!(x)) bug("Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
|
||||
|
||||
#define EXPENSIVE_CHECK(x) /* intentionally left blank */
|
||||
|
||||
#ifdef DEBUGGING
|
||||
#define ASSERT(x) do { if (!(x)) bug("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
|
||||
#define ASSERT(x) ASSERT_DIE(x)
|
||||
#define ASSUME(x) ASSERT_DIE(x)
|
||||
#ifdef ENABLE_EXPENSIVE_CHECKS
|
||||
#undef EXPENSIVE_CHECK
|
||||
#define EXPENSIVE_CHECK(x) ASSERT_DIE(x)
|
||||
#endif
|
||||
#else
|
||||
#define ASSERT(x) do { } while(0)
|
||||
#define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
|
||||
#define ASSUME(x) /* intentionally left blank */
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef DEBUGGING
|
||||
asm(
|
||||
".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
|
||||
".byte 1\n" /* Python */
|
||||
".asciz \"bird-gdb.py\"\n"
|
||||
".popsection\n"
|
||||
);
|
||||
#endif
|
||||
|
||||
/* Pseudorandom numbers */
|
||||
|
197
lib/bitmap.c
Normal file
197
lib/bitmap.c
Normal file
@ -0,0 +1,197 @@
|
||||
/*
|
||||
* BIRD Library -- Bitmaps
|
||||
*
|
||||
* (c) 2019 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2019 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/bitmap.h"
|
||||
#include "lib/bitops.h"
|
||||
#include "lib/resource.h"
|
||||
|
||||
|
||||
/*
|
||||
* Basic bitmap
|
||||
*/
|
||||
|
||||
void
|
||||
bmap_init(struct bmap *b, pool *p, uint size)
|
||||
{
|
||||
b->size = BIRD_ALIGN(size, 4);
|
||||
b->data = mb_allocz(p, b->size);
|
||||
}
|
||||
|
||||
void
|
||||
bmap_reset(struct bmap *b, uint size)
|
||||
{
|
||||
b->size = BIRD_ALIGN(size, 4);
|
||||
memset(b->data, 0, b->size);
|
||||
}
|
||||
|
||||
void
|
||||
bmap_grow(struct bmap *b, uint need)
|
||||
{
|
||||
uint size = b->size * 2;
|
||||
while (size < need)
|
||||
size *= 2;
|
||||
|
||||
uint old_size = b->size;
|
||||
b->size = size;
|
||||
b->data = mb_realloc(b->data, b->size);
|
||||
|
||||
ASSERT(size >= old_size);
|
||||
memset(b->data + (old_size / 4), 0, size - old_size);
|
||||
}
|
||||
|
||||
void
|
||||
bmap_free(struct bmap *b)
|
||||
{
|
||||
mb_free(b->data);
|
||||
b->size = 0;
|
||||
b->data = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Hierarchical bitmap
|
||||
*/
|
||||
|
||||
#define B256_SIZE(b) BIRD_ALIGN(b, 32)
|
||||
#define B256_STEP(b) (BIRD_ALIGN(b, 8192) >> 8)
|
||||
|
||||
void
|
||||
hmap_init(struct hmap *b, pool *p, uint size)
|
||||
{
|
||||
b->size[0] = B256_SIZE(size);
|
||||
b->size[1] = B256_STEP(b->size[0]);
|
||||
b->size[2] = B256_STEP(b->size[1]);
|
||||
b->size[3] = sizeof(b->root);
|
||||
|
||||
b->data[0] = mb_allocz(p, b->size[0]);
|
||||
b->data[1] = mb_allocz(p, b->size[1]);
|
||||
b->data[2] = mb_allocz(p, b->size[2]);
|
||||
b->data[3] = b->root;
|
||||
|
||||
memset(b->root, 0, sizeof(b->root));
|
||||
}
|
||||
|
||||
static void
|
||||
hmap_grow(struct hmap *b, uint need)
|
||||
{
|
||||
uint size = b->size[0] * 2;
|
||||
while (size < need)
|
||||
size *= 2;
|
||||
|
||||
for (uint i = 0; i < 3; i++)
|
||||
{
|
||||
uint old_size = b->size[i];
|
||||
b->size[i] = size;
|
||||
b->data[i] = mb_realloc(b->data[i], b->size[i]);
|
||||
|
||||
ASSERT(size >= old_size);
|
||||
memset(b->data[i] + (old_size / 4), 0, size - old_size);
|
||||
|
||||
size = B256_STEP(size);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
hmap_free(struct hmap *b)
|
||||
{
|
||||
mb_free(b->data[0]);
|
||||
mb_free(b->data[1]);
|
||||
mb_free(b->data[2]);
|
||||
|
||||
memset(b, 0, sizeof(struct hmap));
|
||||
}
|
||||
|
||||
static inline int
|
||||
b256_and(u32 *p)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
if (~p[i])
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
hmap_set(struct hmap *b, uint n)
|
||||
{
|
||||
if (n >= hmap_max(b))
|
||||
hmap_grow(b, n/8 + 1);
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
BIT32_SET(b->data[i], n);
|
||||
n = n >> 8;
|
||||
|
||||
/* Continue if all bits in 256-bit block are set */
|
||||
if (! b256_and(b->data[i] + 8*n))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
hmap_clear(struct hmap *b, uint n)
|
||||
{
|
||||
if (n >= hmap_max(b))
|
||||
return;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
BIT32_CLR(b->data[i], n);
|
||||
n = n >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int
|
||||
b256_first_zero(u32 *p)
|
||||
{
|
||||
for (int i = 0; i < 8; i++)
|
||||
if (~p[i])
|
||||
return 32*i + u32_ctz(~p[i]);
|
||||
|
||||
return 256;
|
||||
}
|
||||
|
||||
u32
|
||||
hmap_first_zero(struct hmap *b)
|
||||
{
|
||||
u32 n = 0;
|
||||
|
||||
for (int i = 3; i >= 0; i--)
|
||||
{
|
||||
if (32*n >= b->size[i])
|
||||
return hmap_max(b);
|
||||
|
||||
u32 *p = b->data[i] + 8*n;
|
||||
|
||||
n = (n << 8) + b256_first_zero(p);
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void
|
||||
hmap_check(struct hmap *b)
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
int max = b->size[i] / 32;
|
||||
|
||||
for (int j = 0; j < max; j++)
|
||||
{
|
||||
int x = b256_and(b->data[i] + 8*j);
|
||||
int y = !!BIT32_TEST(b->data[i+1], j);
|
||||
if (x != y)
|
||||
bug("Inconsistent data on %d:%d (%d vs %d)", i, j, x, y);
|
||||
}
|
||||
}
|
||||
}
|
63
lib/bitmap.h
Normal file
63
lib/bitmap.h
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* BIRD Library -- Bitmaps
|
||||
*
|
||||
* (c) 2019 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2019 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_BITMAP_H_
|
||||
#define _BIRD_BITMAP_H_
|
||||
|
||||
struct bmap
|
||||
{
|
||||
u32 size;
|
||||
u32 *data;
|
||||
};
|
||||
|
||||
void bmap_init(struct bmap *b, pool *p, uint size);
|
||||
void bmap_reset(struct bmap *b, uint size);
|
||||
void bmap_grow(struct bmap *b, uint need);
|
||||
void bmap_free(struct bmap *b);
|
||||
|
||||
static inline uint bmap_max(struct bmap *b)
|
||||
{ return 8 * b->size; }
|
||||
|
||||
static inline int bmap_test(struct bmap *b, uint n)
|
||||
{ return (n < bmap_max(b)) && BIT32_TEST(b->data, n); }
|
||||
|
||||
static inline void bmap_set(struct bmap *b, uint n)
|
||||
{
|
||||
if (n >= bmap_max(b)) bmap_grow(b, n/8 + 1);
|
||||
BIT32_SET(b->data, n);
|
||||
}
|
||||
|
||||
static inline void bmap_clear(struct bmap *b, uint n)
|
||||
{
|
||||
if (n >= bmap_max(b)) return;
|
||||
BIT32_CLR(b->data, n);
|
||||
}
|
||||
|
||||
|
||||
struct hmap
|
||||
{
|
||||
u32 size[4];
|
||||
u32 *data[4];
|
||||
u32 root[8];
|
||||
};
|
||||
|
||||
static inline uint hmap_max(struct hmap *b)
|
||||
{ return 8 * b->size[0]; }
|
||||
|
||||
static inline int hmap_test(struct hmap *b, uint n)
|
||||
{ return (n < hmap_max(b)) && BIT32_TEST(b->data[0], n); }
|
||||
|
||||
void hmap_init(struct hmap *b, pool *p, uint size);
|
||||
void hmap_free(struct hmap *b);
|
||||
void hmap_set(struct hmap *b, uint n);
|
||||
void hmap_clear(struct hmap *b, uint n);
|
||||
u32 hmap_first_zero(struct hmap *b);
|
||||
void hmap_check(struct hmap *b);
|
||||
|
||||
#endif
|
186
lib/bitmap_test.c
Normal file
186
lib/bitmap_test.c
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* BIRD Library -- Bitmap Tests
|
||||
*
|
||||
* (c) 2019 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2019 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "sysdep/config.h"
|
||||
#include "lib/bitmap.h"
|
||||
|
||||
#define MAX_NUM (1 << 20)
|
||||
#define MAX_SET (1 << 19)
|
||||
#define MAX_CLR (1 << 17)
|
||||
|
||||
#define STEP_NUM 1000
|
||||
#define STEP_SET 1000
|
||||
#define STEP_CLR 500
|
||||
|
||||
static int
|
||||
t_bmap_set_clear_random(void)
|
||||
{
|
||||
struct bmap b;
|
||||
|
||||
resource_init();
|
||||
bmap_init(&b, &root_pool, 1024);
|
||||
|
||||
char expected[MAX_NUM] = {};
|
||||
uint i, n;
|
||||
|
||||
for (i = 0; i < MAX_SET; i++)
|
||||
{
|
||||
do n = bt_random() % MAX_NUM;
|
||||
while (expected[n]);
|
||||
|
||||
bmap_set(&b, n);
|
||||
expected[n] = 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_CLR; i++)
|
||||
{
|
||||
do n = bt_random() % MAX_NUM;
|
||||
while (!expected[n]);
|
||||
|
||||
bmap_clear(&b, n);
|
||||
expected[n] = 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
if (bmap_test(&b, i) != expected[i])
|
||||
bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, bmap_test(&b, i), expected[i]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_hmap_set_clear_random(void)
|
||||
{
|
||||
struct hmap b;
|
||||
|
||||
resource_init();
|
||||
hmap_init(&b, &root_pool, 1024);
|
||||
|
||||
char expected[MAX_NUM] = {};
|
||||
uint i, n;
|
||||
|
||||
for (i = 0; i < MAX_SET; i++)
|
||||
{
|
||||
do n = bt_random() % MAX_NUM;
|
||||
while (expected[n]);
|
||||
|
||||
hmap_set(&b, n);
|
||||
expected[n] = 1;
|
||||
}
|
||||
|
||||
hmap_check(&b);
|
||||
|
||||
for (i = 0; i < MAX_CLR; i++)
|
||||
{
|
||||
do n = bt_random() % MAX_NUM;
|
||||
while (!expected[n]);
|
||||
|
||||
hmap_clear(&b, n);
|
||||
expected[n] = 0;
|
||||
}
|
||||
|
||||
hmap_check(&b);
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
if (hmap_test(&b, i) != expected[i])
|
||||
bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, hmap_test(&b, i), expected[i]);
|
||||
|
||||
for (i = 0; 1; i++)
|
||||
{
|
||||
n = hmap_first_zero(&b);
|
||||
bt_assert(n >= i);
|
||||
bt_assert(n <= MAX_NUM);
|
||||
|
||||
for (; i < n; i++)
|
||||
bt_assert(expected[i]);
|
||||
|
||||
if (n == MAX_NUM)
|
||||
break;
|
||||
|
||||
bt_assert(!expected[i]);
|
||||
|
||||
hmap_set(&b, n);
|
||||
}
|
||||
|
||||
hmap_check(&b);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_hmap_set_clear_fill(void)
|
||||
{
|
||||
struct hmap b;
|
||||
|
||||
resource_init();
|
||||
hmap_init(&b, &root_pool, 1024);
|
||||
|
||||
char expected[MAX_NUM] = {};
|
||||
uint i, j, n, max = 0;
|
||||
|
||||
for (i = 0; i < STEP_NUM; i++)
|
||||
{
|
||||
uint last = 0;
|
||||
uint step_set = bt_random() % STEP_SET;
|
||||
uint step_clr = bt_random() % STEP_CLR;
|
||||
|
||||
for (j = 0; j < step_set; j++)
|
||||
{
|
||||
n = hmap_first_zero(&b);
|
||||
bt_assert(n > last || !last);
|
||||
bt_assert(n < MAX_NUM);
|
||||
|
||||
if (!last)
|
||||
last = n;
|
||||
|
||||
for (; last < n; last++)
|
||||
bt_assert(expected[last]);
|
||||
|
||||
bt_assert(!expected[n]);
|
||||
|
||||
hmap_set(&b, n);
|
||||
expected[n] = 1;
|
||||
max = MAX(max, n);
|
||||
}
|
||||
|
||||
for (j = 0; j < step_clr; j++)
|
||||
{
|
||||
uint k = 0;
|
||||
do n = bt_random() % max;
|
||||
while (!expected[n] && (k++ < 8));
|
||||
|
||||
if (!expected[n])
|
||||
continue;
|
||||
|
||||
hmap_clear(&b, n);
|
||||
expected[n] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
if (hmap_test(&b, i) != expected[i])
|
||||
bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, hmap_test(&b, i), expected[i]);
|
||||
|
||||
hmap_check(&b);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_bmap_set_clear_random, "BMap - random sequence of sets / clears");
|
||||
bt_test_suite(t_hmap_set_clear_random, "HMap - random sequence of sets / clears");
|
||||
bt_test_suite(t_hmap_set_clear_fill, "HMap - linear sets and random clears");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
@ -28,15 +28,15 @@ u32_mkmask(uint n)
|
||||
*
|
||||
* This function checks whether the given integer @x represents
|
||||
* a valid bit mask (binary representation contains first ones, then
|
||||
* zeroes) and returns the number of ones or -1 if the mask is invalid.
|
||||
* zeroes) and returns the number of ones or 255 if the mask is invalid.
|
||||
*/
|
||||
int
|
||||
uint
|
||||
u32_masklen(u32 x)
|
||||
{
|
||||
int l = 0;
|
||||
u32 n = ~x;
|
||||
|
||||
if (n & (n+1)) return -1;
|
||||
if (n & (n+1)) return 255;
|
||||
if (x & 0x0000ffff) { x &= 0x0000ffff; l += 16; }
|
||||
if (x & 0x00ff00ff) { x &= 0x00ff00ff; l += 8; }
|
||||
if (x & 0x0f0f0f0f) { x &= 0x0f0f0f0f; l += 4; }
|
||||
|
@ -9,6 +9,8 @@
|
||||
#ifndef _BIRD_BITOPTS_H_
|
||||
#define _BIRD_BITOPTS_H_
|
||||
|
||||
#include "sysdep/config.h"
|
||||
|
||||
/*
|
||||
* Bit mask operations:
|
||||
*
|
||||
@ -19,7 +21,7 @@
|
||||
*/
|
||||
|
||||
u32 u32_mkmask(uint n);
|
||||
int u32_masklen(u32 x);
|
||||
uint u32_masklen(u32 x);
|
||||
|
||||
u32 u32_log2(u32 v);
|
||||
|
||||
@ -27,4 +29,9 @@ static inline u32 u32_hash(u32 v) { return v * 2902958171u; }
|
||||
|
||||
static inline u8 u32_popcount(u32 v) { return __builtin_popcount(v); }
|
||||
|
||||
static inline int u32_clz(u32 v) { return __builtin_clz(v); }
|
||||
static inline int u32_ctz(u32 v) { return __builtin_ctz(v); }
|
||||
|
||||
static inline int uint_is_pow2(uint n) { return n && !(n & (n-1)); }
|
||||
|
||||
#endif
|
||||
|
123
lib/bitops_test.c
Normal file
123
lib/bitops_test.c
Normal file
@ -0,0 +1,123 @@
|
||||
/*
|
||||
* BIRD Library -- Generic Bit Operations Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "test/bt-utils.h" /* naive_pow() */
|
||||
|
||||
#include "lib/bitops.h"
|
||||
|
||||
#define MAX_NUM 1000
|
||||
#define CHECK_BIT(var,pos) ((var) & (u32)(1<<(pos)))
|
||||
|
||||
static int
|
||||
t_mkmask(void)
|
||||
{
|
||||
int i;
|
||||
u32 compute, expect;
|
||||
|
||||
bt_assert(u32_mkmask(0) == 0x00000000);
|
||||
for (i = 1; i <= 32; i++)
|
||||
{
|
||||
compute = u32_mkmask(i);
|
||||
expect = (u32) (0xffffffff << (32-i));
|
||||
bt_assert_msg(compute == expect, "u32_mkmask(%d) = 0x%08X, expected 0x%08X", i, compute, expect);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
u32_masklen_expected(u32 mask)
|
||||
{
|
||||
int j, expect = 0;
|
||||
|
||||
int valid = 0;
|
||||
for (j = 0; j <= 32; j++)
|
||||
if (mask == (j ? (0xffffffff << (32-j)) : 0)) /* Shifting 32-bit value by 32 bits is undefined behavior */
|
||||
valid = 1;
|
||||
|
||||
if (!valid && mask != 0)
|
||||
expect = 255;
|
||||
else
|
||||
for (j = 0; j <= 31; j++)
|
||||
if (CHECK_BIT(mask, (31-j)))
|
||||
expect = j+1;
|
||||
else
|
||||
break;
|
||||
return expect;
|
||||
}
|
||||
|
||||
static void
|
||||
check_mask(u32 mask)
|
||||
{
|
||||
int expected, masklen;
|
||||
|
||||
expected = u32_masklen_expected(mask);
|
||||
masklen = u32_masklen(mask);
|
||||
int ok = (expected == masklen);
|
||||
bt_debug("u32_masklen(Ox%08x) = %d, expected %d %s\n", mask, masklen, expected, ok ? "OK" : "FAIL!");
|
||||
bt_assert(ok);
|
||||
}
|
||||
|
||||
static int
|
||||
t_masklen(void)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
check_mask(0x82828282);
|
||||
check_mask(0x00000000);
|
||||
|
||||
for (i = 0; i <= 32; i++)
|
||||
check_mask(((u32) (i ? (0xffffffff << (32-i)) : 0)) & 0xffffffff); /* Shifting 32-bit value by 32 bits is undefined behavior */
|
||||
|
||||
for (i = 0; i <= MAX_NUM; i++)
|
||||
check_mask(bt_random());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
check_log2(u32 n)
|
||||
{
|
||||
u32 log = u32_log2(n);
|
||||
u32 low = bt_naive_pow(2, log);
|
||||
u32 high = bt_naive_pow(2, log+1);
|
||||
|
||||
bt_assert_msg(n >= low && n < high,
|
||||
"u32_log2(%u) = %u, %u should be in the range <%u, %u)",
|
||||
n, log, n, low, high);
|
||||
}
|
||||
|
||||
static int
|
||||
t_log2(void)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
for (i = 0; i < 31; i++)
|
||||
bt_assert(u32_log2(bt_naive_pow(2, i+1)) == i+1);
|
||||
|
||||
for (i = 1; i < MAX_NUM; i++)
|
||||
check_log2(i);
|
||||
|
||||
for (i = 1; i < MAX_NUM; i++)
|
||||
check_log2(((u32) bt_random()) % 0x0fffffff);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_mkmask, "u32_mkmask()");
|
||||
bt_test_suite(t_masklen, "u32_masklen()");
|
||||
bt_test_suite(t_log2, "u32_log2()");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
20
lib/buffer.h
20
lib/buffer.h
@ -13,10 +13,14 @@
|
||||
#include "lib/resource.h"
|
||||
#include "sysdep/config.h"
|
||||
|
||||
#define BUFFER(type) struct { type *data; uint used, size; }
|
||||
|
||||
#define BUFFER_(type) struct { type *data; uint used, size; }
|
||||
#define BUFFER_TYPE(v) typeof(* (v).data)
|
||||
#define BUFFER_SIZE(v) ((v).size * sizeof(* (v).data))
|
||||
|
||||
#ifndef PARSER
|
||||
#define BUFFER(type) BUFFER_(type)
|
||||
#endif
|
||||
|
||||
#define BUFFER_INIT(v,pool,isize) \
|
||||
({ \
|
||||
(v).used = 0; \
|
||||
@ -46,4 +50,16 @@
|
||||
|
||||
#define BUFFER_FLUSH(v) ({ (v).used = 0; })
|
||||
|
||||
#define BUFFER_EMPTY(v) ({ (v).used == 0; })
|
||||
|
||||
#define BUFFER_WALK(v,n) \
|
||||
for (BUFFER_TYPE(v) *_n = (v).data, n; _n < ((v).data + (v).used) && (n = *_n, 1); _n++)
|
||||
|
||||
#define BUFFER_SHALLOW_COPY(dst, src) \
|
||||
({ \
|
||||
(dst).used = (src).used; \
|
||||
(dst).size = (src).size; \
|
||||
(dst).data = (src).data; \
|
||||
})
|
||||
|
||||
#endif /* _BIRD_BUFFER_H_ */
|
||||
|
167
lib/buffer_test.c
Normal file
167
lib/buffer_test.c
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* BIRD Library -- Buffer Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/buffer.h"
|
||||
|
||||
#define MAX_NUM 33
|
||||
|
||||
typedef BUFFER(int) buffer_int;
|
||||
static int expected[MAX_NUM];
|
||||
static buffer_int buf;
|
||||
static struct pool *buffer_pool;
|
||||
|
||||
static void
|
||||
show_buf(buffer_int *b)
|
||||
{
|
||||
uint i;
|
||||
bt_debug(".used = %d, .size = %d\n", b->used, b->size);
|
||||
|
||||
for (i = 0; i < b->used; i++)
|
||||
bt_debug(" .data[%3u] = %-16d expected %-16d %s\n", i, b->data[i], expected[i], (b->data[i] == expected[i] ? "OK" : "FAIL!"));
|
||||
}
|
||||
|
||||
static void
|
||||
fill_expected_array(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
expected[i] = bt_random();
|
||||
}
|
||||
|
||||
static void
|
||||
init_buffer(void)
|
||||
{
|
||||
resource_init();
|
||||
buffer_pool = &root_pool;
|
||||
BUFFER_INIT(buf, buffer_pool, MAX_NUM);
|
||||
}
|
||||
|
||||
static int
|
||||
is_buffer_as_expected(buffer_int *b)
|
||||
{
|
||||
show_buf(b);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
bt_assert(b->data[i] == expected[i]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_buffer_push(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_buffer();
|
||||
fill_expected_array();
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i];
|
||||
is_buffer_as_expected(&buf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_buffer_pop(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_buffer();
|
||||
fill_expected_array();
|
||||
|
||||
/* POP a half of elements */
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i];
|
||||
for (i = MAX_NUM-1; i >= MAX_NUM/2; i--)
|
||||
BUFFER_POP(buf);
|
||||
for (i = MAX_NUM/2; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i] = bt_random();
|
||||
is_buffer_as_expected(&buf);
|
||||
|
||||
/* POP all of elements */
|
||||
for (i = MAX_NUM-1; i >= 0; i--)
|
||||
BUFFER_POP(buf);
|
||||
bt_assert(buf.used == 0);
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i];
|
||||
is_buffer_as_expected(&buf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_buffer_resize(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_buffer();
|
||||
BUFFER_INIT(buf, buffer_pool, 0);
|
||||
fill_expected_array();
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i];
|
||||
is_buffer_as_expected(&buf);
|
||||
bt_assert(buf.size >= MAX_NUM);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_buffer_flush(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_buffer();
|
||||
fill_expected_array();
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i];
|
||||
|
||||
BUFFER_FLUSH(buf);
|
||||
bt_assert(buf.used == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_buffer_walk(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_buffer();
|
||||
fill_expected_array();
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
BUFFER_PUSH(buf) = expected[i];
|
||||
|
||||
i = 0;
|
||||
BUFFER_WALK(buf, v)
|
||||
bt_assert(v == expected[i++]);
|
||||
|
||||
bt_assert(i == MAX_NUM);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_buffer_push, "Pushing new elements");
|
||||
bt_test_suite(t_buffer_pop, "Fill whole buffer (PUSH), a half of elements POP and PUSH new elements");
|
||||
bt_test_suite(t_buffer_resize, "Init a small buffer and try overfill");
|
||||
bt_test_suite(t_buffer_flush, "Fill and flush all elements");
|
||||
bt_test_suite(t_buffer_walk, "Fill and walk through buffer");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
94
lib/checksum_test.c
Normal file
94
lib/checksum_test.c
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* BIRD Library -- IP One-Complement Checksum Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/checksum.h"
|
||||
|
||||
#define MAX_NUM 10000
|
||||
|
||||
static u16
|
||||
ipsum_calculate_expected(u32 *a)
|
||||
{
|
||||
int i;
|
||||
u32 sum = 0;
|
||||
|
||||
for(i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
sum += a[i] & 0xffff;
|
||||
bt_debug("low) \t0x%08X \n", sum);
|
||||
|
||||
sum += a[i] >> 16;
|
||||
bt_debug("high) \t0x%08X \n", sum);
|
||||
|
||||
u16 carry = sum >> 16;
|
||||
sum = (sum & 0xffff) + carry;
|
||||
bt_debug("carry) \t0x%08X \n\n", sum);
|
||||
}
|
||||
bt_debug("sum) \t0x%08X \n", sum);
|
||||
|
||||
sum = sum ^ 0xffff;
|
||||
bt_debug("~sum) \t0x%08X \n", sum);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int
|
||||
t_calculate(void)
|
||||
{
|
||||
u32 a[MAX_NUM];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
a[i] = bt_random();
|
||||
|
||||
u16 sum_calculated = ipsum_calculate(a, sizeof(a), NULL);
|
||||
u16 sum_calculated_2 = ipsum_calculate(&a[0], sizeof(u32)*(MAX_NUM/2), &a[MAX_NUM/2], sizeof(u32)*(MAX_NUM - MAX_NUM/2), NULL);
|
||||
bt_assert(sum_calculated == sum_calculated_2);
|
||||
|
||||
u16 sum_expected = ipsum_calculate_expected(a);
|
||||
|
||||
bt_debug("sum_calculated: %08X \n", sum_calculated);
|
||||
bt_debug("sum_expected: %08X \n", sum_expected);
|
||||
|
||||
bt_assert(sum_calculated == sum_expected);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_verify(void)
|
||||
{
|
||||
u32 a[MAX_NUM+1];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
a[i] = bt_random();
|
||||
|
||||
u16 sum = ipsum_calculate_expected(a);
|
||||
|
||||
a[MAX_NUM] = sum;
|
||||
|
||||
bt_assert(ipsum_verify(a, sizeof(a), NULL));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_calculate, "Checksum of pseudo-random data");
|
||||
bt_test_suite(t_verify, "Verification of pseudo-random data.");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
56
lib/event.c
56
lib/event.c
@ -23,6 +23,7 @@
|
||||
#include "lib/event.h"
|
||||
|
||||
event_list global_event_list;
|
||||
event_list global_work_list;
|
||||
|
||||
inline void
|
||||
ev_postpone(event *e)
|
||||
@ -114,6 +115,22 @@ ev_schedule(event *e)
|
||||
ev_enqueue(&global_event_list, e);
|
||||
}
|
||||
|
||||
/**
|
||||
* ev_schedule_work - schedule a work-event.
|
||||
* @e: an event
|
||||
*
|
||||
* This function schedules an event by enqueueing it to a system-wide work-event
|
||||
* list which is run by the platform dependent code whenever appropriate. This
|
||||
* is designated for work-events instead of regular events. They are executed
|
||||
* less often in order to not clog I/O loop.
|
||||
*/
|
||||
void
|
||||
ev_schedule_work(event *e)
|
||||
{
|
||||
if (!ev_active(e))
|
||||
add_tail(&global_work_list, &e->n);
|
||||
}
|
||||
|
||||
void io_log_event(void *hook, void *data);
|
||||
|
||||
/**
|
||||
@ -136,10 +153,47 @@ ev_run_list(event_list *l)
|
||||
event *e = SKIP_BACK(event, n, n);
|
||||
|
||||
/* This is ugly hack, we want to log just events executed from the main I/O loop */
|
||||
if (l == &global_event_list)
|
||||
if ((l == &global_event_list) || (l == &global_work_list))
|
||||
io_log_event(e->hook, e->data);
|
||||
|
||||
ev_run(e);
|
||||
}
|
||||
|
||||
return !EMPTY_LIST(*l);
|
||||
}
|
||||
|
||||
int
|
||||
ev_run_list_limited(event_list *l, uint limit)
|
||||
{
|
||||
node *n;
|
||||
list tmp_list;
|
||||
|
||||
init_list(&tmp_list);
|
||||
add_tail_list(&tmp_list, l);
|
||||
init_list(l);
|
||||
|
||||
WALK_LIST_FIRST(n, tmp_list)
|
||||
{
|
||||
event *e = SKIP_BACK(event, n, n);
|
||||
|
||||
if (!limit)
|
||||
break;
|
||||
|
||||
/* This is ugly hack, we want to log just events executed from the main I/O loop */
|
||||
if ((l == &global_event_list) || (l == &global_work_list))
|
||||
io_log_event(e->hook, e->data);
|
||||
|
||||
ev_run(e);
|
||||
limit--;
|
||||
}
|
||||
|
||||
if (!EMPTY_LIST(tmp_list))
|
||||
{
|
||||
/* Attach new items after the unprocessed old items */
|
||||
add_tail_list(&tmp_list, l);
|
||||
init_list(l);
|
||||
add_tail_list(l, &tmp_list);
|
||||
}
|
||||
|
||||
return !EMPTY_LIST(*l);
|
||||
}
|
||||
|
@ -21,14 +21,17 @@ typedef struct event {
|
||||
typedef list event_list;
|
||||
|
||||
extern event_list global_event_list;
|
||||
extern event_list global_work_list;
|
||||
|
||||
event *ev_new(pool *);
|
||||
void ev_run(event *);
|
||||
#define ev_init_list(el) init_list(el)
|
||||
void ev_enqueue(event_list *, event *);
|
||||
void ev_schedule(event *);
|
||||
void ev_schedule_work(event *);
|
||||
void ev_postpone(event *);
|
||||
int ev_run_list(event_list *);
|
||||
int ev_run_list_limited(event_list *, uint);
|
||||
|
||||
static inline int
|
||||
ev_active(event *e)
|
||||
@ -37,7 +40,7 @@ ev_active(event *e)
|
||||
}
|
||||
|
||||
static inline event*
|
||||
ev_new_set(pool *p, void (*hook)(void *), void *data)
|
||||
ev_new_init(pool *p, void (*hook)(void *), void *data)
|
||||
{
|
||||
event *e = ev_new(p);
|
||||
e->hook = hook;
|
||||
|
89
lib/event_test.c
Normal file
89
lib/event_test.c
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* BIRD Library -- Event Processing Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/net.h"
|
||||
#include "lib/event.h"
|
||||
#include "conf/conf.h"
|
||||
#include "nest/locks.h"
|
||||
#include "sysdep/unix/unix.h"
|
||||
#include "nest/iface.h"
|
||||
#include "nest/route.h"
|
||||
|
||||
#define MAX_NUM 4
|
||||
|
||||
int event_check_points[MAX_NUM];
|
||||
|
||||
#define event_hook_body(num) \
|
||||
do { \
|
||||
bt_debug("Event Hook " #num "\n"); \
|
||||
event_check_points[num] = 1; \
|
||||
bt_assert_msg(event_check_points[num-1], "Events should be run in right order"); \
|
||||
} while (0)
|
||||
|
||||
static void event_hook_1(void *data UNUSED) { event_hook_body(1); }
|
||||
static void event_hook_2(void *data UNUSED) { event_hook_body(2); }
|
||||
static void event_hook_3(void *data UNUSED) { event_hook_body(3); }
|
||||
|
||||
#define schedule_event(num) \
|
||||
do { \
|
||||
struct event *event_##num = ev_new(&root_pool); \
|
||||
event_##num->hook = event_hook_##num; \
|
||||
ev_schedule(event_##num); \
|
||||
} while (0)
|
||||
|
||||
static void
|
||||
init_event_check_points(void)
|
||||
{
|
||||
int i;
|
||||
event_check_points[0] = 1;
|
||||
for (i = 1; i < MAX_NUM; i++)
|
||||
event_check_points[i] = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
t_ev_run_list(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
resource_init();
|
||||
olock_init();
|
||||
timer_init();
|
||||
io_init();
|
||||
rt_init();
|
||||
if_init();
|
||||
// roa_init();
|
||||
config_init();
|
||||
config = config_alloc("");
|
||||
|
||||
init_event_check_points();
|
||||
|
||||
schedule_event(1);
|
||||
schedule_event(2);
|
||||
schedule_event(3);
|
||||
|
||||
ev_run_list(&global_event_list);
|
||||
|
||||
for (i = 1; i < MAX_NUM; i++)
|
||||
bt_assert(event_check_points[i]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_ev_run_list, "Schedule and run 3 events in right order.");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
||||
|
180
lib/fletcher16_test.c
Normal file
180
lib/fletcher16_test.c
Normal file
@ -0,0 +1,180 @@
|
||||
/*
|
||||
* BIRD Library -- Fletcher-16 Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "lib/fletcher16.h"
|
||||
|
||||
static u16
|
||||
straightforward_fletcher16_compute(const char *data)
|
||||
{
|
||||
int count = strlen(data);
|
||||
|
||||
u16 sum1 = 0;
|
||||
u16 sum2 = 0;
|
||||
int index;
|
||||
|
||||
for (index = 0; index < count; ++index)
|
||||
{
|
||||
sum1 = (sum1 + data[index]) % 255;
|
||||
sum2 = (sum2 + sum1) % 255;
|
||||
}
|
||||
|
||||
sum2 = (sum2 + sum1) % 255;
|
||||
sum2 = (sum2 + sum1) % 255;
|
||||
|
||||
return (sum1 << 8) | sum2;
|
||||
}
|
||||
|
||||
static u16
|
||||
straightforward_fletcher16_checksum(const char *data)
|
||||
{
|
||||
u16 csum;
|
||||
u16 c0,c1,x,y;
|
||||
|
||||
csum = straightforward_fletcher16_compute(data);
|
||||
c0 = (csum >> 8) & 0xff;
|
||||
c1 = csum & 0xff;
|
||||
|
||||
x = (255 + c0 - c1) % 255;
|
||||
y = (510 - 2*c0 + c1) % 255;
|
||||
|
||||
if (!x) x = 255;
|
||||
if (!y) y = 255;
|
||||
|
||||
return (x << 8) | y;
|
||||
}
|
||||
|
||||
const u8 zero16[2] = {};
|
||||
|
||||
static int
|
||||
test_fletcher16(void *out_, const void *in_, const void *expected_out_)
|
||||
{
|
||||
u16 *out = out_;
|
||||
const char *in = in_;
|
||||
const u16 *expected_out = expected_out_;
|
||||
|
||||
struct fletcher16_context ctxt;
|
||||
|
||||
fletcher16_init(&ctxt);
|
||||
fletcher16_update(&ctxt, in, strlen(in));
|
||||
fletcher16_update(&ctxt, zero16, 2);
|
||||
*out = fletcher16_compute(&ctxt);
|
||||
|
||||
return *out == *expected_out;
|
||||
}
|
||||
|
||||
static int
|
||||
test_fletcher16_checksum(void *out_, const void *in_, const void *expected_out_)
|
||||
{
|
||||
u16 *out = out_;
|
||||
const char *in = in_;
|
||||
const u16 *expected_out = expected_out_;
|
||||
|
||||
struct fletcher16_context ctxt;
|
||||
int len = strlen(in);
|
||||
|
||||
fletcher16_init(&ctxt);
|
||||
fletcher16_update(&ctxt, in, len);
|
||||
fletcher16_update(&ctxt, zero16, 2);
|
||||
*out = fletcher16_final(&ctxt, len+2, len);
|
||||
|
||||
return *out == *expected_out;
|
||||
}
|
||||
|
||||
static int
|
||||
t_fletcher16_compute(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = "\001\002",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("\001\002") }),
|
||||
},
|
||||
{
|
||||
.in = "",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("") }),
|
||||
},
|
||||
{
|
||||
.in = "a",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("a") }),
|
||||
},
|
||||
{
|
||||
.in = "abcd",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("abcd") }),
|
||||
},
|
||||
{
|
||||
.in = "message digest",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("message digest") }),
|
||||
},
|
||||
{
|
||||
.in = "abcdefghijklmnopqrstuvwxyz",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("abcdefghijklmnopqrstuvwxyz") }),
|
||||
},
|
||||
{
|
||||
.in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
|
||||
},
|
||||
{
|
||||
.in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
|
||||
.out = & ((const u16) { straightforward_fletcher16_compute("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_fletcher16, bt_fmt_str, bt_fmt_unsigned);
|
||||
}
|
||||
|
||||
static int
|
||||
t_fletcher16_checksum(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = "\001\002",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("\001\002") }),
|
||||
},
|
||||
{
|
||||
.in = "",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("") }),
|
||||
},
|
||||
{
|
||||
.in = "a",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("a") }),
|
||||
},
|
||||
{
|
||||
.in = "abcd",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("abcd") }),
|
||||
},
|
||||
{
|
||||
.in = "message digest",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("message digest") }),
|
||||
},
|
||||
{
|
||||
.in = "abcdefghijklmnopqrstuvwxyz",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("abcdefghijklmnopqrstuvwxyz") }),
|
||||
},
|
||||
{
|
||||
.in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
|
||||
},
|
||||
{
|
||||
.in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
|
||||
.out = & ((const u16) { straightforward_fletcher16_checksum("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_fletcher16_checksum, bt_fmt_str, bt_fmt_unsigned);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_fletcher16_compute, "Fletcher-16 Compute Tests");
|
||||
bt_test_suite(t_fletcher16_checksum, "Fletcher-16 Checksum Tests");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
1211
lib/flowspec.c
Normal file
1211
lib/flowspec.c
Normal file
File diff suppressed because it is too large
Load Diff
163
lib/flowspec.h
Normal file
163
lib/flowspec.h
Normal file
@ -0,0 +1,163 @@
|
||||
/*
|
||||
* BIRD Library -- Flow specification (RFC 5575)
|
||||
*
|
||||
* (c) 2016 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_FLOWSPEC_H_
|
||||
#define _BIRD_FLOWSPEC_H_
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/buffer.h"
|
||||
#include "lib/net.h"
|
||||
|
||||
|
||||
/* Flow component operators */
|
||||
#define FLOW_OP_TRUE 0x00 /* 0b000 */
|
||||
#define FLOW_OP_EQ 0x01 /* 0b001 */
|
||||
#define FLOW_OP_GT 0x02 /* 0b010 */
|
||||
#define FLOW_OP_GEQ 0x03 /* 0b011 */
|
||||
#define FLOW_OP_LT 0x04 /* 0b100 */
|
||||
#define FLOW_OP_LEQ 0x05 /* 0b101 */
|
||||
#define FLOW_OP_NEQ 0x06 /* 0b110 */
|
||||
#define FLOW_OP_FALSE 0x07 /* 0b111 */
|
||||
|
||||
#define FLOW_OP_OR 0x00
|
||||
#define FLOW_OP_AND 0x40
|
||||
|
||||
|
||||
/* Types of components in flowspec */
|
||||
enum flow_type {
|
||||
FLOW_TYPE_DST_PREFIX = 1,
|
||||
FLOW_TYPE_SRC_PREFIX = 2,
|
||||
FLOW_TYPE_IP_PROTOCOL = 3,
|
||||
FLOW_TYPE_NEXT_HEADER = 3, /* IPv6 */
|
||||
FLOW_TYPE_PORT = 4,
|
||||
FLOW_TYPE_DST_PORT = 5,
|
||||
FLOW_TYPE_SRC_PORT = 6,
|
||||
FLOW_TYPE_ICMP_TYPE = 7,
|
||||
FLOW_TYPE_ICMP_CODE = 8,
|
||||
FLOW_TYPE_TCP_FLAGS = 9,
|
||||
FLOW_TYPE_PACKET_LENGTH = 10,
|
||||
FLOW_TYPE_DSCP = 11, /* DiffServ Code Point */
|
||||
FLOW_TYPE_FRAGMENT = 12,
|
||||
FLOW_TYPE_LABEL = 13, /* IPv6 */
|
||||
FLOW_TYPE_MAX
|
||||
};
|
||||
|
||||
const char *flow_type_str(enum flow_type type, int ipv6);
|
||||
|
||||
|
||||
/*
|
||||
* Length
|
||||
*/
|
||||
|
||||
uint flow_write_length(byte *data, u16 len);
|
||||
|
||||
static inline u16 flow_hdr_length(const byte *data)
|
||||
{ return ((*data & 0xf0) == 0xf0) ? 2 : 1; }
|
||||
|
||||
static inline u16 flow_read_length(const byte *data)
|
||||
{ return ((*data & 0xf0) == 0xf0) ? get_u16(data) & 0x0fff : *data; }
|
||||
|
||||
static inline u16 flow4_get_length(const net_addr_flow4 *f)
|
||||
{ return f->length - sizeof(net_addr_flow4); }
|
||||
|
||||
static inline u16 flow6_get_length(const net_addr_flow6 *f)
|
||||
{ return f->length - sizeof(net_addr_flow6); }
|
||||
|
||||
static inline void flow4_set_length(net_addr_flow4 *f, u16 len)
|
||||
{ f->length = sizeof(net_addr_flow4) + flow_write_length(f->data, len) + len; }
|
||||
|
||||
static inline void flow6_set_length(net_addr_flow6 *f, u16 len)
|
||||
{ f->length = sizeof(net_addr_flow6) + flow_write_length(f->data, len) + len; }
|
||||
|
||||
|
||||
/*
|
||||
* Iterators
|
||||
*/
|
||||
|
||||
const byte *flow4_first_part(const net_addr_flow4 *f);
|
||||
const byte *flow6_first_part(const net_addr_flow6 *f);
|
||||
const byte *flow4_next_part(const byte *pos, const byte *end);
|
||||
const byte *flow6_next_part(const byte *pos, const byte *end);
|
||||
const byte *flow4_get_part(const net_addr_flow4 *f, uint type);
|
||||
const byte *flow6_get_part(const net_addr_flow6 *f, uint type);
|
||||
|
||||
|
||||
/*
|
||||
* Flowspec accessors
|
||||
*/
|
||||
|
||||
ip4_addr flow_read_ip4_part(const byte *part);
|
||||
ip6_addr flow_read_ip6_part(const byte *part);
|
||||
static inline int flow_read_pxlen(const byte *part) { return part[1]; }
|
||||
|
||||
|
||||
/*
|
||||
* Flowspec Builder
|
||||
*/
|
||||
|
||||
/* A data structure for keep a state of flow builder */
|
||||
struct flow_builder {
|
||||
BUFFER_(byte) data;
|
||||
enum flow_type this_type;
|
||||
enum flow_type last_type;
|
||||
u16 last_op_offset; /* Position of last operator in data.data */
|
||||
int ipv6;
|
||||
struct {
|
||||
u16 offset; /* Beginning of a component */
|
||||
u16 length; /* Length of a component */
|
||||
} parts[FLOW_TYPE_MAX]; /* Indexing all components */
|
||||
};
|
||||
|
||||
struct flow_builder *flow_builder_init(pool *pool);
|
||||
void flow_builder_clear(struct flow_builder *fb);
|
||||
void flow_builder_set_type(struct flow_builder *fb, enum flow_type p);
|
||||
int flow_builder4_add_pfx(struct flow_builder *fb, const net_addr_ip4 *n4);
|
||||
int flow_builder6_add_pfx(struct flow_builder *fb, const net_addr_ip6 *n6, u32 offset);
|
||||
int flow_builder_add_op_val(struct flow_builder *fb, byte op, u32 value);
|
||||
int flow_builder_add_val_mask(struct flow_builder *fb, byte op, u32 value, u32 mask);
|
||||
net_addr_flow4 *flow_builder4_finalize(struct flow_builder *fb, linpool *lpool);
|
||||
net_addr_flow6 *flow_builder6_finalize(struct flow_builder *fb, linpool *lpool);
|
||||
|
||||
|
||||
/*
|
||||
* Validation
|
||||
*/
|
||||
|
||||
/* Results of validation Flow specification */
|
||||
enum flow_validated_state {
|
||||
FLOW_ST_UNKNOWN_COMPONENT,
|
||||
FLOW_ST_VALID,
|
||||
FLOW_ST_NOT_COMPLETE,
|
||||
FLOW_ST_EXCEED_MAX_PREFIX_LENGTH,
|
||||
FLOW_ST_EXCEED_MAX_PREFIX_OFFSET,
|
||||
FLOW_ST_EXCEED_MAX_VALUE_LENGTH,
|
||||
FLOW_ST_BAD_TYPE_ORDER,
|
||||
FLOW_ST_AND_BIT_SHOULD_BE_UNSET,
|
||||
FLOW_ST_ZERO_BIT_SHOULD_BE_UNSED,
|
||||
FLOW_ST_DEST_PREFIX_REQUIRED,
|
||||
FLOW_ST_INVALID_TCP_FLAGS,
|
||||
FLOW_ST_CANNOT_USE_DONT_FRAGMENT
|
||||
};
|
||||
|
||||
const char *flow_validated_state_str(enum flow_validated_state code);
|
||||
enum flow_validated_state flow4_validate(const byte *nlri, uint len);
|
||||
enum flow_validated_state flow6_validate(const byte *nlri, uint len);
|
||||
void flow_check_cf_value_length(struct flow_builder *fb, u32 expr);
|
||||
void flow_check_cf_bmk_values(struct flow_builder *fb, u8 neg, u32 val, u32 mask);
|
||||
void flow4_validate_cf(net_addr_flow4 *f);
|
||||
void flow6_validate_cf(net_addr_flow6 *f);
|
||||
|
||||
|
||||
/*
|
||||
* Net Formatting
|
||||
*/
|
||||
|
||||
uint flow4_net_format(char *buf, uint blen, const net_addr_flow4 *f);
|
||||
uint flow6_net_format(char *buf, uint blen, const net_addr_flow6 *f);
|
||||
|
||||
#endif /* _BIRD_FLOWSPEC_H_ */
|
689
lib/flowspec_test.c
Normal file
689
lib/flowspec_test.c
Normal file
@ -0,0 +1,689 @@
|
||||
/*
|
||||
* BIRD Library -- Flow specification (RFC 5575) Tests
|
||||
*
|
||||
* (c) 2016 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "lib/flowspec.h"
|
||||
|
||||
#define NET_ADDR_FLOW4_(what,prefix,pxlen,data_) \
|
||||
do \
|
||||
{ \
|
||||
what = alloca(sizeof(net_addr_flow4) + 128); \
|
||||
*what = NET_ADDR_FLOW4(prefix, pxlen, sizeof(data_)); \
|
||||
memcpy(what->data, &(data_), sizeof(data_)); \
|
||||
} while(0)
|
||||
|
||||
#define NET_ADDR_FLOW6_(what,prefix,pxlen,data_) \
|
||||
do \
|
||||
{ \
|
||||
what = alloca(sizeof(net_addr_flow6) + 128); \
|
||||
*what = NET_ADDR_FLOW6(prefix, pxlen, sizeof(data_)); \
|
||||
memcpy(what->data, &(data_), sizeof(data_)); \
|
||||
} while(0)
|
||||
|
||||
static int
|
||||
t_read_length(void)
|
||||
{
|
||||
byte data[] = { 0xcc, 0xcc, 0xcc };
|
||||
|
||||
for (uint expect = 0; expect < 0xf0; expect++)
|
||||
{
|
||||
*data = expect;
|
||||
uint get = flow_read_length(data);
|
||||
bt_assert_msg(get == expect, "Testing get length 0x%02x (get 0x%02x)", expect, get);
|
||||
}
|
||||
|
||||
for (uint expect = 0; expect <= 0xfff; expect++)
|
||||
{
|
||||
put_u16(data, expect | 0xf000);
|
||||
uint get = flow_read_length(data);
|
||||
bt_assert_msg(get == expect, "Testing get length 0x%03x (get 0x%03x)", expect, get);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_write_length(void)
|
||||
{
|
||||
byte data[] = { 0xcc, 0xcc, 0xcc };
|
||||
|
||||
for (uint expect = 0; expect <= 0xfff; expect++)
|
||||
{
|
||||
uint offset = flow_write_length(data, expect);
|
||||
|
||||
uint set = (expect < 0xf0) ? *data : (get_u16(data) & 0x0fff);
|
||||
bt_assert_msg(set == expect, "Testing set length 0x%03x (set 0x%03x)", expect, set);
|
||||
bt_assert(offset == (expect < 0xf0 ? 1 : 2));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_first_part(void)
|
||||
{
|
||||
net_addr_flow4 *f;
|
||||
NET_ADDR_FLOW4_(f, ip4_build(10,0,0,1), 24, ((byte[]) { 0x00, 0x00, 0xab }));
|
||||
|
||||
const byte *under240 = &f->data[1];
|
||||
const byte *above240 = &f->data[2];
|
||||
|
||||
/* Case 0x00 0x00 */
|
||||
bt_assert(flow4_first_part(f) == NULL);
|
||||
|
||||
/* Case 0x01 0x00 */
|
||||
f->data[0] = 0x01;
|
||||
bt_assert(flow4_first_part(f) == under240);
|
||||
|
||||
/* Case 0xef 0x00 */
|
||||
f->data[0] = 0xef;
|
||||
bt_assert(flow4_first_part(f) == under240);
|
||||
|
||||
/* Case 0xf0 0x00 */
|
||||
f->data[0] = 0xf0;
|
||||
bt_assert(flow4_first_part(f) == NULL);
|
||||
|
||||
/* Case 0xf0 0x01 */
|
||||
f->data[1] = 0x01;
|
||||
bt_assert(flow4_first_part(f) == above240);
|
||||
|
||||
/* Case 0xff 0xff */
|
||||
f->data[0] = 0xff;
|
||||
f->data[1] = 0xff;
|
||||
bt_assert(flow4_first_part(f) == above240);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_iterators4(void)
|
||||
{
|
||||
net_addr_flow4 *f;
|
||||
NET_ADDR_FLOW4_(f, ip4_build(5,6,7,0), 24, ((byte[]) {
|
||||
25, /* Length */
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
|
||||
FLOW_TYPE_IP_PROTOCOL, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_TCP_FLAGS, 0x80, 0x55,
|
||||
}));
|
||||
|
||||
const byte *start = f->data;
|
||||
const byte *p1_dst_pfx = &f->data[1];
|
||||
const byte *p2_src_pfx = &f->data[6];
|
||||
const byte *p3_ip_proto = &f->data[12];
|
||||
const byte *p4_port = &f->data[15];
|
||||
const byte *p5_tcp_flags = &f->data[23];
|
||||
const byte *end = &f->data[25];
|
||||
|
||||
bt_assert(flow_read_length(f->data) == (end-start));
|
||||
bt_assert(flow4_first_part(f) == p1_dst_pfx);
|
||||
|
||||
bt_assert(flow4_next_part(p1_dst_pfx, end) == p2_src_pfx);
|
||||
bt_assert(flow4_next_part(p2_src_pfx, end) == p3_ip_proto);
|
||||
bt_assert(flow4_next_part(p3_ip_proto, end) == p4_port);
|
||||
bt_assert(flow4_next_part(p4_port, end) == p5_tcp_flags);
|
||||
bt_assert(flow4_next_part(p5_tcp_flags, end) == NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_iterators6(void)
|
||||
{
|
||||
net_addr_flow6 *f;
|
||||
NET_ADDR_FLOW6_(f, ip6_build(0,0,0x12345678,0x9a000000), 0x68, ((byte[]) {
|
||||
26, /* Length */
|
||||
FLOW_TYPE_DST_PREFIX, 0x68, 0x40, 0x12, 0x34, 0x56, 0x78, 0x9a,
|
||||
FLOW_TYPE_SRC_PREFIX, 0x08, 0x0, 0xc0,
|
||||
FLOW_TYPE_NEXT_HEADER, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_LABEL, 0x80, 0x55,
|
||||
}));
|
||||
|
||||
const byte *start = f->data;
|
||||
const byte *p1_dst_pfx = &f->data[1];
|
||||
const byte *p2_src_pfx = &f->data[9];
|
||||
const byte *p3_next_header = &f->data[13];
|
||||
const byte *p4_port = &f->data[16];
|
||||
const byte *p5_label = &f->data[24];
|
||||
const byte *end = &f->data[26];
|
||||
|
||||
bt_assert(flow_read_length(f->data) == (end-start));
|
||||
bt_assert(flow6_first_part(f) == p1_dst_pfx);
|
||||
|
||||
bt_assert(flow6_next_part(p1_dst_pfx, end) == p2_src_pfx);
|
||||
bt_assert(flow6_next_part(p2_src_pfx, end) == p3_next_header);
|
||||
bt_assert(flow6_next_part(p3_next_header, end) == p4_port);
|
||||
bt_assert(flow6_next_part(p4_port, end) == p5_label);
|
||||
bt_assert(flow6_next_part(p5_label, end) == NULL);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_accessors4(void)
|
||||
{
|
||||
net_addr_flow4 *f;
|
||||
NET_ADDR_FLOW4_(f, ip4_build(5,6,7,0), 24, ((byte[]) {
|
||||
25, /* Length */
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
|
||||
FLOW_TYPE_IP_PROTOCOL, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_TCP_FLAGS, 0x80, 0x55,
|
||||
}));
|
||||
|
||||
const byte *p1_dst_px = &f->data[1];
|
||||
const ip4_addr p1_dst_addr = ip4_build(5,6,7,0);
|
||||
|
||||
const byte *p2_src_px = &f->data[6];
|
||||
const ip4_addr p2_src_addr = ip4_build(10,11,12,13);
|
||||
|
||||
bt_assert(ip4_equal(flow_read_ip4_part(p1_dst_px), p1_dst_addr));
|
||||
bt_assert(ip4_equal(flow_read_ip4_part(p2_src_px), p2_src_addr));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_accessors6(void)
|
||||
{
|
||||
net_addr_flow6 *f;
|
||||
NET_ADDR_FLOW6_(f, ip6_build(0,0,0x12345678,0x9a000000), 0x68, ((byte[]) {
|
||||
26, /* Length */
|
||||
FLOW_TYPE_DST_PREFIX, 0x68, 0x40, 0x12, 0x34, 0x56, 0x78, 0x9a,
|
||||
FLOW_TYPE_SRC_PREFIX, 0x08, 0x0, 0xc0,
|
||||
FLOW_TYPE_NEXT_HEADER, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_LABEL, 0x80, 0x55,
|
||||
}));
|
||||
|
||||
const byte *p1_dst_px = &f->data[1];
|
||||
const ip6_addr p1_dst_addr = ip6_build(0,0,0x12345678,0x9a000000);
|
||||
|
||||
const byte *p2_src_px = &f->data[9];
|
||||
const ip6_addr p2_src_addr = ip6_build(0xc0000000, 0, 0, 0);
|
||||
|
||||
bt_assert(ip6_equal(flow_read_ip6_part(p1_dst_px), p1_dst_addr));
|
||||
bt_assert(ip6_equal(flow_read_ip6_part(p2_src_px), p2_src_addr));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_validation4(void)
|
||||
{
|
||||
enum flow_validated_state res;
|
||||
|
||||
byte nlri1[] = {
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
|
||||
FLOW_TYPE_IP_PROTOCOL, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_TCP_FLAGS, 0x80, 0x55,
|
||||
};
|
||||
|
||||
/* Empty NLRI */
|
||||
res = flow4_validate(nlri1, 0);
|
||||
bt_assert(res == FLOW_ST_VALID);
|
||||
|
||||
/* Valid / Not Complete testing */
|
||||
uint valid_sizes[] = {5, 11, 14, 22, 25, 0};
|
||||
uint valid_idx = 0;
|
||||
for (uint size = 1; size <= sizeof(nlri1); size++)
|
||||
{
|
||||
res = flow4_validate(nlri1, size);
|
||||
bt_debug("size %u, result: %s\n", size, flow_validated_state_str(res));
|
||||
if (size == valid_sizes[valid_idx])
|
||||
{
|
||||
valid_idx++;
|
||||
bt_assert(res == FLOW_ST_VALID);
|
||||
}
|
||||
else
|
||||
{
|
||||
bt_assert(res == FLOW_ST_NOT_COMPLETE);
|
||||
}
|
||||
}
|
||||
|
||||
/* Misc err tests */
|
||||
|
||||
struct tset {
|
||||
enum flow_validated_state expect;
|
||||
char *description;
|
||||
u16 size;
|
||||
byte *nlri;
|
||||
};
|
||||
|
||||
#define TS(type, msg, data) ((struct tset) {type, msg, sizeof(data), (data)})
|
||||
struct tset tset[] = {
|
||||
TS(
|
||||
FLOW_ST_EXCEED_MAX_PREFIX_LENGTH,
|
||||
"33-length IPv4 prefix",
|
||||
((byte []) {
|
||||
FLOW_TYPE_DST_PREFIX, 33, 5, 6, 7, 8, 9
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_BAD_TYPE_ORDER,
|
||||
"Bad flowspec component type order",
|
||||
((byte []) {
|
||||
FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_BAD_TYPE_ORDER,
|
||||
"Doubled destination prefix component",
|
||||
((byte []) {
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_AND_BIT_SHOULD_BE_UNSET,
|
||||
"The first numeric operator has set the AND bit",
|
||||
((byte []) {
|
||||
FLOW_TYPE_PORT, 0x43, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_ZERO_BIT_SHOULD_BE_UNSED,
|
||||
"Set zero bit in operand to one",
|
||||
((byte []) {
|
||||
FLOW_TYPE_IP_PROTOCOL, 0x89, 0x06,
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_UNKNOWN_COMPONENT,
|
||||
"Unknown component of type number 13",
|
||||
((byte []) {
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
FLOW_TYPE_TCP_FLAGS, 0x80, 0x55,
|
||||
13 /*something new*/, 0x80, 0x55,
|
||||
})
|
||||
),
|
||||
};
|
||||
#undef TS
|
||||
|
||||
for (uint tcase = 0; tcase < ARRAY_SIZE(tset); tcase++)
|
||||
{
|
||||
res = flow4_validate(tset[tcase].nlri, tset[tcase].size);
|
||||
bt_assert_msg(res == tset[tcase].expect, "Assertion (%s == %s) %s", flow_validated_state_str(res), flow_validated_state_str(tset[tcase].expect), tset[tcase].description);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_validation6(void)
|
||||
{
|
||||
enum flow_validated_state res;
|
||||
|
||||
byte nlri1[] = {
|
||||
FLOW_TYPE_DST_PREFIX, 103, 61, 0x01, 0x12, 0x34, 0x56, 0x78, 0x98,
|
||||
FLOW_TYPE_SRC_PREFIX, 8, 0, 0xc0,
|
||||
FLOW_TYPE_NEXT_HEADER, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_LABEL, 0x80, 0x55,
|
||||
};
|
||||
|
||||
/* Isn't included destination prefix */
|
||||
res = flow6_validate(nlri1, 0);
|
||||
bt_assert(res == FLOW_ST_VALID);
|
||||
|
||||
/* Valid / Not Complete testing */
|
||||
uint valid_sizes[] = {0, 9, 13, 16, 24, 27, 0};
|
||||
uint valid_idx = 0;
|
||||
for (uint size = 0; size <= sizeof(nlri1); size++)
|
||||
{
|
||||
res = flow6_validate(nlri1, size);
|
||||
bt_debug("size %u, result: %s\n", size, flow_validated_state_str(res));
|
||||
if (size == valid_sizes[valid_idx])
|
||||
{
|
||||
valid_idx++;
|
||||
bt_assert(res == FLOW_ST_VALID);
|
||||
}
|
||||
else
|
||||
{
|
||||
bt_assert(res == FLOW_ST_NOT_COMPLETE);
|
||||
}
|
||||
}
|
||||
|
||||
/* Misc err tests */
|
||||
|
||||
struct tset {
|
||||
enum flow_validated_state expect;
|
||||
char *description;
|
||||
u16 size;
|
||||
byte *nlri;
|
||||
};
|
||||
|
||||
#define TS(type, msg, data) ((struct tset) {type, msg, sizeof(data), (data)})
|
||||
struct tset tset[] = {
|
||||
TS(
|
||||
FLOW_ST_EXCEED_MAX_PREFIX_LENGTH,
|
||||
"129-length IPv6 prefix",
|
||||
((byte []) {
|
||||
FLOW_TYPE_DST_PREFIX, 129, 64, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_EXCEED_MAX_PREFIX_OFFSET,
|
||||
"Prefix offset is higher than prefix length",
|
||||
((byte []) {
|
||||
FLOW_TYPE_DST_PREFIX, 48, 64, 0x40, 0x12, 0x34
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_BAD_TYPE_ORDER,
|
||||
"Bad flowspec component type order",
|
||||
((byte []) {
|
||||
FLOW_TYPE_NEXT_HEADER, 0x81, 0x06,
|
||||
FLOW_TYPE_SRC_PREFIX, 8, 0, 0xc0,
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_BAD_TYPE_ORDER,
|
||||
"Doubled destination prefix component",
|
||||
((byte []) {
|
||||
FLOW_TYPE_DST_PREFIX, 103, 61, 0x01, 0x12, 0x34, 0x56, 0x78, 0x98,
|
||||
FLOW_TYPE_DST_PREFIX, 103, 61, 0x01, 0x12, 0x34, 0x56, 0x78, 0x98,
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_AND_BIT_SHOULD_BE_UNSET,
|
||||
"The first numeric operator has set the AND bit",
|
||||
((byte []) {
|
||||
FLOW_TYPE_PORT, 0x43, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_ZERO_BIT_SHOULD_BE_UNSED,
|
||||
"Set zero bit in operand to one",
|
||||
((byte []) {
|
||||
FLOW_TYPE_NEXT_HEADER, 0x89, 0x06
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_VALID,
|
||||
"Component of type number 13 (Label) is well-known in IPv6",
|
||||
((byte []) {
|
||||
FLOW_TYPE_LABEL, 0x80, 0x55
|
||||
})
|
||||
),
|
||||
TS(
|
||||
FLOW_ST_UNKNOWN_COMPONENT,
|
||||
"Unknown component of type number 14",
|
||||
((byte []) {
|
||||
FLOW_TYPE_LABEL, 0x80, 0x55,
|
||||
14 /*something new*/, 0x80, 0x55,
|
||||
})
|
||||
)
|
||||
};
|
||||
#undef TS
|
||||
|
||||
for (uint tcase = 0; tcase < ARRAY_SIZE(tset); tcase++)
|
||||
{
|
||||
res = flow6_validate(tset[tcase].nlri, tset[tcase].size);
|
||||
bt_assert_msg(res == tset[tcase].expect, "Assertion (%s == %s) %s", flow_validated_state_str(res), flow_validated_state_str(tset[tcase].expect), tset[tcase].description);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Builder tests
|
||||
*/
|
||||
|
||||
static int
|
||||
t_builder4(void)
|
||||
{
|
||||
resource_init();
|
||||
|
||||
struct flow_builder *fb = flow_builder_init(&root_pool);
|
||||
linpool *lp = lp_new_default(&root_pool);
|
||||
|
||||
/* Expectation */
|
||||
|
||||
static byte nlri[] = {
|
||||
25,
|
||||
FLOW_TYPE_DST_PREFIX, 24, 5, 6, 7,
|
||||
FLOW_TYPE_SRC_PREFIX, 32, 10, 11, 12, 13,
|
||||
FLOW_TYPE_IP_PROTOCOL, 0x80, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_TCP_FLAGS, 0x80, 0x55
|
||||
};
|
||||
|
||||
net_addr_flow4 *expect;
|
||||
NET_ADDR_FLOW4_(expect, ip4_build(5, 6, 7, 0), 24, nlri);
|
||||
|
||||
/* Normal order */
|
||||
|
||||
net_addr_ip4 n1;
|
||||
net_fill_ip4((net_addr *) &n1, ip4_build(5,6,7,0), 24);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_DST_PREFIX);
|
||||
flow_builder4_add_pfx(fb, &n1);
|
||||
|
||||
net_addr_ip4 n2;
|
||||
net_fill_ip4((net_addr *) &n2, ip4_build(10,11,12,13), 32);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_SRC_PREFIX);
|
||||
flow_builder4_add_pfx(fb, &n2);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_IP_PROTOCOL);
|
||||
flow_builder_add_op_val(fb, 0, 0x06);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_PORT);
|
||||
flow_builder_add_op_val(fb, 0x03, 0x89);
|
||||
flow_builder_add_op_val(fb, 0x45, 0x8b);
|
||||
flow_builder_add_op_val(fb, 0x01, 0x1f90);
|
||||
|
||||
/* Try put a component twice time */
|
||||
flow_builder_set_type(fb, FLOW_TYPE_IP_PROTOCOL);
|
||||
flow_builder_add_op_val(fb, 0, 0x06);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_TCP_FLAGS);
|
||||
flow_builder_add_op_val(fb, 0, 0x55);
|
||||
|
||||
net_addr_flow4 *res = flow_builder4_finalize(fb, lp);
|
||||
|
||||
bt_assert(memcmp(res, expect, expect->length) == 0);
|
||||
|
||||
/* Reverse order */
|
||||
|
||||
flow_builder_clear(fb);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_TCP_FLAGS);
|
||||
flow_builder_add_op_val(fb, 0, 0x55);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_PORT);
|
||||
flow_builder_add_op_val(fb, 0x03, 0x89);
|
||||
flow_builder_add_op_val(fb, 0x45, 0x8b);
|
||||
flow_builder_add_op_val(fb, 0x01, 0x1f90);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_IP_PROTOCOL);
|
||||
flow_builder_add_op_val(fb, 0, 0x06);
|
||||
|
||||
net_fill_ip4((net_addr *) &n2, ip4_build(10,11,12,13), 32);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_SRC_PREFIX);
|
||||
flow_builder4_add_pfx(fb, &n2);
|
||||
|
||||
net_fill_ip4((net_addr *) &n1, ip4_build(5,6,7,0), 24);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_DST_PREFIX);
|
||||
flow_builder4_add_pfx(fb, &n1);
|
||||
|
||||
bt_assert(memcmp(res, expect, expect->length) == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_builder6(void)
|
||||
{
|
||||
net_addr_ip6 ip;
|
||||
|
||||
resource_init();
|
||||
linpool *lp = lp_new_default(&root_pool);
|
||||
struct flow_builder *fb = flow_builder_init(&root_pool);
|
||||
fb->ipv6 = 1;
|
||||
|
||||
/* Expectation */
|
||||
|
||||
byte nlri[] = {
|
||||
27,
|
||||
FLOW_TYPE_DST_PREFIX, 103, 61, 0x01, 0x12, 0x34, 0x56, 0x78, 0x98,
|
||||
FLOW_TYPE_SRC_PREFIX, 8, 0, 0xc0,
|
||||
FLOW_TYPE_NEXT_HEADER, 0x80, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 0x89, 0x45, 0x8b, 0x91, 0x1f, 0x90,
|
||||
FLOW_TYPE_LABEL, 0x80, 0x55,
|
||||
};
|
||||
|
||||
net_addr_flow6 *expect;
|
||||
NET_ADDR_FLOW6_(expect, ip6_build(0, 1, 0x12345678, 0x98000000), 103, nlri);
|
||||
|
||||
/* Normal order */
|
||||
|
||||
net_fill_ip6((net_addr *) &ip, ip6_build(0, 1, 0x12345678, 0x98000000), 103);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_DST_PREFIX);
|
||||
flow_builder6_add_pfx(fb, &ip, 61);
|
||||
|
||||
/* Try put a component twice time */
|
||||
net_fill_ip6((net_addr *) &ip, ip6_build(0, 1, 0x12345678, 0x98000000), 103);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_DST_PREFIX);
|
||||
bt_assert(flow_builder6_add_pfx(fb, &ip, 61) == 0);
|
||||
|
||||
net_fill_ip6((net_addr *) &ip, ip6_build(0xc0000000,0,0,0), 8);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_SRC_PREFIX);
|
||||
flow_builder6_add_pfx(fb, &ip, 0);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_NEXT_HEADER);
|
||||
flow_builder_add_op_val(fb, 0, 0x06);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_PORT);
|
||||
flow_builder_add_op_val(fb, 0x03, 0x89);
|
||||
flow_builder_add_op_val(fb, 0x45, 0x8b);
|
||||
flow_builder_add_op_val(fb, 0x01, 0x1f90);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_LABEL);
|
||||
flow_builder_add_op_val(fb, 0, 0x55);
|
||||
|
||||
net_addr_flow6 *res = flow_builder6_finalize(fb, lp);
|
||||
bt_assert(memcmp(res, expect, expect->length) == 0);
|
||||
|
||||
/* Reverse order */
|
||||
|
||||
flow_builder_clear(fb);
|
||||
fb->ipv6 = 1;
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_LABEL);
|
||||
flow_builder_add_op_val(fb, 0, 0x55);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_PORT);
|
||||
flow_builder_add_op_val(fb, 0x03, 0x89);
|
||||
flow_builder_add_op_val(fb, 0x45, 0x8b);
|
||||
flow_builder_add_op_val(fb, 0x01, 0x1f90);
|
||||
|
||||
flow_builder_set_type(fb, FLOW_TYPE_NEXT_HEADER);
|
||||
flow_builder_add_op_val(fb, 0, 0x06);
|
||||
|
||||
net_fill_ip6((net_addr *) &ip, ip6_build(0xc0000000,0,0,0), 8);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_SRC_PREFIX);
|
||||
flow_builder6_add_pfx(fb, &ip, 0);
|
||||
|
||||
net_fill_ip6((net_addr *) &ip, ip6_build(0, 1, 0x12345678, 0x98000000), 103);
|
||||
flow_builder_set_type(fb, FLOW_TYPE_DST_PREFIX);
|
||||
flow_builder6_add_pfx(fb, &ip, 61);
|
||||
|
||||
res = flow_builder6_finalize(fb, lp);
|
||||
bt_assert(memcmp(res, expect, expect->length) == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_formatting4(void)
|
||||
{
|
||||
char b[1024];
|
||||
|
||||
byte nlri[] = {
|
||||
0,
|
||||
FLOW_TYPE_DST_PREFIX, 0x08, 10,
|
||||
FLOW_TYPE_IP_PROTOCOL, 0x81, 23,
|
||||
FLOW_TYPE_DST_PORT, 0x02, 24, 0x44, 30, 0x03, 40, 0x45, 50, 0x03, 60, 0x45, 70, 0x01, 80, 0xc3, 90,
|
||||
FLOW_TYPE_SRC_PORT, 0x02, 24, 0x44, 0x1e, 0x01, 0x28, 0x01, 0x32, 0x03, 0x3c, 0x45, 0x46, 0x81, 0x50,
|
||||
FLOW_TYPE_ICMP_TYPE, 0x81, 0x50,
|
||||
FLOW_TYPE_ICMP_CODE, 0x81, 0x5a,
|
||||
FLOW_TYPE_TCP_FLAGS, 0x01, 0x03, 0xc2, 0x0c,
|
||||
FLOW_TYPE_PACKET_LENGTH, 0x03, 0, 0xd5, 0xff, 0xff,
|
||||
FLOW_TYPE_DSCP, 0x81, 63,
|
||||
FLOW_TYPE_FRAGMENT, 0x01, 0x01, 0x82, 0x02
|
||||
};
|
||||
*nlri = (u8) sizeof(nlri);
|
||||
|
||||
net_addr_flow4 *input;
|
||||
NET_ADDR_FLOW4_(input, ip4_build(5, 6, 7, 0), 24, nlri);
|
||||
|
||||
const char *expect = "flow4 { dst 10.0.0.0/8; proto 23; dport > 24 && < 30 || 40..50,60..70,80 && >= 90; sport > 24 && < 30 || 40,50,60..70,80; icmp type 80; icmp code 90; tcp flags 0x3/0x3,0x0/0xc; length 0..65535; dscp 63; fragment dont_fragment || !is_fragment; }";
|
||||
|
||||
bt_assert(flow4_net_format(b, sizeof(b), input) == strlen(expect));
|
||||
bt_debug(" expect: '%s',\n output: '%s'\n", expect, b);
|
||||
bt_assert(strcmp(b, expect) == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_formatting6(void)
|
||||
{
|
||||
char b[1024];
|
||||
|
||||
byte nlri[] = {
|
||||
0,
|
||||
FLOW_TYPE_DST_PREFIX, 103, 61, 0x01, 0x12, 0x34, 0x56, 0x78, 0x98,
|
||||
FLOW_TYPE_SRC_PREFIX, 8, 0, 0xc0,
|
||||
FLOW_TYPE_NEXT_HEADER, 0x81, 0x06,
|
||||
FLOW_TYPE_PORT, 0x03, 20, 0x45, 40, 0x91, 0x01, 0x11,
|
||||
FLOW_TYPE_LABEL, 0xa0, 0x12, 0x34, 0x56, 0x78,
|
||||
};
|
||||
*nlri = (u8) sizeof(nlri);
|
||||
|
||||
net_addr_flow6 *input;
|
||||
NET_ADDR_FLOW6_(input, ip6_build(0, 1, 0x12345678, 0x98000000), 103, nlri);
|
||||
|
||||
const char *expect = "flow6 { dst ::1:1234:5678:9800:0/103 offset 61; src c000::/8; next header 6; port 20..40,273; label !0x0/0x12345678; }";
|
||||
|
||||
bt_assert(flow6_net_format(b, sizeof(b), input) == strlen(expect));
|
||||
bt_debug(" expect: '%s',\n output: '%s'\n", expect, b);
|
||||
bt_assert(strcmp(b, expect) == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_read_length, "Testing get NLRI length");
|
||||
bt_test_suite(t_write_length, "Testing set NLRI length");
|
||||
bt_test_suite(t_first_part, "Searching first part in net_addr_flow");
|
||||
bt_test_suite(t_iterators4, "Testing iterators (IPv4)");
|
||||
bt_test_suite(t_iterators6, "Testing iterators (IPv6)");
|
||||
bt_test_suite(t_accessors4, "Testing accessors (IPv4)");
|
||||
bt_test_suite(t_accessors6, "Testing accessors (IPv6)");
|
||||
bt_test_suite(t_validation4, "Testing validation (IPv4)");
|
||||
bt_test_suite(t_validation6, "Testing validation (IPv6)");
|
||||
bt_test_suite(t_builder4, "Inserting components into existing Flow Specification (IPv4)");
|
||||
bt_test_suite(t_builder6, "Inserting components into existing Flow Specification (IPv6)");
|
||||
bt_test_suite(t_formatting4, "Formatting Flow Specification (IPv4) into text representation");
|
||||
bt_test_suite(t_formatting6, "Formatting Flow Specification (IPv6) into text representation");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
43
lib/hash.h
43
lib/hash.h
@ -194,4 +194,47 @@
|
||||
|
||||
#define HASH_WALK_FILTER_END } while (0)
|
||||
|
||||
|
||||
static inline void
|
||||
mem_hash_init(u64 *h)
|
||||
{
|
||||
*h = 0x001047d54778bcafULL;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mem_hash_mix(u64 *h, const void *p, uint s)
|
||||
{
|
||||
const u64 multiplier = 0xb38bc09a61202731ULL;
|
||||
const char *pp = p;
|
||||
uint i;
|
||||
|
||||
for (i=0; i<s/4; i++)
|
||||
*h = *h * multiplier + ((const u32 *)pp)[i];
|
||||
|
||||
for (i=s & ~0x3; i<s; i++)
|
||||
*h = *h * multiplier + pp[i];
|
||||
}
|
||||
|
||||
static inline uint
|
||||
mem_hash_value(u64 *h)
|
||||
{
|
||||
return ((*h >> 32) ^ (*h & 0xffffffff));
|
||||
}
|
||||
|
||||
static inline uint
|
||||
mem_hash(const void *p, uint s)
|
||||
{
|
||||
static u64 h;
|
||||
mem_hash_init(&h);
|
||||
mem_hash_mix(&h, p, s);
|
||||
return mem_hash_value(&h);
|
||||
}
|
||||
|
||||
static inline uint
|
||||
ptr_hash(void *ptr)
|
||||
{
|
||||
uintptr_t p = (uintptr_t) ptr;
|
||||
return p ^ (p << 8) ^ (p >> 16);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
305
lib/hash_test.c
Normal file
305
lib/hash_test.c
Normal file
@ -0,0 +1,305 @@
|
||||
/*
|
||||
* BIRD Library -- Hash Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#undef LOCAL_DEBUG
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/hash.h"
|
||||
|
||||
struct test_node {
|
||||
struct test_node *next; /* Hash chain */
|
||||
u32 key;
|
||||
};
|
||||
|
||||
#define TEST_KEY(n) n->key
|
||||
#define TEST_NEXT(n) n->next
|
||||
#define TEST_EQ(n1,n2) n1 == n2
|
||||
#define TEST_FN(n) (n) ^ u32_hash((n))
|
||||
#define TEST_ORDER 13
|
||||
#define TEST_PARAMS /TEST_ORDER, *2, 2, 2, TEST_ORDER, 20
|
||||
#define TEST_REHASH test_rehash
|
||||
|
||||
HASH_DEFINE_REHASH_FN(TEST, struct test_node);
|
||||
|
||||
HASH(struct test_node) hash;
|
||||
struct pool *my_pool;
|
||||
|
||||
#define MAX_NUM (1 << TEST_ORDER)
|
||||
|
||||
struct test_node nodes[MAX_NUM];
|
||||
|
||||
static void
|
||||
print_rate_of_fulfilment(void)
|
||||
{
|
||||
int i;
|
||||
int num_stacked_items = 0;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
if (!hash.data[i])
|
||||
num_stacked_items++;
|
||||
|
||||
double percent_stacked_items = ((double)num_stacked_items/(double)MAX_NUM)*100.;
|
||||
bt_debug("%d (%.2f %%) chained of %d hashes \n", num_stacked_items, percent_stacked_items, MAX_NUM);
|
||||
}
|
||||
|
||||
#ifdef LOCAL_DEBUG
|
||||
static void
|
||||
dump_nodes(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
bt_debug("nodes[%3d] is at address %14p has .key %3d, .next %14p \n", i, &nodes[i], nodes[i].key, nodes[i].next);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
init_hash_(uint order)
|
||||
{
|
||||
resource_init();
|
||||
my_pool = rp_new(&root_pool, "Test pool");
|
||||
|
||||
HASH_INIT(hash, my_pool, order);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
nodes[i].key = i;
|
||||
nodes[i].next = NULL;
|
||||
}
|
||||
|
||||
bt_debug("MAX_NUM %d \n", MAX_NUM);
|
||||
}
|
||||
|
||||
static void
|
||||
init_hash(void)
|
||||
{
|
||||
init_hash_(TEST_ORDER);
|
||||
}
|
||||
|
||||
static void
|
||||
validate_filled_hash(void)
|
||||
{
|
||||
int i;
|
||||
struct test_node *node;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
node = HASH_FIND(hash, TEST, nodes[i].key);
|
||||
bt_assert_msg(node->key == nodes[i].key, "Hash should be filled, to find (%p) the node[%d] (%p) with .key = %u, .next %p", node, i, &nodes[i], nodes[i].key, nodes[i].next);
|
||||
}
|
||||
|
||||
print_rate_of_fulfilment();
|
||||
}
|
||||
|
||||
static void
|
||||
validate_empty_hash(void)
|
||||
{
|
||||
int i;
|
||||
struct test_node *node;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
node = HASH_FIND(hash, TEST, nodes[i].key);
|
||||
bt_assert_msg(node == NULL, "Hash should be empty, to find (%p) the node[%d] (%p) with .key %u, .next %p", node, i, &nodes[i], nodes[i].key, nodes[i].next);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
fill_hash(void)
|
||||
{
|
||||
int i;
|
||||
struct test_node *node;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
nodes[i].key = i;
|
||||
node = &nodes[i];
|
||||
HASH_INSERT(hash, TEST, node);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
t_insert_find(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
validate_filled_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_insert_find_random(void)
|
||||
{
|
||||
init_hash();
|
||||
|
||||
int i;
|
||||
struct test_node *node;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
nodes[i].key = bt_random();
|
||||
node = &nodes[i];
|
||||
HASH_INSERT(hash, TEST, node);
|
||||
}
|
||||
|
||||
validate_filled_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_insert2_find(void)
|
||||
{
|
||||
init_hash_(1);
|
||||
|
||||
int i;
|
||||
struct test_node *node;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
nodes[i].key = i;
|
||||
node = &nodes[i];
|
||||
HASH_INSERT2(hash, TEST, my_pool, node);
|
||||
}
|
||||
bt_assert_msg(hash.order != 1, "The hash should auto-resize from order 2^1. The order of the hash is 2^%u.", hash.order);
|
||||
|
||||
validate_filled_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_walk(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
|
||||
uint i;
|
||||
uint check[MAX_NUM];
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
check[i] = 0;
|
||||
|
||||
HASH_WALK(hash, next, n)
|
||||
{
|
||||
check[n->key]++;
|
||||
}
|
||||
HASH_WALK_END;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
bt_assert(check[i] == 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_walk_delsafe_delete(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
|
||||
HASH_WALK_DELSAFE(hash, next, n)
|
||||
{
|
||||
HASH_DELETE(hash, TEST, n->key);
|
||||
}
|
||||
HASH_WALK_DELSAFE_END;
|
||||
|
||||
validate_empty_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_walk_delsafe_remove(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
|
||||
HASH_WALK_DELSAFE(hash, next, n)
|
||||
{
|
||||
HASH_REMOVE(hash, TEST, n);
|
||||
}
|
||||
HASH_WALK_DELSAFE_END;
|
||||
|
||||
validate_empty_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_walk_delsafe_delete2(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
|
||||
HASH_WALK_DELSAFE(hash, next, n)
|
||||
{
|
||||
HASH_DELETE2(hash, TEST, my_pool, n->key);
|
||||
}
|
||||
HASH_WALK_DELSAFE_END;
|
||||
|
||||
validate_empty_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_walk_delsafe_remove2(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
|
||||
HASH_WALK_DELSAFE(hash, next, n)
|
||||
{
|
||||
HASH_REMOVE2(hash, TEST, my_pool, n);
|
||||
}
|
||||
HASH_WALK_DELSAFE_END;
|
||||
|
||||
validate_empty_hash();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_walk_filter(void)
|
||||
{
|
||||
init_hash();
|
||||
fill_hash();
|
||||
|
||||
uint i;
|
||||
uint check[MAX_NUM];
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
check[i] = 0;
|
||||
|
||||
HASH_WALK_FILTER(hash, next, n, m)
|
||||
{
|
||||
bt_assert(n == *m);
|
||||
check[n->key]++;
|
||||
}
|
||||
HASH_WALK_FILTER_END;
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
bt_assert(check[i] == 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_insert_find, "HASH_INSERT and HASH_FIND");
|
||||
bt_test_suite(t_insert_find_random, "HASH_INSERT pseudo-random keys and HASH_FIND");
|
||||
bt_test_suite(t_insert2_find, "HASH_INSERT2 and HASH_FIND. HASH_INSERT2 is HASH_INSERT and a smart auto-resize function");
|
||||
bt_test_suite(t_walk, "HASH_WALK");
|
||||
bt_test_suite(t_walk_delsafe_delete, "HASH_WALK_DELSAFE and HASH_DELETE");
|
||||
bt_test_suite(t_walk_delsafe_delete2, "HASH_WALK_DELSAFE and HASH_DELETE2. HASH_DELETE2 is HASH_DELETE and smart auto-resize function");
|
||||
bt_test_suite(t_walk_delsafe_remove, "HASH_WALK_DELSAFE and HASH_REMOVE");
|
||||
bt_test_suite(t_walk_delsafe_remove2, "HASH_WALK_DELSAFE and HASH_REMOVE2. HASH_REMOVE2 is HASH_REMOVE and smart auto-resize function");
|
||||
bt_test_suite(t_walk_filter, "HASH_WALK_FILTER");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
186
lib/heap_test.c
Normal file
186
lib/heap_test.c
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* BIRD Library -- Universal Heap Macros Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "sysdep/config.h"
|
||||
#include "lib/heap.h"
|
||||
|
||||
#define MAX_NUM 1000
|
||||
#define SPECIAL_KEY -3213
|
||||
|
||||
#define MY_CMP(x, y) ((x) < (y))
|
||||
|
||||
#define MY_HEAP_SWAP(heap,a,b,t) \
|
||||
do { \
|
||||
bt_debug("swap(%u %u) ", a, b); \
|
||||
HEAP_SWAP(heap,a,b,t); \
|
||||
} while(0)
|
||||
|
||||
static int heap[MAX_NUM+1];
|
||||
static uint num;
|
||||
|
||||
/*
|
||||
* A valid heap must follow these rules:
|
||||
* - `num >= 0`
|
||||
* - `heap[i] >= heap[i / 2]` for each `i` in `[2, num]`
|
||||
*/
|
||||
static int
|
||||
is_heap_valid(int heap[], uint num)
|
||||
{
|
||||
uint i;
|
||||
|
||||
if (num > MAX_NUM)
|
||||
return 0;
|
||||
|
||||
for (i = 2; i <= num; i++)
|
||||
if (heap[i] < heap[i / 2])
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
show_heap(void)
|
||||
{
|
||||
uint i;
|
||||
bt_debug("\n");
|
||||
bt_debug("numbers %u; ", num);
|
||||
for (i = 0; i <= num; i++)
|
||||
bt_debug("%d ", heap[i]);
|
||||
bt_debug(is_heap_valid(heap, num) ? "OK" : "NON-VALID HEAP!");
|
||||
bt_debug("\n");
|
||||
}
|
||||
|
||||
static void
|
||||
init_heap(void)
|
||||
{
|
||||
uint i;
|
||||
num = 0;
|
||||
heap[0] = SPECIAL_KEY; /* heap[0] should be unused */
|
||||
for (i = 1; i <= MAX_NUM; i++)
|
||||
heap[i] = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
t_heap_insert(void)
|
||||
{
|
||||
uint i;
|
||||
|
||||
init_heap();
|
||||
|
||||
for (i = MAX_NUM; i >= 1; i--)
|
||||
{
|
||||
bt_debug("ins %u at pos %u ", i, MAX_NUM - i);
|
||||
heap[MAX_NUM - i + 1] = i;
|
||||
HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
|
||||
show_heap();
|
||||
bt_assert(is_heap_valid(heap, num));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_heap_increase_decrease(void)
|
||||
{
|
||||
uint i;
|
||||
|
||||
t_heap_insert();
|
||||
|
||||
for (i = 1; i <= MAX_NUM; i++)
|
||||
{
|
||||
if ((int)i > heap[i])
|
||||
{
|
||||
bt_debug("inc %u ", i);
|
||||
heap[i] = i;
|
||||
HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
||||
}
|
||||
else if ((int)i < heap[i])
|
||||
{
|
||||
bt_debug("dec %u ", i);
|
||||
heap[i] = i;
|
||||
HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
||||
}
|
||||
show_heap();
|
||||
bt_assert(is_heap_valid(heap, num));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_heap_delete(void)
|
||||
{
|
||||
uint i;
|
||||
|
||||
t_heap_insert();
|
||||
|
||||
for (i = 1; i <= num; i++)
|
||||
{
|
||||
bt_debug("del at pos %u ", i);
|
||||
HEAP_DELETE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
|
||||
show_heap();
|
||||
bt_assert(is_heap_valid(heap, num));
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_heap_0(void)
|
||||
{
|
||||
init_heap();
|
||||
t_heap_insert();
|
||||
t_heap_increase_decrease();
|
||||
t_heap_delete();
|
||||
|
||||
return heap[0] == SPECIAL_KEY;
|
||||
}
|
||||
|
||||
static int
|
||||
t_heap_insert_random(void)
|
||||
{
|
||||
int i, j;
|
||||
int expected[MAX_NUM+1];
|
||||
|
||||
init_heap();
|
||||
|
||||
for (i = 1; i <= MAX_NUM; i++)
|
||||
{
|
||||
heap[i] = expected[i] = bt_random();
|
||||
HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
|
||||
show_heap();
|
||||
bt_assert(is_heap_valid(heap, num));
|
||||
}
|
||||
|
||||
for (i = 1; i <= MAX_NUM; i++)
|
||||
for (j = 1; j <= MAX_NUM; j++)
|
||||
if(expected[i] == heap[j])
|
||||
break;
|
||||
else if (j == MAX_NUM)
|
||||
{
|
||||
show_heap();
|
||||
bt_abort_msg("Did not find a number %d in heap.", expected[i]);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_heap_insert, "Inserting a descending sequence of numbers (the worst case)");
|
||||
bt_test_suite(t_heap_insert_random, "Inserting pseudo-random numbers");
|
||||
bt_test_suite(t_heap_increase_decrease, "Increasing/Decreasing");
|
||||
bt_test_suite(t_heap_delete, "Deleting");
|
||||
bt_test_suite(t_heap_0, "Is a heap[0] really unused?");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
76
lib/idm.c
Normal file
76
lib/idm.c
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* BIRD Library -- ID Map
|
||||
*
|
||||
* (c) 2013--2015 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2013--2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/idm.h"
|
||||
#include "lib/resource.h"
|
||||
#include "lib/string.h"
|
||||
|
||||
|
||||
void
|
||||
idm_init(struct idm *m, pool *p, uint size)
|
||||
{
|
||||
m->pos = 0;
|
||||
m->used = 1;
|
||||
m->size = size;
|
||||
m->data = mb_allocz(p, m->size * sizeof(u32));
|
||||
|
||||
/* ID 0 is reserved */
|
||||
m->data[0] = 1;
|
||||
}
|
||||
|
||||
static inline int u32_cto(uint x) { return ffs(~x) - 1; }
|
||||
|
||||
u32
|
||||
idm_alloc(struct idm *m)
|
||||
{
|
||||
uint i, j;
|
||||
|
||||
for (i = m->pos; i < m->size; i++)
|
||||
if (m->data[i] != 0xffffffff)
|
||||
goto found;
|
||||
|
||||
/* If we are at least 7/8 full, expand */
|
||||
if (m->used > (m->size * 28))
|
||||
{
|
||||
m->size *= 2;
|
||||
m->data = mb_realloc(m->data, m->size * sizeof(u32));
|
||||
memset(m->data + i, 0, (m->size - i) * sizeof(u32));
|
||||
goto found;
|
||||
}
|
||||
|
||||
for (i = 0; i < m->pos; i++)
|
||||
if (m->data[i] != 0xffffffff)
|
||||
goto found;
|
||||
|
||||
ASSERT(0);
|
||||
|
||||
found:
|
||||
ASSERT(i < 0x8000000);
|
||||
|
||||
m->pos = i;
|
||||
j = u32_cto(m->data[i]);
|
||||
|
||||
m->data[i] |= (1 << j);
|
||||
m->used++;
|
||||
return 32 * i + j;
|
||||
}
|
||||
|
||||
void
|
||||
idm_free(struct idm *m, u32 id)
|
||||
{
|
||||
uint i = id / 32;
|
||||
uint j = id % 32;
|
||||
|
||||
ASSERT((i < m->size) && (m->data[i] & (1 << j)));
|
||||
m->data[i] &= ~(1 << j);
|
||||
m->used--;
|
||||
}
|
25
lib/idm.h
Normal file
25
lib/idm.h
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* BIRD Library -- ID Map
|
||||
*
|
||||
* (c) 2013--2015 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2013--2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_IDM_H_
|
||||
#define _BIRD_IDM_H_
|
||||
|
||||
struct idm
|
||||
{
|
||||
u32 *data;
|
||||
u32 pos;
|
||||
u32 used;
|
||||
u32 size;
|
||||
};
|
||||
|
||||
void idm_init(struct idm *m, pool *p, uint size);
|
||||
u32 idm_alloc(struct idm *m);
|
||||
void idm_free(struct idm *m, u32 id);
|
||||
|
||||
#endif
|
15
lib/ip.c
15
lib/ip.c
@ -58,7 +58,7 @@ ip6_mkmask(uint n)
|
||||
return a;
|
||||
}
|
||||
|
||||
int
|
||||
uint
|
||||
ip6_masklen(ip6_addr *a)
|
||||
{
|
||||
int i, j, n;
|
||||
@ -67,12 +67,12 @@ ip6_masklen(ip6_addr *a)
|
||||
if (a->addr[i] != ~0U)
|
||||
{
|
||||
j = u32_masklen(a->addr[i]);
|
||||
if (j < 0)
|
||||
if (j == 255)
|
||||
return j;
|
||||
n += j;
|
||||
while (++i < 4)
|
||||
if (a->addr[i])
|
||||
return -1;
|
||||
return 255;
|
||||
break;
|
||||
}
|
||||
|
||||
@ -245,7 +245,7 @@ ip4_pton(const char *a, ip4_addr *o)
|
||||
char *d, *c = strchr(a, '.');
|
||||
if (!c != !i)
|
||||
return 0;
|
||||
l = strtoul(a, &d, 10);
|
||||
l = bstrtoul10(a, &d);
|
||||
if (((d != c) && *d) || (l > 255))
|
||||
return 0;
|
||||
ia = (ia << 8) | l;
|
||||
@ -264,6 +264,9 @@ ip6_pton(const char *a, ip6_addr *o)
|
||||
int i, j, k, l, hfil;
|
||||
const char *start;
|
||||
|
||||
if (!a[0]) /* Empty string check */
|
||||
return 0;
|
||||
|
||||
if (a[0] == ':') /* Leading :: */
|
||||
{
|
||||
if (a[1] != ':')
|
||||
@ -306,7 +309,7 @@ ip6_pton(const char *a, ip6_addr *o)
|
||||
|
||||
if (*a == ':' && a[1])
|
||||
a++;
|
||||
else if (*a == '.' && (i == 6 || i < 6 && hfil >= 0))
|
||||
else if (*a == '.' && (i == 6 || (i < 6 && hfil >= 0)))
|
||||
{ /* Embedded IPv4 address */
|
||||
ip4_addr x;
|
||||
if (!ip4_pton(start, &x))
|
||||
@ -333,6 +336,8 @@ ip6_pton(const char *a, ip6_addr *o)
|
||||
for (; i>=hfil; i--)
|
||||
words[i] = 0;
|
||||
}
|
||||
else if (i != 8) /* Incomplete address */
|
||||
return 0;
|
||||
|
||||
/* Convert the address to ip6_addr format */
|
||||
for (i=0; i<4; i++)
|
||||
|
204
lib/ip.h
204
lib/ip.h
@ -9,7 +9,7 @@
|
||||
#ifndef _BIRD_IP_H_
|
||||
#define _BIRD_IP_H_
|
||||
|
||||
#include "lib/endian.h"
|
||||
#include "sysdep/unix/endian.h"
|
||||
#include "lib/string.h"
|
||||
#include "lib/bitops.h"
|
||||
#include "lib/unaligned.h"
|
||||
@ -31,6 +31,13 @@
|
||||
#define IP4_NONE _MI4(0)
|
||||
#define IP6_NONE _MI6(0,0,0,0)
|
||||
|
||||
#define IP4_MAX_PREFIX_LENGTH 32
|
||||
#define IP6_MAX_PREFIX_LENGTH 128
|
||||
|
||||
#define IP4_MAX_TEXT_LENGTH 15 /* "255.255.255.255" */
|
||||
#define IP6_MAX_TEXT_LENGTH 39 /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" */
|
||||
#define IPA_MAX_TEXT_LENGTH 39
|
||||
|
||||
#define IP4_MIN_MTU 576
|
||||
#define IP6_MIN_MTU 1280
|
||||
|
||||
@ -41,17 +48,11 @@
|
||||
#define UDP_HEADER_LENGTH 8
|
||||
|
||||
|
||||
#ifdef IPV6
|
||||
#define MAX_PREFIX_LENGTH 128
|
||||
#define BITS_PER_IP_ADDRESS 128
|
||||
#define STD_ADDRESS_P_LENGTH 39
|
||||
#define SIZE_OF_IP_HEADER 40
|
||||
#else
|
||||
#define MAX_PREFIX_LENGTH 32
|
||||
#define BITS_PER_IP_ADDRESS 32
|
||||
#define STD_ADDRESS_P_LENGTH 15
|
||||
#define SIZE_OF_IP_HEADER 24
|
||||
#endif
|
||||
/* IANA Address Family Numbers */
|
||||
/* https://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml */
|
||||
/* Would use AF_ prefix, but that collides with POSIX address family numbers */
|
||||
#define AFI_IPV4 1
|
||||
#define AFI_IPV6 2
|
||||
|
||||
|
||||
#ifdef DEBUGGING
|
||||
@ -84,8 +85,6 @@ typedef struct ip6_addr {
|
||||
#define _I3(a) ((a).addr[3])
|
||||
|
||||
|
||||
#ifdef IPV6
|
||||
|
||||
/* Structure ip_addr may contain both IPv4 and IPv6 addresses */
|
||||
typedef ip6_addr ip_addr;
|
||||
#define IPA_NONE IP6_NONE
|
||||
@ -101,24 +100,8 @@ typedef ip6_addr ip_addr;
|
||||
#define ipa_is_ip4(a) ip6_is_v4mapped(a)
|
||||
#define ipa_is_ip6(a) (! ip6_is_v4mapped(a))
|
||||
|
||||
#else
|
||||
|
||||
/* Provisionary ip_addr definition same as ip4_addr */
|
||||
typedef ip4_addr ip_addr;
|
||||
#define IPA_NONE IP4_NONE
|
||||
|
||||
#define ipa_from_ip4(x) x
|
||||
#define ipa_from_ip6(x) IPA_NONE
|
||||
#define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
|
||||
|
||||
#define ipa_to_ip4(x) x
|
||||
#define ipa_to_ip6(x) IP6_NONE
|
||||
#define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
|
||||
|
||||
#define ipa_is_ip4(a) 1
|
||||
#define ipa_is_ip6(a) 0
|
||||
|
||||
#endif
|
||||
#define IPA_NONE4 ipa_from_ip4(IP4_NONE)
|
||||
#define IPA_NONE6 ipa_from_ip6(IP6_NONE)
|
||||
|
||||
|
||||
/*
|
||||
@ -183,7 +166,6 @@ static inline ip6_addr ip6_not(ip6_addr a)
|
||||
{ return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
|
||||
|
||||
|
||||
#ifdef IPV6
|
||||
#define ipa_equal(x,y) ip6_equal(x,y)
|
||||
#define ipa_zero(x) ip6_zero(x)
|
||||
#define ipa_nonzero(x) ip6_nonzero(x)
|
||||
@ -191,19 +173,8 @@ static inline ip6_addr ip6_not(ip6_addr a)
|
||||
#define ipa_or(x,y) ip6_or(x,y)
|
||||
#define ipa_xor(x,y) ip6_xor(x,y)
|
||||
#define ipa_not(x) ip6_not(x)
|
||||
#else
|
||||
#define ipa_equal(x,y) ip4_equal(x,y)
|
||||
#define ipa_zero(x) ip4_zero(x)
|
||||
#define ipa_nonzero(x) ip4_nonzero(x)
|
||||
#define ipa_and(x,y) ip4_and(x,y)
|
||||
#define ipa_or(x,y) ip4_or(x,y)
|
||||
#define ipa_xor(x,y) ip4_xor(x,y)
|
||||
#define ipa_not(x) ip4_not(x)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef IPV6
|
||||
/*
|
||||
* A zero address is either a token for invalid/unused, or the prefix of default
|
||||
* routes. These functions should be used in the second case, where both IPv4
|
||||
@ -216,42 +187,15 @@ static inline int ipa_zero2(ip_addr a)
|
||||
static inline int ipa_nonzero2(ip_addr a)
|
||||
{ return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
|
||||
|
||||
#else
|
||||
#define ipa_zero2(x) ip4_zero(x)
|
||||
#define ipa_nonzero2(x) ip4_nonzero(x)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Hash and compare functions
|
||||
*/
|
||||
|
||||
static inline uint ip4_hash(ip4_addr a)
|
||||
{
|
||||
/* Returns a 16-bit value */
|
||||
u32 x = _I(a);
|
||||
x ^= x >> 16;
|
||||
x ^= x << 10;
|
||||
return x & 0xffff;
|
||||
}
|
||||
static inline u32 ip4_hash(ip4_addr a)
|
||||
{ return u32_hash(_I(a)); }
|
||||
|
||||
static inline u32 ip4_hash32(ip4_addr a)
|
||||
{
|
||||
/* Returns a 32-bit value, although low-order bits are not mixed */
|
||||
u32 x = _I(a);
|
||||
x ^= x << 16;
|
||||
x ^= x << 12;
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline uint ip6_hash(ip6_addr a)
|
||||
{
|
||||
/* Returns a 16-bit hash key */
|
||||
u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
|
||||
return (x ^ (x >> 16) ^ (x >> 8)) & 0xffff;
|
||||
}
|
||||
|
||||
static inline u32 ip6_hash32(ip6_addr a)
|
||||
static inline u32 ip6_hash(ip6_addr a)
|
||||
{
|
||||
/* Returns a 32-bit hash key, although low-order bits are not mixed */
|
||||
u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
|
||||
@ -263,16 +207,8 @@ static inline int ip4_compare(ip4_addr a, ip4_addr b)
|
||||
|
||||
int ip6_compare(ip6_addr a, ip6_addr b);
|
||||
|
||||
|
||||
#ifdef IPV6
|
||||
#define ipa_hash(x) ip6_hash(x)
|
||||
#define ipa_hash32(x) ip6_hash32(x)
|
||||
#define ipa_compare(x,y) ip6_compare(x,y)
|
||||
#else
|
||||
#define ipa_hash(x) ip4_hash(x)
|
||||
#define ipa_hash32(x) ip4_hash32(x)
|
||||
#define ipa_compare(x,y) ip4_compare(x,y)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@ -303,17 +239,13 @@ static inline int ip6_is_link_local(ip6_addr a)
|
||||
static inline int ip6_is_v4mapped(ip6_addr a)
|
||||
{ return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
|
||||
|
||||
#ifdef IPV6
|
||||
#define ipa_classify(x) ip6_classify(&(x))
|
||||
#define ipa_is_link_local(x) ip6_is_link_local(x)
|
||||
#else
|
||||
#define ipa_classify(x) ip4_classify(x)
|
||||
#define ipa_is_link_local(x) 0
|
||||
#endif
|
||||
|
||||
static inline int ip4_is_unicast(ip4_addr a)
|
||||
{ return _I(a) < 0xe0000000; }
|
||||
|
||||
/* XXXX remove */
|
||||
static inline int ipa_classify_net(ip_addr a)
|
||||
{ return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
|
||||
|
||||
@ -325,11 +257,11 @@ static inline int ipa_classify_net(ip_addr a)
|
||||
static inline ip4_addr ip4_mkmask(uint n)
|
||||
{ return _MI4(u32_mkmask(n)); }
|
||||
|
||||
static inline int ip4_masklen(ip4_addr a)
|
||||
static inline uint ip4_masklen(ip4_addr a)
|
||||
{ return u32_masklen(_I(a)); }
|
||||
|
||||
ip6_addr ip6_mkmask(uint n);
|
||||
int ip6_masklen(ip6_addr *a);
|
||||
uint ip6_masklen(ip6_addr *a);
|
||||
|
||||
/* ipX_pxlen() requires that x != y */
|
||||
static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
|
||||
@ -351,6 +283,18 @@ static inline u32 ip4_getbit(ip4_addr a, uint pos)
|
||||
static inline u32 ip6_getbit(ip6_addr a, uint pos)
|
||||
{ return a.addr[pos / 32] & (0x80000000 >> (pos % 32)); }
|
||||
|
||||
static inline u32 ip4_setbit(ip4_addr *a, uint pos)
|
||||
{ return _I(*a) |= (0x80000000 >> pos); }
|
||||
|
||||
static inline u32 ip6_setbit(ip6_addr *a, uint pos)
|
||||
{ return a->addr[pos / 32] |= (0x80000000 >> (pos % 32)); }
|
||||
|
||||
static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
|
||||
{ return _I(*a) &= ~(0x80000000 >> pos); }
|
||||
|
||||
static inline u32 ip6_clrbit(ip6_addr *a, uint pos)
|
||||
{ return a->addr[pos / 32] &= ~(0x80000000 >> (pos % 32)); }
|
||||
|
||||
static inline ip4_addr ip4_opposite_m1(ip4_addr a)
|
||||
{ return _MI4(_I(a) ^ 1); }
|
||||
|
||||
@ -365,21 +309,8 @@ static inline ip6_addr ip6_opposite_m2(ip6_addr a)
|
||||
|
||||
ip4_addr ip4_class_mask(ip4_addr ad);
|
||||
|
||||
#ifdef IPV6
|
||||
#define ipa_mkmask(x) ip6_mkmask(x)
|
||||
#define ipa_masklen(x) ip6_masklen(&x)
|
||||
#define ipa_pxlen(x,y) ip6_pxlen(x,y)
|
||||
#define ipa_getbit(x,n) ip6_getbit(x,n)
|
||||
#define ipa_opposite_m1(x) ip6_opposite_m1(x)
|
||||
#define ipa_opposite_m2(x) ip6_opposite_m2(x)
|
||||
#else
|
||||
#define ipa_mkmask(x) ip4_mkmask(x)
|
||||
#define ipa_masklen(x) ip4_masklen(x)
|
||||
#define ipa_pxlen(x,y) ip4_pxlen(x,y)
|
||||
#define ipa_getbit(x,n) ip4_getbit(x,n)
|
||||
#define ipa_opposite_m1(x) ip4_opposite_m1(x)
|
||||
#define ipa_opposite_m2(x) ip4_opposite_m2(x)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
@ -398,25 +329,44 @@ static inline ip6_addr ip6_hton(ip6_addr a)
|
||||
static inline ip6_addr ip6_ntoh(ip6_addr a)
|
||||
{ return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
|
||||
|
||||
#ifdef IPV6
|
||||
#define ipa_hton(x) x = ip6_hton(x)
|
||||
#define ipa_ntoh(x) x = ip6_ntoh(x)
|
||||
#else
|
||||
#define ipa_hton(x) x = ip4_hton(x)
|
||||
#define ipa_ntoh(x) x = ip4_ntoh(x)
|
||||
#endif
|
||||
#define MPLS_MAX_LABEL_STACK 8
|
||||
typedef struct mpls_label_stack {
|
||||
uint len;
|
||||
u32 stack[MPLS_MAX_LABEL_STACK];
|
||||
} mpls_label_stack;
|
||||
|
||||
static inline int
|
||||
mpls_get(const char *buf, int buflen, u32 *stack)
|
||||
{
|
||||
for (int i=0; (i<MPLS_MAX_LABEL_STACK) && (i*4+3 < buflen); i++)
|
||||
{
|
||||
u32 s = get_u32(buf + i*4);
|
||||
stack[i] = s >> 12;
|
||||
if (s & 0x100)
|
||||
return i+1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline int
|
||||
mpls_put(char *buf, int len, u32 *stack)
|
||||
{
|
||||
for (int i=0; i<len; i++)
|
||||
put_u32(buf + i*4, stack[i] << 12 | (i+1 == len ? 0x100 : 0));
|
||||
|
||||
return len*4;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unaligned data access (in network order)
|
||||
*/
|
||||
|
||||
static inline ip4_addr get_ip4(void *buf)
|
||||
static inline ip4_addr get_ip4(const void *buf)
|
||||
{
|
||||
return _MI4(get_u32(buf));
|
||||
}
|
||||
|
||||
static inline ip6_addr get_ip6(void *buf)
|
||||
static inline ip6_addr get_ip6(const void *buf)
|
||||
{
|
||||
ip6_addr a;
|
||||
memcpy(&a, buf, 16);
|
||||
@ -436,15 +386,6 @@ static inline void * put_ip6(void *buf, ip6_addr a)
|
||||
return buf+16;
|
||||
}
|
||||
|
||||
// XXXX these functions must be redesigned or removed
|
||||
#ifdef IPV6
|
||||
#define get_ipa(x) get_ip6(x)
|
||||
#define put_ipa(x,y) put_ip6(x,y)
|
||||
#else
|
||||
#define get_ipa(x) get_ip4(x)
|
||||
#define put_ipa(x,y) put_ip4(x,y)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Binary/text form conversions
|
||||
@ -462,34 +403,11 @@ static inline char * ip6_ntox(ip6_addr a, char *b)
|
||||
int ip4_pton(const char *a, ip4_addr *o);
|
||||
int ip6_pton(const char *a, ip6_addr *o);
|
||||
|
||||
// XXXX these functions must be redesigned or removed
|
||||
#ifdef IPV6
|
||||
#define ipa_ntop(x,y) ip6_ntop(x,y)
|
||||
#define ipa_ntox(x,y) ip6_ntox(x,y)
|
||||
#define ipa_pton(x,y) ip6_pton(x,y)
|
||||
#else
|
||||
#define ipa_ntop(x,y) ip4_ntop(x,y)
|
||||
#define ipa_ntox(x,y) ip4_ntox(x,y)
|
||||
#define ipa_pton(x,y) ip4_pton(x,y)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Miscellaneous
|
||||
*/
|
||||
|
||||
// XXXX review this
|
||||
|
||||
#define ip_is_prefix(a,l) (!ipa_nonzero(ipa_and(a, ipa_not(ipa_mkmask(l)))))
|
||||
#define ipa_in_net(x,n,p) (ipa_zero(ipa_and(ipa_xor((n),(x)),ipa_mkmask(p))))
|
||||
#define net_in_net(n1,l1,n2,l2) (((l1) >= (l2)) && (ipa_zero(ipa_and(ipa_xor((n1),(n2)),ipa_mkmask(l2)))))
|
||||
|
||||
char *ip_scope_text(uint);
|
||||
|
||||
struct prefix {
|
||||
ip_addr addr;
|
||||
uint len;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
182
lib/ip_test.c
Normal file
182
lib/ip_test.c
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* BIRD Library -- IP address functions Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/ip.h"
|
||||
|
||||
#define IP4_MAX_LEN 16
|
||||
|
||||
static int
|
||||
test_ip4_pton(void *out_, const void *in_, const void *expected_out_)
|
||||
{
|
||||
ip_addr *out = out_;
|
||||
const char *in = in_;
|
||||
const ip_addr *expected_out = expected_out_;
|
||||
ip4_addr ip4;
|
||||
|
||||
if (expected_out)
|
||||
{
|
||||
bt_assert(ip4_pton(in, &ip4));
|
||||
*out = ipa_from_ip4(ip4);
|
||||
return ipa_equal(*out, *expected_out);
|
||||
}
|
||||
else
|
||||
return !ip4_pton(in, &ip4);
|
||||
|
||||
}
|
||||
|
||||
static int
|
||||
test_ip6_pton(void *out_, const void *in_, const void *expected_out_)
|
||||
{
|
||||
ip_addr *out = out_;
|
||||
const char *in = in_;
|
||||
const ip_addr *expected_out = expected_out_;
|
||||
|
||||
if (expected_out)
|
||||
{
|
||||
bt_assert(ip6_pton(in, out));
|
||||
return ipa_equal(*out, *expected_out);
|
||||
}
|
||||
else
|
||||
return !ip6_pton(in, out);
|
||||
}
|
||||
|
||||
static int
|
||||
t_ip4_pton(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = "192.168.1.128",
|
||||
.out = & ipa_build4(192, 168, 1, 128),
|
||||
},
|
||||
{
|
||||
.in = "255.255.255.255",
|
||||
.out = & ipa_build4(255, 255, 255, 255),
|
||||
},
|
||||
{
|
||||
.in = "0.0.0.0",
|
||||
.out = & ipa_build4(0, 0, 0, 0),
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_ip4_pton, bt_fmt_str, bt_fmt_ipa);
|
||||
}
|
||||
|
||||
static int
|
||||
t_ip6_pton(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = "2001:0db8:0000:0000:0000:0000:1428:57ab",
|
||||
.out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
|
||||
},
|
||||
{
|
||||
.in = "2001:0db8:0000:0000:0000::1428:57ab",
|
||||
.out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
|
||||
},
|
||||
{
|
||||
.in = "2001:0db8::1428:57ab",
|
||||
.out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
|
||||
},
|
||||
{
|
||||
.in = "2001:db8::1428:57ab",
|
||||
.out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
|
||||
},
|
||||
{
|
||||
.in = "::1",
|
||||
.out = & ipa_build6(0x00000000, 0x00000000, 0x00000000, 0x00000001),
|
||||
},
|
||||
{
|
||||
.in = "::",
|
||||
.out = & ipa_build6(0x00000000, 0x00000000, 0x00000000, 0x00000000),
|
||||
},
|
||||
{
|
||||
.in = "2605:2700:0:3::4713:93e3",
|
||||
.out = & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3),
|
||||
},
|
||||
{
|
||||
.in = "2605:2700:0:3:4713:93e3",
|
||||
.out = NULL,
|
||||
},
|
||||
{
|
||||
.in = "2",
|
||||
.out = NULL,
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_ip6_pton, bt_fmt_str, bt_fmt_ipa);
|
||||
}
|
||||
|
||||
static int
|
||||
test_ipa_ntop(void *out_, const void *in_, const void *expected_out_)
|
||||
{
|
||||
char *out = out_;
|
||||
const ip_addr *in = in_;
|
||||
const char *expected_out = expected_out_;
|
||||
|
||||
if (ipa_is_ip4(*in))
|
||||
ip4_ntop(ipa_to_ip4(*in), out);
|
||||
else
|
||||
ip6_ntop(ipa_to_ip6(*in), out);
|
||||
|
||||
int result = strncmp(out, expected_out, ipa_is_ip4(*in) ? IP4_MAX_TEXT_LENGTH : IP6_MAX_TEXT_LENGTH) == 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
static int
|
||||
t_ip4_ntop(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = & ipa_build4(192, 168, 1, 128),
|
||||
.out = "192.168.1.128",
|
||||
},
|
||||
{
|
||||
.in = & ipa_build4(255, 255, 255, 255),
|
||||
.out = "255.255.255.255",
|
||||
},
|
||||
{
|
||||
.in = & ipa_build4(0, 0, 0, 1),
|
||||
.out = "0.0.0.1",
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_ipa_ntop, bt_fmt_ipa, bt_fmt_str);
|
||||
}
|
||||
|
||||
static int
|
||||
t_ip6_ntop(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
|
||||
.out = "2001:db8::1428:57ab",
|
||||
},
|
||||
{
|
||||
.in = & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3),
|
||||
.out = "2605:2700:0:3::4713:93e3",
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_ipa_ntop, bt_fmt_ipa, bt_fmt_str);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_ip4_pton, "Converting IPv4 string to ip4_addr struct");
|
||||
bt_test_suite(t_ip6_pton, "Converting IPv6 string to ip6_addr struct");
|
||||
bt_test_suite(t_ip4_ntop, "Converting ip4_addr struct to IPv4 string");
|
||||
bt_test_suite(t_ip6_ntop, "Converting ip6_addr struct to IPv6 string");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
||||
|
91
lib/lists.c
91
lib/lists.c
@ -29,6 +29,42 @@
|
||||
#include "nest/bird.h"
|
||||
#include "lib/lists.h"
|
||||
|
||||
LIST_INLINE int
|
||||
check_list(list *l, node *n)
|
||||
{
|
||||
if (!l)
|
||||
{
|
||||
ASSERT_DIE(n);
|
||||
ASSERT_DIE(n->prev);
|
||||
|
||||
do { n = n->prev; } while (n->prev);
|
||||
|
||||
l = SKIP_BACK(list, head_node, n);
|
||||
}
|
||||
|
||||
int seen = 0;
|
||||
|
||||
ASSERT_DIE(l->null == NULL);
|
||||
ASSERT_DIE(l->head != NULL);
|
||||
ASSERT_DIE(l->tail != NULL);
|
||||
|
||||
node *prev = &l->head_node, *cur = l->head, *next = l->head->next;
|
||||
while (next)
|
||||
{
|
||||
if (cur == n)
|
||||
seen++;
|
||||
ASSERT_DIE(cur->prev == prev);
|
||||
prev = cur;
|
||||
cur = next;
|
||||
next = next->next;
|
||||
}
|
||||
|
||||
ASSERT_DIE(cur == &(l->tail_node));
|
||||
ASSERT_DIE(!n || (seen == 1));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* add_tail - append a node to a list
|
||||
* @l: linked list
|
||||
@ -39,6 +75,10 @@
|
||||
LIST_INLINE void
|
||||
add_tail(list *l, node *n)
|
||||
{
|
||||
EXPENSIVE_CHECK(check_list(l, NULL));
|
||||
ASSUME(n->prev == NULL);
|
||||
ASSUME(n->next == NULL);
|
||||
|
||||
node *z = l->tail;
|
||||
|
||||
n->next = &l->tail_node;
|
||||
@ -57,6 +97,10 @@ add_tail(list *l, node *n)
|
||||
LIST_INLINE void
|
||||
add_head(list *l, node *n)
|
||||
{
|
||||
EXPENSIVE_CHECK(check_list(l, NULL));
|
||||
ASSUME(n->prev == NULL);
|
||||
ASSUME(n->next == NULL);
|
||||
|
||||
node *z = l->head;
|
||||
|
||||
n->next = z;
|
||||
@ -76,6 +120,10 @@ add_head(list *l, node *n)
|
||||
LIST_INLINE void
|
||||
insert_node(node *n, node *after)
|
||||
{
|
||||
EXPENSIVE_CHECK(check_list(l, after));
|
||||
ASSUME(n->prev == NULL);
|
||||
ASSUME(n->next == NULL);
|
||||
|
||||
node *z = after->next;
|
||||
|
||||
n->next = z;
|
||||
@ -93,6 +141,8 @@ insert_node(node *n, node *after)
|
||||
LIST_INLINE void
|
||||
rem_node(node *n)
|
||||
{
|
||||
EXPENSIVE_CHECK(check_list(NULL, n));
|
||||
|
||||
node *z = n->prev;
|
||||
node *x = n->next;
|
||||
|
||||
@ -103,24 +153,20 @@ rem_node(node *n)
|
||||
}
|
||||
|
||||
/**
|
||||
* replace_node - replace a node in a list with another one
|
||||
* @old: node to be removed
|
||||
* @new: node to be inserted
|
||||
* update_node - update node after calling realloc on it
|
||||
* @n: node to be updated
|
||||
*
|
||||
* Replaces node @old in the list it's linked in with node @new. Node
|
||||
* @old may be a copy of the original node, which is not accessed
|
||||
* through the list. The function could be called with @old == @new,
|
||||
* which just fixes neighbors' pointers in the case that the node
|
||||
* was reallocated.
|
||||
* Fixes neighbor pointers.
|
||||
*/
|
||||
LIST_INLINE void
|
||||
replace_node(node *old, node *new)
|
||||
update_node(node *n)
|
||||
{
|
||||
old->next->prev = new;
|
||||
old->prev->next = new;
|
||||
ASSUME(n->next->prev == n->prev->next);
|
||||
|
||||
new->prev = old->prev;
|
||||
new->next = old->next;
|
||||
n->next->prev = n;
|
||||
n->prev->next = n;
|
||||
|
||||
EXPENSIVE_CHECK(check_list(NULL, n));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,6 +195,9 @@ init_list(list *l)
|
||||
LIST_INLINE void
|
||||
add_tail_list(list *to, list *l)
|
||||
{
|
||||
EXPENSIVE_CHECK(check_list(to, NULL));
|
||||
EXPENSIVE_CHECK(check_list(l, NULL));
|
||||
|
||||
node *p = to->tail;
|
||||
node *q = l->head;
|
||||
|
||||
@ -157,4 +206,20 @@ add_tail_list(list *to, list *l)
|
||||
q = l->tail;
|
||||
q->next = &to->tail_node;
|
||||
to->tail = q;
|
||||
|
||||
EXPENSIVE_CHECK(check_list(to, NULL));
|
||||
}
|
||||
|
||||
LIST_INLINE uint
|
||||
list_length(list *l)
|
||||
{
|
||||
uint len = 0;
|
||||
node *n;
|
||||
|
||||
EXPENSIVE_CHECK(check_list(l, NULL));
|
||||
|
||||
WALK_LIST(n, *l)
|
||||
len++;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
@ -52,7 +52,10 @@ typedef union list { /* In fact two overlayed nodes */
|
||||
#define WALK_LIST2(n,nn,list,pos) \
|
||||
for(nn=(list).head; NODE_VALID(nn) && (n=SKIP_BACK(typeof(*n),pos,nn)); nn=nn->next)
|
||||
#define WALK_LIST_DELSAFE(n,nxt,list) \
|
||||
for(n=HEAD(list); nxt=NODE_NEXT(n); n=(void *) nxt)
|
||||
for(n=HEAD(list); nxt=NODE_NEXT(n); n=(void *) nxt)
|
||||
#define WALK_LIST2_DELSAFE(n,nn,nxt,list,pos) \
|
||||
for(nn=HEAD(list); (nxt=nn->next) && (n=SKIP_BACK(typeof(*n),pos,nn)); nn=nxt)
|
||||
|
||||
/* WALK_LIST_FIRST supposes that called code removes each processed node */
|
||||
#define WALK_LIST_FIRST(n,list) \
|
||||
while(n=HEAD(list), (NODE (n))->next)
|
||||
@ -77,6 +80,7 @@ void rem_node(node *);
|
||||
void add_tail_list(list *, list *);
|
||||
void init_list(list *);
|
||||
void insert_node(node *, node *);
|
||||
uint list_length(list *);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
290
lib/lists_test.c
Normal file
290
lib/lists_test.c
Normal file
@ -0,0 +1,290 @@
|
||||
/*
|
||||
* BIRD Library -- Linked Lists Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
#include "lib/lists.h"
|
||||
|
||||
#define MAX_NUM 1000
|
||||
|
||||
static node nodes[MAX_NUM];
|
||||
static list l;
|
||||
|
||||
static void
|
||||
show_list(void)
|
||||
{
|
||||
bt_debug("\n");
|
||||
bt_debug("list.null is at %p and point to %p\n", &l.null, l.null);
|
||||
bt_debug("list.head is at %p and point to %p\n", &l.head, l.head);
|
||||
bt_debug("list.tail is at %p and point to %p\n", &l.tail, l.tail);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
bt_debug("n[%3i] is at %p\n", i, &nodes[i]);
|
||||
bt_debug(" prev is at %p and point to %p\n", &(nodes[i].prev), nodes[i].prev);
|
||||
bt_debug(" next is at %p and point to %p\n", &(nodes[i].next), nodes[i].next);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
is_filled_list_well_linked(void)
|
||||
{
|
||||
int i;
|
||||
bt_assert(l.head == &nodes[0]);
|
||||
bt_assert(l.tail == &nodes[MAX_NUM-1]);
|
||||
bt_assert((void *) nodes[0].prev == (void *) &l.head);
|
||||
bt_assert((void *) nodes[MAX_NUM-1].next == (void *) &l.null);
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
if (i < (MAX_NUM-1))
|
||||
bt_assert(nodes[i].next == &nodes[i+1]);
|
||||
|
||||
if (i > 0)
|
||||
bt_assert(nodes[i].prev == &nodes[i-1]);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
is_empty_list_well_unlinked(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
bt_assert(l.head == NODE &l.null);
|
||||
bt_assert(l.tail == NODE &l.head);
|
||||
bt_assert(EMPTY_LIST(l));
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
bt_assert(nodes[i].next == NULL);
|
||||
bt_assert(nodes[i].prev == NULL);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
init_list__(list *l, struct node nodes[])
|
||||
{
|
||||
init_list(l);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
nodes[i].next = NULL;
|
||||
nodes[i].prev = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
init_list_(void)
|
||||
{
|
||||
init_list__(&l, (node *) nodes);
|
||||
}
|
||||
|
||||
static int
|
||||
t_add_tail(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
add_tail(&l, &nodes[i]);
|
||||
bt_debug(".");
|
||||
bt_assert(l.tail == &nodes[i]);
|
||||
bt_assert(l.head == &nodes[0]);
|
||||
bt_assert((void *) nodes[i].next == (void *) &l.null);
|
||||
if (i > 0)
|
||||
{
|
||||
bt_assert(nodes[i-1].next == &nodes[i]);
|
||||
bt_assert(nodes[i].prev == &nodes[i-1]);
|
||||
}
|
||||
}
|
||||
show_list();
|
||||
bt_assert(is_filled_list_well_linked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_add_head(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
for (i = MAX_NUM-1; i >= 0; i--)
|
||||
{
|
||||
add_head(&l, &nodes[i]);
|
||||
bt_debug(".");
|
||||
bt_assert(l.head == &nodes[i]);
|
||||
bt_assert(l.tail == &nodes[MAX_NUM-1]);
|
||||
if (i < MAX_NUM-1)
|
||||
{
|
||||
bt_assert(nodes[i+1].prev == &nodes[i]);
|
||||
bt_assert(nodes[i].next == &nodes[i+1]);
|
||||
}
|
||||
}
|
||||
show_list();
|
||||
bt_assert(is_filled_list_well_linked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
insert_node_(node *n, node *after)
|
||||
{
|
||||
insert_node(n, after);
|
||||
bt_debug(".");
|
||||
}
|
||||
|
||||
static int
|
||||
t_insert_node(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
|
||||
// add first node
|
||||
insert_node_(&nodes[0], NODE &l.head);
|
||||
|
||||
// add odd nodes
|
||||
for (i = 2; i < MAX_NUM; i+=2)
|
||||
insert_node_(&nodes[i], &nodes[i-2]);
|
||||
|
||||
// add even nodes
|
||||
for (i = 1; i < MAX_NUM; i+=2)
|
||||
insert_node_(&nodes[i], &nodes[i-1]);
|
||||
|
||||
bt_debug("\n");
|
||||
bt_assert(is_filled_list_well_linked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
fill_list2(list *l, node nodes[])
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
add_tail(l, &nodes[i]);
|
||||
}
|
||||
|
||||
static void
|
||||
fill_list(void)
|
||||
{
|
||||
fill_list2(&l, (node *) nodes);
|
||||
}
|
||||
|
||||
static int
|
||||
t_remove_node(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
|
||||
/* Fill & Remove & Check */
|
||||
fill_list();
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
rem_node(&nodes[i]);
|
||||
bt_assert(is_empty_list_well_unlinked());
|
||||
|
||||
/* Fill & Remove the half of nodes & Check & Remove the rest nodes & Check */
|
||||
fill_list();
|
||||
for (i = 0; i < MAX_NUM; i+=2)
|
||||
rem_node(&nodes[i]);
|
||||
|
||||
int tail_node_index = (MAX_NUM % 2) ? MAX_NUM - 2 : MAX_NUM - 1;
|
||||
bt_assert(l.head == &nodes[1]);
|
||||
bt_assert(l.tail == &nodes[tail_node_index]);
|
||||
bt_assert(nodes[tail_node_index].next == NODE &l.null);
|
||||
|
||||
for (i = 1; i < MAX_NUM; i+=2)
|
||||
{
|
||||
if (i > 1)
|
||||
bt_assert(nodes[i].prev == &nodes[i-2]);
|
||||
if (i < tail_node_index)
|
||||
bt_assert(nodes[i].next == &nodes[i+2]);
|
||||
}
|
||||
|
||||
for (i = 1; i < MAX_NUM; i+=2)
|
||||
rem_node(&nodes[i]);
|
||||
bt_assert(is_empty_list_well_unlinked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_update_node(void)
|
||||
{
|
||||
node head, inside, tail;
|
||||
|
||||
init_list_();
|
||||
fill_list();
|
||||
|
||||
head = nodes[0];
|
||||
update_node(&head);
|
||||
bt_assert(l.head == &head);
|
||||
bt_assert(head.prev == NODE &l.head);
|
||||
bt_assert(head.next == &nodes[1]);
|
||||
bt_assert(nodes[1].prev == &head);
|
||||
|
||||
inside = nodes[MAX_NUM/2];
|
||||
update_node(&inside);
|
||||
bt_assert(nodes[MAX_NUM/2-1].next == &inside);
|
||||
bt_assert(nodes[MAX_NUM/2+1].prev == &inside);
|
||||
bt_assert(inside.prev == &nodes[MAX_NUM/2-1]);
|
||||
bt_assert(inside.next == &nodes[MAX_NUM/2+1]);
|
||||
|
||||
tail = nodes[MAX_NUM-1];
|
||||
update_node(&tail);
|
||||
bt_assert(l.tail == &tail);
|
||||
bt_assert(tail.prev == &nodes[MAX_NUM-2]);
|
||||
bt_assert(tail.next == NODE &l.null);
|
||||
bt_assert(nodes[MAX_NUM-2].next == &tail);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_add_tail_list(void)
|
||||
{
|
||||
node nodes2[MAX_NUM];
|
||||
list l2;
|
||||
|
||||
init_list__(&l, (node *) nodes);
|
||||
fill_list2(&l, (node *) nodes);
|
||||
|
||||
init_list__(&l2, (node *) nodes2);
|
||||
fill_list2(&l2, (node *) nodes2);
|
||||
|
||||
add_tail_list(&l, &l2);
|
||||
|
||||
bt_assert(nodes[MAX_NUM-1].next == &nodes2[0]);
|
||||
bt_assert(nodes2[0].prev == &nodes[MAX_NUM-1]);
|
||||
bt_assert(l.tail == &nodes2[MAX_NUM-1]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_add_tail, "Adding nodes to tail of list");
|
||||
bt_test_suite(t_add_head, "Adding nodes to head of list");
|
||||
bt_test_suite(t_insert_node, "Inserting nodes to list");
|
||||
bt_test_suite(t_remove_node, "Removing nodes from list");
|
||||
bt_test_suite(t_update_node, "Updating nodes in list");
|
||||
bt_test_suite(t_add_tail_list, "At the tail of a list adding the another list");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
1159
lib/mac_test.c
Normal file
1159
lib/mac_test.c
Normal file
File diff suppressed because it is too large
Load Diff
98
lib/macro.h
Normal file
98
lib/macro.h
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* BIRD Macro Tricks
|
||||
*
|
||||
* (c) 2018 Jan Maria Matejka <mq@jmq.cz>
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*
|
||||
* Contains useful but dirty macro tricks:
|
||||
* MACRO_CONCAT(a, b) -> concatenates a##b
|
||||
* MACRO_BOOL(x) -> convert 0 to 0, anything else to 1
|
||||
* MACRO_IFELSE(b)(true-branch)(false-branch)
|
||||
* -> b shall be 0 or 1; expands to the appropriate branch
|
||||
* MACRO_ISEMPTY(...) -> 1 for empty argument list, 0 otherwise
|
||||
* MACRO_FOREACH(func, ...)
|
||||
* -> calling FOREACH(func, a, b, c, d) expands to
|
||||
* func(a) func(b) func(c) func(d)
|
||||
* MACRO_RPACK(func, terminator, ...)
|
||||
* -> packs the list into recursive calls:
|
||||
* func(func(func(func(terminator, a), b), c), d)
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_MACRO_H_
|
||||
#define _BIRD_MACRO_H_
|
||||
|
||||
/* What to do with args */
|
||||
#define MACRO_DROP(...)
|
||||
#define MACRO_UNPAREN(...) __VA_ARGS__
|
||||
#define MACRO_SEP(a, b, sep) a sep b
|
||||
|
||||
/* Aliases for some special chars */
|
||||
#define MACRO_COMMA ,
|
||||
#define MACRO_LPAREN (
|
||||
#define MACRO_RPAREN )
|
||||
#define MACRO_LPAREN_() (
|
||||
#define MACRO_RPAREN_() )
|
||||
|
||||
/* Multiple expansion trick */
|
||||
#define MACRO_EXPAND0(...) __VA_ARGS__
|
||||
#define MACRO_EXPAND1(...) MACRO_EXPAND0(MACRO_EXPAND0(__VA_ARGS__))
|
||||
#define MACRO_EXPAND2(...) MACRO_EXPAND1(MACRO_EXPAND1(__VA_ARGS__))
|
||||
#define MACRO_EXPAND3(...) MACRO_EXPAND2(MACRO_EXPAND2(__VA_ARGS__))
|
||||
#define MACRO_EXPAND(...) MACRO_EXPAND3(MACRO_EXPAND3(__VA_ARGS__))
|
||||
|
||||
/* Deferring expansion in the expansion trick */
|
||||
#define MACRO_EMPTY()
|
||||
#define MACRO_DEFER(t) t MACRO_EMPTY()
|
||||
#define MACRO_DEFER2(t) t MACRO_EMPTY MACRO_EMPTY()()
|
||||
#define MACRO_DEFER3(t) t MACRO_EMPTY MACRO_EMPTY MACRO_EMPTY()()()
|
||||
|
||||
/* Token concatenation */
|
||||
#define MACRO_CONCAT(prefix, ...) prefix##__VA_ARGS__
|
||||
#define MACRO_CONCAT_AFTER(...) MACRO_CONCAT(__VA_ARGS__)
|
||||
|
||||
/* Get first or second argument only */
|
||||
#define MACRO_FIRST(a, ...) a
|
||||
#define MACRO_SECOND(a, b, ...) b
|
||||
#define MACRO_SECOND_OR_ZERO(...) MACRO_SECOND(__VA_ARGS__, 0,)
|
||||
|
||||
/* Macro Boolean auxiliary macros */
|
||||
#define MACRO_BOOL_CHECK_0 ~, 1
|
||||
#define MACRO_BOOL_NEG(x) MACRO_SECOND_OR_ZERO(MACRO_CONCAT(MACRO_BOOL_CHECK_, x))
|
||||
|
||||
#define MACRO_BOOL_NOT_0 1
|
||||
#define MACRO_BOOL_NOT_1 0
|
||||
|
||||
/* Macro Boolean negation */
|
||||
#define MACRO_NOT(x) MACRO_CONCAT(MACRO_BOOL_NOT_, x)
|
||||
|
||||
/* Convert anything to bool (anything -> 1, 0 -> 0) */
|
||||
#define MACRO_BOOL(x) MACRO_NOT(MACRO_BOOL_NEG(x))
|
||||
|
||||
/*
|
||||
* Macro If/Else condition
|
||||
* Usage: MACRO_IFELSE(condition)(true-branch)(false-branch)
|
||||
* Expands to true-branch if condition is true, otherwise to false-branch.
|
||||
*/
|
||||
#define MACRO_IFELSE(b) MACRO_CONCAT(MACRO_IFELSE_, b)
|
||||
#define MACRO_IFELSE_0(...) MACRO_UNPAREN
|
||||
#define MACRO_IFELSE_1(...) __VA_ARGS__ MACRO_DROP
|
||||
|
||||
/* Auxiliary macros for MACRO_FOREACH */
|
||||
#define MACRO_ISLAST(...) MACRO_BOOL_NEG(MACRO_FIRST(MACRO_ISLAST_CHECK __VA_ARGS__)())
|
||||
#define MACRO_ISLAST_CHECK() 0
|
||||
|
||||
#define MACRO_FOREACH_EXPAND(call, a, ...) MACRO_IFELSE(MACRO_ISLAST(__VA_ARGS__))(call(a))(call(a) MACRO_DEFER2(MACRO_FOREACH_PAREN)()(call, __VA_ARGS__))
|
||||
#define MACRO_FOREACH_PAREN() MACRO_FOREACH_EXPAND
|
||||
|
||||
#define MACRO_RPACK_EXPAND(call, terminator, a, ...) MACRO_IFELSE(MACRO_ISLAST(__VA_ARGS__))(call(terminator, a))(call(MACRO_DEFER2(MACRO_RPACK_PAREN)()(call, terminator, __VA_ARGS__), a))
|
||||
#define MACRO_RPACK_PAREN() MACRO_RPACK_EXPAND
|
||||
/*
|
||||
* Call the first argument for each following:
|
||||
* MACRO_FOREACH(func, a, b, c, d) expands to func(a) func(b) func(c) func(d).
|
||||
* It supports also macros as func.
|
||||
*/
|
||||
#define MACRO_FOREACH(call, ...) MACRO_EXPAND(MACRO_FOREACH_EXPAND(call, __VA_ARGS__))
|
||||
#define MACRO_RPACK(call, terminator, ...) MACRO_EXPAND(MACRO_RPACK_EXPAND(call, terminator, __VA_ARGS__))
|
||||
|
||||
#endif
|
@ -11,7 +11,7 @@
|
||||
*
|
||||
* Linear memory pools are collections of memory blocks which
|
||||
* support very fast allocation of new blocks, but are able to free only
|
||||
* the whole collection at once.
|
||||
* the whole collection at once (or in stack order).
|
||||
*
|
||||
* Example: Each configuration is described by a complex system of structures,
|
||||
* linked lists and function trees which are all allocated from a single linear
|
||||
@ -32,10 +32,12 @@ struct lp_chunk {
|
||||
byte data[0];
|
||||
};
|
||||
|
||||
const int lp_chunk_size = sizeof(struct lp_chunk);
|
||||
|
||||
struct linpool {
|
||||
resource r;
|
||||
byte *ptr, *end;
|
||||
struct lp_chunk *first, *current, **plast; /* Normal (reusable) chunks */
|
||||
struct lp_chunk *first, *current; /* Normal (reusable) chunks */
|
||||
struct lp_chunk *first_large; /* Large chunks */
|
||||
uint chunk_size, threshold, total, total_large;
|
||||
};
|
||||
@ -67,7 +69,6 @@ linpool
|
||||
*lp_new(pool *p, uint blk)
|
||||
{
|
||||
linpool *m = ralloc(p, &lp_class);
|
||||
m->plast = &m->first;
|
||||
m->chunk_size = blk;
|
||||
m->threshold = 3*blk/4;
|
||||
return m;
|
||||
@ -112,22 +113,25 @@ lp_alloc(linpool *m, uint size)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m->current)
|
||||
if (m->current && m->current->next)
|
||||
{
|
||||
/* Still have free chunks from previous incarnation (before lp_flush()) */
|
||||
c = m->current;
|
||||
m->current = c->next;
|
||||
c = m->current->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Need to allocate a new chunk */
|
||||
c = xmalloc(sizeof(struct lp_chunk) + m->chunk_size);
|
||||
m->total += m->chunk_size;
|
||||
*m->plast = c;
|
||||
m->plast = &c->next;
|
||||
c->next = NULL;
|
||||
c->size = m->chunk_size;
|
||||
|
||||
if (m->current)
|
||||
m->current->next = c;
|
||||
else
|
||||
m->first = c;
|
||||
}
|
||||
m->current = c;
|
||||
m->ptr = c->data + size;
|
||||
m->end = c->data + m->chunk_size;
|
||||
}
|
||||
@ -188,9 +192,11 @@ lp_flush(linpool *m)
|
||||
{
|
||||
struct lp_chunk *c;
|
||||
|
||||
/* Relink all normal chunks to free list and free all large chunks */
|
||||
m->ptr = m->end = NULL;
|
||||
m->current = m->first;
|
||||
/* Move ptr to the first chunk and free all large chunks */
|
||||
m->current = c = m->first;
|
||||
m->ptr = c ? c->data : NULL;
|
||||
m->end = c ? c->data + m->chunk_size : NULL;
|
||||
|
||||
while (c = m->first_large)
|
||||
{
|
||||
m->first_large = c->next;
|
||||
@ -199,6 +205,50 @@ lp_flush(linpool *m)
|
||||
m->total_large = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* lp_save - save the state of a linear memory pool
|
||||
* @m: linear memory pool
|
||||
* @p: state buffer
|
||||
*
|
||||
* This function saves the state of a linear memory pool. Saved state can be
|
||||
* used later to restore the pool (to free memory allocated since).
|
||||
*/
|
||||
void
|
||||
lp_save(linpool *m, lp_state *p)
|
||||
{
|
||||
p->current = m->current;
|
||||
p->large = m->first_large;
|
||||
p->ptr = m->ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* lp_restore - restore the state of a linear memory pool
|
||||
* @m: linear memory pool
|
||||
* @p: saved state
|
||||
*
|
||||
* This function restores the state of a linear memory pool, freeing all memory
|
||||
* allocated since the state was saved. Note that the function cannot un-free
|
||||
* the memory, therefore the function also invalidates other states that were
|
||||
* saved between (on the same pool).
|
||||
*/
|
||||
void
|
||||
lp_restore(linpool *m, lp_state *p)
|
||||
{
|
||||
struct lp_chunk *c;
|
||||
|
||||
/* Move ptr to the saved pos and free all newer large chunks */
|
||||
m->current = c = p->current;
|
||||
m->ptr = p->ptr;
|
||||
m->end = c ? c->data + m->chunk_size : NULL;
|
||||
|
||||
while ((c = m->first_large) && (c != p->large))
|
||||
{
|
||||
m->first_large = c->next;
|
||||
m->total_large -= c->size;
|
||||
xfree(c);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
lp_free(resource *r)
|
||||
{
|
||||
|
331
lib/net.c
Normal file
331
lib/net.c
Normal file
@ -0,0 +1,331 @@
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/ip.h"
|
||||
#include "lib/net.h"
|
||||
#include "lib/flowspec.h"
|
||||
|
||||
|
||||
const char * const net_label[] = {
|
||||
[NET_IP4] = "ipv4",
|
||||
[NET_IP6] = "ipv6",
|
||||
[NET_VPN4] = "vpn4",
|
||||
[NET_VPN6] = "vpn6",
|
||||
[NET_ROA4] = "roa4",
|
||||
[NET_ROA6] = "roa6",
|
||||
[NET_FLOW4] = "flow4",
|
||||
[NET_FLOW6] = "flow6",
|
||||
[NET_IP6_SADR]= "ipv6-sadr",
|
||||
[NET_MPLS] = "mpls",
|
||||
};
|
||||
|
||||
const u16 net_addr_length[] = {
|
||||
[NET_IP4] = sizeof(net_addr_ip4),
|
||||
[NET_IP6] = sizeof(net_addr_ip6),
|
||||
[NET_VPN4] = sizeof(net_addr_vpn4),
|
||||
[NET_VPN6] = sizeof(net_addr_vpn6),
|
||||
[NET_ROA4] = sizeof(net_addr_roa4),
|
||||
[NET_ROA6] = sizeof(net_addr_roa6),
|
||||
[NET_FLOW4] = 0,
|
||||
[NET_FLOW6] = 0,
|
||||
[NET_IP6_SADR]= sizeof(net_addr_ip6_sadr),
|
||||
[NET_MPLS] = sizeof(net_addr_mpls),
|
||||
};
|
||||
|
||||
const u8 net_max_prefix_length[] = {
|
||||
[NET_IP4] = IP4_MAX_PREFIX_LENGTH,
|
||||
[NET_IP6] = IP6_MAX_PREFIX_LENGTH,
|
||||
[NET_VPN4] = IP4_MAX_PREFIX_LENGTH,
|
||||
[NET_VPN6] = IP6_MAX_PREFIX_LENGTH,
|
||||
[NET_ROA4] = IP4_MAX_PREFIX_LENGTH,
|
||||
[NET_ROA6] = IP6_MAX_PREFIX_LENGTH,
|
||||
[NET_FLOW4] = IP4_MAX_PREFIX_LENGTH,
|
||||
[NET_FLOW6] = IP6_MAX_PREFIX_LENGTH,
|
||||
[NET_IP6_SADR]= IP6_MAX_PREFIX_LENGTH,
|
||||
[NET_MPLS] = 0,
|
||||
};
|
||||
|
||||
const u16 net_max_text_length[] = {
|
||||
[NET_IP4] = 18, /* "255.255.255.255/32" */
|
||||
[NET_IP6] = 43, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
|
||||
[NET_VPN4] = 40, /* "4294967296:4294967296 255.255.255.255/32" */
|
||||
[NET_VPN6] = 65, /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
|
||||
[NET_ROA4] = 34, /* "255.255.255.255/32-32 AS4294967295" */
|
||||
[NET_ROA6] = 60, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128-128 AS4294967295" */
|
||||
[NET_FLOW4] = 0, /* "flow4 { ... }" */
|
||||
[NET_FLOW6] = 0, /* "flow6 { ... }" */
|
||||
[NET_IP6_SADR]= 92, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128 from ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
|
||||
[NET_MPLS] = 7, /* "1048575" */
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
rd_format(const u64 rd, char *buf, int buflen)
|
||||
{
|
||||
switch (rd >> 48)
|
||||
{
|
||||
case 0: return bsnprintf(buf, buflen, "%u:%u", (u32) (rd >> 32), (u32) rd);
|
||||
case 1: return bsnprintf(buf, buflen, "%I4:%u", ip4_from_u32(rd >> 16), (u32) (rd & 0xffff));
|
||||
case 2: if (((u32) (rd >> 16)) >> 16)
|
||||
return bsnprintf(buf, buflen, "%u:%u", (u32) (rd >> 16), (u32) (rd & 0xffff));
|
||||
else
|
||||
return bsnprintf(buf, buflen, "2:%u:%u", (u32) (rd >> 16), (u32) (rd & 0xffff));
|
||||
default: return bsnprintf(buf, buflen, "X:%08x:%08x", (u32) (rd >> 32), (u32) rd);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
net_format(const net_addr *N, char *buf, int buflen)
|
||||
{
|
||||
net_addr_union *n = (void *) N;
|
||||
buf[0] = 0;
|
||||
|
||||
switch (n->n.type)
|
||||
{
|
||||
case NET_IP4:
|
||||
return bsnprintf(buf, buflen, "%I4/%d", n->ip4.prefix, n->ip4.pxlen);
|
||||
case NET_IP6:
|
||||
return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
|
||||
case NET_VPN4:
|
||||
{
|
||||
int c = rd_format(n->vpn4.rd, buf, buflen);
|
||||
ADVANCE(buf, buflen, c);
|
||||
return bsnprintf(buf, buflen, " %I4/%d", n->vpn4.prefix, n->vpn4.pxlen);
|
||||
}
|
||||
case NET_VPN6:
|
||||
{
|
||||
/* XXX: RD format is specified for VPN4; not found any for VPN6, reusing the same as for VPN4 */
|
||||
int c = rd_format(n->vpn6.rd, buf, buflen);
|
||||
ADVANCE(buf, buflen, c);
|
||||
return bsnprintf(buf, buflen, " %I6/%d", n->vpn6.prefix, n->vpn6.pxlen);
|
||||
}
|
||||
case NET_ROA4:
|
||||
return bsnprintf(buf, buflen, "%I4/%u-%u AS%u", n->roa4.prefix, n->roa4.pxlen, n->roa4.max_pxlen, n->roa4.asn);
|
||||
case NET_ROA6:
|
||||
return bsnprintf(buf, buflen, "%I6/%u-%u AS%u", n->roa6.prefix, n->roa6.pxlen, n->roa6.max_pxlen, n->roa6.asn);
|
||||
case NET_FLOW4:
|
||||
return flow4_net_format(buf, buflen, &n->flow4);
|
||||
case NET_FLOW6:
|
||||
return flow6_net_format(buf, buflen, &n->flow6);
|
||||
case NET_IP6_SADR:
|
||||
return bsnprintf(buf, buflen, "%I6/%d from %I6/%d", n->ip6_sadr.dst_prefix, n->ip6_sadr.dst_pxlen, n->ip6_sadr.src_prefix, n->ip6_sadr.src_pxlen);
|
||||
case NET_MPLS:
|
||||
return bsnprintf(buf, buflen, "%u", n->mpls.label);
|
||||
}
|
||||
|
||||
bug("unknown network type");
|
||||
}
|
||||
|
||||
ip_addr
|
||||
net_pxmask(const net_addr *a)
|
||||
{
|
||||
switch (a->type)
|
||||
{
|
||||
case NET_IP4:
|
||||
case NET_VPN4:
|
||||
case NET_ROA4:
|
||||
case NET_FLOW4:
|
||||
return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
|
||||
|
||||
case NET_IP6:
|
||||
case NET_VPN6:
|
||||
case NET_ROA6:
|
||||
case NET_FLOW6:
|
||||
case NET_IP6_SADR:
|
||||
return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
|
||||
|
||||
case NET_MPLS:
|
||||
default:
|
||||
return IPA_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
net_compare(const net_addr *a, const net_addr *b)
|
||||
{
|
||||
if (a->type != b->type)
|
||||
return uint_cmp(a->type, b->type);
|
||||
|
||||
switch (a->type)
|
||||
{
|
||||
case NET_IP4:
|
||||
return net_compare_ip4((const net_addr_ip4 *) a, (const net_addr_ip4 *) b);
|
||||
case NET_IP6:
|
||||
return net_compare_ip6((const net_addr_ip6 *) a, (const net_addr_ip6 *) b);
|
||||
case NET_VPN4:
|
||||
return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b);
|
||||
case NET_VPN6:
|
||||
return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b);
|
||||
case NET_ROA4:
|
||||
return net_compare_roa4((const net_addr_roa4 *) a, (const net_addr_roa4 *) b);
|
||||
case NET_ROA6:
|
||||
return net_compare_roa6((const net_addr_roa6 *) a, (const net_addr_roa6 *) b);
|
||||
case NET_FLOW4:
|
||||
return net_compare_flow4((const net_addr_flow4 *) a, (const net_addr_flow4 *) b);
|
||||
case NET_FLOW6:
|
||||
return net_compare_flow6((const net_addr_flow6 *) a, (const net_addr_flow6 *) b);
|
||||
case NET_IP6_SADR:
|
||||
return net_compare_ip6_sadr((const net_addr_ip6_sadr *) a, (const net_addr_ip6_sadr *) b);
|
||||
case NET_MPLS:
|
||||
return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define NET_HASH(a,t) net_hash_##t((const net_addr_##t *) a)
|
||||
|
||||
u32
|
||||
net_hash(const net_addr *n)
|
||||
{
|
||||
switch (n->type)
|
||||
{
|
||||
case NET_IP4: return NET_HASH(n, ip4);
|
||||
case NET_IP6: return NET_HASH(n, ip6);
|
||||
case NET_VPN4: return NET_HASH(n, vpn4);
|
||||
case NET_VPN6: return NET_HASH(n, vpn6);
|
||||
case NET_ROA4: return NET_HASH(n, roa4);
|
||||
case NET_ROA6: return NET_HASH(n, roa6);
|
||||
case NET_FLOW4: return NET_HASH(n, flow4);
|
||||
case NET_FLOW6: return NET_HASH(n, flow6);
|
||||
case NET_IP6_SADR: return NET_HASH(n, ip6_sadr);
|
||||
case NET_MPLS: return NET_HASH(n, mpls);
|
||||
default: bug("invalid type");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define NET_VALIDATE(a,t) net_validate_##t((const net_addr_##t *) a)
|
||||
|
||||
int
|
||||
net_validate(const net_addr *n)
|
||||
{
|
||||
switch (n->type)
|
||||
{
|
||||
case NET_IP4: return NET_VALIDATE(n, ip4);
|
||||
case NET_IP6: return NET_VALIDATE(n, ip6);
|
||||
case NET_VPN4: return NET_VALIDATE(n, vpn4);
|
||||
case NET_VPN6: return NET_VALIDATE(n, vpn6);
|
||||
case NET_ROA4: return NET_VALIDATE(n, roa4);
|
||||
case NET_ROA6: return NET_VALIDATE(n, roa6);
|
||||
case NET_FLOW4: return NET_VALIDATE(n, flow4);
|
||||
case NET_FLOW6: return NET_VALIDATE(n, flow6);
|
||||
case NET_IP6_SADR: return NET_VALIDATE(n, ip6_sadr);
|
||||
case NET_MPLS: return NET_VALIDATE(n, mpls);
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
net_normalize(net_addr *N)
|
||||
{
|
||||
net_addr_union *n = (void *) N;
|
||||
|
||||
switch (n->n.type)
|
||||
{
|
||||
case NET_IP4:
|
||||
case NET_VPN4:
|
||||
case NET_ROA4:
|
||||
case NET_FLOW4:
|
||||
return net_normalize_ip4(&n->ip4);
|
||||
|
||||
case NET_IP6:
|
||||
case NET_VPN6:
|
||||
case NET_ROA6:
|
||||
case NET_FLOW6:
|
||||
return net_normalize_ip6(&n->ip6);
|
||||
|
||||
case NET_IP6_SADR:
|
||||
return net_normalize_ip6_sadr(&n->ip6_sadr);
|
||||
|
||||
case NET_MPLS:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
net_classify(const net_addr *N)
|
||||
{
|
||||
net_addr_union *n = (void *) N;
|
||||
|
||||
switch (n->n.type)
|
||||
{
|
||||
case NET_IP4:
|
||||
case NET_VPN4:
|
||||
case NET_ROA4:
|
||||
case NET_FLOW4:
|
||||
return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
|
||||
|
||||
case NET_IP6:
|
||||
case NET_VPN6:
|
||||
case NET_ROA6:
|
||||
case NET_FLOW6:
|
||||
return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
|
||||
|
||||
case NET_IP6_SADR:
|
||||
return ip6_zero(n->ip6_sadr.dst_prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6_sadr.dst_prefix);
|
||||
|
||||
case NET_MPLS:
|
||||
return IADDR_HOST | SCOPE_UNIVERSE;
|
||||
}
|
||||
|
||||
return IADDR_INVALID;
|
||||
}
|
||||
|
||||
int
|
||||
ipa_in_netX(const ip_addr a, const net_addr *n)
|
||||
{
|
||||
switch (n->type)
|
||||
{
|
||||
case NET_IP4:
|
||||
case NET_VPN4:
|
||||
case NET_ROA4:
|
||||
case NET_FLOW4:
|
||||
if (!ipa_is_ip4(a)) return 0;
|
||||
return ip4_zero(ip4_and(ip4_xor(ipa_to_ip4(a), net4_prefix(n)),
|
||||
ip4_mkmask(net4_pxlen(n))));
|
||||
|
||||
case NET_IP6:
|
||||
case NET_VPN6:
|
||||
case NET_ROA6:
|
||||
case NET_FLOW6:
|
||||
if (ipa_is_ip4(a)) return 0;
|
||||
return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
|
||||
ip6_mkmask(net6_pxlen(n))));
|
||||
|
||||
case NET_IP6_SADR:
|
||||
if (ipa_is_ip4(a)) return 0;
|
||||
return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
|
||||
ip6_mkmask(net6_pxlen(n))));
|
||||
|
||||
case NET_MPLS:
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
net_in_netX(const net_addr *a, const net_addr *n)
|
||||
{
|
||||
if (a->type != n->type)
|
||||
return 0;
|
||||
|
||||
return (net_pxlen(n) <= net_pxlen(a)) && ipa_in_netX(net_prefix(a), n);
|
||||
}
|
||||
|
||||
#define CHECK_NET(T,S) \
|
||||
({ if (sizeof(T) != S) die("sizeof %s is %d/%d", #T, (int) sizeof(T), S); })
|
||||
|
||||
void
|
||||
net_init(void)
|
||||
{
|
||||
CHECK_NET(net_addr, 24);
|
||||
CHECK_NET(net_addr_ip4, 8);
|
||||
CHECK_NET(net_addr_ip6, 20);
|
||||
CHECK_NET(net_addr_vpn4, 16);
|
||||
CHECK_NET(net_addr_vpn6, 32);
|
||||
CHECK_NET(net_addr_roa4, 16);
|
||||
CHECK_NET(net_addr_roa6, 28);
|
||||
CHECK_NET(net_addr_flow4, 8);
|
||||
CHECK_NET(net_addr_flow6, 20);
|
||||
CHECK_NET(net_addr_ip6_sadr, 40);
|
||||
CHECK_NET(net_addr_mpls, 8);
|
||||
}
|
624
lib/net.h
Normal file
624
lib/net.h
Normal file
@ -0,0 +1,624 @@
|
||||
/*
|
||||
* BIRD Internet Routing Daemon -- Network addresses
|
||||
*
|
||||
* (c) 2015 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_NET_H_
|
||||
#define _BIRD_NET_H_
|
||||
|
||||
#include "lib/ip.h"
|
||||
|
||||
|
||||
#define NET_IP4 1
|
||||
#define NET_IP6 2
|
||||
#define NET_VPN4 3
|
||||
#define NET_VPN6 4
|
||||
#define NET_ROA4 5
|
||||
#define NET_ROA6 6
|
||||
#define NET_FLOW4 7
|
||||
#define NET_FLOW6 8
|
||||
#define NET_IP6_SADR 9
|
||||
#define NET_MPLS 10
|
||||
#define NET_MAX 11
|
||||
|
||||
#define NB_IP4 (1 << NET_IP4)
|
||||
#define NB_IP6 (1 << NET_IP6)
|
||||
#define NB_VPN4 (1 << NET_VPN4)
|
||||
#define NB_VPN6 (1 << NET_VPN6)
|
||||
#define NB_ROA4 (1 << NET_ROA4)
|
||||
#define NB_ROA6 (1 << NET_ROA6)
|
||||
#define NB_FLOW4 (1 << NET_FLOW4)
|
||||
#define NB_FLOW6 (1 << NET_FLOW6)
|
||||
#define NB_IP6_SADR (1 << NET_IP6_SADR)
|
||||
#define NB_MPLS (1 << NET_MPLS)
|
||||
|
||||
#define NB_IP (NB_IP4 | NB_IP6)
|
||||
#define NB_VPN (NB_VPN4 | NB_VPN6)
|
||||
#define NB_FLOW (NB_FLOW4 | NB_FLOW6)
|
||||
#define NB_DEST (NB_IP | NB_IP6_SADR | NB_VPN | NB_MPLS)
|
||||
#define NB_ANY 0xffffffff
|
||||
|
||||
|
||||
typedef struct net_addr {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
u8 data[20];
|
||||
u64 align[0];
|
||||
} net_addr;
|
||||
|
||||
typedef struct net_addr_ip4 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip4_addr prefix;
|
||||
} net_addr_ip4;
|
||||
|
||||
typedef struct net_addr_ip6 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip6_addr prefix;
|
||||
} net_addr_ip6;
|
||||
|
||||
typedef struct net_addr_vpn4 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip4_addr prefix;
|
||||
u64 rd;
|
||||
} net_addr_vpn4;
|
||||
|
||||
typedef struct net_addr_vpn6 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip6_addr prefix;
|
||||
u32 padding;
|
||||
u64 rd;
|
||||
} net_addr_vpn6;
|
||||
|
||||
typedef struct net_addr_roa4 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip4_addr prefix;
|
||||
u32 max_pxlen;
|
||||
u32 asn;
|
||||
} net_addr_roa4;
|
||||
|
||||
typedef struct net_addr_roa6 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip6_addr prefix;
|
||||
u32 max_pxlen;
|
||||
u32 asn;
|
||||
} net_addr_roa6;
|
||||
|
||||
typedef struct net_addr_flow4 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip4_addr prefix;
|
||||
byte data[0];
|
||||
} net_addr_flow4;
|
||||
|
||||
typedef struct net_addr_flow6 {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
ip6_addr prefix;
|
||||
byte data[0];
|
||||
} net_addr_flow6;
|
||||
|
||||
typedef struct net_addr_mpls {
|
||||
u8 type;
|
||||
u8 pxlen;
|
||||
u16 length;
|
||||
u32 label;
|
||||
} net_addr_mpls;
|
||||
|
||||
typedef struct net_addr_ip6_sadr {
|
||||
u8 type;
|
||||
u8 dst_pxlen;
|
||||
u16 length;
|
||||
ip6_addr dst_prefix;
|
||||
s32 src_pxlen; /* s32 to avoid padding */
|
||||
ip6_addr src_prefix;
|
||||
} net_addr_ip6_sadr;
|
||||
|
||||
typedef union net_addr_union {
|
||||
net_addr n;
|
||||
net_addr_ip4 ip4;
|
||||
net_addr_ip6 ip6;
|
||||
net_addr_vpn4 vpn4;
|
||||
net_addr_vpn6 vpn6;
|
||||
net_addr_roa4 roa4;
|
||||
net_addr_roa6 roa6;
|
||||
net_addr_flow4 flow4;
|
||||
net_addr_flow6 flow6;
|
||||
net_addr_ip6_sadr ip6_sadr;
|
||||
net_addr_mpls mpls;
|
||||
} net_addr_union;
|
||||
|
||||
|
||||
extern const char * const net_label[];
|
||||
extern const u16 net_addr_length[];
|
||||
extern const u8 net_max_prefix_length[];
|
||||
extern const u16 net_max_text_length[];
|
||||
|
||||
#define NET_MAX_TEXT_LENGTH 256
|
||||
|
||||
|
||||
#define NET_ADDR_IP4(prefix,pxlen) \
|
||||
((net_addr_ip4) { NET_IP4, pxlen, sizeof(net_addr_ip4), prefix })
|
||||
|
||||
#define NET_ADDR_IP6(prefix,pxlen) \
|
||||
((net_addr_ip6) { NET_IP6, pxlen, sizeof(net_addr_ip6), prefix })
|
||||
|
||||
#define NET_ADDR_VPN4(prefix,pxlen,rd) \
|
||||
((net_addr_vpn4) { NET_VPN4, pxlen, sizeof(net_addr_vpn4), prefix, rd })
|
||||
|
||||
#define NET_ADDR_VPN6(prefix,pxlen,rd) \
|
||||
((net_addr_vpn6) { NET_VPN6, pxlen, sizeof(net_addr_vpn6), prefix, 0, rd })
|
||||
|
||||
#define NET_ADDR_ROA4(prefix,pxlen,max_pxlen,asn) \
|
||||
((net_addr_roa4) { NET_ROA4, pxlen, sizeof(net_addr_roa4), prefix, max_pxlen, asn })
|
||||
|
||||
#define NET_ADDR_ROA6(prefix,pxlen,max_pxlen,asn) \
|
||||
((net_addr_roa6) { NET_ROA6, pxlen, sizeof(net_addr_roa6), prefix, max_pxlen, asn })
|
||||
|
||||
#define NET_ADDR_FLOW4(prefix,pxlen,dlen) \
|
||||
((net_addr_flow4) { NET_FLOW4, pxlen, sizeof(net_addr_flow4) + dlen, prefix })
|
||||
|
||||
#define NET_ADDR_FLOW6(prefix,pxlen,dlen) \
|
||||
((net_addr_flow6) { NET_FLOW6, pxlen, sizeof(net_addr_flow6) + dlen, prefix })
|
||||
|
||||
#define NET_ADDR_IP6_SADR(dst_prefix,dst_pxlen,src_prefix,src_pxlen) \
|
||||
((net_addr_ip6_sadr) { NET_IP6_SADR, dst_pxlen, sizeof(net_addr_ip6_sadr), dst_prefix, src_pxlen, src_prefix })
|
||||
|
||||
#define NET_ADDR_MPLS(label) \
|
||||
((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label })
|
||||
|
||||
|
||||
static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
|
||||
{ *(net_addr_ip4 *)a = NET_ADDR_IP4(prefix, pxlen); }
|
||||
|
||||
static inline void net_fill_ip6(net_addr *a, ip6_addr prefix, uint pxlen)
|
||||
{ *(net_addr_ip6 *)a = NET_ADDR_IP6(prefix, pxlen); }
|
||||
|
||||
static inline void net_fill_vpn4(net_addr *a, ip4_addr prefix, uint pxlen, u64 rd)
|
||||
{ *(net_addr_vpn4 *)a = NET_ADDR_VPN4(prefix, pxlen, rd); }
|
||||
|
||||
static inline void net_fill_vpn6(net_addr *a, ip6_addr prefix, uint pxlen, u64 rd)
|
||||
{ *(net_addr_vpn6 *)a = NET_ADDR_VPN6(prefix, pxlen, rd); }
|
||||
|
||||
static inline void net_fill_roa4(net_addr *a, ip4_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
|
||||
{ *(net_addr_roa4 *)a = NET_ADDR_ROA4(prefix, pxlen, max_pxlen, asn); }
|
||||
|
||||
static inline void net_fill_roa6(net_addr *a, ip6_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
|
||||
{ *(net_addr_roa6 *)a = NET_ADDR_ROA6(prefix, pxlen, max_pxlen, asn); }
|
||||
|
||||
static inline void net_fill_ip6_sadr(net_addr *a, ip6_addr dst_prefix, uint dst_pxlen, ip6_addr src_prefix, uint src_pxlen)
|
||||
{ *(net_addr_ip6_sadr *)a = NET_ADDR_IP6_SADR(dst_prefix, dst_pxlen, src_prefix, src_pxlen); }
|
||||
|
||||
static inline void net_fill_mpls(net_addr *a, u32 label)
|
||||
{ *(net_addr_mpls *)a = NET_ADDR_MPLS(label); }
|
||||
|
||||
static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen)
|
||||
{
|
||||
if (ipa_is_ip4(prefix))
|
||||
net_fill_ip4(a, ipa_to_ip4(prefix), pxlen);
|
||||
else
|
||||
net_fill_ip6(a, ipa_to_ip6(prefix), pxlen);
|
||||
}
|
||||
|
||||
static inline void net_fill_ip_host(net_addr *a, ip_addr prefix)
|
||||
{
|
||||
if (ipa_is_ip4(prefix))
|
||||
net_fill_ip4(a, ipa_to_ip4(prefix), IP4_MAX_PREFIX_LENGTH);
|
||||
else
|
||||
net_fill_ip6(a, ipa_to_ip6(prefix), IP6_MAX_PREFIX_LENGTH);
|
||||
}
|
||||
|
||||
static inline void net_fill_flow4(net_addr *a, ip4_addr prefix, uint pxlen, byte *data, uint dlen)
|
||||
{
|
||||
net_addr_flow4 *f = (void *) a;
|
||||
*f = NET_ADDR_FLOW4(prefix, pxlen, dlen);
|
||||
memcpy(f->data, data, dlen);
|
||||
}
|
||||
|
||||
static inline void net_fill_flow6(net_addr *a, ip6_addr prefix, uint pxlen, byte *data, uint dlen)
|
||||
{
|
||||
net_addr_flow6 *f = (void *) a;
|
||||
*f = NET_ADDR_FLOW6(prefix, pxlen, dlen);
|
||||
memcpy(f->data, data, dlen);
|
||||
}
|
||||
|
||||
/* Make NET_IP6_SADR from NET_IP6, assuming there is enough space */
|
||||
static inline void net_make_ip6_sadr(net_addr *a)
|
||||
{
|
||||
net_addr_ip6_sadr *n = (void *) a;
|
||||
n->type = NET_IP6_SADR;
|
||||
n->length = sizeof(net_addr_ip6_sadr);
|
||||
n->src_pxlen = 0;
|
||||
n->src_prefix = IP6_NONE;
|
||||
}
|
||||
|
||||
static inline int net_val_match(u8 type, u32 mask)
|
||||
{ return !!((1 << type) & mask); }
|
||||
|
||||
static inline int net_type_match(const net_addr *a, u32 mask)
|
||||
{ return net_val_match(a->type, mask); }
|
||||
|
||||
static inline int net_is_ip(const net_addr *a)
|
||||
{ return (a->type == NET_IP4) || (a->type == NET_IP6); }
|
||||
|
||||
static inline int net_is_vpn(const net_addr *a)
|
||||
{ return (a->type == NET_VPN4) || (a->type == NET_VPN6); }
|
||||
|
||||
static inline int net_is_roa(const net_addr *a)
|
||||
{ return (a->type == NET_ROA4) || (a->type == NET_ROA6); }
|
||||
|
||||
static inline int net_is_flow(const net_addr *a)
|
||||
{ return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); }
|
||||
|
||||
static inline int net_is_sadr(const net_addr *a)
|
||||
{ return (a->type == NET_IP6_SADR); }
|
||||
|
||||
static inline ip4_addr net4_prefix(const net_addr *a)
|
||||
{ return ((net_addr_ip4 *) a)->prefix; }
|
||||
|
||||
static inline ip6_addr net6_prefix(const net_addr *a)
|
||||
{ return ((net_addr_ip6 *) a)->prefix; }
|
||||
|
||||
static inline ip_addr net_prefix(const net_addr *a)
|
||||
{
|
||||
switch (a->type)
|
||||
{
|
||||
case NET_IP4:
|
||||
case NET_VPN4:
|
||||
case NET_ROA4:
|
||||
case NET_FLOW4:
|
||||
return ipa_from_ip4(net4_prefix(a));
|
||||
|
||||
case NET_IP6:
|
||||
case NET_VPN6:
|
||||
case NET_ROA6:
|
||||
case NET_FLOW6:
|
||||
case NET_IP6_SADR:
|
||||
return ipa_from_ip6(net6_prefix(a));
|
||||
|
||||
case NET_MPLS:
|
||||
default:
|
||||
return IPA_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
static inline u32 net_mpls(const net_addr *a)
|
||||
{
|
||||
if (a->type == NET_MPLS)
|
||||
return ((net_addr_mpls *) a)->label;
|
||||
|
||||
bug("Can't call net_mpls on non-mpls net_addr");
|
||||
}
|
||||
|
||||
static inline uint net4_pxlen(const net_addr *a)
|
||||
{ return a->pxlen; }
|
||||
|
||||
static inline uint net6_pxlen(const net_addr *a)
|
||||
{ return a->pxlen; }
|
||||
|
||||
static inline uint net_pxlen(const net_addr *a)
|
||||
{ return a->pxlen; }
|
||||
|
||||
ip_addr net_pxmask(const net_addr *a);
|
||||
|
||||
static inline u64 net_rd(const net_addr *a)
|
||||
{
|
||||
switch (a->type)
|
||||
{
|
||||
case NET_VPN4:
|
||||
return ((net_addr_vpn4 *)a)->rd;
|
||||
case NET_VPN6:
|
||||
return ((net_addr_vpn6 *)a)->rd;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static inline int net_equal(const net_addr *a, const net_addr *b)
|
||||
{ return (a->length == b->length) && !memcmp(a, b, a->length); }
|
||||
|
||||
static inline int net_equal_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_ip4)); }
|
||||
|
||||
static inline int net_equal_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_ip6)); }
|
||||
|
||||
static inline int net_equal_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_vpn4)); }
|
||||
|
||||
static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_vpn6)); }
|
||||
|
||||
static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_roa4)); }
|
||||
|
||||
static inline int net_equal_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_roa6)); }
|
||||
|
||||
static inline int net_equal_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b)
|
||||
{ return net_equal((const net_addr *) a, (const net_addr *) b); }
|
||||
|
||||
static inline int net_equal_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b)
|
||||
{ return net_equal((const net_addr *) a, (const net_addr *) b); }
|
||||
|
||||
static inline int net_equal_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_ip6_sadr)); }
|
||||
|
||||
static inline int net_equal_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
|
||||
{ return !memcmp(a, b, sizeof(net_addr_mpls)); }
|
||||
|
||||
|
||||
static inline int net_equal_prefix_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
|
||||
{ return ip4_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
|
||||
|
||||
static inline int net_equal_prefix_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
|
||||
{ return ip6_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
|
||||
|
||||
static inline int net_equal_dst_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
|
||||
{ return ip6_equal(a->dst_prefix, b->dst_prefix) && (a->dst_pxlen == b->dst_pxlen); }
|
||||
|
||||
static inline int net_equal_src_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
|
||||
{ return ip6_equal(a->src_prefix, b->src_prefix) && (a->src_pxlen == b->src_pxlen); }
|
||||
|
||||
|
||||
static inline int net_zero_ip4(const net_addr_ip4 *a)
|
||||
{ return !a->pxlen && ip4_zero(a->prefix); }
|
||||
|
||||
static inline int net_zero_ip6(const net_addr_ip6 *a)
|
||||
{ return !a->pxlen && ip6_zero(a->prefix); }
|
||||
|
||||
static inline int net_zero_vpn4(const net_addr_vpn4 *a)
|
||||
{ return !a->pxlen && ip4_zero(a->prefix) && !a->rd; }
|
||||
|
||||
static inline int net_zero_vpn6(const net_addr_vpn6 *a)
|
||||
{ return !a->pxlen && ip6_zero(a->prefix) && !a->rd; }
|
||||
|
||||
static inline int net_zero_roa4(const net_addr_roa4 *a)
|
||||
{ return !a->pxlen && ip4_zero(a->prefix) && !a->max_pxlen && !a->asn; }
|
||||
|
||||
static inline int net_zero_roa6(const net_addr_roa6 *a)
|
||||
{ return !a->pxlen && ip6_zero(a->prefix) && !a->max_pxlen && !a->asn; }
|
||||
|
||||
static inline int net_zero_flow4(const net_addr_flow4 *a)
|
||||
{ return !a->pxlen && ip4_zero(a->prefix) && (a->length == sizeof(net_addr_flow4)); }
|
||||
|
||||
static inline int net_zero_flow6(const net_addr_flow6 *a)
|
||||
{ return !a->pxlen && ip6_zero(a->prefix) && (a->length == sizeof(net_addr_flow6)); }
|
||||
|
||||
static inline int net_zero_mpls(const net_addr_mpls *a)
|
||||
{ return !a->label; }
|
||||
|
||||
|
||||
static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
|
||||
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
|
||||
|
||||
static inline int net_compare_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
|
||||
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
|
||||
|
||||
static inline int net_compare_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
|
||||
{ return u64_cmp(a->rd, b->rd) ?: ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
|
||||
|
||||
static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
|
||||
{ return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
|
||||
|
||||
static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
|
||||
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); }
|
||||
|
||||
static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
|
||||
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); }
|
||||
|
||||
static inline int net_compare_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b)
|
||||
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->length, b->length) ?: memcmp(a->data, b->data, a->length - sizeof(net_addr_flow4)); }
|
||||
|
||||
static inline int net_compare_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b)
|
||||
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->length, b->length) ?: memcmp(a->data, b->data, a->length - sizeof(net_addr_flow6)); }
|
||||
|
||||
static inline int net_compare_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
|
||||
{
|
||||
return
|
||||
ip6_compare(a->dst_prefix, b->dst_prefix) ?: uint_cmp(a->dst_pxlen, b->dst_pxlen) ?:
|
||||
ip6_compare(a->src_prefix, b->src_prefix) ?: uint_cmp(a->src_pxlen, b->src_pxlen);
|
||||
}
|
||||
|
||||
static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
|
||||
{ return uint_cmp(a->label, b->label); }
|
||||
|
||||
int net_compare(const net_addr *a, const net_addr *b);
|
||||
|
||||
|
||||
static inline void net_copy(net_addr *dst, const net_addr *src)
|
||||
{ memcpy(dst, src, src->length); }
|
||||
|
||||
static inline void net_copy_ip4(net_addr_ip4 *dst, const net_addr_ip4 *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_ip4)); }
|
||||
|
||||
static inline void net_copy_ip6(net_addr_ip6 *dst, const net_addr_ip6 *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_ip6)); }
|
||||
|
||||
static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_vpn4)); }
|
||||
|
||||
static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_vpn6)); }
|
||||
|
||||
static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_roa4)); }
|
||||
|
||||
static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_roa6)); }
|
||||
|
||||
static inline void net_copy_flow4(net_addr_flow4 *dst, const net_addr_flow4 *src)
|
||||
{ memcpy(dst, src, src->length); }
|
||||
|
||||
static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src)
|
||||
{ memcpy(dst, src, src->length); }
|
||||
|
||||
static inline void net_copy_ip6_sadr(net_addr_ip6_sadr *dst, const net_addr_ip6_sadr *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_ip6_sadr)); }
|
||||
|
||||
static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
|
||||
{ memcpy(dst, src, sizeof(net_addr_mpls)); }
|
||||
|
||||
|
||||
/* XXXX */
|
||||
static inline u32 u64_hash(u64 a)
|
||||
{ return u32_hash(a); }
|
||||
|
||||
static inline u32 net_hash_ip4(const net_addr_ip4 *n)
|
||||
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
|
||||
|
||||
static inline u32 net_hash_ip6(const net_addr_ip6 *n)
|
||||
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
|
||||
|
||||
static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
|
||||
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
|
||||
|
||||
static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
|
||||
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
|
||||
|
||||
static inline u32 net_hash_roa4(const net_addr_roa4 *n)
|
||||
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
|
||||
|
||||
static inline u32 net_hash_roa6(const net_addr_roa6 *n)
|
||||
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
|
||||
|
||||
static inline u32 net_hash_flow4(const net_addr_flow4 *n)
|
||||
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
|
||||
|
||||
static inline u32 net_hash_flow6(const net_addr_flow6 *n)
|
||||
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
|
||||
|
||||
static inline u32 net_hash_ip6_sadr(const net_addr_ip6_sadr *n)
|
||||
{ return net_hash_ip6((net_addr_ip6 *) n); }
|
||||
|
||||
static inline u32 net_hash_mpls(const net_addr_mpls *n)
|
||||
{ return n->label; }
|
||||
|
||||
u32 net_hash(const net_addr *a);
|
||||
|
||||
|
||||
static inline int net_validate_px4(const ip4_addr prefix, uint pxlen)
|
||||
{
|
||||
return (pxlen <= IP4_MAX_PREFIX_LENGTH) &&
|
||||
ip4_zero(ip4_and(prefix, ip4_not(ip4_mkmask(pxlen))));
|
||||
}
|
||||
|
||||
static inline int net_validate_px6(const ip6_addr prefix, uint pxlen)
|
||||
{
|
||||
return (pxlen <= IP6_MAX_PREFIX_LENGTH) &&
|
||||
ip6_zero(ip6_and(prefix, ip6_not(ip6_mkmask(pxlen))));
|
||||
}
|
||||
|
||||
static inline int net_validate_ip4(const net_addr_ip4 *n)
|
||||
{ return net_validate_px4(n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_validate_ip6(const net_addr_ip6 *n)
|
||||
{ return net_validate_px6(n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_validate_vpn4(const net_addr_vpn4 *n)
|
||||
{ return net_validate_px4(n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_validate_vpn6(const net_addr_vpn6 *n)
|
||||
{ return net_validate_px6(n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_validate_roa4(const net_addr_roa4 *n)
|
||||
{
|
||||
return net_validate_px4(n->prefix, n->pxlen) &&
|
||||
(n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP4_MAX_PREFIX_LENGTH);
|
||||
}
|
||||
|
||||
static inline int net_validate_roa6(const net_addr_roa6 *n)
|
||||
{
|
||||
return net_validate_px6(n->prefix, n->pxlen) &&
|
||||
(n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP6_MAX_PREFIX_LENGTH);
|
||||
}
|
||||
|
||||
// FIXME: Better check, call flow_validate?
|
||||
static inline int net_validate_flow4(const net_addr_flow4 *n)
|
||||
{ return net_validate_px4(n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_validate_flow6(const net_addr_flow6 *n)
|
||||
{ return net_validate_px6(n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_validate_mpls(const net_addr_mpls *n)
|
||||
{ return n->label < (1 << 20); }
|
||||
|
||||
static inline int net_validate_ip6_sadr(const net_addr_ip6_sadr *n)
|
||||
{ return net_validate_px6(n->dst_prefix, n->dst_pxlen) && net_validate_px6(n->src_prefix, n->src_pxlen); }
|
||||
|
||||
int net_validate(const net_addr *N);
|
||||
|
||||
|
||||
static inline void net_normalize_ip4(net_addr_ip4 *n)
|
||||
{ n->prefix = ip4_and(n->prefix, ip4_mkmask(n->pxlen)); }
|
||||
|
||||
static inline void net_normalize_ip6(net_addr_ip6 *n)
|
||||
{ n->prefix = ip6_and(n->prefix, ip6_mkmask(n->pxlen)); }
|
||||
|
||||
static inline void net_normalize_vpn4(net_addr_vpn4 *n)
|
||||
{ net_normalize_ip4((net_addr_ip4 *) n); }
|
||||
|
||||
static inline void net_normalize_vpn6(net_addr_vpn6 *n)
|
||||
{ net_normalize_ip6((net_addr_ip6 *) n); }
|
||||
|
||||
static inline void net_normalize_ip6_sadr(net_addr_ip6_sadr *n)
|
||||
{
|
||||
n->dst_prefix = ip6_and(n->dst_prefix, ip6_mkmask(n->dst_pxlen));
|
||||
n->src_prefix = ip6_and(n->src_prefix, ip6_mkmask(n->src_pxlen));
|
||||
}
|
||||
|
||||
void net_normalize(net_addr *N);
|
||||
|
||||
|
||||
int net_classify(const net_addr *N);
|
||||
int net_format(const net_addr *N, char *buf, int buflen);
|
||||
int rd_format(const u64 rd, char *buf, int buflen);
|
||||
|
||||
static inline int ipa_in_px4(ip4_addr a, ip4_addr prefix, uint pxlen)
|
||||
{ return ip4_zero(ip4_and(ip4_xor(a, prefix), ip4_mkmask(pxlen))); }
|
||||
|
||||
static inline int ipa_in_px6(ip6_addr a, ip6_addr prefix, uint pxlen)
|
||||
{ return ip6_zero(ip6_and(ip6_xor(a, prefix), ip6_mkmask(pxlen))); }
|
||||
|
||||
static inline int ipa_in_net_ip4(ip4_addr a, const net_addr_ip4 *n)
|
||||
{ return ipa_in_px4(a, n->prefix, n->pxlen); }
|
||||
|
||||
static inline int ipa_in_net_ip6(ip6_addr a, const net_addr_ip6 *n)
|
||||
{ return ipa_in_px6(a, n->prefix, n->pxlen); }
|
||||
|
||||
static inline int net_in_net_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
|
||||
{ return (a->pxlen >= b->pxlen) && ipa_in_px4(a->prefix, b->prefix, b->pxlen); }
|
||||
|
||||
static inline int net_in_net_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
|
||||
{ return (a->pxlen >= b->pxlen) && ipa_in_px6(a->prefix, b->prefix, b->pxlen); }
|
||||
|
||||
static inline int net_in_net_dst_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
|
||||
{ return (a->dst_pxlen >= b->dst_pxlen) && ipa_in_px6(a->dst_prefix, b->dst_prefix, b->dst_pxlen); }
|
||||
|
||||
static inline int net_in_net_src_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
|
||||
{ return (a->src_pxlen >= b->src_pxlen) && ipa_in_px6(a->src_prefix, b->src_prefix, b->src_pxlen); }
|
||||
|
||||
int ipa_in_netX(const ip_addr A, const net_addr *N);
|
||||
int net_in_netX(const net_addr *A, const net_addr *N);
|
||||
|
||||
void net_init(void);
|
||||
|
||||
#endif
|
149
lib/patmatch_test.c
Normal file
149
lib/patmatch_test.c
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
* BIRD Library -- Pattern Matching Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/string.h"
|
||||
|
||||
#define MATCH (int) { 1 }
|
||||
#define NOMATCH (int) { 0 }
|
||||
|
||||
struct match_pair {
|
||||
byte *pattern;
|
||||
byte *data;
|
||||
};
|
||||
|
||||
static int
|
||||
test_matching(void *out_, const void *in_, const void *expected_out_)
|
||||
{
|
||||
int *out = out_;
|
||||
const struct match_pair *in = in_;
|
||||
const int *expected_out = expected_out_;
|
||||
|
||||
*out = patmatch(in->pattern, in->data);
|
||||
|
||||
return *out == *expected_out;
|
||||
}
|
||||
|
||||
static void
|
||||
fmt_match_pair(char *buf, size_t size, const void *data)
|
||||
{
|
||||
const struct match_pair *mp = data;
|
||||
snprintf(buf, size, "pattern: '%s', subject: '%s'", mp->pattern, mp->data);
|
||||
}
|
||||
|
||||
static void
|
||||
fmt_match_result(char *buf, size_t size, const void *data)
|
||||
{
|
||||
const int *result = data;
|
||||
snprintf(buf, size, *result ? "match" : "no-match");
|
||||
}
|
||||
|
||||
static int
|
||||
t_matching(void)
|
||||
{
|
||||
struct bt_pair test_vectors[] = {
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "",
|
||||
.data = "",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "*",
|
||||
.data = "",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "\\*",
|
||||
.data = "*",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "\\*",
|
||||
.data = "a",
|
||||
},
|
||||
.out = & NOMATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "?",
|
||||
.data = "",
|
||||
},
|
||||
.out = & NOMATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "abcdefghijklmnopqrstuvwxyz",
|
||||
.data = "abcdefghijklmnopqrstuvwxyz",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "??????????????????????????",
|
||||
.data = "abcdefghijklmnopqrstuvwxyz",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "*abcdefghijklmnopqrstuvwxyz*",
|
||||
.data = "abcdefghijklmnopqrstuvwxyz",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "ab?defg*jklmnop*stu*wxy*z",
|
||||
.data = "abcdefghijklmnopqrstuvwxyz",
|
||||
},
|
||||
.out = & MATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "abcdefghijklmnopqrstuvwxyz",
|
||||
.data = "abcdefghijklmnopqrtuvwxyz",
|
||||
},
|
||||
.out = & NOMATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "abcdefghijklmnopqr?uvwxyz",
|
||||
.data = "abcdefghijklmnopqrstuvwxyz",
|
||||
},
|
||||
.out = & NOMATCH,
|
||||
},
|
||||
{
|
||||
.in = & (struct match_pair) {
|
||||
.pattern = "aa*aaaaa?aaaaaaaaaaaaaaaaaaa",
|
||||
.data = "aaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
},
|
||||
.out = & NOMATCH,
|
||||
},
|
||||
};
|
||||
|
||||
return bt_assert_batch(test_vectors, test_matching, fmt_match_pair, fmt_match_result);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_matching, "Pattern matching");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
211
lib/printf.c
211
lib/printf.c
@ -34,13 +34,7 @@ static int skip_atoi(const char **s)
|
||||
#define SPECIAL 32 /* 0x */
|
||||
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
|
||||
|
||||
#define do_div(n,base) ({ \
|
||||
int __res; \
|
||||
__res = ((unsigned long) n) % (unsigned) base; \
|
||||
n = ((unsigned long) n) / (unsigned) base; \
|
||||
__res; })
|
||||
|
||||
static char * number(char * str, long num, int base, int size, int precision,
|
||||
static char * number(char * str, u64 num, uint base, int size, int precision,
|
||||
int type, int remains)
|
||||
{
|
||||
char c,sign,tmp[66];
|
||||
@ -58,7 +52,7 @@ static char * number(char * str, long num, int base, int size, int precision,
|
||||
c = (type & ZEROPAD) ? '0' : ' ';
|
||||
sign = 0;
|
||||
if (type & SIGN) {
|
||||
if (num < 0) {
|
||||
if (num > (u64) INT64_MAX) {
|
||||
sign = '-';
|
||||
num = -num;
|
||||
size--;
|
||||
@ -79,8 +73,11 @@ static char * number(char * str, long num, int base, int size, int precision,
|
||||
i = 0;
|
||||
if (num == 0)
|
||||
tmp[i++]='0';
|
||||
else while (num != 0)
|
||||
tmp[i++] = digits[do_div(num,base)];
|
||||
else while (num != 0) {
|
||||
uint res = num % base;
|
||||
num = num / base;
|
||||
tmp[i++] = digits[res];
|
||||
}
|
||||
if (i > precision)
|
||||
precision = i;
|
||||
size -= precision;
|
||||
@ -118,30 +115,34 @@ static char * number(char * str, long num, int base, int size, int precision,
|
||||
* @fmt: format string
|
||||
* @args: a list of arguments to be formatted
|
||||
*
|
||||
* This functions acts like ordinary sprintf() except that it checks
|
||||
* available space to avoid buffer overflows and it allows some more
|
||||
* format specifiers: |%I| for formatting of IP addresses (any non-zero
|
||||
* width is automatically replaced by standard IP address width which
|
||||
* depends on whether we use IPv4 or IPv6; |%#I| gives hexadecimal format),
|
||||
* |%R| for Router / Network ID (u32 value printed as IPv4 address)
|
||||
* |%lR| for 64bit Router / Network ID (u64 value printed as eight :-separated octets)
|
||||
* and |%m| resp. |%M| for error messages (uses strerror() to translate @errno code to
|
||||
* message text). On the other hand, it doesn't support floating
|
||||
* point numbers.
|
||||
* This functions acts like ordinary sprintf() except that it checks available
|
||||
* space to avoid buffer overflows and it allows some more format specifiers:
|
||||
* |%I| for formatting of IP addresses (width of 1 is automatically replaced by
|
||||
* standard IP address width which depends on whether we use IPv4 or IPv6; |%I4|
|
||||
* or |%I6| can be used for explicit ip4_addr / ip6_addr arguments, |%N| for
|
||||
* generic network addresses (net_addr *), |%R| for Router / Network ID (u32
|
||||
* value printed as IPv4 address), |%lR| for 64bit Router / Network ID (u64
|
||||
* value printed as eight :-separated octets), |%t| for time values (btime) with
|
||||
* specified subsecond precision, and |%m| resp. |%M| for error messages (uses
|
||||
* strerror() to translate @errno code to message text). On the other hand, it
|
||||
* doesn't support floating point numbers. The bvsnprintf() supports |%h| and
|
||||
* |%l| qualifiers, but |%l| is used for s64/u64 instead of long/ulong.
|
||||
*
|
||||
* Result: number of characters of the output string or -1 if
|
||||
* the buffer space was insufficient.
|
||||
*/
|
||||
int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
{
|
||||
int len;
|
||||
unsigned long num;
|
||||
int i, base;
|
||||
int len, i;
|
||||
u64 num;
|
||||
uint base;
|
||||
u32 x;
|
||||
u64 X;
|
||||
btime t;
|
||||
s64 t1, t2;
|
||||
char *str, *start;
|
||||
const char *s;
|
||||
char ipbuf[MAX(STD_ADDRESS_P_LENGTH,ROUTER_ID_64_LENGTH)+1];
|
||||
char ipbuf[NET_MAX_TEXT_LENGTH+1];
|
||||
struct iface *iface;
|
||||
|
||||
int flags; /* flags to number() */
|
||||
@ -149,7 +150,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
int field_width; /* width of output field */
|
||||
int precision; /* min. # of digits for integers; max
|
||||
number of chars for from string */
|
||||
int qualifier; /* 'h', 'l', or 'L' for integer fields */
|
||||
int qualifier; /* 'h' or 'l' for integer fields */
|
||||
|
||||
for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
|
||||
if (*fmt != '%') {
|
||||
@ -158,7 +159,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
*str++ = *fmt;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
/* process flags */
|
||||
flags = 0;
|
||||
repeat:
|
||||
@ -170,7 +171,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
case '#': flags |= SPECIAL; goto repeat;
|
||||
case '0': flags |= ZEROPAD; goto repeat;
|
||||
}
|
||||
|
||||
|
||||
/* get field width */
|
||||
field_width = -1;
|
||||
if (is_digit(*fmt))
|
||||
@ -188,7 +189,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
/* get the precision */
|
||||
precision = -1;
|
||||
if (*fmt == '.') {
|
||||
++fmt;
|
||||
++fmt;
|
||||
if (is_digit(*fmt))
|
||||
precision = skip_atoi(&fmt);
|
||||
else if (*fmt == '*') {
|
||||
@ -238,6 +239,14 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
case 'M':
|
||||
s = strerror(va_arg(args, int));
|
||||
goto str;
|
||||
case 'N': {
|
||||
net_addr *n = va_arg(args, net_addr *);
|
||||
if (field_width == 1)
|
||||
field_width = net_max_text_length[n->type];
|
||||
net_format(n, ipbuf, sizeof(ipbuf));
|
||||
s = ipbuf;
|
||||
goto str;
|
||||
}
|
||||
case 's':
|
||||
s = va_arg(args, char *);
|
||||
if (!s)
|
||||
@ -259,22 +268,31 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
*str++ = ' ';
|
||||
continue;
|
||||
|
||||
case 'V': {
|
||||
const char *vfmt = va_arg(args, const char *);
|
||||
va_list *vargs = va_arg(args, va_list *);
|
||||
int res = bvsnprintf(str, size, vfmt, *vargs);
|
||||
if (res < 0)
|
||||
return -1;
|
||||
str += res;
|
||||
size -= res;
|
||||
continue;
|
||||
}
|
||||
|
||||
case 'p':
|
||||
if (field_width == -1) {
|
||||
field_width = 2*sizeof(void *);
|
||||
flags |= ZEROPAD;
|
||||
}
|
||||
str = number(str,
|
||||
(unsigned long) va_arg(args, void *), 16,
|
||||
field_width, precision, flags, size);
|
||||
str = number(str, (uintptr_t) va_arg(args, void *), 16,
|
||||
field_width, precision, flags, size);
|
||||
if (!str)
|
||||
return -1;
|
||||
continue;
|
||||
|
||||
|
||||
case 'n':
|
||||
if (qualifier == 'l') {
|
||||
long * ip = va_arg(args, long *);
|
||||
s64 * ip = va_arg(args, s64 *);
|
||||
*ip = (str - buf);
|
||||
} else {
|
||||
int * ip = va_arg(args, int *);
|
||||
@ -284,14 +302,35 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
|
||||
/* IP address */
|
||||
case 'I':
|
||||
if (flags & SPECIAL)
|
||||
ipa_ntox(va_arg(args, ip_addr), ipbuf);
|
||||
else {
|
||||
ipa_ntop(va_arg(args, ip_addr), ipbuf);
|
||||
if (field_width == 1)
|
||||
field_width = STD_ADDRESS_P_LENGTH;
|
||||
if (fmt[1] == '4') {
|
||||
/* Explicit IPv4 address */
|
||||
ip4_addr a = va_arg(args, ip4_addr);
|
||||
ip4_ntop(a, ipbuf);
|
||||
i = IP4_MAX_TEXT_LENGTH;
|
||||
fmt++;
|
||||
} else if (fmt[1] == '6') {
|
||||
/* Explicit IPv6 address */
|
||||
ip6_addr a = va_arg(args, ip6_addr);
|
||||
ip6_ntop(a, ipbuf);
|
||||
i = IP6_MAX_TEXT_LENGTH;
|
||||
fmt++;
|
||||
} else {
|
||||
/* Just IP address */
|
||||
ip_addr a = va_arg(args, ip_addr);
|
||||
|
||||
if (ipa_is_ip4(a)) {
|
||||
ip4_ntop(ipa_to_ip4(a), ipbuf);
|
||||
i = IP4_MAX_TEXT_LENGTH;
|
||||
} else {
|
||||
ip6_ntop(ipa_to_ip6(a), ipbuf);
|
||||
i = IP6_MAX_TEXT_LENGTH;
|
||||
}
|
||||
}
|
||||
|
||||
s = ipbuf;
|
||||
if (field_width == 1)
|
||||
field_width = i;
|
||||
|
||||
goto str;
|
||||
|
||||
/* Interface scope after link-local IP address */
|
||||
@ -311,7 +350,7 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
|
||||
/* Router/Network ID - essentially IPv4 address in u32 value */
|
||||
case 'R':
|
||||
if(qualifier == 'l') {
|
||||
if (qualifier == 'l') {
|
||||
X = va_arg(args, u64);
|
||||
bsprintf(ipbuf, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
(uint) ((X >> 56) & 0xff),
|
||||
@ -326,15 +365,55 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
else
|
||||
{
|
||||
x = va_arg(args, u32);
|
||||
bsprintf(ipbuf, "%d.%d.%d.%d",
|
||||
((x >> 24) & 0xff),
|
||||
((x >> 16) & 0xff),
|
||||
((x >> 8) & 0xff),
|
||||
(x & 0xff));
|
||||
ip4_ntop(ip4_from_u32(x), ipbuf);
|
||||
}
|
||||
s = ipbuf;
|
||||
goto str;
|
||||
|
||||
case 't':
|
||||
t = va_arg(args, btime);
|
||||
t1 = t TO_S;
|
||||
t2 = t - t1 S;
|
||||
|
||||
if (precision < 0)
|
||||
precision = 3;
|
||||
|
||||
if (precision > 6)
|
||||
precision = 6;
|
||||
|
||||
/* Compute field_width for second part */
|
||||
if ((precision > 0) && (field_width > 0))
|
||||
field_width -= (1 + precision);
|
||||
|
||||
if (field_width < 0)
|
||||
field_width = 0;
|
||||
|
||||
/* Print seconds */
|
||||
flags |= SIGN;
|
||||
str = number(str, (u64) t1, 10, field_width, 0, flags, size);
|
||||
if (!str)
|
||||
return -1;
|
||||
|
||||
if (precision > 0)
|
||||
{
|
||||
size -= (str-start);
|
||||
start = str;
|
||||
|
||||
if ((1 + precision) > size)
|
||||
return -1;
|
||||
|
||||
/* Convert microseconds to requested precision */
|
||||
for (i = precision; i < 6; i++)
|
||||
t2 /= 10;
|
||||
|
||||
/* Print sub-seconds */
|
||||
*str++ = '.';
|
||||
str = number(str, (u64) t2, 10, precision, 0, ZEROPAD, size - 1);
|
||||
if (!str)
|
||||
return -1;
|
||||
}
|
||||
goto done;
|
||||
|
||||
/* integer number formats - set up the flags and "break" */
|
||||
case 'o':
|
||||
base = 8;
|
||||
@ -364,19 +443,26 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||
--fmt;
|
||||
continue;
|
||||
}
|
||||
if (qualifier == 'l')
|
||||
num = va_arg(args, unsigned long);
|
||||
else if (qualifier == 'h') {
|
||||
num = (unsigned short) va_arg(args, int);
|
||||
if (flags & SIGN)
|
||||
num = (short) num;
|
||||
} else if (flags & SIGN)
|
||||
num = va_arg(args, int);
|
||||
else
|
||||
num = va_arg(args, uint);
|
||||
if (flags & SIGN) {
|
||||
/* Conversions valid per ISO C99 6.3.1.3 (2) */
|
||||
if (qualifier == 'l')
|
||||
num = (u64) va_arg(args, s64);
|
||||
else if (qualifier == 'h')
|
||||
num = (u64) (short) va_arg(args, int);
|
||||
else
|
||||
num = (u64) va_arg(args, int);
|
||||
} else {
|
||||
if (qualifier == 'l')
|
||||
num = va_arg(args, u64);
|
||||
else if (qualifier == 'h')
|
||||
num = (unsigned short) va_arg(args, int);
|
||||
else
|
||||
num = va_arg(args, uint);
|
||||
}
|
||||
str = number(str, num, base, field_width, precision, flags, size);
|
||||
if (!str)
|
||||
return -1;
|
||||
done: ;
|
||||
}
|
||||
if (!size)
|
||||
return -1;
|
||||
@ -443,6 +529,10 @@ int
|
||||
buffer_vprint(buffer *buf, const char *fmt, va_list args)
|
||||
{
|
||||
int i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
|
||||
|
||||
if ((i < 0) && (buf->pos < buf->end))
|
||||
*buf->pos = 0;
|
||||
|
||||
buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
|
||||
return i;
|
||||
}
|
||||
@ -454,9 +544,12 @@ buffer_print(buffer *buf, const char *fmt, ...)
|
||||
int i;
|
||||
|
||||
va_start(args, fmt);
|
||||
i=bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
|
||||
i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if ((i < 0) && (buf->pos < buf->end))
|
||||
*buf->pos = 0;
|
||||
|
||||
buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
|
||||
return i;
|
||||
}
|
||||
@ -465,13 +558,13 @@ void
|
||||
buffer_puts(buffer *buf, const char *str)
|
||||
{
|
||||
byte *bp = buf->pos;
|
||||
byte *be = buf->end;
|
||||
byte *be = buf->end - 1;
|
||||
|
||||
while (bp < be && *str)
|
||||
*bp++ = *str++;
|
||||
|
||||
if (bp < be)
|
||||
if (bp <= be)
|
||||
*bp = 0;
|
||||
|
||||
buf->pos = bp;
|
||||
buf->pos = (bp < be) ? bp : buf->end;
|
||||
}
|
||||
|
128
lib/printf_test.c
Normal file
128
lib/printf_test.c
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* BIRD Library -- String Functions Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/string.h"
|
||||
|
||||
#define BSPRINTF(nw, res, buf, fmt, ...) \
|
||||
do { \
|
||||
int n = bsprintf(buf, fmt, ##__VA_ARGS__); \
|
||||
bt_assert_msg(n == nw, "fmt=\"%s\" returns n=%d, want %d", fmt, n, nw); \
|
||||
bt_assert_msg(buf[n] == 0, "fmt=\"%s\" buf[%d] should be \'\\0\', found 0x%02x", fmt, n, buf[n]); \
|
||||
bt_assert_msg(memcmp(buf, res, nw) == 0, "fmt=\"%s\" writes \"%*s\", want \"%*s\"", fmt, (n < nw ? n : nw), buf, nw, res); \
|
||||
} while (0)
|
||||
|
||||
static int
|
||||
t_simple(void)
|
||||
{
|
||||
char buf[256];
|
||||
memset(buf, 0xa5, 256);
|
||||
|
||||
BSPRINTF(0, "", buf, "", NULL);
|
||||
BSPRINTF(1, "%", buf, "%%", NULL);
|
||||
BSPRINTF(2, "%%", buf, "%%%%", NULL);
|
||||
|
||||
BSPRINTF(1, "\x00", buf, "%c", 0);
|
||||
BSPRINTF(1, "@", buf, "@", 64);
|
||||
BSPRINTF(1, "\xff", buf, "%c", 0xff);
|
||||
|
||||
errno = 5;
|
||||
BSPRINTF(18, "Input/output error", buf, "%m");
|
||||
errno = 0;
|
||||
|
||||
BSPRINTF(18, "Input/output error", buf, "%M", 5);
|
||||
|
||||
BSPRINTF(11, "TeSt%StRiNg", buf, "%s", "TeSt%StRiNg");
|
||||
|
||||
if (sizeof(void *) == 4)
|
||||
BSPRINTF(8, "1a15600d", buf, "%p", (void *) 0x1a15600d);
|
||||
else
|
||||
BSPRINTF(16, "00000fee1a15600d", buf, "%p", (void *) 0xfee1a15600d);
|
||||
|
||||
s64 ln = 0;
|
||||
BSPRINTF(10, "TeStStRiNg", buf, "TeStS%lntRiNg", &ln);
|
||||
bt_assert_msg(ln == 5, "fmt=\"TeStS%%lntRiNg\", &ln makes ln=%ld, want 5", ln);
|
||||
|
||||
BSPRINTF(2, "%d", buf, "%%d", 1);
|
||||
BSPRINTF(1, "1", buf, "%d", 1);
|
||||
BSPRINTF(2, "+1", buf, "%+d", 1);
|
||||
BSPRINTF(2, " 1", buf, "% d", 1);
|
||||
BSPRINTF(2, "-1", buf, "%d", -1);
|
||||
BSPRINTF(11, "-2147483648", buf, "%d", INT32_MIN);
|
||||
BSPRINTF(10, "2147483647", buf, "%d", INT32_MAX);
|
||||
|
||||
BSPRINTF(1, "0", buf, "%u", 0x0);
|
||||
BSPRINTF(10, "4294967295", buf, "%u", 0xFFFFFFFF);
|
||||
|
||||
BSPRINTF(4, "-100", buf, "%ld", (s64) -100);
|
||||
BSPRINTF(3, "100", buf, "%ld", (s64) 100);
|
||||
BSPRINTF(20, "-9223372036854775808", buf, "%ld", INT64_MIN);
|
||||
BSPRINTF(19, "9223372036854775807", buf, "%ld", INT64_MAX);
|
||||
|
||||
BSPRINTF(3, "0 8", buf, "%lu %lu", U64(0), U64(8));
|
||||
BSPRINTF(20, "18446744073709551615", buf, "%lu", UINT64_MAX);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_router_id(void)
|
||||
{
|
||||
char buf[256];
|
||||
|
||||
BSPRINTF(7, "1.2.3.4", buf, "%R", (u32) 0x01020304);
|
||||
BSPRINTF(15, "240.224.208.192", buf, "%R", (u32) 0xF0E0D0C0);
|
||||
BSPRINTF(23, "01:02:03:04:05:06:07:08", buf, "%lR", (u64) 0x0102030405060708);
|
||||
BSPRINTF(23, "f0:e0:d0:c0:b0:a0:90:80", buf, "%lR", (u64) 0xF0E0D0C0B0A09080);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_time(void)
|
||||
{
|
||||
char buf[256];
|
||||
|
||||
BSPRINTF(7, "123.456", buf, "%t", (btime) 123456789);
|
||||
BSPRINTF(7, "123.456", buf, "%2t", (btime) 123456789);
|
||||
BSPRINTF(8, " 123.456", buf, "%8t", (btime) 123456789);
|
||||
BSPRINTF(4, " 123", buf, "%4.0t", (btime) 123456789);
|
||||
BSPRINTF(8, "123.4567", buf, "%8.4t", (btime) 123456789);
|
||||
BSPRINTF(9, "0123.4567", buf, "%09.4t", (btime) 123456789);
|
||||
BSPRINTF(12, " 123.456789", buf, "%12.10t", (btime) 123456789);
|
||||
BSPRINTF(8, " 123.004", buf, "%8t", (btime) 123004 MS);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_bstrcmp(void)
|
||||
{
|
||||
bt_assert(bstrcmp("aa", "aa") == 0);
|
||||
bt_assert(bstrcmp("aa", "bb") == -1);
|
||||
bt_assert(bstrcmp("bb", "aa") == 1);
|
||||
bt_assert(bstrcmp(NULL, NULL) == 0);
|
||||
bt_assert(bstrcmp(NULL, "bb") == -1);
|
||||
bt_assert(bstrcmp("bb", NULL) == 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_simple, "printf without varargs");
|
||||
bt_test_suite(t_router_id, "print router id");
|
||||
bt_test_suite(t_time, "print time");
|
||||
bt_test_suite(t_bstrcmp, "bstrcmp");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
@ -31,7 +31,7 @@
|
||||
struct pool {
|
||||
resource r;
|
||||
list inside;
|
||||
char *name;
|
||||
const char *name;
|
||||
};
|
||||
|
||||
static void pool_dump(resource *);
|
||||
@ -61,7 +61,7 @@ static int indent;
|
||||
* parent pool.
|
||||
*/
|
||||
pool *
|
||||
rp_new(pool *p, char *name)
|
||||
rp_new(pool *p, const char *name)
|
||||
{
|
||||
pool *z = ralloc(p, &pool_class);
|
||||
z->name = name;
|
||||
@ -340,6 +340,7 @@ mb_alloc(pool *p, unsigned size)
|
||||
struct mblock *b = xmalloc(sizeof(struct mblock) + size);
|
||||
|
||||
b->r.class = &mb_class;
|
||||
b->r.n = (node) {};
|
||||
add_tail(&p->inside, &b->r.n);
|
||||
b->size = size;
|
||||
return b->data;
|
||||
@ -387,7 +388,7 @@ mb_realloc(void *m, unsigned size)
|
||||
struct mblock *b = SKIP_BACK(struct mblock, data, m);
|
||||
|
||||
b = xrealloc(b, sizeof(struct mblock) + size);
|
||||
replace_node(&b->r.n, &b->r.n);
|
||||
update_node(&b->r.n);
|
||||
b->size = size;
|
||||
return b->data;
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ struct resclass {
|
||||
typedef struct pool pool;
|
||||
|
||||
void resource_init(void);
|
||||
pool *rp_new(pool *, char *); /* Create new pool */
|
||||
pool *rp_new(pool *, const char *); /* Create new pool */
|
||||
void rfree(void *); /* Free single resource */
|
||||
void rdump(void *); /* Dump to debug output */
|
||||
size_t rmemsize(void *res); /* Return size of memory used by the resource */
|
||||
@ -59,11 +59,23 @@ void mb_free(void *);
|
||||
|
||||
typedef struct linpool linpool;
|
||||
|
||||
typedef struct lp_state {
|
||||
void *current, *large;
|
||||
byte *ptr;
|
||||
} lp_state;
|
||||
|
||||
linpool *lp_new(pool *, unsigned blk);
|
||||
void *lp_alloc(linpool *, unsigned size); /* Aligned */
|
||||
void *lp_allocu(linpool *, unsigned size); /* Unaligned */
|
||||
void *lp_allocz(linpool *, unsigned size); /* With clear */
|
||||
void lp_flush(linpool *); /* Free everything, but leave linpool */
|
||||
void lp_save(linpool *m, lp_state *p); /* Save state */
|
||||
void lp_restore(linpool *m, lp_state *p); /* Restore state */
|
||||
|
||||
extern const int lp_chunk_size;
|
||||
#define LP_GAS 1024
|
||||
#define LP_GOOD_SIZE(x) (((x + LP_GAS - 1) & (~(LP_GAS - 1))) - lp_chunk_size)
|
||||
#define lp_new_default(p) lp_new(p, LP_GOOD_SIZE(LP_GAS*4))
|
||||
|
||||
/* Slabs */
|
||||
|
||||
@ -71,6 +83,7 @@ typedef struct slab slab;
|
||||
|
||||
slab *sl_new(pool *, unsigned size);
|
||||
void *sl_alloc(slab *);
|
||||
void *sl_allocz(slab *);
|
||||
void sl_free(slab *, void *);
|
||||
|
||||
/*
|
||||
@ -89,9 +102,13 @@ void buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_siz
|
||||
*/
|
||||
#define DMALLOC_DISABLE
|
||||
#include <dmalloc.h>
|
||||
#define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
|
||||
#define xrealloc(size) _xrealloc_leap(__FILE__, __LINE__, size)
|
||||
#define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
|
||||
#define xmalloc(size) \
|
||||
dmalloc_malloc(__FILE__, __LINE__, (size), DMALLOC_FUNC_MALLOC, 0, 1)
|
||||
#define xrealloc(ptr, size) \
|
||||
dmalloc_realloc(__FILE__, __LINE__, (ptr), (size), DMALLOC_FUNC_REALLOC, 1)
|
||||
#define xfree(ptr) \
|
||||
dmalloc_free(__FILE__, __LINE__, (ptr), DMALLOC_FUNC_FREE)
|
||||
|
||||
#else
|
||||
/*
|
||||
* Unfortunately, several libraries we might want to link to define
|
||||
|
31
lib/slab.c
31
lib/slab.c
@ -88,6 +88,14 @@ sl_alloc(slab *s)
|
||||
return o->data;
|
||||
}
|
||||
|
||||
void *
|
||||
sl_allocz(slab *s)
|
||||
{
|
||||
void *obj = sl_alloc(s);
|
||||
memset(obj, 0, s->size);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void
|
||||
sl_free(slab *s, void *oo)
|
||||
{
|
||||
@ -216,8 +224,11 @@ sl_new_head(slab *s)
|
||||
struct sl_obj *no;
|
||||
uint n = s->objs_per_slab;
|
||||
|
||||
h->first_free = o;
|
||||
h->num_full = 0;
|
||||
*h = (struct sl_head) {
|
||||
.first_free = o,
|
||||
.num_full = 0,
|
||||
};
|
||||
|
||||
while (n--)
|
||||
{
|
||||
o->slab = h;
|
||||
@ -275,6 +286,22 @@ no_partial:
|
||||
goto okay;
|
||||
}
|
||||
|
||||
/**
|
||||
* sl_allocz - allocate an object from Slab and zero it
|
||||
* @s: slab
|
||||
*
|
||||
* sl_allocz() allocates space for a single object from the
|
||||
* Slab and returns a pointer to the object after zeroing out
|
||||
* the object memory.
|
||||
*/
|
||||
void *
|
||||
sl_allocz(slab *s)
|
||||
{
|
||||
void *obj = sl_alloc(s);
|
||||
memset(obj, 0, s->data_size);
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* sl_free - return a free object back to a Slab
|
||||
* @s: slab
|
||||
|
384
lib/slist_test.c
Normal file
384
lib/slist_test.c
Normal file
@ -0,0 +1,384 @@
|
||||
/*
|
||||
* BIRD Library -- Safe Linked Lists Tests
|
||||
*
|
||||
* (c) 2015 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "test/birdtest.h"
|
||||
|
||||
#include "lib/slists.h"
|
||||
|
||||
#define MAX_NUM 1000
|
||||
|
||||
static snode nodes[MAX_NUM];
|
||||
static slist lst;
|
||||
|
||||
static void
|
||||
show_list(void)
|
||||
{
|
||||
bt_debug("\n");
|
||||
bt_debug("list.null is at %p and point to %p \n", &lst.null, lst.null);
|
||||
bt_debug("list.head is at %p and point to %p \n", &lst.head, lst.head);
|
||||
bt_debug("list.tail is at %p and point to %p \n", &lst.tail, lst.tail);
|
||||
bt_debug("list.tail_readers is at %p and point to %p \n", &lst.tail_readers, lst.tail_readers);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
bt_debug("n[%3i] is at %p, .prev (%p) points to %p, .next (%p) points to %p, .readers (%p) points to %p \n",
|
||||
i, &nodes[i], &(nodes[i].prev), nodes[i].prev, &(nodes[i].next), nodes[i].next, &(nodes[i].readers), nodes[i].readers);
|
||||
}
|
||||
|
||||
static int
|
||||
is_filled_list_well_linked(void)
|
||||
{
|
||||
int i;
|
||||
bt_assert(lst.head == &nodes[0]);
|
||||
bt_assert(lst.tail == &nodes[MAX_NUM-1]);
|
||||
bt_assert((void *) nodes[0].prev == (void *) &lst.head);
|
||||
bt_assert((void *) nodes[MAX_NUM-1].next == (void *) &lst.null);
|
||||
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
if (i < (MAX_NUM-1))
|
||||
bt_assert(nodes[i].next == &nodes[i+1]);
|
||||
|
||||
if (i > 0)
|
||||
bt_assert(nodes[i].prev == &nodes[i-1]);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
is_empty_list_well_unlinked(void)
|
||||
{
|
||||
bt_assert(lst.head == SNODE &lst.null);
|
||||
bt_assert(lst.tail == SNODE &lst.head);
|
||||
|
||||
bt_assert(EMPTY_SLIST(lst));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
init_list__(slist *l, struct snode nodes[])
|
||||
{
|
||||
s_init_list(l);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
nodes[i].next = NULL;
|
||||
nodes[i].prev = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
init_list_(void)
|
||||
{
|
||||
init_list__(&lst, nodes);
|
||||
}
|
||||
|
||||
static int
|
||||
t_add_tail(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
s_add_tail(&lst, &nodes[i]);
|
||||
bt_debug(".");
|
||||
bt_assert(lst.tail == &nodes[i]);
|
||||
bt_assert(lst.head == &nodes[0]);
|
||||
bt_assert((void *) nodes[i].next == (void *) &lst.null);
|
||||
if (i > 0)
|
||||
{
|
||||
bt_assert(nodes[i-1].next == &nodes[i]);
|
||||
bt_assert(nodes[i].prev == &nodes[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
bt_assert(is_filled_list_well_linked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_add_head(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
for (i = MAX_NUM-1; i >= 0; i--)
|
||||
{
|
||||
s_add_head(&lst, &nodes[i]);
|
||||
bt_debug(".");
|
||||
bt_assert(lst.head == &nodes[i]);
|
||||
bt_assert(lst.tail == &nodes[MAX_NUM-1]);
|
||||
if (i < MAX_NUM-1)
|
||||
{
|
||||
bt_assert(nodes[i+1].prev == &nodes[i]);
|
||||
bt_assert(nodes[i].next == &nodes[i+1]);
|
||||
}
|
||||
}
|
||||
|
||||
bt_assert(is_filled_list_well_linked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
insert_node_(snode *n, snode *after)
|
||||
{
|
||||
s_insert_node(n, after);
|
||||
bt_debug(".");
|
||||
}
|
||||
|
||||
static int
|
||||
t_insert_node(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
|
||||
// add first node
|
||||
insert_node_(&nodes[0], SNODE &lst.head);
|
||||
|
||||
// add odd nodes
|
||||
for (i = 2; i < MAX_NUM; i+=2)
|
||||
insert_node_(&nodes[i], &nodes[i-2]);
|
||||
|
||||
// add even nodes
|
||||
for (i = 1; i < MAX_NUM; i+=2)
|
||||
insert_node_(&nodes[i], &nodes[i-1]);
|
||||
|
||||
bt_debug("\n");
|
||||
bt_assert(is_filled_list_well_linked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
fill_list2(slist *l, snode nodes[])
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
s_add_tail(l, &nodes[i]);
|
||||
}
|
||||
|
||||
static void
|
||||
fill_list(void)
|
||||
{
|
||||
fill_list2(&lst, SNODE nodes);
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
t_remove_node(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
init_list_();
|
||||
|
||||
/* Fill & Remove & Check */
|
||||
fill_list();
|
||||
for (i = 0; i < MAX_NUM; i++)
|
||||
s_rem_node(&nodes[i]);
|
||||
bt_assert(is_empty_list_well_unlinked());
|
||||
|
||||
/* Fill & Remove the half of nodes & Check & Remove the rest nodes & Check */
|
||||
fill_list();
|
||||
for (i = 0; i < MAX_NUM; i+=2)
|
||||
s_rem_node(&nodes[i]);
|
||||
|
||||
int tail_node_index = (MAX_NUM % 2) ? MAX_NUM - 2 : MAX_NUM - 1;
|
||||
bt_assert(lst.head == &nodes[1]);
|
||||
bt_assert(lst.tail == &nodes[tail_node_index]);
|
||||
bt_assert(nodes[tail_node_index].next == SNODE &lst.null);
|
||||
|
||||
for (i = 1; i < MAX_NUM; i+=2)
|
||||
{
|
||||
if (i > 1)
|
||||
bt_assert(nodes[i].prev == &nodes[i-2]);
|
||||
if (i < tail_node_index)
|
||||
bt_assert(nodes[i].next == &nodes[i+2]);
|
||||
}
|
||||
|
||||
for (i = 1; i < MAX_NUM; i+=2)
|
||||
s_rem_node(&nodes[i]);
|
||||
bt_assert(is_empty_list_well_unlinked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_add_tail_list(void)
|
||||
{
|
||||
snode nodes2[MAX_NUM];
|
||||
slist l2;
|
||||
|
||||
init_list__(&lst, SNODE &nodes);
|
||||
fill_list2(&lst, SNODE &nodes);
|
||||
|
||||
init_list__(&l2, SNODE &nodes2);
|
||||
fill_list2(&l2, SNODE &nodes2);
|
||||
|
||||
s_add_tail_list(&lst, &l2);
|
||||
|
||||
bt_assert(nodes[MAX_NUM-1].next == &nodes2[0]);
|
||||
bt_assert(nodes2[0].prev == &nodes[MAX_NUM-1]);
|
||||
bt_assert(lst.tail == &nodes2[MAX_NUM-1]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
dump(const char *str, slist *a)
|
||||
{
|
||||
snode *x;
|
||||
|
||||
bt_debug("%s \n", str);
|
||||
for (x = SHEAD(*a); x; x = x->next)
|
||||
{
|
||||
siterator *i, *j;
|
||||
bt_debug("%p", x);
|
||||
j = (siterator *) x;
|
||||
for (i = x->readers; i; i = i->next)
|
||||
{
|
||||
if (i->prev != j)
|
||||
bt_debug(" ???");
|
||||
j = i;
|
||||
bt_debug(" [%p:%p]", i, i->node);
|
||||
}
|
||||
bt_debug("\n");
|
||||
}
|
||||
bt_debug("---\n");
|
||||
}
|
||||
|
||||
static int
|
||||
t_iterator_walk(void)
|
||||
{
|
||||
snode *node;
|
||||
siterator iter;
|
||||
|
||||
init_list_();
|
||||
fill_list();
|
||||
|
||||
int k;
|
||||
int i = 0;
|
||||
|
||||
show_list();
|
||||
|
||||
s_init(&iter, &lst);
|
||||
WALK_SLIST(node, lst)
|
||||
{
|
||||
s_get(&iter);
|
||||
s_put(&iter, node);
|
||||
bt_debug("node->readers: %p, iter: %p, nodes[%d].readers: %p, node: %p, nodes[i]: %p, node->next: %p \n",
|
||||
node->readers, &iter, i, nodes[i].readers, node, &(nodes[i]), node->next);
|
||||
bt_assert(node->readers == &iter);
|
||||
bt_assert(node->readers == nodes[i].readers);
|
||||
bt_assert(node == &(nodes[i]));
|
||||
for (k = 0; k < MAX_NUM; k++)
|
||||
if (k != i)
|
||||
bt_assert(nodes[k].readers == NULL);
|
||||
|
||||
dump("",&lst);
|
||||
i++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_original(void)
|
||||
{
|
||||
slist a, b;
|
||||
snode *x, *y;
|
||||
siterator i, j;
|
||||
|
||||
s_init_list(&a);
|
||||
s_init_list(&b);
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&a, x);
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&a, x);
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&a, x);
|
||||
dump("1", &a);
|
||||
|
||||
s_init(&i, &a);
|
||||
s_init(&j, &a);
|
||||
dump("2", &a);
|
||||
|
||||
x = s_get(&i);
|
||||
bt_debug("Got %p\n", x);
|
||||
dump("3", &a);
|
||||
|
||||
s_put(&i, x->next);
|
||||
dump("4", &a);
|
||||
|
||||
y = s_get(&j);
|
||||
while (y)
|
||||
{
|
||||
s_put(&j, y);
|
||||
dump("5*", &a);
|
||||
y = s_get(&j)->next;
|
||||
}
|
||||
|
||||
dump("5 done", &a);
|
||||
|
||||
s_rem_node(a.head->next);
|
||||
dump("6 (deletion)", &a);
|
||||
|
||||
s_put(&i, s_get(&i)->next);
|
||||
dump("6 (relink)", &a);
|
||||
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&b, x);
|
||||
dump("7 (second list)", &b);
|
||||
|
||||
s_add_tail_list(&b, &a);
|
||||
dump("8 (after merge)", &b);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
t_safe_del_walk(void)
|
||||
{
|
||||
init_list_();
|
||||
fill_list();
|
||||
|
||||
show_list();
|
||||
|
||||
snode *node, *node_next;
|
||||
WALK_SLIST_DELSAFE(node,node_next, lst)
|
||||
{
|
||||
bt_debug("Will remove node %p \n", node);
|
||||
s_rem_node(SNODE node);
|
||||
}
|
||||
bt_assert(is_empty_list_well_unlinked());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
bt_init(argc, argv);
|
||||
|
||||
bt_test_suite(t_add_tail, "Adding nodes to tail of list");
|
||||
bt_test_suite(t_add_head, "Adding nodes to head of list");
|
||||
bt_test_suite(t_insert_node, "Inserting nodes to list");
|
||||
bt_test_suite(t_remove_node, "Removing nodes from list");
|
||||
bt_test_suite(t_add_tail_list, "At the tail of a list adding the another list");
|
||||
bt_test_suite(t_iterator_walk, "Iterator walk");
|
||||
bt_test_suite(t_safe_del_walk, "WALK_SLIST_DELSAFE and s_rem_node all nodes");
|
||||
bt_test_suite(t_original, "The original BIRD test suit for SLIST");
|
||||
|
||||
return bt_exit_value();
|
||||
}
|
82
lib/slists.c
82
lib/slists.c
@ -150,85 +150,3 @@ s_add_tail_list(slist *to, slist *l)
|
||||
to->tail = q;
|
||||
s_merge((snode *) &l->null, (snode *) &to->null);
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
#include "lib/resource.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void dump(char *c, slist *a)
|
||||
{
|
||||
snode *x;
|
||||
|
||||
puts(c);
|
||||
for(x=SHEAD(*a); x; x=x->next)
|
||||
{
|
||||
siterator *i, *j;
|
||||
printf("%p", x);
|
||||
j = (siterator *) x;
|
||||
for(i=x->readers; i; i=i->next)
|
||||
{
|
||||
if (i->prev != j)
|
||||
printf(" ???");
|
||||
j = i;
|
||||
printf(" [%p:%p]", i, i->node);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
puts("---");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
slist a, b;
|
||||
snode *x, *y;
|
||||
siterator i, j;
|
||||
|
||||
s_init_list(&a);
|
||||
s_init_list(&b);
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&a, x);
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&a, x);
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&a, x);
|
||||
dump("1", &a);
|
||||
|
||||
s_init(&i, &a);
|
||||
s_init(&j, &a);
|
||||
dump("2", &a);
|
||||
|
||||
x = s_get(&i);
|
||||
printf("Got %p\n", x);
|
||||
dump("3", &a);
|
||||
|
||||
s_put(&i, x->next);
|
||||
dump("4", &a);
|
||||
|
||||
y = s_get(&j);
|
||||
while (y)
|
||||
{
|
||||
s_put(&j, y);
|
||||
dump("5*", &a);
|
||||
y = s_get(&j)->next;
|
||||
}
|
||||
|
||||
dump("5 done", &a);
|
||||
|
||||
s_rem_node(a.head->next);
|
||||
dump("6 (deletion)", &a);
|
||||
|
||||
s_put(&i, s_get(&i)->next);
|
||||
dump("6 (relink)", &a);
|
||||
|
||||
x = xmalloc(sizeof(*x));
|
||||
s_add_tail(&b, x);
|
||||
dump("7 (second list)", &b);
|
||||
|
||||
s_add_tail_list(&b, &a);
|
||||
dump("8 (after merge)", &b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
93
lib/socket.h
93
lib/socket.h
@ -10,16 +10,40 @@
|
||||
#define _BIRD_SOCKET_H_
|
||||
|
||||
#include <errno.h>
|
||||
// #include <sys/socket.h>
|
||||
|
||||
#include "lib/resource.h"
|
||||
#ifdef HAVE_LIBSSH
|
||||
#define LIBSSH_LEGACY_0_4
|
||||
#include <libssh/libssh.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LIBSSH
|
||||
struct ssh_sock {
|
||||
const char *username; /* (Required) SSH user name */
|
||||
const char *server_hostkey_path; /* (Optional) Filepath to the SSH public key of remote side, can be knownhost file */
|
||||
const char *client_privkey_path; /* (Optional) Filepath to the SSH private key of BIRD */
|
||||
const char *subsystem; /* (Optional) Name of SSH subsytem */
|
||||
ssh_session session; /* Internal */
|
||||
ssh_channel channel; /* Internal */
|
||||
int state; /* Internal */
|
||||
#define SK_SSH_CONNECT 0 /* Start state */
|
||||
#define SK_SSH_SERVER_KNOWN 1 /* Internal */
|
||||
#define SK_SSH_USERAUTH 2 /* Internal */
|
||||
#define SK_SSH_CHANNEL 3 /* Internal */
|
||||
#define SK_SSH_SESSION 4 /* Internal */
|
||||
#define SK_SSH_SUBSYSTEM 5 /* Internal */
|
||||
#define SK_SSH_ESTABLISHED 6 /* Final state */
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef struct birdsock {
|
||||
resource r;
|
||||
pool *pool; /* Pool where incoming connections should be allocated (for SK_xxx_PASSIVE) */
|
||||
int type; /* Socket type */
|
||||
int subtype; /* Socket subtype */
|
||||
void *data; /* User data */
|
||||
ip_addr saddr, daddr; /* IPA_NONE = unspecified */
|
||||
const char *host; /* Alternative to daddr, NULL = unspecified */
|
||||
uint sport, dport; /* 0 = unspecified (for IP: protocol type) */
|
||||
int tos; /* TOS / traffic class, -1 = default */
|
||||
int priority; /* Local socket priority, -1 = default */
|
||||
@ -46,14 +70,15 @@ typedef struct birdsock {
|
||||
uint lifindex; /* local interface that received the datagram */
|
||||
/* laddr and lifindex are valid only if SKF_LADDR_RX flag is set to request it */
|
||||
|
||||
int af; /* Address family (AF_INET, AF_INET6 or 0 for non-IP) of fd */
|
||||
int af; /* System-dependend adress family (e.g. AF_INET) */
|
||||
int fd; /* System-dependent data */
|
||||
int index; /* Index in poll buffer */
|
||||
int rcv_ttl; /* TTL of last received datagram */
|
||||
node n;
|
||||
void *rbuf_alloc, *tbuf_alloc;
|
||||
char *password; /* Password for MD5 authentication */
|
||||
char *err; /* Error message */
|
||||
const char *password; /* Password for MD5 authentication */
|
||||
const char *err; /* Error message */
|
||||
struct ssh_sock *ssh; /* Used in SK_SSH */
|
||||
} sock;
|
||||
|
||||
sock *sock_new(pool *); /* Allocate new socket */
|
||||
@ -69,26 +94,19 @@ void sk_set_tbsize(sock *s, uint val); /* Resize TX buffer, keeping content */
|
||||
void sk_set_tbuf(sock *s, void *tbuf); /* Switch TX buffer, NULL-> return to internal */
|
||||
void sk_dump_all(void);
|
||||
|
||||
static inline int sk_send_buffer_empty(sock *sk)
|
||||
int sk_is_ipv4(sock *s); /* True if socket is IPv4 */
|
||||
int sk_is_ipv6(sock *s); /* True if socket is IPv6 */
|
||||
|
||||
static inline int sk_tx_buffer_empty(sock *sk)
|
||||
{ return sk->tbuf == sk->tpos; }
|
||||
|
||||
|
||||
#ifdef IPV6
|
||||
#define sk_is_ipv4(X) 0
|
||||
#define sk_is_ipv6(X) 1
|
||||
#else
|
||||
#define sk_is_ipv4(X) 1
|
||||
#define sk_is_ipv6(X) 0
|
||||
#endif
|
||||
|
||||
|
||||
int sk_setup_multicast(sock *s); /* Prepare UDP or IP socket for multicasting */
|
||||
int sk_join_group(sock *s, ip_addr maddr); /* Join multicast group on sk iface */
|
||||
int sk_leave_group(sock *s, ip_addr maddr); /* Leave multicast group on sk iface */
|
||||
int sk_setup_broadcast(sock *s);
|
||||
int sk_set_ttl(sock *s, int ttl); /* Set transmit TTL for given socket */
|
||||
int sk_set_min_ttl(sock *s, int ttl); /* Set minimal accepted TTL for given socket */
|
||||
int sk_set_md5_auth(sock *s, ip_addr local, ip_addr remote, struct iface *ifa, char *passwd, int setkey);
|
||||
int sk_set_md5_auth(sock *s, ip_addr local, ip_addr remote, int pxlen, struct iface *ifa, const char *passwd, int setkey);
|
||||
int sk_set_ipv6_checksum(sock *s, int offset);
|
||||
int sk_set_icmp6_filter(sock *s, int p1, int p2);
|
||||
void sk_log_error(sock *s, const char *p);
|
||||
@ -100,7 +118,6 @@ extern int sk_priority_control; /* Suggested priority for control traffic, shou
|
||||
|
||||
/* Socket flags */
|
||||
|
||||
#define SKF_V4ONLY 0x01 /* Use IPv4 for IP sockets */
|
||||
#define SKF_V6ONLY 0x02 /* Use IPV6_V6ONLY socket option */
|
||||
#define SKF_LADDR_RX 0x04 /* Report local address for RX packets */
|
||||
#define SKF_TTL_RX 0x08 /* Report TTL / Hop Limit for RX packets */
|
||||
@ -124,26 +141,38 @@ extern int sk_priority_control; /* Suggested priority for control traffic, shou
|
||||
#define SK_MAGIC 7 /* Internal use by sysdep code */
|
||||
#define SK_UNIX_PASSIVE 8
|
||||
#define SK_UNIX 9
|
||||
#define SK_SSH_ACTIVE 10 /* - - * * - ? - DA = host */
|
||||
#define SK_SSH 11
|
||||
|
||||
/*
|
||||
* For SK_UDP or SK_IP sockets setting DA/DP allows to use sk_send(),
|
||||
* otherwise sk_send_to() must be used.
|
||||
* Socket subtypes
|
||||
*/
|
||||
|
||||
#define SK_IPV4 1
|
||||
#define SK_IPV6 2
|
||||
|
||||
/*
|
||||
* For TCP/IP sockets, Address family (IPv4 or IPv6) can be specified either
|
||||
* explicitly (SK_IPV4 or SK_IPV6) or implicitly (based on saddr, daddr). But
|
||||
* these specifications must be consistent.
|
||||
*
|
||||
* For SK_IP sockets setting DP specifies protocol number, which is used
|
||||
* for both receiving and sending.
|
||||
* For SK_UDP or SK_IP sockets setting DA/DP allows to use sk_send(), otherwise
|
||||
* sk_send_to() must be used.
|
||||
*
|
||||
* For multicast on SK_UDP or SK_IP sockets set IF and TTL,
|
||||
* call sk_setup_multicast() to enable multicast on that socket,
|
||||
* and then use sk_join_group() and sk_leave_group() to manage
|
||||
* a set of received multicast groups.
|
||||
* For SK_IP sockets setting DP specifies protocol number, which is used for
|
||||
* both receiving and sending.
|
||||
*
|
||||
* For datagram (SK_UDP, SK_IP) sockets, there are two ways to handle
|
||||
* source address. The socket could be bound to it using bind()
|
||||
* syscall, but that also forbids the reception of multicast packets,
|
||||
* or the address could be set on per-packet basis using platform
|
||||
* dependent options (but these are not available in some corner
|
||||
* cases). The first way is used when SKF_BIND is specified, the
|
||||
* second way is used otherwise.
|
||||
* For multicast on SK_UDP or SK_IP sockets set IF and TTL, call
|
||||
* sk_setup_multicast() to enable multicast on that socket, and then use
|
||||
* sk_join_group() and sk_leave_group() to manage a set of received multicast
|
||||
* groups.
|
||||
*
|
||||
* For datagram (SK_UDP, SK_IP) sockets, there are two ways to handle source
|
||||
* address. The socket could be bound to it using bind() syscall, but that also
|
||||
* forbids the reception of multicast packets, or the address could be set on
|
||||
* per-packet basis using platform dependent options (but these are not
|
||||
* available in some corner cases). The first way is used when SKF_BIND is
|
||||
* specified, the second way is used otherwise.
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
32
lib/string.h
32
lib/string.h
@ -13,6 +13,8 @@
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "lib/resource.h"
|
||||
|
||||
int bsprintf(char *str, const char *fmt, ...);
|
||||
int bvsprintf(char *str, const char *fmt, va_list args);
|
||||
int bsnprintf(char *str, int size, const char *fmt, ...);
|
||||
@ -22,6 +24,9 @@ int buffer_vprint(buffer *buf, const char *fmt, va_list args);
|
||||
int buffer_print(buffer *buf, const char *fmt, ...);
|
||||
void buffer_puts(buffer *buf, const char *str);
|
||||
|
||||
u64 bstrtoul10(const char *str, char **end);
|
||||
u64 bstrtoul16(const char *str, char **end);
|
||||
|
||||
int patmatch(const byte *pat, const byte *str);
|
||||
|
||||
static inline char *xbasename(const char *str)
|
||||
@ -39,6 +44,15 @@ xstrdup(const char *c)
|
||||
return z;
|
||||
}
|
||||
|
||||
static inline char *
|
||||
lp_strdup(linpool *lp, const char *c)
|
||||
{
|
||||
size_t l = strlen(c) + 1;
|
||||
char *z = lp_allocu(lp, l);
|
||||
memcpy(z, c, l);
|
||||
return z;
|
||||
}
|
||||
|
||||
static inline void
|
||||
memset32(void *D, u32 val, uint n)
|
||||
{
|
||||
@ -49,6 +63,24 @@ memset32(void *D, u32 val, uint n)
|
||||
dst[i] = val;
|
||||
}
|
||||
|
||||
static inline int
|
||||
bstrcmp(const char *s1, const char *s2)
|
||||
{
|
||||
if (s1 && s2)
|
||||
return strcmp(s1, s2);
|
||||
else
|
||||
return !s2 - !s1;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
bmemcpy(void *dest, const void *src, size_t n)
|
||||
{
|
||||
if (n)
|
||||
return memcpy(dest, src, n);
|
||||
else
|
||||
return dest;
|
||||
}
|
||||
|
||||
#define ROUTER_ID_64_LENGTH 23
|
||||
|
||||
#endif
|
||||
|
61
lib/strtoul.c
Normal file
61
lib/strtoul.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* BIRD Library -- Parse numbers
|
||||
*
|
||||
* (c) 2019 Maria Matejka <mq@jmq.cz>
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/string.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#define ULI_MAX_DIV10 (UINT64_MAX / 10)
|
||||
#define ULI_MAX_MOD10 (UINT64_MAX % 10)
|
||||
|
||||
u64
|
||||
bstrtoul10(const char *str, char **end)
|
||||
{
|
||||
u64 out = 0;
|
||||
for (*end = (char *) str; (**end >= '0') && (**end <= '9'); (*end)++) {
|
||||
u64 digit = **end - '0';
|
||||
if ((out > ULI_MAX_DIV10) ||
|
||||
(out == ULI_MAX_DIV10) && (digit > ULI_MAX_MOD10)) {
|
||||
errno = ERANGE;
|
||||
return UINT64_MAX;
|
||||
}
|
||||
|
||||
out *= 10;
|
||||
out += (**end) - '0';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
u64
|
||||
bstrtoul16(const char *str, char **end)
|
||||
{
|
||||
u64 out = 0;
|
||||
for (int i=0; i<=(64/4); i++) {
|
||||
switch (str[i]) {
|
||||
case '0' ... '9':
|
||||
out *= 16;
|
||||
out += str[i] - '0';
|
||||
break;
|
||||
case 'a' ... 'f':
|
||||
out *= 16;
|
||||
out += str[i] + 10 - 'a';
|
||||
break;
|
||||
case 'A' ... 'F':
|
||||
out *= 16;
|
||||
out += str[i] + 10 - 'A';
|
||||
break;
|
||||
default:
|
||||
*end = (char *) &(str[i]);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
errno = ERANGE;
|
||||
return UINT64_MAX;
|
||||
}
|
32
lib/tbf.c
32
lib/tbf.c
@ -8,22 +8,30 @@
|
||||
*/
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "lib/timer.h"
|
||||
|
||||
void
|
||||
tbf_update(struct tbf *f)
|
||||
int
|
||||
tbf_limit(struct tbf *f)
|
||||
{
|
||||
bird_clock_t delta = now - f->timestamp;
|
||||
btime delta = current_time() - f->timestamp;
|
||||
|
||||
if (delta == 0)
|
||||
return;
|
||||
|
||||
f->timestamp = now;
|
||||
|
||||
if ((0 < delta) && (delta < f->burst))
|
||||
if (delta > 0)
|
||||
{
|
||||
u32 next = f->count + delta * f->rate;
|
||||
f->count = MIN(next, f->burst);
|
||||
u64 next = f->count + delta * f->rate;
|
||||
u64 burst = (u64) f->burst << 20;
|
||||
f->count = MIN(next, burst);
|
||||
f->timestamp += delta;
|
||||
}
|
||||
|
||||
if (f->count < 1000000)
|
||||
{
|
||||
f->drop++;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
f->count = f->burst;
|
||||
{
|
||||
f->count -= 1000000;
|
||||
f->drop = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
376
lib/timer.c
Normal file
376
lib/timer.c
Normal file
@ -0,0 +1,376 @@
|
||||
/*
|
||||
* BIRD -- Timers
|
||||
*
|
||||
* (c) 2013--2017 Ondrej Zajicek <santiago@crfreenet.org>
|
||||
* (c) 2013--2017 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
/**
|
||||
* DOC: Timers
|
||||
*
|
||||
* Timers are resources which represent a wish of a module to call a function at
|
||||
* the specified time. The timer code does not guarantee exact timing, only that
|
||||
* a timer function will not be called before the requested time.
|
||||
*
|
||||
* In BIRD, time is represented by values of the &btime type which is signed
|
||||
* 64-bit integer interpreted as a relative number of microseconds since some
|
||||
* fixed time point in past. The current time can be obtained by current_time()
|
||||
* function with reasonable accuracy and is monotonic. There is also a current
|
||||
* 'wall-clock' real time obtainable by current_real_time() reported by OS.
|
||||
*
|
||||
* Each timer is described by a &timer structure containing a pointer to the
|
||||
* handler function (@hook), data private to this function (@data), time the
|
||||
* function should be called at (@expires, 0 for inactive timers), for the other
|
||||
* fields see |timer.h|.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "nest/bird.h"
|
||||
|
||||
#include "lib/heap.h"
|
||||
#include "lib/resource.h"
|
||||
#include "lib/timer.h"
|
||||
|
||||
|
||||
struct timeloop main_timeloop;
|
||||
|
||||
|
||||
#ifdef USE_PTHREADS
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
/* Data accessed and modified from proto/bfd/io.c */
|
||||
pthread_key_t current_time_key;
|
||||
|
||||
static inline struct timeloop *
|
||||
timeloop_current(void)
|
||||
{
|
||||
return pthread_getspecific(current_time_key);
|
||||
}
|
||||
|
||||
static inline void
|
||||
timeloop_init_current(void)
|
||||
{
|
||||
pthread_key_create(¤t_time_key, NULL);
|
||||
pthread_setspecific(current_time_key, &main_timeloop);
|
||||
}
|
||||
|
||||
void wakeup_kick_current(void);
|
||||
|
||||
#else
|
||||
|
||||
/* Just use main timelooop */
|
||||
static inline struct timeloop * timeloop_current(void) { return &main_timeloop; }
|
||||
static inline void timeloop_init_current(void) { }
|
||||
|
||||
#endif
|
||||
|
||||
btime
|
||||
current_time(void)
|
||||
{
|
||||
return timeloop_current()->last_time;
|
||||
}
|
||||
|
||||
btime
|
||||
current_real_time(void)
|
||||
{
|
||||
struct timeloop *loop = timeloop_current();
|
||||
|
||||
if (!loop->real_time)
|
||||
times_update_real_time(loop);
|
||||
|
||||
return loop->real_time;
|
||||
}
|
||||
|
||||
|
||||
#define TIMER_LESS(a,b) ((a)->expires < (b)->expires)
|
||||
#define TIMER_SWAP(heap,a,b,t) (t = heap[a], heap[a] = heap[b], heap[b] = t, \
|
||||
heap[a]->index = (a), heap[b]->index = (b))
|
||||
|
||||
|
||||
static void
|
||||
tm_free(resource *r)
|
||||
{
|
||||
timer *t = (void *) r;
|
||||
|
||||
tm_stop(t);
|
||||
}
|
||||
|
||||
static void
|
||||
tm_dump(resource *r)
|
||||
{
|
||||
timer *t = (void *) r;
|
||||
|
||||
debug("(code %p, data %p, ", t->hook, t->data);
|
||||
if (t->randomize)
|
||||
debug("rand %d, ", t->randomize);
|
||||
if (t->recurrent)
|
||||
debug("recur %d, ", t->recurrent);
|
||||
if (t->expires)
|
||||
debug("expires in %d ms)\n", (t->expires - current_time()) TO_MS);
|
||||
else
|
||||
debug("inactive)\n");
|
||||
}
|
||||
|
||||
|
||||
static struct resclass tm_class = {
|
||||
"Timer",
|
||||
sizeof(timer),
|
||||
tm_free,
|
||||
tm_dump,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
timer *
|
||||
tm_new(pool *p)
|
||||
{
|
||||
timer *t = ralloc(p, &tm_class);
|
||||
t->index = -1;
|
||||
return t;
|
||||
}
|
||||
|
||||
void
|
||||
tm_set(timer *t, btime when)
|
||||
{
|
||||
struct timeloop *loop = timeloop_current();
|
||||
uint tc = timers_count(loop);
|
||||
|
||||
if (!t->expires)
|
||||
{
|
||||
t->index = ++tc;
|
||||
t->expires = when;
|
||||
BUFFER_PUSH(loop->timers) = t;
|
||||
HEAP_INSERT(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP);
|
||||
}
|
||||
else if (t->expires < when)
|
||||
{
|
||||
t->expires = when;
|
||||
HEAP_INCREASE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
||||
}
|
||||
else if (t->expires > when)
|
||||
{
|
||||
t->expires = when;
|
||||
HEAP_DECREASE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_BFD
|
||||
/* Hack to notify BFD loops */
|
||||
if ((loop != &main_timeloop) && (t->index == 1))
|
||||
wakeup_kick_current();
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
tm_start(timer *t, btime after)
|
||||
{
|
||||
tm_set(t, current_time() + MAX(after, 0));
|
||||
}
|
||||
|
||||
void
|
||||
tm_stop(timer *t)
|
||||
{
|
||||
if (!t->expires)
|
||||
return;
|
||||
|
||||
struct timeloop *loop = timeloop_current();
|
||||
uint tc = timers_count(loop);
|
||||
|
||||
HEAP_DELETE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
||||
BUFFER_POP(loop->timers);
|
||||
|
||||
t->index = -1;
|
||||
t->expires = 0;
|
||||
}
|
||||
|
||||
void
|
||||
timers_init(struct timeloop *loop, pool *p)
|
||||
{
|
||||
times_init(loop);
|
||||
|
||||
BUFFER_INIT(loop->timers, p, 4);
|
||||
BUFFER_PUSH(loop->timers) = NULL;
|
||||
}
|
||||
|
||||
void io_log_event(void *hook, void *data);
|
||||
|
||||
void
|
||||
timers_fire(struct timeloop *loop)
|
||||
{
|
||||
btime base_time;
|
||||
timer *t;
|
||||
|
||||
times_update(loop);
|
||||
base_time = loop->last_time;
|
||||
|
||||
while (t = timers_first(loop))
|
||||
{
|
||||
if (t->expires > base_time)
|
||||
return;
|
||||
|
||||
if (t->recurrent)
|
||||
{
|
||||
btime when = t->expires + t->recurrent;
|
||||
|
||||
if (when <= loop->last_time)
|
||||
when = loop->last_time + t->recurrent;
|
||||
|
||||
if (t->randomize)
|
||||
when += random() % (t->randomize + 1);
|
||||
|
||||
tm_set(t, when);
|
||||
}
|
||||
else
|
||||
tm_stop(t);
|
||||
|
||||
/* This is ugly hack, we want to log just timers executed from the main I/O loop */
|
||||
if (loop == &main_timeloop)
|
||||
io_log_event(t->hook, t->data);
|
||||
|
||||
t->hook(t);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
timer_init(void)
|
||||
{
|
||||
timers_init(&main_timeloop, &root_pool);
|
||||
timeloop_init_current();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* tm_parse_time - parse a date and time
|
||||
* @x: time string
|
||||
*
|
||||
* tm_parse_time() takes a textual representation of a date and time
|
||||
* (yyyy-mm-dd[ hh:mm:ss[.sss]]) and converts it to the corresponding value of
|
||||
* type &btime.
|
||||
*/
|
||||
btime
|
||||
tm_parse_time(const char *x)
|
||||
{
|
||||
struct tm tm = {};
|
||||
int usec, n1, n2, n3, r;
|
||||
|
||||
r = sscanf(x, "%d-%d-%d%n %d:%d:%d%n.%d%n",
|
||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &n1,
|
||||
&tm.tm_hour, &tm.tm_min, &tm.tm_sec, &n2,
|
||||
&usec, &n3);
|
||||
|
||||
if ((r == 3) && !x[n1])
|
||||
tm.tm_hour = tm.tm_min = tm.tm_sec = usec = 0;
|
||||
else if ((r == 6) && !x[n2])
|
||||
usec = 0;
|
||||
else if ((r == 7) && !x[n3])
|
||||
{
|
||||
/* Convert subsecond digits to proper precision */
|
||||
int digits = n3 - n2 - 1;
|
||||
if ((usec < 0) || (usec > 999999) || (digits < 1) || (digits > 6))
|
||||
return 0;
|
||||
|
||||
while (digits++ < 6)
|
||||
usec *= 10;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
|
||||
tm.tm_mon--;
|
||||
tm.tm_year -= 1900;
|
||||
s64 ts = mktime(&tm);
|
||||
if ((ts == (s64) (time_t) -1) || (ts < 0) || (ts > ((s64) 1 << 40)))
|
||||
return 0;
|
||||
|
||||
return ts S + usec;
|
||||
}
|
||||
|
||||
/**
|
||||
* tm_format_time - convert date and time to textual representation
|
||||
* @x: destination buffer of size %TM_DATETIME_BUFFER_SIZE
|
||||
* @fmt: specification of resulting textual representation of the time
|
||||
* @t: time
|
||||
*
|
||||
* This function formats the given relative time value @t to a textual
|
||||
* date/time representation (dd-mm-yyyy hh:mm:ss) in real time.
|
||||
*/
|
||||
void
|
||||
tm_format_time(char *x, struct timeformat *fmt, btime t)
|
||||
{
|
||||
btime dt = current_time() - t;
|
||||
btime rt = current_real_time() - dt;
|
||||
int v1 = !fmt->limit || (dt < fmt->limit);
|
||||
|
||||
if (!tm_format_real_time(x, TM_DATETIME_BUFFER_SIZE, v1 ? fmt->fmt1 : fmt->fmt2, rt))
|
||||
strcpy(x, "<error>");
|
||||
}
|
||||
|
||||
/* Replace %f in format string with usec scaled to requested precision */
|
||||
static int
|
||||
strfusec(char *buf, int size, const char *fmt, uint usec)
|
||||
{
|
||||
char *str = buf;
|
||||
int parity = 0;
|
||||
|
||||
while (*fmt)
|
||||
{
|
||||
if (!size)
|
||||
return 0;
|
||||
|
||||
if ((fmt[0] == '%') && (!parity) &&
|
||||
((fmt[1] == 'f') || (fmt[1] >= '1') && (fmt[1] <= '6') && (fmt[2] == 'f')))
|
||||
{
|
||||
int digits = (fmt[1] == 'f') ? 6 : (fmt[1] - '0');
|
||||
uint d = digits, u = usec;
|
||||
|
||||
/* Convert microseconds to requested precision */
|
||||
while (d++ < 6)
|
||||
u /= 10;
|
||||
|
||||
int num = bsnprintf(str, size, "%0*u", digits, u);
|
||||
if (num < 0)
|
||||
return 0;
|
||||
|
||||
fmt += (fmt[1] == 'f') ? 2 : 3;
|
||||
ADVANCE(str, size, num);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Handle '%%' expression */
|
||||
parity = (*fmt == '%') ? !parity : 0;
|
||||
*str++ = *fmt++;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
if (!size)
|
||||
return 0;
|
||||
|
||||
*str = 0;
|
||||
return str - buf;
|
||||
}
|
||||
|
||||
int
|
||||
tm_format_real_time(char *x, size_t max, const char *fmt, btime t)
|
||||
{
|
||||
s64 t1 = t TO_S;
|
||||
s64 t2 = t - t1 S;
|
||||
|
||||
time_t ts = t1;
|
||||
struct tm tm;
|
||||
if (!localtime_r(&ts, &tm))
|
||||
return 0;
|
||||
|
||||
byte tbuf[TM_DATETIME_BUFFER_SIZE];
|
||||
if (!strfusec(tbuf, max, fmt, t2))
|
||||
return 0;
|
||||
|
||||
if (!strftime(x, max, tbuf, &tm))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user