PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ruby/trema/link.rb

http://github.com/trema/trema
Ruby | 179 lines | 59 code | 27 blank | 93 comment | 6 complexity | 06e975637fbba6d79f74e2747704d508 MD5 | raw file
Possible License(s): GPL-2.0
  1. #
  2. # Author: Yasuhito Takamiya <yasuhito@gmail.com>
  3. #
  4. # Copyright (C) 2008-2012 NEC Corporation
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License, version 2, as
  8. # published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. #
  19. require "trema/network-component"
  20. require "trema/path"
  21. $LOAD_PATH.unshift File.join( Trema.vendor_ruby_ifconfig, "lib" )
  22. require "ifconfig"
  23. module Trema
  24. #
  25. # Network link between hosts and switches.
  26. #
  27. class Link < NetworkComponent
  28. #
  29. # Returns the name of link interface
  30. #
  31. # @example
  32. # link.name => "trema3-0"
  33. #
  34. # @return [String]
  35. #
  36. attr_reader :name
  37. #
  38. # Returns the name of link peer interface
  39. #
  40. # @example
  41. # link.name => "trema3-1"
  42. #
  43. # @return [String]
  44. #
  45. attr_reader :name_peer
  46. #
  47. # Returns the configuration names of link peers
  48. #
  49. # @example
  50. # link.peers => [ "host 0", "switch 1" ]
  51. #
  52. # @return [Array]
  53. #
  54. attr_reader :peers
  55. #
  56. # Creates a new Trema link from {DSL::Link}
  57. #
  58. # @example
  59. # link = Trema::Link.new( stanza )
  60. #
  61. # @return [Link]
  62. #
  63. def initialize stanza
  64. @link_id = Link.instances.size
  65. @stanza = stanza
  66. if real_eth?
  67. @name = real_eth
  68. @name_peer = nil
  69. @peers = @stanza.peers - [ real_eth ]
  70. else
  71. @name = "trema#{ @link_id }-0"
  72. @name_peer = "trema#{ @link_id }-1"
  73. @peers = @stanza.peers
  74. end
  75. Link.add self
  76. end
  77. #
  78. # Adds a virtual link
  79. #
  80. # @example
  81. # link.add!
  82. #
  83. # @return [undefined]
  84. #
  85. def add!
  86. return if real_eth?
  87. sh "sudo ip link add name #{ @name } type veth peer name #{ @name_peer }"
  88. sh "sudo sysctl -w net.ipv6.conf.#{ @name }.disable_ipv6=1 >/dev/null 2>&1"
  89. sh "sudo sysctl -w net.ipv6.conf.#{ @name_peer }.disable_ipv6=1 >/dev/null 2>&1"
  90. end
  91. #
  92. # Ups the peer interfaces of a virtual link
  93. #
  94. # @example
  95. # link.up!
  96. #
  97. # @return [undefined]
  98. #
  99. def up!
  100. return if real_eth?
  101. sh "sudo /sbin/ifconfig #{ @name } up"
  102. sh "sudo /sbin/ifconfig #{ @name_peer } up"
  103. end
  104. #
  105. # Creates and enables a virtual link
  106. #
  107. # @example
  108. # link.enable!
  109. #
  110. # @return [undefined]
  111. #
  112. def enable!
  113. add!
  114. up!
  115. end
  116. #
  117. # Deletes a virtual link
  118. #
  119. # @example
  120. # link.delete!
  121. #
  122. # @return [undefined]
  123. #
  124. def delete!
  125. return if real_eth?
  126. # FIXME: do not rescue nil
  127. sh "sudo ip link delete #{ @name } 2>/dev/null" rescue nil
  128. end
  129. ############################################################################
  130. private
  131. ############################################################################
  132. def real_eth
  133. interfaces = IfconfigWrapper.new.parse.interfaces
  134. @stanza.peers.each do | each |
  135. return each if interfaces.include?( each )
  136. end
  137. raise
  138. end
  139. def real_eth?
  140. interfaces = IfconfigWrapper.new.parse.interfaces
  141. @stanza.peers.each do | each |
  142. return true if interfaces.include?( each )
  143. end
  144. false
  145. end
  146. end
  147. end
  148. ### Local variables:
  149. ### mode: Ruby
  150. ### coding: utf-8-unix
  151. ### indent-tabs-mode: nil
  152. ### End: