You should check if the incoming packet_in is a ipv4 packet or not. If it
is not then you will get the above error.

On Sat, Oct 27, 2018 at 7:18 PM Moh'd Reza Abbasi <mr.mrabb...@gmail.com>
wrote:
On Fri, Oct 26, 2018 at 7:12 PM Munim Shabir <munim...@hotmail.com> wrote:

> okay. So can i print in in event_packet_handler like below?
>
> whenever a packetIN comes I am able to print it's source and dst mac. but
> when I try IP the way you tell.
>
> It says
>
>
> *  dstip=pktipv4.dst #ADDED THESE FOR IP EXTRACTION **AttributeError:
> 'NoneType' object has no attribute 'dst'*
>
>
> Below is the  code with changes in BOLD
>
>     dstip=pktipv4.dst #ADDED THESE FOR IP EXTRACTION
> AttributeError: 'NoneType' object has no attribute 'dst'
>
> ---------------
>
> # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
> #
> # Licensed under the Apache License, Version 2.0 (the "License");
> # you may not use this file except in compliance with the License.
> # You may obtain a copy of the License at
> #
> #    http://www.apache.org/licenses/LICENSE-2.0
> #
> # Unless required by applicable law or agreed to in writing, software
> # distributed under the License is distributed on an "AS IS" BASIS,
> # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> # implied.
> # See the License for the specific language governing permissions and
> # limitations under the License.
>
> """
> An OpenFlow 1.0 L2 learning switch implementation.
> """
>
>
> from ryu.base import app_manager
> from ryu.controller import ofp_event
> from ryu.controller.handler import MAIN_DISPATCHER
> from ryu.controller.handler import set_ev_cls
> from ryu.ofproto import ofproto_v1_0
> from ryu.lib.mac import haddr_to_bin
> from ryu.lib.packet import packet
> from ryu.lib.packet import ethernet
> from ryu.lib.packet import ether_types
> from ryu.lib.packet import arp
> from ryu.lib.packet import ipv4
> from ryu.lib.packet.icmpv6 import icmpv6
> msgl=[]
> def modi(msg_modi):   # made this function
>     if msg_modi.in_port!=1:
>         print "this is possibly 2"
>         print msg_modi.in_port
>         print msg_modi
>     else:
>         print "this is aying on 1"
>         msgl.append(msg_modi)
>         print ("in_port", msg_modi.in_port)
>         print ("whole list", len(msgl))
>
> class SimpleSwitch(app_manager.RyuApp):
>     OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
>
>     def __init__(self, *args, **kwargs):
>         super(SimpleSwitch, self).__init__(*args, **kwargs)
>         self.mac_to_port = {}
>
>     def add_flow(self, datapath, in_port, dst, src, actions):
>         ofproto = datapath.ofproto
>
>         match = datapath.ofproto_parser.OFPMatch(
>             in_port=in_port,
>             dl_dst=haddr_to_bin(dst), dl_src=haddr_to_bin(src))
>
>         mod = datapath.ofproto_parser.OFPFlowMod(
>             datapath=datapath, match=match, cookie=0,
>             command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
>             priority=ofproto.OFP_DEFAULT_PRIORITY,
>             flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
>         datapath.send_msg(mod)
>
>     @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
>     def _packet_in_handler(self, ev):
>         msg = ev.msg
>         datapath = msg.datapath
>         ofproto = datapath.ofproto
>
>         pkt = packet.Packet(msg.data)
>         eth = pkt.get_protocol(ethernet.ethernet)
>         pktarp = pkt.get_protocol(arp.arp)
>         pktipv4 = pkt.get_protocol(ipv4.ipv4)
>            #added this
>         if eth.ethertype == ether_types.ETH_TYPE_LLDP:
>             # ignore lldp packet
>             return
>
>         dst = eth.dst
>         src = eth.src
>          #added
>
>
>
>
>
>
>
> *  dstip=pktipv4.dst #ADDED THESE FOR IP EXTRACTION *
> *        srcip=pktipv4.src #ADDED THESE FOR IP EXTRACTION *
>
>
>
>         dpid = datapath.id
>
>         self.mac_to_port.setdefault(dpid, {})
>
>         self.logger.info("packet in %s %s %s %s", dpid, src, dst,
> msg.in_port)
>
>         # learn a mac address to avoid FLOOD next time.
>         self.mac_to_port[dpid][src] = msg.in_port
>
>         if dst in self.mac_to_port[dpid]:
>             out_port = self.mac_to_port[dpid][dst]
>         else:
>
>
>
> *   print ("destination mac ADD", dst )   *
> *            print ("destination IP ADD", dstip )   #added this*
>             out_port = ofproto.OFPP_FLOOD
>
>         actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
>
>         # install a flow to avoid packet_in next time
>         if out_port != ofproto.OFPP_FLOOD:
>             self.add_flow(datapath, msg.in_port, dst, src, actions)
>
>         data = None
>         if msg.buffer_id == ofproto.OFP_NO_BUFFER:
>             data = msg.data
>
>         out = datapath.ofproto_parser.OFPPacketOut(
>             datapath=datapath, buffer_id=msg.buffer_id,
> in_port=msg.in_port,
>             actions=actions, data=data)
>         datapath.send_msg(out)
>
>     @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
>     def _port_status_handler(self, ev):
>         msg = ev.msg
>         reason = msg.reason
>         port_no = msg.desc.port_no
>
>         ofproto = msg.datapath.ofproto
>         if reason == ofproto.OFPPR_ADD:
>             self.logger.info("port added %s", port_no)
>         elif reason == ofproto.OFPPR_DELETE:
>             self.logger.info("port deleted %s", port_no)
>         elif reason == ofproto.OFPPR_MODIFY:
>             self.logger.info("port modified %s", port_no)
>         else:
>             self.logger.info("Illeagal port state %s %s", port_no, reason)
>
>
>
>
>
>
>
>
> Thank
> Munneem
>


> Yes, you can. You can do something like this.
>
> from ryu.lib.packet import packet
> from ryu.lib.packet import ipv4
>
> pkt = packet.Packet(msg.data)
> ip_pkt = pkt.get_protocol(ipv4.ipv4)
> src, dst = ip_pkt.src, ip_pkt.dst
>
> On Fri, Oct 26, 2018 at 5:52 PM Munim Shabir <munim...@hotmail.com> wrote:
>
>> Hi Guys,
>>
>> Can I extract IPv4 addresses from incoming PacketIN like we do for Src
>> dst mac addresses?
>>
>> I need src and dst IP addresses to take some decisions.
>>
>> Thanks
>> Muneem Shabir
>> +923215363267
>> _______________________________________________
>> Ryu-devel mailing list
>> Ryu-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/ryu-devel
>>
>
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to