EncodingScheme.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "benc/Dict.h"
  17. #include "memory/Allocator.h"
  18. #include "switch/EncodingScheme.h"
  19. #include "util/Bits.h"
  20. #include "util/Hex.h"
  21. int EncodingScheme_getFormNum(struct EncodingScheme* scheme, uint64_t routeLabel)
  22. {
  23. if (scheme->count == 1) {
  24. return 0;
  25. }
  26. for (int i = 0; i < scheme->count; i++) {
  27. struct EncodingScheme_Form* form = &scheme->forms[i];
  28. Assert_true(form->prefixLen > 0 && form->prefixLen < 32);
  29. Assert_true(form->bitCount > 0 && form->bitCount < 32);
  30. if (0 == ((form->prefix ^ (uint32_t)routeLabel) << (32 - form->prefixLen))) {
  31. return i;
  32. }
  33. }
  34. return EncodingScheme_getFormNum_INVALID;
  35. }
  36. uint64_t EncodingScheme_convertLabel(struct EncodingScheme* scheme,
  37. uint64_t routeLabel,
  38. int convertTo)
  39. {
  40. int formNum = EncodingScheme_getFormNum(scheme, routeLabel);
  41. if (formNum == EncodingScheme_getFormNum_INVALID) {
  42. return EncodingScheme_convertLabel_INVALID;
  43. }
  44. struct EncodingScheme_Form* currentForm = &scheme->forms[formNum];
  45. if (scheme->count == 1
  46. || (routeLabel & Bits_maxBits64(currentForm->prefixLen + currentForm->bitCount)) == 1)
  47. {
  48. // fixed width encoding or it's a self label, this is easy
  49. switch (convertTo) {
  50. case 0:
  51. case EncodingScheme_convertLabel_convertTo_CANNONICAL: return routeLabel;
  52. default: return EncodingScheme_convertLabel_INVALID;
  53. }
  54. }
  55. routeLabel >>= currentForm->prefixLen;
  56. uint64_t director = routeLabel & Bits_maxBits64(currentForm->bitCount);
  57. routeLabel >>= currentForm->bitCount;
  58. // ACKTUNG: Magic afoot!
  59. // Conversions are necessary for two reasons.
  60. // #1 ensure 0001 always references interface 1, the self interface.
  61. // #2 reuse interface the binary encoding for interface 1 in other EncodingForms
  62. // because interface 1 cannot be expressed as anything other than 0001
  63. if ((currentForm->prefix & Bits_maxBits64(currentForm->prefixLen)) == 1) {
  64. // Swap 0 and 1 if the prefix is 1, this makes 0001 alias to 1
  65. // because 0 can never show up in the wild, we reuse it for 1.
  66. Assert_true(director != 0);
  67. if (director == 1) { director--; }
  68. } else if (director) {
  69. // Reuse the number 1 for 2 and 2 for 3 etc. to gain an extra slot in all other encodings.
  70. director++;
  71. }
  72. if (convertTo == EncodingScheme_convertLabel_convertTo_CANNONICAL) {
  73. // Take into account the fact that if the destination form does not have a 1 prefix,
  74. // an extra number will be available.
  75. int minBitsA = Bits_log2x64(director) + 1;
  76. int minBitsB = Bits_log2x64(director-1) + 1;
  77. for (int i = 0; i < scheme->count; i++) {
  78. struct EncodingScheme_Form* form = &scheme->forms[i];
  79. int minBits = ((form->prefix & Bits_maxBits64(form->prefixLen)) == 1)
  80. ? minBitsA : minBitsB;
  81. if (form->bitCount >= minBits) {
  82. convertTo = i;
  83. break;
  84. }
  85. }
  86. }
  87. if (convertTo < 0 || convertTo >= scheme->count) {
  88. // convertTo value is insane
  89. return EncodingScheme_convertLabel_INVALID;
  90. }
  91. struct EncodingScheme_Form* nextForm = &scheme->forms[convertTo];
  92. if ((nextForm->prefix & Bits_maxBits64(nextForm->prefixLen)) == 1) {
  93. // Swap 1 and 0 back if necessary.
  94. if (director == 0) { director++; }
  95. } else if (director) {
  96. // Or move the numbers down by one.
  97. director--;
  98. }
  99. if ((Bits_log2x64(director) + 1) > nextForm->bitCount) {
  100. // won't fit in requested form
  101. return EncodingScheme_convertLabel_INVALID;
  102. }
  103. if (Bits_log2x64(routeLabel) + EncodingScheme_formSize(nextForm) > 59) {
  104. return EncodingScheme_convertLabel_INVALID;
  105. }
  106. routeLabel <<= nextForm->bitCount;
  107. routeLabel |= director;
  108. routeLabel <<= nextForm->prefixLen;
  109. routeLabel |= nextForm->prefix;
  110. if ((routeLabel & Bits_maxBits64(nextForm->prefixLen + nextForm->bitCount)) == 1) {
  111. // looks like a self-route
  112. return EncodingScheme_convertLabel_INVALID;
  113. }
  114. return routeLabel;
  115. }
  116. /**
  117. * Decode a form from it's binary representation.
  118. * Can only use a maximum of 41 bits.
  119. *
  120. * @param out the output which will be populated with the encoding form data.
  121. * @param data the binary data in host order.
  122. * @return the number of bits of data which were consumed by the decoding.
  123. * If the content is definitely not an encoding form, 0 is returned.
  124. */
  125. static inline int decodeForm(struct EncodingScheme_Form* out, uint64_t d)
  126. {
  127. out->prefixLen = d & Bits_maxBits64(5);
  128. d >>= 5;
  129. int bitCount = d & Bits_maxBits64(5);
  130. if (bitCount < 1) {
  131. return 0;
  132. }
  133. out->bitCount = bitCount;
  134. d >>= 5;
  135. out->prefix = d & Bits_maxBits64(out->prefixLen);
  136. return 5 + 5 + out->prefixLen;
  137. }
  138. static inline int encodeForm(struct EncodingScheme_Form* in, uint64_t* data, int bits)
  139. {
  140. *data |= ((uint64_t)in->prefixLen & Bits_maxBits64(5)) << bits;
  141. bits += 5;
  142. *data |= ((uint64_t)in->bitCount & Bits_maxBits64(5)) << bits;
  143. bits += 5;
  144. *data |= ((uint64_t)in->prefix & Bits_maxBits64(in->prefixLen)) << bits;
  145. return 5 + 5 + in->prefixLen;
  146. }
  147. bool EncodingScheme_isSane(struct EncodingScheme* scheme)
  148. {
  149. // Check for obviously insane encoding.
  150. if (scheme->count == 0) {
  151. // No encoding schemes
  152. return false;
  153. }
  154. if (scheme->count > 31) {
  155. // impossible, each form must have a different bitCount and bitCount
  156. // can only be expressed in 5 bits limiting it to 31 bits max and a form
  157. // using zero bits is not allowed so there are only 31 max possibilities.
  158. return false;
  159. }
  160. if (scheme->count == 1) {
  161. // Fixed width encoding, prefix is not allowed and bitcount must be non-zero
  162. if (scheme->forms[0].prefixLen != 0 || scheme->forms[0].prefix != 0) {
  163. // prefixLen must be 0
  164. return false;
  165. }
  166. if (scheme->forms[0].bitCount == 0 || scheme->forms[0].bitCount > 31) {
  167. // bitcount must be non-zero and can't overflow the number
  168. return false;
  169. }
  170. return true;
  171. }
  172. // Variable width encoding.
  173. for (int i = 0; i < scheme->count; i++) {
  174. struct EncodingScheme_Form* form = &scheme->forms[i];
  175. if (form->prefixLen == 0 || form->prefixLen > 31) {
  176. // Prefix must exist in order to distinguish between forms
  177. return false;
  178. }
  179. if (form->bitCount == 0 || form->bitCount > 31) {
  180. // Bitcount must be non-zero
  181. return false;
  182. }
  183. if (EncodingScheme_formSize(form) > 59) {
  184. // cannot be represented in the usable space in a label
  185. return false;
  186. }
  187. if (i > 0 && form->bitCount <= scheme->forms[i-1].bitCount) {
  188. // Forms must be in ascending order.
  189. return false;
  190. }
  191. for (int j = 0; j < scheme->count; j++) {
  192. // Forms must be distinguishable by their prefixes.
  193. if (j != i
  194. && (scheme->forms[j].prefix & Bits_maxBits64(form->prefixLen)) == form->prefix)
  195. {
  196. return false;
  197. }
  198. }
  199. }
  200. return true;
  201. }
  202. List* EncodingScheme_asList(struct EncodingScheme* list, struct Allocator* alloc)
  203. {
  204. Assert_ifParanoid(EncodingScheme_isSane(list));
  205. String* prefixLen = String_new("prefixLen", alloc);
  206. String* bitCount = String_new("bitCount", alloc);
  207. String* prefix = String_new("prefix", alloc);
  208. List* scheme = NULL;
  209. for (int i = 0; i < (int)list->count; i++) {
  210. Dict* form = Dict_new(alloc);
  211. Dict_putInt(form, prefixLen, list->forms[i].prefixLen, alloc);
  212. Dict_putInt(form, bitCount, list->forms[i].bitCount, alloc);
  213. String* pfx = String_newBinary(NULL, 8, alloc);
  214. uint32_t prefix_be = Endian_hostToBigEndian32(list->forms[i].prefix);
  215. Hex_encode(pfx->bytes, 8, (uint8_t*)&prefix_be, 4);
  216. Dict_putString(form, prefix, pfx, alloc);
  217. scheme = List_addDict(scheme, form, alloc);
  218. }
  219. return scheme;
  220. }
  221. struct EncodingScheme* EncodingScheme_fromList(List* scheme, struct Allocator* alloc)
  222. {
  223. struct EncodingScheme* list = Allocator_malloc(alloc, sizeof(struct EncodingScheme));
  224. list->count = List_size(scheme);
  225. list->forms = Allocator_malloc(alloc, sizeof(struct EncodingScheme_Form) * list->count);
  226. for (int i = 0; i < (int)list->count; i++) {
  227. Dict* form = List_getDict(scheme, i);
  228. uint64_t* prefixLen = Dict_getInt(form, String_CONST("prefixLen"));
  229. uint64_t* bitCount = Dict_getInt(form, String_CONST("bitCount"));
  230. String* prefixStr = Dict_getString(form, String_CONST("prefix"));
  231. if (!prefixLen || !bitCount || !prefixStr || prefixStr->len != 8) {
  232. return NULL;
  233. }
  234. uint32_t prefix_be;
  235. if (Hex_decode((uint8_t*)&prefix_be, 4, prefixStr->bytes, 8) != 4) {
  236. return NULL;
  237. }
  238. list->forms[i].prefixLen = *prefixLen;
  239. list->forms[i].bitCount = *bitCount;
  240. list->forms[i].prefix = Endian_bigEndianToHost32(prefix_be);
  241. }
  242. if (!EncodingScheme_isSane(list)) {
  243. return NULL;
  244. }
  245. return list;
  246. }
  247. String* EncodingScheme_serialize(struct EncodingScheme* list,
  248. struct Allocator* alloc)
  249. {
  250. Assert_ifParanoid(EncodingScheme_isSane(list));
  251. // Create the string as the largest that is possible for the list size.
  252. String* out = String_newBinary(NULL, list->count * 6, alloc);
  253. int bits = 0;
  254. int outIndex = 0;
  255. uint64_t block = 0;
  256. for (int listIndex = 0; listIndex < (int)list->count; listIndex++) {
  257. bits += encodeForm(&list->forms[listIndex], &block, bits);
  258. while (bits > 8) {
  259. Assert_true(outIndex < (int)out->len);
  260. out->bytes[outIndex++] = (uint8_t) (block & 0xff);
  261. bits -= 8;
  262. block >>= 8;
  263. }
  264. }
  265. if (bits > 0) {
  266. out->bytes[outIndex++] = (uint8_t) (block & 0xff);
  267. }
  268. out->len = outIndex;
  269. return out;
  270. }
  271. struct EncodingScheme* EncodingScheme_deserialize(String* data,
  272. struct Allocator* alloc)
  273. {
  274. struct EncodingScheme_Form* forms = NULL;
  275. int outCount = 0;
  276. uint64_t block = 0;
  277. int bits = 0;
  278. int dataIndex = 0;
  279. for (;;) {
  280. // load data into the block from the incoming data source
  281. while (bits < 56 && dataIndex < (int)data->len) {
  282. block |= (((uint64_t)data->bytes[dataIndex++] & 0xff) << bits);
  283. bits += 8;
  284. }
  285. struct EncodingScheme_Form next;
  286. int ret = decodeForm(&next, block);
  287. bits -= ret;
  288. if (!ret || bits < 0) {
  289. if (block || dataIndex < (int)data->len || bits < 0) {
  290. // Invalid encoding
  291. return NULL;
  292. }
  293. break;
  294. }
  295. block >>= ret;
  296. Assert_true((next.prefix >> next.prefixLen) == 0);
  297. outCount += 1;
  298. forms = Allocator_realloc(alloc, forms, outCount * sizeof(struct EncodingScheme_Form));
  299. Bits_memcpyConst(&forms[outCount-1], &next, sizeof(struct EncodingScheme_Form));
  300. }
  301. struct EncodingScheme* out = Allocator_clone(alloc, (&(struct EncodingScheme) {
  302. .forms = forms,
  303. .count = outCount
  304. }));
  305. return EncodingScheme_isSane(out) ? out : NULL;
  306. }
  307. struct EncodingScheme* EncodingScheme_defineFixedWidthScheme(int bitCount, struct Allocator* alloc)
  308. {
  309. struct NumberCompress_FixedWidthScheme
  310. {
  311. struct EncodingScheme scheme;
  312. struct EncodingScheme_Form form;
  313. };
  314. struct NumberCompress_FixedWidthScheme* out =
  315. Allocator_malloc(alloc, sizeof(struct NumberCompress_FixedWidthScheme));
  316. struct NumberCompress_FixedWidthScheme scheme = {
  317. .scheme = { .count = 1, .forms = &out->form },
  318. .form = { .bitCount = bitCount, .prefixLen = 0, .prefix = 0, },
  319. };
  320. Bits_memcpyConst(out, &scheme, sizeof(struct NumberCompress_FixedWidthScheme));
  321. Assert_true(EncodingScheme_isSane(&out->scheme));
  322. return &out->scheme;
  323. }
  324. struct EncodingScheme* EncodingScheme_defineDynWidthScheme(struct EncodingScheme_Form* forms,
  325. int formCount,
  326. struct Allocator* alloc)
  327. {
  328. struct EncodingScheme_Form* formsCopy =
  329. Allocator_malloc(alloc, sizeof(struct EncodingScheme_Form) * formCount);
  330. Bits_memcpy(formsCopy, forms, sizeof(struct EncodingScheme_Form) * formCount);
  331. struct EncodingScheme* scheme = Allocator_clone(alloc, (&(struct EncodingScheme) {
  332. .count = formCount,
  333. .forms = formsCopy
  334. }));
  335. Assert_ifParanoid(EncodingScheme_isSane(scheme));
  336. return scheme;
  337. }
  338. int EncodingScheme_compare(struct EncodingScheme* a, struct EncodingScheme* b)
  339. {
  340. if (a->count == b->count) {
  341. return Bits_memcmp(a->forms, b->forms, sizeof(struct EncodingScheme_Form) * a->count);
  342. }
  343. return a->count > b->count ? 1 : -1;
  344. }
  345. /**
  346. * Return true if the route is to the switch's router interface.
  347. */
  348. int EncodingScheme_isSelfRoute(struct EncodingScheme* scheme, uint64_t routeLabel)
  349. {
  350. int formNum = EncodingScheme_getFormNum(scheme, routeLabel);
  351. if (formNum == EncodingScheme_getFormNum_INVALID) {
  352. return 0;
  353. }
  354. struct EncodingScheme_Form* currentForm = &scheme->forms[formNum];
  355. return (routeLabel & Bits_maxBits64(currentForm->prefixLen + currentForm->bitCount)) == 1;
  356. }