trema 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,9 +74,14 @@ module Trema
74
74
  end
75
75
 
76
76
 
77
- ##############################################################################
77
+ def to_s
78
+ "SendOutPort: port=#{ @port_number }, max_len=#{ @max_len }"
79
+ end
80
+
81
+
82
+ ############################################################################
78
83
  private
79
- ##############################################################################
84
+ ############################################################################
80
85
 
81
86
 
82
87
  def check_port_number
@@ -41,6 +41,11 @@ module Trema
41
41
  def initialize mac_address
42
42
  super mac_address
43
43
  end
44
+
45
+
46
+ def to_s
47
+ "SetEthDstAddr: mac_address=#{ @mac_address }"
48
+ end
44
49
  end
45
50
 
46
51
 
@@ -41,6 +41,11 @@ module Trema
41
41
  def initialize mac_address
42
42
  super mac_address
43
43
  end
44
+
45
+
46
+ def to_s
47
+ "SetEthSrcAddr: mac_address=#{ @mac_address }"
48
+ end
44
49
  end
45
50
 
46
51
 
@@ -168,69 +168,65 @@ Init_stats_reply() {
168
168
  static VALUE
169
169
  get_action( const struct ofp_action_header *ah ) {
170
170
  VALUE action = Qnil;
171
- VALUE options = rb_hash_new();
172
171
 
173
172
  switch ( ah->type ) {
174
173
  case OFPAT_OUTPUT:
175
174
  {
176
175
  const struct ofp_action_output *ao = ( const struct ofp_action_output * ) ah;
177
176
 
178
- rb_hash_aset( options, ID2SYM( rb_intern( "port" ) ), UINT2NUM( ao->port ) );
179
- action = rb_funcall( rb_eval_string( "Trema::ActionOutput" ), rb_intern( "new" ), 1, options );
177
+ VALUE options = rb_hash_new();
178
+ rb_hash_aset( options, ID2SYM( rb_intern( "port_number" ) ), UINT2NUM( ao->port ) );
179
+ rb_hash_aset( options, ID2SYM( rb_intern( "max_len" ) ), UINT2NUM( ao->max_len ) );
180
+ action = rb_funcall( rb_eval_string( "Trema::SendOutPort" ), rb_intern( "new" ), 1, options );
180
181
  }
181
182
  break;
182
183
  case OFPAT_SET_VLAN_VID:
183
184
  {
184
185
  const struct ofp_action_vlan_vid *action_vlan_vid = ( const struct ofp_action_vlan_vid * ) ah;
185
186
 
186
- rb_hash_aset( options, ID2SYM( rb_intern( "vlan_vid" ) ), UINT2NUM( action_vlan_vid->vlan_vid ) );
187
- action = rb_funcall( rb_eval_string( "Trema::ActionSetVlanVid" ), rb_intern( "new" ), 1, options );
187
+ VALUE vlan_id = UINT2NUM( action_vlan_vid->vlan_vid );
188
+ action = rb_funcall( rb_eval_string( "Trema::SetVlanVid" ), rb_intern( "new" ), 1, vlan_id );
188
189
  }
189
190
  break;
190
191
  case OFPAT_SET_VLAN_PCP:
191
192
  {
192
193
  const struct ofp_action_vlan_pcp *action_vlan_pcp = ( const struct ofp_action_vlan_pcp * ) ah;
193
194
 
194
- rb_hash_aset( options, ID2SYM( rb_intern( "vlan_pcp" ) ), UINT2NUM( action_vlan_pcp->vlan_pcp ) );
195
- action = rb_funcall( rb_eval_string( "Trema::ActionSetVlanPcp" ), rb_intern( "new" ), 1, options );
195
+ VALUE vlan_priority = UINT2NUM( action_vlan_pcp->vlan_pcp );
196
+ action = rb_funcall( rb_eval_string( "Trema::SetVlanPriority" ), rb_intern( "new" ), 1, vlan_priority );
196
197
  }
197
198
  break;
198
199
  case OFPAT_STRIP_VLAN:
199
200
  {
200
- action = rb_funcall( rb_eval_string( "Trema::ActionStripVlan" ), rb_intern( "new" ), 0 );
201
+ action = rb_funcall( rb_eval_string( "Trema::StripVlanHeader" ), rb_intern( "new" ), 0 );
201
202
  }
202
203
  break;
203
204
  case OFPAT_SET_DL_SRC:
204
205
  case OFPAT_SET_DL_DST:
205
206
  {
206
- VALUE dl_addr;
207
+ VALUE mac_address;
207
208
  const struct ofp_action_dl_addr *action_dl_addr = ( const struct ofp_action_dl_addr * ) ah;
208
209
 
209
- dl_addr = rb_funcall( rb_eval_string( "Trema::Mac" ), rb_intern( "new" ), 1, ULL2NUM( mac_to_uint64( action_dl_addr->dl_addr ) ) );
210
+ mac_address = rb_funcall( rb_eval_string( "Trema::Mac" ), rb_intern( "new" ), 1, ULL2NUM( mac_to_uint64( action_dl_addr->dl_addr ) ) );
210
211
  if ( ah->type == OFPAT_SET_DL_SRC ) {
211
- rb_hash_aset( options, ID2SYM( rb_intern( "dl_src" ) ), dl_addr );
212
- action = rb_funcall( rb_eval_string( "Trema::ActionSetDlSrc" ), rb_intern( "new" ), 1, options );
212
+ action = rb_funcall( rb_eval_string( "Trema::SetEthSrcAddr" ), rb_intern( "new" ), 1, mac_address );
213
213
  }
214
214
  else {
215
- rb_hash_aset( options, ID2SYM( rb_intern( "dl_dst" ) ), dl_addr );
216
- action = rb_funcall( rb_eval_string( "Trema::ActionSetDlDst" ), rb_intern( "new" ), 1, options );
215
+ action = rb_funcall( rb_eval_string( "Trema::SetEthDstAddr" ), rb_intern( "new" ), 1, mac_address );
217
216
  }
218
217
  }
219
218
  break;
220
219
  case OFPAT_SET_NW_SRC:
221
220
  case OFPAT_SET_NW_DST:
222
221
  {
223
- VALUE nw_addr;
224
222
  const struct ofp_action_nw_addr *action_nw_addr = ( const struct ofp_action_nw_addr * ) ah;
225
223
 
226
- nw_addr = rb_funcall( rb_eval_string( "Trema::IP " ), rb_intern( "new" ), 1, UINT2NUM( action_nw_addr->nw_addr ) );
224
+ VALUE ip_address = rb_funcall( rb_eval_string( "Trema::IP " ), rb_intern( "new" ), 1, UINT2NUM( action_nw_addr->nw_addr ) );
227
225
  if ( ah->type == OFPAT_SET_NW_SRC ) {
228
- rb_hash_aset( options, ID2SYM( rb_intern( "nw_src" ) ), nw_addr );
229
- action = rb_funcall( rb_eval_string( "Trema::ActionSetNwSrc" ), rb_intern( "new" ), 1, options );
226
+ action = rb_funcall( rb_eval_string( "Trema::SetIpSrcAddr" ), rb_intern( "new" ), 1, rb_funcall( ip_address, rb_intern( "to_s" ), 0 ) );
230
227
  }
231
228
  else {
232
- rb_hash_aset( options, ID2SYM( rb_intern( "nw_dst" ) ), nw_addr );
233
- action = rb_funcall( rb_eval_string( "Trema::ActionSetNwDst" ), rb_intern( "new" ), 1, options );
229
+ action = rb_funcall( rb_eval_string( "Trema::SetIpDstAddr" ), rb_intern( "new" ), 1, rb_funcall( ip_address, rb_intern( "to_s" ), 0 ) );
234
230
  }
235
231
  }
236
232
  break;
@@ -238,40 +234,53 @@ get_action( const struct ofp_action_header *ah ) {
238
234
  {
239
235
  const struct ofp_action_nw_tos *action_nw_tos = ( const struct ofp_action_nw_tos * ) ah;
240
236
 
241
- rb_hash_aset( options, ID2SYM( rb_intern( "nw_tos" ) ), ULL2NUM( action_nw_tos->nw_tos ) );
242
- action = rb_funcall( rb_eval_string( "Trema::ActionSetNwTos" ), rb_intern( "new" ), 1, options );
237
+ VALUE type_of_service = ULL2NUM( action_nw_tos->nw_tos );
238
+ action = rb_funcall( rb_eval_string( "Trema::SetIpTos" ), rb_intern( "new" ), 1, type_of_service );
243
239
  }
244
240
  break;
245
241
  case OFPAT_SET_TP_SRC:
246
242
  {
247
243
  const struct ofp_action_tp_port *action_tp_port = ( const struct ofp_action_tp_port * ) ah;
248
244
 
249
- rb_hash_aset( options, ID2SYM( rb_intern( "tp_src" ) ), ULL2NUM( action_tp_port->tp_port ) );
250
- action = rb_funcall( rb_eval_string( "Trema::ActionSetTpSrc" ), rb_intern( "new" ), 1, options );
245
+ VALUE port_number = ULL2NUM( action_tp_port->tp_port );
246
+ action = rb_funcall( rb_eval_string( "Trema::SetTransportSrcPort" ), rb_intern( "new" ), 1, port_number );
251
247
  }
252
248
  break;
253
249
  case OFPAT_SET_TP_DST:
254
250
  {
255
251
  const struct ofp_action_tp_port *action_tp_port = ( const struct ofp_action_tp_port * ) ah;
256
252
 
257
- rb_hash_aset( options, ID2SYM( rb_intern( "tp_dst" ) ), ULL2NUM( action_tp_port->tp_port ) );
258
- action = rb_funcall( rb_eval_string( "Trema::ActionSetTpDst" ), rb_intern( "new" ), 1, options );
253
+ VALUE port_number = ULL2NUM( action_tp_port->tp_port );
254
+ action = rb_funcall( rb_eval_string( "Trema::SetTransportDstPort" ), rb_intern( "new" ), 1, port_number );
259
255
  }
260
256
  break;
261
257
  case OFPAT_ENQUEUE:
262
258
  {
263
259
  const struct ofp_action_enqueue *action_enqueue = ( const struct ofp_action_enqueue * ) ah;
264
- rb_hash_aset( options, ID2SYM( rb_intern( "port" ) ), ULL2NUM( action_enqueue->port ) );
260
+ VALUE options = rb_hash_new();
261
+ rb_hash_aset( options, ID2SYM( rb_intern( "port_number" ) ), UINT2NUM( action_enqueue->port ) );
265
262
  rb_hash_aset( options, ID2SYM( rb_intern( "queue_id" ) ), ULL2NUM( action_enqueue->queue_id ) );
266
- action = rb_funcall( rb_eval_string( "Trema::ActionEnqueue" ), rb_intern( "new" ), 1, options );
263
+ action = rb_funcall( rb_eval_string( "Trema::Enqueue" ), rb_intern( "new" ), 1, options );
267
264
  }
268
265
  break;
269
266
  case OFPAT_VENDOR:
270
267
  {
271
- const struct ofp_action_vendor_header *action_vendor = ( const struct ofp_action_vendor_header * ) ah;
272
-
273
- rb_hash_aset( options, ID2SYM( rb_intern( "vendor" ) ), ULL2NUM( action_vendor->vendor ) );
274
- action = rb_funcall( rb_eval_string( "Trema::ActionVendor" ), rb_intern( "new" ), 1, options );
268
+ const struct ofp_action_vendor_header *vendor_header = ( const struct ofp_action_vendor_header * ) ah;
269
+
270
+ VALUE vendor_id = ULL2NUM( vendor_header->vendor );
271
+ if ( vendor_header->len > ( uint16_t ) sizeof( *vendor_header ) ) {
272
+ long length = ( long ) ( vendor_header->len - sizeof( *vendor_header ) );
273
+ VALUE data_array = rb_ary_new2( length );
274
+ const uint8_t *data = ( const uint8_t * ) ( ( const char * ) vendor_header + sizeof( *vendor_header ) );
275
+ long i;
276
+ for ( i = 0; i < length; i++ ) {
277
+ rb_ary_push( data_array, UINT2NUM( data[ i ] ) );
278
+ }
279
+ action = rb_funcall( rb_eval_string( "Trema::VendorAction" ), rb_intern( "new" ), 2, vendor_id, data_array );
280
+ }
281
+ else {
282
+ action = rb_funcall( rb_eval_string( "Trema::VendorAction" ), rb_intern( "new" ), 1, vendor_id );
283
+ }
275
284
  }
276
285
  break;
277
286
  }
@@ -17,7 +17,7 @@
17
17
 
18
18
 
19
19
  module Trema
20
- VERSION = "0.2.8"
20
+ VERSION = "0.3.0"
21
21
  end
22
22
 
23
23
 
@@ -24,6 +24,7 @@ describe SendOutPort, ".new( 1 )" do
24
24
  subject { SendOutPort.new( 1 ) }
25
25
  its( :port_number ) { should == 1 }
26
26
  its( :max_len ) { should == 2 ** 16 - 1 }
27
+ its( :to_s ) { should == "SendOutPort: port=1, max_len=65535" }
27
28
  end
28
29
 
29
30
 
@@ -34,6 +35,7 @@ describe SendOutPort, ".new( :port_number => number )" do
34
35
  context "when :port_number == 1" do
35
36
  let( :port_number ) { 1 }
36
37
  its( :port_number ) { should == 1 }
38
+ its( :to_s ) { should == "SendOutPort: port=1, max_len=65535" }
37
39
  end
38
40
  end
39
41
 
@@ -45,6 +47,7 @@ describe SendOutPort, ".new( :port_number => 1, :max_len => number )" do
45
47
  context "when :max_len == 256" do
46
48
  let( :max_len ) { 256 }
47
49
  its( :max_len ) { should == 256 }
50
+ its( :to_s ) { should == "SendOutPort: port=1, max_len=256" }
48
51
  end
49
52
  end
50
53
 
@@ -23,12 +23,14 @@ require "trema"
23
23
  describe SetEthDstAddr, %{.new( "52:54:00:a8:ad:8c" )} do
24
24
  subject { SetEthDstAddr.new( "52:54:00:a8:ad:8c" ) }
25
25
  its( :mac_address ) { should == Mac.new( "52:54:00:a8:ad:8c" ) }
26
+ its( :to_s ) { should == "SetEthDstAddr: mac_address=52:54:00:a8:ad:8c" }
26
27
  end
27
28
 
28
29
 
29
30
  describe SetEthDstAddr, %{.new( Mac.new( "52:54:00:a8:ad:8c" ) )} do
30
31
  subject { SetEthDstAddr.new( Mac.new( "52:54:00:a8:ad:8c" ) )}
31
32
  its( :mac_address ) { should == Mac.new( "52:54:00:a8:ad:8c" ) }
33
+ its( :to_s ) { should == "SetEthDstAddr: mac_address=52:54:00:a8:ad:8c" }
32
34
  end
33
35
 
34
36
 
@@ -23,12 +23,14 @@ require "trema"
23
23
  describe SetEthSrcAddr, %{.new( "52:54:00:a8:ad:8c" )} do
24
24
  subject { SetEthSrcAddr.new( "52:54:00:a8:ad:8c" ) }
25
25
  its( :mac_address ) { should == Mac.new( "52:54:00:a8:ad:8c" ) }
26
+ its( :to_s ) { should == "SetEthSrcAddr: mac_address=52:54:00:a8:ad:8c" }
26
27
  end
27
28
 
28
29
 
29
30
  describe SetEthSrcAddr, %{.new( Mac.new( "52:54:00:a8:ad:8c" ) )} do
30
31
  subject { SetEthSrcAddr.new( Mac.new( "52:54:00:a8:ad:8c" ) )}
31
32
  its( :mac_address ) { should == Mac.new( "52:54:00:a8:ad:8c" ) }
33
+ its( :to_s ) { should == "SetEthSrcAddr: mac_address=52:54:00:a8:ad:8c" }
32
34
  end
33
35
 
34
36
 
@@ -0,0 +1,18 @@
1
+ This directory includes an OpenFlow controller that controls a single
2
+ OpenFlow switch and emulates a router.
3
+
4
+ # How to run
5
+
6
+ Run this
7
+
8
+ % sudo trema run ./src/examples/simple_router/simple-router.rb -c ./src/examples/simple_router/simple_router_netns.conf -d
9
+
10
+ and send packets like as follows.
11
+
12
+ % sudo ip netns exec host1 ping 192.168.2.2
13
+
14
+ Then check flow entries in OpenFlow switch.
15
+
16
+ % sudo trema dump_flows 0x1
17
+
18
+ This will display flow entries created by the controller.
@@ -37,7 +37,7 @@ class RoutingTable
37
37
  dest = IPAddr.new( options[ :destination ] )
38
38
  masklen = options[ :masklen ]
39
39
  prefix = dest.mask( masklen )
40
- @db[ masklen ][ prefix.to_i ] = IPAddr.new( options[ :gateway ] )
40
+ @db[ masklen ][ prefix.to_i ] = IPAddr.new( options[ :nexthop ] )
41
41
  end
42
42
 
43
43
 
@@ -130,11 +130,11 @@ class SimpleRouter < Controller
130
130
 
131
131
 
132
132
  def resolve_next_hop( daddr )
133
- next_hop = @routing_table.lookup( daddr.value )
134
- if next_hop
135
- next_hop
136
- else
133
+ interface = @interfaces.find_by_prefix( daddr.value )
134
+ if interface
137
135
  daddr.value
136
+ else
137
+ @routing_table.lookup( daddr.value )
138
138
  end
139
139
  end
140
140
 
@@ -165,8 +165,8 @@ class SimpleRouter < Controller
165
165
 
166
166
  def create_action_from( macsa, macda, port )
167
167
  [
168
- SetEthSrcAddr.new( macsa.to_s ),
169
- SetEthDstAddr.new( macda.to_s ),
168
+ SetEthSrcAddr.new( macsa ),
169
+ SetEthDstAddr.new( macda ),
170
170
  SendOutPort.new( port )
171
171
  ]
172
172
  end
@@ -15,4 +15,10 @@ $interface = [
15
15
  }
16
16
  ]
17
17
 
18
- $route = []
18
+ $route = [
19
+ {
20
+ :destination => "0.0.0.0",
21
+ :masklen => 0,
22
+ :nexthop => "192.168.1.2"
23
+ }
24
+ ]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 8
10
- version: 0.2.8
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Yasuhito Takamiya
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: .
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-01 00:00:00 Z
18
+ date: 2013-01-07 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  prerelease: false
@@ -466,6 +466,7 @@ files:
466
466
  - src/examples/repeater_hub/repeater-hub_spec.rb
467
467
  - src/examples/repeater_hub/repeater_hub.c
468
468
  - src/examples/repeater_hub/repeater_hub.conf
469
+ - src/examples/simple_router/README
469
470
  - src/examples/simple_router/arp-table.rb
470
471
  - src/examples/simple_router/interface.rb
471
472
  - src/examples/simple_router/router-utils.rb