In the [previous post](/2016/12/02/xen-a-backend-frontend-driver-example/) I explained how to initially setup a split driver
for Xen with the backend in *dom0* and the frontend in a *domU*.
This time we are taking a look at the internal states each side goes through.
Most of this code is a trimmed down version of the [Xen network driver](https://github.com/torvalds/linux/blob/bc3913a5378cd0ddefd1dfec6917cc12eb23a946/drivers/net/xen-netfront.c#L2024-L2060).
The full code can be found in [chapter 2](https://github.com/badboy/xen-split-driver-example/tree/master/chapter02) of the example repository.
## Background
The frontend part of the driver sits in an uprivileged *domU* and gets its input from the kernel in this virtual machine.
Depending on its usecase it then passed on commands what to do and the data over to the backend part,
sitting in an privileged domain such as *dom0*.
For example in case of the network driver, the *domU* kernel generates network packets
which are passed over to the backend,
which is then responsible to transfering this data to the actual network card.
Before all of this can happen both parts need to be able to communicate with each other.
Each part must probably set up a few things before it can do its job.
Some of these things must be advanced in lock-step, so each part advances to its next status
and then waits for counterpart to advance as well.
## The state machine
Internally this is all done through a state machine.
Both sides start in the `XenbusStateInitialising` state.
The goal is to reach `XenbusStateConnected` once fully setup.
If no setup is required at all, it is as easy as saying so:
~~~c
xenbus_switch_state(dev, XenbusStateConnected);
~~~
Of course a driver rarely has to do nothing at all.
Instead in each intermediate state some work can be done.
This results in a fairly large state machine on both ends, but most of it is just boilerplate.
This results in about 30 lines extra in the frontend and 100 lines in the backend.
In this blog post I will focus only on a few relevant lines.
Both sides gain another callback function, to be notified when the other side changes its state.