Skip to content

Instantly share code, notes, and snippets.

View oshothebig's full-sized avatar

Sho Shimizu oshothebig

  • Preferred Networks, Inc.
  • Tokyo, Japan
View GitHub Profile
private static class OpenFlowDecoder extends OneToOneDecoder {
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (!(msg instanceof ChannelBuffer)) {
return msg;
}
ChannelBuffer channelBuffer = (ChannelBuffer)msg;
ByteBuffer byteBuffer = channelBuffer.toByteBuffer();
List<OFMessage> messages = factory.parseMessages(byteBuffer);
private static class OpenFlowEncoder extends OneToOneEncoder {
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (msg instanceof OFMessage) {
OFMessage response = (OFMessage) msg;
// may have an bad effect on performance
ByteBuffer buffer = ByteBuffer.allocate(response.getLength());
response.writeTo(buffer);
buffer.flip();
return ChannelBuffers.wrappedBuffer(buffer);
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
// add the binary codec combination first
pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(
MAXIMUM_PACKET_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH, LENGTH_FIELD_MODIFICATION, 0));
pipeline.addLast("decoder", new OpenFlowDecoder());
pipeline.addLast("encoder", new OpenFlowEncoder());
// add then the business logic
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (e.getMessage() instanceof OFMessage) {
OFMessage in = (OFMessage)e.getMessage();
log.debug("Message received: message({}), switch({})", in.getType(), ctx.getChannel().getRemoteAddress());
switch (in.getType()) {
case HELLO:
ctx.getChannel().write(factory.getMessage(OFType.FEATURES_REQUEST));
case ECHO_REQUEST:
int xid = in.getXid();
OFMessage out = factory.getMessage(OFType.ECHO_REPLY);
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
OFMessage out = factory.getMessage(OFType.HELLO);
ctx.getChannel().write(out);
}
public static void main(String[] args) {
// リピータハブとして動作
MessageListener hub = new MessageListener() {
public void messageReceived(Channel channel, OFMessage msg) {
if (msg.getType() == OFType.PACKET_IN) {
OFPacketIn in = (OFPacketIn)msg;
OFPacketOut out = (OFPacketOut)factory.getMessage(OFType.PACKET_OUT);
out.setBufferId(in.getBufferId());
out.setInPort(in.getInPort());
@oshothebig
oshothebig / gist:1191031
Created September 3, 2011 11:19
ofp_header
struct ofp_header {
uint8_t version; /* OFP_VERSION. */
uint8_t type; /* One of the OFPT_ constants. */
uint16_t length; /* Length including this ofp_header. */
uint32_t xid; /* Transaction id associated with this packet.
Replies use the same id as was in the request
to facilitate pairing. */
}
OFP_ASSERT(sizeof(struct ofp_header) == 8);
@oshothebig
oshothebig / SimpleController.java
Created September 3, 2011 12:56
Simple OpenFlow Controller
/*
* Copyright (c) 2011, Sho SHIMIZU
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
OFMessage in = (OFMessage) e.getMessage();
OFMessage out;
switch (in.getType()) {
case HELLO:
break;
case ECHO_REQUEST:
out = fakeSwitch.echoReplyData((OFEchoRequest)in);
ctx.getChannel().write(out);
@oshothebig
oshothebig / ovs-lines.txt
Created March 19, 2012 13:05
Lines of code of Open vSwitch
486 ./datapath/actions.c
578 ./datapath/brcompat_main.c
271 ./datapath/checksum.c
149 ./datapath/checksum.h
84 ./datapath/compat.h
2241 ./datapath/datapath.c
195 ./datapath/datapath.h
74 ./datapath/dp_notify.c
38 ./datapath/dp_sysfs.h
419 ./datapath/dp_sysfs_dp.c