@@ -161,6 +161,22 @@ def parse_priority(self):
161161 E_PARSER , "'{}' is not a valid priority" .format (priority )
162162 )
163163
164+ def parse_cookie (self ):
165+ COOKIE_REGEX = re .compile (
166+ r"^0x[0-9a-fA-F]{1,16}$"
167+ )
168+
169+ cookie = self .args .get ("cookie" )
170+ if cookie is None :
171+ return "0x0"
172+
173+ if not COOKIE_REGEX .match (cookie ):
174+ log_and_raise_error (
175+ E_PARSER , "'{}' is not a valid cookie" .format (cookie )
176+ )
177+
178+ return hex (int (cookie , 16 ))
179+
164180 def read (self , key , parse_fn , dests = None ):
165181 # parse_fn can return a single value or a tuple of values.
166182 # In this case we are expecting dests to match the expected
@@ -303,6 +319,7 @@ def build_rule_string(direction, ofport, args, uplink=False):
303319 rule_parts = {
304320 "priority" : ("priority" , "priority" ),
305321 "protocol" : (None , None ),
322+ "cookie" : ("cookie" , "cookie" ),
306323 "ofport" : ("in_port" , "in_port" ),
307324 "mac" : ("dl_src" , "dl_dst" ),
308325 "iprange" : ("nw_dst" , "nw_src" ),
@@ -318,6 +335,8 @@ def build_rule_string(direction, ofport, args, uplink=False):
318335 if args .get ("priority" ):
319336 rule += "priority={}" .format (args ["priority" ]) + ","
320337 rule += args ["protocol" ]
338+ if args .get ("cookie" ):
339+ rule += ",cookie={}" .format (args ["cookie" ])
321340 if uplink :
322341 rule += ",dl_vlan={}" .format (vlanid )
323342 if ofport :
@@ -342,6 +361,7 @@ def run_ofctl_cmd(cmd, bridge, rule):
342361 % (format (ofctl_cmd ), cmd ["stderr" ]),
343362 )
344363 _LOGGER .info ("Applied rule: {}" .format (ofctl_cmd ))
364+ return cmd
345365
346366
347367@error_wrapped
@@ -358,6 +378,7 @@ def add_rule(_session, args):
358378 parser .read ("port" , parser .parse_port )
359379 parser .read ("allow" , parser .parse_allow )
360380 parser .read ("priority" , parser .parse_priority )
381+ parser .read ("cookie" , parser .parse_cookie )
361382 except XenAPIPlugin .Failure as e :
362383 log_and_raise_error (
363384 E_PARSER , "add_rule: Failed to get parameters: {}" .format (e .params [1 ])
@@ -383,6 +404,14 @@ def add_rule(_session, args):
383404 E_PORTS , "No ports found for bridge: {}" .format (rule_args ["bridge" ])
384405 )
385406
407+ # validate cookie isn't already used
408+ if rule_args ["cookie" ] != "0x0" :
409+ cmd = run_ofctl_cmd ("dump-flows" , "" , "cookie={}/-1" .format (rule_args ["cookie" ]))
410+ if cmd ["stdout" ] != "\n " :
411+ log_and_raise_error (
412+ E_PARAMS , "add_rule: this cookie is already used"
413+ )
414+
386415 # We can now build the open flow rule
387416 rules = build_rules_strings (rule_args )
388417 _LOGGER .info ("Built rules: {}" .format (rules ))
@@ -408,6 +437,7 @@ def del_rule(_session, args):
408437 parser .read ("protocol" , parser .parse_protocol )
409438 parser .read ("iprange" , parser .parse_iprange )
410439 parser .read ("port" , parser .parse_port )
440+ parser .read ("cookie" , parser .parse_cookie )
411441 except XenAPIPlugin .Failure as e :
412442 log_and_raise_error (
413443 E_PARSER , "del_rule: Failed to get parameters: {}" .format (e .params [1 ])
@@ -427,11 +457,21 @@ def del_rule(_session, args):
427457 E_PARAMS , "del_rule: No port provided, tcp and udp requires one"
428458 )
429459
430- update_args_from_ovs (rule_args )
460+ # to match on a cookie, need to specify a mask
461+ rule_args ["cookie" ] = "{}/-1" .format (rule_args ["cookie" ])
431462
432- # We can now build the open flow rule
433- rules = build_rules_strings (rule_args )
434- _LOGGER .info ("Built rules: {}" .format (rules ))
463+ if rule_args ["cookie" ] == "0x0/-1" :
464+ update_args_from_ovs (rule_args )
465+
466+ # We can now build the open flow rule
467+ rules = build_rules_strings (rule_args )
468+ _LOGGER .info ("Built rules: {}" .format (rules ))
469+
470+ else :
471+ # if cookie value is meanful, use it to remove all related rules
472+ rules = [
473+ "cookie={}" .format (rule_args ["cookie" ]),
474+ ]
435475
436476 for rule in rules :
437477 run_ofctl_cmd ("del-flows" , rule_args ["parent-bridge" ], rule )
0 commit comments