openflow_instructions *
pack_instruction( VALUE r_instruction ) {
  openflow_instructions *instructions = create_instructions();
  VALUE r_ins_instance;
  VALUE r_id = rb_intern( "pack_instruction" );

  if ( !NIL_P( r_instruction ) ) {
    switch ( TYPE( r_instruction ) ) {
      case T_ARRAY: {
        VALUE *each = RARRAY_PTR( r_instruction );

        for ( int i = 0; i < RARRAY_LEN( r_instruction ); i++ ) {
          if ( rb_respond_to( each[ i ], r_id ) ) {
            r_ins_instance = Data_Wrap_Struct( rb_obj_class( each[ i ] ), NULL, NULL, instructions );
            rb_funcall( each[ i ], r_id, 1, r_ins_instance );
          }
        }
      }
      break;
      case T_OBJECT:
        if ( rb_respond_to( rb_obj_class( r_instruction ), r_id ) ) {
          r_ins_instance = Data_Wrap_Struct( r_instruction, NULL, NULL, instructions );
          rb_funcall( r_instruction, r_id, 1, r_ins_instance );
        }
      break;
      default:
        rb_raise( rb_eTypeError, "Instruction argument must be either an Array or an Instruction object" );
      break;
    }
  }
  Data_Get_Struct( r_ins_instance, openflow_instructions, instructions );
  return instructions;
}
static void
handle_switch_ready( uint64_t datapath_id, void *user_data ) {
  UNUSED( user_data );

  openflow_actions *actions = create_actions();
  append_action_output( actions, OFPP_CONTROLLER, OFPCML_NO_BUFFER );
  openflow_instructions *insts = create_instructions();
  append_instructions_apply_actions( insts, actions );

  buffer *flow_mod = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_LOW_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    NULL,
    insts
  );
  send_openflow_message( datapath_id, flow_mod );
  free_buffer( flow_mod );

  delete_instructions( insts );
  delete_actions( actions );
}
Exemple #3
0
static void
handle_packet_in( uint64_t datapath_id, packet_in message ) {
  if ( message.data == NULL ) {
    error( "data must not be NULL" );
    return;
  }
  uint32_t in_port = get_in_port_from_oxm_matches( message.match );
  if ( in_port == 0 ) {
    return;
  }

  openflow_actions *actions = create_actions();
  append_action_output( actions, OFPP_ALL, OFPCML_NO_BUFFER );

  openflow_instructions *insts = create_instructions();
  append_instructions_apply_actions( insts, actions );

  oxm_matches *match = create_oxm_matches();
  set_match_from_packet( match, in_port, 0, message.data );

  buffer *flow_mod = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    60,
    0,
    OFP_HIGH_PRIORITY,
    message.buffer_id,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match,
    insts
  );
  send_openflow_message( datapath_id, flow_mod );
  free_buffer( flow_mod );
  delete_oxm_matches( match );
  delete_instructions( insts );

  if ( message.buffer_id == OFP_NO_BUFFER ) {
    buffer *frame = duplicate_buffer( message.data );
    fill_ether_padding( frame );
    buffer *packet_out = create_packet_out(
      get_transaction_id(),
      message.buffer_id,
      in_port,
      actions,
      frame
    );
    send_openflow_message( datapath_id, packet_out );
    free_buffer( packet_out );
    free_buffer( frame );
  }

  delete_actions( actions );
}
static void
send_packet( uint32_t destination_port, packet_in packet_in, uint32_t in_port ) {
  openflow_actions *actions = create_actions();
  append_action_output( actions, destination_port, OFPCML_NO_BUFFER );

  openflow_instructions *insts = create_instructions();
  append_instructions_apply_actions( insts, actions );

  oxm_matches *match = create_oxm_matches();
  set_match_from_packet( match, in_port, NULL, packet_in.data );

  buffer *flow_mod = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    60,
    0,
    OFP_HIGH_PRIORITY,
    packet_in.buffer_id,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match,
    insts
  );
  send_openflow_message( packet_in.datapath_id, flow_mod );
  free_buffer( flow_mod );
  delete_oxm_matches( match );
  delete_instructions( insts );

  if ( packet_in.buffer_id == OFP_NO_BUFFER ) {
    buffer *frame = duplicate_buffer( packet_in.data );
    fill_ether_padding( frame );
    buffer *packet_out = create_packet_out(
      get_transaction_id(),
      packet_in.buffer_id,
      in_port,
      actions,
      frame
    );
    send_openflow_message( packet_in.datapath_id, packet_out );
    free_buffer( packet_out );
    free_buffer( frame );
  }

  delete_actions( actions );
}
Exemple #5
0
static void
on_new_kinect_device (GObject      *obj,
                      GAsyncResult *res,
                      gpointer      user_data)
{
  ClutterActor *stage, *instructions;
  GError *error = NULL;
  gint width = 640;
  gint height = 480;

  kinect = gfreenect_device_new_finish (res, &error);
  if (kinect == NULL)
    {
      g_debug ("Failed to created kinect device: %s", error->message);
      g_error_free (error);
      clutter_main_quit ();
      return;
    }

  g_debug ("Kinect device created!");

  stage = clutter_stage_get_default ();
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Kinect Test");
  clutter_actor_set_size (stage, width * 2, height + 200);
  clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);

  g_signal_connect (stage, "destroy", G_CALLBACK (on_destroy), kinect);
  g_signal_connect (stage,
                    "key-release-event",
                    G_CALLBACK (on_key_release),
                    kinect);

  depth_tex = clutter_cairo_texture_new (width, height);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), depth_tex);

  video_tex = clutter_cairo_texture_new (width, height);
  clutter_actor_set_position (video_tex, width, 0.0);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), video_tex);

  info_text = clutter_text_new ();
  set_info_text ();
  clutter_actor_set_position (info_text, 50, height + 20);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), info_text);

  instructions = create_instructions ();
  clutter_actor_set_position (instructions, 50, height + 70);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), instructions);

  clutter_actor_show_all (stage);

  skeleton = SKELTRACK_SKELETON (skeltrack_skeleton_new ());

  g_signal_connect (kinect,
                    "depth-frame",
                    G_CALLBACK (on_depth_frame),
                    NULL);

  g_signal_connect (kinect,
                    "video-frame",
                    G_CALLBACK (on_video_frame),
                    NULL);

  g_signal_connect (depth_tex,
                    "draw",
                    G_CALLBACK (on_texture_draw),
                    NULL);

  gfreenect_device_set_tilt_angle (kinect, 0, NULL, NULL, NULL);

  gfreenect_device_start_depth_stream (kinect,
                                       GFREENECT_DEPTH_FORMAT_MM,
                                       NULL);

  gfreenect_device_start_video_stream (kinect,
                                       GFREENECT_RESOLUTION_MEDIUM,
                                       GFREENECT_VIDEO_FORMAT_RGB, NULL);
}
int EScriptProgram::read(const char *fname)
{
    FILE* fp = NULL;

    try
    {
        name = fname;

        fp = fopen(fname, "rb");
	    if (!fp)
            throw runtime_error( string("Unable to open ") + fname + " for reading." );

	    BSCRIPT_FILE_HDR hdr;
        if (fread( &hdr, sizeof hdr, 1, fp ) != 1)
	    {
            cerr << "Error loading script " << fname
                 << ": error reading header" << endl;
		    fclose(fp);
		    return -1;
	    }
	    if (hdr.magic2[0] != BSCRIPT_FILE_MAGIC0 ||
            hdr.magic2[1] != BSCRIPT_FILE_MAGIC1)
	    {
            cerr << "Error loading script " << fname 
                 << ": bad magic value '" << hdr.magic2[0] << hdr.magic2[1] << "'" << endl;
		    fclose(fp);
		    return -1;
	    }
		// auto-check for latest version (see filefmt.h for setting)
	    if (hdr.version != ESCRIPT_FILE_VER_CURRENT)
	    {
            cerr << "Error loading script " << fname 
                 << ": Recompile required. Bad version number " << hdr.version << endl;
		    fclose(fp);
		    return -1;
	    }
        version = hdr.version;
        nglobals = hdr.globals;
	    BSCRIPT_SECTION_HDR sechdr;
	    while (fread( &sechdr, sizeof sechdr, 1, fp ) == 1)
	    {
		    switch( sechdr.type )
		    {
                case BSCRIPT_SECTION_PROGDEF:
                    if (read_progdef_hdr(fp))
                    {
                        cerr << "Error loading script " << fname
                             << ": error reading progdef section" << endl;
                        fclose(fp);
                        return -1;
                    }
                    break;

			    case BSCRIPT_SECTION_MODULE:
				    if (read_module(fp))
				    {
                        cerr << "Error loading script " << fname
                             << ": error reading module section" << endl;
					    fclose(fp);
					    return -1;
				    }
				    break;
			    case BSCRIPT_SECTION_CODE:
				    tokens.read(fp);
				    break;
			    case BSCRIPT_SECTION_SYMBOLS:
				    symbols.read(fp);
				    break;
                case BSCRIPT_SECTION_GLOBALVARNAMES:
                    if (read_globalvarnames(fp))
                    {
                        cerr << "Error loading script " << fname
                             << ": error reading global variable name section" << endl;
					    fclose(fp);
					    return -1;
                    }
                    break;
                case BSCRIPT_SECTION_EXPORTED_FUNCTIONS:
                    if (read_exported_functions(fp, &sechdr))
                    {
                        cerr << "Error loading script " << fname
                             << ": error reading exported functions section" << endl;
                        fclose(fp);
                        return -1;
                    }
                    break;
			    default:
                        cerr << "Error loading script " << fname
                             << ": unknown section type " << int(sechdr.type) << endl;
				    fclose(fp);
				    return -1;
		    }
	    }
        fclose(fp);

        return create_instructions();
    }
    catch( std::exception& ex )
    {
        cerr << "Exception caught while loading script " << fname << ": " << ex.what() << endl;
        if (fp != NULL)
            fclose(fp);
        return -1;
    }
#ifndef WIN32
    catch(...)
    {
        cerr << "Exception caught while loading script " << fname << endl;
        if (fp != NULL)
            fclose(fp);
        return -1;
    }
#endif
}
static void
handle_switch_ready( uint64_t datapath_id, void *user_data ) {
  UNUSED( user_data );

  openflow_actions *actions = create_actions();
  append_action_output( actions, OFPP_CONTROLLER, OFPCML_NO_BUFFER );

  openflow_instructions *insts = create_instructions();
  append_instructions_apply_actions( insts, actions );

  buffer *flow_mod = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_LOW_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    NULL,
    insts
  );
  send_openflow_message( datapath_id, flow_mod );
  free_buffer( flow_mod );
  delete_instructions( insts );

//  sleep(2000);
  
  info("sending ARP config to %d",datapath_id);
  uint32_t dest = OFPP_ALL;
  oxm_matches *match2 = create_oxm_matches();
  append_oxm_match_eth_type( match2, 0x0806);



  openflow_actions *actions2 = create_actions();
  append_action_output( actions2, dest, OFPCML_NO_BUFFER );

  openflow_instructions *insts2 = create_instructions();
  append_instructions_apply_actions( insts2, actions2 );

  buffer *flow_mod2 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match2,
    insts2
  );
  send_openflow_message( datapath_id, flow_mod2 );
  free_buffer( flow_mod2 );
  delete_oxm_matches( match2 );
  delete_instructions( insts2 );

//  sleep(2000);

  info("sending second config: ipv4");
  oxm_matches *match3 = create_oxm_matches();
  append_oxm_match_eth_type( match3, 0x0800);
//  append_oxm_match_in_port( match2, 2);
//  dest = 1;

  openflow_actions *actions3 = create_actions();
  append_action_push_mpls(actions3,0x8847);
  append_action_set_field_mpls_label(actions3,4120955);
  append_action_output( actions3, dest, OFPCML_NO_BUFFER );

  openflow_instructions *insts3 = create_instructions();
  append_instructions_apply_actions( insts3, actions3 );

  buffer *flow_mod3 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match3,
    insts3
  );
  send_openflow_message( datapath_id, flow_mod3 );
  free_buffer( flow_mod3 );
  delete_oxm_matches( match3 );
  delete_instructions( insts3 );
  info("done sending");

//  sleep(2000);

  info("sending third config");
  oxm_matches *match4 = create_oxm_matches();
  append_oxm_match_eth_type( match4, 0x8847);
  append_oxm_match_mpls_label(match4, 4120955);

  openflow_actions *actions4 = create_actions();
  append_action_pop_mpls(actions4, 0x0800);
  append_action_output( actions4, dest, OFPCML_NO_BUFFER );


  openflow_instructions *insts4 = create_instructions();
  append_instructions_apply_actions( insts4, actions4 );

  buffer *flow_mod4 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match4,
    insts4
  );
  send_openflow_message( datapath_id, flow_mod4);
  free_buffer( flow_mod4 );
  delete_oxm_matches( match4 );
  delete_instructions( insts4 );
  info("done sending");


}
static void
handle_switch_ready( uint64_t datapath_id, void *user_data ) {
  UNUSED( user_data );

  openflow_actions *actions = create_actions();
  append_action_output( actions, OFPP_CONTROLLER, OFPCML_NO_BUFFER );

  openflow_instructions *insts = create_instructions();
  append_instructions_apply_actions( insts, actions );

  buffer *flow_mod = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_LOW_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    NULL,
    insts
  );
  send_openflow_message( datapath_id, flow_mod );
  free_buffer( flow_mod );
  delete_instructions( insts );

//  sleep(2000);
  
  info("sending ARP config: flood - %d",datapath_id);
  uint32_t dest = OFPP_ALL;
  oxm_matches *match2 = create_oxm_matches();
  append_oxm_match_eth_type( match2, 0x0806);



  openflow_actions *actions2 = create_actions();
  append_action_output( actions2, dest, OFPCML_NO_BUFFER );

  openflow_instructions *insts2 = create_instructions();
  append_instructions_apply_actions( insts2, actions2 );

  buffer *flow_mod2 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match2,
    insts2
  );
  send_openflow_message( datapath_id, flow_mod2 );
  free_buffer( flow_mod2 );
  delete_oxm_matches( match2 );
  delete_instructions( insts2 );

//  sleep(2000);

  info("sending second config: match on eth src");
  uint8_t eth_src[ OFP_ETH_ALEN ] = {0x0,0x24,0xe8,0x77,0xa5,0x32};
  uint8_t set_src[ OFP_ETH_ALEN ] = {0x0,0x10,0x18,0x56,0xab,0xc2};
  uint8_t eth_mask[ OFP_ETH_ALEN ] = {0xff,0xff,0xff,0xff,0xff,0xff};
  oxm_matches *match3 = create_oxm_matches();
  append_oxm_match_eth_type( match3, 0x0800);
  append_oxm_match_eth_src( match3,eth_src,eth_mask);
//  append_oxm_match_in_port( match2, 2);
//  dest = 1;

  openflow_actions *actions3 = create_actions();
  append_action_set_field_eth_src( actions3, set_src );
  append_action_push_mpls(actions3,0x8847);
  append_action_set_field_mpls_label(actions3,412095);
  append_action_output( actions3, 3, OFPCML_NO_BUFFER );

  openflow_instructions *insts3 = create_instructions();
  append_instructions_apply_actions( insts3, actions3 );

  buffer *flow_mod3 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match3,
    insts3
  );
  send_openflow_message( datapath_id, flow_mod3 );
  free_buffer( flow_mod3 );
  delete_oxm_matches( match3 );
  delete_instructions( insts3 );
  info("done sending");

//  sleep(2000);

  info("sending config:match mpls label -> pop mpls,  set dest eth");
  oxm_matches *match4 = create_oxm_matches();
  append_oxm_match_eth_type( match4, 0x8847);
  append_oxm_match_mpls_label(match4, 412095);

  uint8_t set_src2[ OFP_ETH_ALEN ] = {0x0,0x24,0xe8,0x77,0xa5,0x32};

  openflow_actions *actions4 = create_actions();
  append_action_pop_mpls(actions4, 0x0800);
  append_action_output( actions4, 1, OFPCML_NO_BUFFER );
  append_action_set_field_eth_src( actions4, set_src2 );

  openflow_instructions *insts4 = create_instructions();
  append_instructions_apply_actions( insts4, actions4 );

  buffer *flow_mod4 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match4,
    insts4
  );
  send_openflow_message( 0x00101856ab98 , flow_mod4);
  free_buffer( flow_mod4 );
  delete_oxm_matches( match4 );
  delete_instructions( insts4 );


  info("sending second config: match on eth src");
  uint8_t set_src3[ OFP_ETH_ALEN ] = {0x0,0x10,0x18,0x56,0xab,0xc2};
  oxm_matches *match5 = create_oxm_matches();
  append_oxm_match_eth_type( match5, 0x0800);
  
//  append_oxm_match_eth_src( match3,eth_src,eth_mask);
//  append_oxm_match_in_port( match2, 2);
//  dest = 1;

  openflow_actions *actions5 = create_actions();
  append_action_set_field_eth_src( actions5, set_src3 );
  append_action_push_mpls(actions5,0x8847);
  append_action_set_field_mpls_label(actions5,442095);
  append_action_output( actions5, 3, OFPCML_NO_BUFFER );

  openflow_instructions *insts5 = create_instructions();
  append_instructions_apply_actions( insts5, actions5 );

  buffer *flow_mod5 = create_flow_mod(
    get_transaction_id(),
    get_cookie(),
    0,
    0,
    OFPFC_ADD,
    0,
    0,
    OFP_HIGH_PRIORITY,
    OFP_NO_BUFFER,
    0,
    0,
    OFPFF_SEND_FLOW_REM,
    match5,
    insts5
  );
  send_openflow_message( datapath_id, flow_mod5 );
  free_buffer( flow_mod5 );
  delete_oxm_matches( match5 );
  delete_instructions( insts5 );
  info("done sending");





}