Add per-source ratelimit support
[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     v4persrcratelimitcnt = 0
294     v6persrcratelimitcnt = 0
295
296     lastrule = None
297     for line in sys.stdin.readlines():
298         if "{" in line:
299             if lastrule is not None:
300                 print("Skipped rule due to lack of understood community tag: " + lastrule)
301             lastrule = line
302             continue
303         if "BGP.ext_community: " in line:
304             assert lastrule is not None
305
306             t = lastrule.split("{")
307             if t[0].strip() == "flow4":
308                 proto = 4
309                 rules4 += "\tdo {\\\n"
310             elif t[0].strip() == "flow6":
311                 proto = 6
312                 rules6 += "\tdo {\\\n"
313             else:
314                 continue
315
316             def write_rule(r):
317                 global rules4, rules6
318                 if proto == 6:
319                     rules6 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
320                 else:
321                     rules4 += "\t\t" + r.replace("\n", " \\\n\t\t") + " \\\n"
322
323             rule = t[1].split("}")[0].strip()
324             for step in rule.split(";"):
325                 if step.strip().startswith("src") or step.strip().startswith("dst"):
326                     nets = step.strip()[3:].strip().split(" ")
327                     if len(nets) > 1:
328                         assert nets[1] == "offset"
329                         offset = nets[2]
330                     else:
331                         offset = None
332                     if step.strip().startswith("src"):
333                         write_rule(ip_to_rule(proto, nets[0], "saddr", offset))
334                     else:
335                         write_rule(ip_to_rule(proto, nets[0], "daddr", offset))
336                 elif step.strip().startswith("proto") and proto == 4:
337                     write_rule(proto_to_rule(4, step.strip()[6:]))
338                 elif step.strip().startswith("next header") and proto == 6:
339                     write_rule(proto_to_rule(6, step.strip()[12:]))
340                 elif step.strip().startswith("icmp type"):
341                     write_rule(icmp_type_to_rule(proto, step.strip()[10:]))
342                 elif step.strip().startswith("icmp code"):
343                     write_rule(icmp_code_to_rule(proto, step.strip()[10:]))
344                 elif step.strip().startswith("sport") or step.strip().startswith("dport") or step.strip().startswith("port"):
345                     write_rule(port_to_rule(step.strip().split(" ")[0], step.strip().split(" ", 1)[1]))
346                 elif step.strip().startswith("length"):
347                     write_rule(len_to_rule(step.strip()[7:]))
348                 elif step.strip().startswith("dscp"):
349                     write_rule(dscp_to_rule(proto, step.strip()[5:]))
350                 elif step.strip().startswith("tcp flags"):
351                     write_rule(tcp_flags_to_rule(step.strip()[10:]))
352                 elif step.strip().startswith("label"):
353                     write_rule(flow_label_to_rule(step.strip()[6:]))
354                 elif step.strip().startswith("fragment"):
355                     if proto == 6:
356                         use_v6_frags = True
357                     write_rule(fragment_to_rule(proto, step.strip()[9:]))
358                 elif step.strip() == "":
359                     pass
360                 else:
361                     assert False
362
363             # Now write the match handling!
364             first_action = None
365             last_action = None
366             for community in line.split("("):
367                 if not community.startswith("generic, "):
368                     continue
369                 blocks = community.split(",")
370                 assert len(blocks) == 3
371                 if len(blocks[1].strip()) != 10: # Should be 0x12345678
372                     continue
373                 ty = blocks[1].strip()[:6]
374                 high_byte = int(blocks[1].strip()[8:], 16)
375                 low_bytes = int(blocks[2].strip(") \n"), 16)
376                 if ty == "0x8006" or ty == "0x800c" or ty == "0x8306" or ty == "0x830c":
377                     if first_action is not None:
378                         # Two ratelimit actions, just drop the old one. RFC 8955 says we can.
379                         first_action = None
380                     exp = (low_bytes & (0xff << 23)) >> 23
381                     if low_bytes == 0:
382                         first_action = "return XDP_DROP;"
383                     elif low_bytes & (1 <<  31) != 0:
384                         # Negative limit, just drop
385                         first_action = "return XDP_DROP;"
386                     elif exp == 0xff:
387                         # NaN/INF. Just treat as INF and accept
388                         first_action = None
389                     elif exp <= 127: # < 1
390                         first_action = "return XDP_DROP;"
391                     elif exp >= 127 + 63: # The count won't even fit in 64-bits, just accept
392                         first_action = None
393                     else:
394                         mantissa = low_bytes & ((1 << 23) - 1)
395                         value = 1.0 + mantissa / (2**23)
396                         value *= 2**(exp-127)
397                         if ty == "0x8006" or ty == "0x8306":
398                             accessor = "rate->rate.sent_bytes"
399                         else:
400                             accessor = "rate->rate.sent_packets"
401                         # Note that int64_t will overflow after 292 years of uptime
402                         first_action = "int64_t time = bpf_ktime_get_ns();\n"
403                         first_action +=  "uint64_t allowed_since_last = 0;\n"
404                         if ty == "0x8006" or ty == "0x800c":
405                             spin_lock = "bpf_spin_lock(&rate->lock);"
406                             spin_unlock = "bpf_spin_unlock(&rate->lock);"
407                             first_action += f"const uint32_t ratelimitidx = {ratelimitcnt};\n"
408                             first_action += "struct ratelimit *rate = bpf_map_lookup_elem(&rate_map, &ratelimitidx);\n"
409                             ratelimitcnt += 1
410                         else:
411                             spin_lock = "/* No locking as we're per-CPU */"
412                             spin_unlock = "/* No locking as we're per-CPU */"
413                             if proto == 4:
414                                 if high_byte > 32:
415                                     continue
416                                 first_action += f"const uint32_t srcip = ip->saddr & MASK4({high_byte});\n"
417                                 first_action += f"void *rate_map = &v4_src_rate_{v4persrcratelimitcnt};\n"
418                                 v4persrcratelimitcnt += 1
419                             else:
420                                 if high_byte > 128:
421                                     continue
422                                 first_action += f"const uint128_t srcip = ip6->saddr & MASK6({high_byte});\n"
423                                 first_action += f"void *rate_map = &v6_src_rate_{v6persrcratelimitcnt};\n"
424                                 v6persrcratelimitcnt += 1
425                             first_action += f"struct percpu_ratelimit *rate = bpf_map_lookup_elem(rate_map, &srcip);\n"
426                         first_action +=  "if (rate) {\n"
427                         first_action += f"\t{spin_lock}\n"
428                         first_action += f"\tif (likely({accessor} > 0))" + " {\n"
429                         first_action +=  "\t\tint64_t diff = time - rate->sent_time;\n"
430                         # Unlikely or not, if the flow is slow, take a perf hit (though with the else if branch it doesn't matter)
431                         first_action +=  "\t\tif (unlikely(diff > 1000000000))\n"
432                         first_action += f"\t\t\t{accessor} = 0;\n"
433                         first_action +=  "\t\telse if (likely(diff > 0))\n"
434                         first_action += f"\t\t\tallowed_since_last = ((uint64_t)diff) * {math.floor(value)} / 1000000000;\n"
435                         first_action +=  "\t}\n"
436                         first_action += f"\tif ({accessor} - ((int64_t)allowed_since_last) <= 0)" + " {\n"
437                         if ty == "0x8006" or ty == "0x8306":
438                             first_action += f"\t\t{accessor} = data_end - pktdata;\n"
439                         else:
440                             first_action += f"\t\t{accessor} = 1;\n"
441                         first_action +=  "\t\trate->sent_time = time;\n"
442                         first_action += f"\t\t{spin_unlock}\n"
443                         first_action +=  "\t} else {\n"
444                         first_action += f"\t\t{spin_unlock}\n"
445                         first_action +=  "\t\treturn XDP_DROP;\n"
446                         first_action +=  "\t}\n"
447                         if ty == "0x8306" or ty == "0x830c":
448                             first_action +=  "} else {\n"
449                             first_action +=  "\tstruct percpu_ratelimit new_rate = { .sent_time = time, };\n"
450                             first_action +=  "\trate = &new_rate;\n"
451                             if ty == "0x8006" or ty == "0x8306":
452                                 first_action += f"\t\t{accessor} = data_end - pktdata;\n"
453                             else:
454                                 first_action += f"\t\t{accessor} = 1;\n"
455                             first_action +=  "\tbpf_map_update_elem(rate_map, &srcip, rate, BPF_ANY);\n"
456                         first_action +=  "}\n"
457                 elif ty == "0x8007":
458                     if low_bytes & 1 == 0:
459                         last_action = "return XDP_PASS;"
460                     if low_bytes & 2 == 2:
461                         write_rule(f"const uint32_t ruleidx = STATIC_RULE_CNT + {rulecnt};")
462                         write_rule("INCREMENT_MATCH(ruleidx);")
463                 elif ty == "0x8008":
464                     assert False # We do not implement the redirect action
465                 elif ty == "0x8009":
466                     if low_bytes & ~0b111111 != 0:
467                         assert False # Invalid DSCP value
468                     if proto == 4:
469                         write_rule("int32_t chk = ~BE16(ip->check) & 0xffff;")
470                         write_rule("uint8_t orig_tos = ip->tos;")
471                         write_rule("ip->tos = (ip->tos & 3) | " + str(low_bytes << 2) + ";")
472                         write_rule("chk = (chk - orig_tos + ip->tos);")
473                         write_rule("if (unlikely(chk > 0xffff)) { chk -= 65535; }")
474                         write_rule("else if (unlikely(chk < 0)) { chk += 65535; }")
475                         write_rule("ip->check = ~BE16(chk);")
476                     else:
477                         write_rule("ip6->priority = " + str(low_bytes >> 2) + ";")
478                         write_rule("ip6->flow_lbl[0] = (ip6->flow_lbl[0] & 0x3f) | " + str((low_bytes & 3) << 6) + ";")
479             if first_action is not None:
480                 write_rule(first_action)
481             if last_action is not None:
482                 write_rule(last_action)
483             if proto == 6:
484                 rules6 += "\t} while(0);\\\n"
485             else:
486                 rules4 += "\t} while(0);\\\n"
487             rulecnt += 1
488             lastrule = None
489
490     out.write("\n")
491     out.write(f"#define RULECNT {rulecnt}\n")
492     if ratelimitcnt != 0:
493         out.write(f"#define RATE_CNT {ratelimitcnt}\n")
494     if v4persrcratelimitcnt != 0:
495         out.write(f"#define V4_SRC_RATE_CNT {v4persrcratelimitcnt}\n")
496     if v6persrcratelimitcnt != 0:
497         out.write(f"#define V6_SRC_RATE_CNT {v6persrcratelimitcnt}\n")
498     if rules4 != "":
499         out.write("#define NEED_V4_PARSE\n")
500         out.write("#define RULES4 {\\\n" + rules4 + "}\n")
501     if rules6:
502         out.write("#define NEED_V6_PARSE\n")
503         out.write("#define RULES6 {\\\n" + rules6 + "}\n")
504     if args.v6frag == "ignore-parse-if-rule":
505         if use_v6_frags:
506             out.write("#define PARSE_V6_FRAG PARSE\n")