implement priority aware algorithm,

add dataset size printout
This commit is contained in:
2025-06-07 01:10:05 +00:00
parent ae3128f6e8
commit c208037ae9
2 changed files with 121 additions and 23 deletions

File diff suppressed because one or more lines are too long

View File

@@ -103,7 +103,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"id": "0dc1d6d4", "id": "0dc1d6d4",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -117,8 +117,8 @@
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", "[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"id mapping: \n", "id mapping: \n",
"[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n", "[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n",
"13312\n", "TCAM bits: 13312\n",
"110\n" "RAM bits: 110\n"
] ]
} }
], ],
@@ -204,6 +204,7 @@
"\tfor layer in layers:\n", "\tfor layer in layers:\n",
"\t\tnum_prefixes = 0\n", "\t\tnum_prefixes = 0\n",
"\t\tprefix_width = field_width[layer]\n", "\t\tprefix_width = field_width[layer]\n",
"\t\t# for each range in the layer, convert the ranges to prefixes using naive range expansion\n",
"\t\tfor r in layers[layer]:\n", "\t\tfor r in layers[layer]:\n",
"\t\t\tif r[\"min\"] == None:\n", "\t\t\tif r[\"min\"] == None:\n",
"\t\t\t\tr[\"min\"] = 0\n", "\t\t\t\tr[\"min\"] = 0\n",
@@ -250,7 +251,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 8,
"id": "48011528", "id": "48011528",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -264,8 +265,8 @@
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", "[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"id mapping: \n", "id mapping: \n",
"[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n", "[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n",
"3520\n", "TCAM bits: 3520\n",
"110\n" "RAM bits: 110\n"
] ]
} }
], ],
@@ -274,6 +275,111 @@
"print(f\"TCAM bits: {tcam_bits}\")\n", "print(f\"TCAM bits: {tcam_bits}\")\n",
"print(f\"RAM bits: {ram_bits}\")" "print(f\"RAM bits: {ram_bits}\")"
] ]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "64b7271e",
"metadata": {},
"outputs": [],
"source": [
"# for this technique, we note that given disjoint ranges [0,a][a,b],[b,c] ...\n",
"# then if using a TCAM that selects the first matching prefix, then [0,a],[0,b],[0,c] would be equivalent\n",
"# this is because if for some k<a, even though the range [0,b] could be selected, as long as the prefixes for [0,a] are before [0,b] then the correct prefix will still be selected\n",
"\n",
"def priority_aware(tree):\n",
"\trmt = []\n",
"\tstep = 0\n",
"\n",
"\ttcam_bits = 0\n",
"\tram_bits = 0\n",
"\n",
"\tfor layer in layers:\n",
"\t\tnum_prefixes = 0\n",
"\t\tprefix_width = field_width[layer]\n",
"\t\t# for each range, run the regular prefix expansion, and also the prefix expansion setting the minimum to 0\n",
"\t\t# then check which set of prefixes would be better\n",
"\t\t# we will assume the ranges are already disjoin and in the correct order\n",
"\t\tfor r in layers[layer]:\n",
"\t\t\tif r[\"min\"] == None:\n",
"\t\t\t\tr[\"min\"] = 0\n",
"\t\t\telif r[\"max\"] == None:\n",
"\t\t\t\tr[\"max\"] = 2 ** prefix_width\n",
"\t\t\tregular_prefixes = convert_range(r[\"min\"], r[\"max\"], prefix_width)\n",
"\t\t\tzero_start_prefixes = convert_range(0, r[\"max\"], prefix_width)\n",
"\n",
"\t\t\tif len(regular_prefixes) <= len(zero_start_prefixes):\n",
"\t\t\t\tpfx_type = \"exact\"\n",
"\t\t\t\tprefixes = regular_prefixes\n",
"\t\t\telse:\n",
"\t\t\t\tpfx_type = \"zero\"\n",
"\t\t\t\tprefixes = zero_start_prefixes\n",
"\n",
"\t\t\tr[\"prefixes\"] = prefixes\n",
"\t\t\tr[\"prefix_type\"] = pfx_type\n",
"\t\t\tnum_prefixes += len(prefixes)\n",
"\t\t\ttcam_bits += len(prefixes) * prefix_width\n",
"\n",
"\t\ttcam = {\n",
"\t\t\t\"id\": f\"{layer}_range\",\n",
"\t\t\t\"step\": step,\n",
"\t\t\t\"match\": \"ternary\",\n",
"\t\t\t\"entries\": num_prefixes,\n",
"\t\t\t\"key_size\": prefix_width,\n",
"\t\t\t\"ranges\": layers[layer]\n",
"\t\t}\n",
"\n",
"\t\tnum_ranges = len(layers[layer])\n",
"\t\t# assume no pointer reuse for metadata storage\n",
"\t\tram = {\n",
"\t\t\t\"id\": f\"{layer}_meta\",\n",
"\t\t\t\"step\": step,\n",
"\t\t\t\"match\": \"exact\",\n",
"\t\t\t\"method\": \"index\",\n",
"\t\t\t\"key_size\": math.ceil(math.log2(num_ranges)),\n",
"\t\t\t\"data_size\": len(classes)\n",
"\t\t}\n",
"\t\tram_bits += math.ceil(math.log2(num_ranges)) * len(classes)\n",
"\n",
"\t\trmt.append(tcam)\n",
"\t\trmt.append(ram)\n",
"\n",
"\t\tstep += 1\n",
"\n",
"\treturn rmt, tcam_bits, ram_bits\n",
"\n",
"x, tcam_bits, ram_bits = priority_aware(tree)\n",
"f = open(\"priority_aware.json\", \"w+\")\n",
"f.write(json.dumps(x, indent=4))\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "cd706e41",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TCAM mapping: \n",
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"SRAM mapping: \n",
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"id mapping: \n",
"[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n",
"TCAM bits: 2120\n",
"RAM bits: 110\n"
]
}
],
"source": [
"! command python3 ideal-rmt-simulator/sim.py priority_aware.json\n",
"print(f\"TCAM bits: {tcam_bits}\")\n",
"print(f\"RAM bits: {ram_bits}\")"
]
} }
], ],
"metadata": { "metadata": {