setvlan.tcl 4.31 KB
Newer Older
place's avatar
place committed
1 2
#!/usr/local/bin/tclsh

place's avatar
place committed
3
# given the testbed intermediate representation (IR) on stdin,
place's avatar
place committed
4 5 6 7 8
# modify switch config file to contain new vlan information.
# currently, switch config file format is detailed in ~place/vlan/format-iq.

# vlan names must already be known to switch

place's avatar
place committed
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
proc config_switch name {
   global argv0
   switch $name {
      alpha {set filename iqadbad4.p}
      beta  {set filename iqad24c3.p}
      default {
         puts stderr "$argv0 : switch name $name is not valid"
         exit 1
      }
   }

   if {[catch "open $filename r+" iqadfile]} {
      puts stderr "$argv0 $errorInfo"
      exit 1
   }

   # switch_name to stdout
   puts -nonewline "$name VLANs:"

   set iqadcontents [read $iqadfile]
   set DSA [string first "STDALONE" $iqadcontents]
   # DSA == Domain String Address. everything I want is at a fixed offset from
   # this string

   # clear any existing VLAN info
   seek $iqadfile [expr $DSA + 0x22]
   for {set i 0} {$i < 0x1f3} {incr i} {
      puts -nonewline $iqadfile "\0\0\0\0\0\0\0\0"
   }

   seek $iqadfile [expr $DSA + 0x22]

   # for a switch, each line goes as follows:
   # vlan_name port port port port port ...
   while {[gets stdin line] >=0} {
      # comment found
      if {[regexp -nocase {^[ \t]*#} $line]} {continue}
      #empty line
      if {[regexp -nocase {^[ \t]*$} $line]} {continue}

      # "end" of switch config 
      if {[regexp -nocase {^[ \t]*end[ \t]*$} $line]} {break}

      # vlan name is first field
      set line [split [string trim $line]]

      # write NULL-terminated VLAN name
      set vlanname [lindex $line 0]
      puts -nonewline " $vlanname"
      puts -nonewline $iqadfile $vlanname
      puts -nonewline $iqadfile [format "%c" 0]
      #field length 0x14 bytes (-1 for the \0 byte)
      seek $iqadfile [expr 0x13-[string length $vlanname]]  current

      #every port has a magic unique number.
      #they start at 0xc9 for no discernible reason 
      set magicnumber 0xc9

      # port list
      set listsize [llength $line]
      for {set i 1} {$i < $listsize} {incr i} {
         set port [lindex $line $i]
         # split may have given ""s when encountering whitespace
         if {$port != ""} {
            lappend ports $port
         }
      }
      set ports [lsort -integer $ports]
      set listsize [llength $ports]
      for {set i 0} {$i < $listsize} {incr i} {
         set curr [lindex $ports $i]
         if {$curr == ""} {continue}
place's avatar
place committed
81

place's avatar
place committed
82 83 84 85 86 87 88
         scan $curr "%d" portnum
         #one byte for magic number and one for port
         puts -nonewline $iqadfile [format "%c%c" $magicnumber $portnum]
         incr magicnumber
      }
      unset ports
      lappend vlanEndOffsets [expr [tell $iqadfile] -$DSA -0x1A]
place's avatar
place committed
89
   }
place's avatar
place committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

   if {[eof stdin]} {
      error "unexpected EOF in IR during VLAN processing"
      exit 1
   }

   #write config length.
   set configlen [expr [tell $iqadfile] - $DSA - 0x1A]
   seek $iqadfile [expr $DSA + 0x1F]
   puts -nonewline $iqadfile [format "%c" $configlen]

   #write end-of-vlan offsets
   seek $iqadfile [expr $DSA + 0xFEF]

   foreach offset $vlanEndOffsets {
      set offsetstr [format "%c%c" $offset 0]
   # null byte follows each offset
      puts -nonewline $iqadfile $offsetstr
   }
   close $iqadfile
   puts ""
place's avatar
place committed
111 112
}

place's avatar
place committed
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

# MAIN

puts "VLAN configuration\n Switches configured:"

while {[gets stdin line] >=0} {
   # comment
   if {[regexp -nocase {^[ \t]*#} $line]} {continue}
   # empty line
   if {[regexp -nocase {^[ \t]*$} $line]} {continue}

   # look for "START vlan"
   if {[regexp -nocase {^[ \t]*START[ \t]*vlan[ \t]*$} $line]} {

      # look for switches. format is "switch switchname"
      while {[gets stdin line] >=0} {
	 
         # ignore comments
         if {[string index $line 0] == "#"} {continue}

         # exit cleanly if end of VLAN config section reached
         if {[regexp -nocase {^[ \t]*END[ \t]*vlan[ \t]*$} $line]} {exit 0}

         if {[regexp -nocase {^[ \t]*switch.*} $line]} {
            #found switch line (format is "switch switch_name")
            #isolate the switch name
            set switchname [string trim [string range $line \
                [expr [string first "switch" [string tolower $line]] +7] end]]
            string trim $switchname
            config_switch $switchname
         }
      }
      # END vlan never encountered
      error "unexpected EOF in IR during VLAN processing"
      exit 1
   }
place's avatar
place committed
149
}