Partially implement sorting
[flowspec-xdp] / genrules.py
1 #!/usr/bin/env python3
2
3 import sys
4 import ipaddress
5 from enum import Enum
6 import argparse
7 import math
8
9
10 IP_PROTO_ICMP = 1
11 IP_PROTO_ICMPV6 = 58
12 IP_PROTO_TCP = 6
13 IP_PROTO_UDP = 17
14
15 ORD_LESS = 0
16 ORD_GREATER = 1
17 ORD_EQUAL = 2
18
19 class ASTAction(Enum):
20     OR = 1
21     AND = 2
22     NOT = 3
23     FALSE = 4
24     TRUE = 5
25     EXPR = 6
26 class ASTNode:
27     def __init__(self, action, left, right=None):
28         self.action = action
29         if action == ASTAction.FALSE or action == ASTAction.TRUE:
30             assert left is None and right is None
31             return
32         self.left = left
33         if right is None:
34             assert action == ASTAction.EXPR or action == ASTAction.NOT
35         else:
36             self.right = right
37
38     def write(self, expr_param, expr_param2=None):
39         if self.action == ASTAction.OR:
40             return "(" + self.left.write(expr_param, expr_param2) + ") || (" + self.right.write(expr_param, expr_param2) + ")"
41         if self.action == ASTAction.AND:
42             return "(" + self.left.write(expr_param, expr_param2) + ") && (" + self.right.write(expr_param, expr_param2) + ")"
43         if self.action == ASTAction.NOT:
44             return "!(" + self.left.write(expr_param, expr_param2) + ")"
45         if self.action == ASTAction.FALSE:
46             return "0"
47         if self.action == ASTAction.TRUE:
48             return "1"
49         if self.action == ASTAction.EXPR:
50             return self.left.write(expr_param, expr_param2)
51
52 def parse_ast(expr, parse_expr, comma_is_or):
53     expr = expr.strip()
54
55     comma_split = expr.split(",", 1)
56     or_split = expr.split("||", 1)
57     if len(comma_split) > 1 and not "||" in comma_split[0]:
58         # Confusingly, BIRD uses `,` as either || or &&, depending on the type
59         # of expression being parsed. Specifically, a `numbers-match` uses `,`
60         # as OR, whereas a `bitmask-match` uses `,` as AND.
61         if comma_is_or:
62             return ASTNode(ASTAction.OR, parse_ast(comma_split[0], parse_expr, comma_is_or), parse_ast(comma_split[1], parse_expr, comma_is_or))
63         else:
64             return ASTNode(ASTAction.AND, parse_ast(comma_split[0], parse_expr, comma_is_or), parse_ast(comma_split[1], parse_expr, comma_is_or))
65     if len(or_split) > 1:
66         assert not "," in or_split[0]
67         return ASTNode(ASTAction.OR, parse_ast(or_split[0], parse_expr, comma_is_or), parse_ast(or_split[1], parse_expr, comma_is_or))
68
69     and_split = expr.split("&&", 1)
70     if len(and_split) > 1:
71         return ASTNode(ASTAction.AND, parse_ast(and_split[0], parse_expr, comma_is_or), parse_ast(and_split[1], parse_expr, comma_is_or))
72
73     if expr.strip() == "true":
74         return ASTNode(ASTAction.TRUE, None)
75     if expr.strip() == "false":
76         return ASTNode(ASTAction.FALSE, None)
77
78     if expr.startswith("!"):
79         return ASTNode(ASTAction.NOT, parse_ast(expr[1:], parse_expr, comma_is_or))
80
81     return parse_expr(expr)
82
83
84 class NumbersAction(Enum):
85     EQ = "=="
86     GT = ">"
87     GTOE = ">="
88     LT = "<"
89     LTOE = "<="
90 class NumbersExpr:
91     def __init__(self, action, val):
92         self.action = action
93         self.val = val
94
95     def write(self, param, param2):
96         if param2 is not None:
97             return "(" + param + self.action.value + self.val + ") || (" + param2 + self.action.value + self.val + ")"
98         return param + self.action.value + self.val
99
100 def parse_numbers_expr(expr):
101     space_split = expr.split(" ")
102     if expr.startswith(">="):
103         assert len(space_split) == 2
104         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GTOE, space_split[1]))
105     if expr.startswith(">"):
106         assert len(space_split) == 2
107         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GT, space_split[1]))
108     if expr.startswith("<="):
109         assert len(space_split) == 2
110         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LTOE, space_split[1]))
111     if expr.startswith("<"):
112         assert len(space_split) == 2
113         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LT, space_split[1]))
114     if ".." in expr:
115         rangesplit = expr.split("..")
116         assert len(rangesplit) == 2
117         #XXX: Are ranges really inclusive,inclusive?
118         left = ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GTOE, rangesplit[0]))
119         right = ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LTOE, rangesplit[1]))
120         return ASTNode(ASTAction.AND, left, right)
121     
122     if expr.startswith("= "):
123         expr = expr[2:]
124     return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.EQ, expr))
125
126 class FragExpr(Enum):
127     IF = 1
128     FF = 2
129     DF = 3
130     LF = 4
131
132     def write(self, ipproto, _param2):
133         if ipproto == 4:
134             if self == FragExpr.IF:
135                 return "(ip->frag_off & BE16(IP_MF|IP_OFFSET)) != 0"
136             elif self == FragExpr.FF:
137                 return "((ip->frag_off & BE16(IP_MF)) != 0 && (ip->frag_off & BE16(IP_OFFSET)) == 0)"
138             elif self == FragExpr.DF:
139                 return "(ip->frag_off & BE16(IP_DF)) != 0"
140             elif self == FragExpr.LF:
141                 return "((ip->frag_off & BE16(IP_MF)) == 0 && (ip->frag_off & BE16(IP_OFFSET)) != 0)"
142             else:
143                 assert False
144         else:
145             if self == FragExpr.IF:
146                 return "frag6 != NULL"
147             elif self == FragExpr.FF:
148                 return "(frag6 != NULL && (frag6->frag_off & BE16(IP6_MF)) != 0 && (frag6->frag_off & BE16(IP6_FRAGOFF)) == 0)"
149             elif self == FragExpr.DF:
150                 assert False # No such thing in v6
151             elif self == FragExpr.LF:
152                 return "(frag6 != NULL && (frag6->frag_off & BE16(IP6_MF)) == 0 && (frag6->frag_off & BE16(IP6_FRAGOFF)) != 0)"
153             else:
154                 assert False
155
156 def parse_frag_expr(expr):
157     if expr == "is_fragment":
158         return ASTNode(ASTAction.EXPR, FragExpr.IF)
159     elif expr == "first_fragment":
160         return ASTNode(ASTAction.EXPR, FragExpr.FF)
161     elif expr == "dont_fragment":
162         return ASTNode(ASTAction.EXPR, FragExpr.DF)
163     elif expr == "last_fragment":
164         return ASTNode(ASTAction.EXPR, FragExpr.LF)
165     else:
166         assert False
167
168 class BitExpr:
169     def __init__(self, val):
170         s = val.split("/")
171         assert len(s) == 2
172         self.match = s[0]
173         self.mask = s[1]
174
175     def write(self, param, _param2):
176         return f"({param} & {self.mask}) == {self.match}"
177
178 def parse_bit_expr(expr):
179     return ASTNode(ASTAction.EXPR, BitExpr(expr))
180
181
182 class IpRule:
183     def __init__(self, ty, offset, net, proto):
184         self.ty = ty
185         self.offset = offset
186         if offset is None:
187             self.offset = 0
188         self.net = net
189         self.proto = proto
190
191     def ord(self, other):
192         assert self.ty == other.ty
193         assert self.proto == other.proto
194         if self.offset < other.offset:
195             return ORD_LESS
196         if self.offset > other.offset:
197             return ORD_GREATER
198
199         if self.net.overlaps(other.net):
200             if self.net.prefixlen > other.net.prefixlen:
201                 return ORD_LESS
202             elif self.net.prefixlen < other.net.prefixlen:
203                 return ORD_GREATER
204         else:
205             if self.net < other.net:
206                 return ORD_LESS
207             else:
208                 assert self.net > other.net
209                 return ORD_GREATER
210
211         return ORD_EQUAL
212
213     def __lt__(self, other):
214         return self.ord(other) == ORD_LESS
215
216     def __eq__(self, other):
217         return type(other) == IpRule and self.ty == other.ty and self.offset == other.offset and self.net == other.net and self.proto == other.proto
218
219     def __str__(self):
220         if self.proto == 4:
221             assert self.offset == 0
222             return f"""if ((ip->{self.ty} & MASK4({self.net.prefixlen})) != BIGEND32({int(self.net.network_address)}ULL))
223         break;"""
224         else:
225             u32s = [(int(self.net.network_address) >> (3*32)) & 0xffffffff,
226                     (int(self.net.network_address) >> (2*32)) & 0xffffffff,
227                     (int(self.net.network_address) >> (1*32)) & 0xffffffff,
228                     (int(self.net.network_address) >> (0*32)) & 0xffffffff]
229             if self.offset == 0:
230                 mask = f"MASK6({self.net.prefixlen})"
231             else:
232                 mask = f"MASK6_OFFS({self.offset}, {self.net.prefixlen})"
233             return f"""if ((ip6->{self.ty} & {mask}) != (BIGEND128({u32s[0]}ULL, {u32s[1]}ULL, {u32s[2]}ULL, {u32s[3]}ULL) & {mask}))
234         break;"""
235 def ip_to_rule(proto, inip, ty, offset):
236     if proto == 4:
237         assert offset is None
238         net = ipaddress.IPv4Network(inip.strip())
239         return IpRule(ty, offset, net, 4)
240     else:
241         net = ipaddress.IPv6Network(inip.strip())
242         return IpRule(ty, offset, net, 6)
243
244 def fragment_to_rule(ipproto, rules):
245     ast = parse_ast(rules, parse_frag_expr, False)
246     return "if (!( " + ast.write(ipproto) + " )) break;"
247
248 def len_to_rule(rules):
249     ast = parse_ast(rules, parse_numbers_expr, True)
250     return "if (!( " + ast.write("(data_end - pktdata)") + " )) break;"
251  
252 def proto_to_rule(ipproto, proto):
253     ast = parse_ast(proto, parse_numbers_expr, True)
254
255     if ipproto == 4:
256         return "if (!( " + ast.write("ip->protocol") + " )) break;"
257     else:
258         return "if (!( " + ast.write("ip6->nexthdr") + " )) break;"
259
260 def icmp_type_to_rule(proto, ty):
261     ast = parse_ast(ty, parse_numbers_expr, True)
262     if proto == 4:
263         return "if (icmp == NULL) break;\nif (!( " + ast.write("icmp->type") + " )) break;"
264     else:
265         return "if (icmpv6 == NULL) break;\nif (!( " + ast.write("icmpv6->icmp6_type") + " )) break;"
266
267 def icmp_code_to_rule(proto, code):
268     ast = parse_ast(code, parse_numbers_expr, True)
269     if proto == 4:
270         return "if (icmp == NULL) break;\nif (!( " + ast.write("icmp->code") + " )) break;"
271     else:
272         return "if (icmpv6 == NULL) break;\nif (!( " + ast.write("icmpv6->icmp6_code") + " )) break;"
273
274 def dscp_to_rule(proto, rules):
275     ast = parse_ast(rules, parse_numbers_expr, True)
276
277     if proto == 4:
278         return "if (!( " + ast.write("((ip->tos & 0xfc) >> 2)") + " )) break;"
279     else:
280         return "if (!( " + ast.write("((ip6->priority << 2) | ((ip6->flow_lbl[0] & 0xc0) >> 6))") + " )) break;"
281
282 def port_to_rule(ty, rules):
283     if ty == "port" :
284         ast = parse_ast(rules, parse_numbers_expr, True)
285         return "if (sport == -1 || dport == -1) break;\nif (!( " + ast.write("sport", "dport") + " )) break;"
286
287     ast = parse_ast(rules, parse_numbers_expr, True)
288     return "if (" + ty + " == -1) break;\nif (!( " + ast.write(ty) + " )) break;"
289
290 def tcp_flags_to_rule(rules):
291     ast = parse_ast(rules, parse_bit_expr, False)
292
293     return f"""if (tcp == NULL) break;
294 if (!( {ast.write("(ntohs(tcp->flags) & 0xfff)")} )) break;"""
295
296 def flow_label_to_rule(rules):
297     ast = parse_ast(rules, parse_bit_expr, False)
298
299     return f"""if (ip6 == NULL) break;
300 if (!( {ast.write("((((uint32_t)(ip6->flow_lbl[0] & 0xf)) << 2*8) | (((uint32_t)ip6->flow_lbl[1]) << 1*8) | (uint32_t)ip6->flow_lbl[0])")} )) break;"""
301
302 class RuleAction(Enum):
303     CONDITIONS = 1
304     ACTION = 2
305     LIST = 3
306 class RuleNode:
307     def __init__(self, ty, action, inner):
308         self.ty = ty
309         self.action = action
310         self.inner = inner
311         if ty == RuleAction.ACTION:
312             assert inner is None
313             assert type(action) == str
314         elif ty == RuleAction.LIST:
315             assert type(inner) == list
316             assert action is None
317             for item in inner:
318                 assert type(item) == RuleNode
319         else:
320             assert ty == RuleAction.CONDITIONS
321             assert type(action) == list
322             assert type(inner) == RuleNode
323
324     def __lt__(self, other):
325         assert self.ty == RuleAction.CONDITIONS
326         assert other.ty == RuleAction.CONDITIONS
327
328         o = ORD_EQUAL
329
330         # RFC first has us sort by dest, then source, then other conditions. We don't implement the
331         # other conditions because doing so requires re-implementing the Flowspec wire format,
332         # which isn't trivial. However, we do implement the source/dest sorting in the hopes it
333         # allows us to group rules according to source/dest IP and hopefully LLVM optimizes out
334         # later rules.
335
336         selfdest = next(filter(lambda a : type(a) == IpRule and a.ty == "daddr", self.action), None)
337         otherdest = next(filter(lambda a : type(a) == IpRule and a.ty == "daddr", self.action), None)
338         if o == ORD_EQUAL and selfdest is not None and otherdest is not None:
339             o = selfdest.ord(otherdest)
340
341         if o == ORD_EQUAL:
342             selfsrc = next(filter(lambda a : type(a) == IpRule and a.ty == "saddr", self.action), None)
343             othersrc = next(filter(lambda a : type(a) == IpRule and a.ty == "saddr", self.action), None)
344             if selfsrc is not None and othersrc is None:
345                 return True
346             elif selfsrc is None and othersrc is not None:
347                 return False
348             elif selfsrc is not None and othersrc is not None:
349                 o = selfsrc.ord(othersrc)
350
351         if o == ORD_LESS:
352             return True
353         return self.action < other.action
354
355     def maybe_join(self, neighbor):
356         if self.ty == RuleAction.CONDITIONS and neighbor.ty == RuleAction.CONDITIONS:
357             overlapping_conditions = [x for x in self.action if x in neighbor.action]
358             if len(overlapping_conditions) != 0:
359                 us = RuleNode(RuleAction.CONDITIONS, [x for x in self.action if x not in overlapping_conditions], self.inner)
360                 them = RuleNode(RuleAction.CONDITIONS, [x for x in neighbor.action if x not in overlapping_conditions], neighbor.inner)
361                 self.action = overlapping_conditions
362                 if self.inner.ty == RuleAction.LIST and us.action == []:
363                     self.inner.inner.append(them)
364                 else:
365                     self.inner = RuleNode(RuleAction.LIST, None, [us, them])
366                 self.inner.flatten()
367                 return True
368         return False
369
370     def flatten(self):
371         # LLVM can be pretty bad at optimizing out common subexpressions. Thus, we have to do a
372         # pass here to toptimize out common subexpressions in back-to-back rules.
373         # See https://bugs.llvm.org/show_bug.cgi?id=52455
374         assert self.ty == RuleAction.LIST
375         did_update = True
376         while did_update:
377             did_update = False
378             for i in range(0, len(self.inner) - 1):
379                 if self.inner[i].maybe_join(self.inner[i + 1]):
380                     del self.inner[i + 1]
381                     did_update = True
382                     break
383
384     def write(self, out, pfx="\t"):
385         if self.ty == RuleAction.CONDITIONS:
386             out.write(pfx + "do {\\\n")
387             for cond in self.action:
388                 out.write("\t" + pfx + str(cond).replace("\n", " \\\n\t" + pfx) + " \\\n")
389             self.inner.write(out, pfx)
390             out.write(pfx + "} while(0);\\\n")
391         elif self.ty == RuleAction.LIST:
392             for item in self.inner:
393                 item.write(out, pfx + "\t")
394         else:
395             out.write("\t" + pfx + self.action.strip().replace("\n", " \\\n\t" + pfx) + " \\\n")
396
397 with open("rules.h", "w") as out:
398     parse = argparse.ArgumentParser()
399     parse.add_argument("--ihl", dest="ihl", required=True, choices=["drop-options","accept-options","parse-options"])
400     parse.add_argument("--v6frag", dest="v6frag", required=True, choices=["drop-frags","ignore","parse-frags","ignore-parse-if-rule"])
401     parse.add_argument("--8021q", dest="vlan", required=True, choices=["drop-vlan","accept-vlan","parse-vlan"])
402     parse.add_argument("--require-8021q", dest="vlan_tag")
403     args = parse.parse_args(sys.argv[1:])
404
405     if args.ihl == "drop-options":
406         out.write("#define PARSE_IHL XDP_DROP\n")
407     elif args.ihl == "accept-options":
408         out.write("#define PARSE_IHL XDP_PASS\n")
409     elif args.ihl == "parse-options":
410         out.write("#define PARSE_IHL PARSE\n")
411
412     if args.v6frag == "drop-frags":
413         out.write("#define PARSE_V6_FRAG XDP_DROP\n")
414     elif args.v6frag == "ignore":
415         pass
416     elif args.v6frag == "parse-frags":
417         out.write("#define PARSE_V6_FRAG PARSE\n")
418
419     if args.vlan == "drop-vlan":
420         out.write("#define PARSE_8021Q XDP_DROP\n")
421     elif args.vlan == "accept-vlan":
422         out.write("#define PARSE_8021Q XDP_PASS\n")
423     elif args.vlan == "parse-vlan":
424         out.write("#define PARSE_8021Q PARSE\n")
425
426     if args.vlan_tag is not None:
427         if args.vlan != "parse-vlan":
428             assert False
429         out.write("#define REQ_8021Q " + args.vlan_tag + "\n")
430
431     rules6 = []
432     rules4 = []
433     use_v6_frags = False
434     stats_rulecnt = 0
435     ratelimitcnt = 0
436     v4persrcratelimits = []
437     v5persrcratelimits = []
438     v6persrcratelimits = []
439
440     lastrule = None
441     for line in sys.stdin.readlines():
442         if "{" in line:
443             if lastrule is not None:
444                 print("Skipped rule due to lack of understood community tag: " + lastrule)
445             lastrule = line
446             continue
447         if "BGP.ext_community: " in line:
448             assert lastrule is not None
449
450             t = lastrule.split("{")
451             if t[0].strip() == "flow4":
452                 proto = 4
453             elif t[0].strip() == "flow6":
454                 proto = 6
455             else:
456                 continue
457
458             conditions = []
459             def write_rule(r):
460                 global conditions
461                 conditions.append(r)
462
463             rule = t[1].split("}")[0].strip()
464             for step in rule.split(";"):
465                 if step.strip().startswith("src") or step.strip().startswith("dst"):
466                     nets = step.strip()[3:].strip().split(" ")
467                     if len(nets) > 1:
468                         assert nets[1] == "offset"
469                         offset = nets[2]
470                     else:
471                         offset = None
472                     if step.strip().startswith("src"):
473                         write_rule(ip_to_rule(proto, nets[0], "saddr", offset))
474                     else:
475                         write_rule(ip_to_rule(proto, nets[0], "daddr", offset))
476                 elif step.strip().startswith("proto") and proto == 4:
477                     write_rule(proto_to_rule(4, step.strip()[6:]))
478                 elif step.strip().startswith("next header") and proto == 6:
479                     write_rule(proto_to_rule(6, step.strip()[12:]))
480                 elif step.strip().startswith("icmp type"):
481                     write_rule(icmp_type_to_rule(proto, step.strip()[10:]))
482                 elif step.strip().startswith("icmp code"):
483                     write_rule(icmp_code_to_rule(proto, step.strip()[10:]))
484                 elif step.strip().startswith("sport") or step.strip().startswith("dport") or step.strip().startswith("port"):
485                     write_rule(port_to_rule(step.strip().split(" ")[0], step.strip().split(" ", 1)[1]))
486                 elif step.strip().startswith("length"):
487                     write_rule(len_to_rule(step.strip()[7:]))
488                 elif step.strip().startswith("dscp"):
489                     write_rule(dscp_to_rule(proto, step.strip()[5:]))
490                 elif step.strip().startswith("tcp flags"):
491                     write_rule(tcp_flags_to_rule(step.strip()[10:]))
492                 elif step.strip().startswith("label"):
493                     write_rule(flow_label_to_rule(step.strip()[6:]))
494                 elif step.strip().startswith("fragment"):
495                     if proto == 6:
496                         use_v6_frags = True
497                     write_rule(fragment_to_rule(proto, step.strip()[9:]))
498                 elif step.strip() == "":
499                     pass
500                 else:
501                     assert False
502
503             actions = ""
504             def write_rule(r):
505                 global actions
506                 actions += r + "\n"
507
508             # Now write the match handling!
509             first_action = None
510             stats_action = ""
511             last_action = None
512             for community in line.split("("):
513                 if not community.startswith("generic, "):
514                     continue
515                 blocks = community.split(",")
516                 assert len(blocks) == 3
517                 if len(blocks[1].strip()) != 10: # Should be 0x12345678
518                     continue
519                 ty = blocks[1].strip()[:6]
520                 high_byte = int(blocks[1].strip()[6:8], 16)
521                 mid_byte = int(blocks[1].strip()[8:], 16)
522                 low_bytes = int(blocks[2].strip(") \n"), 16)
523                 if ty == "0x8006" or ty == "0x800c" or ty == "0x8306" or ty == "0x830c":
524                     if first_action is not None:
525                         # Two ratelimit actions, just drop the old one. RFC 8955 says we can.
526                         first_action = None
527                     exp = (low_bytes & (0xff << 23)) >> 23
528                     if low_bytes == 0:
529                         first_action = "{stats_replace}\nreturn XDP_DROP;"
530                     elif low_bytes & (1 <<  31) != 0:
531                         # Negative limit, just drop
532                         first_action = "{stats_replace}\nreturn XDP_DROP;"
533                     elif exp == 0xff:
534                         # NaN/INF. Just treat as INF and accept
535                         first_action = None
536                     elif exp < 127: # < 1
537                         first_action = "{stats_replace}\nreturn XDP_DROP;"
538                     elif exp >= 127 + 29: # We can't handle the precision required with ns this high
539                         first_action = None
540                     else:
541                         mantissa = low_bytes & ((1 << 23) - 1)
542                         value = 1.0 + mantissa / (2**23)
543                         value *= 2**(exp-127)
544
545                         first_action =   "int64_t time_masked = bpf_ktime_get_ns() & RATE_TIME_MASK;\n"
546                         first_action += f"int64_t per_pkt_ns = (1000000000LL << RATE_BUCKET_INTEGER_BITS) / {math.floor(value)};\n"
547                         if ty == "0x8006" or ty == "0x8306":
548                             first_action += "uint64_t amt = data_end - pktdata;\n"
549                         else:
550                             first_action += "uint64_t amt = 1;\n"
551                         if ty == "0x8006" or ty == "0x800c":
552                             first_action += f"const uint32_t ratelimitidx = {ratelimitcnt};\n"
553                             first_action += "struct ratelimit *rate = bpf_map_lookup_elem(&rate_map, &ratelimitidx);\n"
554                             ratelimitcnt += 1
555                             first_action +=  "int matched = 0;\n"
556                             first_action += "DO_RATE_LIMIT(bpf_spin_lock(&rate->lock), rate, time_masked, amt, per_pkt_ns, matched);\n"
557                             first_action += "if (rate) { bpf_spin_unlock(&rate->lock); }\n"
558                         else:
559                             if proto == 4:
560                                 if mid_byte > 32:
561                                     continue
562                                 first_action += f"const uint32_t srcip = ip->saddr & MASK4({mid_byte});\n"
563                                 first_action += f"void *rate_map = &v4_src_rate_{len(v4persrcratelimits)};\n"
564                                 first_action += f"int matched = check_v4_persrc_ratelimit(srcip, rate_map, {(high_byte + 1) * 4096}, time_masked, amt, per_pkt_ns);\n"
565                                 v4persrcratelimits.append((high_byte + 1) * 4096)
566                             elif mid_byte <= 64:
567                                 first_action += f"const uint64_t srcip = BE128BEHIGH64(ip6->saddr & MASK6({mid_byte}));\n"
568                                 first_action += f"void *rate_map = &v5_src_rate_{len(v5persrcratelimits)};\n"
569                                 first_action += f"int matched = check_v5_persrc_ratelimit(srcip, rate_map, {(high_byte + 1) * 4096}, time_masked, amt, per_pkt_ns);\n"
570                                 v5persrcratelimits.append((high_byte + 1) * 4096)
571                             else:
572                                 if mid_byte > 128:
573                                     continue
574                                 first_action += f"const uint128_t srcip = ip6->saddr & MASK6({mid_byte});\n"
575                                 first_action += f"void *rate_map = &v6_src_rate_{len(v6persrcratelimits)};\n"
576                                 first_action += f"int matched = check_v6_persrc_ratelimit(srcip, rate_map, {(high_byte + 1) * 4096}, time_masked, amt, per_pkt_ns);\n"
577                                 v6persrcratelimits.append((high_byte + 1) * 4096)
578                         first_action +=  "if (matched) {\n"
579                         first_action +=  "\t{stats_replace}\n"
580                         first_action +=  "\treturn XDP_DROP;\n"
581                         first_action +=  "}\n"
582                 elif ty == "0x8007":
583                     if low_bytes & 1 == 0:
584                         last_action = "return XDP_PASS;"
585                     if low_bytes & 2 == 2:
586                         stats_action = f"const uint32_t ruleidx = STATIC_RULE_CNT + {stats_rulecnt};\n"
587                         stats_action += "INCREMENT_MATCH(ruleidx);"
588                 elif ty == "0x8008":
589                     assert False # We do not implement the redirect action
590                 elif ty == "0x8009":
591                     if low_bytes & ~0b111111 != 0:
592                         assert False # Invalid DSCP value
593                     if proto == 4:
594                         write_rule("int32_t chk = ~BE16(ip->check) & 0xffff;")
595                         write_rule("uint8_t orig_tos = ip->tos;")
596                         write_rule("ip->tos = (ip->tos & 3) | " + str(low_bytes << 2) + ";")
597                         write_rule("chk = (chk - orig_tos + ip->tos);")
598                         write_rule("if (unlikely(chk > 0xffff)) { chk -= 65535; }")
599                         write_rule("else if (unlikely(chk < 0)) { chk += 65535; }")
600                         write_rule("ip->check = ~BE16(chk);")
601                     else:
602                         write_rule("ip6->priority = " + str(low_bytes >> 2) + ";")
603                         write_rule("ip6->flow_lbl[0] = (ip6->flow_lbl[0] & 0x3f) | " + str((low_bytes & 3) << 6) + ";")
604             if first_action is not None:
605                 write_rule(first_action.replace("{stats_replace}", stats_action))
606             if stats_action != "" and (first_action is None or "{stats_replace}" not in first_action):
607                 write_rule(stats_action)
608             if last_action is not None:
609                 write_rule(last_action)
610             if proto == 6:
611                 rules6.append(RuleNode(RuleAction.CONDITIONS, conditions, RuleNode(RuleAction.ACTION, actions, None)))
612             else:
613                 rules4.append(RuleNode(RuleAction.CONDITIONS, conditions, RuleNode(RuleAction.ACTION, actions, None)))
614             if stats_action != "":
615                 print(rule)
616                 stats_rulecnt += 1
617             lastrule = None
618
619     out.write("\n")
620     out.write(f"#define STATS_RULECNT {stats_rulecnt}\n")
621     if ratelimitcnt != 0:
622         out.write(f"#define RATE_CNT {ratelimitcnt}\n")
623
624     if len(rules4) != 0:
625         out.write("#define NEED_V4_PARSE\n")
626         out.write("#define RULES4 {\\\n")
627         # First sort the rules according to the RFC, then make it a single
628         # LIST rule and call flatten() to unify redundant conditions
629         rules4.sort()
630         rules4 = RuleNode(RuleAction.LIST, None, rules4)
631         rules4.flatten()
632         rules4.write(out)
633         out.write("}\n")
634
635     if len(rules6) != 0:
636         out.write("#define NEED_V6_PARSE\n")
637         out.write("#define RULES6 {\\\n")
638         # First sort the rules according to the RFC, then make it a single
639         # LIST rule and call flatten() to unify redundant conditions
640         rules6.sort()
641         rules6 = RuleNode(RuleAction.LIST, None, rules6)
642         rules6.flatten()
643         rules6.write(out)
644         out.write("}\n")
645
646     if args.v6frag == "ignore-parse-if-rule":
647         if use_v6_frags:
648             out.write("#define PARSE_V6_FRAG PARSE\n")
649     with open("maps.h", "w") as out:
650         for idx, limit in enumerate(v4persrcratelimits):
651             out.write(f"SRC_RATE_DEFINE(4, {idx}, {limit})\n")
652         for idx, limit in enumerate(v5persrcratelimits):
653             out.write(f"SRC_RATE_DEFINE(5, {idx}, {limit})\n")
654         for idx, limit in enumerate(v6persrcratelimits):
655             out.write(f"SRC_RATE_DEFINE(6, {idx}, {limit})\n")