Sending packets from separate application to Ryu - sockets

I am trying to send data from a Northbound application that I have written, into the Ryu application.
The NB app is performing a lot of computation, so I have chosen to run it outside of Ryu. How do I send this information into the Ryu controller so the controller can act on it.
This set up would look like this:
[Northbound packet sender with actionable information (not in the SDN dataplane!!)]
↓
[Ethernet 1 (if on a different machine), or the Linux localhost (if on the same machine)]
↓
[Ryu Machine]
↓
[Ethernet 2 (using OpenFlow)]
↓
[Data plane]
In Pox I have simply set up a thread that can receive packets on a port. When it receives a packet it just creates an event, and my main SDN app can then use this event to perform the network changes needed. When I try this approach in Ryu, it just doesn't work. It seems that when I try to register a socket inside a Ryu app, Ryu will not run until that functionality has been removed.
Any advice?
Thanks in advance,

Related

J1939 Communication over CAN, J1939 Protocol Stack

I need some guidance on how to use the J1939 library for the Simulink Real-time toolbox.
I am using the previous version which is 6.7 (R2017b). There is very little documentation or example on it.
What I want to achieve is to send a CAN message on the channel 1 and receive it on channel 2.
Can anyone share the steps on how to send a frame, with the setup of the different blocks.
In my model I have considered the blocks:
J1939 Database where I added the dbc and it is fine
J1939 Controller Application : I just used the General tab. I am not about the Network management tab content, where I have put Arbitrary Address Capable
J1939 Protocol Stack: Not sure what I can connect to the inputs or outputs of the block: CAN Msg and N?
J1939 Transmit message, I added the frame and it seems ok
J1939 Receive message, I also added the same frame and the block seems ok too
Setup block CAN-AC2-PCI B1 FIFO: CAN1 / CAN2 to manage the CAN board which also seem to be OK
At the moment with the way I configured them, I am seeing any frame sent on the bus.
Can anyone share with me how to configure the blocks?
Many thanks for your support.

Connect mavlink (on RPi) with QGroundControl

I am trying to connect Mavlink (installed on RPi) with QGroundControl via 3DR Radio Telemetry as to transmit telemetry data from RPi without dependency of Ardupilot/Px4 flightcontrollers. I want to achieve transmitting data such as GPS or Postion using the message format of mavlink without dependency of ardupilot/PX4.
So far, I have taken the example code from mavlink git and have built the code as per instructions. I start the QGroundControl and was able receive the System ID and component ID from QGC. I modified the code to transmit a heartbeat from RPI-mavlink to QGroundControl.
mavlink_msg_heartbeat_pack(system_id, 200, &msg, MAV_TYPE_HELICOPTER, MAV_AUTOPILOT_GENERIC, MAV_MODE_GUIDED_ARMED, 0, MAV_STATE_ACTIVE);
int len_hb = write_message(msg);
After building and executing this code, I was expecting the QGroundControl to connect/show received message, but nothing was available at the QGroundControl.
I need to understand if what I am doing is the right way to do or if there is predefined method to establish such kind of communication. Kindly, help me out with documentations (if any) available to accomplish message transmission without ardupilot/Px4. If someone could guide me on this issue, it would be really helpful.
Thanks
If you use RPi you not need the 3DR Radio Telemetry. You can connect RPi by WiFi or LTE to send/receive the Mavlink data. You need:
Wire the RPi to the flight controller;
Setup the Mavlink port in the QGroundControl, let the flight controller send the data to a telemetry port;
Install the mavlink-router to RPI;
Setup the mavlink-router send data to groundstation;
When you can receive mavlink data in the groundstation, you use pymavlink to send mavlink message. There is a sample code
If I have understood the question correctly, then what you are trying to do is to develop your own flight controller using raspberry pi. And, you want it to be detected as a vehicle in the QGC.
The example code that you are referring to from mavlink repository is used on the ground station to receive messages from a flight controller and send command messages to it. However, you are trying to send message from flight controller to the ground station.
What you will first have to do is to keep sending a heartbeat message at the rate of 1Hz as described in heartbeat microservice on mavlink documentation page. QGroundControl will detect a vehicle if it receives heartbeats from a vehicle I guess for about 5 seconds continuously. You have to keep sending the heartbeat at 1Hz after that as well, otherwise the QGC will state communication loss even if you are sending other mavlink messages to it.
On top of that, there are many mavlink messages which can have different interpretation for different flight controller. So, at some point you either will have to match your code to a certain extent to an existing flight controller or you will have to modify QGC to work with how you implement your flight controller.

Power states notifications in a Linux daemon

I have a Linux daemon and I want to be notified about the standard power states changes: sleep/resume, hibernate.
Right now I'm using pm-utils hooks, but I wonder if there is something similar to udev (by registering you can receive notifications about devices: add/remove/change/move).
acpid seems to be designed to notify user-space programs of ACPI events. I don't know if it can be used for a daemon.
As far as I know, there is no uevent broadcaster which sends sleep/wakeup related event. But you can send a uevent by yourself writing a driver which generates and sends one as soon as receiving PM_SUSPEND_PREPARE/PM_POST_SUSPEND. To do this, you should implement a notification block which receives PM_* notification and register it to the PM notifier chain using register_pm_notifier(&your_notification_block). It means that you can write a translator driver which translates PM_* notification to a uevent.
But, when the system is going to sleep, the kernel freezes all processes prior to suspending. It means, although your daemon receives the uevent, there will be no time to do something you want.

Sending and receiving data over Internet

This question is not for a concrete implementation of how this is done. It is more about the concept and design of sending information over Internet with some kind of protocol - either TCP or UDP. I know only that sockets are needed, but I am wondering about the rest. For example after a connection is made and you send the information through that, but how does the other end listen for a specific port and does it listen constantly?
Is listening done in a background thread waiting for information to be received? (In order to be able to do other things/processing while waiting for information)
So in essence, I think a real world example of how such an application works on a high level would be enough to explain the data flow. For example sending files in Skype or something similar.
P.S. Most other questions on similar topics are about a concrete implementation or a bug that someone has.
What I currently do in an application is the following using POSIX sockets with the TCP Protocol:
Most important thing is: The most function are blocking functions. So when you tell your server to wait for client connection, the function will block until a connection is established (if you need a server that handles multiple clients at once, you need to use threading!)
Server listens for specific port until a client connects. After the connect, you will get a new socket file descriptor to communicate with the client whilst the initial socket can listen to new connections. My server then creats a new thread to handle that client whilst waiting for new connections on the initial socket. In the new thread the server waits for a request command from the Client (e.g. Request Login Token). After a request was received by the server, the server will gather its informations, packs it together using Googles Protocol Buffers and sends it to the client. The client now either tells the server to terminate the session (if every data is received by the client that it needs) or send another request.
Thats basically the idea in my server. The bigger problem is the way you transmit and receive data. E.g. you cant send structs or classes (at least not via C++) over the wire, you need some kind of serializer and you have to make sure the other part knows how much to receive. So what i do is, first send a 4byte integer over the wire containing the size of the incomming package, then send the package itself using a serializer (in my case Googles Protocol buffers). The other side waits for 4 byte to be available, knowing that this will be the size of the incomming package. After 4 bytes are received, the program waits for exact that amount of data being available on the socket, when available, read the data out of the buffer and deserialize it. When the socket is not receiving data for 30 seconds, trigger a timeout and terminate the connection.
What you always need to be aware of is the endianess of the systems. E.g. a big endian system (e.g. PowerPC) and a little endian system (e.g. x86) will have problems when you send an integer directly over the wire. For example a
0001
on the x86, is a
1000
on the Power PC, thus making a 8 out of a 1. So you should always use functions like ntohl, an htonl, which will convert data from and to host byte order from and to network byte order (network byte order is always big endian).
Hope this kind of helps. I could also provide some code to you if that would help.

intercept packet in kernel and pass in userspace

Assume that I implemented a kernel driver that parses RX packet and decides to pass it to the user space depending on EthType. What are the "official" ways to do that in the Linux kernel?
The only one that comes on my mind is the user application opens a socket to the kernel and listens on it, while the kernel pushes packets satisfying criteria (eg. specific EthType) in to the socket's buffer. I'm certainly not accurate about this, but I hope you get my point :)
Are there any other ways to do this?
Thanks.
You can achieve your goal by using Netfilter framework. Netfilter framework helps intercept a ingrees/egrees packet. The points where packets can be intercepted inside the Kernel/Network stack are called as HOOKS in Netfilter.We can write a kernel module, which can get hooked at any of these HOOKS. The kernel module must have a function defined by us, which can parse the packet and its headers and than take a decision to whether drop a packet, send it to kernel stack, queue it to user space etc.
The packet of our interest can be intercepted at IP_PREROUTING hook and queued by returning NF_QUEUE from our function. The packets will be queued and can be accessed by any application.
Please go through Netfilter documentation.
Regards,
Roy
When the packet arrives on the NIC, these packets are first copied onto the kernel buffers and then copied onto the user space, which are accessed through the socket() followed by read()/write() calls in the user space. You may want to refer to Kernel Network Flow for more details.
Additionally, NIC can directly copy the packets into the DMA bypassing the CPU. Refer to: What happens after a packet is captured?