[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-API] [PATCH 3 of 4] CP-1548: select network bridge or vswitch backend at runtime



Select Netdev.Bridge or Netdev.Vswitch based on contents of
/etc/xensource/network.conf

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/netdev/netdev.ml
--- a/ocaml/netdev/netdev.ml    Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/netdev/netdev.ml    Tue Dec 15 12:30:48 2009 +0000
@@ -13,6 +13,38 @@
  *)
 open Stringext
 open Forkhelpers
+
+type kind = Bridge | Vswitch
+
+type network_ops = { 
+  kind: kind;
+  add: string -> unit;
+  del: string -> unit;
+  list: unit -> string list;
+
+  exists: string -> bool;
+
+  intf_add: string -> string -> unit;
+  intf_del: string -> string -> unit;
+  intf_list: string -> string list;
+
+  get_bridge: string -> string;
+  is_on_bridge: string -> bool;
+
+  set_forward_delay: string -> int -> unit;
+}
+
+exception Unknown_network_backend of string
+exception Invalid_network_backend_operation of string * kind
+
+let string_of_kind kind = match kind with
+  | Bridge -> "bridge"
+  | Vswitch -> "vswitch"
+
+let kind_of_string s = match s with
+  | "bridge" -> Bridge
+  | "vswitch" -> Vswitch
+  | _ -> raise (Unknown_network_backend s)
 
 module Internal = struct
 
@@ -158,6 +190,25 @@
 
 let get_bridge name = Filename.basename (Unix.readlink ((getpath name 
"brport") ^ "/bridge"))
 
+let ops = {
+  kind = Bridge;
+
+  add = add;
+  del = del;
+  list = list;
+
+  exists = exists;
+
+  intf_add = intf_add;
+  intf_del = intf_del;
+  intf_list = intf_list;
+
+  get_bridge = get_bridge;
+  is_on_bridge = is_on_bridge;
+
+  set_forward_delay = set_forward_delay;
+}
+
 end
 
 module Vswitch = struct
@@ -191,6 +242,25 @@
   | l::[] -> true
   | [] -> false
   | _ -> failwith ("ovs-vsctl port-to-br: returned an unexpected number of 
results for port " ^ name)
+
+let ops = {
+  kind = Vswitch;
+
+  add = add;
+  del = del;
+  list = list;
+
+  exists = exists;
+
+  intf_add = intf_add;
+  intf_del = intf_del;
+  intf_list = intf_list;
+
+  get_bridge = get_bridge;
+  is_on_bridge = is_on_bridge;
+
+  set_forward_delay = fun name v -> raise (Invalid_network_backend_operation 
("set_forward_delay", Vswitch))
+}
 
 end
 
@@ -317,3 +387,17 @@
 
 (** Indicates whether the given interface is a physical interface *)
 let is_physical name = try Unix.access (getpath name "device") [ Unix.F_OK ]; 
true with _ -> false
+
+(* Dispatch network backend operations. *)
+
+let network_config_file = "/etc/xensource/network.conf"
+let network_backend = 
+  try 
+    kind_of_string (String.strip String.isspace 
(Unixext.read_whole_file_to_string network_config_file))
+  with
+  | Unix.Unix_error(Unix.ENOENT, "open", _) -> Bridge
+  | Unix.Unix_error(err, op, path) -> failwith (Printf.sprintf "Unix error: %s 
(%s,%s)\n" (Unix.error_message err) op path)
+
+let network = match network_backend with
+  | Bridge -> Bridge.ops
+  | Vswitch -> Vswitch.ops
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xapi/quicktest.ml
--- a/ocaml/xapi/quicktest.ml   Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xapi/quicktest.ml   Tue Dec 15 12:30:48 2009 +0000
@@ -338,7 +338,7 @@
        let network = Client.VIF.get_network !rpc session_id vif in
        let bridge = Client.Network.get_bridge !rpc session_id network in
        let device = Printf.sprintf "vif%Ld.%s" (Client.VM.get_domid !rpc 
session_id vm) (Client.VIF.get_device !rpc session_id vif) in
-       let devices = Netdev.Bridge.intf_list bridge in
+       let devices = Netdev.network.Netdev.intf_list bridge in
        if not(List.mem device devices) 
        then failed test (Printf.sprintf "Failed to find device %s on bridge %s 
(found [ %s ])" device bridge (String.concat ", " devices))
        else debug test (Printf.sprintf "Device %s is on bridge %s" device 
bridge);
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xapi/vmops.ml
--- a/ocaml/xapi/vmops.ml       Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xapi/vmops.ml       Tue Dec 15 12:30:48 2009 +0000
@@ -90,7 +90,8 @@
 
   Xapi_xenops_errors.handle_xenops_error
     (fun () ->
-       let (_: Device_common.device) = Device.Vif.add ~xs 
~devid:vif_device.Vm_config.devid ~netty:(Netman.Bridge 
vif_device.Vm_config.bridge) 
+       let netty = Netman.netty_of_bridge vif_device.Vm_config.bridge in
+       let (_: Device_common.device) = Device.Vif.add ~xs 
~devid:vif_device.Vm_config.devid ~netty 
         ~mac:vif_device.Vm_config.mac ~mtu:vif_device.Vm_config.mtu 
~rate:vif_device.Vm_config.rate ~protocol:vif_device.Vm_config.protocol 
         ~other_config:vif_device.Vm_config.other_config ~extra_private_keys
         vif_device.Vm_config.domid in
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xapi/xapi_network.ml
--- a/ocaml/xapi/xapi_network.ml        Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xapi/xapi_network.ml        Tue Dec 15 12:30:48 2009 +0000
@@ -25,8 +25,8 @@
 *)
 
 let create_internal_bridge ~bridge =
-  let current = Netdev.Bridge.list () in
-  if not(List.mem bridge current) then Netdev.Bridge.add bridge;
+  let current = Netdev.network.Netdev.list () in
+  if not(List.mem bridge current) then Netdev.network.Netdev.add bridge;
   if not(Netdev.Link.is_up bridge) then Netdev.Link.up bridge
 
 let attach_internal ?(management_interface=false) ~__context ~self () =
@@ -66,14 +66,14 @@
 
 let detach bridge_name = 
   Xapi_network_real.maybe_shutdown_guest_installer_network bridge_name;
-  if Netdev.Bridge.exists bridge_name then begin
+  if Netdev.network.Netdev.exists bridge_name then begin
     List.iter (fun iface ->
                 D.warn "Untracked interface %s exists on bridge %s: deleting" 
iface bridge_name;
                 Netdev.Link.down iface;
-                Netdev.Bridge.intf_del bridge_name iface
-             ) (Netdev.Bridge.intf_list bridge_name);
+                Netdev.network.Netdev.intf_del bridge_name iface
+             ) (Netdev.network.Netdev.intf_list bridge_name);
     Netdev.Link.down bridge_name;
-    Netdev.Bridge.del bridge_name
+    Netdev.network.Netdev.del bridge_name
   end
 
 let attach ~__context ~network ~host = attach_internal ~__context 
~self:network ()
@@ -85,7 +85,7 @@
 let do_bridge_gc rpc session_id =
   let all_networks = Client.Network.get_all_records_where ~rpc ~session_id 
~expr:"true" in
   let db_bridge_names = List.map (fun r->r.API.network_bridge) (List.map snd 
all_networks) in
-  let my_bridges = Netdev.Bridge.list () in
+  let my_bridges = Netdev.network.Netdev.list () in
     List.iter
       (fun mybridge -> if not (List.mem mybridge db_bridge_names) then detach 
mybridge)
       my_bridges
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xapi/xapi_pif.ml
--- a/ocaml/xapi/xapi_pif.ml    Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xapi/xapi_pif.ml    Tue Dec 15 12:30:48 2009 +0000
@@ -442,7 +442,8 @@
   )
     (Db.PIF.get_all_records ~__context)
 
-let start_of_day_best_effort_bring_up() =
+let start_of_day_best_effort_bring_up() = begin
+  debug "Configured network backend: %s" (Netdev.string_of_kind 
Netdev.network.Netdev.kind);
   Server_helpers.exec_with_new_task "Bringing up physical PIFs"
     (fun __context ->
        List.iter
@@ -452,4 +453,4 @@
                 plug ~__context ~self:pif) pif) 
         (calculate_pifs_required_at_start_of_day ~__context)
     )
-
+end
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xe-cli/rt/networks.ml
--- a/ocaml/xe-cli/rt/networks.ml       Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xe-cli/rt/networks.ml       Tue Dec 15 12:30:48 2009 +0000
@@ -55,7 +55,7 @@
 let network_create_destroy cli vmid = 
   let list_bridge () = 
     let ifaces = List.filter Netdev.Link.is_up (Netdev.list ()) in
-    List.filter (fun x -> Netdev.Bridge.exists x) ifaces in
+    List.filter (fun x -> Netdev.network.Netdev.exists x) ifaces in
 
   delete_existing_networks cli;
   
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xenops/netman.ml
--- a/ocaml/xenops/netman.ml    Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xenops/netman.ml    Tue Dec 15 12:30:48 2009 +0000
@@ -14,8 +14,12 @@
 module D = Debug.Debugger(struct let name = "netman" end)
 open D
 
+type netty = Bridge of string | Vswitch of string | DriverDomain | Nat
 
-type netty = Bridge of string | Vswitch of string | DriverDomain | Nat
+let netty_of_bridge bridge = 
+  match Netdev.network.Netdev.kind with
+  | Netdev.Bridge -> (Bridge bridge)
+  | Netdev.Vswitch -> (Vswitch bridge)
 
 let online vif netty =
        let setup_bridge_port dev =
@@ -29,8 +33,8 @@
        match netty with
        | Bridge bridgename ->
                let add_to_bridge br dev =
-                       Netdev.Bridge.set_forward_delay br 0;
-                       Netdev.Bridge.intf_add br dev;
+                       Netdev.network.Netdev.set_forward_delay br 0;
+                       Netdev.network.Netdev.intf_add br dev;
                        Netdev.Link.up dev
                        in
                debug "Adding %s to bridge %s" vif bridgename;
@@ -38,7 +42,7 @@
                add_to_bridge bridgename vif
        | Vswitch bridgename ->
                let add_to_bridge br dev =
-                       Netdev.Vswitch.intf_add br dev;
+                       Netdev.network.Netdev.intf_add br dev;
                        Netdev.Link.up dev
                        in
                debug "Adding %s to bridge %s" vif bridgename;
@@ -52,7 +56,7 @@
        | Bridge bridgename ->
                debug "Removing %s from bridge %s" vif bridgename;
                begin try
-                       Netdev.Bridge.intf_del bridgename vif;
+                       Netdev.network.Netdev.intf_del bridgename vif;
                        Netdev.Link.down vif
                with _ ->
                        warn "interface %s already removed from bridge %s" vif 
bridgename;
@@ -60,7 +64,7 @@
        | Vswitch bridgename ->
                debug "Removing %s from bridge %s" vif bridgename;
                begin try
-                       Netdev.Vswitch.intf_del bridgename vif;
+                       Netdev.network.Netdev.intf_del bridgename vif;
                        Netdev.Link.down vif
                with _ ->
                        warn "interface %s already removed from bridge %s" vif 
bridgename;
diff -r 475dd68da5f8 -r 8f62d72f7fad ocaml/xiu/xiu.ml
--- a/ocaml/xiu/xiu.ml  Tue Dec 15 12:30:48 2009 +0000
+++ b/ocaml/xiu/xiu.ml  Tue Dec 15 12:30:48 2009 +0000
@@ -272,9 +272,9 @@
                                if ty = "vif" then (
                                        let device_path = sprintf 
"/xapi/%s/hotplug/vif/%s/vif" domid id in
                                        let device = xs.Xs.read device_path in
-                                       if Netdev.Bridge.is_on_bridge device 
then begin
-                                         let bridge = Netdev.Bridge.get_bridge 
device in
-                                         Netdev.Bridge.intf_del bridge device
+                                       if Netdev.network.Netdev.is_on_bridge 
device then begin
+                                         let bridge = 
Netdev.network.Netdev.get_bridge device in
+                                         Netdev.network.Netdev.intf_del bridge 
device
                                        end;
                                        eprintf "(XIU) Adding device to free 
list: %s\n" device;
                                        vif_free_list := device :: 
!vif_free_list;

_______________________________________________
xen-api mailing list
xen-api@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/mailman/listinfo/xen-api


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.