2 * Copyright (c) 2016 Mohamed Aslan <maslan@sce.carleton.ca>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <net/if_arp.h>
21 #include <netinet/in.h>
22 #include <netinet/if_ether.h>
29 static struct actl_ctx
*ctx
;
32 init(struct actl_ctx
*c
)
35 printf("l2_hub initialized.\n");
40 main(int argc
, char **argv
)
42 printf("L2_hub ready.\n");
47 handler(struct of_event
*ev
)
49 struct ether_header
*eh
;
50 struct of10_packet_in
*ph
;
51 struct of10_packet_out
*msg_out
;
52 struct of10_action_output
*action
;
54 if (ev
->type
!= OFEV_PROTO_MESSAGE
)
57 ph
= (struct of10_packet_in
*)(ev
->ofp_hdr
);
58 eh
= (struct ether_header
*)(ph
->data
);
61 printf("in_port: %hu, ", ntohs(ph
->in_port
));
62 printf("%x:%x:%x:%x:%x:%x ->", eh
->ether_shost
[0], eh
->ether_shost
[1],
63 eh
->ether_shost
[2], eh
->ether_shost
[3], eh
->ether_shost
[4],
65 printf(" %x:%x:%x:%x:%x:%x, ", eh
->ether_dhost
[0], eh
->ether_dhost
[1],
66 eh
->ether_dhost
[2], eh
->ether_dhost
[3], eh
->ether_dhost
[4],
68 printf("type: 0x%hx ", ntohs(eh
->ether_type
));
69 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
)
71 else if (ntohs(eh
->ether_type
) == ETHERTYPE_IP
)
77 msg_out
= (struct of10_packet_out
*)malloc(sizeof(struct of10_packet_out
) + sizeof(struct of10_action_output
));
80 msg_out
->hdr
.version
= OFP_VERSION_10
;
81 msg_out
->hdr
.type
= OFPT10_PACKET_OUT
;
82 msg_out
->hdr
.length
= htons(sizeof(struct of10_packet_out
) + sizeof(struct of10_action_output
));
84 msg_out
->buffer_id
= ph
->buffer_id
;
85 msg_out
->in_port
= ph
->in_port
;
86 msg_out
->actions_len
= htons(sizeof(struct of10_action_output
));
87 action
= (struct of10_action_output
*)(msg_out
->actions
);
88 action
->type
= htons(OFPAT10_OUTPUT
);
89 action
->len
= htons(sizeof(struct of10_action_output
));
90 action
->port
= htons(OFPP10_FLOOD
);
92 ctx
->cntl
->send(ctx
->cntl
, ev
->dp
, (struct ofp_header
*)msg_out
);