module D = Debug.Debugger(struct let name="xapi" end)
open D
+let choose_tunnel_device_name ~__context ~host =
+ let pifs = List.filter (fun self -> Db.PIF.get_host ~__context ~self = host) (Db.PIF.get_all ~__context) in
+ let devices = List.map (fun self -> Db.PIF.get_device ~__context ~self) pifs in
+ let rec choose n =
+ let name = Printf.sprintf "tunnel%d" n in
+ if List.mem name devices
+ then choose (n + 1)
+ else name in
+ choose 0
+
let create ~__context ~transport_PIF ~network =
- debug "CREATE TUNNEL";
- Ref.make ()
+ let tunnel = Ref.make () in
+ let access_PIF = Ref.make () in
+ let host = Db.PIF.get_host ~__context ~self:transport_PIF in
+ let device = choose_tunnel_device_name ~__context ~host in
+ let device_name = device in
+ let mAC = Xapi_vif_helpers.gen_mac (0, Uuid.to_string (Uuid.make_uuid ())) in
+ Db.PIF.create ~__context ~ref:access_PIF ~uuid:(Uuid.to_string (Uuid.make_uuid ()))
+ ~device ~device_name ~network ~host ~mAC ~mTU:(-1L) ~vLAN:(-1L) ~metrics:Ref.null
+ ~physical:false ~currently_attached:false
+ ~ip_configuration_mode:`None ~iP:"" ~netmask:"" ~gateway:"" ~dNS:"" ~bond_slave_of:Ref.null
+ ~vLAN_master_of:Ref.null ~management:false ~other_config:[] ~disallow_unplug:false;
+ Db.Tunnel.create ~__context ~ref:tunnel ~uuid:(Uuid.to_string (Uuid.make_uuid ()))
+ ~access_PIF ~transport_PIF ~status:["active", "false"] ~other_config:[];
+ Xapi_pif.plug ~__context ~self:access_PIF;
+ tunnel
let destroy ~__context ~self =
- debug "DESTROY TUNNEL";
- ()
+ let pif = Db.Tunnel.get_access_PIF ~__context ~self in
+ Xapi_pif.unplug ~__context ~self:pif;
+ Db.PIF.destroy ~__context ~self:pif;
+ Db.Tunnel.destroy ~__context ~self