[dpdk-dev,00/22] enable hotplug on multi-process

Message ID 20180607123849.14439-1-qi.z.zhang@intel.com (mailing list archive)
Headers
Series enable hotplug on multi-process |

Message

Qi Zhang June 7, 2018, 12:38 p.m. UTC
  Background:
===========

Currently secondary process will only sync ethdev from primary
process at init stage, but it will not be aware if device
is attached/detached on primary process at runtime.

While there is the requirement from application that take
primary-secondary process model. The primary process work as a
resource management process, it will create/destroy virtual device
at runtime, while the secondary process deal with the network stuff
with these devices.

Solution:
=========

So the orignial intention is to fix this gap, but beyond that
the patch set provide a more comprehesive solution to handle
different hotplug cases in multi-process situation, it cover below
scenario:

1. Attach a share device from primary
2. Detach a share device from primary
3. Attach a share device from secondary
4. Detach a share device from secondary
5. Attach a private device from secondary
6. Detach a private device from secondary
7. Detach a share device from secondary privately
8. Attach a share device from secondary privately

In primary-secondary process model, we assume ethernet devices are
shared by default. that means attach or detach a device on any process will
broadcast to all other processes through mp channel then device information
will be synchronized on all processes.

Any failure during attaching process will cause inconsistent status
between processes, so proper rollback action should be considered.
Also, it is not safe to detach a share device when other process still
use it, so a handshake mechanism is introduced.

Scenario for Case 1, 2:

attach device from primary
a) primary attach the new device if failed goto h).
b) primary send attach sync request to all secondary.
c) secondary receive request and attach device and send reply.
d) primary check the reply if all success go to i).
e) primary send attach rollback sync request to all secondary.
f) secondary receive the request and detach device and send reply.
g) primary receive the reply and detach device as rollback action.
h) attach fail
i) attach success

detach device from primary
a) primary perform pre-detach check, if device is locked, goto i).
b) primary send pre-detach sync request to all secondary.
c) secondary perform pre-detach check and send reply.
d) primary check the reply if any fail goto i).
e) primary send detach sync request to all secondary
f) secondary detach the device and send reply (assume no fail)
g) primary detach the device.
h) detach success
i) detach failed

Scenario for case 3, 4:

attach device from secondary:
a) seconary send asycn request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and attach the new device if failed
   goto i).
c) primary forward attach request to all secondary as async request
   (because this in mp thread context, use sync request will deadlock,
    same reason for all following async request.)
d) secondary receive request and attach device and send reply.
e) primary check the reply if all success go to j).
f) primary send attach rollback async request to all secondary.
g) secondary receive the request and detach device and send reply.
h) primary receive the reply and detach device as rollback action.
i) send fail response to secondary, goto k).
j) send success response to secondary.
k) secondary process receive response and return.

detach device from secondary:
a) secondary send async request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and  perform pre-detach check, if device
   is locked, goto j).
c) primary send pre-detach async request to all secondary.
d) secondary perform pre-detach check and send reply.
e) primary check the reply if any fail goto j).
f) primary send detach async request to all secondary
g) secondary detach the device and send reply
h) primary detach the device.
i) send success response to secondary, goto k).
j) send fail response to secondary.
k) secondary process receive response and return.

Case 5, 6:
Secondary process can attach private device which only visible to
itself, in this case no IPC is involved, primary process is not allowed
to have private device so far.

Case 7, 8:
Secondary process can also temporally to detach a share device
"privately" then attach it back later, this action also not impact other
processes.

APIs chenages:
==============

rte_eth_dev_attach and rte_eth_dev_attach are extended to support
share device attach/detach in primary-secondary process model, it will
be called in case 1,2,3,4.

New API rte_eth_dev_attach_private and rte_eth_dev_detach_private are
introduced to cover case 5,6,7,8, this API can only be invoked in
secondary process.

New API rte_eth_dev_lock and rte_eth_dev_unlock are introduced to let
application lock or unlock on specific ethdev, a locked device
can't be detached. This help applicaiton to prevent unexpected
device detaching, especially in multi-process envrionment.
Aslo the new API let application to register a callback function
which will be invoked before a device is going to be detached,
the return value of the function will decide if device will continue
be detached or not, this support application to do condition check
at runtime.

PMD Impact:
===========

Currently device removing is not handled well in secondary process on most
pmd drivers, rte_eth_dev_relase_port will be invoked and will mess up
primary process since it reset all shared data. So we introduced new API
rte_eth_dev_release_port_local which only reset ethdev's state to unsued but
not touch shared data so other process will not be impacted.
Since not all device driver is target to support primary-secondary
process model, so the patch set only fix this on all Intel devices and
vdev, it can be refereneced by other driver when equevalent fix is required

Limitation:
===========

The solution does not cover the case that primary process exit while
secondary processes still be active. Though this is not a typial use
case, but if this happens:
1. secondary process can't attach / detach any shared device since no
primary exist.
2. secondary process still can attach / detach private device.
3. secondary process still can detach a share device privately but may
not attach it back, that ethdev slot will become zombie slot.

Example:
========

The patchset also contains a example to demonstrate device management
in multi-process model, below are detail instructions.

/* start sample code as primary then secondary */
./devmgm_mp --proc-type=auto

Command Line Example:

>help
>list

/* attach a af_packet vdev */
>attach net_af_packet,iface=eth0

/* detach port 0 */
>detach 0

/* attach a private af_packet vdev (secondary process only)*/
>attachp net_af_packet,iface=eth0

/* detach a private device (secondary process only) */
>detachp 0

/* lock port 0 */
>lock 0

/* unlock port 0 */
>unlock 0


Qi Zhang (22):
  eal: introduce one device scan
  bus/vdev: enable one device scan
  ethdev: add function to release port in local process
  ethdev: enable hotplug on multi-process
  ethdev: introduce device lock
  ethdev: support attach or detach share device from secondary
  net/i40e: enable port detach on secondary process
  net/ixgbe: enable port detach on secondary process
  net/e1000: enable port detach on secondary process
  net/igb: enable port detach on secondary process
  net/fm10k: enable port detach on secondary process
  net/af_packet: enable port detach on secondary process
  net/bonding: enable port detach on secondary process
  net/failsafe: enable port detach on secondary process
  net/kni: enable port detach on secondary process
  net/null: enable port detach on secondary process
  net/octeontx: enable port detach on secondary process
  net/pcap: enable port detach on secondary process
  net/softnic: enable port detach on secondary process
  net/tap: enable port detach on secondary process
  net/vhost: enable port detach on secondary process
  examples/devmgm_mp: add simple device management sample

 drivers/bus/vdev/vdev.c                   |  30 ++
 drivers/net/af_packet/rte_eth_af_packet.c |  11 +
 drivers/net/bonding/rte_eth_bond_pmd.c    |  11 +
 drivers/net/e1000/em_ethdev.c             |   9 +
 drivers/net/e1000/igb_ethdev.c            |   9 +
 drivers/net/failsafe/failsafe.c           |  16 +
 drivers/net/fm10k/fm10k_ethdev.c          |   9 +
 drivers/net/i40e/i40e_ethdev.c            |   2 +
 drivers/net/i40e/i40e_ethdev_vf.c         |   9 +
 drivers/net/ixgbe/ixgbe_ethdev.c          |  12 +
 drivers/net/kni/rte_eth_kni.c             |  11 +
 drivers/net/null/rte_eth_null.c           |  16 +-
 drivers/net/octeontx/octeontx_ethdev.c    |  13 +
 drivers/net/pcap/rte_eth_pcap.c           |  15 +-
 drivers/net/softnic/rte_eth_softnic.c     |  19 +-
 drivers/net/tap/rte_eth_tap.c             |  17 +-
 drivers/net/vhost/rte_eth_vhost.c         |  11 +
 examples/devmgm_mp/Makefile               |  64 +++
 examples/devmgm_mp/commands.c             | 383 ++++++++++++++++++
 examples/devmgm_mp/commands.h             |  10 +
 examples/devmgm_mp/main.c                 |  41 ++
 examples/devmgm_mp/meson.build            |  11 +
 lib/librte_eal/common/eal_common_dev.c    |  17 +-
 lib/librte_eal/common/eal_private.h       |   8 +
 lib/librte_eal/common/include/rte_bus.h   |   4 +
 lib/librte_eal/linuxapp/eal/eal.c         |   6 +
 lib/librte_ethdev/Makefile                |   2 +
 lib/librte_ethdev/rte_ethdev.c            | 246 ++++++++++-
 lib/librte_ethdev/rte_ethdev.h            | 101 +++++
 lib/librte_ethdev/rte_ethdev_core.h       |   5 +
 lib/librte_ethdev/rte_ethdev_driver.h     |  40 ++
 lib/librte_ethdev/rte_ethdev_lock.c       | 102 +++++
 lib/librte_ethdev/rte_ethdev_lock.h       |  23 ++
 lib/librte_ethdev/rte_ethdev_mp.c         | 653 ++++++++++++++++++++++++++++++
 lib/librte_ethdev/rte_ethdev_mp.h         |  45 ++
 35 files changed, 1950 insertions(+), 31 deletions(-)
 create mode 100644 examples/devmgm_mp/Makefile
 create mode 100644 examples/devmgm_mp/commands.c
 create mode 100644 examples/devmgm_mp/commands.h
 create mode 100644 examples/devmgm_mp/main.c
 create mode 100644 examples/devmgm_mp/meson.build
 create mode 100644 lib/librte_ethdev/rte_ethdev_lock.c
 create mode 100644 lib/librte_ethdev/rte_ethdev_lock.h
 create mode 100644 lib/librte_ethdev/rte_ethdev_mp.c
 create mode 100644 lib/librte_ethdev/rte_ethdev_mp.h
  

Comments

Burakov, Anatoly June 15, 2018, 3:16 p.m. UTC | #1
Hi Qi,

I haven't read the code yet, and i'll be the first to admit that i'm not 
too well versed on how shared/private device data works, so my apologies 
in advance if all of below comments are addressed by implementation 
details or are way off base!



On 07-Jun-18 1:38 PM, Qi Zhang wrote:
> 
> 1. Attach a share device from primary
> 2. Detach a share device from primary
> 3. Attach a share device from secondary
> 4. Detach a share device from secondary
> 5. Attach a private device from secondary
> 6. Detach a private device from secondary
> 7. Detach a share device from secondary privately
> 8. Attach a share device from secondary privately
> 

<...>

> Case 7, 8:
> Secondary process can also temporally to detach a share device
> "privately" then attach it back later, this action also not impact other
> processes.
>

Do we really need to implement these cases? It seems to me that this 
"reattach it later" introduces unnecessary complexity. If secondary has 
detached the device, i think it is safer if we cannot reattach it, 
period, because it was a shared device. What if we try to attach it when 
a handshake has already completed and all other processes expect to 
detach it?

(in fact, do we differentiate between non-existent device and shared 
device that has been "privately detached"? I would expect that we keep 
the device as detached as opposed to forgetting about it, so that, come 
handshake, we can safely reply "yeah, we can detach the device", but 
maybe it's OK to not treat request to detach a non-existent device as an 
error... thoughts? am i getting something wrong?)

> APIs chenages:
> ==============
> 
> rte_eth_dev_attach and rte_eth_dev_attach are extended to support
> share device attach/detach in primary-secondary process model, it will
> be called in case 1,2,3,4.
> 
> New API rte_eth_dev_attach_private and rte_eth_dev_detach_private are
> introduced to cover case 5,6,7,8, this API can only be invoked in
> secondary process.
> 
> New API rte_eth_dev_lock and rte_eth_dev_unlock are introduced to let
> application lock or unlock on specific ethdev, a locked device
> can't be detached. This help applicaiton to prevent unexpected
> device detaching, especially in multi-process envrionment.
> Aslo the new API let application to register a callback function
> which will be invoked before a device is going to be detached,
> the return value of the function will decide if device will continue
> be detached or not, this support application to do condition check
> at runtime.

I assume that you've added device locking to avoid having to do 
handshake before detach, correct? Is this a shared lock of some kind, or 
is it a private lock? If it's shared lock, what happens if the process 
holding that lock crashes?

> 
> PMD Impact:
> ===========
> 
> Currently device removing is not handled well in secondary process on most
> pmd drivers, rte_eth_dev_relase_port will be invoked and will mess up
> primary process since it reset all shared data. So we introduced new API
> rte_eth_dev_release_port_local which only reset ethdev's state to unsued but
> not touch shared data so other process will not be impacted.
> Since not all device driver is target to support primary-secondary
> process model, so the patch set only fix this on all Intel devices and
> vdev, it can be refereneced by other driver when equevalent fix is required

Nitpick - why the naming mismatch between *_private() and *_local()?

> 
> Limitation:
> ===========
> 
> The solution does not cover the case that primary process exit while
> secondary processes still be active. Though this is not a typial use
> case, but if this happens:
> 1. secondary process can't attach / detach any shared device since no
> primary exist.
> 2. secondary process still can attach / detach private device.
> 3. secondary process still can detach a share device privately but may
> not attach it back, that ethdev slot will become zombie slot.

I think this should be explicit and by design. Shared devices can only 
be communicated to all secondaries through a primary process. No primary 
- no shared devices. I don't think we can do anything about it unless we 
implement some kind of peer-to-peer IPC (which isn't happening as far as 
i'm aware).



Thanks for your work on this patchset!
  
Qi Zhang June 19, 2018, 2:43 a.m. UTC | #2
Hi Anatoly:

	Thanks for the review, see my reply in inline.

> -----Original Message-----
> From: Burakov, Anatoly
> Sent: Friday, June 15, 2018 11:16 PM
> To: Zhang, Qi Z <qi.z.zhang@intel.com>; thomas@monjalon.net
> Cc: Ananyev, Konstantin <konstantin.ananyev@intel.com>; dev@dpdk.org;
> Richardson, Bruce <bruce.richardson@intel.com>; Yigit, Ferruh
> <ferruh.yigit@intel.com>; Shelton, Benjamin H
> <benjamin.h.shelton@intel.com>; Vangati, Narender
> <narender.vangati@intel.com>
> Subject: Re: [PATCH 00/22] enable hotplug on multi-process
> 
> Hi Qi,
> 
> I haven't read the code yet, and i'll be the first to admit that i'm not too well
> versed on how shared/private device data works, so my apologies in advance
> if all of below comments are addressed by implementation details or are way
> off base!
> 
> 
> 
> On 07-Jun-18 1:38 PM, Qi Zhang wrote:
> >
> > 1. Attach a share device from primary
> > 2. Detach a share device from primary
> > 3. Attach a share device from secondary 4. Detach a share device from
> > secondary 5. Attach a private device from secondary 6. Detach a
> > private device from secondary 7. Detach a share device from secondary
> > privately 8. Attach a share device from secondary privately
> >
> 
> <...>
> 
> > Case 7, 8:
> > Secondary process can also temporally to detach a share device
> > "privately" then attach it back later, this action also not impact other
> > processes.
> >
> 
> Do we really need to implement these cases? It seems to me that this
> "reattach it later" introduces unnecessary complexity. 

I agree it's not necessary, but this looks like a free feature based on current implementation :)


>If secondary has
> detached the device, I think it is safer if we cannot reattach it,
> period, because it was a shared device. What if we try to attach it when
> a handshake has already completed and all other processes expect to
> detach it?

in the case: attach back a shared device already be detached will fail as expected.

For PCI devices, it will fail at driver probe.
For vdev, it will failed at rte_eth_dev_attach_secondary.

> 
> (in fact, do we differentiate between non-existent device and shared
> device that has been "privately detached"? I would expect that we keep
> the device as detached as opposed to forgetting about it, so that, come
> handshake, we can safely reply "yeah, we can detach the device", but
> maybe it's OK to not treat request to detach a non-existent device as an
> error... thoughts? am i getting something wrong?)
> 
> > APIs chenages:
> > ==============
> >
> > rte_eth_dev_attach and rte_eth_dev_attach are extended to support
> > share device attach/detach in primary-secondary process model, it will
> > be called in case 1,2,3,4.
> >
> > New API rte_eth_dev_attach_private and rte_eth_dev_detach_private are
> > introduced to cover case 5,6,7,8, this API can only be invoked in
> > secondary process.
> >
> > New API rte_eth_dev_lock and rte_eth_dev_unlock are introduced to let
> > application lock or unlock on specific ethdev, a locked device
> > can't be detached. This help applicaiton to prevent unexpected
> > device detaching, especially in multi-process envrionment.
> > Aslo the new API let application to register a callback function
> > which will be invoked before a device is going to be detached,
> > the return value of the function will decide if device will continue
> > be detached or not, this support application to do condition check
> > at runtime.
> 
> I assume that you've added device locking to avoid having to do
> handshake before detach, correct? 
Yes.
>Is this a shared lock of some kind, or
> is it a private lock? If it's shared lock, what happens if the process
> holding that lock crashes?

It’s a kind of process's private lock, but a shared device be locked any process will prevent it be detached.

> 
> >
> > PMD Impact:
> > ===========
> >
> > Currently device removing is not handled well in secondary process on
> most
> > pmd drivers, rte_eth_dev_relase_port will be invoked and will mess up
> > primary process since it reset all shared data. So we introduced new API
> > rte_eth_dev_release_port_local which only reset ethdev's state to unsued
> but
> > not touch shared data so other process will not be impacted.
> > Since not all device driver is target to support primary-secondary
> > process model, so the patch set only fix this on all Intel devices and
> > vdev, it can be refereneced by other driver when equevalent fix is required
> 
> Nitpick - why the naming mismatch between *_private() and *_local()?
Agree.
"private" make the API more identical.

> 
> >
> > Limitation:
> > ===========
> >
> > The solution does not cover the case that primary process exit while
> > secondary processes still be active. Though this is not a typial use
> > case, but if this happens:
> > 1. secondary process can't attach / detach any shared device since no
> > primary exist.
> > 2. secondary process still can attach / detach private device.
> > 3. secondary process still can detach a share device privately but may
> > not attach it back, that ethdev slot will become zombie slot.
> 
> I think this should be explicit and by design. Shared devices can only
> be communicated to all secondaries through a primary process. No primary
> - no shared devices. I don't think we can do anything about it unless we
> implement some kind of peer-to-peer IPC (which isn't happening as far as
> i'm aware).

Agree.

> 
> 
> 
> Thanks for your work on this patchset!

Thanks for the design review and all the helpful inputs.

Regards
Qi

> 
> --
> Thanks,
> Anatoly
  
Qi Zhang June 28, 2018, 1:49 a.m. UTC | #3
v6:
- remove bus->scan_one, since ABI break is not necessary.
- remove patch for failsafe PMD since it will not support secondary.
- fix wrong implemenation on ixgbe.
- add rte_eth_dev_release_port_private into rte_eth_dev_pci_generic_remove for
  secondary process, so we don't need to patch on PMD if PMD use the
  default remove function.
- add release notes update.

v5:
- since we will keep mp thread separate from interrupt thread,
  it is not necessary to use temporary thread, we use rte_eal_alarm_set.
- remove the change in rte_eth_dev_release_port, since there is a better
  way to prevent rte_eth_dev_release_port be called after
  rte_eth_dev_release_port_private.
- fix the issue that lock does not take effect on secondary due to
  previous re-work
- fix the issue when the first attached device is a private device from
  secondary. (patch 8/24)
- work around for reply a sync request in separate thread, this is still
  an open and in discussion as below.
  https://mails.dpdk.org/archives/dev/2018-June/105359.html

v4:
- since mp thread will be merged to interrupt thread, the fix on v3
  for sync IPC deadlock will not work. the new version enable the
  machanism to invoke a mp action callback in a temporary thread to
  avoid the IPC deadlock, with this, secondary to primary request
  impelemtation also be simplified, since we can use sync request
  directly in a separate thread.

v3:
- enable mp init callback register to help non-eal module to initialize
  mp channel during rte_eal_init
- fix when attach share device from secondary.
  1) dead lock due to sync IPC be invoked in rte_malloc in primary
     process when handle secondary request to attach device, the
     solution is primary process to issue share device attach/detach
     in interrupt thread.
  2) return port_id not correct.
- check nb_sent and nb_received in sync IPC.
- fix memory leak duirng error handling at attach_on_secondary.
- improve clean_lock_callback to only lock/unlock spinlock once
- improve error code return in check-reply during async IPC.
- remove rte_ prefix of internal function in ethdev_mp.c
- sample code improvement.
  1) rename sample to "hotplug_mp", and move to example/multi-process.
  2) cleanup header include.
  3) call rte_eal_cleanup before exit.

v2:
- rename rte_ethdev_mp.* to ethdev_mp.*
- rename rte_ethdev_lock.* to ethdev_lock.*
- move internal funciton to ethdev_private.h
- separate rte_eth_dev_[un]lock into rte_eth_dev_[un]lock and
  rte_eth_dev_[un]lock_with_callback
- lock callbacks will be removed automatically after device is detached.
- add experimental tag for all new APIs.
- fix coding style issue.
- fix wrong lisence header in sample code.
- fix spelling 
- fix meson.build.
- improve comments. 

Background:
===========

Currently secondary process will only sync ethdev from primary
process at init stage, but it will not be aware if device
is attached/detached on primary process at runtime.

While there is the requirement from application that take
primary-secondary process model. The primary process work as a
resource management process, it will create/destroy virtual device
at runtime, while the secondary process deal with the network stuff
with these devices.

Solution:
=========

So the orignial intention is to fix this gap, but beyond that
the patch set provide a more comprehesive solution to handle
different hotplug cases in multi-process situation, it cover below
scenario:

1. Attach a share device from primary
2. Detach a share device from primary
3. Attach a share device from secondary
4. Detach a share device from secondary
5. Attach a private device from secondary
6. Detach a private device from secondary
7. Detach a share device from secondary privately
8. Attach a share device from secondary privately

In primary-secondary process model, we assume ethernet devices are
shared by default. that means attach or detach a device on any process
will broadcast to all other processes through mp channel then device
information will be synchronized on all processes.

Any failure during attaching process will cause inconsistent status
between processes, so proper rollback action should be considered.
Also, it is not safe to detach a share device when other process still
use it, so a handshake mechanism is introduced.

Scenario for Case 1, 2:

attach device from primary
a) primary attach the new device if failed goto h).
b) primary send attach sync request to all secondary.
c) secondary receive request and attach device and send reply.
d) primary check the reply if all success go to i).
e) primary send attach rollback sync request to all secondary.
f) secondary receive the request and detach device and send reply.
g) primary receive the reply and detach device as rollback action.
h) attach fail
i) attach success

detach device from primary
a) primary perform pre-detach check, if device is locked, goto i).
b) primary send pre-detach sync request to all secondary.
c) secondary perform pre-detach check and send reply.
d) primary check the reply if any fail goto i).
e) primary send detach sync request to all secondary
f) secondary detach the device and send reply (assume no fail)
g) primary detach the device.
h) detach success
i) detach failed

Scenario for case 3, 4:

attach device from secondary:
a) seconary send asycn request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and attach the new device if failed
   goto i).
c) primary forward attach request to all secondary as async request
   (because this in mp thread context, use sync request will deadlock,
    same reason for all following async request.)
d) secondary receive request and attach device and send reply.
e) primary check the reply if all success go to j).
f) primary send attach rollback async request to all secondary.
g) secondary receive the request and detach device and send reply.
h) primary receive the reply and detach device as rollback action.
i) send fail response to secondary, goto k).
j) send success response to secondary.
k) secondary process receive response and return.
 
detach device from secondary:
a) secondary send async request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and  perform pre-detach check, if device
   is locked, goto j).
c) primary send pre-detach async request to all secondary.
d) secondary perform pre-detach check and send reply.
e) primary check the reply if any fail goto j).
f) primary send detach async request to all secondary
g) secondary detach the device and send reply
h) primary detach the device.
i) send success response to secondary, goto k).
j) send fail response to secondary.
k) secondary process receive response and return.

Case 5, 6:
Secondary process can attach private device which only visible to
itself, in this case no IPC is involved, primary process is not allowed
to have private device so far.

Case 7, 8:
Secondary process can also temporally to detach a share device
"privately" then attach it back later, this action also not impact other
processes.

APIs chenages:
==============

rte_eth_dev_attach and rte_eth_dev_attach are extended to support
share device attach/detach in primary-secondary process model, it will
be called in case 1,2,3,4.

New API rte_eth_dev_attach_private and rte_eth_dev_detach_private are
introduced to cover case 5,6,7,8, this API can only be invoked in
secondary process.

New API rte_eth_dev_lock and rte_eth_dev_unlock are introduced to let
application lock or unlock on specific ethdev, a locked device
can't be detached. This help applicaiton to prevent unexpected
device detaching, especially in multi-process envrionment.
Aslo the new API let application to register a callback function
which will be invoked before a device is going to be detached,
the return value of the function will decide if device will continue
be detached or not, this support application to do condition check
at runtime.

PMD Impact:
===========

Currently device removing is not handled well in secondary process on
most pmd drivers, rte_eth_dev_relase_port will be invoked and will mess up
primary process since it reset all shared data. So we introduced new API
rte_eth_dev_release_port_local which only reset ethdev's state to unsued
but not touch shared data so other process will not be impacted.
Since not all device driver is target to support primary-secondary
process model, so the patch set only fix this on all Intel devices and
vdev, it can be refereneced by other driver when equevalent fix is
required

Limitation:
===========

1. The solution does not cover the case that primary process exit while
   secondary processes still be active. Though this is not a typial use
   case, but if this happens:
   a. secondary process can't attach / detach any shared device since no
      primary exist.
   b. secondary process still can attach / detach private device.
   c. secondary process still can detach a share device privately but may
      not attach it back, that ethdev slot will become zombie slot.

2. So for, for PCI bus, case 5,6 is not supported. PCI bus scan/probe
   mechanism can be improved to support attach private device on secondary
   process, but this is not the scope of this patchset.

Example:
========

The patchset also contains a example to demonstrate device hotplug
in multi-process model, below are detail instructions.

/* start sample code as primary then secondary */
./hotplug_mp --proc-type=auto

Command Line Example:

>help
>list

/* attach a af_packet vdev */
>attach net_af_packet,iface=eth0

/* detach port 0 */
>detach 0

/* attach a private af_packet vdev (secondary process only)*/
>attachp net_af_packet,iface=eth0

/* detach a private device (secondary process only) */
>detachp 0

/* lock port 0 */
>lock 0

/* unlock port 0 */
>unlock 0

Qi Zhang (19):
  ethdev: add function to release port in local process
  eal: enable multi process init callback
  ethdev: enable hotplug on multi-process
  ethdev: introduce device lock
  ethdev: support attach or detach share device from secondary
  ethdev: support attach private device as first
  net/i40e: enable port detach on secondary process
  net/ixgbe: enable port detach on secondary process
  net/af_packet: enable port detach on secondary process
  net/bonding: enable port detach on secondary process
  net/kni: enable port detach on secondary process
  net/null: enable port detach on secondary process
  net/octeontx: enable port detach on secondary process
  net/pcap: enable port detach on secondary process
  net/softnic: enable port detach on secondary process
  net/tap: enable port detach on secondary process
  net/vhost: enable port detach on secondary process
  examples/multi_process: add hotplug sample
  doc: update release notes for multi process hotplug

 doc/guides/rel_notes/release_18_08.rst       |  21 ++
 drivers/net/af_packet/rte_eth_af_packet.c    |  11 +
 drivers/net/bonding/rte_eth_bond_pmd.c       |  11 +
 drivers/net/i40e/i40e_ethdev.c               |   2 +
 drivers/net/ixgbe/ixgbe_ethdev.c             |   3 +
 drivers/net/kni/rte_eth_kni.c                |  11 +
 drivers/net/null/rte_eth_null.c              |  16 +-
 drivers/net/octeontx/octeontx_ethdev.c       |  16 ++
 drivers/net/pcap/rte_eth_pcap.c              |  15 +-
 drivers/net/softnic/rte_eth_softnic.c        |  19 +-
 drivers/net/tap/rte_eth_tap.c                |  17 +-
 drivers/net/vhost/rte_eth_vhost.c            |  11 +
 examples/multi_process/Makefile              |   1 +
 examples/multi_process/hotplug_mp/Makefile   |  23 ++
 examples/multi_process/hotplug_mp/commands.c | 356 +++++++++++++++++++++++
 examples/multi_process/hotplug_mp/commands.h |  10 +
 examples/multi_process/hotplug_mp/main.c     |  41 +++
 lib/librte_eal/common/eal_common_proc.c      |  51 +++-
 lib/librte_eal/common/eal_private.h          |   5 +
 lib/librte_eal/common/include/rte_eal.h      |  34 +++
 lib/librte_eal/linuxapp/eal/eal.c            |   2 +
 lib/librte_ethdev/Makefile                   |   2 +
 lib/librte_ethdev/ethdev_lock.c              | 140 +++++++++
 lib/librte_ethdev/ethdev_lock.h              |  31 ++
 lib/librte_ethdev/ethdev_mp.c                | 415 +++++++++++++++++++++++++++
 lib/librte_ethdev/ethdev_mp.h                |  42 +++
 lib/librte_ethdev/ethdev_private.h           |  42 +++
 lib/librte_ethdev/meson.build                |   2 +
 lib/librte_ethdev/rte_ethdev.c               | 312 ++++++++++++++++++--
 lib/librte_ethdev/rte_ethdev.h               | 169 +++++++++++
 lib/librte_ethdev/rte_ethdev_core.h          |   5 +
 lib/librte_ethdev/rte_ethdev_driver.h        |  13 +
 lib/librte_ethdev/rte_ethdev_pci.h           |   3 +
 33 files changed, 1815 insertions(+), 37 deletions(-)
 create mode 100644 examples/multi_process/hotplug_mp/Makefile
 create mode 100644 examples/multi_process/hotplug_mp/commands.c
 create mode 100644 examples/multi_process/hotplug_mp/commands.h
 create mode 100644 examples/multi_process/hotplug_mp/main.c
 create mode 100644 lib/librte_ethdev/ethdev_lock.c
 create mode 100644 lib/librte_ethdev/ethdev_lock.h
 create mode 100644 lib/librte_ethdev/ethdev_mp.c
 create mode 100644 lib/librte_ethdev/ethdev_mp.h
 create mode 100644 lib/librte_ethdev/ethdev_private.h
  
Qi Zhang July 12, 2018, 1:14 a.m. UTC | #4
v13:
- Since rte_eth_dev_attach/rte_eth_dev_detach will be deprecated,
  so, modify the sample code to use rte_eal_hotplug_add and
  rte_eal_hotplug_remove to attach/detach device.

v12:
- fix return value in eal_dev_hotplug_request_to_primary.
- add more error log in rte_eal_hotplug_add.
- fix return value in rte_eal_hotplug_add and rte_eal_hotplug_remove
  any failure due to IPC error will return -ENOMSG, but not -1.
- remove unnecessary changes from previous rework.

v11:
- move out common code from pci_vfio_unmap_secondary and
  pci_vfio_unmap_primary.
- move RTE_BUS_NAME_MAX_LEN and RTE_DEV_ARGS_MAX_LEN into hotplug_mp.h
- fix reply check in eal_dev_hotplug_request_to_primary.
- move skeleton code for attaching device from secondary from patch 6/19
  to patch 5/19 to improve code readability.

v10:
- Since hotplug add/remove a vdev on a secondary process will sync on
  all processes now, it is not necessary to support private vdev for
  a secondary process which is identified by a not-NULL devargs in
  "--vdev". So re-work on all vdev driver changes to simpified device
  probe scenario on a secondary process, devargs will be ignored on
  secondary process now.
- fix lisence header in example/multi-process/hotplug_mp/Makefile.

v9:
- Move hotplug IPC from rte_eth_dev_attach/rte_eth_dev_detach to
  eal_dev_hotplug_add and eal_dev_hotplug_remove, now all kinds of
  devices will be synced in multi-process.
- Fix couple issue when a device is bound to vfio.
  1) The device can't be detached clearly in a secondary process, which
     also cause it can't be attached again, due to the error that
     /dev/vfio/<group_fd> is still busy.(see Patch 3/19 and 4/19)
  2) repeat detach/attach device will cause "cannot find TAILQ entry
     for PCI device" due to incorrect PCI address compare.
     (see patch 2/19).
- Removed device lock.
- Removed private device support.
- Fix commit log grammar issue

v8:
- update rte_eal_version.map due to new API added.
- minor reword on release note.
- minor fix on commit log and code style.

NOTE:
  Some issues which is not related with this patchset is expected when
  play with hotplug_mp sample as belows.

- Attach a PCI device twice may cause device can't be detached
  below fix is required:
  https://patches.dpdk.org/patch/42030/

- ixgbe device can't detached, below fix is required
  https://patches.dpdk.org/patch/42031/

v7:
- update rte_ethdev_version.map for new APIs.
- improve code readability in __handle_secondary_request by use goto.
- add comments to explain why need to call rte_eal_alarm_set.
- add error log when process_mp_init_callbacks failed.
- reword release notes base on Anatoly's suggestion.
- add back previous "Acked-by" and "Reviewed-by" in commit log.

  NOTE: current patchset depends on below IPC fix, or it may not be able
  to attach a shared vdev.
  https://patches.dpdk.org/patch/41647/

v6:
- remove bus->scan_one, since ABI break is not necessary.
- remove patch for failsafe PMD since it will not support secondary.
- fix wrong implemenation on ixgbe.
- add rte_eth_dev_release_port_private into rte_eth_dev_pci_generic_remove for
  secondary process, so we don't need to patch on PMD if PMD use the
  default remove function.
- add release notes update.
- agreed to use strdup(peer) as workaround for repling a sync request in seperate
  thread.

v5:
- since we will keep mp thread separate from interrupt thread,
  it is not necessary to use temporary thread, we use rte_eal_alarm_set.
- remove the change in rte_eth_dev_release_port, since there is a better
  way to prevent rte_eth_dev_release_port be called after
  rte_eth_dev_release_port_private.
- fix the issue that lock does not take effect on secondary due to
  previous re-work
- fix the issue when the first attached device is a private device from
  secondary. (patch 8/24)
- work around for reply a sync request in separate thread, this is still
  an open and in discussion as below.
  https://mails.dpdk.org/archives/dev/2018-June/105359.html

v4:
- since mp thread will be merged to interrupt thread, the fix on v3
  for sync IPC deadlock will not work. the new version enable the
  machanism to invoke a mp action callback in a temporary thread to
  avoid the IPC deadlock, with this, secondary to primary request
  impelemtation also be simplified, since we can use sync request
  directly in a separate thread.

v3:
- enable mp init callback register to help non-eal module to initialize
  mp channel during rte_eal_init
- fix when attach share device from secondary.
  1) dead lock due to sync IPC be invoked in rte_malloc in primary
     process when handle secondary request to attach device, the
     solution is primary process to issue share device attach/detach
     in interrupt thread.
  2) return port_id not correct.
- check nb_sent and nb_received in sync IPC.
- fix memory leak duirng error handling at attach_on_secondary.
- improve clean_lock_callback to only lock/unlock spinlock once
- improve error code return in check-reply during async IPC.
- remove rte_ prefix of internal function in ethdev_mp.c
- sample code improvement.
  1) rename sample to "hotplug_mp", and move to example/multi-process.
  2) cleanup header include.
  3) call rte_eal_cleanup before exit.

v2:
- rename rte_ethdev_mp.* to ethdev_mp.*
- rename rte_ethdev_lock.* to ethdev_lock.*
- move internal funciton to ethdev_private.h
- separate rte_eth_dev_[un]lock into rte_eth_dev_[un]lock and
  rte_eth_dev_[un]lock_with_callback
- lock callbacks will be removed automatically after device is detached.
- add experimental tag for all new APIs.
- fix coding style issue.
- fix wrong lisence header in sample code.
- fix spelling 
- fix meson.build.
- improve comments. 

Background:
===========

Currently secondary process will only sync ethdev from primary
process at init stage, but it will not be aware if device
is attached/detached on primary process at runtime.

While there is the requirement from application that take
primary-secondary process model. The primary process work as a
resource management process, it will create/destroy virtual device
at runtime, while the secondary process deal with the network stuff
with these devices.

Solution:
=========

So the orignial intention is to fix this gap, but beyond that
the patch set provide a more comprehesive solution to handle
different hotplug cases in multi-process situation, it cover below
scenario:

1. Attach a device from the primary
2. Detach a device from the primary
3. Attach a device from a secondary
4. Detach a device from a secondary

In primary-secondary process model, we assume ethernet devices are
shared by default. that means attach or detach a device on any process
will broadcast to all other processes through mp channel then device
information will be synchronized on all processes.

Any failure during attaching or detaching process will cause inconsistent
status between processes, so proper rollback action should be considered.

Scenario for Case 1, 2:

attach device from primary
a) primary attach the new device if failed goto h).
b) primary send attach sync request to all secondary.
c) secondary receive request and attach device and send reply.
d) primary check the reply if all success go to i).
e) primary send attach rollback sync request to all secondary.
f) secondary receive the request and detach device and send reply.
g) primary receive the reply and detach device as rollback action.
h) attach fail
i) attach success

detach device from primary
a) primary perform pre-detach check, if device is locked, goto i).
b) primary send pre-detach sync request to all secondary.
c) secondary perform pre-detach check and send reply.
d) primary check the reply if any fail goto i).
e) primary send detach sync request to all secondary
f) secondary detach the device and send reply (assume no fail)
g) primary detach the device.
h) detach success
i) detach failed

Scenario for case 3, 4:

attach device from secondary:
a) seconary send asycn request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and attach the new device if failed
   goto i).
c) primary forward attach request to all secondary as async request
   (because this in mp thread context, use sync request will deadlock,
    same reason for all following async request.)
d) secondary receive request and attach device and send reply.
e) primary check the reply if all success go to j).
f) primary send attach rollback async request to all secondary.
g) secondary receive the request and detach device and send reply.
h) primary receive the reply and detach device as rollback action.
i) send fail response to secondary, goto k).
j) send success response to secondary.
k) secondary process receive response and return.
 
detach device from secondary:
a) secondary send async request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and  perform pre-detach check, if device
   is locked, goto j).
c) primary send pre-detach async request to all secondary.
d) secondary perform pre-detach check and send reply.
e) primary check the reply if any fail goto j).
f) primary send detach async request to all secondary
g) secondary detach the device and send reply
h) primary detach the device.
i) send success response to secondary, goto k).
j) send fail response to secondary.
k) secondary process receive response and return.

APIs chenages:
==============

scope of rte_eal_hotplug_add and rte_eal_hotplug_remove is extended.
In primary-secondary process model, rte_eal_hotplug_add will guarantee
that device be attached on all processes, while rte_eal_hotplug_remove will
guarantee device be detached on all processes.


PMD Impact:
===========

Currently device removing is not handled well in secondary process on
most pmd drivers, rte_eth_dev_relase_port will be invoked and will mess up
primary process since it reset all shared data. So we introduced new API
rte_eth_dev_release_port_local which only reset ethdev's state to unsued
but not touch shared data so other process will not be impacted.
Since not all device driver is target to support primary-secondary
process model, so the patch set only fix this on all Intel devices and
vdev, it can be refereneced by other driver when equevalent fix is
required

Example:
========

The patchset also contains a example to demonstrate device hotplug
in multi-process model, below are detail instructions.

/* start sample code as primary then secondary */
./hotplug_mp --proc-type=auto

Command Line Example:

>help
>list

/* attach a pci device */
> attach 0000:81:00.0

/* detach the pci device */
> detach 0000:81:00.0

/* attach a vdev af_packet device */
> attach net_af_packet,iface=eth0

/* detach the vdev af_packet device */
> detach net_af_packet

Qi Zhang (19):
  ethdev: add function to release port in local process
  bus/pci: fix PCI address compare
  bus/pci: enable vfio unmap resource for secondary
  vfio: remove uneccessary IPC for group fd clear
  eal: enable hotplug on multi-process
  eal: support attach or detach share device from  secondary
  net/i40e: enable hotplug on secondary process
  net/ixgbe: enable hotplug on secondary process
  net/af_packet: enable hotplug on secondary process
  net/bonding: enable hotplug on secondary process
  net/kni: enable hotplug on secondary process
  net/null: enable hotplug on secondary process
  net/octeontx: enable hotplug on secondary process
  net/pcap: enable hotplug on secondary process
  net/softnic: enable hotplug on secondary process
  net/tap: enable hotplug on secondary process
  net/vhost: enable hotplug on secondary process
  examples/multi_process: add hotplug sample
  doc: update release notes for multi process hotplug

 doc/guides/rel_notes/release_18_08.rst         |  11 +
 drivers/bus/pci/linux/pci_vfio.c               | 129 +++++++--
 drivers/net/af_packet/rte_eth_af_packet.c      |   7 +-
 drivers/net/bonding/rte_eth_bond_pmd.c         |   7 +-
 drivers/net/i40e/i40e_ethdev.c                 |   2 +
 drivers/net/ixgbe/ixgbe_ethdev.c               |   3 +
 drivers/net/kni/rte_eth_kni.c                  |   7 +-
 drivers/net/null/rte_eth_null.c                |  12 +-
 drivers/net/octeontx/octeontx_ethdev.c         |   9 +
 drivers/net/pcap/rte_eth_pcap.c                |  11 +-
 drivers/net/softnic/rte_eth_softnic.c          |  15 +-
 drivers/net/tap/rte_eth_tap.c                  |  13 +-
 drivers/net/vhost/rte_eth_vhost.c              |   7 +-
 examples/multi_process/Makefile                |   1 +
 examples/multi_process/hotplug_mp/Makefile     |  23 ++
 examples/multi_process/hotplug_mp/commands.c   | 214 +++++++++++++++
 examples/multi_process/hotplug_mp/commands.h   |  10 +
 examples/multi_process/hotplug_mp/main.c       |  41 +++
 lib/librte_eal/bsdapp/eal/Makefile             |   1 +
 lib/librte_eal/common/eal_common_dev.c         | 177 ++++++++++++-
 lib/librte_eal/common/eal_private.h            |  37 +++
 lib/librte_eal/common/hotplug_mp.c             | 348 +++++++++++++++++++++++++
 lib/librte_eal/common/hotplug_mp.h             |  48 ++++
 lib/librte_eal/common/include/rte_dev.h        |   6 +
 lib/librte_eal/common/meson.build              |   1 +
 lib/librte_eal/linuxapp/eal/Makefile           |   1 +
 lib/librte_eal/linuxapp/eal/eal.c              |   6 +
 lib/librte_eal/linuxapp/eal/eal_vfio.c         |  45 +---
 lib/librte_eal/linuxapp/eal/eal_vfio.h         |   1 -
 lib/librte_eal/linuxapp/eal/eal_vfio_mp_sync.c |   8 -
 lib/librte_ethdev/rte_ethdev.c                 |  12 +
 lib/librte_ethdev/rte_ethdev_driver.h          |  16 +-
 lib/librte_ethdev/rte_ethdev_pci.h             |   8 +
 33 files changed, 1134 insertions(+), 103 deletions(-)
 create mode 100644 examples/multi_process/hotplug_mp/Makefile
 create mode 100644 examples/multi_process/hotplug_mp/commands.c
 create mode 100644 examples/multi_process/hotplug_mp/commands.h
 create mode 100644 examples/multi_process/hotplug_mp/main.c
 create mode 100644 lib/librte_eal/common/hotplug_mp.c
 create mode 100644 lib/librte_eal/common/hotplug_mp.h
  
Qi Zhang July 12, 2018, 1:18 a.m. UTC | #5
v13:
- Since rte_eth_dev_attach/rte_eth_dev_detach will be deprecated,
  so, modify the sample code to use rte_eal_hotplug_add and
  rte_eal_hotplug_remove to attach/detach device.

v12:
- fix return value in eal_dev_hotplug_request_to_primary.
- add more error log in rte_eal_hotplug_add.
- fix return value in rte_eal_hotplug_add and rte_eal_hotplug_remove
  any failure due to IPC error will return -ENOMSG, but not -1.
- remove unnecessary changes from previous rework.

v11:
- move out common code from pci_vfio_unmap_secondary and
  pci_vfio_unmap_primary.
- move RTE_BUS_NAME_MAX_LEN and RTE_DEV_ARGS_MAX_LEN into hotplug_mp.h
- fix reply check in eal_dev_hotplug_request_to_primary.
- move skeleton code for attaching device from secondary from patch 6/19
  to patch 5/19 to improve code readability.

v10:
- Since hotplug add/remove a vdev on a secondary process will sync on
  all processes now, it is not necessary to support private vdev for
  a secondary process which is identified by a not-NULL devargs in
  "--vdev". So re-work on all vdev driver changes to simpified device
  probe scenario on a secondary process, devargs will be ignored on
  secondary process now.
- fix lisence header in example/multi-process/hotplug_mp/Makefile.

v9:
- Move hotplug IPC from rte_eth_dev_attach/rte_eth_dev_detach to
  eal_dev_hotplug_add and eal_dev_hotplug_remove, now all kinds of
  devices will be synced in multi-process.
- Fix couple issue when a device is bound to vfio.
  1) The device can't be detached clearly in a secondary process, which
     also cause it can't be attached again, due to the error that
     /dev/vfio/<group_fd> is still busy.(see Patch 3/19 and 4/19)
  2) repeat detach/attach device will cause "cannot find TAILQ entry
     for PCI device" due to incorrect PCI address compare.
     (see patch 2/19).
- Removed device lock.
- Removed private device support.
- Fix commit log grammar issue

v8:
- update rte_eal_version.map due to new API added.
- minor reword on release note.
- minor fix on commit log and code style.

NOTE:
  Some issues which is not related with this patchset is expected when
  play with hotplug_mp sample as belows.

- Attach a PCI device twice may cause device can't be detached
  below fix is required:
  https://patches.dpdk.org/patch/42030/

- ixgbe device can't detached, below fix is required
  https://patches.dpdk.org/patch/42031/

v7:
- update rte_ethdev_version.map for new APIs.
- improve code readability in __handle_secondary_request by use goto.
- add comments to explain why need to call rte_eal_alarm_set.
- add error log when process_mp_init_callbacks failed.
- reword release notes base on Anatoly's suggestion.
- add back previous "Acked-by" and "Reviewed-by" in commit log.

  NOTE: current patchset depends on below IPC fix, or it may not be able
  to attach a shared vdev.
  https://patches.dpdk.org/patch/41647/

v6:
- remove bus->scan_one, since ABI break is not necessary.
- remove patch for failsafe PMD since it will not support secondary.
- fix wrong implemenation on ixgbe.
- add rte_eth_dev_release_port_private into rte_eth_dev_pci_generic_remove for
  secondary process, so we don't need to patch on PMD if PMD use the
  default remove function.
- add release notes update.
- agreed to use strdup(peer) as workaround for repling a sync request in seperate
  thread.

v5:
- since we will keep mp thread separate from interrupt thread,
  it is not necessary to use temporary thread, we use rte_eal_alarm_set.
- remove the change in rte_eth_dev_release_port, since there is a better
  way to prevent rte_eth_dev_release_port be called after
  rte_eth_dev_release_port_private.
- fix the issue that lock does not take effect on secondary due to
  previous re-work
- fix the issue when the first attached device is a private device from
  secondary. (patch 8/24)
- work around for reply a sync request in separate thread, this is still
  an open and in discussion as below.
  https://mails.dpdk.org/archives/dev/2018-June/105359.html

v4:
- since mp thread will be merged to interrupt thread, the fix on v3
  for sync IPC deadlock will not work. the new version enable the
  machanism to invoke a mp action callback in a temporary thread to
  avoid the IPC deadlock, with this, secondary to primary request
  impelemtation also be simplified, since we can use sync request
  directly in a separate thread.

v3:
- enable mp init callback register to help non-eal module to initialize
  mp channel during rte_eal_init
- fix when attach share device from secondary.
  1) dead lock due to sync IPC be invoked in rte_malloc in primary
     process when handle secondary request to attach device, the
     solution is primary process to issue share device attach/detach
     in interrupt thread.
  2) return port_id not correct.
- check nb_sent and nb_received in sync IPC.
- fix memory leak duirng error handling at attach_on_secondary.
- improve clean_lock_callback to only lock/unlock spinlock once
- improve error code return in check-reply during async IPC.
- remove rte_ prefix of internal function in ethdev_mp.c
- sample code improvement.
  1) rename sample to "hotplug_mp", and move to example/multi-process.
  2) cleanup header include.
  3) call rte_eal_cleanup before exit.

v2:
- rename rte_ethdev_mp.* to ethdev_mp.*
- rename rte_ethdev_lock.* to ethdev_lock.*
- move internal funciton to ethdev_private.h
- separate rte_eth_dev_[un]lock into rte_eth_dev_[un]lock and
  rte_eth_dev_[un]lock_with_callback
- lock callbacks will be removed automatically after device is detached.
- add experimental tag for all new APIs.
- fix coding style issue.
- fix wrong lisence header in sample code.
- fix spelling 
- fix meson.build.
- improve comments. 

Background:
===========

Currently secondary process will only sync ethdev from primary
process at init stage, but it will not be aware if device
is attached/detached on primary process at runtime.

While there is the requirement from application that take
primary-secondary process model. The primary process work as a
resource management process, it will create/destroy virtual device
at runtime, while the secondary process deal with the network stuff
with these devices.

Solution:
=========

So the orignial intention is to fix this gap, but beyond that
the patch set provide a more comprehesive solution to handle
different hotplug cases in multi-process situation, it cover below
scenario:

1. Attach a device from the primary
2. Detach a device from the primary
3. Attach a device from a secondary
4. Detach a device from a secondary

In primary-secondary process model, we assume ethernet devices are
shared by default. that means attach or detach a device on any process
will broadcast to all other processes through mp channel then device
information will be synchronized on all processes.

Any failure during attaching or detaching process will cause inconsistent
status between processes, so proper rollback action should be considered.

Scenario for Case 1, 2:

attach device from primary
a) primary attach the new device if failed goto h).
b) primary send attach sync request to all secondary.
c) secondary receive request and attach device and send reply.
d) primary check the reply if all success go to i).
e) primary send attach rollback sync request to all secondary.
f) secondary receive the request and detach device and send reply.
g) primary receive the reply and detach device as rollback action.
h) attach fail
i) attach success

detach device from primary
a) primary perform pre-detach check, if device is locked, goto i).
b) primary send pre-detach sync request to all secondary.
c) secondary perform pre-detach check and send reply.
d) primary check the reply if any fail goto i).
e) primary send detach sync request to all secondary
f) secondary detach the device and send reply (assume no fail)
g) primary detach the device.
h) detach success
i) detach failed

Scenario for case 3, 4:

attach device from secondary:
a) seconary send asycn request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and attach the new device if failed
   goto i).
c) primary forward attach request to all secondary as async request
   (because this in mp thread context, use sync request will deadlock,
    same reason for all following async request.)
d) secondary receive request and attach device and send reply.
e) primary check the reply if all success go to j).
f) primary send attach rollback async request to all secondary.
g) secondary receive the request and detach device and send reply.
h) primary receive the reply and detach device as rollback action.
i) send fail response to secondary, goto k).
j) send success response to secondary.
k) secondary process receive response and return.
 
detach device from secondary:
a) secondary send async request to primary and wait on a condition
   which will be released by matched response from primary.
b) primary receive the request and  perform pre-detach check, if device
   is locked, goto j).
c) primary send pre-detach async request to all secondary.
d) secondary perform pre-detach check and send reply.
e) primary check the reply if any fail goto j).
f) primary send detach async request to all secondary
g) secondary detach the device and send reply
h) primary detach the device.
i) send success response to secondary, goto k).
j) send fail response to secondary.
k) secondary process receive response and return.

APIs chenages:
==============

scope of rte_eal_hotplug_add and rte_eal_hotplug_remove is extended.
In primary-secondary process model, rte_eal_hotplug_add will guarantee
that device be attached on all processes, while rte_eal_hotplug_remove will
guarantee device be detached on all processes.


PMD Impact:
===========

Currently device removing is not handled well in secondary process on
most pmd drivers, rte_eth_dev_relase_port will be invoked and will mess up
primary process since it reset all shared data. So we introduced new API
rte_eth_dev_release_port_local which only reset ethdev's state to unsued
but not touch shared data so other process will not be impacted.
Since not all device driver is target to support primary-secondary
process model, so the patch set only fix this on all Intel devices and
vdev, it can be refereneced by other driver when equevalent fix is
required

Example:
========

The patchset also contains a example to demonstrate device hotplug
in multi-process model, below are detail instructions.

/* start sample code as primary then secondary */
./hotplug_mp --proc-type=auto

Command Line Example:

>help
>list

/* attach a pci device */
> attach 0000:81:00.0

/* detach the pci device */
> detach 0000:81:00.0

/* attach a vdev af_packet device */
> attach net_af_packet,iface=eth0

/* detach the vdev af_packet device */
> detach net_af_packet

Qi Zhang (19):
  ethdev: add function to release port in local process
  bus/pci: fix PCI address compare
  bus/pci: enable vfio unmap resource for secondary
  vfio: remove uneccessary IPC for group fd clear
  eal: enable hotplug on multi-process
  eal: support attach or detach share device from  secondary
  net/i40e: enable hotplug on secondary process
  net/ixgbe: enable hotplug on secondary process
  net/af_packet: enable hotplug on secondary process
  net/bonding: enable hotplug on secondary process
  net/kni: enable hotplug on secondary process
  net/null: enable hotplug on secondary process
  net/octeontx: enable hotplug on secondary process
  net/pcap: enable hotplug on secondary process
  net/softnic: enable hotplug on secondary process
  net/tap: enable hotplug on secondary process
  net/vhost: enable hotplug on secondary process
  examples/multi_process: add hotplug sample
  doc: update release notes for multi process hotplug

 doc/guides/rel_notes/release_18_08.rst         |  11 +
 drivers/bus/pci/linux/pci_vfio.c               | 129 +++++++--
 drivers/net/af_packet/rte_eth_af_packet.c      |   7 +-
 drivers/net/bonding/rte_eth_bond_pmd.c         |   7 +-
 drivers/net/i40e/i40e_ethdev.c                 |   2 +
 drivers/net/ixgbe/ixgbe_ethdev.c               |   3 +
 drivers/net/kni/rte_eth_kni.c                  |   7 +-
 drivers/net/null/rte_eth_null.c                |  12 +-
 drivers/net/octeontx/octeontx_ethdev.c         |   9 +
 drivers/net/pcap/rte_eth_pcap.c                |  11 +-
 drivers/net/softnic/rte_eth_softnic.c          |  15 +-
 drivers/net/tap/rte_eth_tap.c                  |  13 +-
 drivers/net/vhost/rte_eth_vhost.c              |   7 +-
 examples/multi_process/Makefile                |   1 +
 examples/multi_process/hotplug_mp/Makefile     |  23 ++
 examples/multi_process/hotplug_mp/commands.c   | 214 +++++++++++++++
 examples/multi_process/hotplug_mp/commands.h   |  10 +
 examples/multi_process/hotplug_mp/main.c       |  41 +++
 lib/librte_eal/bsdapp/eal/Makefile             |   1 +
 lib/librte_eal/common/eal_common_dev.c         | 177 ++++++++++++-
 lib/librte_eal/common/eal_private.h            |  37 +++
 lib/librte_eal/common/hotplug_mp.c             | 348 +++++++++++++++++++++++++
 lib/librte_eal/common/hotplug_mp.h             |  48 ++++
 lib/librte_eal/common/include/rte_dev.h        |   6 +
 lib/librte_eal/common/meson.build              |   1 +
 lib/librte_eal/linuxapp/eal/Makefile           |   1 +
 lib/librte_eal/linuxapp/eal/eal.c              |   6 +
 lib/librte_eal/linuxapp/eal/eal_vfio.c         |  45 +---
 lib/librte_eal/linuxapp/eal/eal_vfio.h         |   1 -
 lib/librte_eal/linuxapp/eal/eal_vfio_mp_sync.c |   8 -
 lib/librte_ethdev/rte_ethdev.c                 |  12 +
 lib/librte_ethdev/rte_ethdev_driver.h          |  16 +-
 lib/librte_ethdev/rte_ethdev_pci.h             |   8 +
 33 files changed, 1134 insertions(+), 103 deletions(-)
 create mode 100644 examples/multi_process/hotplug_mp/Makefile
 create mode 100644 examples/multi_process/hotplug_mp/commands.c
 create mode 100644 examples/multi_process/hotplug_mp/commands.h
 create mode 100644 examples/multi_process/hotplug_mp/main.c
 create mode 100644 lib/librte_eal/common/hotplug_mp.c
 create mode 100644 lib/librte_eal/common/hotplug_mp.h
  
Thomas Monjalon July 12, 2018, 8:30 a.m. UTC | #6
12/07/2018 03:14, Qi Zhang:
> v13:
> - Since rte_eth_dev_attach/rte_eth_dev_detach will be deprecated,
>   so, modify the sample code to use rte_eal_hotplug_add and
>   rte_eal_hotplug_remove to attach/detach device.

Yes, this is what I tried to explain you.

I think it is now too late for 18.08.
We see that this patchset deserves more reviews.
  
Qi Zhang July 12, 2018, 9:11 a.m. UTC | #7
> -----Original Message-----
> From: Thomas Monjalon [mailto:thomas@monjalon.net]
> Sent: Thursday, July 12, 2018 4:30 PM
> To: Zhang, Qi Z <qi.z.zhang@intel.com>
> Cc: dev@dpdk.org; Burakov, Anatoly <anatoly.burakov@intel.com>; Ananyev,
> Konstantin <konstantin.ananyev@intel.com>; Richardson, Bruce
> <bruce.richardson@intel.com>; Yigit, Ferruh <ferruh.yigit@intel.com>; Shelton,
> Benjamin H <benjamin.h.shelton@intel.com>; Vangati, Narender
> <narender.vangati@intel.com>
> Subject: Re: [dpdk-dev] [PATCH v12 00/19] enable hotplug on multi-process
> 
> 12/07/2018 03:14, Qi Zhang:
> > v13:
> > - Since rte_eth_dev_attach/rte_eth_dev_detach will be deprecated,
> >   so, modify the sample code to use rte_eal_hotplug_add and
> >   rte_eal_hotplug_remove to attach/detach device.
> 
> Yes, this is what I tried to explain you.
> 
> I think it is now too late for 18.08.

Understand, but probably patch 2,3,4 could be considered in 18.08, since they fix general issue but not just for hotplug mp.
What do you think?


> We see that this patchset deserves more reviews.
> 
>
  
Thomas Monjalon July 12, 2018, 9:21 a.m. UTC | #8
12/07/2018 11:11, Zhang, Qi Z:
> From: Thomas Monjalon [mailto:thomas@monjalon.net]
> > 12/07/2018 03:14, Qi Zhang:
> > > v13:
> > > - Since rte_eth_dev_attach/rte_eth_dev_detach will be deprecated,
> > >   so, modify the sample code to use rte_eal_hotplug_add and
> > >   rte_eal_hotplug_remove to attach/detach device.
> > 
> > Yes, this is what I tried to explain you.
> > 
> > I think it is now too late for 18.08.
> 
> Understand, but probably patch 2,3,4 could be considered in 18.08, since they fix general issue but not just for hotplug mp.
> What do you think?

Yes, you are right.

Please send a separate patchset and try to get reviews.
Gaetan, Anatoly, please review patches 2 and 3.