Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper SWIG for Java Protocol OpenFlow 1.3 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions examples/java/LearningSwitch13.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

import fluid.base.*;
import fluid.msg13.*;

public class LearningSwitch13 extends OFServer {
static {
System.loadLibrary("fluid_base");
System.loadLibrary("fluid_msg_of10");
System.loadLibrary("fluid_msg_of13");
}

static short test(short bytes) {
return bytes;
}

static Map<Integer, Map<Long, Integer>> table = new HashMap<Integer, Map<Long, Integer>>();

@Override
public void connection_callback(OFConnection conn,
OFConnection.Event event_type) {
System.out.println("## Connection id=" + conn.get_id() + " event="
+ event_type);
}

private Long get_long_mac(byte[] mac) {
ByteBuffer br = ByteBuffer.allocate(8);
br.put(mac);
br.putShort((short) 0x0000);
br.flip();
return br.getLong();
}


private void flood(OFConnection conn, PacketIn pi) {
// TODO: implement this method

}

@Override
public void message_callback(OFConnection conn, short type, byte[] data,
long len) {

System.out.println("Type Packet: "+type + " Version Protcol: "+conn.get_version());
}

public LearningSwitch13(String address, int port, int nthreads) {
super(address, port, nthreads,false,new OFServerSettings().supported_version((short)1).supported_version((short)4).keep_data_ownership(false));
}

public static void main(String argv[]) {
LearningSwitch13 l2 = new LearningSwitch13("0.0.0.0", 6653, 1);
l2.start(true);
}
}
19 changes: 14 additions & 5 deletions examples/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,36 @@ CXX=c++
SWIG=swig
JAVA_INCLUDE_PATH=/usr/lib/jvm/java-7-openjdk-amd64/include

LearningSwitch: libfluid_base libfluid_msg_of10
LearningSwitch: libfluid_base libfluid_msg_of10 libfluid_msg_of13
javac LearningSwitch.java

libfluid_base:
mkdir -p fluid/base
cd swig;\
$(SWIG) -c++ -java -package fluid.base -I/usr/include libfluid_base.i;\
$(CXX) -c libfluid_base_wrap.cxx -I$(JAVA_INCLUDE_PATH) -fPIC;\
$(SWIG) -c++ -java -package fluid.base -I/usr/local/include libfluid_base.i;\
$(CXX) -c libfluid_base_wrap.cxx -I$(JAVA_INCLUDE_PATH) -I$(JAVA_INCLUDE_PATH)/linux -fPIC;\
$(CXX) libfluid_base_wrap.o -o libfluid_base.so -shared -lfluid_base -fPIC;\
mv *.java ../fluid/base;\
mv *.so ../fluid;

libfluid_msg_of10:
mkdir -p fluid/msg
cd swig;\
$(SWIG) -c++ -java -package fluid.msg -I/usr/include libfluid_msg_of10.i;\
$(CXX) -c libfluid_msg_of10_wrap.cxx -I$(JAVA_INCLUDE_PATH) -fPIC;\
$(SWIG) -c++ -java -package fluid.msg -I/usr/local/include libfluid_msg_of10.i;\
$(CXX) -c libfluid_msg_of10_wrap.cxx -I$(JAVA_INCLUDE_PATH) -I$(JAVA_INCLUDE_PATH)/linux -fPIC;\
$(CXX) libfluid_msg_of10_wrap.o -o libfluid_msg_of10.so -shared -lfluid_msg -fPIC;\
mv *.java ../fluid/msg;\
mv *.so ../fluid;

libfluid_msg_of13:
mkdir -p fluid/msg13
cd swig;\
$(SWIG) -c++ -java -package fluid.msg13 -I/usr/local/include libfluid_msg_of13.i;\
$(CXX) -c libfluid_msg_of13_wrap.cxx -I$(JAVA_INCLUDE_PATH) -I$(JAVA_INCLUDE_PATH)/linux -fPIC;\
$(CXX) libfluid_msg_of13_wrap.o -o libfluid_msg_of13.so -shared -lfluid_msg -fPIC;\
mv *.java ../fluid/msg13;\
mv *.so ../fluid;

clean:
cd swig;\
ls | grep -v "\.i$$" | xargs rm -f;
Expand Down
10 changes: 10 additions & 0 deletions examples/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ Swig 2.0 and the JDK (`openjdk-7-jdk` package on Ubuntu 12.04).
So do what's needed first, and then run:
~~~{.sh}
$ make
OpenFlow 1.0
$ LD_LIBRARY_PATH=./fluid:$LD_LIBRARY_PATH java LearningSwitch
OR OpenFlow 1.3
$ LD_LIBRARY_PATH=./fluid:$LD_LIBRARY_PATH java LearningSwitch13
~~~

It is an OpenFlow 1.0 learning switch test application.

The Java code is in `LearningSwitch.java`.

It is an OpenFlow 1.3 learning switch test application.
The Java code is in `LearningSwitch13.java`.

> This example uses the new, IANA-defined OpenFlow port: **6653** instead of
> the previous, non-standard default (6633).
> Exemple Conection Mininet Openflow Protocolo Version 1.0:
> > sudo mn -v output --topo linear,5 --switch ovs,protocols=OpenFlow10 --controller remote,ip=[IP],port=6653
Or Version 1.3
> > sudo mn -v output --topo linear,5 --switch ovs,protocols=OpenFlow13 --controller remote,ip=[IP],port=6653
65 changes: 65 additions & 0 deletions examples/java/swig/libfluid_msg_of13.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* File: example.i */
%module(directors="1") libfluid_msg_of13
#pragma SWIG nowarn=362,503,509
%{
/* Includes the header in the wrapper code */
typedef unsigned int of_error;
#include <fluid/of13msg.hh>
%}
%include "stdint.i"
%include "cdata.i"
%include "various.i"

%typemap(jni) (uint8_t *) "jbyteArray"
%typemap(jni) (void *) "jbyteArray"
%typemap(jtype) (uint8_t *) "byte[]"
%typemap(jtype) (void *) "byte[]"
%typemap(jstype) (uint8_t *) "byte[]"
%typemap(jstype) (void *) "byte[]"

%typemap(in) void* {
$1 = (void *) JCALL2(GetByteArrayElements, jenv, $input, 0);
}

%typemap(in) uint8_t* {
$1 = (uint8_t *) JCALL2(GetByteArrayElements, jenv, $input, 0);
}

%typemap(javain) (uint8_t *) "$javainput"
%typemap(javain) (void *) "$javainput"

%typemap(out, descriptor="[B") void* data() {
$result = (jenv)->NewByteArray(arg1->data_len());
(jenv)->SetByteArrayRegion($result, 0, arg1->data_len(), (jbyte*) $1);
}

%typemap(out, descriptor="[B") uint8_t* {
int len = htons(*((uint16_t*) $1 + 1));
$result = (jenv)->NewByteArray(len);
(jenv)->SetByteArrayRegion($result, 0, len, (jbyte*) $1);
}

%typemap(javaout) (uint8_t *) {
return $jnicall;
}

%typemap(javaout) (void *) {
return $jnicall;
}

%include "fluid/util/ethaddr.hh"
%include "fluid/util/ipaddr.hh"

%include "fluid/ofcommon/action.hh"
%include "fluid/ofcommon/common.hh"
%include "fluid/ofcommon/msg.hh"

%include "fluid/of13/of13match.hh"
%include "fluid/of13/of13instruction.hh"
%include "fluid/of13/of13action.hh"
%include "fluid/of13/of13common.hh"
%include "fluid/of13/of13meter.hh"

%include "fluid/of13msg.hh"

typedef unsigned int of_error;