Less effecient, but much, much less naive rate-limiter
[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 class ASTAction(Enum):
16     OR = 1
17     AND = 2
18     NOT = 3
19     FALSE = 4
20     TRUE = 5
21     EXPR = 6
22 class ASTNode:
23     def __init__(self, action, left, right=None):
24         self.action = action
25         if action == ASTAction.FALSE or action == ASTAction.TRUE:
26             assert left is None and right is None
27             return
28         self.left = left
29         if right is None:
30             assert action == ASTAction.EXPR or action == ASTAction.NOT
31         else:
32             self.right = right
33
34     def write(self, expr_param, expr_param2=None):
35         if self.action == ASTAction.OR:
36             return "(" + self.left.write(expr_param, expr_param2) + ") || (" + self.right.write(expr_param, expr_param2) + ")"
37         if self.action == ASTAction.AND:
38             return "(" + self.left.write(expr_param, expr_param2) + ") && (" + self.right.write(expr_param, expr_param2) + ")"
39         if self.action == ASTAction.NOT:
40             return "!(" + self.left.write(expr_param, expr_param2) + ")"
41         if self.action == ASTAction.FALSE:
42             return "0"
43         if self.action == ASTAction.TRUE:
44             return "1"
45         if self.action == ASTAction.EXPR:
46             return self.left.write(expr_param, expr_param2)
47
48 def parse_ast(expr, parse_expr):
49     expr = expr.strip()
50
51     comma_split = expr.split(",", 1)
52     or_split = expr.split("||", 1)
53     if len(comma_split) > 1 and not "||" in comma_split[0]:
54         return ASTNode(ASTAction.OR, parse_ast(comma_split[0], parse_expr), parse_ast(comma_split[1], parse_expr))
55     if len(or_split) > 1:
56         assert not "," in or_split[0]
57         return ASTNode(ASTAction.OR, parse_ast(or_split[0], parse_expr), parse_ast(or_split[1], parse_expr))
58
59     and_split = expr.split("&&", 1)
60     if len(and_split) > 1:
61         return ASTNode(ASTAction.AND, parse_ast(and_split[0], parse_expr), parse_ast(and_split[1], parse_expr))
62
63     if expr.strip() == "true":
64         return ASTNode(ASTAction.TRUE, None)
65     if expr.strip() == "false":
66         return ASTNode(ASTAction.FALSE, None)
67
68     if expr.startswith("!"):
69         return ASTNode(ASTAction.NOT, parse_ast(expr[1:], parse_expr))
70
71     return parse_expr(expr)
72
73
74 class NumbersAction(Enum):
75     EQ = "=="
76     GT = ">"
77     GTOE = ">="
78     LT = "<"
79     LTOE = "<="
80 class NumbersExpr:
81     def __init__(self, action, val):
82         self.action = action
83         self.val = val
84
85     def write(self, param, param2):
86         if param2 is not None:
87             return "(" + param + self.action.value + self.val + ") || (" + param2 + self.action.value + self.val + ")"
88         return param + self.action.value + self.val
89
90 def parse_numbers_expr(expr):
91     space_split = expr.split(" ")
92     if expr.startswith(">="):
93         assert len(space_split) == 2
94         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GTOE, space_split[1]))
95     if expr.startswith(">"):
96         assert len(space_split) == 2
97         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GT, space_split[1]))
98     if expr.startswith("<="):
99         assert len(space_split) == 2
100         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LTOE, space_split[1]))
101     if expr.startswith("<"):
102         assert len(space_split) == 2
103         return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LT, space_split[1]))
104     if ".." in expr:
105         rangesplit = expr.split("..")
106         assert len(rangesplit) == 2
107         #XXX: Are ranges really inclusive,inclusive?
108         left = ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.GTOE, rangesplit[0]))
109         right = ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.LTOE, rangesplit[1]))
110         return ASTNode(ASTAction.AND, left, right)
111     
112     if expr.startswith("= "):
113         expr = expr[2:]
114     return ASTNode(ASTAction.EXPR, NumbersExpr(NumbersAction.EQ, expr))
115
116 class FragExpr(Enum):
117     IF = 1
118     FF = 2
119     DF = 3
120     LF = 4
121
122     def write(self, ipproto, _param2):
123         if ipproto == 4:
124             if self == FragExpr.IF:
125                 return "(ip->frag_off & BE16(IP_MF|IP_OFFSET)) != 0"
126             elif self == FragExpr.FF:
127                 return "((ip->frag_off & BE16(IP_MF)) != 0 && (ip->frag_off & BE16(IP_OFFSET)) == 0)"
128             elif self == FragExpr.DF:
129                 return "(ip->frag_off & BE16(IP_DF)) != 0"
130             elif self == FragExpr.LF:
131                 return "((ip->frag_off & BE16(IP_MF)) == 0 && (ip->frag_off & BE16(IP_OFFSET)) != 0)"
132             else:
133                 assert False
134         else:
135             if self == FragExpr.IF:
136                 return "frag6 != NULL"
137             elif self == FragExpr.FF:
138                 return "(frag6 != NULL && (frag6->frag_off & BE16(IP6_MF)) != 0 && (frag6->frag_off & BE16(IP6_FRAGOFF)) == 0)"
139             elif self == FragExpr.DF:
140                 assert False # No such thing in v6
141             elif self == FragExpr.LF:
142                 return "(frag6 != NULL && (frag6->frag_off & BE16(IP6_MF)) == 0 && (frag6->frag_off & BE16(IP6_FRAGOFF)) != 0)"
143             else:
144                 assert False
145
146 def parse_frag_expr(expr):
147     if expr == "is_fragment":
148         return ASTNode(ASTAction.EXPR, FragExpr.IF)
149     elif expr == "first_fragment":
150         return ASTNode(ASTAction.EXPR, FragExpr.FF)
151     elif expr == "dont_fragment":
152         return ASTNode(ASTAction.EXPR, FragExpr.DF)
153     elif expr == "last_fragment":
154         return ASTNode(ASTAction.EXPR, FragExpr.LF)
155     else:
156         assert False
157
158 class BitExpr:
159     def __init__(self, val):
160         s = val.split("/")
161         assert len(s) == 2
162         self.match = s[0]
163         self.mask = s[1]
164
165     def write(self, param, _param2):
166         return f"({param} & {self.mask}) == {self.match}"
167
168 def parse_bit_expr(expr):
169     return ASTNode(ASTAction.EXPR, BitExpr(expr))
170
171
172 def ip_to_rule(proto, inip, ty, offset):
173     if proto == 4:
174         assert offset is None
175         net = ipaddress.IPv4Network(inip.strip())
176         if net.prefixlen == 0:
177             return ""
178         return f"""if ((ip->{ty} & MASK4({net.prefixlen})) != BIGEND32({int(net.network_address)}ULL))
179         break;"""
180     else:
181         net = ipaddress.IPv6Network(inip.strip())
182         if net.prefixlen == 0:
183             return ""
184         u32s = [(int(net.network_address) >> (3*32)) & 0xffffffff,
185                 (int(net.network_address) >> (2*32)) & 0xffffffff,
186                 (int(net.network_address) >> (1*32)) & 0xffffffff,
187                 (int(net.network_address) >> (0*32)) & 0xffffffff]
188         if offset is None:
189             mask = f"MASK6({net.prefixlen})"
190         else:
191             mask = f"MASK6_OFFS({offset}, {net.prefixlen})"
192         return f"""if ((ip6->{ty} & {mask}) != (BIGEND128({u32s[0]}ULL, {u32s[1]}ULL, {u32s[2]}ULL, {u32s[3]}ULL) & {mask}))
193         break;"""
194
195 def fragment_to_rule(ipproto, rules):
196     ast = parse_ast(rules, parse_frag_expr)
197     return "if (!( " + ast.write(ipproto) + " )) break;"
198
199 def len_to_rule(rules):
200     ast = parse_ast(rules, parse_numbers_expr)
201     return "if (!( " + ast.write("(data_end - pktdata)") + " )) break;"
202  
203 def proto_to_rule(ipproto, proto):
204     ast = parse_ast(proto, parse_numbers_expr)
205
206     if ipproto == 4:
207         return "if (!( " + ast.write("ip->protocol") + " )) break;"
208     else:
209         return "if (!( " + ast.write("ip6->nexthdr") + " )) break;"
210
211 def icmp_type_to_rule(proto, ty):
212     ast = parse_ast(ty, parse_numbers_expr)
213     if proto == 4:
214         return "if (icmp == NULL) break;\nif (!( " + ast.write("icmp->type") + " )) break;"
215     else:
216         return "if (icmpv6 == NULL) break;\nif (!( " + ast.write("icmpv6->icmp6_type") + " )) break;"
217
218 def icmp_code_to_rule(proto, code):
219     ast = parse_ast(code, parse_numbers_expr)
220     if proto == 4:
221         return "if (icmp == NULL) break;\nif (!( " + ast.write("icmp->code") + " )) break;"
222     else:
223         return "if (icmpv6 == NULL) break;\nif (!( " + ast.write("icmpv6->icmp6_code") + " )) break;"
224
225 def dscp_to_rule(proto, rules):
226     ast = parse_ast(rules, parse_numbers_expr)
227
228     if proto == 4:
229         return "if (!( " + ast.write("((ip->tos & 0xfc) >> 2)") + " )) break;"
230     else:
231         return "if (!( " + ast.write("((ip6->priority << 2) | ((ip6->flow_lbl[0] & 0xc0) >> 6))") + " )) break;"
232
233 def port_to_rule(ty, rules):
234     if ty == "port" :
235         ast = parse_ast(rules, parse_numbers_expr)
236         return "if (!ports_valid) break;\nif (!( " + ast.write("sport", "dport") + " )) break;"
237
238     ast = parse_ast(rules, parse_numbers_expr)
239     return "if (!ports_valid) break;\nif (!( " + ast.write(ty) + " )) break;"
240
241 def tcp_flags_to_rule(rules):
242     ast = parse_ast(rules, parse_bit_expr)
243
244     return f"""if (tcp == NULL) break;
245 if (!( {ast.write("(ntohs(tcp->flags) & 0xfff)")} )) break;"""
246
247 def flow_label_to_rule(rules):
248     ast = parse_ast(rules, parse_bit_expr)
249
250     return f"""if (ip6 == NULL) break;
251 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;"""
252
253
254 with open("rules.h", "w") as out:
255     parse = argparse.ArgumentParser()
256     parse.add_argument("--ihl", dest="ihl", required=True, choices=["drop-options","accept-options","parse-options"])
257     parse.add_argument("--v6frag", dest="v6frag", required=True, choices=["drop-frags","ignore","parse-frags","ignore-parse-if-rule"])
258     parse.add_argument("--8021q", dest="vlan", required=True, choices=["drop-vlan","accept-vlan","parse-vlan"])
259     parse.add_argument("--require-8021q", dest="vlan_tag")
260     args = parse.parse_args(sys.argv[1:])
261
262     if args.ihl == "drop-options":
263         out.write("#define PARSE_IHL XDP_DROP\n")
264     elif args.ihl == "accept-options":
265         out.write("#define PARSE_IHL XDP_PASS\n")
266     elif args.ihl == "parse-options":
267         out.write("#define PARSE_IHL PARSE\n")
268
269     if args.v6frag == "drop-frags":
270         out.write("#define PARSE_V6_FRAG XDP_DROP\n")
271     elif args.v6frag == "ignore":
272         pass
273     elif args.v6frag == "parse-frags":
274         out.write("#define PARSE_V6_FRAG PARSE\n")
275
276     if args.vlan == "drop-vlan":
277         out.write("#define PARSE_8021Q XDP_DROP\n")
278     elif args.vlan == "accept-vlan":
279         out.write("#define PARSE_8021Q XDP_PASS\n")
280     elif args.vlan == "parse-vlan":
281         out.write("#define PARSE_8021Q PARSE\n")
282
283     if args.vlan_tag is not None:
284         if args.vlan != "parse-vlan":
285             assert False
286         out.write("#define REQ_8021Q " + args.vlan_tag + "\n")
287
288     rules6 = ""
289     rules4 = ""
290     use_v6_frags = False
291     rulecnt = 0
292     ratelimitcnt = 0
293
294     lastrule = None
295     for line in sys.stdin.readlines():
296         if "{" in line:
297             if lastrule is not None:
298                 print("Skipped rule due to lack of understood community tag: " + lastrule)
299             lastrule = line
300             continue
301         if "BGP.ext_community: " in line:
302             assert lastrule is not None
303
304             t = lastrule.split("{")
305             if t[0].strip() == "flow4":
306                 proto = 4
307                 rules4 += "\tdo {\\\n"
308             elif t[0].strip() == "flow6":
309                 proto = 6
310                 rules6 += "\tdo {\\\n"
311             else:
312                 continue
313
314             def write_rule(r):
315                 global rules4, rules6
316                 if proto == 6:
317                     rules6 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
318                 else:
319                     rules4 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
320
321             rule = t[1].split("}")[0].strip()
322             for step in rule.split(";"):
323                 if step.strip().startswith("src") or step.strip().startswith("dst"):
324                     nets = step.strip()[3:].strip().split(" ")
325                     if len(nets) > 1:
326                         assert nets[1] == "offset"
327                         offset = nets[2]
328                     else:
329                         offset = None
330                     if step.strip().startswith("src"):
331                         write_rule(ip_to_rule(proto, nets[0], "saddr", offset))
332                     else:
333                         write_rule(ip_to_rule(proto, nets[0], "daddr", offset))
334                 elif step.strip().startswith("proto") and proto == 4:
335                     write_rule(proto_to_rule(4, step.strip()[6:]))
336                 elif step.strip().startswith("next header") and proto == 6:
337                     write_rule(proto_to_rule(6, step.strip()[12:]))
338                 elif step.strip().startswith("icmp type"):
339                     write_rule(icmp_type_to_rule(proto, step.strip()[10:]))
340                 elif step.strip().startswith("icmp code"):
341                     write_rule(icmp_code_to_rule(proto, step.strip()[10:]))
342                 elif step.strip().startswith("sport") or step.strip().startswith("dport") or step.strip().startswith("port"):
343                     write_rule(port_to_rule(step.strip().split(" ")[0], step.strip().split(" ", 1)[1]))
344                 elif step.strip().startswith("length"):
345                     write_rule(len_to_rule(step.strip()[7:]))
346                 elif step.strip().startswith("dscp"):
347                     write_rule(dscp_to_rule(proto, step.strip()[5:]))
348                 elif step.strip().startswith("tcp flags"):
349                     write_rule(tcp_flags_to_rule(step.strip()[10:]))
350                 elif step.strip().startswith("label"):
351                     write_rule(flow_label_to_rule(step.strip()[6:]))
352                 elif step.strip().startswith("fragment"):
353                     if proto == 6:
354                         use_v6_frags = True
355                     write_rule(fragment_to_rule(proto, step.strip()[9:]))
356                 elif step.strip() == "":
357                     pass
358                 else:
359                     assert False
360
361             # Now write the match handling!
362             first_action = None
363             last_action = None
364             for community in line.split("("):
365                 if not community.startswith("generic, "):
366                     continue
367                 blocks = community.split(",")
368                 assert len(blocks) == 3
369                 if len(blocks[1].strip()) != 10: # Should be 0x12345678
370                     continue
371                 ty = blocks[1].strip()[:6]
372                 low_bytes = int(blocks[2].strip(") \n"), 16)
373                 if ty == "0x8006":
374                     if first_action is not None:
375                         assert False # Two ratelimit actions?
376                     if low_bytes == 0:
377                         first_action = "return XDP_DROP;"
378                     else:
379                         if low_bytes & (1 <<  31) != 0:
380                             assert False # Negative ratelimit?
381                         exp = (low_bytes & (0xff << 23)) >> 23
382                         if exp == 0xff:
383                             assert False # NaN/INF?
384                         if exp <= 127: # < 1
385                             first_action = "return XDP_DROP;"
386                         if exp >= 127 + 63: # The count won't even fit in 64-bits, just accept
387                             first_action = "return XDP_PASS;"
388                         mantissa = low_bytes & ((1 << 23) - 1)
389                         value = 1.0 + mantissa / (2**23)
390                         value *= 2**(exp-127)
391                         # Note that int64_t will overflow after 292 years of uptime
392                         first_action = "int64_t time = bpf_ktime_get_ns();\n"
393                         first_action += f"const uint32_t ratelimitidx = {ratelimitcnt};\n"
394                         first_action += "size_t pktlen = data_end - pktdata;\n"
395                         first_action += "uint64_t extra_bytes = 0;\n"
396                         first_action += "struct ratelimit *rate = bpf_map_lookup_elem(&rate_map, &ratelimitidx);\n"
397                         first_action += "if (rate) {\n"
398                         first_action += "\tbpf_spin_lock(&rate->lock);\n"
399                         first_action += "\tif (likely(rate->sent_bytes > 0)) {\n"
400                         first_action += "\t\tint64_t diff = time - rate->sent_time;\n"
401                         # Unlikely or not, if the flow is slow, take a perf hit (though with the else if branch it doesn't matter)
402                         first_action += "\t\tif (unlikely(diff > 1000000000))\n"
403                         first_action += "\t\t\trate->sent_bytes = 0;\n"
404                         first_action += "\t\telse if (likely(diff > 0))\n"
405                         first_action += f"\t\t\textra_bytes = ((uint64_t)diff) * {math.floor(value)} / 1000000000;\n"
406                         first_action += "\t}\n"
407                         first_action += "\tif (rate->sent_bytes - ((int64_t)extra_bytes) <= 0) {\n"
408                         first_action += "\t\trate->sent_bytes = data_end - pktdata;\n"
409                         first_action += "\t\trate->sent_time = time;\n"
410                         first_action += "\t\tbpf_spin_unlock(&rate->lock);\n"
411                         first_action += "\t} else {\n"
412                         first_action += "\t\tbpf_spin_unlock(&rate->lock);\n"
413                         first_action += "\t\treturn XDP_DROP;\n"
414                         first_action += "\t}\n"
415                         first_action += "}\n"
416                         ratelimitcnt += 1
417                 elif ty == "0x8007":
418                     if low_bytes & 1 == 0:
419                         last_action = "return XDP_PASS;"
420                     if low_bytes & 2 == 2:
421                         write_rule(f"const uint32_t ruleidx = STATIC_RULE_CNT + {rulecnt};")
422                         write_rule("INCREMENT_MATCH(ruleidx);")
423                 elif ty == "0x8008":
424                     assert False # We do not implement the redirect action
425                 elif ty == "0x8009":
426                     if low_bytes & ~0b111111 != 0:
427                         assert False # Invalid DSCP value
428                     if proto == 4:
429                         write_rule("int32_t chk = ~BE16(ip->check) & 0xffff;")
430                         write_rule("uint8_t orig_tos = ip->tos;")
431                         write_rule("ip->tos = (ip->tos & 3) | " + str(low_bytes << 2) + ";")
432                         write_rule("chk = (chk - orig_tos + ip->tos);")
433                         write_rule("if (unlikely(chk < 0)) { chk += 65534; }")
434                         write_rule("ip->check = ~BE16(chk);")
435                     else:
436                         write_rule("ip6->priority = " + str(low_bytes >> 2) + ";")
437                         write_rule("ip6->flow_lbl[0] = (ip6->flow_lbl[0] & 0x3f) | " + str((low_bytes & 3) << 6) + ";")
438             if first_action is not None:
439                 write_rule(first_action)
440             if last_action is not None:
441                 write_rule(last_action)
442             if proto == 6:
443                 rules6 += "\t} while(0);\\\n"
444             else:
445                 rules4 += "\t} while(0);\\\n"
446             rulecnt += 1
447             lastrule = None
448
449     out.write("\n")
450     out.write(f"#define RULECNT {rulecnt}\n")
451     if ratelimitcnt != 0:
452         out.write(f"#define RATE_CNT {ratelimitcnt}\n")
453     if rules4 != "":
454         out.write("#define NEED_V4_PARSE\n")
455         out.write("#define RULES4 {\\\n" + rules4 + "}\n")
456     if rules6:
457         out.write("#define NEED_V6_PARSE\n")
458         out.write("#define RULES6 {\\\n" + rules6 + "}\n")
459     if args.v6frag == "ignore-parse-if-rule":
460         if use_v6_frags:
461             out.write("#define PARSE_V6_FRAG PARSE\n")