- first_action += "\tif (secs != rate->bucket_secs) {\n"
- first_action += "\t\trate->bucket_secs = secs;\n"
- first_action += "\t\trate->bucket_count = 0;\n"
+ first_action += "\tif (likely(rate->sent_bytes > 0)) {\n"
+ first_action += "\t\tint64_t diff = time - rate->sent_time;\n"
+ # Unlikely or not, if the flow is slow, take a perf hit (though with the else if branch it doesn't matter)
+ first_action += "\t\tif (unlikely(diff > 1000000000))\n"
+ first_action += "\t\t\trate->sent_bytes = 0;\n"
+ first_action += "\t\telse if (likely(diff > 0))\n"
+ first_action += f"\t\t\textra_bytes = ((uint64_t)diff) * {math.floor(value)} / 1000000000;\n"
+ first_action += "\t}\n"
+ first_action += "\tif (rate->sent_bytes - ((int64_t)extra_bytes) <= 0) {\n"
+ first_action += "\t\trate->sent_bytes = data_end - pktdata;\n"
+ first_action += "\t\trate->sent_time = time;\n"
+ first_action += "\t\tbpf_spin_unlock(&rate->lock);\n"
+ first_action += "\t} else {\n"
+ first_action += "\t\tbpf_spin_unlock(&rate->lock);\n"
+ first_action += "\t\treturn XDP_DROP;\n"