trema 0.2.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. data/.gitmodules +3 -0
  2. data/.travis.yml +13 -0
  3. data/.yardopts +4 -0
  4. data/Gemfile +2 -3
  5. data/README.md +43 -97
  6. data/Rakefile +60 -4
  7. data/Rantfile +11 -10
  8. data/cruise.rb +4 -6
  9. data/features/example.packetin_filter_config.feature +10 -10
  10. data/features/example.switch_monitor.feature +14 -2
  11. data/features/step_definitions/kill_steps.rb +2 -2
  12. data/features/step_definitions/{off_steps.rb → killall_steps.rb} +2 -2
  13. data/features/step_definitions/misc_steps.rb +1 -1
  14. data/features/step_definitions/up_steps.rb +30 -0
  15. data/features/trema.feature +1 -0
  16. data/features/trema.run.feature +1 -0
  17. data/locale/README.ja.md +19 -0
  18. data/locale/ja/yard.po +3762 -0
  19. data/locale/yard.pot +3740 -0
  20. data/ruby/extconf.rb +4 -1
  21. data/ruby/trema/action-common.c +3 -17
  22. data/ruby/trema/action-common.h +3 -7
  23. data/ruby/trema/action.rb +33 -0
  24. data/ruby/trema/app.rb +1 -1
  25. data/ruby/trema/barrier-request.c +1 -0
  26. data/ruby/trema/command/run.rb +13 -9
  27. data/ruby/trema/command/usage.rb +1 -0
  28. data/ruby/trema/command/version.rb +1 -1
  29. data/ruby/trema/controller.c +133 -50
  30. data/ruby/trema/controller.rb +2 -2
  31. data/ruby/trema/desc-stats-reply.rb +77 -0
  32. data/ruby/trema/dsl/configuration.rb +3 -14
  33. data/ruby/trema/dsl/rswitch.rb +47 -0
  34. data/ruby/trema/dsl/runner.rb +4 -1
  35. data/ruby/trema/dsl/syntax.rb +11 -8
  36. data/ruby/trema/echo-reply.c +59 -45
  37. data/ruby/trema/echo-reply.h +1 -3
  38. data/ruby/trema/echo-request.c +49 -71
  39. data/ruby/trema/echo-request.h +0 -2
  40. data/ruby/trema/echo.c +99 -0
  41. data/ruby/trema/{action-enqueue.h → echo.h} +6 -7
  42. data/ruby/trema/enqueue.rb +87 -0
  43. data/ruby/trema/error.c +109 -104
  44. data/ruby/trema/error.h +0 -2
  45. data/ruby/trema/features-reply.c +89 -35
  46. data/ruby/trema/features-reply.h +0 -6
  47. data/ruby/trema/features-request.c +63 -37
  48. data/ruby/trema/features-request.h +0 -2
  49. data/ruby/trema/flow-mod.c +149 -0
  50. data/ruby/trema/{action-output.h → flow-mod.h} +6 -6
  51. data/ruby/trema/get-config-request.c +1 -0
  52. data/ruby/trema/hardware-switch.rb +88 -0
  53. data/ruby/trema/hello.c +55 -31
  54. data/ruby/trema/hello.h +0 -2
  55. data/ruby/trema/ip.rb +12 -2
  56. data/ruby/trema/logger.rb +29 -0
  57. data/ruby/trema/mac.rb +57 -36
  58. data/ruby/trema/match.c +7 -9
  59. data/ruby/trema/monkey-patch/integer/ranges.rb +0 -2
  60. data/ruby/trema/network-component.rb +1 -1
  61. data/ruby/trema/open-vswitch.rb +2 -2
  62. data/ruby/trema/openflow-switch.rb +3 -54
  63. data/ruby/trema/{packet_in.c → packet-in.c} +262 -175
  64. data/ruby/trema/{packet_in.h → packet-in.h} +0 -2
  65. data/ruby/trema/packet-queue.rb +4 -3
  66. data/ruby/trema/port-mod.c +8 -9
  67. data/ruby/trema/port-status-add.rb +60 -0
  68. data/ruby/trema/port-status-delete.rb +60 -0
  69. data/ruby/trema/port-status-modify.rb +60 -0
  70. data/ruby/trema/port-status.c +48 -15
  71. data/ruby/trema/port-status.h +6 -8
  72. data/ruby/trema/port.c +63 -8
  73. data/ruby/trema/queue-get-config-request.c +1 -0
  74. data/ruby/trema/ruby-switch.rb +62 -0
  75. data/ruby/trema/send-out-port.rb +97 -0
  76. data/ruby/trema/set-config.c +1 -0
  77. data/ruby/trema/set-eth-addr.rb +45 -0
  78. data/ruby/trema/set-eth-dst-addr.rb +54 -0
  79. data/ruby/trema/set-eth-src-addr.rb +54 -0
  80. data/ruby/trema/set-ip-addr.rb +47 -0
  81. data/ruby/trema/set-ip-dst-addr.rb +53 -0
  82. data/ruby/trema/set-ip-src-addr.rb +52 -0
  83. data/ruby/trema/set-ip-tos.rb +63 -0
  84. data/ruby/trema/set-transport-dst-port.rb +53 -0
  85. data/ruby/trema/set-transport-port.rb +52 -0
  86. data/ruby/trema/set-transport-src-port.rb +54 -0
  87. data/ruby/trema/set-vlan-priority.rb +65 -0
  88. data/ruby/trema/set-vlan-vid.rb +64 -0
  89. data/ruby/trema/shell/down.rb +1 -1
  90. data/ruby/trema/shell/link.rb +4 -4
  91. data/ruby/trema/shell/run.rb +8 -6
  92. data/ruby/trema/shell/up.rb +3 -3
  93. data/ruby/trema/stats-reply.c +27 -2
  94. data/ruby/trema/stats-request.c +64 -23
  95. data/ruby/trema/strip-vlan-header.rb +41 -0
  96. data/ruby/trema/switch.c +196 -0
  97. data/ruby/trema/{action-vendor.h → switch.h} +5 -7
  98. data/ruby/trema/switch.rb +28 -9
  99. data/ruby/trema/trema-ruby-utils.c +66 -0
  100. data/ruby/trema/{action-set-dl-src.h → trema-ruby-utils.h} +9 -11
  101. data/ruby/trema/trema.c +61 -61
  102. data/ruby/trema/vendor-action.rb +73 -0
  103. data/ruby/trema/vendor.c +121 -52
  104. data/ruby/trema/vendor.h +6 -10
  105. data/ruby/trema/version.rb +1 -1
  106. data/spec/spec_helper.rb +1 -1
  107. data/spec/support/action.rb +52 -0
  108. data/spec/support/mandatory-option.rb +56 -0
  109. data/spec/support/openflow-message.rb +91 -7
  110. data/spec/support/port-status.rb +38 -0
  111. data/spec/trema/controller_spec.rb +0 -26
  112. data/spec/trema/dsl/configuration_spec.rb +3 -3
  113. data/spec/trema/dsl/runner_spec.rb +12 -32
  114. data/spec/trema/dsl/syntax_spec.rb +2 -11
  115. data/spec/trema/echo-reply_spec.rb +49 -14
  116. data/spec/trema/echo-request_spec.rb +86 -34
  117. data/spec/trema/enqueue_spec.rb +76 -0
  118. data/spec/trema/error_spec.rb +43 -58
  119. data/spec/trema/features-reply_spec.rb +58 -24
  120. data/spec/trema/features-request_spec.rb +54 -28
  121. data/spec/trema/flow-mod_spec.rb +99 -0
  122. data/spec/trema/{openflow-switch_spec.rb → hardware-switch_spec.rb} +3 -3
  123. data/spec/trema/hello_spec.rb +28 -14
  124. data/spec/trema/ip_spec.rb +54 -0
  125. data/spec/trema/mac_spec.rb +49 -64
  126. data/spec/trema/match_spec.rb +1 -1
  127. data/spec/trema/open-vswitch_spec.rb +7 -7
  128. data/spec/trema/packet-in_spec.rb +73 -16
  129. data/spec/trema/port-status-add_spec.rb +32 -0
  130. data/spec/trema/port-status-delete_spec.rb +32 -0
  131. data/spec/trema/port-status-modify_spec.rb +71 -0
  132. data/spec/trema/port-status_spec.rb +5 -76
  133. data/spec/trema/{action-output_spec.rb → send-out-port_spec.rb} +20 -47
  134. data/spec/trema/set-eth-dst-addr_spec.rb +75 -0
  135. data/spec/trema/set-eth-src-addr_spec.rb +72 -0
  136. data/spec/trema/set-ip-dst-addr_spec.rb +58 -0
  137. data/spec/trema/set-ip-src-addr_spec.rb +58 -0
  138. data/spec/trema/set-ip-tos_spec.rb +65 -0
  139. data/spec/trema/set-transport-dst-port_spec.rb +65 -0
  140. data/spec/trema/set-transport-src-port_spec.rb +65 -0
  141. data/spec/trema/set-vlan-priority_spec.rb +65 -0
  142. data/spec/trema/set-vlan-vid_spec.rb +65 -0
  143. data/spec/trema/shell/vhost_spec.rb +4 -1
  144. data/spec/trema/shell/vswitch_spec.rb +11 -11
  145. data/spec/trema/stats-reply_spec.rb +59 -13
  146. data/spec/trema/stats-request_spec.rb +6 -0
  147. data/spec/trema/{action-strip-vlan_spec.rb → strip-vlan-header_spec.rb} +3 -17
  148. data/spec/trema/switch-daemon_spec.rb +39 -0
  149. data/spec/trema/vendor-action_spec.rb +81 -0
  150. data/spec/trema/vendor_spec.rb +76 -0
  151. data/spec/trema_spec.rb +56 -0
  152. data/src/examples/dumper/dumper.c +0 -8
  153. data/src/examples/dumper/dumper.rb +52 -52
  154. data/src/examples/hello_trema/hello_trema.c +0 -2
  155. data/src/examples/learning_switch/learning-switch.rb +3 -3
  156. data/src/examples/multi_learning_switch/multi-learning-switch.rb +3 -3
  157. data/src/examples/openflow_message/features-request.rb +3 -3
  158. data/src/examples/packetin_filter_config/utils.c +4 -4
  159. data/src/examples/repeater_hub/repeater-hub.rb +3 -3
  160. data/src/examples/switch_info/switch_info.rb +2 -2
  161. data/src/examples/switch_monitor/switch_monitor.c +1 -1
  162. data/src/examples/traffic_monitor/traffic-monitor.rb +3 -3
  163. data/src/lib/arp.h +4 -1
  164. data/src/lib/chibach.c +391 -0
  165. data/src/lib/chibach.h +71 -0
  166. data/src/lib/chibach_private.c +170 -0
  167. data/src/lib/chibach_private.h +52 -0
  168. data/src/lib/ether.c +2 -1
  169. data/src/lib/ether.h +0 -1
  170. data/src/lib/ipv4.h +13 -14
  171. data/{ruby/trema/action-set-nw-src.h → src/lib/ipv6.h} +13 -9
  172. data/src/lib/log.c +161 -58
  173. data/src/lib/log.h +11 -16
  174. data/src/lib/messenger.c +36 -1
  175. data/src/lib/messenger.h +1 -0
  176. data/src/lib/openflow_application_interface.c +128 -28
  177. data/src/lib/openflow_application_interface.h +31 -6
  178. data/src/lib/openflow_message.c +2 -1
  179. data/src/lib/openflow_service_interface.h +1 -0
  180. data/src/lib/openflow_switch_interface.c +1380 -0
  181. data/src/lib/openflow_switch_interface.h +264 -0
  182. data/src/lib/packet_info.c +94 -11
  183. data/src/lib/packet_info.h +22 -3
  184. data/src/lib/packet_parser.c +38 -2
  185. data/src/lib/secure_channel.c +498 -0
  186. data/{ruby/trema/vendor-request.h → src/lib/secure_channel.h} +11 -10
  187. data/src/lib/tcp.h +0 -3
  188. data/src/lib/trema.c +38 -5
  189. data/{ruby/trema/action-set-nw-dst.h → src/lib/trema.hpp} +17 -8
  190. data/src/lib/trema_wrapper.c +5 -0
  191. data/src/lib/trema_wrapper.h +4 -0
  192. data/src/lib/utility.c +93 -4
  193. data/src/lib/utility.h +1 -0
  194. data/src/lib/wrapper.c +30 -7
  195. data/src/lib/wrapper.h +2 -0
  196. data/src/switch_manager/ofpmsg_recv.c +44 -30
  197. data/src/switch_manager/ofpmsg_send.c +40 -1
  198. data/src/switch_manager/ofpmsg_send.h +2 -0
  199. data/src/switch_manager/switch.c +153 -8
  200. data/src/switch_manager/switch.h +1 -0
  201. data/src/switch_manager/switchinfo.h +5 -0
  202. data/src/tremashark/README +2 -2
  203. data/src/tremashark/plugin/packet-trema/packet-trema.c +1 -0
  204. data/trema +1 -1
  205. data/unittests/lib/log_test.c +158 -34
  206. data/unittests/lib/openflow_application_interface_test.c +252 -69
  207. data/unittests/lib/openflow_message_test.c +3 -1
  208. data/unittests/lib/packet_parser_test.c +60 -15
  209. data/unittests/lib/test_packets/icmp6_echo_rep.cap +0 -0
  210. data/unittests/lib/test_packets/icmp6_echo_req.cap +0 -0
  211. data/unittests/lib/trema_test.c +2 -0
  212. data/unittests/lib/utility_test.c +65 -2
  213. data/unittests/lib/wrapper_test.c +29 -0
  214. data/vendor/{README → README.md} +2 -12
  215. data/vendor/packet-openflow.diff +13 -0
  216. metadata +86 -53
  217. data/GPL2 +0 -339
  218. data/ruby/trema/action-enqueue.c +0 -161
  219. data/ruby/trema/action-output.c +0 -169
  220. data/ruby/trema/action-set-dl-dst.c +0 -131
  221. data/ruby/trema/action-set-dl-dst.h +0 -44
  222. data/ruby/trema/action-set-dl-src.c +0 -131
  223. data/ruby/trema/action-set-nw-dst.c +0 -135
  224. data/ruby/trema/action-set-nw-src.c +0 -140
  225. data/ruby/trema/action-set-nw-tos.c +0 -124
  226. data/ruby/trema/action-set-nw-tos.h +0 -42
  227. data/ruby/trema/action-set-tp-dst.c +0 -122
  228. data/ruby/trema/action-set-tp-dst.h +0 -42
  229. data/ruby/trema/action-set-tp-src.c +0 -124
  230. data/ruby/trema/action-set-tp-src.h +0 -42
  231. data/ruby/trema/action-set-vlan-pcp.c +0 -128
  232. data/ruby/trema/action-set-vlan-pcp.h +0 -42
  233. data/ruby/trema/action-set-vlan-vid.c +0 -125
  234. data/ruby/trema/action-set-vlan-vid.h +0 -42
  235. data/ruby/trema/action-strip-vlan.c +0 -81
  236. data/ruby/trema/action-strip-vlan.h +0 -42
  237. data/ruby/trema/action-vendor.c +0 -121
  238. data/ruby/trema/vendor-request.c +0 -193
  239. data/spec/trema/action-enqueue_spec.rb +0 -100
  240. data/spec/trema/action-set-dl-dst_spec.rb +0 -95
  241. data/spec/trema/action-set-dl-src_spec.rb +0 -92
  242. data/spec/trema/action-set-nw-dst_spec.rb +0 -96
  243. data/spec/trema/action-set-nw-src_spec.rb +0 -97
  244. data/spec/trema/action-set-nw-tos_spec.rb +0 -88
  245. data/spec/trema/action-set-tp-dst_spec.rb +0 -88
  246. data/spec/trema/action-set-tp-src_spec.rb +0 -88
  247. data/spec/trema/action-set-vlan-pcp_spec.rb +0 -91
  248. data/spec/trema/action-set-vlan-vid_spec.rb +0 -91
  249. data/spec/trema/action-vendor_spec.rb +0 -90
  250. data/spec/trema/vendor-request_spec.rb +0 -79
@@ -26,7 +26,7 @@
26
26
  void
27
27
  timeout( void *user_data ) {
28
28
  handler_data *data = user_data;
29
- char match_string[ 256 ];
29
+ char match_string[ 512 ];
30
30
  match_to_string( &data->match, match_string, sizeof( match_string ) );
31
31
 
32
32
  error( "Timeout ( match = [%s], service_name = %s, strict = %s ).",
@@ -39,7 +39,7 @@ timeout( void *user_data ) {
39
39
  void
40
40
  add_filter_completed( int status, void *user_data ) {
41
41
  handler_data *data = user_data;
42
- char match_string[ 256 ];
42
+ char match_string[ 512 ];
43
43
  match_to_string( &data->match, match_string, sizeof( match_string ) );
44
44
 
45
45
  if ( status != PACKETIN_FILTER_OPERATION_SUCCEEDED ) {
@@ -56,7 +56,7 @@ add_filter_completed( int status, void *user_data ) {
56
56
  void
57
57
  delete_filter_completed( int status, int n_deleted, void *user_data ) {
58
58
  handler_data *data = user_data;
59
- char match_string[ 256 ];
59
+ char match_string[ 512 ];
60
60
  match_to_string( &data->match, match_string, sizeof( match_string ) );
61
61
 
62
62
  if ( status != PACKETIN_FILTER_OPERATION_SUCCEEDED ) {
@@ -74,7 +74,7 @@ delete_filter_completed( int status, int n_deleted, void *user_data ) {
74
74
  void
75
75
  dump_filters( int status, int n_entries, packetin_filter_entry *entries, void *user_data ) {
76
76
  handler_data *data = user_data;
77
- char match_string[ 256 ];
77
+ char match_string[ 512 ];
78
78
  match_to_string( &data->match, match_string, sizeof( match_string ) );
79
79
 
80
80
  if ( status != PACKETIN_FILTER_OPERATION_SUCCEEDED ) {
@@ -20,17 +20,17 @@
20
20
  #
21
21
 
22
22
 
23
- class RepeaterHub < Trema::Controller
23
+ class RepeaterHub < Controller
24
24
  def packet_in datapath_id, message
25
25
  send_flow_mod_add(
26
26
  datapath_id,
27
27
  :match => ExactMatch.from( message ),
28
- :actions => Trema::ActionOutput.new( OFPP_FLOOD )
28
+ :actions => ActionOutput.new( OFPP_FLOOD )
29
29
  )
30
30
  send_packet_out(
31
31
  datapath_id,
32
32
  :packet_in => message,
33
- :actions => Trema::ActionOutput.new( OFPP_FLOOD )
33
+ :actions => ActionOutput.new( OFPP_FLOOD )
34
34
  )
35
35
  end
36
36
  end
@@ -26,8 +26,8 @@ class SwitchInfoController < Controller
26
26
  end
27
27
 
28
28
 
29
- def features_reply message
30
- info "datapath_id: %#x" % message.datapath_id
29
+ def features_reply datapath_id, message
30
+ info "datapath_id: %#x" % datapath_id
31
31
  info "transaction_id: %#x" % message.transaction_id
32
32
  info "n_buffers: %u" % message.n_buffers
33
33
  info "n_tables: %u" % message.n_tables
@@ -77,7 +77,7 @@ insert_datapath_id( list_element **switches, uint64_t datapath_id ) {
77
77
  insert_in_front( switches, new );
78
78
  }
79
79
  else {
80
- insert_before( switches, element, new );
80
+ insert_before( switches, element->data, new );
81
81
  }
82
82
  }
83
83
 
@@ -24,7 +24,7 @@ require "counter"
24
24
  require "fdb"
25
25
 
26
26
 
27
- class TrafficMonitor < Trema::Controller
27
+ class TrafficMonitor < Controller
28
28
  periodic_timer_event :show_counter, 10
29
29
 
30
30
 
@@ -73,7 +73,7 @@ class TrafficMonitor < Trema::Controller
73
73
  datapath_id,
74
74
  :hard_timeout => 10,
75
75
  :match => Match.new( :dl_src => macsa, :dl_dst => macda ),
76
- :actions => Trema::ActionOutput.new( out_port )
76
+ :actions => ActionOutput.new( out_port )
77
77
  )
78
78
  end
79
79
 
@@ -82,7 +82,7 @@ class TrafficMonitor < Trema::Controller
82
82
  send_packet_out(
83
83
  datapath_id,
84
84
  :packet_in => message,
85
- :actions => Trema::ActionOutput.new( out_port )
85
+ :actions => ActionOutput.new( out_port )
86
86
  )
87
87
  end
88
88
 
data/src/lib/arp.h CHANGED
@@ -24,7 +24,6 @@
24
24
  #define ARP_H
25
25
 
26
26
 
27
- #include <net/if_arp.h>
28
27
  #include <stdint.h>
29
28
  #include "buffer.h"
30
29
  #include "ether.h"
@@ -50,6 +49,10 @@ typedef struct arp_header {
50
49
  } __attribute__((packed)) arp_header_t;
51
50
 
52
51
 
52
+ #define ARP_OP_REQUEST 1
53
+ #define ARP_OP_REPLY 2
54
+
55
+
53
56
  #endif // ARP_H
54
57
 
55
58
 
data/src/lib/chibach.c ADDED
@@ -0,0 +1,391 @@
1
+ /*
2
+ * Author: Yasunobu Chiba
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
+
20
+
21
+ #include <arpa/inet.h>
22
+ #include <assert.h>
23
+ #include <errno.h>
24
+ #include <getopt.h>
25
+ #include <libgen.h>
26
+ #include <limits.h>
27
+ #include <netinet/in.h>
28
+ #include <signal.h>
29
+ #include <stdio.h>
30
+ #include <stdlib.h>
31
+ #include <string.h>
32
+ #include <sys/socket.h>
33
+ #include <sys/stat.h>
34
+ #include <sys/types.h>
35
+ #include <unistd.h>
36
+ #include "chibach.h"
37
+ #include "chibach_private.h"
38
+ #include "daemon.h"
39
+ #include "doubly_linked_list.h"
40
+ #include "log.h"
41
+ #include "messenger.h"
42
+ #include "openflow_switch_interface.h"
43
+ #include "stat.h"
44
+ #include "timer.h"
45
+ #include "utility.h"
46
+ #include "wrapper.h"
47
+
48
+
49
+ static bool initialized = false;
50
+ static bool chibach_started = false;
51
+ static bool run_as_daemon = false;
52
+ static char *chibach_log = NULL;
53
+ static char *chibach_pid = NULL;
54
+ static char *chibach_name = NULL;
55
+ static uint64_t datapath_id = 0;
56
+
57
+ static struct {
58
+ uint32_t ip;
59
+ uint16_t port;
60
+ } controller;
61
+
62
+
63
+ static struct option long_options[] = {
64
+ { "datapath_id", required_argument, NULL, 'i' },
65
+ { "controller", required_argument, NULL, 'c' },
66
+ { "port", required_argument, NULL, 'p' },
67
+ { "daemonize", no_argument, NULL, 'd' },
68
+ { "logging_level", required_argument, NULL, 'l' },
69
+ { "help", no_argument, NULL, 'h' },
70
+ { NULL, 0, NULL, 0 },
71
+ };
72
+
73
+ static char short_options[] = "i:c:p:dl:h";
74
+
75
+
76
+ void
77
+ usage() {
78
+ printf(
79
+ "Usage: %s [OPTION]...\n"
80
+ "\n"
81
+ " -i, --datapath_id=DATAPATH_ID set datapath id\n"
82
+ " -c, --controller=IP_ADDR set controller host\n"
83
+ " -p, --port=TCP_PORT set controller TCP port\n"
84
+ " -d, --daemonize run in the background\n"
85
+ " -l, --logging_level=LEVEL set logging level\n"
86
+ " -h, --help display this help and exit\n",
87
+ get_chibach_name()
88
+ );
89
+ }
90
+
91
+
92
+ static const char *
93
+ get_chibach_log() {
94
+ if ( chibach_log == NULL ) {
95
+ char path[ PATH_MAX ];
96
+ sprintf( path, "%s/log", get_chibach_tmp() );
97
+ chibach_log = xstrdup( path );
98
+ }
99
+ return chibach_log;
100
+ }
101
+
102
+
103
+ static const char *
104
+ get_chibach_pid() {
105
+ if ( chibach_pid == NULL ) {
106
+ char path[ PATH_MAX ];
107
+ sprintf( path, "%s/pid", get_chibach_tmp() );
108
+ chibach_pid = xstrdup( path );
109
+ }
110
+ return chibach_pid;
111
+ }
112
+
113
+
114
+ const char *
115
+ get_chibach_name() {
116
+ if ( chibach_name == NULL ) {
117
+ return "";
118
+ }
119
+
120
+ return chibach_name;
121
+ }
122
+
123
+
124
+ static void
125
+ maybe_finalize_openflow_switch_interface() {
126
+ if ( openflow_switch_interface_is_initialized() ) {
127
+ finalize_openflow_switch_interface();
128
+ }
129
+ }
130
+
131
+
132
+ static void
133
+ die_unless_initialized() {
134
+ if ( !initialized ) {
135
+ die( "Chibach is not initialized. Call init_chibach() first." );
136
+ }
137
+ }
138
+
139
+
140
+ static void
141
+ finalize_chibach() {
142
+ die_unless_initialized();
143
+
144
+ debug( "Terminating chibach..." );
145
+
146
+ maybe_finalize_openflow_switch_interface();
147
+
148
+ finalize_messenger();
149
+ finalize_stat();
150
+ finalize_timer();
151
+ chibach_started = false;
152
+ unlink_pid( get_chibach_pid(), get_chibach_name() );
153
+
154
+ unset_chibach_home();
155
+ unset_chibach_tmp();
156
+
157
+ xfree( chibach_name );
158
+ chibach_name = NULL;
159
+
160
+ xfree( chibach_log );
161
+ chibach_log = NULL;
162
+
163
+ initialized = false;
164
+ }
165
+
166
+
167
+ static void
168
+ check_chibach_tmp() {
169
+ struct stat st;
170
+ int ret = stat( get_chibach_tmp(), &st );
171
+ if ( ( ret != 0 ) && ( errno == ENOENT ) ) {
172
+ die( "Chibach temporary directory does not exist: %s", get_chibach_tmp() );
173
+ }
174
+ }
175
+
176
+
177
+ static void
178
+ reset_getopt() {
179
+ optind = 0;
180
+ opterr = 1;
181
+ }
182
+
183
+
184
+ static void
185
+ parse_argv( int *argc, char ***argv ) {
186
+ assert( argc != NULL );
187
+ assert( argv != NULL );
188
+
189
+ int argc_tmp = *argc;
190
+ char *new_argv[ *argc ];
191
+
192
+ run_as_daemon = false;
193
+ controller.ip = 0x7f000001;
194
+ controller.port = 6633;
195
+
196
+ for ( int i = 0; i < *argc; ++i ) {
197
+ new_argv[ i ] = ( *argv )[ i ];
198
+ }
199
+
200
+ for ( ;; ) {
201
+ opterr = 0;
202
+ int c = getopt_long( *argc, *argv, short_options, long_options, NULL );
203
+
204
+ if ( c == -1 ) {
205
+ break;
206
+ }
207
+
208
+ switch ( c ) {
209
+ case 'i':
210
+ if ( optarg != NULL ) {
211
+ string_to_datapath_id( optarg, &datapath_id );
212
+ }
213
+ break;
214
+ case 'c':
215
+ if ( optarg != NULL ) {
216
+ struct in_addr addr;
217
+ inet_aton( optarg, &addr );
218
+ controller.ip = ntohl( addr.s_addr );
219
+ }
220
+ break;
221
+ case 'p':
222
+ if ( optarg != NULL && atoi( optarg ) <= UINT16_MAX ) {
223
+ controller.port = ( uint16_t ) atoi( optarg );
224
+ }
225
+ break;
226
+ case 'd':
227
+ run_as_daemon = true;
228
+ break;
229
+ case 'l':
230
+ set_logging_level( optarg );
231
+ break;
232
+ case 'h':
233
+ usage();
234
+ exit( EXIT_SUCCESS );
235
+ break;
236
+ default:
237
+ continue;
238
+ }
239
+
240
+ if ( optarg == NULL || strchr( new_argv[ optind - 1 ], '=' ) != NULL ) {
241
+ argc_tmp -= 1;
242
+ new_argv[ optind - 1 ] = NULL;
243
+ }
244
+ else {
245
+ argc_tmp -= 2;
246
+ new_argv[ optind - 1 ] = NULL;
247
+ new_argv[ optind - 2 ] = NULL;
248
+ }
249
+ }
250
+
251
+ for ( int i = 0, j = 0; i < *argc; ++i ) {
252
+ if ( new_argv[ i ] != NULL ) {
253
+ ( *argv )[ j ] = new_argv[ i ];
254
+ j++;
255
+ }
256
+ }
257
+ if ( argc_tmp < *argc ) {
258
+ ( *argv )[ argc_tmp ] = NULL;
259
+ }
260
+ *argc = argc_tmp;
261
+
262
+ reset_getopt();
263
+ }
264
+
265
+
266
+ static void
267
+ ignore_sigpipe() {
268
+ struct sigaction sigpipe_ignore;
269
+
270
+ memset( &sigpipe_ignore, 0, sizeof( struct sigaction ) );
271
+ sigpipe_ignore.sa_handler = SIG_IGN;
272
+ sigaction( SIGPIPE, &sigpipe_ignore, NULL );
273
+ }
274
+
275
+
276
+ static void
277
+ set_exit_handler() {
278
+ struct sigaction signal_exit;
279
+
280
+ memset( &signal_exit, 0, sizeof( struct sigaction ) );
281
+ signal_exit.sa_handler = ( void * ) stop_chibach;
282
+ sigaction( SIGINT, &signal_exit, NULL );
283
+ sigaction( SIGTERM, &signal_exit, NULL );
284
+ }
285
+
286
+
287
+ static void
288
+ set_dump_stats_as_external_callback() {
289
+ set_external_callback( dump_stats );
290
+ }
291
+
292
+
293
+ static void
294
+ set_usr1_handler() {
295
+ struct sigaction signal_usr1;
296
+
297
+ memset( &signal_usr1, 0, sizeof( struct sigaction ) );
298
+ signal_usr1.sa_handler = ( void * ) set_dump_stats_as_external_callback;
299
+ sigaction( SIGUSR1, &signal_usr1, NULL );
300
+ }
301
+
302
+
303
+ static void
304
+ maybe_daemonize() {
305
+ if ( run_as_daemon ) {
306
+ daemonize( get_chibach_home() );
307
+ }
308
+ }
309
+
310
+
311
+ void
312
+ init_chibach( int *argc, char ***argv ) {
313
+ assert( argc != NULL );
314
+ assert( argv != NULL );
315
+
316
+ chibach_log = NULL;
317
+ initialized = false;
318
+ chibach_started = false;
319
+ run_as_daemon = false;
320
+
321
+ chibach_name = xstrdup( basename( *argv[ 0 ] ) );
322
+
323
+ parse_argv( argc, argv );
324
+ set_chibach_home();
325
+ set_chibach_tmp();
326
+ check_chibach_tmp();
327
+ if ( run_as_daemon ) {
328
+ init_log( get_chibach_name(), get_chibach_log(), LOGGING_TYPE_FILE );
329
+ }
330
+ else {
331
+ init_log( get_chibach_name(), get_chibach_log(), LOGGING_TYPE_FILE | LOGGING_TYPE_STDOUT );
332
+ }
333
+ ignore_sigpipe();
334
+ set_exit_handler();
335
+ set_usr1_handler();
336
+ init_stat();
337
+ init_timer();
338
+ init_messenger( get_chibach_tmp() );
339
+ if ( datapath_id != 0 ) {
340
+ init_openflow_switch_interface( datapath_id, controller.ip, controller.port );
341
+ }
342
+
343
+ initialized = true;
344
+ }
345
+
346
+
347
+ static void
348
+ start_chibach_up() {
349
+ debug( "Starting chibach ... (CHIBACH_HOME = %s)", get_chibach_home() );
350
+
351
+ maybe_daemonize();
352
+ write_pid( get_chibach_pid(), get_chibach_name() );
353
+ chibach_started = true;
354
+
355
+ start_messenger();
356
+ }
357
+
358
+
359
+ static void
360
+ start_chibach_down() {
361
+ finalize_chibach();
362
+ }
363
+
364
+
365
+ void
366
+ start_chibach() {
367
+ start_chibach_up();
368
+ start_event_handler();
369
+ start_chibach_down();
370
+ }
371
+
372
+
373
+ void
374
+ stop_chibach() {
375
+ stop_event_handler();
376
+ stop_messenger();
377
+ }
378
+
379
+
380
+ uint64_t
381
+ get_datapath_id() {
382
+ return datapath_id;
383
+ }
384
+
385
+
386
+ /*
387
+ * Local variables:
388
+ * c-basic-offset: 2
389
+ * indent-tabs-mode: nil
390
+ * End:
391
+ */