File: switch.ml

package info (click to toggle)
marionnet 0.90.6+bzr508-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,532 kB
  • sloc: ml: 18,130; sh: 5,384; xml: 1,152; makefile: 1,003; ansic: 275
file content (637 lines) | stat: -rw-r--r-- 22,311 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
(* This file is part of Marionnet, a virtual network laboratory
   Copyright (C) 2010  Jean-Vincent Loddo
   Copyright (C) 2010  Université Paris 13

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. *)


(** "Switch" component implementation. *)

#load "where_p4.cmo"
;;

open Gettext

(* Switch related constants: *)
(* TODO: make it configurable! *)
module Const = struct
 let port_no_default = 4
 let port_no_min = 4
 let port_no_max = 16

 let initial_content_for_rcfiles =
"# ===== FAST SPANNING TREE COMMANDS
# fstp/setfstp 0/1             Fast spanning tree protocol 1=ON 0=OFF
# fstp/setedge VLAN PORT 1/0   Define an edge port for a vlan 1=Y 0=N
# fstp/bonus   VLAN PORT COST  set the port bonus for a vlan
# ===== PORT STATUS COMMANDS
# port/sethub  0/1             1=HUB 0=switch
# port/setvlan PORT VLAN       assign PORT to VLAN (untagged port)
# ===== VLAN MANAGEMENT COMMANDS
# vlan/create  VLAN            create the vlan VLAN
# vlan/remove  VLAN            remove the vlan VLAN
# vlan/addport VLAN PORT       add PORT to the VLAN's trunk (tagged)
# vlan/delport VLAN PORT       remove PORT from the VLAN's trunk
" ;;

end

(* The type of data exchanged with the dialog: *)
module Data = struct
type t = {
  name              : string;
  label             : string;
  port_no           : int;
  show_vde_terminal : bool;
  activate_fstp     : bool;
  rc_config         : bool * string; (* run commands (rc) file configuration *)
  old_name          : string;
  }

let to_string t = "<obj>" (* TODO? *)
end (* Data *)

module Make_menus (Params : sig
  val st      : State.globalState
  val packing : [ `toolbar of GButton.toolbar | `menu_parent of Menu_factory.menu_parent ]
 end) = struct

  open Params

  module Toolbar_entry = struct
   let imagefile = "ico.switch.palette.png"
   let tooltip   = (s_ "Switch")
   let packing   = Params.packing
  end

  module Add = struct
    include Data

    let key = Some GdkKeysyms._S

    let ok_callback t = Gui_bricks.Ok_callback.check_name t.name t.old_name st#network#name_exists t

    let dialog () =
      let name = st#network#suggestedName "S" in
      Dialog_add_or_update.make ~title:(s_ "Add switch") ~name ~ok_callback ()

    let reaction
       { name = name; label = label; port_no = port_no;
         show_vde_terminal = show_vde_terminal; activate_fstp = activate_fstp;
         rc_config = rc_config; }
      =
      let action () =
        ignore
          (new User_level_switch.switch
                 ~network:st#network ~name ~label ~port_no ~show_vde_terminal ~activate_fstp ~rc_config ())
      in
      st#network_change action ();

  end

  module Properties = struct
    include Data
    let dynlist () = st#network#get_node_names_that_can_startup ~devkind:`Switch ()

    let dialog name () =
     let d = (st#network#get_node_by_name name) in
     let s = ((Obj.magic d):> User_level_switch.switch) in
     let title = (s_ "Modify switch")^" "^name in
     let label = s#get_label in
     let port_no = s#get_port_no in
     let port_no_min = st#network#port_no_lower_of (s :> User_level.node) in
     let show_vde_terminal = s#get_show_vde_terminal in
     let activate_fstp = s#get_activate_fstp in
     let rc_config = s#get_rc_config in
     Dialog_add_or_update.make
       ~title ~name ~label ~port_no ~port_no_min
       ~show_vde_terminal ~activate_fstp ~rc_config
       ~ok_callback:Add.ok_callback ()

    let reaction { name = name; label = label; port_no = port_no;
                   old_name = old_name;
                   show_vde_terminal = show_vde_terminal;
                   activate_fstp = activate_fstp;
                   rc_config = rc_config }
      =
      let d = (st#network#get_node_by_name old_name) in
      let s = ((Obj.magic d):> User_level_switch.switch) in
      let action () = s#update_switch_with ~name ~label ~port_no ~show_vde_terminal ~activate_fstp ~rc_config in
      st#network_change action ();

  end

  module Remove = struct
    type t = string (* just the name *)
    let to_string = (Printf.sprintf "name = %s\n")

    let dynlist = Properties.dynlist

    let dialog name () =
      Gui_bricks.Dialog.yes_or_cancel_question
        ~title:(s_ "Remove")
        ~markup:(Printf.sprintf (f_ "Are you sure that you want to remove %s\nand all the cables connected to this %s?") name (s_ "switch"))
        ~context:name
        ()

    let reaction name =
      let d = (st#network#get_node_by_name name) in
      let h = ((Obj.magic d):> User_level_switch.switch) in
      let action () = h#destroy in
      st#network_change action ();

  end

  module Startup = struct
    type t = string (* just the name *)
    let to_string = (Printf.sprintf "name = %s\n")
    let dynlist    = Properties.dynlist
    let dialog     = Menu_factory.no_dialog_but_simply_return_name
    let reaction name = (st#network#get_node_by_name name)#startup

  end

  module Stop = struct
    type t = string (* just the name *)
    let to_string = (Printf.sprintf "name = %s\n")
    let dynlist () = st#network#get_node_names_that_can_gracefully_shutdown ~devkind:`Switch ()
    let dialog = Menu_factory.no_dialog_but_simply_return_name
    let reaction name = (st#network#get_node_by_name name)#gracefully_shutdown

  end

  module Suspend = struct
    type t = string (* just the name *)
    let to_string = (Printf.sprintf "name = %s\n")
    let dynlist () = st#network#get_node_names_that_can_suspend ~devkind:`Switch ()
    let dialog = Menu_factory.no_dialog_but_simply_return_name
    let reaction name = (st#network#get_node_by_name name)#suspend

  end

  module Resume = struct
    type t = string (* just the name *)
    let to_string = (Printf.sprintf "name = %s\n")
    let dynlist () = st#network#get_node_names_that_can_resume ~devkind:`Switch ()
    let dialog = Menu_factory.no_dialog_but_simply_return_name
    let reaction name = (st#network#get_node_by_name name)#resume

  end

 module Create_entries =
  Gui_toolbar_COMPONENTS_layouts.Layout_for_network_node (Params) (Toolbar_entry) (Add) (Properties) (Remove) (Startup) (Stop) (Suspend) (Resume)

 (* Subscribe this kind of component to the network club: *)
 st#network#subscribe_a_try_to_add_procedure Eval_forest_child.try_to_add_switch;

end

(*-----*)
  WHERE
(*-----*)

module Dialog_add_or_update = struct

(* This function may be useful for testing the widget creation without
   recompiling the whole project. *)
let make
 ?(title="Add switch")
 ?(name="")
 ?label
 ?(port_no=Const.port_no_default)
 ?(port_no_min=Const.port_no_min)
 ?(port_no_max=Const.port_no_max)
 ?(show_vde_terminal=false)
 ?(activate_fstp=false)
 ?(rc_config=(false, Const.initial_content_for_rcfiles))
 ?(help_callback=help_callback) (* defined backward with "WHERE" *)
 ?(ok_callback=(fun data -> Some data))
 ?(dialog_image_file=Initialization.Path.images^"ico.switch.dialog.png")
 () :'result option =
  let old_name = name in
  let (dialog_switch,_,name,label) =
    Gui_bricks.Dialog_add_or_update.make_window_image_name_and_label
      ~title
      ~image_file:dialog_image_file
      ~image_tooltip:(s_ "Switch")
      ~name
      ~name_tooltip:(s_ "Switch name. This name must be unique in the virtual network. Suggested: S1, S2, ...")
      ?label
      ()
  in
  let (port_no, show_vde_terminal, activate_fstp, rc_config) =
    let vbox = GPack.vbox ~homogeneous:false ~border_width:20 ~spacing:10 ~packing:dialog_switch#vbox#add () in
    let form =
      Gui_bricks.make_form_with_labels
        ~packing:vbox#add
        [(s_ "Ports number");
         (s_ "Show VDE terminal");
         (s_ "Activate FSTP");
         (s_ "Startup configuration");
         ]
    in
    let port_no =
      Gui_bricks.spin_byte
        ~packing:(form#add_with_tooltip (s_ "Switch ports number"))
        ~lower:port_no_min ~upper:port_no_max ~step_incr:2
        port_no
    in
    let show_vde_terminal =
      GButton.check_button
        ~active:show_vde_terminal
        ~packing:(form#add_with_tooltip (s_ "Check to access the switch through a terminal" ))
        ()
    in
    let activate_fstp =
      GButton.check_button
        ~active:activate_fstp
        ~packing:(form#add_with_tooltip (s_ "Check to activate the FSTP (Fast Spanning Tree Protocol)" ))
        ()
    in
    let rc_config =
       Gui_bricks.make_rc_config_widget 
         ~filter_names:[`CONF; `RC; `ALL] 
         ~parent:(dialog_switch :> GWindow.window_skel)
         ~packing:(form#add_with_tooltip (s_ "Check to activate a startup configuration" )) 
         ~active:(fst rc_config)
         ~content:(snd rc_config)
         ~device_name:(old_name)
         ~language:("vde_switch") (* special syntax *)
         ()
    in
    (port_no, show_vde_terminal, activate_fstp, rc_config)
  in
  (* --- *)
  let get_widget_data () :'result =
    let name = name#text in
    let label = label#text in
    let port_no = int_of_float port_no#value in
    let show_vde_terminal = show_vde_terminal#active in
    let rc_config = (rc_config#active, rc_config#content) in
    let activate_fstp = activate_fstp#active in
      { Data.name = name;
        Data.label = label;
        Data.port_no = port_no;
        Data.show_vde_terminal = show_vde_terminal;
        Data.activate_fstp = activate_fstp;
        Data.rc_config = rc_config;
        Data.old_name = old_name;
        }
  in
  (* The result of make is the result of the dialog loop (of type 'result option): *)
  Gui_bricks.Dialog_run.ok_or_cancel (dialog_switch) ~ok_callback ~help_callback ~get_widget_data ()

(*-----*)
  WHERE
(*-----*)

 let help_callback =
   let title = (s_ "ADD OR MODIFY A SWITCH") in
   let msg   = (s_ "\
In this dialog window you can define the name of an Ethernet switch \
and set parameters for it:\n\n\
- Label: a string appearing near the switch icon in the network graph; it may \
allow, for example, to know at a glance the Ethernet network realized by the device; \
this field is exclusively for graphic purposes, is not taken in consideration \
for the configuration.\n\n\
- Nb of Ports: the number of ports of the switch (default 4); this number must \
not be increased without a reason, because the number of processes needed for the \
device emulation is proportional to his ports number.")
   in Simple_dialogs.help title msg

end

(*-----*)
  WHERE
(*-----*)

module Eval_forest_child = struct

 let try_to_add_switch (network:User_level.network) ((root,children):Xforest.tree) =
  try
   (match root with
    | ("switch", attrs) ->
    	let name  = List.assoc "name" attrs in
	let port_no = int_of_string (List.assoc "port_no" attrs) in
        Log.printf2 "Importing switch \"%s\" with %d ports...\n" name port_no;
	let x = new User_level_switch.switch ~network ~name ~port_no () in
	x#from_tree ("switch", attrs) children;
        Log.printf1 "Switch \"%s\" successfully imported.\n" name;
        true

    (* backward compatibility *)
    | ("device", attrs) ->
	let name  = List.assoc "name" attrs in
	let port_no = try int_of_string (List.assoc "eth" attrs) with _ -> Const.port_no_default in
	let kind = List.assoc "kind" attrs in
	(match kind with
	| "switch" ->
            Log.printf2 "Importing switch \"%s\" with %d ports...\n" name port_no;
	    let x = new User_level_switch.switch ~network ~name ~port_no () in
	    x#from_tree ("device", attrs) children; (* Just for the label... *)
            Log.printf "This is an old project: we set the user port offset to 1...\n";
	    network#defects#change_port_user_offset ~device_name:name ~user_port_offset:1;
	    Log.printf1 "Switch \"%s\" successfully imported.\n" name;
	    true
	| _ -> false
	)
   | _ -> false
   )
  with _ -> false

end (* module Eval_forest_child *)


(*-----*)
  WHERE
(*-----*)


module User_level_switch = struct

class switch =

 fun ~network
     ~name
     ?label
     ~port_no
     ?(show_vde_terminal=false)
     ?(activate_fstp=false)
     ?(rc_config=(false,""))
     () ->
  object (self) inherit OoExtra.destroy_methods ()

  inherit
    User_level.node_with_ledgrid_and_defects
      ~network
      ~name ?label ~devkind:`Switch
      ~port_no
      ~port_no_min:Const.port_no_min
      ~port_no_max:Const.port_no_max
      ~user_port_offset:1 (* in order to have a perfect mapping with VDE *)
      ~port_prefix:"port"
      ()
    as self_as_node_with_ledgrid_and_defects
    
  method ledgrid_label = "Switch"
  method defects_device_type = "switch"
  method polarity = User_level.MDI_X
  method string_of_devkind = "switch"

  val mutable show_vde_terminal : bool = show_vde_terminal
  method get_show_vde_terminal = show_vde_terminal
  method set_show_vde_terminal x = show_vde_terminal <- x

  val mutable activate_fstp : bool = activate_fstp
  method get_activate_fstp  = activate_fstp
  method set_activate_fstp x = activate_fstp <- x

  val mutable rc_config : bool * string  = rc_config
  method get_rc_config = rc_config
  method set_rc_config x = rc_config <- x

  method dotImg iconsize =
   let imgDir = Initialization.Path.images in
   (imgDir^"ico.switch."^(self#string_of_simulated_device_state)^"."^iconsize^".png")

  method update_switch_with ~name ~label ~port_no
   ~show_vde_terminal ~activate_fstp ~rc_config
   =
   (* The following call ensure that the simulated device will be destroyed: *)
   self_as_node_with_ledgrid_and_defects#update_with ~name ~label ~port_no;
   self#set_show_vde_terminal (show_vde_terminal);
   self#set_activate_fstp (activate_fstp);
   self#set_rc_config (rc_config);

  (** Create the simulated device *)
  method private make_simulated_device =
    let hublet_no = self#get_port_no in
    let show_vde_terminal = self#get_show_vde_terminal in
    let fstp = Option.of_bool (self#get_activate_fstp) in
    let rcfile_content =
      match self#get_rc_config with
      | false, _ -> None
      | true, content -> Some content
    in
    let unexpected_death_callback = self#destroy_because_of_unexpected_death in
    ((new Simulation_level_switch.switch
       ~parent:self
       ~hublet_no          (* TODO: why not accessible from parent? *)
       ~show_vde_terminal  (* TODO: why not accessible from parent? *)
       ?fstp
       ?rcfile_content
       ~working_directory:(network#working_directory)
       ~unexpected_death_callback
       ()) :> User_level.node Simulation_level.device)

  method to_tree =
   Forest.tree_of_leaf ("switch", [
      ("name"     ,  self#get_name );
      ("label"    ,  self#get_label);
      ("port_no"  ,  (string_of_int self#get_port_no))  ;
      ("show_vde_terminal" , string_of_bool (self#get_show_vde_terminal));
      ("activate_fstp"     , string_of_bool (self#get_activate_fstp));
      ("rc_config"         , Marshal.to_string self#get_rc_config []);
      ])

  method eval_forest_attribute = function
  | ("name"     , x ) -> self#set_name x
  | ("label"    , x ) -> self#set_label x
  | ("port_no"  , x ) -> self#set_port_no (int_of_string x)
  | ("show_vde_terminal", x ) -> self#set_show_vde_terminal (bool_of_string x)
  | ("activate_fstp", x )     -> self#set_activate_fstp (bool_of_string x)
  | ("rc_config", x )         -> self#set_rc_config (Marshal.from_string x 0)
  | _ -> () (* Forward-comp. *)

end (* class switch *)

end (* module User_level *)

(*-----*)
  WHERE
(*-----*)

module Simulation_level_switch = struct

(* The question is "port/print" *)
let scan_vde_switch_answer_to_port_print (ch:Network.stream_channel) : int =
  let rec loop n =
    let answer = ch#input_line () in
    try (Scanf.sscanf answer "Port %d %s ACTIVE") (fun i _ -> ()); loop (n+1) with _ ->
    try (Scanf.sscanf answer ".") (); n with _ -> loop n
  in
  loop 0

let ask_vde_switch_for_current_active_ports ~socketfile () =
  let protocol (ch:Network.stream_channel) =
    ch#output_line "port/print";
    scan_vde_switch_answer_to_port_print ch
  in
  Network.stream_unix_client ~socketfile ~protocol ()

let wait_vde_switch_until_ports_will_be_allocated ~numports ~socketfile () =
  let rec protocol (ch:Network.stream_channel) =
    ch#output_line "port/print";
    let active_ports = scan_vde_switch_answer_to_port_print ch in
    if active_ports >= numports then active_ports else (Thread.delay 0.2; (protocol ch))
  in
  Network.stream_unix_client ~socketfile ~protocol ()

(*let send_commands_to_vde_switch ~socketfile ~commands () =
  Log.printf "Sending commands to a switch:\n---\n%s\n---\n" commands;
  let protocol (ch:Network.stream_channel) = ch#send commands in
  Network.stream_unix_client ~socketfile ~protocol ()*)

let get_lines_removing_comments (commands:string) : string list =
  let t = StringExtra.Text.of_string commands in
  let result = StringExtra.Text.grep (Str.regexp "^[^#]") t in
  result

(* Currently unused, but useful for testing: *)
let get_vde_switch_boolean_answer (ch:Network.stream_channel) : bool =
  let ignore2 _ _ = () in
  let rec loop () =
    Log.printf "Waiting for an answer...\n";
    let answer = ch#input_line () in
    Log.printf1 "Received answer `%s'\n" answer;
    try (Scanf.sscanf answer "vde$ 1000 Success" ()); true with _ ->
    try (Scanf.sscanf answer "vde$ %d %s" ignore2); false with _ ->
    loop ()
  in
  loop ()

(* Currently unused, but useful for testing: *)
let send_commands_to_vde_switch_and_get_answers ~socketfile ~commands ()
  : (exn, (string * bool) list) Either.t
  =
  let lines = get_lines_removing_comments commands in
  Log.printf1 "Sending commands to a switch:\n---\n%s\n---\n" commands;
  let protocol (ch:Network.stream_channel) =
    List.map
       (fun line ->
          Log.printf1 "Sending line: %s\n" line;
          ch#output_line line;
          let answer = get_vde_switch_boolean_answer ch in
          Log.printf1 "Received boolean answer: %b\n" (answer);
          (line, answer))
       lines
  in
  Network.stream_unix_client ~socketfile ~protocol ()

let rec repeat_until_exception f x =
 try ignore (f x); repeat_until_exception f x with _ -> ()

let send_commands_to_vde_switch_ignoring_answers ~socketfile ~commands () =
  let lines = get_lines_removing_comments commands in
  let protocol (ch:Network.stream_channel) =
    ignore (Thread.create (repeat_until_exception ch#input_line) ());
    List.iter
       (fun line ->
          Log.printf1 "Sending line: %s\n" line;
          ch#output_line line;
          Thread.delay 0.01;
          ())
       lines
  in
  Network.stream_unix_client ~socketfile ~protocol ()


(** A switch: just a [hub_or_switch] with [hub = false] *)
class ['parent] switch =
  fun ~(parent:'parent)
      ~hublet_no
      ?(last_user_visible_port_index:int option)
      ?(show_vde_terminal=false)
      ?fstp
      ?rcfile (* Unused: vde_switch doesn't interpret correctly commands provided in this way! *)
      ?rcfile_content
      ~working_directory
      ~unexpected_death_callback
      () ->
object(self)
  inherit ['parent] Simulation_level.hub_or_switch
      ~parent
      ~hublet_no
      ?last_user_visible_port_index
      ~hub:false
      ~management_socket:()
      ?fstp
      ?rcfile
      ~working_directory
      ~unexpected_death_callback
      ()
      as super
  method device_type = "switch"

  method spawn_internal_cables =
    match show_vde_terminal || (rcfile_content <> None) with
    | false -> super#spawn_internal_cables
    | true ->
        (* If the user want to configure VLANs etc, we must be sure that
           the port numbering will be the same for marionnet and vde_switch: *)
        let socketfile = Option.extract self#get_management_socket_name in
        let numports = ref (Either.extract (ask_vde_switch_for_current_active_ports ~socketfile ())) in
        let name = parent#get_name in
        Log.printf2 "The vde_switch %s has currently %d active ports.\n" name !numports;
        Log.printf1 "Spawning internal cables for switch %s...\n" name;
	List.iter (fun thunk -> thunk ())
	  (List.map (* Here map returns a list of thunks *)
	     begin fun internal_cable_process () ->
	       (* The protocol implemented here should ensure that vde_switch will not be solicited
	          before having accepted the previously asked connection. However, for safety we add
	          a little delay in order to give to vde_switch the time to allocate the previous port: *)
	       Thread.delay 0.1;
	       (* Now we launch the process that will ask vde_switch to obtain a new port: *)
	       internal_cable_process#spawn;
	       incr numports;
	       let answer =
	         Either.extract (wait_vde_switch_until_ports_will_be_allocated ~numports:(!numports) ~socketfile ())
	       in
	       (if answer <> !numports then 
	          Log.printf3 "Unexpected vde_switch %s answer: %d instead of the expected value %d. Ignoring.\n" name answer !numports
	        );
	       Log.printf2 "Ok, the vde_switch %s has now %d allocated ports.\n" name !numports;
	       end
 	     self#get_internal_cable_processes);
 	(* Now send rc commands to the switch: *)
        match rcfile_content with
        | None -> ()
        | Some commands ->
            ignore
              (Thread.create
                 (send_commands_to_vde_switch_ignoring_answers ~socketfile ~commands)              ())


  initializer

  match show_vde_terminal with
  | false -> ()
  | true ->
    let name = parent#get_name in
    self#add_accessory_process
      (new Simulation_level.unixterm_process
        ~xterm_title:(name^" terminal")
        ~management_socket_name:(Option.extract self#get_management_socket_name)
 	~unexpected_death_callback:
 	   (fun i _ ->
 	      Death_monitor.stop_monitoring i;
 	      Log.printf2 "Terminal of switch %s closed (pid %d).\n" name i)
	())

end;;

end (* module Simulation_level_switch *)

(** Just for testing: *)
let test = Dialog_add_or_update.make